2 * gethost.c: Create version file from prototype
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 # define perror __perror
36 # define rename __rename
37 # define getopt __getopt
38 # define system __system
50 #define ISSPACE(p) (isspace((unsigned char) (p)) && (p) != '\n')
53 * We cannot do that, because some compilers like #line and others
55 * #define LINEDIRECTIVE
58 static const char *keyword
[] =
89 static int findtoken (char *);
90 static char *gettoken (char **, char *);
93 int main (int, char *[]);
96 * Return the token number of the given token
103 if (ptr
== NULL
|| *ptr
== '\0')
106 for (i
= 0; keyword
[i
] != NULL
; i
++)
107 if (strcmp(keyword
[i
], ptr
) == 0)
115 * Get : delimited token and remove leading/trailing blanks/newlines
118 gettoken(char **pptr
, char *token
)
123 for (; *ptr
&& ISSPACE(*ptr
); ptr
++)
126 for (; *ptr
&& *ptr
!= ':'; *tok
++ = *ptr
++)
134 for (tok
--; tok
>= token
&& *tok
&& ISSPACE(*tok
); tok
--)
144 cat(const char *a
, const char *b
, size_t len
)
155 if ((r
= malloc(l
)) == NULL
)
158 snprintf(r
, l
, "%s%.*s", a
, (int)len
, b
);
160 snprintf(r
, l
, "%.*s", (int)len
, b
);
165 explode(const char *defs
)
167 static const char def
[] = "defined("; /* ) */
170 const char *ptr
, *bptr
, *eptr
= NULL
, *name
;
172 if (strstr(defs
, "#machine(" /* ) */))
174 if (!strstr(defs
, def
))
179 for (ptr
= defs
; (bptr
= strstr(ptr
, def
)) != NULL
; ptr
= eptr
+ 1) {
181 buf
= cat(buf
, ptr
, bptr
- ptr
+ 1);
182 buf
= cat(buf
, "(", 0); /* ) */
183 if ((eptr
= strchr(ptr
+ sizeof(def
) - 1, ')')) == NULL
) {
184 (void) fprintf(stderr
, "%s: missing close paren `%s'\n",
189 buf
= cat(buf
, bptr
, eptr
- bptr
+ 1);
190 name
= bptr
+ sizeof(def
) - 1;
193 (void) fprintf(stderr
, "%s: empty define `%s'\n",
198 if (*name
!= '_' && (*name
!= 'M' && name
[1] != '_')) {
199 char *undername
= malloc(len
+ 10);
200 if (undername
== NULL
)
202 buf
= cat(buf
, ") || defined(", 0);
203 snprintf(undername
, len
+ 10, "__%.*s__)", (int)len
,
205 buf
= cat(buf
, undername
, len
+ 5);
206 buf
= cat(buf
, ") || defined(", 0);
207 snprintf(undername
, len
+ 10, "__%.*s)", (int)len
,
209 buf
= cat(buf
, undername
, len
+ 3);
211 buf
= cat(buf
, "))", 0);
214 (void) fprintf(stderr
, "%s: invalid input `%s'\n", pname
, defs
);
217 buf
= cat(buf
, eptr
+ 1, 0);
223 main(int argc
, char *argv
[])
225 char line
[INBUFSIZE
];
226 const char *fname
= "stdin";
228 char defs
[INBUFSIZE
];
229 char stmt
[INBUFSIZE
];
236 if ((pname
= strrchr(argv
[0], '/')) == NULL
)
242 (void) fprintf(stderr
, "Usage: %s [<filename>]\n", pname
);
247 if ((fp
= fopen(fname
= argv
[1], "r")) == NULL
) {
248 (void) fprintf(stderr
, "%s: Cannot open `%s'\n", pname
, fname
);
254 while ((ptr
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
256 switch (token
= findtoken(gettoken(&ptr
, defs
))) {
294 if (gettoken(&ptr
, defs
) == NULL
) {
295 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing macro name\n",
296 pname
, fname
, lineno
);
299 if (gettoken(&ptr
, stmt
) == NULL
) {
300 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing macro body\n",
301 pname
, fname
, lineno
);
304 (void) fprintf(stdout
, "\n#if %s\n# define %s\n#endif\n\n",
305 explode(stmt
), defs
);
309 if (state
!= S_CODE
&& *defs
!= '\0') {
310 (void) fprintf(stderr
, "%s: \"%s\", %d: Discarded\n",
311 pname
, fname
, lineno
);
313 (void) fprintf(stderr
, "%s: Too many errors\n", pname
);
318 (void) fprintf(stdout
, "%s", line
);
322 (void) fprintf(stderr
, "%s: \"%s\", %d: Unexpected token\n",
323 pname
, fname
, lineno
);
331 (void) fprintf(stdout
, "#endif\n");
336 tok
= gettoken(&ptr
, defs
);
337 if (token
== T_NEWDEF
) {
339 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing enddef\n",
340 pname
, fname
, lineno
);
344 (void) fprintf(stderr
, "%s: \"%s\", %d: No defs\n",
345 pname
, fname
, lineno
);
348 (void) fprintf(stdout
, "\n\n");
350 (void) fprintf(stdout
, "# %d \"%s\"\n", lineno
+ 1, fname
);
351 #endif /* LINEDIRECTIVE */
352 (void) fprintf(stdout
, "#if (%s)\n", explode(defs
));
357 (void) fprintf(stdout
, "# if (%s) && !defined(_%s_)\n",
358 explode(defs
), keyword
[token
]);
360 (void) fprintf(stdout
, "# if !defined(_%s_)\n",
363 if (gettoken(&ptr
, stmt
) == NULL
) {
364 (void) fprintf(stderr
, "%s: \"%s\", %d: No statement\n",
365 pname
, fname
, lineno
);
368 (void) fprintf(stdout
, "# define _%s_\n", keyword
[token
]);
369 (void) fprintf(stdout
, " %s = %s;\n", keyword
[token
], stmt
);
370 (void) fprintf(stdout
, "# endif\n");
375 if (gettoken(&ptr
, defs
))
376 (void) fprintf(stdout
, " /* %s */\n", defs
);
380 if (token
== T_NEWCODE
) {
382 (void) fprintf(stdout
, "# %d \"%s\"\n", lineno
+ 1, fname
);
383 #endif /* LINEDIRECTIVE */
388 (void) fprintf(stderr
, "%s: \"%s\", %d: Unexpected state\n",
389 pname
, fname
, lineno
);
395 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing enddef\n",
396 pname
, fname
, lineno
);