1 /*
2 * macro_test.c
3 *
4 * Test driver for the btparse macro table. Reads simple one-line commands
5 * from stdin; each one consists of a one-letter action code and possibly
6 * some arguments. The allowed actions are:
7 * a <macro> <text> - add macro
8 * p <macro> - print expansion of macro
9 * d <macro> - delete macro
10 * l - delete all macros
11 *
12 * There must be exactly one space between the action and <macro>, and
13 * between <macro> and <text> (where appropriate).
14 *
15 * GPW 1998/03/01
16 *
17 * $Id: macro_test.c 723 2003-10-19 18:57:37Z greg $
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include "btparse.h"
24
25
26 int
main(void)27 main (void)
28 {
29 char line[1024];
30 int line_num;
31 int i;
32 char action;
33 char * macro;
34 char * text;
35
36 bt_initialize();
37
38 /*
39 * Read lines from stdin. Each one starts with a single-letter command,
40 * which may be one of the following:
41 */
42
43 line_num = 0;
44 while (! feof (stdin))
45 {
46 if (fgets (line, 1024, stdin))
47 {
48 line_num++;
49 action = line[0];
50 if (action != 'l') /* other commands take <macro> arg */
51 {
52 line[1] = (char) 0;
53 i = 2;
54 macro = line+2;
55 while (! isspace (line[i])) i++;
56 line[i++] = (char) 0;
57 text = line+i;
58 text[strlen(text)-1] = (char) 0; /* wipe the newline */
59 }
60
61 switch (action)
62 {
63 case 'a':
64 bt_add_macro_text (macro, text, "stdin", line_num);
65 break;
66 case 'p':
67 text = bt_macro_text (macro, "stdin", line_num);
68 if (text)
69 printf ("%s\n", text);
70 break;
71 case 'd':
72 bt_delete_macro (macro);
73 break;
74 case 'l':
75 bt_delete_all_macros ();
76 break;
77 default:
78 fprintf (stderr, "unknown command '%c'\n", action);
79 }
80
81 /* zzs_stat(); */
82
83 }
84
85 } /* while !eof */
86
87 bt_cleanup();
88 return 0;
89 }
90