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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/* $Header: /src/pub/tcsh/gethost.c,v 1.7 1997/10/28 22:34:19 christos Exp $ */
/*
* gethost.c: Create version file from prototype
*/
/*-
* Copyright (c) 1980, 1991 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "sh.h"
RCSID("$Id: gethost.c,v 1.7 1997/10/28 22:34:19 christos Exp $")
#ifdef SCO
# define perror __perror
# define rename __rename
# define getopt __getopt
# define system __system
#endif
#include <stdio.h>
#ifdef SCO
# undef perror
# undef rename
# undef getopt
# undef system
#endif
#include <ctype.h>
/* Some people don't bother to declare these */
#if defined(SUNOS4) || defined(ibm032)
extern int fprintf();
extern int fclose();
#endif /* SUNOS4 || ibm032 */
#define ISSPACE(p) (isspace((unsigned char) (p)) && (p) != '\n')
/*
* We cannot do that, because some compilers like #line and others
* like # <lineno>
* #define LINEDIRECTIVE
*/
static const char *keyword[] =
{
"vendor",
#define T_VENDOR 0
"hosttype",
#define T_HOSTTYPE 1
"machtype",
#define T_MACHTYPE 2
"ostype",
#define T_OSTYPE 3
"newdef",
#define T_NEWDEF 4
"enddef",
#define T_ENDDEF 5
"newcode",
#define T_NEWCODE 6
"endcode",
#define T_ENDCODE 7
"comment",
#define T_COMMENT 8
"macro",
#define T_MACRO 9
NULL
#define T_NONE 10
};
#define S_DISCARD 0
#define S_COMMENT 1
#define S_CODE 2
#define S_KEYWORD 3
static int findtoken __P((char *));
static char *gettoken __P((char **, char *));
int main __P((int, char *[]));
/* findtoken():
* Return the token number of the given token
*/
static int
findtoken(ptr)
char *ptr;
{
int i;
if (ptr == NULL || *ptr == '\0')
return T_NONE;
for (i = 0; keyword[i] != NULL; i++)
if (strcmp(keyword[i], ptr) == 0)
return i;
return T_NONE;
}
/* gettoken():
* Get : delimited token and remove leading/trailing blanks/newlines
*/
static char *
gettoken(pptr, token)
char **pptr;
char *token;
{
char *ptr = *pptr;
char *tok = token;
for (; *ptr && ISSPACE(*ptr); ptr++)
continue;
for (; *ptr && *ptr != ':'; *tok++ = *ptr++)
continue;
if (*ptr == ':')
ptr++;
else
tok--;
for (tok--; tok >= token && *tok && ISSPACE(*tok); tok--)
continue;
*++tok = '\0';
*pptr = ptr;
return token;
}
int
main(argc, argv)
int argc;
char *argv[];
{
char line[INBUFSIZE];
char *pname;
char *fname = "stdin";
char *ptr, *tok;
char defs[INBUFSIZE];
char stmt[INBUFSIZE];
FILE *fp = stdin;
int lineno = 0;
int inprocess = 0;
int token, state;
int errs = 0;
if ((pname = strrchr(argv[0], '/')) == NULL)
pname = argv[0];
else
pname++;
if (argc > 2) {
(void) fprintf(stderr, "Usage: %s [<filename>]\n", pname);
return 1;
}
if (argc == 2)
if ((fp = fopen(fname = argv[1], "r")) == NULL) {
(void) fprintf(stderr, "%s: Cannot open `%s'\n", pname, fname);
return 1;
}
state = S_DISCARD;
while ((ptr = fgets(line, sizeof(line), fp)) != NULL) {
lineno++;
switch (token = findtoken(gettoken(&ptr, defs))) {
case T_NEWCODE:
state = S_CODE;
break;
case T_ENDCODE:
state = S_DISCARD;
break;
case T_COMMENT:
state = S_COMMENT;
break;
case T_NEWDEF:
state = S_KEYWORD;
break;
case T_ENDDEF:
state = S_DISCARD;
break;
case T_VENDOR:
state = S_KEYWORD;
break;
case T_HOSTTYPE:
state = S_KEYWORD;
break;
case T_MACHTYPE:
state = S_KEYWORD;
break;
case T_OSTYPE:
state = S_KEYWORD;
break;
case T_MACRO:
if (gettoken(&ptr, defs) == NULL) {
(void) fprintf(stderr, "%s: \"%s\", %d: Missing macro name\n",
pname, fname, lineno);
break;
}
if (gettoken(&ptr, stmt) == NULL) {
(void) fprintf(stderr, "%s: \"%s\", %d: Missing macro body\n",
pname, fname, lineno);
break;
}
(void) fprintf(stdout, "\n#if %s\n# define %s\n#endif\n\n", stmt,
defs);
break;
case T_NONE:
if (state != S_CODE && defs && *defs != '\0') {
(void) fprintf(stderr, "%s: \"%s\", %d: Discarded\n",
pname, fname, lineno);
if (++errs == 30) {
(void) fprintf(stderr, "%s: Too many errors\n", pname);
return 1;
}
break;
}
(void) fprintf(stdout, "%s", line);
break;
default:
(void) fprintf(stderr, "%s: \"%s\", %d: Unexpected token\n",
pname, fname, lineno);
return 1;
}
switch (state) {
case S_DISCARD:
if (inprocess) {
inprocess = 0;
(void) fprintf(stdout, "#endif\n");
}
break;
case S_KEYWORD:
tok = gettoken(&ptr, defs);
if (token == T_NEWDEF) {
if (inprocess) {
(void) fprintf(stderr, "%s: \"%s\", %d: Missing enddef\n",
pname, fname, lineno);
return 1;
}
if (tok == NULL) {
(void) fprintf(stderr, "%s: \"%s\", %d: No defs\n",
pname, fname, lineno);
return 1;
}
(void) fprintf(stdout, "\n\n");
#ifdef LINEDIRECTIVE
(void) fprintf(stdout, "# %d \"%s\"\n", lineno + 1, fname);
#endif /* LINEDIRECTIVE */
(void) fprintf(stdout, "#if %s\n", defs);
inprocess = 1;
}
else {
if (tok && *tok)
(void) fprintf(stdout, "# if (%s) && !defined(_%s_)\n",
defs, keyword[token]);
else
(void) fprintf(stdout, "# if !defined(_%s_)\n",
keyword[token]);
if (gettoken(&ptr, stmt) == NULL) {
(void) fprintf(stderr, "%s: \"%s\", %d: No statement\n",
pname, fname, lineno);
return 1;
}
(void) fprintf(stdout, "# define _%s_\n", keyword[token]);
(void) fprintf(stdout, " %s = %s;\n", keyword[token], stmt);
(void) fprintf(stdout, "# endif\n");
}
break;
case S_COMMENT:
if (gettoken(&ptr, defs))
(void) fprintf(stdout, " /* %s */\n", defs);
break;
case S_CODE:
if (token == T_NEWCODE) {
#ifdef LINEDIRECTIVE
(void) fprintf(stdout, "# %d \"%s\"\n", lineno + 1, fname);
#endif /* LINEDIRECTIVE */
}
break;
default:
(void) fprintf(stderr, "%s: \"%s\", %d: Unexpected state\n",
pname, fname, lineno);
return 1;
}
}
if (inprocess) {
(void) fprintf(stderr, "%s: \"%s\", %d: Missing enddef\n",
pname, fname, lineno);
return 1;
}
if (fp != stdin)
(void) fclose(fp);
return 0;
}
|