aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2009-04-26 12:42:11 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2009-04-26 12:42:11 +0300
commitd50fc04ded36255465184a16c70eb4c50acdb199 (patch)
tree02a568794c2a211621d1fe39868eaad7963bdc43 /src
parent099a946ad4465c42db4737b247f1e89bd03c83ae (diff)
downloadtagr-d50fc04ded36255465184a16c70eb4c50acdb199.tar.gz
tagr-d50fc04ded36255465184a16c70eb4c50acdb199.tar.bz2
Introduce formats in preprocessor variables.
* configure.ac: Version 1.9.90 * NEWS: Likewise. * etc/Makefile.am (EXTRA_DIST): Add logfilter.awk uptmpl.sed * etc/logfilter.awk: New file * etc/uptmpl.sed: New file * etc/tagr.tmpl: Update * etc/upgrade.awk: Add copyleft header * gnulib.modules: Add fprintftime. * src/graph.c (rate_unit): Change to "Bytes per Second". (number_suffix, number_suffix_count): Move to grid.c * src/html.gram.y: Work with formats. * src/html.lex.l: Likewise. * src/output.c (update_output): Store NOW as a number and provide a formatting function for it. * src/tagr.h (union value, value_format_fn): New types. (pp_value_t): New members: fmt, format (add_numeric_value, add_string_value): Change return type. (init_value): New proto.
Diffstat (limited to 'src')
-rw-r--r--src/graph.c9
-rw-r--r--src/grid.c5
-rw-r--r--src/html.gram.y161
-rw-r--r--src/html.lex.l27
-rw-r--r--src/output.c23
-rw-r--r--src/tagr.h23
6 files changed, 173 insertions, 75 deletions
diff --git a/src/graph.c b/src/graph.c
index 16483bb..bfd00cb 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -55,9 +55,5 @@ int graph_h_margin[2] = { 100, 14 };
int graph_v_margin[2] = { 14, 35 };
-char *rate_unit = "Bits per Second";
-
-static char *short_suffix[] = {"", "k", "M", "G", "T"};
-char **number_suffix = short_suffix;
-size_t number_suffix_count = sizeof (short_suffix) / sizeof (short_suffix[0]);
+char *rate_unit = "Bytes per Second";
#define make_color_index(g, ar) \
@@ -99,5 +95,6 @@ draw_graph (FILE *fp,
#define ytr(y) \
- (unsigned long) ((ymax >= (y) ? (ymax - (y)) : ymax) * yscale + graph_h_margin[1])
+ (unsigned long) ((ymax >= (y) ? (ymax - (y)) : ymax) * \
+ yscale + graph_h_margin[1])
#define xtr(x) \
(unsigned long) (growright ? \
diff --git a/src/grid.c b/src/grid.c
index f4a75e1..1671753 100644
--- a/src/grid.c
+++ b/src/grid.c
@@ -81,4 +81,9 @@ grid_free_data (void *ptr)
+/* FIXME: Suffixes not yet used. */
+static char *short_suffix[] = {"", "k", "M", "G", "T"};
+char **number_suffix = short_suffix;
+size_t number_suffix_count = sizeof (short_suffix) / sizeof (short_suffix[0]);
+
struct ygrid_data
{
diff --git a/src/html.gram.y b/src/html.gram.y
index bce5e93..fae64ff 100644
--- a/src/html.gram.y
+++ b/src/html.gram.y
@@ -37,4 +37,6 @@ static FILE *tmp_file;
static void out_char (int c);
static void out_value (pp_value_t *val);
+static void deduce_format (pp_value_t *res,
+ const pp_value_t *a, const pp_value_t *b);
%}
@@ -84,5 +86,5 @@ eval : IDENT
| obrace error cbrace
{
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
;
@@ -109,14 +111,14 @@ expr : value
if ($1.type == unspecified_value
|| $3.type == unspecified_value)
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
else if ($1.type != $3.type)
{
yyerror ("type mismatch in addition");
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
else
{
$$.type = $1.type;
- $$.prec = maxprec ($1.prec, $3.prec);
+ deduce_format (&$$, &$1, &$3);
switch ($1.type)
{
@@ -141,19 +143,19 @@ expr : value
if ($1.type == unspecified_value
|| $3.type == unspecified_value)
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
else if ($1.type != $3.type)
{
yyerror ("type mismatch in subtraction");
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
else if ($1.type == string_value)
{
yyerror ("subtraction not defined for strings");
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
else
{
$$.type = $1.type;
- $$.prec = maxprec ($1.prec, $3.prec);
+ deduce_format (&$$, &$1, &$3);
$$.v.number = $1.v.number - $3.v.number;
}
@@ -163,19 +165,19 @@ expr : value
if ($1.type == unspecified_value
|| $3.type == unspecified_value)
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
else if ($1.type != $3.type)
{
yyerror ("type mismatch in multiplication");
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
else if ($1.type == string_value)
{
yyerror ("multiplication not defined for strings");
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
else
{
$$.type = $1.type;
- $$.prec = maxprec ($1.prec, $3.prec);
+ deduce_format (&$$, &$1, &$3);
$$.v.number = $1.v.number * $3.v.number;
}
@@ -185,24 +187,24 @@ expr : value
if ($1.type == unspecified_value
|| $3.type == unspecified_value)
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
else if ($1.type != $3.type)
{
yyerror ("type mismatch in division");
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
else if ($1.type == string_value)
{
yyerror ("division not defined for strings");
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
else if (fabs ($3.v.number) < 1.0e-5)
{
yyerror ("division by zero");
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
else
{
$$.type = $1.type;
- $$.prec = maxprec ($1.prec, $3.prec);
+ deduce_format (&$$, &$1, &$3);
$$.v.number = $1.v.number / $3.v.number;
}
@@ -211,14 +213,13 @@ expr : value
{
if ($2.type == unspecified_value)
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
else if ($2.type == string_value)
{
yyerror ("unary minus not defined for strings");
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
else
{
- $$.type = $2.type;
- $$.prec = $2.prec;
+ $$ = $2;
$$.v.number = - $2.v.number;
}
@@ -227,9 +228,9 @@ expr : value
{
if ($2.type == unspecified_value)
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
else if ($2.type == string_value)
{
yyerror ("unary plus not defined for strings");
- $$.type = unspecified_value;
+ init_value (&$$, unspecified_value, NULL);
}
else
@@ -243,7 +244,7 @@ value : IDENT
| NUMBER
{
- $$.type = numeric_value;
- $$.prec = -1;
- $$.v.number = $1;
+ union value v;
+ v.number = $1;
+ init_value (&$$, numeric_value, &v);
}
;
@@ -310,26 +311,58 @@ out_char (int c)
static void
-out_value (pp_value_t *val)
+deduce_format (pp_value_t *res, const pp_value_t *a, const pp_value_t *b)
{
- switch (val->type)
+ res->prec = maxprec (a->prec, b->prec);
+ if (!a->format && b->format)
{
- case unspecified_value:
- fprintf (tmp_file, "#UNSPECIFIED");
- break;
+ res->fmt = b->fmt;
+ res->format = b->format;
+ }
+ else
+ {
+ res->fmt = a->fmt;
+ res->format = a->format;
+ }
+}
- case numeric_value:
- if (val->prec >= 0)
- fprintf (tmp_file, "%.*f", val->prec, val->v.number);
+
+void
+format_unspecified (FILE *fp, union value v, const char *fmt, int prec)
+{
+ fprintf (fp, "#UNSPECIFIED");
+}
+
+void
+format_numeric (FILE *fp, union value v, const char *fmt, int prec)
+{
+ if (prec >= 0)
+ fprintf (fp, "%.*f", prec, v.number);
else
- fprintf (tmp_file, "%f", val->v.number);
- break;
+ fprintf (fp, "%f", v.number);
+}
- case string_value:
- if (val->prec > 0)
- fprintf (tmp_file, "%.*s", val->prec, val->v.string);
+void
+format_string (FILE *fp, union value v, const char *fmt, int prec)
+{
+ if (fmt)
+ fprintf (fp, fmt, v.string);
+ else if (prec > 0)
+ fprintf (fp, "%.*s", prec, v.string);
else
- fprintf (tmp_file, "%s", val->v.string);
- break;
+ fprintf (fp, "%s", v.string);
}
+
+static value_format_fn default_format[] = {
+ format_unspecified,
+ format_numeric,
+ format_string
+};
+
+static void
+out_value (pp_value_t *val)
+{
+ value_format_fn format =
+ val->format ? val->format : default_format[val->type];
+ format (tmp_file, val->v, val->fmt, val->prec);
}
@@ -357,6 +390,25 @@ free_value (pp_value_t *val)
}
-static void
-add_value (pp_tab_t **ptab, const char *name, pp_value_t *val)
+void
+init_value (pp_value_t *p, value_type type, union value *v)
+{
+ memset (p, 0, sizeof (*p));
+ p->type = type;
+ p->prec = -1;
+ switch (p->type)
+ {
+ case unspecified_value:
+ break;
+ case numeric_value:
+ p->v.number = v->number;
+ break;
+ case string_value:
+ p->v.string = xstrdup (v->string);
+ }
+}
+
+static pp_value_t *
+add_value (pp_tab_t **ptab, const char *name,
+ value_type type, union value *v)
{
pp_tab_t *p;
@@ -375,27 +427,24 @@ add_value (pp_tab_t **ptab, const char *name, pp_value_t *val)
*ptab = p;
}
- p->value = *val;
+ init_value (&p->value, type, v);
+ return &p->value;
}
-void
+pp_value_t *
add_numeric_value (pp_tab_t **ptab, const char *name, double number)
{
- pp_value_t val;
+ union value v;
- val.type = numeric_value;
- val.prec = -1;
- val.v.number = number;
- add_value (ptab, name, &val);
+ v.number = number;
+ return add_value (ptab, name, numeric_value, &v);
}
-void
+pp_value_t *
add_string_value (pp_tab_t **ptab, const char *name, const char *string)
{
- pp_value_t val;
+ union value v;
- val.type = string_value;
- val.prec = -1;
- val.v.string = xstrdup (string);
- add_value (ptab, name, &val);
+ v.string = xstrdup (string);
+ return add_value (ptab, name, string_value, &v);
}
diff --git a/src/html.lex.l b/src/html.lex.l
index 9f323dd..931bfc1 100644
--- a/src/html.lex.l
+++ b/src/html.lex.l
@@ -45,5 +45,5 @@ N [0-9]+
{
yyerror ("unknown identifier");
- yylval.value.type = unspecified_value;
+ init_value (&yylval.value, unspecified_value, NULL);
}
return IDENT;
@@ -54,5 +54,5 @@ N [0-9]+
{
yyerror ("unknown identifier");
- yylval.value.type = unspecified_value;
+ init_value (&yylval.value, unspecified_value, NULL);
}
return IDENT;
@@ -64,5 +64,5 @@ N [0-9]+
{
yyerror ("unknown identifier");
- yylval.value.type = unspecified_value;
+ init_value (&yylval.value, unspecified_value, NULL);
}
else
@@ -72,4 +72,25 @@ N [0-9]+
return IDENT;
}
+\$\({NAME}:[^)]+\) {
+ pp_value_t val;
+ char *p = strchr (yytext, ':');
+ *p++ = 0;
+ if (find_value (yytext+2, &val))
+ {
+ yyerror ("unknown identifier");
+ init_value (&yylval.value, unspecified_value, NULL);
+ }
+ else
+ {
+ size_t len = strlen (p) - 1;
+ init_value (&yylval.value, val.type, &val.v);
+ yylval.value.prec = val.prec;
+ yylval.value.format = val.format;
+ yylval.value.fmt = xmalloc (len + 1);
+ memcpy (yylval.value.fmt, p, len);
+ yylval.value.fmt[len] = 0;
+ }
+ return IDENT;
+}
\$\{ return OBRACE;
\$\} return CBRACE;
diff --git a/src/output.c b/src/output.c
index 980f9e1..e376ad4 100644
--- a/src/output.c
+++ b/src/output.c
@@ -24,4 +24,5 @@
#include <errno.h>
#include <sys/stat.h>
+#include <fprintftime.h>
#include <tagr.h>
@@ -221,4 +222,16 @@ struct image_descr img_year = {
};
+static void
+format_timestamp (FILE *fp, union value v, const char *fmt, int prec)
+{
+ time_t t = v.number;
+ struct tm *tm = localtime (&t);
+
+ if (!fmt)
+ fmt = "%c";
+
+ fprintftime(fp, fmt, tm, 0, 0);
+}
+
int
update_output (struct monitor *mon, struct traffic_record *tr,
@@ -231,4 +244,5 @@ update_output (struct monitor *mon, struct traffic_record *tr,
char *tabfile = mkfilename (dirname, mon->name, ".tab");
char *htmlname;
+ pp_value_t *p;
if (!force_update)
@@ -265,7 +279,10 @@ update_output (struct monitor *mon, struct traffic_record *tr,
add_string_value (&tab, "PROGRAM", PACKAGE_NAME);
add_string_value (&tab, "VERSION", PACKAGE_VERSION);
- add_string_value (&tab, "ROUTERNAME", mon->name);
- add_string_value (&tab, "ROUTERIP", mon->id);
- add_string_value (&tab, "NOW", ctime (&timestamp));
+ add_string_value (&tab, "MONITOR", mon->name);
+ add_string_value (&tab, "ID", mon->id);
+
+ p = add_numeric_value (&tab, "NOW", timestamp);
+ p->format = format_timestamp;
+ p->fmt = "%c";
add_numeric_value (&tab, "SPEED", mon->max_rate);
diff --git a/src/tagr.h b/src/tagr.h
index b65b8d4..96035f2 100644
--- a/src/tagr.h
+++ b/src/tagr.h
@@ -98,13 +98,19 @@ int yyerror (char *s);
typedef enum { unspecified_value, numeric_value, string_value } value_type;
+union value
+{
+ double number;
+ char *string;
+};
+
+typedef void (*value_format_fn) (FILE *, union value, const char *, int);
+
typedef struct
{
value_type type;
int prec;
- union
- {
- double number;
- char *string;
- } v;
+ char *fmt;
+ value_format_fn format;
+ union value v;
} pp_value_t;
@@ -118,6 +124,9 @@ typedef struct pp_value_list
int read_symtab (pp_tab_t **tab, const char *name);
int write_symtab (pp_tab_t *tab, const char *name);
-void add_numeric_value (pp_tab_t ** ptab, const char *name, double number);
-void add_string_value (pp_tab_t ** ptab, const char *name, const char *string);
+void init_value (pp_value_t *p, value_type type, union value *v);
+pp_value_t *add_numeric_value (pp_tab_t ** ptab,
+ const char *name, double number);
+pp_value_t *add_string_value (pp_tab_t ** ptab,
+ const char *name, const char *string);
int find_value (char *name, pp_value_t * val);
void free_tab (pp_tab_t ** ptab);

Return to:

Send suggestions and report system problems to the System administrator.