home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume29 / cproto / part02 < prev    next >
Encoding:
Text File  |  1992-04-05  |  47.3 KB  |  1,871 lines

  1. Newsgroups: comp.sources.misc
  2. From: cthuang@zerosan.uucp (Chin Huang)
  3. Subject:  v29i062:  cproto - generate C function prototypes v3PL2, Part02/02
  4. Message-ID: <1992Apr5.042540.2917@sparky.imd.sterling.com>
  5. X-Md4-Signature: df31176d341169ca37cd34bf02348d4f
  6. Date: Sun, 5 Apr 1992 04:25:40 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: cthuang@zerosan.uucp (Chin Huang)
  10. Posting-number: Volume 29, Issue 62
  11. Archive-name: cproto/part02
  12. Environment: UNIX, MS-DOS, getopt, lex, yacc
  13. Supersedes: cproto: Volume 28, Issue 100-101
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then unpack
  17. # it by saving it into a file and typing "sh file".  To overwrite existing
  18. # files, type "sh file -c".  You can also feed this as standard input via
  19. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  20. # will see the following message at the end:
  21. #        "End of shell archive."
  22. # Contents:  config.h cproto.h patchlev.h semantic.h symbol.h cproto.c
  23. #   popen.c semantic.c strstr.c symbol.c
  24. # Wrapped by cthuang@zerosan.UUCP on Sat Apr 04 14:07:39 1992
  25. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  26. if test -f config.h -a "${1}" != "-c" ; then 
  27.   echo shar: Will not over-write existing file \"config.h\"
  28. else
  29. echo shar: Extracting \"config.h\" \(877 characters\)
  30. sed "s/^X//" >config.h <<'END_OF_config.h'
  31. X/* $Id: config.h 3.3 92/04/04 13:59:06 cthuang Exp $
  32. X *
  33. X * cproto configuration and system dependencies
  34. X */
  35. X
  36. X/* maximum include file nesting */
  37. X#ifndef MAX_INC_DEPTH
  38. X#define MAX_INC_DEPTH 15
  39. X#endif
  40. X
  41. X/* maximum number of include directories */
  42. X#ifndef MAX_INC_DIR
  43. X#define MAX_INC_DIR 15
  44. X#endif
  45. X
  46. X/* maximum text buffer size */
  47. X#ifndef MAX_TEXT_SIZE
  48. X#define MAX_TEXT_SIZE 256
  49. X#endif
  50. X
  51. X/* Borland C predefines __MSDOS__ */
  52. X#ifdef __MSDOS__
  53. X#ifndef MSDOS
  54. X#define MSDOS
  55. X#endif
  56. X#endif
  57. X
  58. X#ifdef MSDOS
  59. X#include <malloc.h>
  60. X#include <stdlib.h>
  61. X#else
  62. Xextern char *malloc();
  63. Xextern char *getenv();
  64. X#endif
  65. X
  66. X#ifdef BSD
  67. X#include <strings.h>
  68. X#define strchr index
  69. X#define strrchr rindex
  70. X#else
  71. X#include <string.h>
  72. X#endif
  73. X
  74. X#ifndef MSDOS
  75. Xextern char *strstr();
  76. X#endif
  77. X
  78. X/* C preprocessor */
  79. X#ifdef TURBO_CPP
  80. X#define CPP "cpp -P-"
  81. X#endif
  82. X
  83. X#ifndef MSDOS
  84. X#ifndef CPP
  85. X#define CPP "/lib/cpp"
  86. X#endif
  87. X#endif
  88. END_OF_config.h
  89. if test 877 -ne `wc -c <config.h`; then
  90.     echo shar: \"config.h\" unpacked with wrong size!
  91. fi
  92. # end of overwriting check
  93. fi
  94. if test -f cproto.h -a "${1}" != "-c" ; then 
  95.   echo shar: Will not over-write existing file \"cproto.h\"
  96. else
  97. echo shar: Extracting \"cproto.h\" \(4976 characters\)
  98. sed "s/^X//" >cproto.h <<'END_OF_cproto.h'
  99. X/* $Id: cproto.h 3.4 92/04/04 13:59:08 cthuang Exp $
  100. X *
  101. X * Declarations for C function prototype generator
  102. X */
  103. X#include "config.h"
  104. X
  105. X/* Boolean type */
  106. Xtypedef char boolean;
  107. X#define FALSE    0
  108. X#define TRUE    1
  109. X
  110. X/* Source file text */
  111. Xtypedef struct text {
  112. X    char text[MAX_TEXT_SIZE];    /* source text */
  113. X    long begin;         /* offset in temporary file */
  114. X} Text;
  115. X
  116. X/* This is a list of function parameters. */
  117. Xtypedef struct parameter_list {
  118. X    struct parameter *first;    /* pointer to first parameter in list */
  119. X    struct parameter *last;    /* pointer to last parameter in list */  
  120. X    long begin_comment;     /* begin offset of comment */
  121. X    long end_comment;        /* end offset of comment */
  122. X    char *comment;        /* comment at start of parameter list */
  123. X} ParameterList;
  124. X
  125. X/* Declaration specifier flags */
  126. X#define DS_EXTERN    0    /* default: external declaration */
  127. X#define DS_STATIC    1    /* visible only in current file */
  128. X#define DS_CHAR     2    /* "char" type specifier in declaration */
  129. X#define DS_SHORT    4    /* "short" type specifier in declaration */
  130. X#define DS_FLOAT    8    /* "float" type specifier in declaration */
  131. X#define DS_JUNK     16    /* we're not interested in this declaration */
  132. X
  133. X/* This structure stores information about a declaration specifier. */
  134. Xtypedef struct decl_spec {
  135. X    unsigned short flags;    /* flags defined above */
  136. X    char *text;         /* source text */
  137. X    long begin;         /* offset in temporary file */
  138. X} DeclSpec;
  139. X
  140. X/* Styles of function definitions */
  141. X#define FUNC_NONE        0    /* not a function definition */
  142. X#define FUNC_TRADITIONAL    1    /* traditional style */
  143. X#define FUNC_ANSI        2    /* ANSI style */
  144. Xtypedef int FuncDefStyle;
  145. X
  146. X/* This structure stores information about a declarator. */
  147. Xtypedef struct declarator {
  148. X    char *name;             /* name of variable or function */
  149. X    char *text;             /* source text */
  150. X    long begin;             /* offset in temporary file */
  151. X    long begin_comment;         /* begin offset of comment */
  152. X    long end_comment;            /* end offset of comment */
  153. X    FuncDefStyle func_def;        /* style of function definition */
  154. X    ParameterList params;        /* function parameters */
  155. X    struct declarator *head;        /* head function declarator */
  156. X    struct declarator *func_stack;    /* stack of function declarators */
  157. X    struct declarator *next;        /* next declarator in list */
  158. X} Declarator;
  159. X
  160. X/* This is a list of declarators. */
  161. Xtypedef struct declarator_list {
  162. X    Declarator *first;        /* pointer to first declarator in list */
  163. X    Declarator *last;        /* pointer to last declarator in list */  
  164. X} DeclaratorList;
  165. X
  166. X/* This structure stores information about a function parameter. */
  167. Xtypedef struct parameter {
  168. X    struct parameter *next;    /* next parameter in list */
  169. X    DeclSpec decl_spec;
  170. X    Declarator *declarator;
  171. X    char *comment;        /* comment following the parameter */
  172. X} Parameter;
  173. X
  174. X/* parser stack entry type */
  175. Xtypedef union {
  176. X    Text text;
  177. X    DeclSpec decl_spec;
  178. X    Parameter parameter;
  179. X    ParameterList param_list;
  180. X    Declarator *declarator;
  181. X    DeclaratorList decl_list;
  182. X} YYSTYPE;
  183. X
  184. X/* Prototype styles */
  185. X#define PROTO_NONE        0    /* do not output any prototypes */
  186. X#define PROTO_TRADITIONAL    1    /* comment out parameters */
  187. X#define PROTO_ABSTRACT        2    /* comment out parameter names */
  188. X#define PROTO_ANSI        3    /* ANSI C prototype */
  189. X#define PROTO_MACRO        4    /* macro around parameters */
  190. Xtypedef int PrototypeStyle;
  191. X
  192. X/* The role of a function declarator */
  193. X#define FUNC_OTHER    0    /* miscellaneous declaration */
  194. X#define FUNC_PROTO    1    /* prototype */
  195. X#define FUNC_DEF    2    /* function definition */
  196. Xtypedef int FuncDeclRole;
  197. X
  198. X/* Prototype/function definition output formats */
  199. X#define FMT_OTHER        0    /* miscellaneous */
  200. X#define FMT_PROTO        1    /* prototype */
  201. X#define FMT_FUNC        2    /* function definition */
  202. X#define FMT_FUNC_COMMENT    3    /* func. def. with parameter comments */
  203. Xtypedef int FuncFormatType;
  204. X
  205. X/* Prototype/function definition output format */
  206. Xtypedef struct {
  207. X    char *decl_spec_prefix;    /* output before declaration specifier */
  208. X    char *declarator_prefix;    /* output before declarator name */
  209. X    char *declarator_suffix;    /* output before '(' of parameter list */
  210. X    char *first_param_prefix;    /* output before first parameter */
  211. X    char *middle_param_prefix;    /* output before each subsequent parameter */
  212. X    char *last_param_suffix;    /* output after last parameter */
  213. X} FuncFormat;
  214. X
  215. X/* Program options */
  216. Xextern boolean extern_out;
  217. Xextern boolean static_out;
  218. Xextern boolean variables_out;
  219. Xextern boolean promote_param;
  220. Xextern PrototypeStyle proto_style;
  221. Xextern FuncDefStyle func_style;
  222. Xextern boolean define_macro;
  223. Xextern char *macro_name;
  224. Xextern boolean proto_comments;
  225. Xextern int num_inc_dir;
  226. Xextern char *inc_dir[];
  227. Xextern FuncFormat fmt[4];
  228. X
  229. X/* Global declarations */
  230. Xextern char progname[];
  231. X
  232. Xextern char *xmalloc(), *xstrdup(), *trim_path_sep();
  233. Xextern void put_error();
  234. Xextern void init_parser(), process_file(), pop_file();
  235. Xextern char *cur_file_name();
  236. Xextern unsigned cur_line_num();
  237. Xextern FILE *cur_tmp_file();
  238. Xextern void cur_file_changed();
  239. Xextern long cur_begin_comment();
  240. END_OF_cproto.h
  241. if test 4976 -ne `wc -c <cproto.h`; then
  242.     echo shar: \"cproto.h\" unpacked with wrong size!
  243. fi
  244. # end of overwriting check
  245. fi
  246. if test -f patchlev.h -a "${1}" != "-c" ; then 
  247.   echo shar: Will not over-write existing file \"patchlev.h\"
  248. else
  249. echo shar: Extracting \"patchlev.h\" \(21 characters\)
  250. sed "s/^X//" >patchlev.h <<'END_OF_patchlev.h'
  251. X#define PATCHLEVEL 2
  252. END_OF_patchlev.h
  253. if test 21 -ne `wc -c <patchlev.h`; then
  254.     echo shar: \"patchlev.h\" unpacked with wrong size!
  255. fi
  256. # end of overwriting check
  257. fi
  258. if test -f semantic.h -a "${1}" != "-c" ; then 
  259.   echo shar: Will not over-write existing file \"semantic.h\"
  260. else
  261. echo shar: Extracting \"semantic.h\" \(1823 characters\)
  262. sed "s/^X//" >semantic.h <<'END_OF_semantic.h'
  263. X/* $Id: semantic.h 3.2 92/03/06 00:54:38 cthuang Exp $
  264. X *
  265. X * Declarations of semantic action routines
  266. X */
  267. X
  268. Xextern void new_decl_spec(/*
  269. X    DeclSpec *decl_spec, char *text, long offset, int flags*/);
  270. Xextern void free_decl_spec(/*
  271. X    DeclSpec *decl_spec*/);
  272. Xextern void join_decl_specs(/*
  273. X    DeclSpec *result, DeclSpec *a, DeclSpec *b*/);
  274. Xextern void check_untagged(/*
  275. X    DeclSpec *decl_spec*/);
  276. Xextern Declarator *new_declarator(/*
  277. X    char *text, char *name, long offset*/);
  278. Xextern void free_declarator(/*
  279. X    Declarator *d*/);
  280. Xextern void new_decl_list(/*
  281. X    DeclaratorList *decl_list, Declarator *declarator*/);
  282. Xextern void free_decl_list(/*
  283. X    DeclaratorList *decl_list*/);
  284. Xextern void add_decl_list(/*
  285. X    DeclaratorList *to, DeclaratorList *from, Declarator *declarator*/);
  286. Xextern void new_parameter(/*
  287. X    Parameter *param, DeclSpec *decl_spec, Declarator *declarator*/);
  288. Xextern void free_parameter(/*
  289. X    Parameter *param*/);
  290. Xextern boolean is_void_parameter(/*
  291. X    Parameter *p*/);
  292. Xextern void new_param_list(/*
  293. X    ParameterList *param_list, Parameter *param*/);
  294. Xextern void free_param_list(/*
  295. X    ParameterList *param_list*/);
  296. Xextern void add_param_list(/*
  297. X    ParameterList *to, ParameterList *from, Parameter *param*/);
  298. Xextern void new_ident_list(/*
  299. X    ParameterList *param_list*/);
  300. Xextern void add_ident_list(/*
  301. X    ParameterList *to, ParameterList *from, char *name*/);
  302. Xextern void set_param_types(/*
  303. X    ParameterList *params, DeclSpec *decl_spec, DeclaratorList *declarators*/);
  304. Xextern void gen_declarations(/*
  305. X    DeclSpec *decl_spec, DeclaratorList *decl_list*/);
  306. Xextern void gen_prototype(/*
  307. X    DeclSpec *decl_spec, Declarator *declarator*/);
  308. Xextern void gen_func_declarator(/*
  309. X    Declarator *declarator*/);
  310. Xextern void gen_func_definition(/*
  311. X    DeclSpec *decl_spec, Declarator *declarator*/);
  312. END_OF_semantic.h
  313. if test 1823 -ne `wc -c <semantic.h`; then
  314.     echo shar: \"semantic.h\" unpacked with wrong size!
  315. fi
  316. # end of overwriting check
  317. fi
  318. if test -f symbol.h -a "${1}" != "-c" ; then 
  319.   echo shar: Will not over-write existing file \"symbol.h\"
  320. else
  321. echo shar: Extracting \"symbol.h\" \(776 characters\)
  322. sed "s/^X//" >symbol.h <<'END_OF_symbol.h'
  323. X/* $Id: symbol.h 3.3 92/03/14 11:57:48 cthuang Exp $
  324. X *
  325. X * A symbol table is a collection of string identifiers stored in a
  326. X * hash table.
  327. X */
  328. X#ifndef SYMBOL_H
  329. X#define SYMBOL_H
  330. X
  331. Xtypedef struct symbol {
  332. X    struct symbol *next;    /* next symbol in list */
  333. X    char *name;         /* name of symbol */
  334. X    unsigned short flags;    /* symbol attributes */
  335. X} Symbol;
  336. X
  337. X/* The hash table length should be a prime number. */
  338. X#define SYM_MAX_HASH 251
  339. X
  340. Xtypedef struct symbol_table {
  341. X    Symbol *bucket[SYM_MAX_HASH];    /* hash buckets */
  342. X} SymbolTable;
  343. X
  344. Xextern SymbolTable *new_symbol_table(); /* Create symbol table */
  345. Xextern void free_symbol_table();    /* Destroy symbol table */
  346. Xextern Symbol *find_symbol();        /* Lookup symbol name */
  347. Xextern Symbol *new_symbol();        /* Define new symbol */
  348. X
  349. X#endif
  350. END_OF_symbol.h
  351. if test 776 -ne `wc -c <symbol.h`; then
  352.     echo shar: \"symbol.h\" unpacked with wrong size!
  353. fi
  354. # end of overwriting check
  355. fi
  356. if test -f cproto.c -a "${1}" != "-c" ; then 
  357.   echo shar: Will not over-write existing file \"cproto.c\"
  358. else
  359. echo shar: Extracting \"cproto.c\" \(10377 characters\)
  360. sed "s/^X//" >cproto.c <<'END_OF_cproto.c'
  361. X/* $Id: cproto.c 3.4 92/04/04 13:59:22 cthuang Exp $
  362. X *
  363. X * C function prototype generator and function definition converter
  364. X */
  365. X#ifndef lint
  366. Xstatic char rcsid[] = "$Id: cproto.c 3.4 92/04/04 13:59:22 cthuang Exp $";
  367. X#endif
  368. X#include <stdio.h>
  369. X#include <ctype.h>
  370. X#include "cproto.h"
  371. X#include "patchlev.h"
  372. X
  373. X/* getopt declarations */
  374. Xextern int getopt();
  375. Xextern char *optarg;
  376. Xextern int optind;
  377. X
  378. X/* Name of the program */
  379. Xchar progname[] = "cproto";
  380. X
  381. X/* Program options */
  382. X
  383. X/* If TRUE, output "extern" before global declarations */
  384. Xboolean extern_out = FALSE;
  385. X
  386. X/* If TRUE, generate static declarations */
  387. Xboolean static_out = FALSE;
  388. X
  389. X/* If TRUE, generate variable declarations */
  390. Xboolean variables_out = FALSE;
  391. X
  392. X/* If TRUE, enable formal parameter promotion */
  393. Xboolean promote_param = TRUE;
  394. X
  395. X/* Style of function prototype to generate */
  396. XPrototypeStyle proto_style = PROTO_ANSI;
  397. X
  398. X/* Function definition style converted to */
  399. XFuncDefStyle func_style = FUNC_NONE;
  400. X
  401. X/* Name of macro to guard prototypes */
  402. Xchar *macro_name = "P_";
  403. X
  404. X/* If TRUE, output prototype macro definition */
  405. Xboolean define_macro = TRUE;
  406. X
  407. X/* If TRUE, output comments in prototypes */
  408. Xboolean proto_comments = TRUE;
  409. X
  410. X/* Output formats for function declarators */
  411. XFuncFormat fmt[] = {
  412. X    /* miscellaneous function declarator */
  413. X    { "", " ", "", "", " ", "" },
  414. X    /* prototype */
  415. X    { "", " ", "", "", " ", "" },
  416. X    /* function definition */
  417. X    { "", "\n", " ", "", " ", "" },
  418. X    /* function definition with parameter comments */
  419. X    { "", "\n", " ", "\n    ", "\n    ", "\n" },
  420. X};
  421. X
  422. X/* Include file directories */
  423. X#ifdef MSDOS
  424. Xint num_inc_dir = 1;
  425. Xchar *inc_dir[MAX_INC_DIR] = { "" };
  426. X#else
  427. Xint num_inc_dir = 2;
  428. Xchar *inc_dir[MAX_INC_DIR] = { "", "/usr/include" };
  429. X#endif
  430. X
  431. X/* Run the C preprocessor */
  432. X#ifdef CPP
  433. Xextern FILE *popen();
  434. Xextern int pclose();
  435. Xstatic char *cpp_cmd, *cmd;
  436. X#endif
  437. X
  438. X
  439. X/* Try to allocate some memory.
  440. X * If unsuccessful, output an error message and exit.
  441. X */
  442. Xchar *
  443. Xxmalloc (n)
  444. Xunsigned n;
  445. X{
  446. X    char *p;
  447. X
  448. X    if ((p = malloc(n)) == NULL) {
  449. X    fprintf(stderr, "%s: out of memory\n", progname);
  450. X    exit(1);
  451. X    }
  452. X    return p;
  453. X}
  454. X
  455. X/* Copy the string into allocated memory.
  456. X * If unsuccessful, output an error message and exit.
  457. X */
  458. Xchar *
  459. Xxstrdup (src)
  460. Xchar *src;
  461. X{
  462. X    return strcpy(xmalloc(strlen(src)+1), src);
  463. X}
  464. X
  465. X/* Output the current source file name and line number.
  466. X */
  467. Xvoid
  468. Xput_error ()
  469. X{
  470. X    fprintf(stderr, "\"%s\", line %d: ", cur_file_name(), cur_line_num());
  471. X}
  472. X
  473. X/* Scan for options from a string.
  474. X */
  475. Xstatic void
  476. Xparse_options (src, maxargc, pargc, argv)
  477. Xchar *src;
  478. Xint maxargc, *pargc;
  479. Xchar **argv;
  480. X{
  481. X    char *g, *p, c;
  482. X    int argc;
  483. X
  484. X    argc = 0;
  485. X    g = xstrdup(src);
  486. X    c = *g;
  487. X    while (c != '\0' && argc < maxargc) {
  488. X    while (c == ' ' || c == '\t')
  489. X        c = *++g;
  490. X    if (c == '\0')
  491. X        break;
  492. X    argv[argc++] = g;
  493. X
  494. X    p = g;
  495. X    while (1) {
  496. X        if (c == ' ' || c == '\t' || c == '\0') {
  497. X        *p = '\0';
  498. X        break;
  499. X        } else if (c == '"') {
  500. X        while (1) {
  501. X            c = *++g;
  502. X            if (c == '"') {
  503. X            c = *++g;
  504. X            break;
  505. X            } else if (c == '\0') {
  506. X            break;
  507. X            } else {
  508. X            *p++ = c;
  509. X            }
  510. X        }
  511. X        } else {
  512. X        *p++ = c;
  513. X        c = *++g;
  514. X        }
  515. X    }
  516. X    if (c != '\0')
  517. X        c = *++g;
  518. X    }
  519. X
  520. X    *pargc = argc;
  521. X}
  522. X
  523. X/* Replace any character escape sequences in a string with the actual
  524. X * characters.    Return a pointer to malloc'ed memory containing the result.
  525. X * This function knows only a few escape sequences.
  526. X */
  527. Xstatic char *
  528. Xescape_string (src)
  529. Xchar *src;
  530. X{
  531. X    char *result, *get, *put;
  532. X
  533. X    result = xstrdup(src);
  534. X    put = result;
  535. X    get = src;
  536. X    while (*get != '\0') {
  537. X    if (*get == '\\') {
  538. X        switch (*(++get)) {
  539. X        case 'n':
  540. X        *put++ = '\n';
  541. X        ++get;
  542. X        break;
  543. X        case 't':
  544. X        *put++ = '\t';
  545. X        ++get;
  546. X        break;
  547. X        default:
  548. X        if (*get != '\0')
  549. X            *put++ = *get++;
  550. X        }
  551. X    } else {
  552. X        *put++ = *get++;
  553. X    }
  554. X    }
  555. X    *put = *get;
  556. X    return result;
  557. X}
  558. X
  559. X/* Trim any path name separator from the end of the string.
  560. X * Return a pointer to the string.
  561. X */
  562. Xchar *
  563. Xtrim_path_sep (s)
  564. Xchar *s;
  565. X{
  566. X    char ch;
  567. X    int n;
  568. X
  569. X    n = strlen(s);
  570. X    if (n > 0) {
  571. X    ch = s[n-1];
  572. X    if (ch == '/' || ch == '\\')
  573. X        s[n-1] = '\0';
  574. X    }
  575. X    return s;
  576. X}
  577. X
  578. X/* Output usage message and exit.
  579. X */
  580. Xstatic void
  581. Xusage ()
  582. X{
  583. X    fprintf(stderr, "usage: %s [ option ... ] [ file ... ]\n", progname);
  584. X    fputs("Options:\n", stderr);
  585. X    fputs("  -a       Convert function definitions to ANSI style\n", stderr);
  586. X    fputs("  -c       Omit comments in generated prototypes\n", stderr);
  587. X    fputs("  -e       Output \"extern\" keyword before global declarations\n",
  588. X    stderr);
  589. X    fputs("  -f n     Set function prototype style (0 to 4)\n", stderr);
  590. X    fputs("  -p       Disable formal parameter promotion\n", stderr);
  591. X    fputs("  -s       Output static declarations\n", stderr);
  592. X    fputs("  -t       Convert function definitions to traditional style\n",
  593. X    stderr);
  594. X    fputs("  -v       Output variable declarations\n", stderr);
  595. X    fputs("  -m name  Set name of prototype macro\n", stderr);
  596. X    fputs("  -d       Omit prototype macro definition\n", stderr);
  597. X    fputs("  -P fmt   Set prototype format template \"int main (a, b)\"\n",
  598. X    stderr);
  599. X    fputs("  -F fmt   Set function definition format template \"int main (a, b)\"\n",
  600. X    stderr);
  601. X    fputs("  -C fmt   Set format for function definition with parameter comments\n",
  602. X    stderr);
  603. X    fputs("  -V       Print version information\n", stderr);
  604. X    fputs("  -D name[=value]\n", stderr);
  605. X    fputs("  -U name\n", stderr);
  606. X    fputs("  -I directory\n", stderr);
  607. X    fputs("           Set C preprocessor options\n", stderr);
  608. X    exit(1);
  609. X}
  610. X
  611. X#define MAX_OPTIONS 40
  612. X
  613. X/* Process the command line options.
  614. X */
  615. Xstatic void
  616. Xprocess_options (pargc, pargv)
  617. Xint *pargc;
  618. Xchar ***pargv;
  619. X{
  620. X    int argc, eargc, nargc;
  621. X    char **argv, *eargv[MAX_OPTIONS], **nargv;
  622. X    int i, c;
  623. X    char *s;
  624. X#ifdef CPP
  625. X    unsigned n;
  626. X    char tmp[MAX_TEXT_SIZE];
  627. X#endif
  628. X
  629. X    argc = *pargc;
  630. X    argv = *pargv;
  631. X    if ((s = getenv("CPROTO")) != NULL) {
  632. X    parse_options(s, MAX_OPTIONS, &eargc, eargv);
  633. X    nargv = (char **)xmalloc((eargc+argc+1)*sizeof(char *));
  634. X    nargv[0] = argv[0];
  635. X    nargc = 1;
  636. X    for (i = 0; i < eargc; ++i)
  637. X        nargv[nargc++] = eargv[i];
  638. X    for (i = 1; i < argc; ++i)
  639. X        nargv[nargc++] = argv[i];
  640. X    nargv[nargc] = NULL;
  641. X    argc = nargc;
  642. X    argv = nargv;
  643. X    }
  644. X
  645. X#ifdef CPP
  646. X    /* Allocate buffer for C preprocessor command line. */
  647. X    n = strlen(CPP) + 1;
  648. X    for (i = 0; i < argc; ++i) {
  649. X    n += strlen(argv[i]) + 1;
  650. X    }
  651. X    cpp_cmd = xmalloc(n);
  652. X    strcpy(cpp_cmd, CPP);
  653. X    cmd = xmalloc(n);
  654. X#endif
  655. X
  656. X    while ((c = getopt(argc, argv, "aC:cD:deF:f:I:m:P:pstU:Vv")) != EOF) {
  657. X    switch (c) {
  658. X    case 'I':
  659. X        if (num_inc_dir < MAX_INC_DIR) {
  660. X        inc_dir[num_inc_dir++] = trim_path_sep(xstrdup(optarg));
  661. X        } else {
  662. X        fprintf(stderr, "%s: too many include directories\n",
  663. X            progname);
  664. X        }
  665. X    case 'D':
  666. X    case 'U':
  667. X#ifdef CPP
  668. X        sprintf(tmp, " -%c%s", c, optarg);
  669. X        strcat(cpp_cmd, tmp);
  670. X#endif
  671. X        break;
  672. X    case 'a':
  673. X        func_style = FUNC_ANSI;
  674. X        break;
  675. X    case 'c':
  676. X        proto_comments = FALSE;
  677. X        break;
  678. X    case 'd':
  679. X        define_macro = FALSE;
  680. X        break;
  681. X    case 'e':
  682. X        extern_out = TRUE;
  683. X        break;
  684. X    case 'C':
  685. X    case 'F':
  686. X    case 'P':
  687. X        s = escape_string(optarg);
  688. X        i = (c == 'C') ? FMT_FUNC_COMMENT :
  689. X        ((c == 'F') ? FMT_FUNC : FMT_PROTO);
  690. X
  691. X        fmt[i].decl_spec_prefix = s;
  692. X        while (*s != '\0' && isascii(*s) && !isalnum(*s)) ++s;
  693. X        if (*s == '\0') usage();
  694. X        *s++ = '\0';
  695. X        while (*s != '\0' && isascii(*s) && isalnum(*s)) ++s;
  696. X        if (*s == '\0') usage();
  697. X
  698. X        fmt[i].declarator_prefix = s;
  699. X        while (*s != '\0' && isascii(*s) && !isalnum(*s)) ++s;
  700. X        if (*s == '\0') usage();
  701. X        *s++ = '\0';
  702. X        while (*s != '\0' && isascii(*s) && isalnum(*s)) ++s;
  703. X        if (*s == '\0') usage();
  704. X
  705. X        fmt[i].declarator_suffix = s;
  706. X        while (*s != '\0' && *s != '(') ++s;
  707. X        if (*s == '\0') usage();
  708. X        *s++ = '\0';
  709. X
  710. X        fmt[i].first_param_prefix = s;
  711. X        while (*s != '\0' && isascii(*s) && !isalnum(*s)) ++s;
  712. X        if (*s == '\0') usage();
  713. X        *s++ = '\0';
  714. X        while (*s != '\0' && *s != ',') ++s;
  715. X        if (*s == '\0') usage();
  716. X
  717. X        fmt[i].middle_param_prefix = ++s;
  718. X        while (*s != '\0' && isascii(*s) && !isalnum(*s)) ++s;
  719. X        if (*s == '\0') usage();
  720. X        *s++ = '\0';
  721. X        while (*s != '\0' && isascii(*s) && isalnum(*s)) ++s;
  722. X        if (*s == '\0') usage();
  723. X
  724. X        fmt[i].last_param_suffix = s;
  725. X        while (*s != '\0' && *s != ')') ++s;
  726. X        *s = '\0';
  727. X
  728. X        break;
  729. X    case 'f':
  730. X        proto_style = atoi(optarg);
  731. X        if (proto_style < 0 || proto_style > PROTO_MACRO)
  732. X        usage();
  733. X        break;
  734. X    case 'm':
  735. X        macro_name = optarg;
  736. X        break;
  737. X    case 'p':
  738. X        promote_param = FALSE;
  739. X        break;
  740. X    case 's':
  741. X        static_out = TRUE;
  742. X        break;
  743. X    case 't':
  744. X        func_style = FUNC_TRADITIONAL;
  745. X        break;
  746. X    case 'V':
  747. X        fprintf(stderr, "%s patchlevel %d\n", rcsid, PATCHLEVEL);
  748. X        break;
  749. X    case 'v':
  750. X        variables_out = TRUE;
  751. X        break;
  752. X    default:
  753. X        usage();
  754. X    }
  755. X    }
  756. X
  757. X    *pargc = argc;
  758. X    *pargv = argv;
  759. X}
  760. X
  761. Xint
  762. Xmain (argc, argv)
  763. Xint argc;
  764. Xchar **argv;
  765. X{
  766. X    int i;
  767. X    FILE *inf;
  768. X
  769. X    process_options(&argc, &argv);
  770. X
  771. X    if (proto_style == PROTO_MACRO && define_macro) {
  772. X    printf("#if defined(__STDC__) || defined(__cplusplus)\n");
  773. X    printf("#define %s(s) s\n", macro_name);
  774. X    printf("#else\n");
  775. X    printf("#define %s(s) ()\n", macro_name);
  776. X    printf("#endif\n\n");
  777. X    }
  778. X
  779. X    init_parser();
  780. X    if (optind == argc) {
  781. X    if (func_style != FUNC_NONE) {
  782. X        proto_style = PROTO_NONE;
  783. X        variables_out = FALSE;
  784. X        proto_comments = FALSE;
  785. X    }
  786. X    process_file(stdin, "stdin");
  787. X    pop_file();
  788. X    } else {
  789. X    for (i = optind; i < argc; ++i) {
  790. X#ifdef CPP
  791. X        if (func_style == FUNC_NONE) {
  792. X        sprintf(cmd, "%s %s", cpp_cmd, argv[i]);
  793. X        if ((inf = popen(cmd, "r")) == NULL) {
  794. X            fprintf(stderr, "%s: error running cpp\n", progname);
  795. X            continue;
  796. X        }
  797. X        } else {
  798. X        if ((inf = fopen(argv[i], "r")) == NULL) {
  799. X            fprintf(stderr, "%s: cannot read file %s\n", progname,
  800. X            argv[i]);
  801. X            continue;
  802. X        }
  803. X        }
  804. X#else
  805. X        if ((inf = fopen(argv[i], "r")) == NULL) {
  806. X        fprintf(stderr, "%s: cannot read file %s\n", progname, argv[i]);
  807. X        continue;
  808. X        }
  809. X#endif
  810. X        process_file(inf, argv[i]);
  811. X#ifdef CPP
  812. X        if (func_style == FUNC_NONE) {
  813. X        pclose(inf);
  814. X        } else {
  815. X        pop_file();
  816. X        }
  817. X#else
  818. X        pop_file();
  819. X#endif
  820. X    }
  821. X    }
  822. X
  823. X    if (proto_style == PROTO_MACRO && define_macro) {
  824. X    printf("\n#undef %s\n", macro_name);
  825. X    }
  826. X
  827. X    return 0;
  828. X}
  829. END_OF_cproto.c
  830. if test 10377 -ne `wc -c <cproto.c`; then
  831.     echo shar: \"cproto.c\" unpacked with wrong size!
  832. fi
  833. # end of overwriting check
  834. fi
  835. if test -f popen.c -a "${1}" != "-c" ; then 
  836.   echo shar: Will not over-write existing file \"popen.c\"
  837. else
  838. echo shar: Extracting \"popen.c\" \(1567 characters\)
  839. sed "s/^X//" >popen.c <<'END_OF_popen.c'
  840. X/* $Id: popen.c 3.2 92/03/14 11:58:07 cthuang Exp $
  841. X *
  842. X * Imitate a UNIX pipe in MS-DOS.
  843. X */
  844. X#include <stdio.h>
  845. X#include <stdlib.h>
  846. X#include <process.h>
  847. X#include <io.h>
  848. X#include "cproto.h"
  849. X
  850. Xstatic char pipe_name[FILENAME_MAX];    /* name of the temporary file */
  851. X
  852. X/* Open a pipe for reading.
  853. X */
  854. XFILE *
  855. Xpopen (cmd, type)
  856. Xchar *cmd, *type;
  857. X{
  858. X    char *tmpdir, *argv[30], **arg, *cmdline, *s, opt[FILENAME_MAX];
  859. X    int ostdout, status;
  860. X
  861. X    /* Set temporary file name. */
  862. X    if ((tmpdir = getenv("TMP")) == NULL) {
  863. X    pipe_name[0] = '\0';
  864. X    } else {
  865. X    strcpy(pipe_name, tmpdir);
  866. X    trim_path_sep(pipe_name);
  867. X    strcat(pipe_name, "/");
  868. X    }
  869. X    strcat(pipe_name, tmpnam(NULL));
  870. X
  871. X    /* Split the command into an argument array. */
  872. X    cmdline = xstrdup(cmd);
  873. X    arg = argv;
  874. X    s = strtok(cmdline, " ");
  875. X    *arg++ = s;
  876. X#ifdef M_I86
  877. X    sprintf(opt, "-o%s.", pipe_name);
  878. X#else
  879. X    sprintf(opt, "-o%s", pipe_name);
  880. X#endif
  881. X    *arg++ = opt;
  882. X    while ((s = strtok(NULL, " ")) != NULL) {
  883. X    *arg++ = s;
  884. X    }
  885. X    *arg = NULL;
  886. X    /* Redirect the program's stdout to /dev/null. */
  887. X    ostdout = dup(fileno(stdout));
  888. X    freopen("nul", "w", stdout);
  889. X    /* Run the program. */
  890. X    status = spawnvp(P_WAIT, argv[0], argv);
  891. X    /* Restore stdout. */
  892. X    dup2(ostdout, fileno(stdout));
  893. X    free(cmdline);
  894. X
  895. X    if (status != 0)
  896. X    return NULL;
  897. X    /* Open the intermediate file and return the stream. */
  898. X    return fopen(pipe_name, type) ;
  899. X}
  900. X/* Close the pipe.
  901. X */
  902. Xint
  903. Xpclose (f)
  904. XFILE *f;
  905. X{
  906. X    int status;
  907. X    status = fclose(f);
  908. X    unlink(pipe_name);
  909. X    return status;
  910. X}
  911. END_OF_popen.c
  912. if test 1567 -ne `wc -c <popen.c`; then
  913.     echo shar: \"popen.c\" unpacked with wrong size!
  914. fi
  915. # end of overwriting check
  916. fi
  917. if test -f semantic.c -a "${1}" != "-c" ; then 
  918.   echo shar: Will not over-write existing file \"semantic.c\"
  919. else
  920. echo shar: Extracting \"semantic.c\" \(18606 characters\)
  921. sed "s/^X//" >semantic.c <<'END_OF_semantic.c'
  922. X/* $Id: semantic.c 3.4 92/04/04 13:59:28 cthuang Exp $
  923. X *
  924. X * Semantic actions executed by the parser of the
  925. X * C function prototype generator.
  926. X */
  927. X#include <stdio.h>
  928. X#include "cproto.h"
  929. X#include "semantic.h"
  930. X
  931. X/* Head function declarator in a prototype or function definition */
  932. Xstatic Declarator *func_declarator;
  933. X
  934. X/* Where the declarator appears */
  935. Xstatic int where;
  936. X
  937. X/* Output format to use */
  938. Xstatic int format;
  939. X
  940. X/* Initialize a new declaration specifier part.
  941. X */
  942. Xvoid
  943. Xnew_decl_spec (decl_spec, text, offset, flags)
  944. XDeclSpec *decl_spec;
  945. Xchar *text;
  946. Xlong offset;
  947. Xint flags;
  948. X{
  949. X    decl_spec->text = xstrdup(text);
  950. X    decl_spec->begin = offset;
  951. X    decl_spec->flags = flags;
  952. X}
  953. X
  954. X/* Free storage used by a declaration specifier part.
  955. X */
  956. Xvoid
  957. Xfree_decl_spec (decl_spec)
  958. XDeclSpec *decl_spec;
  959. X{
  960. X    free(decl_spec->text);
  961. X}
  962. X
  963. X/* Create a new string by joining two strings with a space between them.
  964. X * Return a pointer to the resultant string.
  965. X * If out of memory, output an error message and exit.
  966. X */
  967. Xstatic char *
  968. Xconcat_string (a, b)
  969. Xchar *a, *b;
  970. X{
  971. X    char *result;
  972. X
  973. X    result = xmalloc(strlen(a) + strlen(b) + 2);
  974. X    strcpy(result, a);
  975. X    strcat(result, " ");
  976. X    strcat(result, b);
  977. X    return result;
  978. X}
  979. X
  980. X/* Append two declaration specifier parts together.
  981. X */
  982. Xvoid
  983. Xjoin_decl_specs (result, a, b)
  984. XDeclSpec *result, *a, *b;
  985. X{
  986. X    result->text = concat_string(a->text, b->text);
  987. X    result->flags = a->flags | b->flags;
  988. X    result->begin = a->begin;
  989. X}
  990. X
  991. X/* Output an error message if the declaration specifier is an untagged
  992. X * struct, union or enum.
  993. X */
  994. Xvoid
  995. Xcheck_untagged (decl_spec)
  996. XDeclSpec *decl_spec;
  997. X{
  998. X    if (strstr(decl_spec->text, "struct {}") != NULL) {
  999. X    put_error();
  1000. X    fputs("untagged struct declaration\n", stderr);
  1001. X    } else if (strstr(decl_spec->text, "union {}") != NULL) {
  1002. X    put_error();
  1003. X    fputs("untagged union declaration\n", stderr);
  1004. X    } else if (strstr(decl_spec->text, "enum {}") != NULL) {
  1005. X    put_error();
  1006. X    fputs("untagged enum declaration\n", stderr);
  1007. X    }
  1008. X}
  1009. X
  1010. X/* Allocate and initialize a declarator.
  1011. X */
  1012. XDeclarator *
  1013. Xnew_declarator (text, name, offset)
  1014. Xchar *text, *name;
  1015. Xlong offset;
  1016. X{
  1017. X    Declarator *d;
  1018. X
  1019. X    d = (Declarator *)xmalloc(sizeof(Declarator));
  1020. X    d->text = xstrdup(text);
  1021. X    d->name = xstrdup(name);
  1022. X    d->begin = offset;
  1023. X    d->begin_comment = d->end_comment = 0;
  1024. X    d->func_def = FUNC_NONE;
  1025. X    new_ident_list(&d->params);
  1026. X    d->head = d;
  1027. X    d->func_stack = NULL;
  1028. X    return d;
  1029. X}
  1030. X
  1031. X/* Free storage used by a declarator.
  1032. X */
  1033. Xvoid
  1034. Xfree_declarator (d)
  1035. XDeclarator *d;
  1036. X{
  1037. X    free(d->text);
  1038. X    free(d->name);
  1039. X    free_param_list(&(d->params));
  1040. X    if (d->func_stack != NULL)
  1041. X    free_declarator(d->func_stack);
  1042. X    free(d);
  1043. X}
  1044. X
  1045. X/* Initialize a declarator list and add the given declarator to it.
  1046. X */
  1047. Xvoid
  1048. Xnew_decl_list (decl_list, declarator)
  1049. XDeclaratorList *decl_list;
  1050. XDeclarator *declarator;
  1051. X{
  1052. X    decl_list->first = decl_list->last = declarator;
  1053. X    declarator->next = NULL;
  1054. X}
  1055. X
  1056. X/* Free storage used by the declarators in the declarator list.
  1057. X */
  1058. Xvoid
  1059. Xfree_decl_list (decl_list)
  1060. XDeclaratorList *decl_list;
  1061. X{
  1062. X    Declarator *d, *next;
  1063. X
  1064. X    d = decl_list->first;
  1065. X    while (d != NULL) {
  1066. X    next = d->next;
  1067. X    free_declarator(d);
  1068. X    d = next;
  1069. X    }
  1070. X}
  1071. X
  1072. X/* Add the declarator to the declarator list.
  1073. X */
  1074. Xvoid
  1075. Xadd_decl_list (to, from, declarator)
  1076. XDeclaratorList *to, *from;
  1077. XDeclarator *declarator;
  1078. X{
  1079. X    to->first = from->first;
  1080. X    from->last->next = declarator;
  1081. X    to->last = declarator;
  1082. X    to->last->next = NULL;
  1083. X}
  1084. X
  1085. X/* Initialize the parameter structure.
  1086. X */
  1087. Xvoid
  1088. Xnew_parameter (param, decl_spec, declarator)
  1089. XParameter *param;        /* parameter to be initialized */
  1090. XDeclSpec *decl_spec;
  1091. XDeclarator *declarator;
  1092. X{
  1093. X    if (decl_spec == NULL) {
  1094. X    new_decl_spec(&(param->decl_spec), "", 0L, DS_JUNK);
  1095. X    } else {
  1096. X    param->decl_spec = *decl_spec;
  1097. X    }
  1098. X
  1099. X    if (declarator == NULL) {
  1100. X    declarator = new_declarator("", "", 0L);
  1101. X    }
  1102. X    param->declarator = declarator;
  1103. X
  1104. X    param->comment = NULL;
  1105. X}
  1106. X
  1107. X/* Free the storage used by the parameter.
  1108. X */
  1109. Xvoid
  1110. Xfree_parameter (param)
  1111. XParameter *param;
  1112. X{
  1113. X    free_decl_spec(&(param->decl_spec));
  1114. X    free_declarator(param->declarator);
  1115. X    if (param->comment != NULL)
  1116. X    free(param->comment);
  1117. X}
  1118. X
  1119. X/* Return TRUE if the parameter is void.
  1120. X */
  1121. Xboolean
  1122. Xis_void_parameter (p)
  1123. XParameter *p;
  1124. X{
  1125. X    return p == NULL || (strcmp(p->decl_spec.text, "void") == 0 &&
  1126. X    strlen(p->declarator->text) == 0);
  1127. X}
  1128. X
  1129. X/* Initialize a list of function parameters.
  1130. X */
  1131. Xvoid
  1132. Xnew_param_list (param_list, param)
  1133. XParameterList *param_list;
  1134. XParameter *param;
  1135. X{
  1136. X    Parameter *p;
  1137. X
  1138. X    p = (Parameter *)xmalloc(sizeof(Parameter));
  1139. X    *p = *param;
  1140. X    
  1141. X    param_list->first = param_list->last = p;
  1142. X    p->next = NULL;
  1143. X
  1144. X    param_list->begin_comment = param_list->end_comment = 0;
  1145. X    param_list->comment = NULL;
  1146. X}
  1147. X
  1148. X/* Free storage used by the elements in the function parameter list.
  1149. X */
  1150. Xvoid
  1151. Xfree_param_list (param_list)
  1152. XParameterList *param_list;
  1153. X{
  1154. X    Parameter *p, *next;
  1155. X
  1156. X    p = param_list->first;
  1157. X    while (p != NULL) {
  1158. X    next = p->next;
  1159. X    free_parameter(p);
  1160. X    free(p);
  1161. X    p = next;
  1162. X    }
  1163. X
  1164. X    if (param_list->comment != NULL)
  1165. X    free(param_list->comment);
  1166. X}
  1167. X
  1168. X/* Add the function parameter declaration to the list.
  1169. X */
  1170. Xvoid
  1171. Xadd_param_list (to, from, param)
  1172. XParameterList *to, *from;
  1173. XParameter *param;
  1174. X{
  1175. X    Parameter *p;
  1176. X
  1177. X    p = (Parameter *)xmalloc(sizeof(Parameter));
  1178. X    *p = *param;
  1179. X
  1180. X    to->first = from->first;
  1181. X    from->last->next = p;
  1182. X    to->last = p;
  1183. X    p->next = NULL;
  1184. X}
  1185. X
  1186. X/* Initialize an empty list of function parameter names.
  1187. X */
  1188. Xvoid
  1189. Xnew_ident_list (param_list)
  1190. XParameterList *param_list;
  1191. X{
  1192. X    param_list->first = param_list->last = NULL;
  1193. X    param_list->begin_comment = param_list->end_comment = 0;
  1194. X    param_list->comment = NULL;
  1195. X}
  1196. X
  1197. X/* Add an item to the list of function parameter declarations but set only
  1198. X * the parameter name field.
  1199. X */
  1200. Xvoid
  1201. Xadd_ident_list (to, from, name)
  1202. XParameterList *to, *from;
  1203. Xchar *name;
  1204. X{
  1205. X    Parameter *p;
  1206. X    Declarator *declarator;
  1207. X
  1208. X    p = (Parameter *)xmalloc(sizeof(Parameter));
  1209. X    declarator = new_declarator(name, name, 0L);
  1210. X    new_parameter(p, NULL, declarator);
  1211. X
  1212. X    to->first = from->first;
  1213. X    if (to->first == NULL) {
  1214. X    to->first = p;
  1215. X    } else {
  1216. X    from->last->next = p;
  1217. X    }
  1218. X    to->last = p;
  1219. X    p->next = NULL;
  1220. X}
  1221. X
  1222. X/* Search the list of parameters for a matching parameter name.
  1223. X * Return a pointer to the matching parameter or NULL if not found.
  1224. X */
  1225. Xstatic Parameter *
  1226. Xsearch_parameter_list (params, name)
  1227. XParameterList *params;
  1228. Xchar *name;
  1229. X{
  1230. X    Parameter *p;
  1231. X
  1232. X    for (p = params->first; p != NULL; p = p->next) {
  1233. X    if (strcmp(p->declarator->name, name) == 0)
  1234. X        return p;
  1235. X    }
  1236. X    return (Parameter *)NULL;
  1237. X}
  1238. X
  1239. X/* For each name in the declarator list <declarators>, set the declaration
  1240. X * specifier part of the parameter in <params> having the same name.
  1241. X * This is also where we promote formal parameters.  Parameters of type
  1242. X * "char", "unsigned char", "short", or "unsigned short" are promoted to
  1243. X * "int".  Parameters of type "float" are promoted to "double".
  1244. X */
  1245. Xvoid
  1246. Xset_param_types (params, decl_spec, declarators)
  1247. XParameterList *params;
  1248. XDeclSpec *decl_spec;
  1249. XDeclaratorList *declarators;
  1250. X{
  1251. X    Declarator *d;
  1252. X    Parameter *p;
  1253. X    char *decl_spec_text;
  1254. X
  1255. X    for (d = declarators->first; d != NULL; d = d->next) {
  1256. X    /* Search the parameter list for a matching name. */
  1257. X    p = search_parameter_list(params, d->name);
  1258. X    if (p == NULL) {
  1259. X        put_error();
  1260. X        fprintf(stderr, "declared argument \"%s\" is missing\n", d->name);
  1261. X    } else {
  1262. X        decl_spec_text = decl_spec->text;
  1263. X        if (promote_param && strcmp(d->text, d->name) == 0) {
  1264. X        if (decl_spec->flags & (DS_CHAR | DS_SHORT))
  1265. X            decl_spec_text = "int";
  1266. X        else if (decl_spec->flags & DS_FLOAT)
  1267. X            decl_spec_text = "double";
  1268. X        }
  1269. X        free(p->decl_spec.text);
  1270. X        p->decl_spec.text = xstrdup(decl_spec_text);
  1271. X
  1272. X        free_declarator(p->declarator);
  1273. X        p->declarator = d;
  1274. X    }
  1275. X    }
  1276. X}
  1277. X
  1278. Xstatic void put_declarator();
  1279. X
  1280. X/* Output a function parameter.
  1281. X */
  1282. Xstatic void
  1283. Xput_parameter (outf, p)
  1284. XFILE *outf;
  1285. XParameter *p;
  1286. X{
  1287. X    fputs(p->decl_spec.text, outf);
  1288. X    if (strlen(p->declarator->text) > 0) {
  1289. X    if (strcmp(p->declarator->text, "...") != 0)
  1290. X        if (where != FUNC_PROTO || proto_comments)
  1291. X        fputc(' ', outf);
  1292. X    put_declarator(outf, p->declarator);
  1293. X    }
  1294. X}
  1295. X
  1296. X/* Output a parameter list.
  1297. X */
  1298. Xstatic void
  1299. Xput_param_list (outf, declarator)
  1300. XFILE *outf;
  1301. XDeclarator *declarator;
  1302. X{
  1303. X    Parameter *p;
  1304. X    int f;
  1305. X
  1306. X    p = declarator->params.first;
  1307. X    if (is_void_parameter(p)) {
  1308. X    if (p != NULL || ((where == FUNC_PROTO || where == FUNC_DEF) &&
  1309. X     declarator == func_declarator))
  1310. X        fputs("void", outf);
  1311. X    } else {
  1312. X    f = (declarator == func_declarator) ? format : FMT_OTHER;
  1313. X
  1314. X    if (where == FUNC_DEF && declarator->params.comment != NULL)
  1315. X        fputs(declarator->params.comment, outf);
  1316. X        
  1317. X    fputs(fmt[f].first_param_prefix, outf);
  1318. X    put_parameter(outf, p);
  1319. X
  1320. X    while (p->next != NULL) {
  1321. X        fputc(',', outf);
  1322. X        if (where == FUNC_DEF && p->comment != NULL)
  1323. X        fputs(p->comment, outf);
  1324. X
  1325. X        p = p->next;
  1326. X        fputs(fmt[f].middle_param_prefix, outf);
  1327. X        put_parameter(outf, p);
  1328. X    }
  1329. X    if (where == FUNC_DEF && p->comment != NULL)
  1330. X        fputs(p->comment, outf);
  1331. X
  1332. X    fputs(fmt[f].last_param_suffix, outf);
  1333. X    }
  1334. X}
  1335. X
  1336. X/* Output function parameters.
  1337. X */
  1338. Xstatic void
  1339. Xput_parameters (outf, declarator)
  1340. XFILE *outf;
  1341. XDeclarator *declarator;
  1342. X{
  1343. X    Parameter *p;
  1344. X
  1345. X    if (where == FUNC_DEF && func_style == FUNC_TRADITIONAL) {
  1346. X
  1347. X    /* Output parameter name list for traditional function definition. */
  1348. X    p = declarator->params.first;
  1349. X
  1350. X    /* Output paramter name list only for head function declarator. */
  1351. X    if (!is_void_parameter(p) && declarator == func_declarator) {
  1352. X        fputs(fmt[format].first_param_prefix, outf);
  1353. X        fputs(p->declarator->name, outf);
  1354. X        p = p->next;
  1355. X        while (p != NULL && strcmp(p->declarator->text, "...") != 0) {
  1356. X        fputc(',', outf);
  1357. X        fputs(fmt[format].middle_param_prefix, outf);
  1358. X        fputs(p->declarator->name, outf);
  1359. X        p = p->next;
  1360. X        }
  1361. X        fputs(fmt[format].last_param_suffix, outf);
  1362. X    }
  1363. X    } else {
  1364. X
  1365. X    /* Output parameter type list. */
  1366. X    if (where == FUNC_PROTO && proto_style == PROTO_TRADITIONAL &&
  1367. X     declarator == func_declarator) {
  1368. X        if (proto_comments) {
  1369. X        fputs("/*", outf);
  1370. X        put_param_list(outf, declarator);
  1371. X        fputs("*/", outf);
  1372. X        }
  1373. X    } else {
  1374. X        put_param_list(outf, declarator);
  1375. X    }
  1376. X    }
  1377. X}
  1378. X
  1379. X/* Output a function declarator.
  1380. X */
  1381. Xstatic void
  1382. Xput_func_declarator (outf, declarator)
  1383. XFILE *outf;
  1384. XDeclarator *declarator;
  1385. X{
  1386. X    char *s, *t, *decl_text;
  1387. X    int f;
  1388. X
  1389. X    /* Output declarator text before function declarator place holder. */
  1390. X    if ((s = strstr(declarator->text, "%s")) == NULL)
  1391. X    return;
  1392. X    *s = '\0';
  1393. X    fputs(declarator->text, outf);
  1394. X
  1395. X    /* Substitute place holder with function declarator. */
  1396. X    if (declarator->func_stack->func_def == FUNC_NONE) {
  1397. X
  1398. X    decl_text = declarator->func_stack->text;
  1399. X    if (strlen(declarator->name) == 0) {
  1400. X        fputs(decl_text, outf);
  1401. X    } else {
  1402. X
  1403. X        /* Output the declarator text before the declarator name. */
  1404. X        if ((t = strstr(decl_text, declarator->name)) == NULL)
  1405. X        return;
  1406. X        *t = '\0';
  1407. X        fputs(decl_text, outf);
  1408. X        *t = declarator->name[0];
  1409. X
  1410. X        /* Output the declarator prefix before the name. */
  1411. X        f = (declarator == func_declarator) ? format : FMT_OTHER;
  1412. X        if (strcmp(fmt[f].declarator_prefix, " ") != 0)
  1413. X        fputs(fmt[f].declarator_prefix, outf);
  1414. X
  1415. X        /* Output the declarator name. */
  1416. X        if (where == FUNC_PROTO && proto_style == PROTO_ABSTRACT &&
  1417. X         declarator != func_declarator) {
  1418. X        if (proto_comments) {
  1419. X            fputs("/*", outf);
  1420. X            fputs(declarator->name, outf);
  1421. X            fputs("*/", outf);
  1422. X        }
  1423. X        } else {
  1424. X        fputs(declarator->name, outf);
  1425. X        }
  1426. X
  1427. X        /* Output the remaining declarator text. */
  1428. X        fputs(t + strlen(declarator->name), outf);
  1429. X
  1430. X        /* Output the declarator suffix. */
  1431. X        fputs(fmt[f].declarator_suffix, outf);
  1432. X    }
  1433. X    } else {
  1434. X    put_func_declarator(outf, declarator->func_stack);
  1435. X    }
  1436. X    *s = '%';
  1437. X    s += 2;
  1438. X
  1439. X    /* Output declarator text up to but before parameters place holder. */
  1440. X    if ((t = strstr(s, "()")) == NULL)
  1441. X    return;
  1442. X    *t = '\0';
  1443. X    fputs(s, outf);
  1444. X
  1445. X    if (where == FUNC_PROTO && proto_style == PROTO_MACRO &&
  1446. X     declarator == func_declarator) {
  1447. X    fprintf(outf, " %s(", macro_name);
  1448. X    }
  1449. X
  1450. X    /* Substitute place holder with function parameters. */
  1451. X    fputc(*t++ = '(', outf);
  1452. X    put_parameters(outf, declarator);
  1453. X    fputs(t, outf);
  1454. X
  1455. X    if (where == FUNC_PROTO && proto_style == PROTO_MACRO &&
  1456. X     declarator == func_declarator) {
  1457. X    fputc(')', outf);
  1458. X    }
  1459. X}
  1460. X
  1461. X/* Output a declarator.
  1462. X */
  1463. Xstatic void
  1464. Xput_declarator (outf, declarator)
  1465. XFILE *outf;
  1466. XDeclarator *declarator;
  1467. X{
  1468. X    char *s;
  1469. X
  1470. X    if (declarator->func_def == FUNC_NONE) {
  1471. X    if (where == FUNC_PROTO && proto_style == PROTO_ABSTRACT &&
  1472. X     strlen(declarator->name) > 0) {
  1473. X        if ((s = strstr(declarator->text, declarator->name)) == NULL)
  1474. X        return;
  1475. X        *s = '\0';
  1476. X        if (proto_comments) {
  1477. X        fprintf(outf, "%s/*%s*/%s", declarator->text, declarator->name,
  1478. X         s + strlen(declarator->name));
  1479. X        } else {
  1480. X        fprintf(outf, "%s%s", declarator->text,
  1481. X         s + strlen(declarator->name));
  1482. X        }
  1483. X        *s = declarator->name[0];
  1484. X    } else {
  1485. X        fputs(declarator->text, outf);
  1486. X    }
  1487. X    } else {
  1488. X    put_func_declarator(outf, declarator);
  1489. X    }
  1490. X}
  1491. X
  1492. X/* Output a declaration specifier for an external declaration.
  1493. X */
  1494. Xstatic void
  1495. Xput_decl_spec (outf, decl_spec)
  1496. XFILE *outf;
  1497. XDeclSpec *decl_spec;
  1498. X{
  1499. X    if (extern_out && (decl_spec->flags & DS_STATIC) == 0) {
  1500. X    if (strstr(decl_spec->text, "extern") == NULL) {
  1501. X        fputs("extern ", outf);
  1502. X    }
  1503. X    }
  1504. X    fputs(decl_spec->text, outf);
  1505. X    fputc(' ', outf);
  1506. X}
  1507. X
  1508. X/* Generate variable declarations.
  1509. X */
  1510. Xvoid
  1511. Xgen_declarations (decl_spec, decl_list)
  1512. XDeclSpec *decl_spec;        /* declaration specifier */
  1513. XDeclaratorList *decl_list;    /* list of declared variables */
  1514. X{
  1515. X    Declarator *d;
  1516. X
  1517. X    if (!variables_out || (decl_spec->flags & DS_JUNK))
  1518. X    return;
  1519. X    if (!static_out && (decl_spec->flags & DS_STATIC))
  1520. X    return;
  1521. X
  1522. X    func_declarator = NULL;
  1523. X    where = FUNC_OTHER;
  1524. X    format = FMT_OTHER;
  1525. X    for (d = decl_list->first; d != NULL; d = d->next) {
  1526. X    if (d->func_def == FUNC_NONE) {
  1527. X        fputs(fmt[FMT_PROTO].decl_spec_prefix, stdout);
  1528. X        put_decl_spec(stdout, decl_spec);
  1529. X        put_declarator(stdout, d);
  1530. X        fputs(";\n", stdout);
  1531. X    }
  1532. X    }
  1533. X}
  1534. X
  1535. X/* If a parameter name appears in the parameter list of a traditional style
  1536. X * function definition but is not declared in the parameter declarations,
  1537. X * then assign it the default type "int".
  1538. X */
  1539. Xstatic void
  1540. Xset_param_decl_spec (declarator)
  1541. XDeclarator *declarator;
  1542. X{
  1543. X    Parameter *p;
  1544. X
  1545. X    for (p = declarator->params.first; p != NULL; p = p->next) {
  1546. X    if (strlen(p->decl_spec.text) == 0 &&
  1547. X        strcmp(p->declarator->text, "...") != 0) {
  1548. X        free(p->decl_spec.text);
  1549. X        p->decl_spec.text = xstrdup("int");
  1550. X    }
  1551. X    }
  1552. X}
  1553. X
  1554. X/* Generate a function prototype.
  1555. X */
  1556. Xvoid
  1557. Xgen_prototype (decl_spec, declarator)
  1558. XDeclSpec *decl_spec;
  1559. XDeclarator *declarator;
  1560. X{
  1561. X    if (proto_style == PROTO_NONE)
  1562. X    return;
  1563. X    if (decl_spec->flags & DS_JUNK)
  1564. X    return;
  1565. X    if (!static_out && (decl_spec->flags & DS_STATIC))
  1566. X    return;
  1567. X
  1568. X    func_declarator = declarator->head;
  1569. X    set_param_decl_spec(func_declarator);
  1570. X
  1571. X    where = FUNC_PROTO;
  1572. X    format = FMT_PROTO;
  1573. X    fputs(fmt[format].decl_spec_prefix, stdout);
  1574. X    put_decl_spec(stdout, decl_spec);
  1575. X    put_func_declarator(stdout, declarator);
  1576. X    fputs(";\n", stdout);
  1577. X}
  1578. X
  1579. X/* Generate a declarator for a function pointer declarator or prototype.
  1580. X */
  1581. Xvoid
  1582. Xgen_func_declarator (declarator)
  1583. XDeclarator *declarator;
  1584. X{
  1585. X    /* Go to the beginning of the function declarator in the temporary
  1586. X     * file and overwrite it with the converted declarator.
  1587. X     */
  1588. X    fseek(cur_tmp_file(), declarator->begin, 0);
  1589. X    func_declarator = NULL;
  1590. X    where = FUNC_DEF;
  1591. X    format = FMT_FUNC;
  1592. X    put_func_declarator(cur_tmp_file(), declarator);
  1593. X    cur_file_changed();
  1594. X}
  1595. X
  1596. X/* Generate a function definition head.
  1597. X */
  1598. Xvoid
  1599. Xgen_func_definition (decl_spec, declarator)
  1600. XDeclSpec *decl_spec;
  1601. XDeclarator *declarator;
  1602. X{
  1603. X    Parameter *p;
  1604. X    ParameterList *params;
  1605. X    char *comment;
  1606. X    int comment_len, n;
  1607. X
  1608. X    /* Return if the function is already defined in the desired style. */
  1609. X    if (declarator->func_def == func_style)
  1610. X    return;
  1611. X
  1612. X    /* Save the text between the function head and the function body.
  1613. X     * Read the temporary file from after the last ) or ; to the
  1614. X     * end of the file.
  1615. X     */
  1616. X    comment_len = (int)(ftell(cur_tmp_file()) - cur_begin_comment());
  1617. X    comment = xmalloc(comment_len);
  1618. X    fseek(cur_tmp_file(), cur_begin_comment(), 0);
  1619. X    fread(comment, sizeof(char), comment_len, cur_tmp_file());
  1620. X
  1621. X    func_declarator = declarator->head;
  1622. X    format = FMT_FUNC;
  1623. X
  1624. X    /* Save the text before the parameter declarations. */
  1625. X    if (func_style == FUNC_ANSI) {
  1626. X    params = &func_declarator->params;
  1627. X    n = (int)(params->end_comment - params->begin_comment);
  1628. X    if (n > 0) {
  1629. X        params->comment = xmalloc(n+1);
  1630. X        fseek(cur_tmp_file(), params->begin_comment, 0);
  1631. X        fread(params->comment, sizeof(char), n, cur_tmp_file());
  1632. X        params->comment[n] = '\0';
  1633. X        format = FMT_FUNC_COMMENT;
  1634. X    }
  1635. X    }
  1636. X
  1637. X    /* Get the parameter comments. */
  1638. X    for (p = func_declarator->params.first; p != NULL; p = p->next) {
  1639. X    n = (int)(p->declarator->end_comment - p->declarator->begin_comment);
  1640. X    if (n > 0) {
  1641. X        p->comment = xmalloc(n+1);
  1642. X        fseek(cur_tmp_file(), p->declarator->begin_comment, 0);
  1643. X        fread(p->comment, sizeof(char), n, cur_tmp_file());
  1644. X        p->comment[n] = '\0';
  1645. X        format = FMT_FUNC_COMMENT;
  1646. X    }
  1647. X    }
  1648. X
  1649. X    set_param_decl_spec(func_declarator);
  1650. X
  1651. X    /* Go to the beginning of the function head in the temporary file
  1652. X     * and overwrite it with the converted function head.
  1653. X     */
  1654. X    fseek(cur_tmp_file(), decl_spec->begin, 0);
  1655. X
  1656. X    /* Output declarator specifiers. */
  1657. X    fputs(fmt[format].decl_spec_prefix, cur_tmp_file());
  1658. X    fputs(decl_spec->text, cur_tmp_file());
  1659. X    fputc(' ', cur_tmp_file());
  1660. X
  1661. X    /* Output function declarator. */
  1662. X    where = FUNC_DEF;
  1663. X    put_func_declarator(cur_tmp_file(), declarator);
  1664. X
  1665. X    if (func_style == FUNC_TRADITIONAL) {
  1666. X    /* Output traditional style parameter declarations. */
  1667. X    p = func_declarator->params.first;
  1668. X    if (!is_void_parameter(p)) {
  1669. X        fputc('\n', cur_tmp_file());
  1670. X        put_parameter(cur_tmp_file(), p);
  1671. X        fputc(';', cur_tmp_file());
  1672. X        p = p->next;
  1673. X        while (p != NULL && strcmp(p->declarator->text, "...") != 0) {
  1674. X        fputc('\n', cur_tmp_file());
  1675. X        put_parameter(cur_tmp_file(), p);
  1676. X        fputc(';', cur_tmp_file());
  1677. X        p = p->next;
  1678. X        }
  1679. X    }
  1680. X    }
  1681. X
  1682. X    /* Output text between function head and body. */
  1683. X    fwrite(comment, sizeof(char), comment_len, cur_tmp_file());
  1684. X    free(comment);
  1685. X
  1686. X    cur_file_changed();
  1687. X}
  1688. END_OF_semantic.c
  1689. if test 18606 -ne `wc -c <semantic.c`; then
  1690.     echo shar: \"semantic.c\" unpacked with wrong size!
  1691. fi
  1692. # end of overwriting check
  1693. fi
  1694. if test -f strstr.c -a "${1}" != "-c" ; then 
  1695.   echo shar: Will not over-write existing file \"strstr.c\"
  1696. else
  1697. echo shar: Extracting \"strstr.c\" \(547 characters\)
  1698. sed "s/^X//" >strstr.c <<'END_OF_strstr.c'
  1699. X/* $Id: strstr.c 3.2 92/03/06 00:51:10 cthuang Exp $
  1700. X *
  1701. X * Simple implementation of the ANSI strstr() function
  1702. X */
  1703. X#include <stdio.h>
  1704. X#include "config.h"
  1705. X
  1706. X/* Search for a substring within the given string.
  1707. X * Return a pointer to the first occurence within the string,
  1708. X * or NULL if not found.
  1709. X */
  1710. Xchar *
  1711. Xstrstr (src, key)
  1712. Xchar *src, *key;
  1713. X{
  1714. X    char *s;
  1715. X    int keylen;
  1716. X
  1717. X    keylen = strlen(key);
  1718. X    s = strchr(src, *key);
  1719. X    while (s != NULL) {
  1720. X    if (strncmp(s, key, keylen) == 0)
  1721. X        return s;
  1722. X    s = strchr(s+1, *key);
  1723. X    }
  1724. X    return NULL;
  1725. X}
  1726. END_OF_strstr.c
  1727. if test 547 -ne `wc -c <strstr.c`; then
  1728.     echo shar: \"strstr.c\" unpacked with wrong size!
  1729. fi
  1730. # end of overwriting check
  1731. fi
  1732. if test -f symbol.c -a "${1}" != "-c" ; then 
  1733.   echo shar: Will not over-write existing file \"symbol.c\"
  1734. else
  1735. echo shar: Extracting \"symbol.c\" \(2326 characters\)
  1736. sed "s/^X//" >symbol.c <<'END_OF_symbol.c'
  1737. X/* $Id: symbol.c 3.1 92/03/03 10:43:52 cthuang Exp $
  1738. X *
  1739. X * Implements a symbol table abstract data type.
  1740. X */
  1741. X#include <stdio.h>
  1742. X#include "cproto.h"
  1743. X#include "symbol.h"
  1744. X
  1745. X
  1746. X/* Create a symbol table.
  1747. X * Return a pointer to the symbol table or NULL if an error occurs.
  1748. X */
  1749. XSymbolTable *
  1750. Xnew_symbol_table ()
  1751. X{
  1752. X    SymbolTable *symtab;
  1753. X    int i;
  1754. X
  1755. X    if ((symtab = (SymbolTable *)xmalloc(sizeof(SymbolTable))) != NULL) {
  1756. X    for (i = 0; i < SYM_MAX_HASH; ++i)
  1757. X        symtab->bucket[i] = NULL;
  1758. X    }
  1759. X    return symtab;
  1760. X}
  1761. X
  1762. X
  1763. X/* Free the memory allocated to the symbol table.
  1764. X */
  1765. Xvoid
  1766. Xfree_symbol_table (symtab)
  1767. XSymbolTable *symtab;
  1768. X{
  1769. X    int i;
  1770. X    Symbol *sym, *next;
  1771. X
  1772. X    for (i = 0; i < SYM_MAX_HASH; ++i) {
  1773. X    sym = symtab->bucket[i];
  1774. X    while (sym != NULL) {
  1775. X        next = sym->next;
  1776. X        free(sym->name);
  1777. X        free(sym);
  1778. X        sym = next;
  1779. X    }
  1780. X    }
  1781. X}
  1782. X
  1783. X
  1784. X/* This is a simple hash function mapping a symbol name to a hash bucket. */
  1785. X
  1786. Xstatic unsigned
  1787. Xhash (name)
  1788. Xchar *name;
  1789. X{
  1790. X    char *s;
  1791. X    unsigned h;
  1792. X
  1793. X    h = 0;
  1794. X    s = name;
  1795. X    while (*s != '\0')
  1796. X    h = (h << 1) ^ *s++;
  1797. X    return h % SYM_MAX_HASH;
  1798. X}
  1799. X
  1800. X
  1801. X/* Search the list of symbols <list> for the symbol <name>.
  1802. X * Return a pointer to the symbol or NULL if not found.
  1803. X */
  1804. Xstatic Symbol *
  1805. Xsearch_symbol_list (list, name)
  1806. XSymbol *list;
  1807. Xchar *name;
  1808. X{
  1809. X    Symbol *sym;
  1810. X
  1811. X    for (sym = list; sym != NULL; sym = sym->next) {
  1812. X    if (strcmp(sym->name, name) == 0)
  1813. X        return sym;
  1814. X    }
  1815. X    return NULL;
  1816. X}
  1817. X
  1818. X
  1819. X/* Look for symbol <name> in symbol table <symtab>.
  1820. X * Return a pointer to the symbol or NULL if not found.
  1821. X */
  1822. XSymbol *
  1823. Xfind_symbol (symtab, name)
  1824. XSymbolTable *symtab;
  1825. Xchar *name;
  1826. X{
  1827. X    return search_symbol_list(symtab->bucket[hash(name)], name);
  1828. X}
  1829. X
  1830. X
  1831. X/* If the symbol <name> does not already exist in symbol table <symtab>,
  1832. X * then add the symbol to the symbol table.
  1833. X * Return a pointer to the symbol or NULL on an error.
  1834. X */
  1835. XSymbol *
  1836. Xnew_symbol (symtab, name, flags)
  1837. XSymbolTable *symtab;    /* symbol table */
  1838. Xchar *name;        /* symbol name */
  1839. Xint flags;        /* symbol attributes */
  1840. X{
  1841. X    Symbol *sym;
  1842. X    int i;
  1843. X
  1844. X    if ((sym = find_symbol(symtab, name)) == NULL) {
  1845. X    if ((sym = (Symbol *)xmalloc(sizeof(Symbol))) != NULL) {
  1846. X        sym->name = xstrdup(name);
  1847. X        sym->flags = flags;
  1848. X        i = hash(name);
  1849. X        sym->next = symtab->bucket[i];
  1850. X        symtab->bucket[i] = sym;
  1851. X    }
  1852. X    }
  1853. X    return sym;
  1854. X}
  1855. END_OF_symbol.c
  1856. if test 2326 -ne `wc -c <symbol.c`; then
  1857.     echo shar: \"symbol.c\" unpacked with wrong size!
  1858. fi
  1859. # end of overwriting check
  1860. fi
  1861. echo shar: End of shell archive.
  1862. exit 0
  1863.  
  1864. exit 0 # Just in case...
  1865.