1 /* $Header: /src/pub/tcsh/gethost.c,v 1.8 2002/03/08 17:36:46 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("$Id: gethost.c,v 1.8 2002/03/08 17:36:46 christos Exp $")
38 # define perror __perror
39 # define rename __rename
40 # define getopt __getopt
41 # define system __system
53 /* Some people don't bother to declare these */
54 #if defined(SUNOS4) || defined(ibm032)
57 #endif /* SUNOS4 || ibm032 */
59 #define ISSPACE(p) (isspace((unsigned char) (p)) && (p) != '\n')
62 * We cannot do that, because some compilers like #line and others
64 * #define LINEDIRECTIVE
67 static const char *keyword
[] =
98 static int findtoken
__P((char *));
99 static char *gettoken
__P((char **, char *));
101 int main
__P((int, char *[]));
104 * Return the token number of the given token
112 if (ptr
== NULL
|| *ptr
== '\0')
115 for (i
= 0; keyword
[i
] != NULL
; i
++)
116 if (strcmp(keyword
[i
], ptr
) == 0)
124 * Get : delimited token and remove leading/trailing blanks/newlines
127 gettoken(pptr
, token
)
134 for (; *ptr
&& ISSPACE(*ptr
); ptr
++)
137 for (; *ptr
&& *ptr
!= ':'; *tok
++ = *ptr
++)
145 for (tok
--; tok
>= token
&& *tok
&& ISSPACE(*tok
); tok
--)
160 char line
[INBUFSIZE
];
162 char *fname
= "stdin";
164 char defs
[INBUFSIZE
];
165 char stmt
[INBUFSIZE
];
172 if ((pname
= strrchr(argv
[0], '/')) == NULL
)
178 (void) fprintf(stderr
, "Usage: %s [<filename>]\n", pname
);
183 if ((fp
= fopen(fname
= argv
[1], "r")) == NULL
) {
184 (void) fprintf(stderr
, "%s: Cannot open `%s'\n", pname
, fname
);
190 while ((ptr
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
192 switch (token
= findtoken(gettoken(&ptr
, defs
))) {
230 if (gettoken(&ptr
, defs
) == NULL
) {
231 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing macro name\n",
232 pname
, fname
, lineno
);
235 if (gettoken(&ptr
, stmt
) == NULL
) {
236 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing macro body\n",
237 pname
, fname
, lineno
);
240 (void) fprintf(stdout
, "\n#if %s\n# define %s\n#endif\n\n", stmt
,
245 if (state
!= S_CODE
&& defs
&& *defs
!= '\0') {
246 (void) fprintf(stderr
, "%s: \"%s\", %d: Discarded\n",
247 pname
, fname
, lineno
);
249 (void) fprintf(stderr
, "%s: Too many errors\n", pname
);
254 (void) fprintf(stdout
, "%s", line
);
258 (void) fprintf(stderr
, "%s: \"%s\", %d: Unexpected token\n",
259 pname
, fname
, lineno
);
267 (void) fprintf(stdout
, "#endif\n");
272 tok
= gettoken(&ptr
, defs
);
273 if (token
== T_NEWDEF
) {
275 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing enddef\n",
276 pname
, fname
, lineno
);
280 (void) fprintf(stderr
, "%s: \"%s\", %d: No defs\n",
281 pname
, fname
, lineno
);
284 (void) fprintf(stdout
, "\n\n");
286 (void) fprintf(stdout
, "# %d \"%s\"\n", lineno
+ 1, fname
);
287 #endif /* LINEDIRECTIVE */
288 (void) fprintf(stdout
, "#if %s\n", defs
);
293 (void) fprintf(stdout
, "# if (%s) && !defined(_%s_)\n",
294 defs
, keyword
[token
]);
296 (void) fprintf(stdout
, "# if !defined(_%s_)\n",
299 if (gettoken(&ptr
, stmt
) == NULL
) {
300 (void) fprintf(stderr
, "%s: \"%s\", %d: No statement\n",
301 pname
, fname
, lineno
);
304 (void) fprintf(stdout
, "# define _%s_\n", keyword
[token
]);
305 (void) fprintf(stdout
, " %s = %s;\n", keyword
[token
], stmt
);
306 (void) fprintf(stdout
, "# endif\n");
311 if (gettoken(&ptr
, defs
))
312 (void) fprintf(stdout
, " /* %s */\n", defs
);
316 if (token
== T_NEWCODE
) {
318 (void) fprintf(stdout
, "# %d \"%s\"\n", lineno
+ 1, fname
);
319 #endif /* LINEDIRECTIVE */
324 (void) fprintf(stderr
, "%s: \"%s\", %d: Unexpected state\n",
325 pname
, fname
, lineno
);
331 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing enddef\n",
332 pname
, fname
, lineno
);