File: procmeterrc.c

package info (click to toggle)
procmeter3 3.6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,444 kB
  • sloc: ansic: 16,600; makefile: 428; sh: 13
file content (367 lines) | stat: -rw-r--r-- 9,797 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/***************************************
  $Header: /home/amb/CVS/procmeter3/procmeterrc.c,v 1.10 2008-05-05 18:45:17 amb Exp $

  ProcMeter - A system monitoring program for Linux - Version 3.5b.

  Handle the .procmeterrc file.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1998-2008 Andrew M. Bishop
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/stat.h>

#include "procmeter.h"
#include "procmeterp.h"

typedef struct _Section *Section;
typedef struct _Parameter *Parameter;

/*+ A section from the file. +*/
struct _Section
{
 char *name;                    /*+ The name of the section. +*/
 Section next;                  /*+ The next section; +*/
 Parameter first;               /*+ The first of the parameters. +*/
};

/*+ A parameter from the file. +*/
struct _Parameter
{
 char *name;                    /*+ The name of the parameter. +*/
 char *value;                   /*+ The value of the parameter. +*/
 Parameter next;                /*+ The next parameter. +*/
};

/*+ The first section. +*/
Section FirstSection=NULL;


/*++++++++++++++++++++++++++++++++++++++
  Load in the configuration file.

  int *argc The number of command line arguments.

  char **argv The command line arguments.
  ++++++++++++++++++++++++++++++++++++++*/

void LoadProcMeterRC(int *argc,char **argv)
{
 char *rcpath=NULL,*home;
 Section *next_section=&FirstSection;
 Parameter *next_parameter=NULL,prev_parameter=NULL;
 int i;

 /* Find the .procmeterrc file. */

 for(i=1;i<*argc;i++)
    if(!strncmp(argv[i],"--rc=",5))
      {
       rcpath=argv[i]+5;
       for((*argc)--;i<*argc;i++)
          argv[i]=argv[i+1];
      }

 if(!rcpath)
   {
    struct stat buf;

    if(!stat(".procmeterrc",&buf))
       rcpath=".procmeterrc";
    else if((home=getenv("HOME")))
      {
       rcpath=(char*)malloc(strlen(home)+16);

       strcpy(rcpath,home);
       strcat(rcpath,"/.procmeterrc");

       if(stat(rcpath,&buf))
         {free(rcpath);rcpath=NULL;}
      }
   }
 if(!rcpath)
    rcpath=RC_PATH "/procmeterrc";

 /* Read the .procmeterrc file. */

 if(rcpath)
   {
    FILE *rc=fopen(rcpath,"r");
    char *line=NULL;
    size_t length=256;
    int continued=0;

    if(!rc)
      {
       fprintf(stderr,"ProcMeter: Cannot open the configuration file %s for reading.\n",rcpath);
       exit(1);
      }

    while(fgets_realloc(&line,&length,rc))
      {
       char *l=line,*r=line+strlen(line)-1;

       while(*l==' ' || *l=='\t')
          l++;
       while(r>l && (*r=='\n' || *r=='\r' || *r==' ' || *r=='\t'))
          *r--=0;

       if(*l==';' || *l=='#' || l>=r)
          continue;

       if(*l=='[' && *r==']')
         {
          l++;*r--=0;
          *next_section=(Section)malloc(sizeof(struct _Section));
          strcpy((*next_section)->name=(char*)malloc(strlen(l)+1),l);
          (*next_section)->next=NULL;
          (*next_section)->first=NULL;
          next_parameter=&(*next_section)->first;
          next_section=&(*next_section)->next;
          continued=0;
         }
       else if(continued)
         {
          continued=0;

          if(l[strlen(l)-1]=='\\')
            {
             continued=1;
             l[strlen(l)-1]=0;
            }

          prev_parameter->value=(char*)realloc((void*)prev_parameter->value,strlen(l)+strlen(prev_parameter->value)+1);
          strcat(prev_parameter->value,l);
         }
       else if(next_parameter)
         {
          char *equal=strchr(l,'=');
          if(equal)
            {
             char *lr=equal-1,*rl=equal+1;
             *equal=0;
             while(*lr==' ' || *lr=='\t')
                *lr--=0;
             while(*rl==' ' || *rl=='\t')
                rl++;
             equal=rl;
            }

          *next_parameter=(Parameter)malloc(sizeof(struct _Parameter));
          strcpy((*next_parameter)->name=(char*)malloc(strlen(l)+1),l);

          continued=0;

          if(equal)
            {
             if(equal[strlen(equal)-1]=='\\')
               {
                continued=1;
                equal[strlen(equal)-1]=0;
               }
             strcpy((*next_parameter)->value=(char*)malloc(strlen(equal)+1),equal);
            }
          else
             (*next_parameter)->value=NULL;

          (*next_parameter)->next=NULL;
          prev_parameter=*next_parameter;
          next_parameter=&(*next_parameter)->next;
         }
      }

    fclose(rc);
   }

 /* Add in extra command line options. */

 for(i=1;i<*argc;i++)
    if(!strncmp(argv[i],"--",2))
      {
       int j;
       char *equal=strchr(argv[i],'='),*dot;

       if(equal)
         {
          char *section=argv[i]+2,*parameter,*value=equal+1;
          Section this_section=FirstSection;

          *equal=0;
          dot=strchr(section,'.');

          if(dot)
            {
             char *dot2=strchr(dot+1,'.');

             if(dot2) /* 2 dots => (module.output).parameter */
               {
                parameter=dot2+1;
                *dot2=0;
               }
             else     /* 1 dot => section.parameter */
               {
                parameter=dot+1;
                *dot=0;
               }

             while(this_section)
               {
                if(!strcasecmp(section,this_section->name))
                  {
                   Parameter this_parameter=this_section->first;

                   while(this_parameter)
                     {
                      if(!strcasecmp(parameter,this_parameter->name))
                        {
                         strcpy(this_parameter->value=(char*)realloc((void*)this_parameter->value,strlen(value)+1),value);
                         break;
                        }
                      this_parameter=this_parameter->next;
                     }

                   if(!this_parameter)
                     {
                      Parameter new_parameter=(Parameter)malloc(sizeof(struct _Parameter));
                      strcpy(new_parameter->name=(char*)malloc(strlen(parameter)+1),parameter);
                      if(value)
                         strcpy(new_parameter->value=(char*)malloc(strlen(value)+1),value);
                      else
                         new_parameter->value=NULL;

                      new_parameter->next=this_section->first;
                      this_section->first=new_parameter;
                     }

                   break;
                  }

                this_section=this_section->next;
               }

             if(!this_section)
               {
                Section new_section=(Section)malloc(sizeof(struct _Section));
                strcpy(new_section->name=(char*)malloc(strlen(section)+1),section);

                new_section->first=(Parameter)malloc(sizeof(struct _Parameter));
                strcpy(new_section->first->name=(char*)malloc(strlen(parameter)+1),parameter);
                if(value)
                   strcpy(new_section->first->value=(char*)malloc(strlen(value)+1),value);
                else
                   new_section->first->value=NULL;
                new_section->first->next=NULL;

                new_section->next=FirstSection;
                FirstSection=new_section;
               }
            }
         }

       for(j=i,i--,(*argc)--;j<*argc;j++)
          argv[j]=argv[j+1];
      }
}


/*++++++++++++++++++++++++++++++++++++++
  Return the results of a section of the configuration file.

  char *GetProcMeterRC Returns the result string or NULL if none.

  char *section The section of the config file.

  char *parameter The parameter in the section of the config file.
  ++++++++++++++++++++++++++++++++++++++*/

char *GetProcMeterRC(char *section,char *parameter)
{
 Section this_section=FirstSection;

 while(this_section)
   {
    if(!strcasecmp(section,this_section->name))
      {
       Parameter this_parameter=this_section->first;

       while(this_parameter)
         {
          if(!strcasecmp(parameter,this_parameter->name))
             return(this_parameter->value);
          this_parameter=this_parameter->next;
         }
      }

    this_section=this_section->next;
   }

 return(NULL);
}


/*++++++++++++++++++++++++++++++++++++++
  Get the resources for an output.

  char *GetProcMeterRC2 Returns the result string or NULL if none.

  char *module The module name (part of section name).

  char *output The output name (part of section name).

  char *parameter The parameter to search for.
  ++++++++++++++++++++++++++++++++++++++*/

char *GetProcMeterRC2(char *module,char *output,char *parameter)
{
 ProcMeterModule m;
 ProcMeterOutput o;
 char section[sizeof(m.name)+sizeof(o.name)+2];

 strcpy(section,module);
 strcat(section,".");
 strcat(section,output);

 return(GetProcMeterRC(section,parameter));
}


/*++++++++++++++++++++++++++++++++++++++
  Free up the memory that is used in this module.
  ++++++++++++++++++++++++++++++++++++++*/

void FreeProcMeterRC(void)
{
 Section this_section=FirstSection;

 while(this_section)
   {
    Section last_section=this_section;
    Parameter this_parameter=this_section->first;

    while(this_parameter)
      {
       Parameter last_parameter=this_parameter;

       if(this_parameter->name)
          free(this_parameter->name);
       if(this_parameter->value)
          free(this_parameter->value);
       this_parameter=this_parameter->next;

       free(last_parameter);
      }

    this_section=this_section->next;

    free(last_section->name);
    free(last_section);
   }
}