aboutsummaryrefslogtreecommitdiffstats
path: root/parser.c
blob: 64544e71da36caa86cf35ac77641353c076e3a0c (plain)
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
/* $Id: parser.c,v 1.17 2002/08/19 04:41:20 reinelt Exp $
 *
 * row definition parser
 *
 * Copyright 1999, 2000 by Michael Reinelt (reinelt@eunet.at)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * $Log: parser.c,v $
 * Revision 1.17  2002/08/19 04:41:20  reinelt
 * introduced bar.c, moved bar stuff from display.h to bar.h
 *
 * Revision 1.16  2001/03/16 16:40:17  ltoetsch
 * implemented time bar
 *
 * Revision 1.15  2001/03/14 13:19:29  ltoetsch
 * Added pop3/imap4 mail support
 *
 * Revision 1.14  2001/03/13 08:34:15  reinelt
 *
 * corrected a off-by-one bug with sensors
 *
 * Revision 1.13  2001/03/08 15:25:38  ltoetsch
 * improved exec
 *
 * Revision 1.12  2001/03/07 18:10:21  ltoetsch
 * added e(x)ec commands
 *
 * Revision 1.11  2001/03/02 10:18:03  ltoetsch
 * added /proc/apm battery stat
 *
 * Revision 1.10  2001/02/19 00:15:46  reinelt
 *
 * integrated mail and seti client
 * major rewrite of parser and tokenizer to support double-byte tokens
 *
 * Revision 1.9  2001/02/16 08:23:09  reinelt
 *
 * new token 'ic' (ISDN connected) by Carsten Nau <info@cnau.de>
 *
 * Revision 1.8  2001/02/14 07:40:16  reinelt
 *
 * first (incomplete) GPO implementation
 *
 * Revision 1.7  2000/08/10 09:44:09  reinelt
 *
 * new debugging scheme: error(), info(), debug()
 * uses syslog if in daemon mode
 *
 * Revision 1.6  2000/05/21 06:20:35  reinelt
 *
 * added ppp throughput
 * token is '%t[iomt]' at the moment, but this will change in the near future
 *
 * Revision 1.5  2000/03/24 11:36:56  reinelt
 *
 * new syntax for raster configuration
 * changed XRES and YRES to be configurable
 * PPM driver works nice
 *
 * Revision 1.4  2000/03/19 08:41:28  reinelt
 *
 * documentation available! README, README.MatrixOrbital, README.Drivers
 * added Skeleton.c as a starting point for new drivers
 *
 * Revision 1.3  2000/03/18 10:31:06  reinelt
 *
 * added sensor handling (for temperature etc.)
 * made data collecting happen only if data is used
 * (reading /proc/meminfo takes a lot of CPU!)
 * released lcd4linux-0.92
 *
 * Revision 1.2  2000/03/17 09:21:42  reinelt
 *
 * various memory statistics added
 *
 * Revision 1.1  2000/03/13 15:58:24  reinelt
 *
 * release 0.9
 * moved row parsing to parser.c
 * all basic work finished
 *
 */

/* 
 * exported functions:
 *
 * char *parse_row (char *string, int supported_bars, int usage[])
 *    converts a row definition from the config file
 *    into the internal form using tokens
 *    sets the array usage[token] to usage count
 *
 * char parse_gpo (char *string, int usage[])
 *    converts a GPO definition from the config file
 *    into the internal form using tokens
 *    sets the array usage[token] to usage count
 *
 */

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

#include "debug.h"
#include "display.h"
#include "bar.h"
#include "parser.h"

typedef struct {
  char *symbol;
  TOKEN token;
  CLASS class;
  int bar;
} SYMTAB;

static SYMTAB Symtab[] = {{ "%",  T_PERCENT,    C_GENERIC, 0 },
			  { "$",  T_DOLLAR,     C_GENERIC, 0 },
			  { "o",  T_OS,         C_GENERIC, 0 },
			  { "v",  T_RELEASE,    C_GENERIC, 0 },
			  { "p",  T_CPU,        C_GENERIC, 0 },
			  { "r",  T_RAM,        C_GENERIC, 0 },
			  { "mt", T_MEM_TOTAL,  C_MEM,     1 },
			  { "mu", T_MEM_USED,   C_MEM,     1 },
			  { "mf", T_MEM_FREE,   C_MEM,     1 },
			  { "ms", T_MEM_SHARED, C_MEM,     1 },
			  { "mb", T_MEM_BUFFER, C_MEM,     1 },
			  { "mc", T_MEM_CACHE,  C_MEM,     1 },
			  { "ma", T_MEM_AVAIL,  C_MEM,     1 },
			  { "l1", T_LOAD_1,     C_LOAD,    1 },
			  { "l2", T_LOAD_2,     C_LOAD,    1 },
			  { "l3", T_LOAD_3,     C_LOAD,    1 },
			  { "L",  T_OVERLOAD,   C_LOAD,    0 },
			  { "cu", T_CPU_USER,   C_CPU,     1 },
			  { "cn", T_CPU_NICE,   C_CPU,     1 },
			  { "cs", T_CPU_SYSTEM, C_CPU,     1 },
			  { "cb", T_CPU_BUSY,   C_CPU,     1 },
			  { "ci", T_CPU_IDLE,   C_CPU,     1 },
			  { "dr", T_DISK_READ,  C_DISK,    1 },
			  { "dw", T_DISK_WRITE, C_DISK,    1 },
			  { "dt", T_DISK_TOTAL, C_DISK,    1 },
			  { "dm", T_DISK_MAX,   C_DISK,    1 },
			  { "nr", T_ETH_RX,     C_ETH,     1 },
			  { "nw", T_ETH_TX,     C_ETH,     1 },
			  { "nt", T_ETH_TOTAL,  C_ETH,     1 },
			  { "nm", T_ETH_MAX,    C_ETH,     1 },
			  { "ic", T_ISDN_USED,  C_ISDN,    0 },
			  { "ii", T_ISDN_IN,    C_ISDN,    1 },
			  { "io", T_ISDN_OUT,   C_ISDN,    1 },
			  { "it", T_ISDN_TOTAL, C_ISDN,    1 },
			  { "im", T_ISDN_MAX,   C_ISDN,    1 },
			  { "ti", T_PPP_RX,     C_PPP,     1 },
			  { "to", T_PPP_TX,     C_PPP,     1 },
			  { "tt", T_PPP_TOTAL,  C_PPP,     1 },
			  { "tm", T_PPP_MAX,    C_PPP,     1 },
			  { "hc", T_SETI_PRC,   C_SETI,    1 },
			  { "ht", T_SETI_CPU,   C_SETI,    0 },
			  { "bp", T_BATT_PERC,  C_BATT,    1 },
			  { "bs", T_BATT_STAT,  C_BATT,    0 },
			  { "bd", T_BATT_DUR,   C_BATT,    0 },
			  { "e*", T_MAIL,       C_MAIL,    0 },
			  { "u*", T_MAIL_UNSEEN,C_MAIL,    0 },
			  { "s*", T_SENSOR,     C_SENSOR,  1 },
			  { "x*", T_EXEC,       C_EXEC,    1 },
			  { "",  -1,            0 }};

static int bar_type (char tag)
{
  switch (tag) {
  case 'l':
    return BAR_L;
  case 'r':
    return BAR_R;
  case 'u':
    return BAR_U;
  case 'd':
    return BAR_D;
  case 't':
    return BAR_T;
  default:
    return 0;
  }
}

static int get_token (char *s, char **p, int bar, int usage[])
{
  char c;
  int  i, l;
  
  for (i=0; Symtab[i].token!=-1; i++) {
    if (bar && !Symtab[i].bar) continue;
    l=strlen(Symtab[i].symbol);
    if (Symtab[i].symbol[l-1]=='*') {
      if (Symtab[i].token<T_EXTENDED) {
	error ("ERROR: internal error in symbol '%s'", Symtab[i].symbol);
	return -1;
      }
      c=*(s+l-1);
      if (strncmp(Symtab[i].symbol, s, l-1)==0 && c>='0' && c<='9') {
	*p=s+l;
	usage[Symtab[i].token]|=(1<<(c-'0'));
	usage[Symtab[i].class]=1;
	return Symtab[i].token+(c<<8);
      }
    } else if (strncmp(Symtab[i].symbol, s, l)==0) {
      *p=s+l;
      usage[Symtab[i].token]=1;
      usage[Symtab[i].class]=1;
      return Symtab[i].token;
    }
  }
  return -1;
}

char *parse_row (char *string, int supported_bars, int usage[])
{
  static char buffer[256];
  char *s=string;
  char *p=buffer;
  int token, token2;
  int type, len;

  do {
    switch (*s) {
      
    case '%':
      if ((token=get_token (++s, &s, 0, usage))==-1) {
	error ("WARNING: unknown token <%%%c> in <%s>", *s, string);
      } else {
	*p++='%';
	*p++=token&255;
	if (token>256) *p++=token>>8;
      }
      break;
      
    case '$':
      type=bar_type(tolower(*++s));
      if (type==0) {
	error ("WARNING: invalid bar type <$%c> in <%s>", *s, string);
	break;
      }
      if (!(type & supported_bars)) {
	error ("WARNING: driver does not support bar type '%c'", *s);
	break;
      }
      if (isupper(*s)) type |= BAR_LOG;
      len=strtol(++s, &s, 10);
      if (len<1 || len>127) {
	error ("WARNING: invalid bar length in <%s>", string);
	break;
      }
      if ((token=get_token (s, &s, 0, usage))==-1) {
	error ("WARNING: unknown token <$%c> in <%s>", *s, string);
	break;
      }
      token2=-1;
      if (*s=='+') {
	if ((type & BAR_H) && (supported_bars & BAR_H2)) {
	  type |= BAR_H2;
	} else if ((type & BAR_V) && (supported_bars & BAR_V2)) {
	  type |= BAR_V2;
	} else {
	  error ("WARNING: driver does not support double bars");
	  break;
	}
	if ((token2=get_token (s+1, &s, 0, usage))==-1) {
	  error ("WARNING: unknown token <$%c> in <%s>", *s, string);
	  break;
	}
      }
      else if (*s == ',' && (type & BAR_T))
        token2=strtol(++s, &s, 10); /* get horizontal length */
      *p++='$';
      *p++=type;
      *p++=len;
      *p++=token&255;
      if (token>256) *p++=token>>8;
      if (token2!=-1) {
	*p++=token2&255;
	if (token>256 && !(type & BAR_T)) *p++=token2>>8;
      }
      break;
      
    case '\\':
      if (*(s+1)=='\\') {
	*p++='\\';
	s++;
      } else {
	unsigned int c=0; int n;
	sscanf (s+1, "%3o%n", &c, &n);
	if (c==0 || c>255) {
	  error ("WARNING: illegal '\\' in <%s>", string);
	} else {
	  *p++=c;
	  s+=n+1;
	}
      }
      break;
      
    default:
      if ((*p++=*s)!='\0') s++;
    }
    
  } while (*s);

  *p='\0';
  return buffer;
}

int parse_gpo (char *string, int usage[])
{
  char *s=string;
  int token=-1;
  
  if (*s=='%') {
    if ((token=get_token (++s, &s, 0, usage))==-1) {
      error ("WARNING: unknown token <%%%c> in <%s>", *s, string);
    }
  }
  
  if (*s!='\0') {
    error ("WARNING: error while parsing <%s>", string);
  }
  
  return token;
}