1 /* $Header: /p/tcsh/cvsroot/tcsh/gethost.c,v 1.12 2006/03/02 18:46:44 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.12 2006/03/02 18:46:44 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 *);
95 int main (int, char *[]);
98 * Return the token number of the given token
105 if (ptr
== NULL
|| *ptr
== '\0')
108 for (i
= 0; keyword
[i
] != NULL
; i
++)
109 if (strcmp(keyword
[i
], ptr
) == 0)
117 * Get : delimited token and remove leading/trailing blanks/newlines
120 gettoken(char **pptr
, char *token
)
125 for (; *ptr
&& ISSPACE(*ptr
); ptr
++)
128 for (; *ptr
&& *ptr
!= ':'; *tok
++ = *ptr
++)
136 for (tok
--; tok
>= token
&& *tok
&& ISSPACE(*tok
); tok
--)
147 main(int argc
, char *argv
[])
149 char line
[INBUFSIZE
];
151 const char *fname
= "stdin";
153 char defs
[INBUFSIZE
];
154 char stmt
[INBUFSIZE
];
161 if ((pname
= strrchr(argv
[0], '/')) == NULL
)
167 (void) fprintf(stderr
, "Usage: %s [<filename>]\n", pname
);
172 if ((fp
= fopen(fname
= argv
[1], "r")) == NULL
) {
173 (void) fprintf(stderr
, "%s: Cannot open `%s'\n", pname
, fname
);
179 while ((ptr
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
181 switch (token
= findtoken(gettoken(&ptr
, defs
))) {
219 if (gettoken(&ptr
, defs
) == NULL
) {
220 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing macro name\n",
221 pname
, fname
, lineno
);
224 if (gettoken(&ptr
, stmt
) == NULL
) {
225 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing macro body\n",
226 pname
, fname
, lineno
);
229 (void) fprintf(stdout
, "\n#if %s\n# define %s\n#endif\n\n", stmt
,
234 if (state
!= S_CODE
&& defs
&& *defs
!= '\0') {
235 (void) fprintf(stderr
, "%s: \"%s\", %d: Discarded\n",
236 pname
, fname
, lineno
);
238 (void) fprintf(stderr
, "%s: Too many errors\n", pname
);
243 (void) fprintf(stdout
, "%s", line
);
247 (void) fprintf(stderr
, "%s: \"%s\", %d: Unexpected token\n",
248 pname
, fname
, lineno
);
256 (void) fprintf(stdout
, "#endif\n");
261 tok
= gettoken(&ptr
, defs
);
262 if (token
== T_NEWDEF
) {
264 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing enddef\n",
265 pname
, fname
, lineno
);
269 (void) fprintf(stderr
, "%s: \"%s\", %d: No defs\n",
270 pname
, fname
, lineno
);
273 (void) fprintf(stdout
, "\n\n");
275 (void) fprintf(stdout
, "# %d \"%s\"\n", lineno
+ 1, fname
);
276 #endif /* LINEDIRECTIVE */
277 (void) fprintf(stdout
, "#if %s\n", defs
);
282 (void) fprintf(stdout
, "# if (%s) && !defined(_%s_)\n",
283 defs
, keyword
[token
]);
285 (void) fprintf(stdout
, "# if !defined(_%s_)\n",
288 if (gettoken(&ptr
, stmt
) == NULL
) {
289 (void) fprintf(stderr
, "%s: \"%s\", %d: No statement\n",
290 pname
, fname
, lineno
);
293 (void) fprintf(stdout
, "# define _%s_\n", keyword
[token
]);
294 (void) fprintf(stdout
, " %s = %s;\n", keyword
[token
], stmt
);
295 (void) fprintf(stdout
, "# endif\n");
300 if (gettoken(&ptr
, defs
))
301 (void) fprintf(stdout
, " /* %s */\n", defs
);
305 if (token
== T_NEWCODE
) {
307 (void) fprintf(stdout
, "# %d \"%s\"\n", lineno
+ 1, fname
);
308 #endif /* LINEDIRECTIVE */
313 (void) fprintf(stderr
, "%s: \"%s\", %d: Unexpected state\n",
314 pname
, fname
, lineno
);
320 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing enddef\n",
321 pname
, fname
, lineno
);