-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.c
More file actions
523 lines (467 loc) · 10.9 KB
/
eval.c
File metadata and controls
523 lines (467 loc) · 10.9 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* Stripchart -- the gnome-utils stripchart plotting utility
* Copyright (C) 2000 John Kodis <kodis@jagunet.com>
* vim:sts=2:sw=2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* 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.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#ifdef __FreeBSD__
#define HAVE_SYSCTL 1
#endif
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <linux/sysctl.h>
#if HAVE_SYSCTL
#include <sys/resource.h>
#endif
#include "chart-app.h"
#include "chart.h"
#include "strip.h"
/*
* skipbl -- skips over leading whitespace in a string.
*/
static char *
skipbl(char *s)
{
while (isspace(*s))
s++;
return s;
}
#if HAVE_SYSCTL
struct fn_sysctl {
char tag; /* '=' */
size_t len;
int mib[/*len*/];
};
#endif
/*
* Expr -- the info required to evaluate an expression.
*/
typedef struct
{
char *s;
double *t_diff;
int vars;
double *last, *now;
jmp_buf err_jmp;
double *filter;
char *filename;
char *equation, *pattern;
int pass;
double val;
char *error;
}
Expr;
/*
* eval_error -- called to report an error in expression evaluation.
*
* Only pre-initialization errors are reported. After initialization,
* the expression evaluator just returns a result of zero, and keeps
* on truckin'.
*/
static void
eval_error(Expr *expr, char *msg, ...)
{
va_list args;
const char *err_msg;
va_start(args, msg);
err_msg = verror(msg, args);
va_end(args);
expr->val = 0.0;
expr->error = g_strdup(err_msg);
longjmp(expr->err_jmp, 1);
}
/*
* stripbl -- skips some chars, then strips any leading whitespace.
*/
static void
stripbl(Expr *expr, int skip)
{
expr->s = skipbl(expr->s + skip);
}
/*
* add_op requires a forward prototype since it gets called from
* num_op when recursing to evaluate a parenthesized expression.
*/
static double add_op(Expr *expr);
/*
* num_op -- evaluates numeric constants, parenthesized expressions,
* and named variables.
*/
static double
num_op(Expr *expr)
{
double val = 0;
if (isdigit(*expr->s)
|| (*expr->s && strchr("+-.", *expr->s) && isdigit(expr->s[1])))
{
char *r;
val = strtod(expr->s, &r);
stripbl(expr, (int)(r - expr->s));
}
else if (*expr->s == '(')
{
stripbl(expr, 1);
val = add_op(expr);
if (*expr->s == ')')
stripbl(expr, 1);
else
eval_error(expr, ("closing parenthesis expected"));
}
else if (*expr->s == '$' || *expr->s == '~')
{
int c, id_intro;
char *idp, id[1000]; /* FIX THIS */
id_intro = *expr->s++;
for (idp = id; isalnum(c = (*idp++ = *expr->s++)) || c == '_'; )
;
idp[-1] = '\0';
expr->s--;
if (isdigit(*id))
{
int id_num = atoi(id);
if (id_num > expr->vars)
eval_error(expr, ("no such field: %d"), id_num);
val = expr->now[id_num-1];
if (id_intro == '~')
val -= expr->last[id_num-1];
}
else if (streq(id, "t")) /* time or delta time, in seconds */
{
if (id_intro == '~')
val = *expr->t_diff;
else
{
struct timeval t;
static struct timeval t0;
if (t0.tv_sec == 0)
gettimeofday(&t0, NULL);
gettimeofday(&t, NULL);
val = (t.tv_sec - t0.tv_sec) + (t.tv_usec - t0.tv_usec) / 1e6;
}
}
else if (!*id)
eval_error(expr, ("missing variable identifer"));
else
eval_error(expr, ("invalid variable identifer: %s"), id);
stripbl(expr, 0);
}
else
eval_error(expr, ("number expected"));
return val;
}
/*
* mul_op -- evaluates multiplication, division, and remaindering.
*/
static double
mul_op(Expr *expr)
{
double val1 = num_op(expr);
while (*expr->s == '*' || *expr->s == '/' || *expr->s == '%')
{
char c = *expr->s;
stripbl(expr, 1);
if (c == '*')
val1 *= num_op(expr);
else
{
double val2 = num_op(expr);
if (val2 == 0) /* FIX THIS: there's got to be a better way. */
val1 = 0;
else if (c == '/')
val1 /= val2;
else
val1 = fmod(val1, val2);
}
}
return val1;
}
/*
* add_op -- evaluates addition and subtraction,
*/
static double
add_op(Expr *expr)
{
double val = mul_op(expr);
while (*expr->s == '+' || *expr->s == '-')
{
char c = *expr->s;
stripbl(expr, 1);
if (c == '+')
val += mul_op(expr);
else
val -= mul_op(expr);
}
return val;
}
/*
* eval -- sets up an Expr, then calls add_op to evaluate the expression.
*/
static int
eval(Expr *expr)
{
expr->pass++;
expr->s = expr->equation;
expr->val = 0;
if (setjmp(expr->err_jmp))
return -1;
stripbl(expr, 0);
expr->val = add_op(expr);
if (*expr->s && *expr->s != ';')
eval_error(expr, ("extra junk at end: \"%s\""), expr->s);
return 0;
}
static int
stat_value(char *fn)
{
struct stat stat_buf;
int status = stat(fn, &stat_buf);
if (status == -1)
return -1;
if (stat_buf.st_size == 0)
return 0;
if (stat_buf.st_mtime < stat_buf.st_atime)
return 1;
return 2;
}
static int
split_and_extract(char *str, int vars, double *var)
{
int i = 0;
char *t = strtok(str, " \t:");
while (t && i < vars)
{
var[i] = atof(t);
t = strtok(NULL, " \t:");
i++;
}
return i;
}
static gdouble
evaluate_equation(Expr *expr)
{
double last = expr->val;
expr->val = 0;
if (expr->error != NULL)
return 0;
if (expr->filename)
{
FILE *fd = NULL;
if (*expr->filename == '?')
expr->val = stat_value(skipbl(expr->filename + 1));
else if (*expr->filename == '|')
fd = popen(expr->filename + 1, "r");
#if HAVE_SYSCTL
else if (*expr->filename == '=')
{
struct fn_sysctl *fn = (struct fn_sysctl *)expr->filename;
char buf[64];
size_t len = sizeof(buf);
if (sysctl(fn->mib, fn->len, buf, &len, NULL, 0) != 0)
{
fprintf(stderr, "sysctl error: %s\n", strerror(errno));
return 0;
}
switch (len)
{
case 4:
if (expr->vars)
expr->now[0] = *(int *)buf;
break;
case 8:
if (expr->vars)
expr->now[0] = *(long long *)buf;
break;
case sizeof(struct loadavg):
{
struct loadavg *tv = (struct loadavg *)buf;
int i = 0;
for (i = 0; i < MIN(expr->vars, 3); i++)
expr->now[i] = (double)tv->ldavg[i]/(double)tv->fscale;
break;
}
default:
fprintf(stderr, "sysctl: unknown size for '%s': %zu\n", expr->filename, len);
return 0;
}
}
#endif
else
fd = fopen(expr->filename, "r");
if (expr->vars)
{
char buf[1000];
*buf = '\0';
if (fd)
{
fgets(buf, sizeof(buf), fd);
if (expr->pattern && *expr->pattern)
while (!ferror(fd) && !feof(fd) && !strstr(buf, expr->pattern))
fgets(buf, sizeof(buf), fd);
}
memcpy(expr->last, expr->now, expr->vars * sizeof(*expr->now));
split_and_extract(buf, expr->vars, expr->now);
}
if (fd)
{
if (*expr->filename == '|')
pclose(fd);
else
fclose(fd);
}
}
if (expr->equation && eval(expr) != 0)
return 0;
switch (expr->pass)
{
case 0:
case 1:
expr->val = 0;
break;
case 2:
break;
default:
if (isfinite(last))
expr->val = last + *expr->filter * (expr->val - last);
break;
}
return expr->val;
}
static char *
expand_env(char *src)
{
if (!src)
return NULL;
#if HAVE_SYSCTL
if (*src == '=')
{
static int name[CTL_MAXNAME];
size_t namelen = CTL_MAXNAME;
if (sysctlnametomib(src+1, name, &namelen) != 0)
{
fprintf(stderr, "error looking up sysctl '%s': %s\n", src, strerror(errno));
return NULL;
}
struct fn_sysctl *mib = g_malloc(sizeof(struct fn_sysctl) + namelen * sizeof(int));
mib->tag = *src;
mib->len = namelen;
memcpy(mib->mib, name, namelen * sizeof(int));
return (char *)mib;
}
#endif
char *exp, *dst, *key, *val, *tmp;
exp = dst = g_malloc(strlen(src) + 1);
while (*src)
{
if (*src == '$')
{
key = val = g_strdup(src);
for (tmp = dst, *tmp++ = *val++; isalnum(*tmp++ = *val++); )
;
*--val = '\0';
if ((val = getenv(key + 1)) == NULL)
dst = tmp;
else
{
*dst = '\0';
tmp = g_malloc(strlen(exp) + strlen(val) + strlen(src) + 1);
strcpy(tmp, exp);
strcpy(tmp + strlen(tmp), val);
g_free(exp);
exp = tmp;
dst = exp + strlen(exp);
}
src += strlen(key);
g_free(key);
}
else
*dst++ = *src++;
}
*dst = '\0';
return exp;
}
static void
free_expr(Expr *expr)
{
if (expr->equation) g_free(expr->equation);
if (expr->pattern) g_free(expr->pattern);
if (expr->filename) g_free(expr->filename);
if (expr->last) g_free(expr->last);
if (expr->now) g_free(expr->now);
if (expr) g_free(expr);
}
ChartDatum *
chart_equation_add(Chart *chart,
Param_group *group, const Param_desc *desc, ChartAdjustment *adj,
int pageno, int rescale)
{
char *s;
ChartDatum *datum;
Expr *expr = g_malloc(sizeof(*expr));
expr->val = 0;
expr->pass = -1;
expr->error = NULL;
expr->t_diff = &group->t_diff;
expr->filter = &group->filter;
//expr->gtop_now = &group->gtop_now;
//expr->gtop_last = &group->gtop_last;
expr->equation = desc && desc->eqn ? g_strdup(desc->eqn) : NULL;
expr->filename = desc && desc->fn ? expand_env(desc->fn) : NULL;
expr->pattern = desc && desc->pattern ? g_strdup(desc->pattern) : NULL;
expr->vars = 0;
if (expr->equation)
for (s = expr->equation; *(s += strcspn(s, "$~")); )
if (isdigit(*++s))
if (expr->vars < atoi(s))
expr->vars = atoi(s);
if (expr->vars)
{
expr->last = g_malloc(expr->vars * sizeof(*expr->last));
expr->now = g_malloc(expr->vars * sizeof(*expr->now));
}
evaluate_equation(expr);
if (expr->error != NULL)
{
free_expr(expr);
return NULL;
}
datum = chart_parameter_add(chart,
evaluate_equation, expr, desc->color_names, adj, pageno,
str_to_gdouble(desc->bot_min, -G_MAXDOUBLE),
str_to_gdouble(desc->bot_max, +G_MAXDOUBLE),
str_to_gdouble(desc->top_min, -G_MAXDOUBLE),
str_to_gdouble(desc->top_max, +G_MAXDOUBLE));
chart_set_autorange(datum, rescale);
chart_set_scale_style(datum,
desc ? str_to_scale_style(desc->scale) : chart_scale_linear);
chart_set_plot_style(datum,
desc ? str_to_plot_style(desc->plot) : chart_plot_line);
return datum;
}
void
chart_start(GtkWidget *chart, Param_group *pg)
{
pg->t_last = pg->t_now;
gettimeofday(&pg->t_now, NULL);
pg->t_diff = (pg->t_now.tv_sec - pg->t_last.tv_sec) +
(pg->t_now.tv_usec - pg->t_last.tv_usec) / 1e6;
}