ansi2knr.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises. All rights reserved. */
  2. /*$Id: ansi2knr.c,v 1.14 2003/09/06 05:36:56 eggert Exp $*/
  3. /* Convert ANSI C function definitions to K&R ("traditional C") syntax */
  4. /*
  5. ansi2knr is distributed in the hope that it will be useful, but WITHOUT ANY
  6. WARRANTY. No author or distributor accepts responsibility to anyone for the
  7. consequences of using it or for whether it serves any particular purpose or
  8. works at all, unless he says so in writing. Refer to the GNU General Public
  9. License (the "GPL") for full details.
  10. Everyone is granted permission to copy, modify and redistribute ansi2knr,
  11. but only under the conditions described in the GPL. A copy of this license
  12. is supposed to have been given to you along with ansi2knr so you can know
  13. your rights and responsibilities. It should be in a file named COPYLEFT,
  14. or, if there is no file named COPYLEFT, a file named COPYING. Among other
  15. things, the copyright notice and this notice must be preserved on all
  16. copies.
  17. We explicitly state here what we believe is already implied by the GPL: if
  18. the ansi2knr program is distributed as a separate set of sources and a
  19. separate executable file which are aggregated on a storage medium together
  20. with another program, this in itself does not bring the other program under
  21. the GPL, nor does the mere fact that such a program or the procedures for
  22. constructing it invoke the ansi2knr executable bring any other part of the
  23. program under the GPL.
  24. */
  25. /*
  26. * Usage:
  27. ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]]
  28. * --filename provides the file name for the #line directive in the output,
  29. * overriding input_file (if present).
  30. * If no input_file is supplied, input is read from stdin.
  31. * If no output_file is supplied, output goes to stdout.
  32. * There are no error messages.
  33. *
  34. * ansi2knr recognizes function definitions by seeing a non-keyword
  35. * identifier at the left margin, followed by a left parenthesis, with a
  36. * right parenthesis as the last character on the line, and with a left
  37. * brace as the first token on the following line (ignoring possible
  38. * intervening comments and/or preprocessor directives), except that a line
  39. * consisting of only
  40. * identifier1(identifier2)
  41. * will not be considered a function definition unless identifier2 is
  42. * the word "void", and a line consisting of
  43. * identifier1(identifier2, <<arbitrary>>)
  44. * will not be considered a function definition.
  45. * ansi2knr will recognize a multi-line header provided that no intervening
  46. * line ends with a left or right brace or a semicolon. These algorithms
  47. * ignore whitespace, comments, and preprocessor directives, except that
  48. * the function name must be the first thing on the line. The following
  49. * constructs will confuse it:
  50. * - Any other construct that starts at the left margin and
  51. * follows the above syntax (such as a macro or function call).
  52. * - Some macros that tinker with the syntax of function headers.
  53. */
  54. /*
  55. * The original and principal author of ansi2knr is L. Peter Deutsch
  56. * <ghost@aladdin.com>. Other authors are noted in the change history
  57. * that follows (in reverse chronological order):
  58. lpd 2000-04-12 backs out Eggert's changes because of bugs:
  59. - concatlits didn't declare the type of its bufend argument;
  60. - concatlits didn't recognize when it was inside a comment;
  61. - scanstring could scan backward past the beginning of the string; when
  62. - the check for \ + newline in scanstring was unnecessary.
  63. 2000-03-05 Paul Eggert <eggert@twinsun.com>
  64. Add support for concatenated string literals.
  65. * ansi2knr.c (concatlits): New decl.
  66. (main): Invoke concatlits to concatenate string literals.
  67. (scanstring): Handle backslash-newline correctly. Work with
  68. character constants. Fix bug when scanning backwards through
  69. backslash-quote. Check for unterminated strings.
  70. (convert1): Parse character constants, too.
  71. (appendline, concatlits): New functions.
  72. * ansi2knr.1: Document this.
  73. lpd 1999-08-17 added code to allow preprocessor directives
  74. wherever comments are allowed
  75. lpd 1999-04-12 added minor fixes from Pavel Roskin
  76. <pavel_roskin@geocities.com> for clean compilation with
  77. gcc -W -Wall
  78. lpd 1999-03-22 added hack to recognize lines consisting of
  79. identifier1(identifier2, xxx) as *not* being procedures
  80. lpd 1999-02-03 made indentation of preprocessor commands consistent
  81. lpd 1999-01-28 fixed two bugs: a '/' in an argument list caused an
  82. endless loop; quoted strings within an argument list
  83. confused the parser
  84. lpd 1999-01-24 added a check for write errors on the output,
  85. suggested by Jim Meyering <meyering@ascend.com>
  86. lpd 1998-11-09 added further hack to recognize identifier(void)
  87. as being a procedure
  88. lpd 1998-10-23 added hack to recognize lines consisting of
  89. identifier1(identifier2) as *not* being procedures
  90. lpd 1997-12-08 made input_file optional; only closes input and/or
  91. output file if not stdin or stdout respectively; prints
  92. usage message on stderr rather than stdout; adds
  93. --filename switch (changes suggested by
  94. <ceder@lysator.liu.se>)
  95. lpd 1996-01-21 added code to cope with not HAVE_CONFIG_H and with
  96. compilers that don't understand void, as suggested by
  97. Tom Lane
  98. lpd 1996-01-15 changed to require that the first non-comment token
  99. on the line following a function header be a left brace,
  100. to reduce sensitivity to macros, as suggested by Tom Lane
  101. <tgl@sss.pgh.pa.us>
  102. lpd 1995-06-22 removed #ifndefs whose sole purpose was to define
  103. undefined preprocessor symbols as 0; changed all #ifdefs
  104. for configuration symbols to #ifs
  105. lpd 1995-04-05 changed copyright notice to make it clear that
  106. including ansi2knr in a program does not bring the entire
  107. program under the GPL
  108. lpd 1994-12-18 added conditionals for systems where ctype macros
  109. don't handle 8-bit characters properly, suggested by
  110. Francois Pinard <pinard@iro.umontreal.ca>;
  111. removed --varargs switch (this is now the default)
  112. lpd 1994-10-10 removed CONFIG_BROKETS conditional
  113. lpd 1994-07-16 added some conditionals to help GNU `configure',
  114. suggested by Francois Pinard <pinard@iro.umontreal.ca>;
  115. properly erase prototype args in function parameters,
  116. contributed by Jim Avera <jima@netcom.com>;
  117. correct error in writeblanks (it shouldn't erase EOLs)
  118. lpd 1989-xx-xx original version
  119. */
  120. /* Most of the conditionals here are to make ansi2knr work with */
  121. /* or without the GNU configure machinery. */
  122. #if HAVE_CONFIG_H
  123. # include <config.h>
  124. #endif
  125. #include <stdio.h>
  126. #include <ctype.h>
  127. #if HAVE_CONFIG_H
  128. /*
  129. For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
  130. This will define HAVE_CONFIG_H and so, activate the following lines.
  131. */
  132. # if STDC_HEADERS || HAVE_STRING_H
  133. # include <string.h>
  134. # else
  135. # include <strings.h>
  136. # endif
  137. #else /* not HAVE_CONFIG_H */
  138. /* Otherwise do it the hard way */
  139. # ifdef BSD
  140. # include <strings.h>
  141. # else
  142. # ifdef VMS
  143. extern int strlen(), strncmp();
  144. # else
  145. # include <string.h>
  146. # endif
  147. # endif
  148. #endif /* not HAVE_CONFIG_H */
  149. #if STDC_HEADERS
  150. # include <stdlib.h>
  151. #else
  152. /*
  153. malloc and free should be declared in stdlib.h,
  154. but if you've got a K&R compiler, they probably aren't.
  155. */
  156. # ifdef MSDOS
  157. # include <malloc.h>
  158. # else
  159. # ifdef VMS
  160. extern char *malloc();
  161. extern void free();
  162. # else
  163. extern char *malloc();
  164. extern int free();
  165. # endif
  166. # endif
  167. #endif
  168. /* Define NULL (for *very* old compilers). */
  169. #ifndef NULL
  170. # define NULL (0)
  171. #endif
  172. /*
  173. * The ctype macros don't always handle 8-bit characters correctly.
  174. * Compensate for this here.
  175. */
  176. #ifdef isascii
  177. # undef HAVE_ISASCII /* just in case */
  178. # define HAVE_ISASCII 1
  179. #else
  180. #endif
  181. #if STDC_HEADERS || !HAVE_ISASCII
  182. # define is_ascii(c) 1
  183. #else
  184. # define is_ascii(c) isascii(c)
  185. #endif
  186. #define is_space(c) (is_ascii(c) && isspace(c))
  187. #define is_alpha(c) (is_ascii(c) && isalpha(c))
  188. #define is_alnum(c) (is_ascii(c) && isalnum(c))
  189. /* Scanning macros */
  190. #define isidchar(ch) (is_alnum(ch) || (ch) == '_')
  191. #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
  192. /* Forward references */
  193. char *ppdirforward();
  194. char *ppdirbackward();
  195. char *skipspace();
  196. char *scanstring();
  197. int writeblanks();
  198. int test1();
  199. int convert1();
  200. /* The main program */
  201. int
  202. main(argc, argv)
  203. int argc;
  204. char *argv[];
  205. { FILE *in = stdin;
  206. FILE *out = stdout;
  207. char *filename = 0;
  208. char *program_name = argv[0];
  209. char *output_name = 0;
  210. #define bufsize 5000 /* arbitrary size */
  211. char *buf;
  212. char *line;
  213. char *more;
  214. char *usage =
  215. "Usage: ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]]\n";
  216. /*
  217. * In previous versions, ansi2knr recognized a --varargs switch.
  218. * If this switch was supplied, ansi2knr would attempt to convert
  219. * a ... argument to va_alist and va_dcl; if this switch was not
  220. * supplied, ansi2knr would simply drop any such arguments.
  221. * Now, ansi2knr always does this conversion, and we only
  222. * check for this switch for backward compatibility.
  223. */
  224. int convert_varargs = 1;
  225. int output_error;
  226. while ( argc > 1 && argv[1][0] == '-' ) {
  227. if ( !strcmp(argv[1], "--varargs") ) {
  228. convert_varargs = 1;
  229. argc--;
  230. argv++;
  231. continue;
  232. }
  233. if ( !strcmp(argv[1], "--filename") && argc > 2 ) {
  234. filename = argv[2];
  235. argc -= 2;
  236. argv += 2;
  237. continue;
  238. }
  239. fprintf(stderr, "%s: Unrecognized switch: %s\n", program_name,
  240. argv[1]);
  241. fprintf(stderr, usage);
  242. exit(1);
  243. }
  244. switch ( argc )
  245. {
  246. default:
  247. fprintf(stderr, usage);
  248. exit(0);
  249. case 3:
  250. output_name = argv[2];
  251. out = fopen(output_name, "w");
  252. if ( out == NULL ) {
  253. fprintf(stderr, "%s: Cannot open output file %s\n",
  254. program_name, output_name);
  255. exit(1);
  256. }
  257. /* falls through */
  258. case 2:
  259. in = fopen(argv[1], "r");
  260. if ( in == NULL ) {
  261. fprintf(stderr, "%s: Cannot open input file %s\n",
  262. program_name, argv[1]);
  263. exit(1);
  264. }
  265. if ( filename == 0 )
  266. filename = argv[1];
  267. /* falls through */
  268. case 1:
  269. break;
  270. }
  271. if ( filename )
  272. fprintf(out, "#line 1 \"%s\"\n", filename);
  273. buf = malloc(bufsize);
  274. if ( buf == NULL )
  275. {
  276. fprintf(stderr, "Unable to allocate read buffer!\n");
  277. exit(1);
  278. }
  279. line = buf;
  280. while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL )
  281. {
  282. test: line += strlen(line);
  283. switch ( test1(buf) )
  284. {
  285. case 2: /* a function header */
  286. convert1(buf, out, 1, convert_varargs);
  287. break;
  288. case 1: /* a function */
  289. /* Check for a { at the start of the next line. */
  290. more = ++line;
  291. f: if ( line >= buf + (bufsize - 1) ) /* overflow check */
  292. goto wl;
  293. if ( fgets(line, (unsigned)(buf + bufsize - line), in) == NULL )
  294. goto wl;
  295. switch ( *skipspace(ppdirforward(more), 1) )
  296. {
  297. case '{':
  298. /* Definitely a function header. */
  299. convert1(buf, out, 0, convert_varargs);
  300. fputs(more, out);
  301. break;
  302. case 0:
  303. /* The next line was blank or a comment: */
  304. /* keep scanning for a non-comment. */
  305. line += strlen(line);
  306. goto f;
  307. default:
  308. /* buf isn't a function header, but */
  309. /* more might be. */
  310. fputs(buf, out);
  311. strcpy(buf, more);
  312. line = buf;
  313. goto test;
  314. }
  315. break;
  316. case -1: /* maybe the start of a function */
  317. if ( line != buf + (bufsize - 1) ) /* overflow check */
  318. continue;
  319. /* falls through */
  320. default: /* not a function */
  321. wl: fputs(buf, out);
  322. break;
  323. }
  324. line = buf;
  325. }
  326. if ( line != buf )
  327. fputs(buf, out);
  328. free(buf);
  329. if ( output_name ) {
  330. output_error = ferror(out);
  331. output_error |= fclose(out);
  332. } else { /* out == stdout */
  333. fflush(out);
  334. output_error = ferror(out);
  335. }
  336. if ( output_error ) {
  337. fprintf(stderr, "%s: error writing to %s\n", program_name,
  338. (output_name ? output_name : "stdout"));
  339. exit(1);
  340. }
  341. if ( in != stdin )
  342. fclose(in);
  343. return 0;
  344. }
  345. /*
  346. * Skip forward or backward over one or more preprocessor directives.
  347. */
  348. char *
  349. ppdirforward(p)
  350. char *p;
  351. {
  352. for (; *p == '#'; ++p) {
  353. for (; *p != '\r' && *p != '\n'; ++p)
  354. if (*p == 0)
  355. return p;
  356. if (*p == '\r' && p[1] == '\n')
  357. ++p;
  358. }
  359. return p;
  360. }
  361. char *
  362. ppdirbackward(p, limit)
  363. char *p;
  364. char *limit;
  365. {
  366. char *np = p;
  367. for (;; p = --np) {
  368. if (*np == '\n' && np[-1] == '\r')
  369. --np;
  370. for (; np > limit && np[-1] != '\r' && np[-1] != '\n'; --np)
  371. if (np[-1] == 0)
  372. return np;
  373. if (*np != '#')
  374. return p;
  375. }
  376. }
  377. /*
  378. * Skip over whitespace, comments, and preprocessor directives,
  379. * in either direction.
  380. */
  381. char *
  382. skipspace(p, dir)
  383. char *p;
  384. int dir; /* 1 for forward, -1 for backward */
  385. {
  386. for ( ; ; ) {
  387. while ( is_space(*p) )
  388. p += dir;
  389. if ( !(*p == '/' && p[dir] == '*') )
  390. break;
  391. p += dir; p += dir;
  392. while ( !(*p == '*' && p[dir] == '/') ) {
  393. if ( *p == 0 )
  394. return p; /* multi-line comment?? */
  395. p += dir;
  396. }
  397. p += dir; p += dir;
  398. }
  399. return p;
  400. }
  401. /* Scan over a quoted string, in either direction. */
  402. char *
  403. scanstring(p, dir)
  404. char *p;
  405. int dir;
  406. {
  407. for (p += dir; ; p += dir)
  408. if (*p == '"' && p[-dir] != '\\')
  409. return p + dir;
  410. }
  411. /*
  412. * Write blanks over part of a string.
  413. * Don't overwrite end-of-line characters.
  414. */
  415. int
  416. writeblanks(start, end)
  417. char *start;
  418. char *end;
  419. { char *p;
  420. for ( p = start; p < end; p++ )
  421. if ( *p != '\r' && *p != '\n' )
  422. *p = ' ';
  423. return 0;
  424. }
  425. /*
  426. * Test whether the string in buf is a function definition.
  427. * The string may contain and/or end with a newline.
  428. * Return as follows:
  429. * 0 - definitely not a function definition;
  430. * 1 - definitely a function definition;
  431. * 2 - definitely a function prototype (NOT USED);
  432. * -1 - may be the beginning of a function definition,
  433. * append another line and look again.
  434. * The reason we don't attempt to convert function prototypes is that
  435. * Ghostscript's declaration-generating macros look too much like
  436. * prototypes, and confuse the algorithms.
  437. */
  438. int
  439. test1(buf)
  440. char *buf;
  441. { char *p = buf;
  442. char *bend;
  443. char *endfn;
  444. int contin;
  445. if ( !isidfirstchar(*p) )
  446. return 0; /* no name at left margin */
  447. bend = skipspace(ppdirbackward(buf + strlen(buf) - 1, buf), -1);
  448. switch ( *bend )
  449. {
  450. case ';': contin = 0 /*2*/; break;
  451. case ')': contin = 1; break;
  452. case '{': return 0; /* not a function */
  453. case '}': return 0; /* not a function */
  454. default: contin = -1;
  455. }
  456. while ( isidchar(*p) )
  457. p++;
  458. endfn = p;
  459. p = skipspace(p, 1);
  460. if ( *p++ != '(' )
  461. return 0; /* not a function */
  462. p = skipspace(p, 1);
  463. if ( *p == ')' )
  464. return 0; /* no parameters */
  465. /* Check that the apparent function name isn't a keyword. */
  466. /* We only need to check for keywords that could be followed */
  467. /* by a left parenthesis (which, unfortunately, is most of them). */
  468. { static char *words[] =
  469. { "asm", "auto", "case", "char", "const", "double",
  470. "extern", "float", "for", "if", "int", "long",
  471. "register", "return", "short", "signed", "sizeof",
  472. "static", "switch", "typedef", "unsigned",
  473. "void", "volatile", "while", 0
  474. };
  475. char **key = words;
  476. char *kp;
  477. unsigned len = endfn - buf;
  478. while ( (kp = *key) != 0 )
  479. { if ( strlen(kp) == len && !strncmp(kp, buf, len) )
  480. return 0; /* name is a keyword */
  481. key++;
  482. }
  483. }
  484. {
  485. char *id = p;
  486. int len;
  487. /*
  488. * Check for identifier1(identifier2) and not
  489. * identifier1(void), or identifier1(identifier2, xxxx).
  490. */
  491. while ( isidchar(*p) )
  492. p++;
  493. len = p - id;
  494. p = skipspace(p, 1);
  495. if (*p == ',' ||
  496. (*p == ')' && (len != 4 || strncmp(id, "void", 4)))
  497. )
  498. return 0; /* not a function */
  499. }
  500. /*
  501. * If the last significant character was a ), we need to count
  502. * parentheses, because it might be part of a formal parameter
  503. * that is a procedure.
  504. */
  505. if (contin > 0) {
  506. int level = 0;
  507. for (p = skipspace(buf, 1); *p; p = skipspace(p + 1, 1))
  508. level += (*p == '(' ? 1 : *p == ')' ? -1 : 0);
  509. if (level > 0)
  510. contin = -1;
  511. }
  512. return contin;
  513. }
  514. /* Convert a recognized function definition or header to K&R syntax. */
  515. int
  516. convert1(buf, out, header, convert_varargs)
  517. char *buf;
  518. FILE *out;
  519. int header; /* Boolean */
  520. int convert_varargs; /* Boolean */
  521. { char *endfn;
  522. char *p;
  523. /*
  524. * The breaks table contains pointers to the beginning and end
  525. * of each argument.
  526. */
  527. char **breaks;
  528. unsigned num_breaks = 2; /* for testing */
  529. char **btop;
  530. char **bp;
  531. char **ap;
  532. char *vararg = 0;
  533. /* Pre-ANSI implementations don't agree on whether strchr */
  534. /* is called strchr or index, so we open-code it here. */
  535. for ( endfn = buf; *(endfn++) != '('; )
  536. ;
  537. top: p = endfn;
  538. breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
  539. if ( breaks == NULL )
  540. { /* Couldn't allocate break table, give up */
  541. fprintf(stderr, "Unable to allocate break table!\n");
  542. fputs(buf, out);
  543. return -1;
  544. }
  545. btop = breaks + num_breaks * 2 - 2;
  546. bp = breaks;
  547. /* Parse the argument list */
  548. do
  549. { int level = 0;
  550. char *lp = NULL;
  551. char *rp = NULL;
  552. char *end = NULL;
  553. if ( bp >= btop )
  554. { /* Filled up break table. */
  555. /* Allocate a bigger one and start over. */
  556. free((char *)breaks);
  557. num_breaks <<= 1;
  558. goto top;
  559. }
  560. *bp++ = p;
  561. /* Find the end of the argument */
  562. for ( ; end == NULL; p++ )
  563. { switch(*p)
  564. {
  565. case ',':
  566. if ( !level ) end = p;
  567. break;
  568. case '(':
  569. if ( !level ) lp = p;
  570. level++;
  571. break;
  572. case ')':
  573. if ( --level < 0 ) end = p;
  574. else rp = p;
  575. break;
  576. case '/':
  577. if (p[1] == '*')
  578. p = skipspace(p, 1) - 1;
  579. break;
  580. case '"':
  581. p = scanstring(p, 1) - 1;
  582. break;
  583. default:
  584. ;
  585. }
  586. }
  587. /* Erase any embedded prototype parameters. */
  588. if ( lp && rp )
  589. writeblanks(lp + 1, rp);
  590. p--; /* back up over terminator */
  591. /* Find the name being declared. */
  592. /* This is complicated because of procedure and */
  593. /* array modifiers. */
  594. for ( ; ; )
  595. { p = skipspace(p - 1, -1);
  596. switch ( *p )
  597. {
  598. case ']': /* skip array dimension(s) */
  599. case ')': /* skip procedure args OR name */
  600. { int level = 1;
  601. while ( level )
  602. switch ( *--p )
  603. {
  604. case ']': case ')':
  605. level++;
  606. break;
  607. case '[': case '(':
  608. level--;
  609. break;
  610. case '/':
  611. if (p > buf && p[-1] == '*')
  612. p = skipspace(p, -1) + 1;
  613. break;
  614. case '"':
  615. p = scanstring(p, -1) + 1;
  616. break;
  617. default: ;
  618. }
  619. }
  620. if ( *p == '(' && *skipspace(p + 1, 1) == '*' )
  621. { /* We found the name being declared */
  622. while ( !isidfirstchar(*p) )
  623. p = skipspace(p, 1) + 1;
  624. goto found;
  625. }
  626. break;
  627. default:
  628. goto found;
  629. }
  630. }
  631. found: if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )
  632. { if ( convert_varargs )
  633. { *bp++ = "va_alist";
  634. vararg = p-2;
  635. }
  636. else
  637. { p++;
  638. if ( bp == breaks + 1 ) /* sole argument */
  639. writeblanks(breaks[0], p);
  640. else
  641. writeblanks(bp[-1] - 1, p);
  642. bp--;
  643. }
  644. }
  645. else
  646. { while ( isidchar(*p) ) p--;
  647. *bp++ = p+1;
  648. }
  649. p = end;
  650. }
  651. while ( *p++ == ',' );
  652. *bp = p;
  653. /* Make a special check for 'void' arglist */
  654. if ( bp == breaks+2 )
  655. { p = skipspace(breaks[0], 1);
  656. if ( !strncmp(p, "void", 4) )
  657. { p = skipspace(p+4, 1);
  658. if ( p == breaks[2] - 1 )
  659. { bp = breaks; /* yup, pretend arglist is empty */
  660. writeblanks(breaks[0], p + 1);
  661. }
  662. }
  663. }
  664. /* Put out the function name and left parenthesis. */
  665. p = buf;
  666. while ( p != endfn ) putc(*p, out), p++;
  667. /* Put out the declaration. */
  668. if ( header )
  669. { fputs(");", out);
  670. for ( p = breaks[0]; *p; p++ )
  671. if ( *p == '\r' || *p == '\n' )
  672. putc(*p, out);
  673. }
  674. else
  675. { for ( ap = breaks+1; ap < bp; ap += 2 )
  676. { p = *ap;
  677. while ( isidchar(*p) )
  678. putc(*p, out), p++;
  679. if ( ap < bp - 1 )
  680. fputs(", ", out);
  681. }
  682. fputs(") ", out);
  683. /* Put out the argument declarations */
  684. for ( ap = breaks+2; ap <= bp; ap += 2 )
  685. (*ap)[-1] = ';';
  686. if ( vararg != 0 )
  687. { *vararg = 0;
  688. fputs(breaks[0], out); /* any prior args */
  689. fputs("va_dcl", out); /* the final arg */
  690. fputs(bp[0], out);
  691. }
  692. else
  693. fputs(breaks[0], out);
  694. }
  695. free((char *)breaks);
  696. return 0;
  697. }