Debian Codegraph @

setmaps.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 /*
9 * Setmaps runs at build time, which makes life hard for
10 * cross-compilers: we need the tune.h/sysdep.h definitions
11 * for the target machine, i.e. whatever SYSDEFS the makefile
12 * provides, but those may not be appropriate for the host,
13 * which, might be a very different compiler.
14 */
15
16 #define NO_EXTERNS
17 #include "jove.h"
18 #include "chars.h"
19 #include "commands.h"
20 #include "vars.h"
21
22 #define LINESIZE 100 /* hope this is big enough */
23 #define STACKLIMIT 10 /* max conditional depth */
24
25 #define PROC(p) (cmdproc_t)0 /* discard function pointers */
26 #include "commands.tab"
27
28 #define VAR(v) NULL, (size_t)0 /* discard variable pointers */
29 #include "vars.tab"
30
31 private int
32 matchcmd(const struct cmd choices[], char *what)
33 {
34 int i;
35
36 for (i = 0; choices[i].Name != NULL; i++) {
37 if (what[0] == choices[i].Name[0]
38 && strcmp(what, choices[i].Name) == 0)
39 return i;
40 }
41 return -1;
42 }
43
44 #ifdef MAC
45 matchvar(const struct variable choices[], char *what)
46 {
47 int len,
48 i;
49
50 len = strlen(what);
51 for (i = 0; choices[i].Name != NULL; i++) {
52 if (what[0] == choices[i].Name[0]
53 && strcmp(what, choices[i].Name) == 0)
54 return i;
55 }
56 return -1;
57 }
58 #endif
59
60 private int
61 StartsWith(const char *s, const char *pre)
62 {
63 return strncmp(s, pre, strlen(pre)) == 0;
64 }
65
66 private char *
67 PPkey(int c)
68 {
69 static char str[16];
70 char *cp = str;
71
72 if (c & METABIT) {
73 c &= ~METABIT;
74 strcpy(cp, "M-");
75 cp += 2;
76 }
77 if (c == ESC) {
78 strcpy(cp, "ESC");
79 } else if (c < ' ') {
80 *cp++ = '^';
81 *cp++ = c + '@';
82 *cp = '\0';
83 } else if (c == DEL) {
84 strcpy(cp, "^?");
85 } else {
86 *cp++ = c;
87 *cp = '\0';
88 }
89 return str;
90 }
91
92 private void
93 extract(char *into, char *from)
94 {
95 from += 2; /* Past tab and first double quote. */
96 while ((*into = *from++) != '"')
97 into += 1;
98 *into = '\0';
99 }
100
101
102 int
103 main(int UNUSED(argc), char UNUSED(*argv[]))
104 {
105 FILE
106 *ifile,
107 *of;
108 char
109 line[LINESIZE],
110 comname[LINESIZE];
111 int
112 comnum,
113 lino,
114 ch;
115 struct {
116 int first;
117 int last;
118 char condition[LINESIZE];
119 }
120 stackspace[STACKLIMIT], /* first entry not used */
121 *sp = stackspace;
122 #ifdef MAC
123 char *which;
124 int filecnt = 0;
125 jbool inmenu = NO;
126 struct fname {
127 char *in, *out;
128 };
129 static const struct fname fnt[] = {
130 { "keys.txt", "keys.c" },
131 { "menumaps.txt", "menumaps.c" },
132 { NULL, NULL }
133 };
134 const struct fname *fnp;
135 #endif /* MAC */
136
137 static int cmdidx[IDXSZ], varidx[IDXSZ];
138 int lastidx = -1, ic;
139
140 for (comnum = 0; commands[comnum+1].Name != NULL; comnum++) {
141 ic = IDX(commands[comnum].Name[0]);
142 if (ic < 0 || ic >= IDXSZ) {
143 fprintf(stderr, "command %s idx %d is out of range, must be 0..%d\n",
144 commands[comnum].Name, ic, IDXSZ);
145 exit(1);
146 }
147 if (lastidx != ic) {
148 cmdidx[ic] = comnum;
149 lastidx = ic;
150 }
151 if (strcmp(commands[comnum].Name, commands[comnum+1].Name) >= 0) {
152 fprintf(stderr, "command %s is out of order\n",
153 commands[comnum].Name);
154 exit(1);
155 }
156 }
157 lastidx = -1;
158 for (comnum = 0; variables[comnum+1].Name != NULL; comnum++) {
159 ic = IDX(variables[comnum].Name[0]);
160 if (ic < 0 || ic >= IDXSZ) {
161 fprintf(stderr, "variable %s idx %d is out of range, must be 0..%d\n",
162 variables[comnum].Name, ic, IDXSZ);
163 exit(1);
164 }
165 if (lastidx != ic) {
166 varidx[ic] = comnum;
167 lastidx = ic;
168 }
169 if (strcmp(variables[comnum].Name, variables[comnum+1].Name) >= 0) {
170 fprintf(stderr, "variable %s is out of order\n", variables[comnum].Name);
171 exit(1);
172 }
173 }
174 #ifdef MAC
175 /* don't know how to redirect, so we do tricks */
176 for (fnp = fnt; fnp->in != NULL; fnp++) {
177 printf("setmaps <%s >%s\n", fnp->in, fnp->out);
178 ifile = fopen(fnp->in, "r");
179 if (ifile == NULL) {
180 perror(fnp->in);
181 exit(1);
182 }
183 of = fopen(fnp->out, "w");
184 if (of == NULL) {
185 perror(fnp->out);
186 exit(1);
187 }
188 #else /* !MAC */
189 ifile = stdin;
190 of = stdout;
191 if (ifile == NULL || of == NULL) {
192 fprintf(stderr, "Cannot read input or write output.\n");
193 exit(1);
194 }
195 #endif /* !MAC */
196 lino = 0;
197 ch = 0;
198 for (;;) {
199 if (fgets(line, (int)sizeof(line), ifile) == NULL) {
200 if (sp != stackspace) {
201 fprintf(stderr, "EOF inside #if\n");
202 exit(1);
203 }
204 fclose(of);
205 fclose(ifile);
206 break;
207 }
208 lino += 1;
209 if (StartsWith(line, "/* IDX_TAG")) {
210 int i, idx;
211 fputs("const struct cmd *cmdidx[IDXSZ] = {\n", of);
212 for (i = 0; i < IDXSZ; i++) {
213 if (i == 0 || cmdidx[i] != 0) {
214 idx = cmdidx[i];
215 fprintf(of, "\t&commands[%d], /* %s */\n",
216 idx, commands[idx].Name);
217 } else {
218 fputs("\tNULL,\n", of);
219 }
220 }
221 fputs("};\nconst struct variable *varidx[IDXSZ] = {\n", of);
222 for (i = 0; i < IDXSZ; i++) {
223 if (i == 0 || varidx[i] != 0) {
224 idx = varidx[i];
225 fprintf(of, "\t&variables[%d], /* %s */\n",
226 idx, variables[idx].Name);
227 } else {
228 fputs("\tNULL,\n", of);
229 }
230 }
231 fputs("};", of);
232 } else if (StartsWith(line, "#if")) {
233 sp += 1;
234 if (sp == &stackspace[STACKLIMIT]) {
235 fprintf(stderr,
236 "conditionals nested too deeply at line %d\n",
237 lino);
238 exit(1);
239 }
240 sp->first = ch;
241 sp->last = -1;
242 strcpy(sp->condition, line);
243 fputs(line, of);
244 } else if (StartsWith(line, "#else")) {
245 if (sp == stackspace || sp->last != -1) {
246 fprintf(stderr, "ifdef/endif mismatch at line %d!\n",
247 lino);
248 exit(1);
249 }
250 sp->last = ch;
251 ch = sp->first;
252 fputs(line, of);
253 } else if (StartsWith(line, "#endif")) {
254 if (sp == stackspace) {
255 fprintf(stderr, "ifdef/endif mismatch at line %d!\n",
256 lino);
257 exit(1);
258 }
259 if (sp->last != -1 && ch != sp->last) {
260 fprintf(stderr,
261 "warning: unbalanced number of entries in #if ending at line %d",
262 lino);
263 }
264 sp -= 1;
265 fputs(line, of);
266 #ifdef MAC
267 } else if (StartsWith(line, "#MENU")) {
268 inmenu = YES;
269 #endif
270 } else if (StartsWith(line, "\t\"")) {
271 extract(comname, line);
272 if (strcmp(comname, "unbound") == 0) {
273 comnum = -1;
274 } else {
275 comnum = matchcmd(commands, comname);
276 #ifdef MAC
277 which = "commands";
278 if (comnum < 0 && inmenu) {
279 comnum = matchvar(variables, comname);
280 which = "variables";
281 }
282 #endif
283 if (comnum < 0) {
284 fprintf(stderr,
285 "warning: cannot find \"%s\", line %d",
286 comname, lino);
287 if (sp == stackspace) {
288 fprintf(stderr, ".\n");
289 } else {
290 /* Note: condition ends with \n */
291 fprintf(stderr, ", inside%s %s",
292 sp->last == -1? "" : " else of",
293 sp->condition);
294 }
295 }
296 }
297 #ifdef MAC
298 if (inmenu) {
299 if (comnum < 0)
300 fprintf(of, "\t(data_obj *) NULL,\n");
301 else
302 fprintf(of, "\t(data_obj *) &%s[%d],\n",which, comnum);
303 } else /*...*/
304 #endif
305 {
306 if (comnum < 0)
307 fprintf(of, "\t(data_obj *) NULL,\t\t/* %s %s */\n", PPkey(ch), comname);
308 else
309 fprintf(of, "\t(data_obj *) &commands[%d],\t/* %s %s */\n", comnum, PPkey(ch), comname);
310 ch += 1;
311 }
312 } else {
313 /* If unrecognized, pass and prepare to start new table */
314 fputs(line, of);
315 ch = 0;
316 }
317 }
318 #ifdef MAC
319 }
320 #endif
321 return 0;
322 }