10 date 93.08.19.21.05.42; author jromine; state Exp;
15 date 93.06.12.06.46.16; author jromine; state Exp;
20 date 92.12.15.00.20.22; author jromine; state Exp;
25 date 92.10.26.22.45.35; author jromine; state Exp;
30 date 92.02.09.21.42.12; author jromine; state Exp;
35 date 92.02.09.07.09.49; author jromine; state Exp;
40 date 92.02.05.04.57.21; author jromine; state Exp;
45 date 92.01.23.23.10.58; author jromine; state Exp;
50 date 91.01.10.08.35.40; author mh; state Exp;
55 date 91.01.09.22.33.06; author mh; state Exp;
60 date 91.01.09.22.19.43; author mh; state Exp;
65 date 91.01.09.16.31.16; author mh; state Exp;
70 date 90.12.27.17.19.13; author mh; state Exp;
75 date 90.04.05.15.32.23; author sources; state Exp;
80 date 90.04.05.14.49.56; author sources; state Exp;
85 date 90.02.21.15.51.02; author sources; state Exp;
90 date 90.02.21.15.46.42; author sources; state Exp;
101 @allow "_" in header field names
104 @/* fmtcompile.c - "compile" format strings for fmtscan */
106 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.16 1993/06/12 06:46:16 jromine Exp jromine $";
110 #include "../h/addrsbr.h"
111 #include "../h/formatsbr.h"
112 #include "../zotnet/tws.h"
113 #include "../h/fmtcompile.h"
116 #include <sys/types.h>
117 #include <sys/stat.h>
119 static struct format *formatvec; /* array to hold formats */
120 static struct format *next_fp; /* next free format slot */
121 static struct format *fp; /* current format slot */
122 static struct comp *cm; /* most recent comp ref */
123 static struct ftable *ftbl; /* most recent func ref */
125 static int infunction; /* function nesting cnt */
127 extern char *getusr();
128 extern struct mailname fmt_mnull;
131 char *name; /* function name */
132 char type; /* argument type */
133 #define TF_COMP 0 /* component expected */
134 #define TF_NUM 1 /* number expected */
135 #define TF_STR 2 /* string expected */
136 #define TF_EXPR 3 /* component or func. expected */
137 #define TF_NONE 4 /* no argument */
138 #define TF_MYBOX 5 /* special - get current user's mbox */
139 #define TF_NOW 6 /* special - get current unix time */
140 #define TF_EXPR_SV 7 /* like expr but save current str reg */
141 #define TF_NOP 8 /* like expr but no result */
142 char f_type; /* fmt type */
143 char extra; /* arg. type dependent extra info */
145 #define TFL_PUTS 1 /* implicit putstr if top level */
146 #define TFL_PUTN 2 /* implicit putnum if top level */
149 static struct ftable functable[] = {
150 "nonzero", TF_EXPR, FT_V_NE, FT_IF_V_NE, 0,
151 "zero", TF_EXPR, FT_V_EQ, FT_IF_V_EQ, 0,
152 "eq", TF_NUM, FT_V_EQ, FT_IF_V_EQ, 0,
153 "ne", TF_NUM, FT_V_NE, FT_IF_V_NE, 0,
154 "gt", TF_NUM, FT_V_GT, FT_IF_V_GT, 0,
155 "null", TF_EXPR, FT_S_NULL, FT_IF_S_NULL, 0,
156 "nonnull", TF_EXPR, FT_S_NONNULL, FT_IF_S, 0,
157 "match", TF_STR, FT_V_MATCH, FT_IF_MATCH, 0,
158 "amatch", TF_STR, FT_V_AMATCH, FT_IF_AMATCH, 0,
160 "putstr", TF_EXPR, FT_STR, 0, 0,
161 "putstrf", TF_EXPR, FT_STRF, 0, 0,
162 "putnum", TF_EXPR, FT_NUM, 0, 0,
163 "putnumf", TF_EXPR, FT_NUMF, 0, 0,
164 "putaddr", TF_STR, FT_PUTADDR, 0, 0,
165 "void", TF_NOP, 0, 0, 0,
167 "comp", TF_COMP, FT_LS_COMP, 0, TFL_PUTS,
168 "lit", TF_STR, FT_LS_LIT, 0, TFL_PUTS,
169 "getenv", TF_STR, FT_LS_GETENV, 0, TFL_PUTS,
170 "profile", TF_STR, FT_LS_MFIND, 0, TFL_PUTS,
171 "trim", TF_EXPR, FT_LS_TRIM, 0, 0,
172 "compval", TF_COMP, FT_LV_COMP, 0, TFL_PUTN,
173 "compflag", TF_COMP, FT_LV_COMPFLAG, 0, TFL_PUTN,
174 "num", TF_NUM, FT_LV_LIT, 0, TFL_PUTN,
175 "msg", TF_NONE, FT_LV_DAT, 0, TFL_PUTN,
176 "cur", TF_NONE, FT_LV_DAT, 1, TFL_PUTN,
177 "size", TF_NONE, FT_LV_DAT, 2, TFL_PUTN,
178 "width", TF_NONE, FT_LV_DAT, 3, TFL_PUTN,
179 "unseen", TF_NONE, FT_LV_DAT, 4, TFL_PUTN,
180 "dat", TF_NUM, FT_LV_DAT, 0, TFL_PUTN,
181 "strlen", TF_NONE, FT_LV_STRLEN, 0, TFL_PUTN,
182 "me", TF_MYBOX, FT_LS_LIT, 0, TFL_PUTS,
183 "plus", TF_NUM, FT_LV_PLUS_L, 0, TFL_PUTN,
184 "minus", TF_NUM, FT_LV_MINUS_L, 0, TFL_PUTN,
185 "divide", TF_NUM, FT_LV_DIVIDE_L, 0, TFL_PUTN,
186 "modulo", TF_NUM, FT_LV_MODULO_L, 0, TFL_PUTN,
187 "charleft", TF_NONE, FT_LV_CHAR_LEFT, 0, TFL_PUTN,
188 "timenow", TF_NOW, FT_LV_LIT, 0, TFL_PUTN,
190 "month", TF_COMP, FT_LS_MONTH, FT_PARSEDATE, TFL_PUTS,
191 "lmonth", TF_COMP, FT_LS_LMONTH, FT_PARSEDATE, TFL_PUTS,
192 "tzone", TF_COMP, FT_LS_ZONE, FT_PARSEDATE, TFL_PUTS,
193 "day", TF_COMP, FT_LS_DAY, FT_PARSEDATE, TFL_PUTS,
194 "weekday", TF_COMP, FT_LS_WEEKDAY, FT_PARSEDATE, TFL_PUTS,
195 "tws", TF_COMP, FT_LS_822DATE, FT_PARSEDATE, TFL_PUTS,
196 "sec", TF_COMP, FT_LV_SEC, FT_PARSEDATE, TFL_PUTN,
197 "min", TF_COMP, FT_LV_MIN, FT_PARSEDATE, TFL_PUTN,
198 "hour", TF_COMP, FT_LV_HOUR, FT_PARSEDATE, TFL_PUTN,
199 "mday", TF_COMP, FT_LV_MDAY, FT_PARSEDATE, TFL_PUTN,
200 "mon", TF_COMP, FT_LV_MON, FT_PARSEDATE, TFL_PUTN,
201 "year", TF_COMP, FT_LV_YEAR, FT_PARSEDATE, TFL_PUTN,
202 "yday", TF_COMP, FT_LV_YDAY, FT_PARSEDATE, TFL_PUTN,
203 "wday", TF_COMP, FT_LV_WDAY, FT_PARSEDATE, TFL_PUTN,
204 "zone", TF_COMP, FT_LV_ZONE, FT_PARSEDATE, TFL_PUTN,
205 "clock", TF_COMP, FT_LV_CLOCK, FT_PARSEDATE, TFL_PUTN,
206 "rclock", TF_COMP, FT_LV_RCLOCK, FT_PARSEDATE, TFL_PUTN,
207 "sday", TF_COMP, FT_LV_DAYF, FT_PARSEDATE, TFL_PUTN,
208 "szone", TF_COMP, FT_LV_ZONEF, FT_PARSEDATE, TFL_PUTN,
209 "dst", TF_COMP, FT_LV_DST, FT_PARSEDATE, TFL_PUTN,
210 "pretty", TF_COMP, FT_LS_PRETTY, FT_PARSEDATE, TFL_PUTS,
211 "nodate", TF_COMP, FT_LV_COMPFLAG, FT_PARSEDATE, TFL_PUTN,
212 "date2local", TF_COMP, FT_LOCALDATE, FT_PARSEDATE, 0,
213 "date2gmt", TF_COMP, FT_GMTDATE, FT_PARSEDATE, 0,
215 "pers", TF_COMP, FT_LS_PERS, FT_PARSEADDR, TFL_PUTS,
216 "mbox", TF_COMP, FT_LS_MBOX, FT_PARSEADDR, TFL_PUTS,
217 "host", TF_COMP, FT_LS_HOST, FT_PARSEADDR, TFL_PUTS,
218 "path", TF_COMP, FT_LS_PATH, FT_PARSEADDR, TFL_PUTS,
219 "gname", TF_COMP, FT_LS_GNAME, FT_PARSEADDR, TFL_PUTS,
220 "note", TF_COMP, FT_LS_NOTE, FT_PARSEADDR, TFL_PUTS,
221 "addr", TF_COMP, FT_LS_ADDR, FT_PARSEADDR, TFL_PUTS,
222 "proper", TF_COMP, FT_LS_822ADDR, FT_PARSEADDR, TFL_PUTS,
223 "type", TF_COMP, FT_LV_HOSTTYPE, FT_PARSEADDR, TFL_PUTN,
224 "ingrp", TF_COMP, FT_LV_INGRPF, FT_PARSEADDR, TFL_PUTN,
225 "nohost", TF_COMP, FT_LV_NOHOSTF, FT_PARSEADDR, TFL_PUTN,
226 "formataddr", TF_EXPR_SV, FT_FORMATADDR, FT_FORMATADDR, 0,
227 "friendly", TF_COMP, FT_LS_FRIENDLY, FT_PARSEADDR, TFL_PUTS,
229 "mymbox", TF_COMP, FT_LV_COMPFLAG, FT_MYMBOX, TFL_PUTN,
230 "addtoseq", TF_STR, FT_ADDTOSEQ, 0, 0,
232 (char *)0, 0, 0, 0, 0
238 static struct ftable *lookup(name)
241 register struct ftable *t = functable;
243 register char c = *name;
245 while (nm = t->name) {
246 if (*nm == c && strcmp (nm, name) == 0)
251 return (struct ftable *)0;
255 #define NEWCOMP(cm,name)\
256 cm = ((struct comp *)calloc(1, sizeof (struct comp)));\
257 cm->c_name = name; ncomp++;\
258 i = CHASH(name); cm->c_next = wantcomp[i]; wantcomp[i] = cm;
260 #define NEWFMT (next_fp++)
261 #define NEW(type,fill,wid)\
262 fp=NEWFMT; fp->f_type=(type); fp->f_fill=(fill); fp->f_width=(wid);
265 FINDCOMP( cm, name );\
271 #define LV( type, value ) NEW(type,0,0); fp->f_value = (value);
272 #define LS( type, str ) NEW(type,0,0); fp->f_text = (str);
273 #define PUTCOMP( comp ) NEW(FT_COMP,0,0); ADDC(comp);
274 #define PUTLIT( str ) NEW(FT_LIT,0,0); fp->f_text = (str);
275 #define PUTC( c ) NEW(FT_CHAR,0,0); fp->f_char = (c);
277 static char *compile();
278 static char *do_spec();
279 static char *do_name();
280 static char *do_func();
281 static char *do_expr();
282 static char *do_if();
283 static char *do_loop();
285 static char *format_string;
286 static char *usr_fstring; /* for CERROR */
288 #define CERROR(str) compile_error (str, cp)
291 compile_error(str, cp)
295 int errpos = cp - format_string;
296 int errctx = errpos > 20 ? 20 : errpos;
299 usr_fstring[errpos] = '\0';
300 for (i = errpos-errctx; i < errpos; i++)
302 if (iscntrl(usr_fstring[i]))
304 if (usr_fstring[i] < 32)
306 usr_fstring[i] = '_';
307 advise(NULLCP, "\"%s\": format compile error - %s",
308 &usr_fstring[errpos-errctx], str);
309 adios (NULLCP, "%*s", errctx+1, "^");
313 * Compile format string "fstring" into format list "fmt".
314 * Return the number of header components found in the format
317 fmt_compile( fstring, fmt )
318 register char *fstring;
325 (void) free (format_string);
326 format_string = getcpy (fstring);
327 usr_fstring = fstring;
329 /* init the component hash table. */
330 for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++)
333 bzero ((char *) &fmt_mnull, sizeof fmt_mnull);
335 /* it takes at least 4 char to generate one format so we
336 * allocate a worst-case format array using 1/4 the length
337 * of the format string. We actually need twice this much
338 * to handle both pre-processing (e.g., address parsing) and
341 i = strlen(fstring)/2 + 1;
342 next_fp = formatvec = (struct format *)calloc ((unsigned) i,
343 sizeof(struct format));
345 adios (NULLCP, "unable to allocate format storage");
350 cp = compile(format_string);
352 CERROR("extra '%>', '%|' or '%?'");
354 LV(FT_DONE,0); /* really done */
360 static char *compile (sp)
363 register char *cp = sp;
368 while ((c = *cp) && c != '%')
404 case ';': /* comment line */
406 while ((c = *cp++) && c != '\n')
418 static char *do_spec(sp)
421 register char *cp = sp;
424 register int ljust = 0;
425 #endif /* not lint */
426 register int wid = 0;
427 register char fill = ' ';
439 wid = wid*10 + (c - '0');
445 fp->f_type = wid? FT_COMPF : FT_COMP;
450 if (ftbl->flags & TFL_PUTS) {
451 LV( wid? FT_STRF : FT_STR, ftbl->extra);
453 else if (ftbl->flags & TFL_PUTN) {
454 LV( wid? FT_NUMF : FT_NUM, ftbl->extra);
459 CERROR("component or function name expected");
469 static char *do_name(sp, preprocess)
473 register char *cp = sp;
476 static int primed = 0;
478 while (isalnum(c = *cp++) || c == '-' || c == '_')
481 CERROR("'}' expected");
485 switch (preprocess) {
488 if (cm->c_type & CT_ADDR) {
489 CERROR("component used as both date and address");
491 if (! (cm->c_type & CT_DATE)) {
492 cm->c_tws = (struct tws *)
493 calloc((unsigned) 1, sizeof *cm -> c_tws);
494 fp->f_type = preprocess;
496 cm->c_type |= CT_DATE;
502 (void) ismymbox ((struct mailname *) 0);
505 cm->c_type |= CT_MYMBOX;
508 if (cm->c_type & CT_DATE) {
509 CERROR("component used as both date and address");
511 if (! (cm->c_type & CT_ADDRPARSE)) {
512 cm->c_mn = &fmt_mnull;
513 fp->f_type = preprocess;
515 cm->c_type |= (CT_ADDR | CT_ADDRPARSE);
520 if (cm->c_type & CT_DATE) {
521 CERROR("component used as both date and address");
523 cm->c_type |= CT_ADDR;
529 static char *do_func(sp)
532 register char *cp = sp;
534 register struct ftable *t;
536 int mflag; /* minus sign in NUM */
540 while (isalnum(c = *cp++))
542 if (c != '(' && c != '{' && c != ' ' && c != ')') {
543 CERROR("'(', '{', ' ' or ')' expected");
546 if ((t = lookup (sp)) == 0) {
547 CERROR("unknown function");
556 CERROR("component name expected");
558 cp = do_name(cp, t->extra);
559 fp->f_type = t->f_type;
564 if (mflag = (c == '-'))
568 n = n*10 + (c - '0');
578 while (c && c != ')')
585 LV(t->f_type,t->extra);
589 LS(t->f_type, getusr());
593 LV(t->f_type, time((long *) 0));
601 cp = do_expr(cp, t->extra);
609 cp = do_expr(cp, t->extra);
615 CERROR("')' expected");
621 static char *do_expr (sp, preprocess)
624 register char *cp = sp;
627 if ((c = *cp++) == '{') {
628 cp = do_name (cp, preprocess);
629 fp->f_type = FT_LS_COMP;
630 } else if (c == '(') {
632 } else if (c == ')') {
634 } else if (c == '%' && *cp == '<') {
637 CERROR ("'(', '{', '%<' or ')' expected");
642 static char *do_loop(sp)
645 register char *cp = sp;
646 struct format *floop;
651 CERROR ("']' expected");
653 LV(FT_DONE, 1); /* not yet done */
655 fp->f_skip = floop - fp; /* skip backwards */
660 static char *do_if(sp)
663 register char *cp = sp;
664 register struct format *fexpr,
665 *fif = (struct format *)NULL;
666 register int c = '<';
669 if (c == '<') { /* doing an IF */
670 if ((c = *cp++) == '{') /*}*/{
672 fp->f_type = FT_LS_COMP;
677 /* see if we can merge the load and the "if" */
678 if (ftbl->f_type >= IF_FUNCS)
679 fp->f_type = ftbl->extra;
685 CERROR("'(' or '{' expected"); /*}*/
689 fexpr = fp; /* loc of [ELS]IF */
690 cp = compile (cp); /* compile IF TRUE stmts */
692 fif->f_skip = next_fp - fif;
694 if ((c = *cp++) == '|') { /* the last ELSE */
696 fif = fp; /* loc of GOTO */
697 fexpr->f_skip = next_fp - fexpr;
699 fexpr = (struct format *)NULL;/* no extra ENDIF */
701 cp = compile (cp); /* compile ELSE stmts */
702 fif->f_skip = next_fp - fif;
705 else if (c == '?') { /* another ELSIF */
707 fif = fp; /* loc of GOTO */
708 fexpr->f_skip = next_fp - fexpr;
710 c = '<'; /* impersonate an IF */
717 CERROR("'>' expected.");
720 if (fexpr) /* IF ... [ELSIF ...] ENDIF */
721 fexpr->f_skip = next_fp - fexpr;
730 @add %(profile xxx) to return m_find(xxx)
735 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.15 1992/12/15 00:20:22 jromine Exp jromine $";
738 while (isalnum(c = *cp++) || c == '-')
749 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.14 1992/10/26 22:45:35 jromine Exp jromine $";
761 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.13 1992/02/09 21:42:12 jromine Exp jromine $";
771 @allow comment lines with '%; ... \n'
776 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.12 1992/02/09 07:09:49 jromine Exp jromine $";
784 @fix bug in %(zero)/%(nonzero) (actually, all TF_EXPR
785 formats which include a IF_V_... test
787 put Van Jacobson changes under "options LBL"
792 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.11 1992/02/05 04:57:21 jromine Exp $";
799 @add unseen - uses dat[5]
804 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.10 1992/01/23 23:10:58 jromine Exp jromine $";
815 LV(t->f_type, t->extra);
821 @new formatsbr changes under JLR
826 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.9 1991/01/10 08:35:40 mh Exp jromine $";
839 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.8 91/01/09 22:33:06 mh Exp Locker: mh $";
859 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.7 91/01/09 22:19:43 mh Exp Locker: mh $";
862 register struct format *fexpr, *fif = (struct format *)NULL;
868 if ((c = *cp++) == '|') { /* another ELSE/ELSIF */
871 fexpr->f_skip = next_fp - fexpr; /* next stmt */
874 fexpr = (struct format *)NULL;
877 cp = compile (cp); /* compile ELSE stmts */
880 fexpr->f_skip = next_fp - fexpr; /* next stmt */
883 c = '<'; /* impresonate an IF */
897 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.6 91/01/09 16:31:16 mh Exp Locker: mh $";
900 CERROR("extra '%>' or '%|'");
903 if ((c = *cp) == '<') {
907 else if (c == '\\') /* allow quoted '<' chars */
915 @allow "ELSEIF" with "%|<"
921 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.5 90/12/27 17:19:13 mh Exp Locker: mh $";
928 @add "addr" address format.
934 static char ident[] = "@@(#)$Id: fmtcompile.c,v 1.4 90/04/05 15:32:23 sources Exp Locker: mh $";
937 register struct format *fexpr, *fif, *felse;
941 if ((c = *cp++) == '{') {
943 fp->f_type = FT_LS_COMP;
948 /* see if we can merge the load and the "if" */
949 if (ftbl->f_type >= IF_FUNCS)
950 fp->f_type = ftbl->extra;
956 CERROR("'(' or '{' expected");
960 if ((c = *cp++) == '|') {
965 fif->f_skip = next_fp - fif;
972 fexpr->f_skip = felse - fexpr;
983 static char ident[] = "@@(#)$Id:$";
995 static char ident[] = "$Id:";
1001 @Fixes from Van Jacobson
1017 "dst", TF_COMP, FT_LV_TZONEF, FT_PARSEDATE, TFL_PUTN,
1022 static compile_error (str, cp)
1026 ljust++; /* should do something with this */