File: setmaps.c

package info (click to toggle)
jove 4.16.0.72-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,012 kB
  • ctags: 2,932
  • sloc: ansic: 27,591; makefile: 472; sh: 46
file content (271 lines) | stat: -rw-r--r-- 5,832 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**************************************************************************
 * This program is Copyright (C) 1986-2002 by Jonathan Payne.  JOVE is    *
 * provided by Jonathan and Jovehacks without charge and without          *
 * warranty.  You may copy, modify, and/or distribute JOVE, provided that *
 * this notice is included in all the source files and documentation.     *
 **************************************************************************/

#include <stdio.h>
#include "jove.h"
#include "chars.h"
#include "commands.h"
#include "vars.h"

#define LINESIZE	100	/* hope this is big enough */
#define STACKLIMIT	10	/* max conditional depth */

#define	PROC(p)	NULL	/* discard function pointers */
#include "commands.tab"

#define VAR(v)	NULL, (size_t)0	/* discard variable pointers */
#include "vars.tab"

private int
matchcmd(choices, what)
register const struct cmd	choices[];
register char	*what;
{
	register int	i;

	for (i = 0; choices[i].Name != NULL; i++) {
		if (what[0] == choices[i].Name[0]
		&& strcmp(what, choices[i].Name) == 0)
			return i;
	}
	return -1;
}

#ifdef MAC
matchvar(choices, what)
register const struct variable choices[];
register char	*what;
{
	register int	len;
	int	i;

	len = strlen(what);
	for (i = 0; choices[i].Name != NULL; i++) {
		if (what[0] == choices[i].Name[0]
		&& strcmp(what, choices[i].Name) == 0)
			return i;
	}
	return -1;
}
#endif

private int
StartsWith(s, pre)
const char *s, *pre;
{
    return strncmp(s, pre, strlen(pre)) == 0;
}

private char *
PPchar(c)
int	c;
{
	static char	str[16];
	char	*cp = str;

	if (c & METABIT) {
		c &= ~METABIT;
		strcpy(cp, "M-");
		cp += 2;
	}
	if (c == ESC)
		strcpy(cp, "ESC");
	else if (c < ' ')
		(void) sprintf(cp, "^%c", c + '@');
	else if (c == DEL)
		strcpy(cp, "^?");
	else
		(void) sprintf(cp, "%c", c);
	return str;
}

private void
extract(into, from)
char	*into,
	*from;
{
	from += 2;	/* Past tab and first double quote. */
	while ((*into = *from++) != '"')
		into += 1;
	*into = '\0';
}


int
main()
{
	FILE
		*ifile,
		*of;
	char
		line[LINESIZE],
		comname[LINESIZE];
	int
		comnum,
		lino,
		ch;
	struct {
		int	first;
		int	last;
		char	condition[LINESIZE];
	}
		stackspace[STACKLIMIT],	/* first entry not used */
		*sp = stackspace;
#ifdef MAC
	char	*which;
	int	filecnt = 0;
	bool	inmenu = NO;
	struct fname {
		char	*in, *out;
	};
	static const struct fname	fnt[] = {
			{ "keys.txt", "keys.c" },
			{ "menumaps.txt", "menumaps.c" },
			{ NULL, NULL }
	};
	const struct fname *fnp;
#endif /* MAC */

	for (comnum = 1; commands[comnum].Name != NULL; comnum++) {
		if (strcmp(commands[comnum-1].Name, commands[comnum].Name) >= 0) {
			fprintf(stderr, "command %s is out of order\n",
				commands[comnum].Name);
			exit(1);
		}
	}
	for (comnum = 1; variables[comnum].Name != NULL; comnum++) {
		if (strcmp(variables[comnum-1].Name, variables[comnum].Name) >= 0) {
			fprintf(stderr, "variable %s is out of order\n", variables[comnum].Name);
			exit(1);
		}
	}
#ifdef MAC
	/* don't know how to redirect, so we do tricks */
for (fnp = fnt; fnp->in != NULL; fnp++) {
	printf("setmaps <%s >%s\n", fnp->in, fnp->out);
	ifile = fopen(fnp->in, "r");
	if (ifile == NULL) {
		perror(fnp->in);
		exit(1);
	}
	of = fopen(fnp->out, "w");
	if (of == NULL) {
		perror(fnp->out);
		exit(1);
	}
#else /* !MAC */
	ifile = stdin;
	of = stdout;
	if (ifile == NULL || of == NULL) {
		fprintf(stderr, "Cannot read input or write output.\n");
		exit(1);
	}
#endif /* !MAC */
	lino = 0;
	ch = 0;
	for (;;) {
		if (fgets(line, sizeof line, ifile) == NULL) {
			if (sp != stackspace) {
				fprintf(stderr, "EOF inside #if\n");
				exit(1);
			}
			fclose(of);
			fclose(ifile);
			break;
		}
		lino += 1;
		if (StartsWith(line, "#if")) {
			sp += 1;
			if (sp == &stackspace[STACKLIMIT]) {
				fprintf(stderr,
					"conditionals nested too deeply at line %d\n",
					lino);
				exit(1);
			}
			sp->first = ch;
			sp->last = -1;
			strcpy(sp->condition, line);
			fputs(line, of);
		} else if (StartsWith(line, "#else")) {
			if (sp == stackspace || sp->last != -1) {
				fprintf(stderr, "ifdef/endif mismatch at line %d!\n",
					lino);
				exit(1);
			}
			sp->last = ch;
			ch = sp->first;
			fputs(line, of);
		} else if (StartsWith(line, "#endif")) {
			if (sp == stackspace) {
				fprintf(stderr, "ifdef/endif mismatch at line %d!\n",
					lino);
				exit(1);
			}
			if (sp->last != -1 && ch != sp->last) {
				fprintf(stderr,
					"warning: unbalanced number of entries in #if ending at line %d",
					lino);
			}
			sp -= 1;
			fputs(line, of);
#ifdef MAC
		} else if (StartsWith(line, "#MENU")) {
			inmenu = YES;
#endif
		} else if (StartsWith(line, "\t\"")) {
			extract(comname, line);
			if (strcmp(comname, "unbound") == 0) {
				comnum = -1;
			} else {
				comnum = matchcmd(commands, comname);
#ifdef MAC
				which = "commands";
				if (comnum < 0 && inmenu) {
					comnum = matchvar(variables, comname);
					which = "variables";
				}
#endif
				if (comnum < 0) {
					fprintf(stderr,
						"warning: cannot find \"%s\", line %d",
						comname, lino);
					if (sp == stackspace) {
						fprintf(stderr, ".\n");
					} else {
						/* Note: condition ends with \n */
						fprintf(stderr, ", inside%s %s",
							sp->last == -1? "" : " else of",
							sp->condition);
					}
				}
			}
#ifdef MAC
			if (inmenu) {
				if (comnum < 0)
					fprintf(of, "\t(data_obj *) NULL,\n");
				else
					fprintf(of, "\t(data_obj *) &%s[%d],\n",which, comnum);
			} else /*...*/
#endif
			{
				if (comnum < 0)
					fprintf(of, "\t(data_obj *) NULL,\t\t/* %s */\n", PPchar(ch));
				else
					fprintf(of, "\t(data_obj *) &commands[%d],\t/* %s */\n", comnum, PPchar(ch));
				ch += 1;
			}
		} else {
			/* If unrecognized, pass and prepare to start new table */
			fputs(line, of);
			ch = 0;
		}
	}
#ifdef MAC
}
#endif
	return 0;
}