"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "gmemusage-0.2/resource.c" of archive gmemusage-0.2.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 /*
    2  * resource.c
    3  * Manage resources for gmemusage
    4  *
    5  * Copyright (C) 1997, 1998 by Raju Mathur.
    6  * Lots of code and ideas shamelessly stolen from xlockmore-3.0/resource.c
    7  *
    8  * See file COPYING (included in this distribution) for copyright information.
    9  */
   10 #include <X11/Xlib.h>
   11 #include <X11/Xutil.h>
   12 #include <X11/Xos.h>
   13 #include <X11/Xresource.h>
   14 #include <stdio.h>
   15 #include <string.h>
   16 #include <malloc.h>
   17 #include <stdlib.h>
   18 
   19 #include "common.h"
   20 #include "defaults.h"
   21 /*
   22  * Built-in defaults
   23  */
   24 #define DEF_FONT	"fixed"
   25 #define DEF_GEOMETRY	"320x400"
   26 #define DEF_NCOLORS	"3"
   27 #ifdef PASTEL_COLORS
   28 #define DEF_COLOR1	"maroon"
   29 #define DEF_COLOR2	"OliveDrab"
   30 #define DEF_COLOR3	"SlateBlue"
   31 #else
   32 #define DEF_COLOR1	"red"
   33 #define DEF_COLOR2	"green"
   34 #define DEF_COLOR3	"blue"
   35 #endif
   36 #define DEF_COLOR4	""
   37 #define DEF_COLOR5	""
   38 #define DEF_COLOR6	""
   39 #define DEF_COLOR7	""
   40 #define DEF_COLOR8	""
   41 #define DEF_COLOR9	""
   42 #define DEF_FOREGROUND	"black"
   43 #define DEF_BACKGROUND	"grey4"
   44 #define DEF_UPDATE	"5"
   45 #define DEF_THRESHHOLD	"400"
   46 #define DEF_CLASSNAME	"Gmemusage"
   47 
   48 static XrmOptionDescRec optionTable [] =
   49 {
   50    { "-geometry" , ".geometry" , XrmoptionSepArg , (caddr_t) NULL } ,
   51    { "-ncolors" , ".ncolors" , XrmoptionSepArg , (caddr_t) NULL } ,
   52    { "-color1" , ".color1" , XrmoptionSepArg , (caddr_t) NULL } ,
   53    { "-color2" , ".color2" , XrmoptionSepArg , (caddr_t) NULL } ,
   54    { "-color3" , ".color3" , XrmoptionSepArg , (caddr_t) NULL } ,
   55    { "-color4" , ".color4" , XrmoptionSepArg , (caddr_t) NULL } ,
   56    { "-color5" , ".color5" , XrmoptionSepArg , (caddr_t) NULL } ,
   57    { "-color6" , ".color6" , XrmoptionSepArg , (caddr_t) NULL } ,
   58    { "-color7" , ".color7" , XrmoptionSepArg , (caddr_t) NULL } ,
   59    { "-color8" , ".color8" , XrmoptionSepArg , (caddr_t) NULL } ,
   60    { "-color9" , ".color9" , XrmoptionSepArg , (caddr_t) NULL } ,
   61    { "-font" , ".font" , XrmoptionSepArg , (caddr_t) NULL } ,
   62    { "-foreground" , ".foreground" , XrmoptionSepArg , (caddr_t) NULL } ,
   63    { "-background" , ".background" , XrmoptionSepArg , (caddr_t) NULL } ,
   64    { "-update" , ".update" , XrmoptionSepArg , (caddr_t) NULL } ,
   65    { "-threshhold" , ".threshhold" , XrmoptionSepArg , (caddr_t) NULL } ,
   66 } ;
   67 const int
   68    optionEntries = sizeof ( optionTable ) / sizeof ( optionTable [0] ) ;
   69 
   70 static XrmOptionDescRec displayTable [] =
   71 {
   72    { "-display" , ".display" , XrmoptionSepArg , (caddr_t) NULL }
   73 } ;
   74 const int
   75    displayEntries = sizeof ( displayTable ) / sizeof ( displayTable [0] ) ;
   76 
   77 static XrmOptionDescRec nameTable [] =
   78 {
   79    { "-name" , ".name" , XrmoptionSepArg , (caddr_t) NULL }
   80 } ;
   81 const int
   82    nameEntries = sizeof ( nameTable ) / sizeof ( nameTable [0] ) ;
   83 /*
   84  * Variables holding default values
   85  */
   86 static char
   87    *dName ;
   88 char
   89    *dGeometry ,
   90    *dDisplay ,
   91    *dFont ,
   92    *dForeground ,
   93    *dBackground ,
   94    *dColor [MaxColors] ;
   95 int
   96    dnColors ,
   97    dUpdate ,
   98    dThreshhold ;
   99 
  100 #define t_String	0
  101 #define t_Int		1
  102 
  103 typedef struct {
  104     caddr_t    *var;
  105     char       *name;
  106     char       *class;
  107     char       *def;
  108     int         type;
  109 }           argtype;
  110 /*
  111  * Warning! If you change anything here do also modify the Help function
  112  * accordingly.
  113  */
  114 static argtype optionVars[] = {
  115     { (caddr_t *) &dFont , "font" , "Font" , DEF_FONT , t_String } ,
  116     { (caddr_t *) &dGeometry , "geometry" , "Geometry" , DEF_GEOMETRY , t_String } ,
  117     { (caddr_t *) &dnColors , "ncolors" , "NColors" , DEF_NCOLORS , t_Int } ,
  118     { (caddr_t *) &dColor [0] , "color1" , "Color" , DEF_COLOR1 , t_String } ,
  119     { (caddr_t *) &dColor [1] , "color2" , "Color" , DEF_COLOR2 , t_String } ,
  120     { (caddr_t *) &dColor [2] , "color3" , "Color" , DEF_COLOR3 , t_String } ,
  121     { (caddr_t *) &dColor [3] , "color4" , "Color" , DEF_COLOR4 , t_String } ,
  122     { (caddr_t *) &dColor [4] , "color5" , "Color" , DEF_COLOR5 , t_String } ,
  123     { (caddr_t *) &dColor [5] , "color6" , "Color" , DEF_COLOR6 , t_String } ,
  124     { (caddr_t *) &dColor [6] , "color7" , "Color" , DEF_COLOR7 , t_String } ,
  125     { (caddr_t *) &dColor [7] , "color8" , "Color" , DEF_COLOR8 , t_String } ,
  126     { (caddr_t *) &dColor [8] , "color9" , "Color" , DEF_COLOR9 , t_String } ,
  127     { (caddr_t *) &dForeground , "foreground" , "Foreground" , DEF_FOREGROUND ,
  128       t_String } ,
  129     { (caddr_t *) &dBackground , "background" , "Background" , DEF_BACKGROUND ,
  130       t_String } ,
  131     { (caddr_t *) &dUpdate , "update" , "Interval" , DEF_UPDATE , t_Int } ,
  132     { (caddr_t *) &dThreshhold , "threshhold" , "Threshhold" , DEF_THRESHHOLD ,
  133       t_Int } ,
  134 } ;
  135 const int
  136    nVars = sizeof ( optionVars ) / sizeof ( optionVars [0] ) ;
  137 /*
  138  * Print out the help message. This'll need to be kept up-to-date as and when
  139  * commandline options change.
  140  */
  141 static void
  142    Help ( void )
  143 {
  144    fprintf ( stderr , "gmemusage ver %s by Raju (OldMonk) Mathur\n"
  145 	     "Usage: %s [options]\n"
  146 	     "where options (defaults/resources in brackets) are:\n"
  147 	     "    -name resourcename  Class name to use for resources (%s) (name)\n"
  148 	     "    -display display    X server to contact ($DISPLAY) (display)\n"
  149 	     "    -geometry geometry  Initial window geometry (%s) (geometry)\n"
  150 	     "    -font font          Text font (%s) (font)\n"
  151 	     "    -background color   Color to use for window background (%s) (background)\n"
  152 	     "    -update seconds     Update interval (%s) (update)\n"
  153 	     "    -threshhold kb      Threshhold to merge small processes (%s) (threshhold)\n"
  154 	     "    -ncolors ncolors    How many colors to use (%s) (ncolors)\n"
  155 	     "    -color1 color       color 1 (%s) (color1)\n"
  156 	     "    -color2 color       color 2 (%s) (color2)\n"
  157 	     "    -color3 color       color 3 (%s) (color3)\n"
  158 	     "    -color[4-9] color   colors 4 to 9 (undefined) (color4-color9)\n"
  159 	     "    -help               Print this message\n" ,
  160 	     version , progname , DEF_CLASSNAME , DEF_GEOMETRY , DEF_FONT ,
  161 	     DEF_BACKGROUND , DEF_UPDATE , DEF_THRESHHOLD , DEF_NCOLORS ,
  162 	     DEF_COLOR1 , DEF_COLOR2 , DEF_COLOR3 ) ;
  163 }
  164 /*
  165  * Begin shameless thefts mentioned earlier. All I did was (1) remove the
  166  * extra cases for t_Float and t_Bool and (2) hit ESC C-q at the opening
  167  * brace in Xemacs for this function.
  168  */
  169 static void
  170 GetResource ( XrmDatabase database , char *parentname , char *parentclass ,
  171 	      char *name , char *class , int valueType , char *def ,
  172 	      caddr_t *valuep )
  173 {
  174    char       *type;
  175    XrmValue    value;
  176    char       *string;
  177    char        buffer[1024];
  178    char        fullname[1024];
  179    char        fullclass[1024];
  180    int         len;
  181 
  182    (void) sprintf(fullname, "%s.%s", parentname, name);
  183    (void) sprintf(fullclass, "%s.%s", parentclass, class);
  184    if (XrmGetResource(database, fullname, fullclass, &type, &value)) {
  185       string = value.addr;
  186       len = value.size;
  187    } else {
  188       string = def;
  189       len = strlen(string);
  190    }
  191    (void) strncpy(buffer, string, sizeof(buffer));
  192    buffer[sizeof(buffer) - 1] = '\0';
  193 
  194    switch (valueType) {
  195     case t_String:
  196     {
  197        char       *s = (char *) malloc(len + 1);
  198        if (s == (char *) NULL)
  199        {
  200 	  fprintf ( stderr ,"%s: GetResource - couldn't allocate memory" ,
  201 		    progname ) ;
  202 	  perror ( "" ) ;
  203 	  exit ( 1 ) ;
  204        }
  205        (void) strncpy(s, string, len);
  206        s[len] = '\0';
  207        *((char **) valuep) = s;
  208     }
  209     break;
  210 
  211     case t_Int:
  212      *((int *) valuep) = atoi(buffer);
  213      break;
  214    }
  215 }
  216 static XrmDatabase
  217    optionDB = NULL ,
  218    serverDB = NULL ,
  219    mergedDB = NULL ;
  220 /*
  221  * External interface to resources. Again, shamelessly stolen from
  222  * xlockmore-3.0 source. I don't want to do what xlockmore does: open the
  223  * display within the GetResource, so we'll break that function up into
  224  * two seperate pieces: the first one will get the "name" and "display"
  225  * resources, return ot the parent which will then open the display
  226  * and then call the second one which will get the rest of the resources.
  227  */
  228 void
  229 GetInitialResources ( int *argc , char **argv )
  230 {
  231    XrmDatabase
  232       nameDB = NULL ,
  233       displayDB = NULL ;
  234 
  235    XrmInitialize () ;
  236 /*
  237  * Get -name if we need to look for resources under another name.
  238  */
  239    XrmParseCommand ( &nameDB , nameTable , nameEntries , progname , argc ,
  240 		     argv ) ;
  241    GetResource ( nameDB , progname , "*" , "name" , "Name" , t_String ,
  242 		 DEF_CLASSNAME , &dName ) ;
  243    XrmParseCommand ( &displayDB , displayTable , displayEntries , progname ,
  244 		     argc , argv ) ;
  245    dDisplay = getenv ( "DISPLAY" ) ;
  246    if ( !dDisplay )
  247    {
  248       dDisplay = "" ;
  249    }
  250    GetResource ( displayDB , progname , dName , "display" , "Display" ,
  251 		 t_String , dDisplay , &dDisplay ) ;
  252 #if 0
  253    printf("name %s, display %s\n",dName,dDisplay);
  254 #endif
  255    XrmMergeDatabases ( nameDB , &mergedDB ) ;
  256    XrmMergeDatabases ( displayDB , &mergedDB ) ;
  257    return ;
  258 }
  259 /*
  260  * Now get the rest of the resources. This function will be called after the
  261  * display has been opened by the application. I'm not going to look in
  262  * ~/.Xdefaults or ~/.Xresources (presumably rdb has already set those values
  263  * in the display RDB) and not in .../app-defaults/... since we are not looking
  264  * at an app-default file for the app yet.
  265  *
  266  * So finally the databases we are left with are:
  267  *	- Server resources (presumably from rdb(1))
  268  *	- cmdline resources
  269  *	- environment (for DISPLAY)
  270  */
  271 void
  272    GetResources ( Display *display , int argc , char **argv )
  273 {
  274    char
  275       *serverString ;
  276    register int
  277       i ;
  278 
  279    serverString = XResourceManagerString ( display ) ;
  280    if ( serverString )
  281    {
  282       serverDB = XrmGetStringDatabase ( serverString ) ;
  283       (void) XrmMergeDatabases ( serverDB , &mergedDB ) ;
  284    }
  285    XrmParseCommand ( &optionDB , optionTable , optionEntries , progname ,
  286 		     &argc , argv ) ;
  287 /*
  288  * Now there shouldn't be any arguments left at all. If there are any,
  289  * they're errors and we should flag them.
  290  */
  291    if ( argc > 1 )		/* argv[0] still remains, presumably */
  292    {
  293       if ( strcmp ( argv [1] , "-help" ) )
  294       {
  295 	 fprintf ( stderr , "%s: unknown option %s\n" , progname , argv [1] ) ;
  296       }
  297       Help () ;
  298       exit ( 1 ) ;
  299    }
  300    XrmMergeDatabases ( optionDB , &mergedDB ) ;
  301    for ( i = 0 ; i < nVars ; i++ )
  302    {
  303       GetResource ( mergedDB , progname , dName , optionVars [i] . name ,
  304 		    optionVars [i] . class , optionVars [i] . type ,
  305 		    optionVars [i] . def , optionVars [i] . var ) ;
  306    }
  307    XrmDestroyDatabase ( mergedDB ) ;
  308    return ;
  309 }