]> diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/uip/RCS/fmtdump.c,v
sbr/mts.c: Delete mmdlm2; use same-valued mmdlm1 instead.
[nmh] / docs / historical / mh-6.8.5 / uip / RCS / fmtdump.c,v
1 head 1.10;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.10
9 date 92.11.04.00.42.31; author jromine; state Exp;
10 branches;
11 next 1.9;
12
13 1.9
14 date 92.02.13.20.50.54; author jromine; state Exp;
15 branches;
16 next 1.8;
17
18 1.8
19 date 92.02.09.07.14.20; author jromine; state Exp;
20 branches;
21 next 1.7;
22
23 1.7
24 date 92.01.24.00.04.45; author jromine; state Exp;
25 branches;
26 next 1.6;
27
28 1.6
29 date 91.11.27.23.52.39; author jromine; state Exp;
30 branches;
31 next 1.5;
32
33 1.5
34 date 91.11.27.23.50.51; author jromine; state Exp;
35 branches;
36 next 1.4;
37
38 1.4
39 date 91.11.27.23.44.22; author jromine; state Exp;
40 branches;
41 next 1.3;
42
43 1.3
44 date 91.01.25.16.08.27; author mh; state Exp;
45 branches;
46 next 1.2;
47
48 1.2
49 date 91.01.25.15.38.06; author mh; state Exp;
50 branches;
51 next 1.1;
52
53 1.1
54 date 91.01.25.15.37.45; author mh; state Exp;
55 branches;
56 next ;
57
58
59 desc
60 @@
61
62
63 1.10
64 log
65 @LOCALE
66 @
67 text
68 @/* fmtdump.c - compile format file and dump out instructions */
69 #ifndef lint
70 static char ident[] = "@@(#)$Id: fmtdump.c,v 1.9 1992/02/13 20:50:54 jromine Exp jromine $";
71 #endif
72
73 #include "../h/mh.h"
74 #include "../h/formatsbr.h"
75 #include "../h/fmtcompile.h"
76 #include "../h/scansbr.h"
77 #include <stdio.h>
78 #ifdef LOCALE
79 #include <locale.h>
80 #endif
81
82
83 static struct swit switches[] = {
84 #define FORMSW 0
85 "form formatfile", 0,
86 #define FMTSW 1
87 "format string", 5,
88
89 #define HELPSW 2
90 "help", 4,
91
92 NULL, 0
93 };
94
95
96 /* for assignlabel */
97 static struct format *lvec[128];
98 static lused = 0;
99
100 static char *f_typestr(), *c_typestr();
101 static void fmt_dump(), dumpone(), assignlabel(), litputs(), litputc();
102 static int findlabel();
103
104 /* \f */
105
106 /* ARGSUSED */
107
108 main (argc, argv)
109 int argc;
110 char *argv[];
111 {
112 int ncomps;
113 char *cp,
114 *form = NULL,
115 *format = NULL,
116 buf[100],
117 **ap,
118 **argp,
119 *nfs,
120 *arguments[MAXARGS];
121 struct format *fmt;
122
123 #ifdef LOCALE
124 setlocale(LC_ALL, "");
125 #endif
126 invo_name = r1bindex (argv[0], '/');
127 if ((cp = m_find (invo_name)) != NULL) {
128 ap = brkstring (cp = getcpy (cp), " ", "\n");
129 ap = copyip (ap, arguments);
130 }
131 else
132 ap = arguments;
133 (void) copyip (argv + 1, ap);
134 argp = arguments;
135
136 /* \f */
137
138 while (cp = *argp++) {
139 if (*cp == '-')
140 switch (smatch (++cp, switches)) {
141 case AMBIGSW:
142 ambigsw (cp, switches);
143 done (1);
144 case UNKWNSW:
145 adios (NULLCP, "-%s unknown", cp);
146 case HELPSW:
147 (void) sprintf (buf, "%s [switches]",
148 invo_name);
149 help (buf, switches);
150 done (1);
151
152 case FORMSW:
153 if (!(form = *argp++) || *form == '-')
154 adios (NULLCP, "missing argument to %s", argp[-2]);
155 format = NULL;
156 continue;
157 case FMTSW:
158 if (!(format = *argp++) || *format == '-')
159 adios (NULLCP, "missing argument to %s", argp[-2]);
160 form = NULL;
161 continue;
162
163 }
164 if (form)
165 adios (NULLCP, "only one form at a time!");
166 else
167 form = cp;
168 }
169
170 nfs = new_fs (form, format, FORMAT); /* must be before chdir() */
171 ncomps = fmt_compile(nfs, &fmt);
172
173 fmt_dump(fmt);
174 done(0);
175 }
176
177 static void
178 fmt_dump (fmth)
179 register struct format *fmth;
180 {
181 int i;
182 register struct format *fmt, *addr;
183
184 /* Assign labels */
185 for (fmt = fmth; fmt; ++fmt) {
186 i = fmt->f_type;
187 if (i == FT_IF_S ||
188 i == FT_IF_S_NULL ||
189 i == FT_IF_V_EQ ||
190 i == FT_IF_V_NE ||
191 i == FT_IF_V_GT ||
192 i == FT_IF_MATCH ||
193 i == FT_IF_AMATCH ||
194 i == FT_GOTO) {
195 addr = fmt + fmt->f_skip;
196 if (findlabel(addr) < 0)
197 assignlabel(addr);
198 }
199 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
200 break;
201 }
202
203 /* Dump them out! */
204 for (fmt = fmth; fmt; ++fmt) {
205 dumpone(fmt);
206 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
207 break;
208 }
209 }
210
211 static void
212 dumpone(fmt)
213 register struct format *fmt;
214 {
215 register int i;
216
217 if ((i = findlabel(fmt)) >= 0)
218 printf("L%d:", i);
219 putchar('\t');
220
221 fputs(f_typestr((int)fmt->f_type), stdout);
222
223 switch (fmt->f_type) {
224
225 case FT_COMP:
226 case FT_LS_COMP:
227 case FT_LV_COMPFLAG:
228 case FT_LV_COMP:
229 printf(", comp ");
230 litputs(fmt->f_comp->c_name);
231 if (fmt->f_comp->c_type)
232 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
233 if (fmt->f_comp->c_flags)
234 printf(", c_flags %d", fmt->f_comp->c_flags);
235 break;
236
237 case FT_LV_SEC:
238 case FT_LV_MIN:
239 case FT_LV_HOUR:
240 case FT_LV_MDAY:
241 case FT_LV_MON:
242 case FT_LS_MONTH:
243 case FT_LS_LMONTH:
244 case FT_LS_ZONE:
245 case FT_LV_YEAR:
246 case FT_LV_WDAY:
247 case FT_LS_DAY:
248 case FT_LS_WEEKDAY:
249 case FT_LV_YDAY:
250 case FT_LV_ZONE:
251 case FT_LV_CLOCK:
252 case FT_LV_RCLOCK:
253 case FT_LV_DAYF:
254 case FT_LV_ZONEF:
255 case FT_LV_DST:
256 case FT_LS_822DATE:
257 case FT_LS_PRETTY:
258 case FT_LOCALDATE:
259 case FT_GMTDATE:
260 case FT_PARSEDATE:
261 printf(", c_name ");
262 litputs(fmt->f_comp->c_name);
263 if (fmt->f_comp->c_type)
264 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
265 if (fmt->f_comp->c_flags)
266 printf(", c_flags %d", fmt->f_comp->c_flags);
267 break;
268
269 case FT_LS_ADDR:
270 case FT_LS_PERS:
271 case FT_LS_MBOX:
272 case FT_LS_HOST:
273 case FT_LS_PATH:
274 case FT_LS_GNAME:
275 case FT_LS_NOTE:
276 case FT_LS_822ADDR:
277 case FT_LV_HOSTTYPE:
278 case FT_LV_INGRPF:
279 case FT_LV_NOHOSTF:
280 case FT_LS_FRIENDLY:
281 case FT_PARSEADDR:
282 case FT_MYMBOX:
283 printf(", c_name ");
284 litputs(fmt->f_comp->c_name);
285 if (fmt->f_comp->c_type)
286 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
287 if (fmt->f_comp->c_flags)
288 printf(", c_flags %d", fmt->f_comp->c_flags);
289 break;
290
291 case FT_COMPF:
292 printf(", width %d, fill '", fmt->f_width);
293 litputc(fmt->f_fill);
294 printf("' name ");
295 litputs(fmt->f_comp->c_name);
296 if (fmt->f_comp->c_type)
297 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
298 if (fmt->f_comp->c_flags)
299 printf(", c_flags %d", fmt->f_comp->c_flags);
300 break;
301
302 case FT_STRF:
303 case FT_NUMF:
304 printf(", width %d, fill '", fmt->f_width);
305 litputc(fmt->f_fill);
306 putchar('\'');
307 break;
308
309 case FT_LIT:
310 #ifdef FT_LIT_FORCE
311 case FT_LIT_FORCE:
312 #endif
313 putchar(' ');
314 litputs(fmt->f_text);
315 break;
316
317 case FT_LITF:
318 printf(", width %d, fill '", fmt->f_width);
319 litputc(fmt->f_fill);
320 printf("' ");
321 litputs(fmt->f_text);
322 break;
323
324 case FT_CHAR:
325 putchar(' ');
326 putchar('\'');
327 litputc(fmt->f_char);
328 putchar('\'');
329 break;
330
331
332 case FT_IF_S:
333 case FT_IF_S_NULL:
334 case FT_IF_MATCH:
335 case FT_IF_AMATCH:
336 printf(" continue else goto");
337 case FT_GOTO:
338 i = findlabel(fmt + fmt->f_skip);
339 printf(" L%d", i);
340 break;
341
342 case FT_IF_V_EQ:
343 case FT_IF_V_NE:
344 case FT_IF_V_GT:
345 i = findlabel(fmt + fmt->f_skip);
346 printf(" %d continue else goto L%d", fmt->f_value, i);
347 break;
348
349 case FT_V_EQ:
350 case FT_V_NE:
351 case FT_V_GT:
352 case FT_LV_LIT:
353 case FT_LV_PLUS_L:
354 case FT_LV_MINUS_L:
355 case FT_LV_DIVIDE_L:
356 case FT_LV_MODULO_L:
357 printf(" value %d", fmt->f_value);
358 break;
359
360 case FT_LS_LIT:
361 printf(" str ");
362 litputs(fmt->f_text);
363 break;
364
365 case FT_LS_GETENV:
366 printf(" getenv ");
367 litputs(fmt->f_text);
368 break;
369
370 case FT_LS_TRIM:
371 printf(", width %d", fmt->f_width);
372 break;
373
374 case FT_LV_DAT:
375 printf(", value dat[%d]", fmt->f_value);
376 break;
377 }
378 putchar('\n');
379 }
380
381 static int
382 findlabel(addr)
383 register struct format *addr;
384 {
385 register int i;
386
387 for (i = 0; i < lused; ++i)
388 if (addr == lvec[i])
389 return(i);
390 return(-1);
391 }
392
393 static void
394 assignlabel(addr)
395 register struct format *addr;
396 {
397 lvec[lused++] = addr;
398 }
399
400 static char *
401 f_typestr(t)
402 int t;
403 {
404 static char buf[32];
405
406 switch (t) {
407 case FT_COMP: return("COMP");
408 case FT_COMPF: return("COMPF");
409 case FT_LIT: return("LIT");
410 case FT_LITF: return("LITF");
411 #ifdef FT_LIT_FORCE
412 case FT_LIT_FORCE: return("LIT_FORCE");
413 #endif
414 case FT_CHAR: return("CHAR");
415 case FT_NUM: return("NUM");
416 case FT_NUMF: return("NUMF");
417 case FT_STR: return("STR");
418 case FT_STRF: return("STRF");
419 case FT_STRFW: return("STRFW");
420 case FT_PUTADDR: return("PUTADDR");
421 case FT_LS_COMP: return("LS_COMP");
422 case FT_LS_LIT: return("LS_LIT");
423 case FT_LS_GETENV: return("LS_GETENV");
424 case FT_LS_TRIM: return("LS_TRIM");
425 case FT_LV_COMP: return("LV_COMP");
426 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
427 case FT_LV_LIT: return("LV_LIT");
428 case FT_LV_DAT: return("LV_DAT");
429 case FT_LV_STRLEN: return("LV_STRLEN");
430 case FT_LV_PLUS_L: return("LV_PLUS_L");
431 case FT_LV_MINUS_L: return("LV_MINUS_L");
432 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
433 case FT_LV_MODULO_L: return("LV_MODULO_L");
434 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
435 case FT_LS_MONTH: return("LS_MONTH");
436 case FT_LS_LMONTH: return("LS_LMONTH");
437 case FT_LS_ZONE: return("LS_ZONE");
438 case FT_LS_DAY: return("LS_DAY");
439 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
440 case FT_LS_822DATE: return("LS_822DATE");
441 case FT_LS_PRETTY: return("LS_PRETTY");
442 case FT_LV_SEC: return("LV_SEC");
443 case FT_LV_MIN: return("LV_MIN");
444 case FT_LV_HOUR: return("LV_HOUR");
445 case FT_LV_MDAY: return("LV_MDAY");
446 case FT_LV_MON: return("LV_MON");
447 case FT_LV_YEAR: return("LV_YEAR");
448 case FT_LV_YDAY: return("LV_YDAY");
449 case FT_LV_WDAY: return("LV_WDAY");
450 case FT_LV_ZONE: return("LV_ZONE");
451 case FT_LV_CLOCK: return("LV_CLOCK");
452 case FT_LV_RCLOCK: return("LV_RCLOCK");
453 case FT_LV_DAYF: return("LV_DAYF");
454 case FT_LV_DST: return("LV_DST");
455 case FT_LV_ZONEF: return("LV_ZONEF");
456 case FT_LS_ADDR: return("LS_ADDR");
457 case FT_LS_PERS: return("LS_PERS");
458 case FT_LS_MBOX: return("LS_MBOX");
459 case FT_LS_HOST: return("LS_HOST");
460 case FT_LS_PATH: return("LS_PATH");
461 case FT_LS_GNAME: return("LS_GNAME");
462 case FT_LS_NOTE: return("LS_NOTE");
463 case FT_LS_822ADDR: return("LS_822ADDR");
464 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
465 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
466 case FT_LV_INGRPF: return("LV_INGRPF");
467 case FT_LV_NOHOSTF: return("LV_NOHOSTF");
468 case FT_LOCALDATE: return("LOCALDATE");
469 case FT_GMTDATE: return("GMTDATE");
470 case FT_PARSEDATE: return("PARSEDATE");
471 case FT_PARSEADDR: return("PARSEADDR");
472 case FT_FORMATADDR: return("FORMATADDR");
473 case FT_MYMBOX: return("MYMBOX");
474 #ifdef FT_ADDTOSEQ
475 case FT_ADDTOSEQ: return("ADDTOSEQ");
476 #endif
477 case FT_SAVESTR: return("SAVESTR");
478 #ifdef FT_PAUSE
479 case FT_PAUSE: return ("PAUSE");
480 #endif
481 case FT_DONE: return("DONE");
482 case FT_NOP: return("NOP");
483 case FT_GOTO: return("GOTO");
484 case FT_IF_S_NULL: return("IF_S_NULL");
485 case FT_IF_S: return("IF_S");
486 case FT_IF_V_EQ: return("IF_V_EQ");
487 case FT_IF_V_NE: return("IF_V_NE");
488 case FT_IF_V_GT: return("IF_V_GT");
489 case FT_IF_MATCH: return("IF_MATCH");
490 case FT_IF_AMATCH: return("IF_AMATCH");
491 case FT_S_NULL: return("S_NULL");
492 case FT_S_NONNULL: return("S_NONNULL");
493 case FT_V_EQ: return("V_EQ");
494 case FT_V_NE: return("V_NE");
495 case FT_V_GT: return("V_GT");
496 case FT_V_MATCH: return("V_MATCH");
497 case FT_V_AMATCH: return("V_AMATCH");
498 default:
499 (void)sprintf(buf, "/* ??? #%d */", t);
500 return(buf);
501 }
502 }
503
504 #define FNORD(v, s) if (t & (v)) { \
505 if (i++ > 0) \
506 strcat(buf, "|"); \
507 strcat(buf, s); }
508
509 static char *
510 c_typestr(t)
511 int t;
512 {
513 register int i;
514 static char buf[64];
515
516 buf[0] = '\0';
517 if (t & ~(CT_ADDR|CT_DATE|CT_MYMBOX|CT_ADDRPARSE))
518 (void)sprintf(buf, "0x%x ", t);
519 strcat(buf, "<");
520 i = 0;
521 FNORD(CT_ADDR, "ADDR");
522 FNORD(CT_DATE, "DATE");
523 FNORD(CT_MYMBOX, "MYMBOX");
524 FNORD(CT_ADDRPARSE, "ADDRPARSE");
525 strcat(buf, ">");
526 return(buf);
527 #undef FNORD
528 }
529
530 static void
531 litputs(s)
532 register char *s;
533 {
534 if (s) {
535 putc('"', stdout);
536 while (*s)
537 litputc(*s++);
538 putc('"', stdout);
539 } else
540 fputs("<nil>", stdout);
541 }
542
543 static void
544 litputc(c)
545 char c;
546 {
547 if (c & ~ 0177) {
548 putc('M', stdout);
549 putc('-', stdout);
550 c &= 0177;
551 }
552 if (c < 0x20 || c == 0177) {
553 if (c == '\b') {
554 putc('\\', stdout);
555 putc('b', stdout);
556 } else if (c == '\f') {
557 putc('\\', stdout);
558 putc('f', stdout);
559 } else if (c == '\n') {
560 putc('\\', stdout);
561 putc('n', stdout);
562 } else if (c == '\r') {
563 putc('\\', stdout);
564 putc('r', stdout);
565 } else if (c == '\t') {
566 putc('\\', stdout);
567 putc('t', stdout);
568 } else {
569 putc('^', stdout);
570 putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
571 }
572 } else
573 putc(c, stdout);
574 }
575 @
576
577
578 1.9
579 log
580 @bring into the fold
581 @
582 text
583 @d3 1
584 a3 1
585 static char ident[] = "@@(#)$Id: fmtdump.c,v 1.8 1992/02/09 07:14:20 jromine Exp jromine $";
586 d11 3
587 d56 3
588 @
589
590
591 1.8
592 log
593 @cleanup output slightly
594 add modulo function
595 @
596 text
597 @d1 1
598 d3 1
599 a3 1
600 static char ident[] = "@@(#)$Id: fmtdump.c,v 1.7 1992/01/24 00:04:45 jromine Exp jromine $";
601 a4 8
602 /*
603 * fmtdump - compile format file and dump out instructions
604 *
605 * usage:
606 *
607 * fmtdump [formatfile]
608 *
609 */
610 d9 1
611 a11 2
612 static struct format *lvec[128];
613 static lused = 0;
614 d13 5
615 a17 2
616 char *f_typestr(), *c_typestr();
617 static int findlabel();
618 d19 22
619 a40 4
620 void
621 main(argc, argv)
622 int argc;
623 char **argv;
624 d42 10
625 a51 4
626 int n;
627 struct format *fmt;
628 char *file = "scan.format";
629 char *str;
630 d53 9
631 a61 2
632 if (argc > 1)
633 file = argv[1];
634 d63 1
635 a63 2
636 /* Read format file into memory */
637 str = new_fs(file, 0, 0);
638 d65 13
639 a77 2
640 /* Compile format */
641 n = fmt_compile(str, &fmt);
642 d79 23
643 a101 3
644 /* Dump format */
645 dumpall(fmt);
646 exit(0);
647 d104 3
648 a106 2
649 dumpall(fmth)
650 register struct format *fmth;
651 d138 1
652 d320 1
653 d327 1
654 a327 1
655 char *
656 d436 1
657 a436 1
658 char *
659 d457 1
660 d470 1
661 @
662
663
664 1.7
665 log
666 @add getenv
667 @
668 text
669 @d2 1
670 a2 1
671 static char ident[] = "@@(#)$Id: fmtdump.c,v 1.6 1991/11/27 23:52:39 jromine Exp jromine $";
672 d205 1
673 a205 1
674 printf(" false, goto");
675 d215 1
676 a215 1
677 printf(" %d goto L%d", fmt->f_value, i);
678 d225 1
679 d301 1
680 @
681
682
683 1.6
684 log
685 @*** empty log message ***
686 @
687 text
688 @d2 1
689 a2 1
690 static char ident[] = "@@(#)$Id: fmtdump.c,v 1.5 1991/11/27 23:50:51 jromine Exp jromine $";
691 d69 1
692 a69 1
693 if (fmt->f_type == FT_DONE)
694 d76 1
695 a76 1
696 if (fmt->f_type == FT_DONE)
697 d200 1
698 d233 5
699 d290 1
700 d344 3
701 @
702
703
704 1.5
705 log
706 @fixup
707 @
708 text
709 @d2 1
710 a2 1
711 static char ident[] = "@@(#)$Id: fmtdump.c,v 1.4 1991/11/27 23:44:22 jromine Exp jromine $";
712 d204 1
713 a204 1
714 printf(" false");
715 @
716
717
718 1.4
719 log
720 @explain IF_S statements better
721 @
722 text
723 @d2 1
724 a2 1
725 static char ident[] = "@@(#)$Id: fmtdump.c,v 1.3 91/01/25 16:08:27 mh Exp $";
726 d204 1
727 d207 1
728 a207 1
729 printf(" false goto L%d", i);
730 @
731
732
733 1.3
734 log
735 @add LS_ADDR
736 @
737 text
738 @d2 1
739 a2 1
740 static char ident[] = "@@(#)$Id: fmtdump.c,v 1.2 91/01/25 15:38:06 mh Exp Locker: mh $";
741 d206 1
742 a206 1
743 printf(" L%d", i);
744 @
745
746
747 1.2
748 log
749 @*** empty log message ***
750 @
751 text
752 @d2 1
753 a2 1
754 static char ident[] = "@@(#)$Id: fmtdump.c,v 1.1 91/01/25 15:37:45 mh Exp Locker: mh $";
755 d138 1
756 d314 1
757 @
758
759
760 1.1
761 log
762 @Initial revision
763 @
764 text
765 @d2 1
766 a2 1
767 static char ident[] = "@@(#)$Id: fmtdump.c,v 1.3 90/08/09 22:04:27 leres Exp $ (LBL)";
768 @