1 /******************************************************************************
2 * This file is Copyright 1993 by Philip G. Richards. All Rights Reserved.
3 * See the file README that came with this distribution for permissions on
4 * code usage, copying, and distribution. It comes with absolutely no warranty.
5 * email: <pgr@prg.ox.ac.uk>
6 ******************************************************************************/
7
8 /* ---INFOBEGIN--- * DO NOT DELETE THIS COMMENT BLOCK!!!
9 COMMAND macro none "define a named macro"
10 * ---INFOEND--- */
11
12 #include "client.h"
13 #include "macro.h"
14 #include "table.h"
15 #include <ctype.h>
16 #include <stdlib.h>
17
18 typedef enum { MACRO_DEFINE, MACRO_LIST, MACRO_REMOVE } MacroOp;
19
20 static int
define_macro(char * name,char * help)21 define_macro(char *name, char *help)
22 {
23 char buf[1024];
24 int linecnt = 0;
25 char **thismacro = 0;
26
27 ffprintf(STDPROMPT,
28 "enter macro definition terminated by `.' at start of blank line\n");
29
30 while (!feof(STDIN))
31 { /* tidy the string up a little bit (remove leading and trailing WS) */
32 char *sobuf, *eobuf, *pnt;
33
34 ffprintf(STDPROMPT,"[%03d] ", linecnt+1);
35
36 if (my_fgets(buf, 1024, STDIN) == 0)
37 break;
38
39 if (buf[0] == '.' && (buf[1] == '\0' || buf[1] == '\n'))
40 break;
41
42 for (pnt = buf, sobuf = eobuf = (char*)0; *pnt; pnt++)
43 if (!isspace(*pnt))
44 {
45 if (!sobuf) sobuf = pnt;
46 eobuf = pnt;
47 }
48
49 if (!sobuf)
50 continue;
51
52 *(eobuf + 1) = '\0';
53 linecnt++;
54
55 if (thismacro)
56 thismacro = (char**)realloc((char*)thismacro,
57 sizeof(char*) * linecnt);
58 else
59 thismacro = (char**)malloc(sizeof(char*) * linecnt);
60
61 thismacro[linecnt-1] = strdup(sobuf);
62 }
63
64 if (feof(STDIN))
65 {
66 ffprintf(STDERR, "?macro: read EOF before end of macro `%s'\n", name);
67 return 1;
68 }
69
70 return install_macro(name, linecnt, thismacro, help);
71 }
72
73 int
lmacro_main(int argc,char * const * argv,char ** envp)74 lmacro_main(int argc, char *const *argv, char **envp)
75 {
76 int retval, errcnt, ch;
77 MacroOp macro_op;
78
79 optind = 0;
80 opterr = 0;
81 errcnt = 0;
82 #ifdef HAVE_OPTRESET
83 optreset = 1;
84 optind = 1;
85 #endif
86
87 macro_op = MACRO_DEFINE;
88
89 while ((ch = getopt(argc, argv, "lr")) != EOF)
90 switch (ch)
91 {
92 case 'l':
93 macro_op = MACRO_LIST;
94 break;
95 case 'r':
96 macro_op = MACRO_REMOVE;
97 break;
98 default:
99 errcnt++;
100 break;
101 }
102
103 if (errcnt > 0)
104 return 1;
105
106 if (optind == argc)
107 {
108 ffprintf(STDERR, "?macro: macro needs to be named\n");
109 return 1;
110 }
111
112 switch (macro_op)
113 {
114 case MACRO_DEFINE:
115 retval = define_macro(argv[optind], argv[optind+1]);
116 break;
117 case MACRO_LIST:
118 retval = list_macro(argv[optind]);
119 break;
120 case MACRO_REMOVE:
121 retval = remove_macro(argv[optind]);
122 break;
123 }
124
125 return retval;
126 }
127