ansi2knr.c 20 KB

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