1 /* $Header: /p/tcsh/cvsroot/tcsh/gethost.c,v 1.15 2012/01/15 17:14: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.15 2012/01/15 17:14: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
)
154 l
= strlen(a
) + len
+ 1;
155 if ((r
= malloc(l
)) == NULL
)
157 snprintf(r
, l
, "%s%.*s", a
, (int)len
, b
);
162 explode(const char *defs
)
164 static const char def
[] = "defined("; /* ) */
167 const char *ptr
, *bptr
, *eptr
= NULL
, *name
;
169 if (strstr(defs
, "#machine(" /* ) */))
173 buf
= strdup("("); /* ) */
174 for (ptr
= defs
; (bptr
= strstr(ptr
, def
)) != NULL
; ptr
= eptr
+ 1) {
176 buf
= cat(buf
, ptr
, bptr
- ptr
);
177 if ((eptr
= strchr(ptr
+ sizeof(def
) - 1, ')')) == NULL
) {
178 (void) fprintf(stderr
, "%s: missing close paren `%s'\n",
182 buf
= cat(buf
, bptr
, eptr
- bptr
+ 1);
183 name
= bptr
+ sizeof(def
) - 1;
186 (void) fprintf(stderr
, "%s: empty define `%s'\n",
191 char *undername
= malloc(len
+ 10);
192 buf
= cat(buf
, " || defined(", 0);
193 snprintf(undername
, len
+ 10, "__%.*s__)", (int)len
,
195 buf
= cat(buf
, undername
, len
+ 5);
196 buf
= cat(buf
, " || defined(", 0);
197 snprintf(undername
, len
+ 10, "__%.*s)", (int)len
,
199 buf
= cat(buf
, undername
, len
+ 3);
203 (void) fprintf(stderr
, "%s: invalid input `%s'\n", pname
, defs
);
206 buf
= cat(buf
, eptr
+ 1, 0);
207 buf
= cat(buf
, ")", 0);
213 main(int argc
, char *argv
[])
215 char line
[INBUFSIZE
];
216 const char *fname
= "stdin";
218 char defs
[INBUFSIZE
];
219 char stmt
[INBUFSIZE
];
226 if ((pname
= strrchr(argv
[0], '/')) == NULL
)
232 (void) fprintf(stderr
, "Usage: %s [<filename>]\n", pname
);
237 if ((fp
= fopen(fname
= argv
[1], "r")) == NULL
) {
238 (void) fprintf(stderr
, "%s: Cannot open `%s'\n", pname
, fname
);
244 while ((ptr
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
246 switch (token
= findtoken(gettoken(&ptr
, defs
))) {
284 if (gettoken(&ptr
, defs
) == NULL
) {
285 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing macro name\n",
286 pname
, fname
, lineno
);
289 if (gettoken(&ptr
, stmt
) == NULL
) {
290 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing macro body\n",
291 pname
, fname
, lineno
);
294 (void) fprintf(stdout
, "\n#if %s\n# define %s\n#endif\n\n", stmt
,
299 if (state
!= S_CODE
&& *defs
!= '\0') {
300 (void) fprintf(stderr
, "%s: \"%s\", %d: Discarded\n",
301 pname
, fname
, lineno
);
303 (void) fprintf(stderr
, "%s: Too many errors\n", pname
);
308 (void) fprintf(stdout
, "%s", line
);
312 (void) fprintf(stderr
, "%s: \"%s\", %d: Unexpected token\n",
313 pname
, fname
, lineno
);
321 (void) fprintf(stdout
, "#endif\n");
326 tok
= gettoken(&ptr
, defs
);
327 if (token
== T_NEWDEF
) {
329 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing enddef\n",
330 pname
, fname
, lineno
);
334 (void) fprintf(stderr
, "%s: \"%s\", %d: No defs\n",
335 pname
, fname
, lineno
);
338 (void) fprintf(stdout
, "\n\n");
340 (void) fprintf(stdout
, "# %d \"%s\"\n", lineno
+ 1, fname
);
341 #endif /* LINEDIRECTIVE */
342 (void) fprintf(stdout
, "#if %s\n", defs
);
347 (void) fprintf(stdout
, "# if (%s) && !defined(_%s_)\n",
348 explode(defs
), keyword
[token
]);
350 (void) fprintf(stdout
, "# if !defined(_%s_)\n",
353 if (gettoken(&ptr
, stmt
) == NULL
) {
354 (void) fprintf(stderr
, "%s: \"%s\", %d: No statement\n",
355 pname
, fname
, lineno
);
358 (void) fprintf(stdout
, "# define _%s_\n", keyword
[token
]);
359 (void) fprintf(stdout
, " %s = %s;\n", keyword
[token
], stmt
);
360 (void) fprintf(stdout
, "# endif\n");
365 if (gettoken(&ptr
, defs
))
366 (void) fprintf(stdout
, " /* %s */\n", defs
);
370 if (token
== T_NEWCODE
) {
372 (void) fprintf(stdout
, "# %d \"%s\"\n", lineno
+ 1, fname
);
373 #endif /* LINEDIRECTIVE */
378 (void) fprintf(stderr
, "%s: \"%s\", %d: Unexpected state\n",
379 pname
, fname
, lineno
);
385 (void) fprintf(stderr
, "%s: \"%s\", %d: Missing enddef\n",
386 pname
, fname
, lineno
);