home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1735 / xmon.c next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  10.8 KB  |  440 lines

  1. /*
  2.  *    Xmon : Graphically monitor a list
  3.  *
  4.  *    George Ferguson, ferguson@cs.rochester.edu,  4 May 1990.
  5.  *
  6.  *    $Id: xmon.c,v 1.4 90/08/21 11:14:39 ferguson Exp $
  7.  *
  8.  */
  9. static char *rcsid = "$Id: xmon.c,v 1.4 90/08/21 11:14:39 ferguson Exp $";
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <X11/Intrinsic.h>
  13. #include <X11/StringDefs.h>
  14. #include <X11/Xaw/Label.h>    
  15. #include <X11/Xaw/Toggle.h>    
  16. #include <X11/Xaw/Cardinals.h>    
  17. #include "app-resources.h"
  18. extern WidgetClass classNameToWidgetClass();
  19. extern char *getTextFromWidget();
  20.  
  21. /*    -    -    -    -    -    -    -    -    */
  22. /*
  23.  * Functions defined in this file:
  24.  */
  25. static void initGraphics(), initWidgets();
  26. static void quit(), update(), info(), reinit(), cd(), cmd();
  27. static void timeoutProc();
  28. static void syntax();
  29. void fail();
  30.  
  31. /*
  32.  * Action binding table
  33.  */
  34. static XtActionsRec cmdActionsTable[] = {
  35.     { "xmon-update", update },
  36.     { "xmon-quit", quit },
  37.     { "xmon-info", info },
  38.     { "xmon-reinit", reinit },
  39.     { "xmon-cd", cd },
  40.     { "xmon-cmd", cmd },
  41. };
  42.  
  43. /*
  44.  * Global widget data
  45.  */
  46. Widget toplevel,faceBox,infoLabel,radioGroup;
  47. AppResources appResources;
  48. static XtAppContext app_con;
  49. static XtIntervalId interval;
  50.  
  51. /*
  52.  * Global graphics data
  53.  */
  54. GC gc;
  55.  
  56. /*
  57.  * Other global data
  58.  */
  59. char *program;
  60.  
  61. /*
  62.  *    Non-widget resources obtained from resource manager
  63.  */
  64. static XtResource resources[] = {
  65.     { "addNames", "AddNames", XtRBoolean, sizeof(Boolean),
  66.       XtOffset(AppResources *,addNames), XtRImmediate, False },
  67.     { "opaqueNames", "OpaqueNames", XtRBoolean, sizeof(Boolean),
  68.       XtOffset(AppResources *,opaqueNames), XtRImmediate, False },
  69.     { "smallFont", "SmallFont", XtRFont, sizeof(Font),
  70.       XtOffset(AppResources *,smallFont), XtRString, SMALLFONT },
  71.     { "updateInterval", "UpdateInterval", XtRInt, sizeof(int),
  72.       XtOffset(AppResources *,updateInterval), XtRImmediate, (XtPointer)60 },
  73.     { "faceDirectory", "FaceDirectory", XtRString, sizeof(String),
  74.       XtOffset(AppResources *,faceDirectory), XtRImmediate, FACEDIR },
  75.     { "updateCommand", "UpdateCommand", XtRString, sizeof(String),
  76.       XtOffset(AppResources *,updateCommand), XtRImmediate, UPDATECOMMAND },
  77.     { "updateCursor", "UpdateCursor", XtRCursor, sizeof(Cursor),
  78.       XtOffset(AppResources *,updateCursor), XtRString, UPDATECURSOR },
  79.     { "nameCommand", "nameCommand", XtRString, sizeof(String),
  80.       XtOffset(AppResources *,nameCommand), XtRImmediate, NULL },
  81.     { "debug", "Debug", XtRBoolean, sizeof(Boolean),
  82.       XtOffset(AppResources *,debug), XtRImmediate, False },
  83.     { "revision", "Revision", XtRString, sizeof(String),
  84.       XtOffset(AppResources *,revision), XtRImmediate, "" },
  85.     { "widgets", "Widgets", XtRString, sizeof(String),
  86.       XtOffset(AppResources *,widgets), XtRImmediate, "" },
  87. };
  88.  
  89. /*
  90.  *    Non-widget resources settable on command line.
  91.  */
  92. static XrmOptionDescRec options[] = {
  93.     { "-addNames",    ".addNames",    XrmoptionNoArg,        "True" },
  94.     { "-opaqueNames",    ".opaqueNames",    XrmoptionNoArg,        "True" },
  95.     { "-smallFont",    ".smallFont",    XrmoptionSepArg,    SMALLFONT },
  96.     { "-updateInterval",".updateInterval",XrmoptionSepArg,    "60" },
  97.     { "-faceDirectory",    ".faceDirectory",XrmoptionSepArg,    FACEDIR },
  98.     { "-updateCommand",    ".updateCommand",XrmoptionSepArg,    UPDATECOMMAND},
  99.     { "-nameCommand",    ".nameCommand",    XrmoptionSepArg,    NULL},
  100.     { "-debug",        ".debug",    XrmoptionNoArg,        "True"},
  101. };
  102.  
  103. /*
  104.  *    Widget and non-widget resources if the application defaults
  105.  *    file can't be found.
  106.  *    [ Generated automatically from Xmon.ad. ]
  107.  */
  108. static String fallbackResources[] = {
  109. #include "Xmon.ad.h"
  110.     NULL
  111. };
  112.  
  113. /*    -    -    -    -    -    -    -    -    */
  114.  
  115. main(argc, argv)
  116. int argc;
  117. char **argv;
  118. {
  119.     program = *argv;
  120.     initGraphics(argc,argv);
  121.     initWidgets();
  122.     initMemory();
  123.     XtRealizeWidget(toplevel);
  124.     updateFaces();
  125.     if (appResources.updateInterval != 0)
  126.     interval = XtAppAddTimeOut(app_con,appResources.updateInterval*1000,
  127.                             timeoutProc,NULL);
  128.     XtAppMainLoop(app_con);
  129. }
  130.  
  131. static void
  132. initGraphics(argc,argv)
  133. int argc;
  134. char **argv;
  135. {
  136.     XGCValues gcvals;
  137.     char *rev,*strchr();
  138.  
  139.     toplevel = XtAppInitialize(&app_con, "Xmon",
  140.                    options, XtNumber(options),
  141.                    &argc,argv,fallbackResources,NULL,ZERO);
  142.     XtGetApplicationResources(toplevel,(XtPointer)&appResources,
  143.                               resources,XtNumber(resources),NULL,ZERO);
  144.     rev = strchr(fallbackResources[0],'$');
  145.     if (strcmp(appResources.revision,rev) != 0) {
  146.     fprintf(stderr,"%s: app-defaults release not %s\n",program,rev);
  147.     fprintf(stderr,"%s: you may have an outdated app-defaults file\n",
  148.                                 program);
  149.     }
  150.     XtAppAddActions(app_con,cmdActionsTable,XtNumber(cmdActionsTable));
  151.     if (argc != 1) {
  152.     syntax(argc,argv);
  153.     XtDestroyApplicationContext(app_con);
  154.     exit(1);
  155.     }
  156.     gc = XDefaultGC(XtDisplay(toplevel),
  157.                 XScreenNumberOfScreen(XtScreen(toplevel)));
  158.     XSetFont(XtDisplay(toplevel),gc,appResources.smallFont);
  159. }
  160.  
  161. static void
  162. initWidgets()
  163. {
  164.     char name[32],class[32],parent[32];
  165.     char *s;
  166.     int i;
  167.     Boolean isShell;
  168.     WidgetClass wc;
  169.     Widget w;
  170.  
  171.     if ((s=appResources.widgets) == NULL)
  172.     fail("no widgets specified!\n","");
  173.     while (*s) {
  174.         while (isspace(*s))
  175.             s += 1;
  176.     if (!*s)
  177.         break;
  178.         i = 0;
  179.         while (*s && !isspace(*s))
  180.             class[i++] = *s++;
  181.         class[i] = '\0';
  182.         while (isspace(*s))
  183.             s += 1;
  184.         i = 0;
  185.         while (*s && !isspace(*s))
  186.             name[i++] = *s++;
  187.         name[i] = '\0';
  188.         while (isspace(*s))
  189.             s += 1;
  190.         i = 0;
  191.         while (*s && !isspace(*s))
  192.             parent[i++] = *s++;
  193.         parent[i] = '\0';
  194.     isShell = False;
  195.         if ((wc=classNameToWidgetClass(class,&isShell)) == NULL)
  196.         fail("can't convert string \"%s\" to widgetClass\n",class);
  197.     if (strcmp(parent,"toplevel") == 0)
  198.         w = toplevel;
  199.     else if ((w=XtNameToWidget(toplevel,parent)) == NULL)
  200.         fail("can't convert string \"%s\" to widget\n",parent);
  201.     if (isShell)
  202.             w = XtCreatePopupShell(name,wc,w,NULL,ZERO);
  203.         else
  204.             w = XtCreateManagedWidget(name,wc,w,NULL,ZERO);
  205.     }
  206.     if ((faceBox=XtNameToWidget(toplevel,"*faceBox")) == NULL)
  207.     fail("didn't create widget \"faceBox\"\n","");
  208.     if ((infoLabel=XtNameToWidget(toplevel,"*infoLabel")) == NULL)
  209.     fail("didn't create widget \"infoLabel\"\n","");
  210. }
  211.  
  212. /*    -    -    -    -    -    -    -    -    */
  213. /* Action procedures */
  214.  
  215. /*
  216.  * Default action for "quit" button: destroys application.
  217.  */
  218. static void
  219. quit(w,event,params,num_params)
  220. Widget w;
  221. XEvent *event;
  222. String *params;
  223. Cardinal *num_params;
  224. {
  225.     XtDestroyApplicationContext(app_con);
  226.     exit(0);
  227. }
  228.  
  229. /*
  230.  * Default action for all "face" buttons: sets infoLabel.
  231.  */
  232. static void
  233. info(w,event,params,num_params)
  234. Widget w;
  235. XEvent *event;
  236. String *params;
  237. Cardinal *num_params;
  238. {
  239.     Arg arg[1];
  240.  
  241.     if ((w=(Widget)XawToggleGetCurrent(radioGroup)) == NULL)
  242.     XtSetArg(arg[0],XtNlabel,"");
  243.     else
  244.     XtSetArg(arg[0],XtNlabel,getTextFromWidget(w));
  245.     XtSetValues(infoLabel,arg,ONE);
  246. }
  247.  
  248. /*
  249.  * Default action for "update" button: Sets all buttons and resets timer.
  250.  */
  251. static void
  252. update(w,event,params,num_params)
  253. Widget w;
  254. XEvent *event;
  255. String *params;
  256. Cardinal *num_params;
  257. {
  258.     updateFaces();
  259.     if (appResources.updateInterval != NULL) {
  260.     XtRemoveTimeOut(interval);
  261.     interval = XtAppAddTimeOut(app_con,appResources.updateInterval*1000,
  262.                             timeoutProc,NULL);
  263.     }
  264. }
  265.  
  266. /*
  267.  * Forget the bitmaps
  268.  */
  269. static void
  270. reinit(w,event,params,num_params)
  271. Widget w;
  272. XEvent *event;
  273. String *params;
  274. Cardinal *num_params;
  275. {
  276.     if (radioGroup != NULL)
  277.     XawToggleUnsetCurrent(radioGroup);
  278.     clearBitmapCache();
  279.     update(w,event,params,num_params);
  280. }
  281.  
  282. /*
  283.  * Change directory
  284.  */
  285. static void
  286. cd(w,event,params,num_params)
  287. Widget w;
  288. XEvent *event;
  289. String *params;
  290. Cardinal *num_params;
  291. {
  292.     char *text,dirname[256];
  293.     int i;
  294.  
  295.     text = getTextFromWidget((Widget)XawToggleGetCurrent(radioGroup));
  296.     if (text == NULL)
  297.     return;
  298.     i = 0;
  299.     while (*text && !isspace(*text) && i < 255)
  300.     dirname[i++] = *text++;
  301.     dirname[i] = '\0';
  302.     if (chdir(dirname) < 0)
  303.     fprintf(stderr,"%s: couldn't cd to \"%s\"\n",program,dirname);
  304. }
  305.  
  306. /*
  307.  * General action function for user translations
  308.  *    Only test for a selection if the fields are needed.
  309.  */
  310. static char buf[1024];
  311.  
  312. static void cmd(w,event,params,num_params)
  313. Widget w;
  314. XEvent *event;
  315. String *params;
  316. Cardinal *num_params;
  317. {
  318.     char *s,*t,*text;
  319.     int i,field,f,seenDollar,hadBracket;
  320.  
  321.     if (*num_params < ONE)
  322.     return;
  323.     text = getTextFromWidget((Widget)XawToggleGetCurrent(radioGroup));
  324.     s = *params;
  325.     i = 0;
  326.     seenDollar = 0;
  327.     while (*s) {
  328.     if (*s == '$') {
  329.         if (seenDollar) {
  330.         buf[i++] = *s;
  331.         seenDollar = False;
  332.         } else {
  333.         seenDollar = True;
  334.         }
  335.         s += 1;
  336.     } else if (seenDollar && (*s == '{' || isdigit(*s))) {
  337.         if (text == NULL)
  338.         return;
  339.         field = 0;
  340.         if ((hadBracket = (*s == '{')))
  341.         s += 1;
  342.         while (*s && isdigit(*s))
  343.         field = field * 10 + *s++ - '0';
  344.         if (hadBracket) {
  345.         if (*s != '}') {
  346.             fprintf(stderr,"bad field in cmd: \"%s\"\n",*params);
  347.             return;
  348.         } else
  349.             s += 1;
  350.         }
  351.         if (field == 0) {
  352.         t = text;
  353.         while (*t)
  354.             buf[i++] = *t++;
  355.         } else {
  356.         t = text;
  357.         f = 1;
  358.         while (*t && f < field)
  359.             if (isspace(*t)) {
  360.             while (*t && isspace(*t))
  361.                 t += 1;
  362.             f += 1;
  363.             } else
  364.             t += 1;
  365.         if (*t != '\0')
  366.             while (*t && !isspace(*t))
  367.             buf[i++] = *t++;
  368.         }
  369.         seenDollar = False;
  370.     } else {
  371.         if (seenDollar) {
  372.         buf[i++] = '$';
  373.         seenDollar = False;
  374.         }
  375.         buf[i++] = *s++;
  376.     }
  377.     }
  378.     if (seenDollar)
  379.     buf[i++] = '$';        /* final $ if needed */
  380.     buf[i] = '\0';
  381.     if (appResources.debug)
  382.     printf("cmd: \"%s\"\n",buf);
  383.     else
  384.     system(buf);
  385. }
  386.  
  387. /*    -    -    -    -    -    -    -    -    */
  388. /*
  389.  * Callback for timer: Sets all buttons and schedules next update.
  390.  */
  391. static void
  392. timeoutProc(client_data,id)
  393. XtPointer client_data;
  394. XtIntervalId *id;
  395. {
  396.     updateFaces();
  397.     interval = XtAppAddTimeOut(app_con,appResources.updateInterval*1000,
  398.                             timeoutProc,NULL);
  399. }
  400.  
  401. /*    -    -    -    -    -    -    -    -    */
  402. /*
  403.  *    syntax() : print the usage message.
  404.  */
  405. static void
  406. syntax(argc,argv)
  407. int argc;
  408. char **argv;
  409. {
  410.     argv += 1;
  411.     if (argc == 2)
  412.     fprintf(stderr,"%s: bad argument: %s\n",program,*argv);
  413.     else {
  414.     fprintf(stderr,"%s: bad arguments: ",program);
  415.     while (--argc)
  416.         fprintf(stderr,"%s ",*argv++);
  417.     fprintf(stderr,"\n");
  418.     }
  419.     fprintf(stderr,"usage: %s\t-addNames\t\tadd labels to icons\n",program);
  420.     fprintf(stderr,"\t\t-opaqueNames\t\tprint labels opaquely\n");
  421.     fprintf(stderr,"\t\t-updateInterval N\tseconds between updates\n");
  422.     fprintf(stderr,"\t\t-faceDirectory dir\tlocation of icons\n");
  423.     fprintf(stderr,"\t\t-updateCommand cmd\tcommand to monitor\n");
  424.     fprintf(stderr,"\t\t-nameCommand cmd\tcommand to map names to icons\n");
  425.     fprintf(stderr,"\t\t-debug\t\t\tprint command instead of executing\n");
  426. }
  427.  
  428. /*
  429.  *    fail() : Print a message and die.
  430.  */
  431. void
  432. fail(fmt,arg)
  433. char *fmt,*arg;
  434. {
  435.     fprintf(stderr,fmt,arg);
  436.     XtDestroyApplicationContext(app_con);
  437.     exit(1);
  438. }
  439.  
  440.