1 /* $Header: /p/tcsh/cvsroot/tcsh/gethost.c,v 1.19 2014/03/09 00:11:54 christos Exp $ */
3 * gethost.c: Create version file from prototype
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$tcsh: gethost.c,v 1.19 2014/03/09 00:11:54 christos Exp $")
38 # define perror __perror
39 # define rename __rename
40 # define getopt __getopt
41 # define system __system
53 #define ISSPACE(p) (isspace((unsigned char) (p)) && (p) != '\n')
56 * We cannot do that, because some compilers like #line and others
58 * #define LINEDIRECTIVE
61 static const char *keyword
[] =
92 static int findtoken (char *);
93 static char *gettoken (char **, char *);
96 int main (int, char *[]);
99 * Return the token number of the given token
106 if (ptr
== NULL
|| *ptr
== '\0')
109 for (i
= 0; keyword
[i
] != NULL
; i
++)
110 if (strcmp(keyword
[i
], ptr
) == 0)
118 * Get : delimited token and remove leading/trailing blanks/newlines
121 gettoken(char **pptr
, char *token
)
126 for (; *ptr
&& ISSPACE(*ptr
); ptr
++)
129 for (; *ptr
&& *ptr
!= ':'; *tok
++ = *ptr
++)
137 for (tok
--; tok
>= token
&& *tok
&& ISSPACE(*tok
); tok
--)
147 cat(const char *a
, const char *b
, size_t len
)
158 if ((r
= malloc(l
)) == NULL
)
161 snprintf(r
, l
, "%s%.*s", a
, (int)len
, b
);
163 snprintf(r
, l
, "%.*s", (int)len
, b
);
168 explode(const char *defs
)
170 static const char def
[] = "defined("; /* ) */
173 const char *ptr
, *bptr
, *eptr
= NULL
, *name
;
175 if (strstr(defs
, "#machine(" /* ) */))
177 if (!strstr(defs
, def
))
182 for (ptr
= defs
; (bptr
= strstr(ptr
, def
)) != NULL
; ptr
= eptr
+ 1) {
184 buf
= cat(buf
, ptr
, bptr
- ptr
+ 1);
185 buf
= cat(buf
, "(", 0); /* ) */
186 if ((eptr
= strchr(ptr
+ sizeof(def
) - 1, ')')) == NULL
) {
187 (void) fprintf(stderr
, "%s: missing close paren `%s'\n",
192 buf
= cat(buf
, bptr
, eptr
- bptr
+ 1);
193 name
= bptr
+ sizeof(def
) - 1;
196 (void) fprintf(stderr
, "%s: empty define `%s'\n",
201 if (*name
!= '_' && (*name
!= 'M' && name
[1] != '_')) {
202 char *undername
= malloc(len
+ 10);
203 if (undername
== NULL
)
205 buf
= cat(buf
, ") || defined(", 0);
206 snprintf(undername
, len
+ 10, "__%.*s__)", (int)len
,
208 buf
= cat(buf
, undername
, len
+ 5);
209 buf
= cat(buf
, ") || defined(", 0);
210 snprintf(undername
, len
+ 10, "__%.*s)", (int)len
,
212 buf
= cat(buf
, undername
, len
+ 3);
214 buf
= cat(buf
, "))", 0);
217 (void) fprintf(stderr
, "%s: invalid input `%s'\n", pname
, defs
);
220 buf
= cat(buf
, eptr
+ 1, 0);
226 main(int argc
, char *argv
[])
228 char line
[INBUFSIZE
];
229 const char *fname
= "stdin";
231 char defs
[INBUFSIZE
];
232 char stmt
[INBUFSIZE
];
239 if ((pname
= strrchr(argv
[0], '/')) == NULL
)
245 (void) fprintf(stderr
, "Usage: %s [<filename>]\n", pname
);
250 if ((fp
= fopen(fname
= argv
[1], "r")) == NULL
) {
251 (void) fprintf(stderr
, "%s: Cannot open `%s'\n", pname
, fname
);
257 while ((ptr
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
259 switch (token
= findtoken(gettoken(&ptr
, defs
))) {
297 if (gettoken(&ptr
, defs
) == NULL
) {
298 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing macro name\n",
299 pname
, fname
, lineno
);
302 if (gettoken(&ptr
, stmt
) == NULL
) {
303 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing macro body\n",
304 pname
, fname
, lineno
);
307 (void) fprintf(stdout
, "\n#if %s\n# define %s\n#endif\n\n",
308 explode(stmt
), defs
);
312 if (state
!= S_CODE
&& *defs
!= '\0') {
313 (void) fprintf(stderr
, "%s: \"%s\", %d: Discarded\n",
314 pname
, fname
, lineno
);
316 (void) fprintf(stderr
, "%s: Too many errors\n", pname
);
321 (void) fprintf(stdout
, "%s", line
);
325 (void) fprintf(stderr
, "%s: \"%s\", %d: Unexpected token\n",
326 pname
, fname
, lineno
);
334 (void) fprintf(stdout
, "#endif\n");
339 tok
= gettoken(&ptr
, defs
);
340 if (token
== T_NEWDEF
) {
342 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing enddef\n",
343 pname
, fname
, lineno
);
347 (void) fprintf(stderr
, "%s: \"%s\", %d: No defs\n",
348 pname
, fname
, lineno
);
351 (void) fprintf(stdout
, "\n\n");
353 (void) fprintf(stdout
, "# %d \"%s\"\n", lineno
+ 1, fname
);
354 #endif /* LINEDIRECTIVE */
355 (void) fprintf(stdout
, "#if (%s)\n", explode(defs
));
360 (void) fprintf(stdout
, "# if (%s) && !defined(_%s_)\n",
361 explode(defs
), keyword
[token
]);
363 (void) fprintf(stdout
, "# if !defined(_%s_)\n",
366 if (gettoken(&ptr
, stmt
) == NULL
) {
367 (void) fprintf(stderr
, "%s: \"%s\", %d: No statement\n",
368 pname
, fname
, lineno
);
371 (void) fprintf(stdout
, "# define _%s_\n", keyword
[token
]);
372 (void) fprintf(stdout
, " %s = %s;\n", keyword
[token
], stmt
);
373 (void) fprintf(stdout
, "# endif\n");
378 if (gettoken(&ptr
, defs
))
379 (void) fprintf(stdout
, " /* %s */\n", defs
);
383 if (token
== T_NEWCODE
) {
385 (void) fprintf(stdout
, "# %d \"%s\"\n", lineno
+ 1, fname
);
386 #endif /* LINEDIRECTIVE */
391 (void) fprintf(stderr
, "%s: \"%s\", %d: Unexpected state\n",
392 pname
, fname
, lineno
);
398 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing enddef\n",
399 pname
, fname
, lineno
);