"Fossies" - the Fresh Open Source Software Archive

Member "procmeter3-3.6+svn387/procmeterrc.c" (24 Dec 2010, 9797 Bytes) of package /linux/misc/procmeter3-3.6+svn387.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "procmeterrc.c" see the Fossies "Dox" file reference documentation.

    1 /***************************************
    2   $Header: /home/amb/CVS/procmeter3/procmeterrc.c,v 1.10 2008-05-05 18:45:17 amb Exp $
    3 
    4   ProcMeter - A system monitoring program for Linux - Version 3.5b.
    5 
    6   Handle the .procmeterrc file.
    7   ******************/ /******************
    8   Written by Andrew M. Bishop
    9 
   10   This file Copyright 1998-2008 Andrew M. Bishop
   11   It may be distributed under the GNU Public License, version 2, or
   12   any higher version.  See section COPYING of the GNU Public license
   13   for conditions under which this file may be redistributed.
   14   ***************************************/
   15 
   16 #include <stdio.h>
   17 #include <stdlib.h>
   18 #include <string.h>
   19 
   20 #include <sys/stat.h>
   21 
   22 #include "procmeter.h"
   23 #include "procmeterp.h"
   24 
   25 typedef struct _Section *Section;
   26 typedef struct _Parameter *Parameter;
   27 
   28 /*+ A section from the file. +*/
   29 struct _Section
   30 {
   31  char *name;                    /*+ The name of the section. +*/
   32  Section next;                  /*+ The next section; +*/
   33  Parameter first;               /*+ The first of the parameters. +*/
   34 };
   35 
   36 /*+ A parameter from the file. +*/
   37 struct _Parameter
   38 {
   39  char *name;                    /*+ The name of the parameter. +*/
   40  char *value;                   /*+ The value of the parameter. +*/
   41  Parameter next;                /*+ The next parameter. +*/
   42 };
   43 
   44 /*+ The first section. +*/
   45 Section FirstSection=NULL;
   46 
   47 
   48 /*++++++++++++++++++++++++++++++++++++++
   49   Load in the configuration file.
   50 
   51   int *argc The number of command line arguments.
   52 
   53   char **argv The command line arguments.
   54   ++++++++++++++++++++++++++++++++++++++*/
   55 
   56 void LoadProcMeterRC(int *argc,char **argv)
   57 {
   58  char *rcpath=NULL,*home;
   59  Section *next_section=&FirstSection;
   60  Parameter *next_parameter=NULL,prev_parameter=NULL;
   61  int i;
   62 
   63  /* Find the .procmeterrc file. */
   64 
   65  for(i=1;i<*argc;i++)
   66     if(!strncmp(argv[i],"--rc=",5))
   67       {
   68        rcpath=argv[i]+5;
   69        for((*argc)--;i<*argc;i++)
   70           argv[i]=argv[i+1];
   71       }
   72 
   73  if(!rcpath)
   74    {
   75     struct stat buf;
   76 
   77     if(!stat(".procmeterrc",&buf))
   78        rcpath=".procmeterrc";
   79     else if((home=getenv("HOME")))
   80       {
   81        rcpath=(char*)malloc(strlen(home)+16);
   82 
   83        strcpy(rcpath,home);
   84        strcat(rcpath,"/.procmeterrc");
   85 
   86        if(stat(rcpath,&buf))
   87          {free(rcpath);rcpath=NULL;}
   88       }
   89    }
   90  if(!rcpath)
   91     rcpath=RC_PATH "/procmeterrc";
   92 
   93  /* Read the .procmeterrc file. */
   94 
   95  if(rcpath)
   96    {
   97     FILE *rc=fopen(rcpath,"r");
   98     char *line=NULL;
   99     size_t length=256;
  100     int continued=0;
  101 
  102     if(!rc)
  103       {
  104        fprintf(stderr,"ProcMeter: Cannot open the configuration file %s for reading.\n",rcpath);
  105        exit(1);
  106       }
  107 
  108     while(fgets_realloc(&line,&length,rc))
  109       {
  110        char *l=line,*r=line+strlen(line)-1;
  111 
  112        while(*l==' ' || *l=='\t')
  113           l++;
  114        while(r>l && (*r=='\n' || *r=='\r' || *r==' ' || *r=='\t'))
  115           *r--=0;
  116 
  117        if(*l==';' || *l=='#' || l>=r)
  118           continue;
  119 
  120        if(*l=='[' && *r==']')
  121          {
  122           l++;*r--=0;
  123           *next_section=(Section)malloc(sizeof(struct _Section));
  124           strcpy((*next_section)->name=(char*)malloc(strlen(l)+1),l);
  125           (*next_section)->next=NULL;
  126           (*next_section)->first=NULL;
  127           next_parameter=&(*next_section)->first;
  128           next_section=&(*next_section)->next;
  129           continued=0;
  130          }
  131        else if(continued)
  132          {
  133           continued=0;
  134 
  135           if(l[strlen(l)-1]=='\\')
  136             {
  137              continued=1;
  138              l[strlen(l)-1]=0;
  139             }
  140 
  141           prev_parameter->value=(char*)realloc((void*)prev_parameter->value,strlen(l)+strlen(prev_parameter->value)+1);
  142           strcat(prev_parameter->value,l);
  143          }
  144        else if(next_parameter)
  145          {
  146           char *equal=strchr(l,'=');
  147           if(equal)
  148             {
  149              char *lr=equal-1,*rl=equal+1;
  150              *equal=0;
  151              while(*lr==' ' || *lr=='\t')
  152                 *lr--=0;
  153              while(*rl==' ' || *rl=='\t')
  154                 rl++;
  155              equal=rl;
  156             }
  157 
  158           *next_parameter=(Parameter)malloc(sizeof(struct _Parameter));
  159           strcpy((*next_parameter)->name=(char*)malloc(strlen(l)+1),l);
  160 
  161           continued=0;
  162 
  163           if(equal)
  164             {
  165              if(equal[strlen(equal)-1]=='\\')
  166                {
  167                 continued=1;
  168                 equal[strlen(equal)-1]=0;
  169                }
  170              strcpy((*next_parameter)->value=(char*)malloc(strlen(equal)+1),equal);
  171             }
  172           else
  173              (*next_parameter)->value=NULL;
  174 
  175           (*next_parameter)->next=NULL;
  176           prev_parameter=*next_parameter;
  177           next_parameter=&(*next_parameter)->next;
  178          }
  179       }
  180 
  181     fclose(rc);
  182    }
  183 
  184  /* Add in extra command line options. */
  185 
  186  for(i=1;i<*argc;i++)
  187     if(!strncmp(argv[i],"--",2))
  188       {
  189        int j;
  190        char *equal=strchr(argv[i],'='),*dot;
  191 
  192        if(equal)
  193          {
  194           char *section=argv[i]+2,*parameter,*value=equal+1;
  195           Section this_section=FirstSection;
  196 
  197           *equal=0;
  198           dot=strchr(section,'.');
  199 
  200           if(dot)
  201             {
  202              char *dot2=strchr(dot+1,'.');
  203 
  204              if(dot2) /* 2 dots => (module.output).parameter */
  205                {
  206                 parameter=dot2+1;
  207                 *dot2=0;
  208                }
  209              else     /* 1 dot => section.parameter */
  210                {
  211                 parameter=dot+1;
  212                 *dot=0;
  213                }
  214 
  215              while(this_section)
  216                {
  217                 if(!strcasecmp(section,this_section->name))
  218                   {
  219                    Parameter this_parameter=this_section->first;
  220 
  221                    while(this_parameter)
  222                      {
  223                       if(!strcasecmp(parameter,this_parameter->name))
  224                         {
  225                          strcpy(this_parameter->value=(char*)realloc((void*)this_parameter->value,strlen(value)+1),value);
  226                          break;
  227                         }
  228                       this_parameter=this_parameter->next;
  229                      }
  230 
  231                    if(!this_parameter)
  232                      {
  233                       Parameter new_parameter=(Parameter)malloc(sizeof(struct _Parameter));
  234                       strcpy(new_parameter->name=(char*)malloc(strlen(parameter)+1),parameter);
  235                       if(value)
  236                          strcpy(new_parameter->value=(char*)malloc(strlen(value)+1),value);
  237                       else
  238                          new_parameter->value=NULL;
  239 
  240                       new_parameter->next=this_section->first;
  241                       this_section->first=new_parameter;
  242                      }
  243 
  244                    break;
  245                   }
  246 
  247                 this_section=this_section->next;
  248                }
  249 
  250              if(!this_section)
  251                {
  252                 Section new_section=(Section)malloc(sizeof(struct _Section));
  253                 strcpy(new_section->name=(char*)malloc(strlen(section)+1),section);
  254 
  255                 new_section->first=(Parameter)malloc(sizeof(struct _Parameter));
  256                 strcpy(new_section->first->name=(char*)malloc(strlen(parameter)+1),parameter);
  257                 if(value)
  258                    strcpy(new_section->first->value=(char*)malloc(strlen(value)+1),value);
  259                 else
  260                    new_section->first->value=NULL;
  261                 new_section->first->next=NULL;
  262 
  263                 new_section->next=FirstSection;
  264                 FirstSection=new_section;
  265                }
  266             }
  267          }
  268 
  269        for(j=i,i--,(*argc)--;j<*argc;j++)
  270           argv[j]=argv[j+1];
  271       }
  272 }
  273 
  274 
  275 /*++++++++++++++++++++++++++++++++++++++
  276   Return the results of a section of the configuration file.
  277 
  278   char *GetProcMeterRC Returns the result string or NULL if none.
  279 
  280   char *section The section of the config file.
  281 
  282   char *parameter The parameter in the section of the config file.
  283   ++++++++++++++++++++++++++++++++++++++*/
  284 
  285 char *GetProcMeterRC(char *section,char *parameter)
  286 {
  287  Section this_section=FirstSection;
  288 
  289  while(this_section)
  290    {
  291     if(!strcasecmp(section,this_section->name))
  292       {
  293        Parameter this_parameter=this_section->first;
  294 
  295        while(this_parameter)
  296          {
  297           if(!strcasecmp(parameter,this_parameter->name))
  298              return(this_parameter->value);
  299           this_parameter=this_parameter->next;
  300          }
  301       }
  302 
  303     this_section=this_section->next;
  304    }
  305 
  306  return(NULL);
  307 }
  308 
  309 
  310 /*++++++++++++++++++++++++++++++++++++++
  311   Get the resources for an output.
  312 
  313   char *GetProcMeterRC2 Returns the result string or NULL if none.
  314 
  315   char *module The module name (part of section name).
  316 
  317   char *output The output name (part of section name).
  318 
  319   char *parameter The parameter to search for.
  320   ++++++++++++++++++++++++++++++++++++++*/
  321 
  322 char *GetProcMeterRC2(char *module,char *output,char *parameter)
  323 {
  324  ProcMeterModule m;
  325  ProcMeterOutput o;
  326  char section[sizeof(m.name)+sizeof(o.name)+2];
  327 
  328  strcpy(section,module);
  329  strcat(section,".");
  330  strcat(section,output);
  331 
  332  return(GetProcMeterRC(section,parameter));
  333 }
  334 
  335 
  336 /*++++++++++++++++++++++++++++++++++++++
  337   Free up the memory that is used in this module.
  338   ++++++++++++++++++++++++++++++++++++++*/
  339 
  340 void FreeProcMeterRC(void)
  341 {
  342  Section this_section=FirstSection;
  343 
  344  while(this_section)
  345    {
  346     Section last_section=this_section;
  347     Parameter this_parameter=this_section->first;
  348 
  349     while(this_parameter)
  350       {
  351        Parameter last_parameter=this_parameter;
  352 
  353        if(this_parameter->name)
  354           free(this_parameter->name);
  355        if(this_parameter->value)
  356           free(this_parameter->value);
  357        this_parameter=this_parameter->next;
  358 
  359        free(last_parameter);
  360       }
  361 
  362     this_section=this_section->next;
  363 
  364     free(last_section->name);
  365     free(last_section);
  366    }
  367 }