ansi2knr.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /* ansi2knr.c */
  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. [In the IJG distribution, the GPL appears below, not in a separate file.]
  14. Among other things, the copyright notice and this notice must be preserved
  15. on all 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. ---------- Here is the GNU GPL file COPYLEFT, referred to above ----------
  26. ----- These terms do NOT apply to the JPEG software itself; see README ------
  27. GHOSTSCRIPT GENERAL PUBLIC LICENSE
  28. (Clarified 11 Feb 1988)
  29. Copyright (C) 1988 Richard M. Stallman
  30. Everyone is permitted to copy and distribute verbatim copies of this
  31. license, but changing it is not allowed. You can also use this wording
  32. to make the terms for other programs.
  33. The license agreements of most software companies keep you at the
  34. mercy of those companies. By contrast, our general public license is
  35. intended to give everyone the right to share Ghostscript. To make sure
  36. that you get the rights we want you to have, we need to make
  37. restrictions that forbid anyone to deny you these rights or to ask you
  38. to surrender the rights. Hence this license agreement.
  39. Specifically, we want to make sure that you have the right to give
  40. away copies of Ghostscript, that you receive source code or else can get
  41. it if you want it, that you can change Ghostscript or use pieces of it
  42. in new free programs, and that you know you can do these things.
  43. To make sure that everyone has such rights, we have to forbid you to
  44. deprive anyone else of these rights. For example, if you distribute
  45. copies of Ghostscript, you must give the recipients all the rights that
  46. you have. You must make sure that they, too, receive or can get the
  47. source code. And you must tell them their rights.
  48. Also, for our own protection, we must make certain that everyone finds
  49. out that there is no warranty for Ghostscript. If Ghostscript is
  50. modified by someone else and passed on, we want its recipients to know
  51. that what they have is not what we distributed, so that any problems
  52. introduced by others will not reflect on our reputation.
  53. Therefore we (Richard M. Stallman and the Free Software Foundation,
  54. Inc.) make the following terms which say what you must do to be allowed
  55. to distribute or change Ghostscript.
  56. COPYING POLICIES
  57. 1. You may copy and distribute verbatim copies of Ghostscript source
  58. code as you receive it, in any medium, provided that you conspicuously
  59. and appropriately publish on each copy a valid copyright and license
  60. notice "Copyright (C) 1989 Aladdin Enterprises. All rights reserved.
  61. Distributed by Free Software Foundation, Inc." (or with whatever year is
  62. appropriate); keep intact the notices on all files that refer to this
  63. License Agreement and to the absence of any warranty; and give any other
  64. recipients of the Ghostscript program a copy of this License Agreement
  65. along with the program. You may charge a distribution fee for the
  66. physical act of transferring a copy.
  67. 2. You may modify your copy or copies of Ghostscript or any portion of
  68. it, and copy and distribute such modifications under the terms of
  69. Paragraph 1 above, provided that you also do the following:
  70. a) cause the modified files to carry prominent notices stating
  71. that you changed the files and the date of any change; and
  72. b) cause the whole of any work that you distribute or publish,
  73. that in whole or in part contains or is a derivative of Ghostscript
  74. or any part thereof, to be licensed at no charge to all third
  75. parties on terms identical to those contained in this License
  76. Agreement (except that you may choose to grant more extensive
  77. warranty protection to some or all third parties, at your option).
  78. c) You may charge a distribution fee for the physical act of
  79. transferring a copy, and you may at your option offer warranty
  80. protection in exchange for a fee.
  81. Mere aggregation of another unrelated program with this program (or its
  82. derivative) on a volume of a storage or distribution medium does not bring
  83. the other program under the scope of these terms.
  84. 3. You may copy and distribute Ghostscript (or a portion or derivative
  85. of it, under Paragraph 2) in object code or executable form under the
  86. terms of Paragraphs 1 and 2 above provided that you also do one of the
  87. following:
  88. a) accompany it with the complete corresponding machine-readable
  89. source code, which must be distributed under the terms of
  90. Paragraphs 1 and 2 above; or,
  91. b) accompany it with a written offer, valid for at least three
  92. years, to give any third party free (except for a nominal
  93. shipping charge) a complete machine-readable copy of the
  94. corresponding source code, to be distributed under the terms of
  95. Paragraphs 1 and 2 above; or,
  96. c) accompany it with the information you received as to where the
  97. corresponding source code may be obtained. (This alternative is
  98. allowed only for noncommercial distribution and only if you
  99. received the program in object code or executable form alone.)
  100. For an executable file, complete source code means all the source code for
  101. all modules it contains; but, as a special exception, it need not include
  102. source code for modules which are standard libraries that accompany the
  103. operating system on which the executable file runs.
  104. 4. You may not copy, sublicense, distribute or transfer Ghostscript
  105. except as expressly provided under this License Agreement. Any attempt
  106. otherwise to copy, sublicense, distribute or transfer Ghostscript is
  107. void and your rights to use the program under this License agreement
  108. shall be automatically terminated. However, parties who have received
  109. computer software programs from you with this License Agreement will not
  110. have their licenses terminated so long as such parties remain in full
  111. compliance.
  112. 5. If you wish to incorporate parts of Ghostscript into other free
  113. programs whose distribution conditions are different, write to the Free
  114. Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not
  115. yet worked out a simple rule that can be stated here, but we will often
  116. permit this. We will be guided by the two goals of preserving the free
  117. status of all derivatives of our free software and of promoting the
  118. sharing and reuse of software.
  119. Your comments and suggestions about our licensing policies and our
  120. software are welcome! Please contact the Free Software Foundation,
  121. Inc., 675 Mass Ave, Cambridge, MA 02139, or call (617) 876-3296.
  122. NO WARRANTY
  123. BECAUSE GHOSTSCRIPT IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  124. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  125. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC, RICHARD
  126. M. STALLMAN, ALADDIN ENTERPRISES, L. PETER DEUTSCH, AND/OR OTHER PARTIES
  127. PROVIDE GHOSTSCRIPT "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  128. EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  129. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
  130. ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF GHOSTSCRIPT IS WITH
  131. YOU. SHOULD GHOSTSCRIPT PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
  132. NECESSARY SERVICING, REPAIR OR CORRECTION.
  133. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  134. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., L. PETER DEUTSCH, ALADDIN
  135. ENTERPRISES, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND REDISTRIBUTE
  136. GHOSTSCRIPT AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING
  137. ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, INCIDENTAL OR
  138. CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
  139. (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
  140. INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A FAILURE OF THE
  141. PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) GHOSTSCRIPT, EVEN IF YOU
  142. HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM
  143. BY ANY OTHER PARTY.
  144. -------------------- End of file COPYLEFT ------------------------------
  145. */
  146. /*
  147. * Usage:
  148. ansi2knr input_file [output_file]
  149. * If no output_file is supplied, output goes to stdout.
  150. * There are no error messages.
  151. *
  152. * ansi2knr recognizes function definitions by seeing a non-keyword
  153. * identifier at the left margin, followed by a left parenthesis,
  154. * with a right parenthesis as the last character on the line,
  155. * and with a left brace as the first token on the following line
  156. * (ignoring possible intervening comments).
  157. * It will recognize a multi-line header provided that no intervening
  158. * line ends with a left or right brace or a semicolon.
  159. * These algorithms ignore whitespace and comments, except that
  160. * the function name must be the first thing on the line.
  161. * The following constructs will confuse it:
  162. * - Any other construct that starts at the left margin and
  163. * follows the above syntax (such as a macro or function call).
  164. * - Some macros that tinker with the syntax of the function header.
  165. */
  166. /*
  167. * The original and principal author of ansi2knr is L. Peter Deutsch
  168. * <ghost@aladdin.com>. Other authors are noted in the change history
  169. * that follows (in reverse chronological order):
  170. lpd 96-01-21 added code to cope with not HAVE_CONFIG_H and with
  171. compilers that don't understand void, as suggested by
  172. Tom Lane
  173. lpd 96-01-15 changed to require that the first non-comment token
  174. on the line following a function header be a left brace,
  175. to reduce sensitivity to macros, as suggested by Tom Lane
  176. <tgl@sss.pgh.pa.us>
  177. lpd 95-06-22 removed #ifndefs whose sole purpose was to define
  178. undefined preprocessor symbols as 0; changed all #ifdefs
  179. for configuration symbols to #ifs
  180. lpd 95-04-05 changed copyright notice to make it clear that
  181. including ansi2knr in a program does not bring the entire
  182. program under the GPL
  183. lpd 94-12-18 added conditionals for systems where ctype macros
  184. don't handle 8-bit characters properly, suggested by
  185. Francois Pinard <pinard@iro.umontreal.ca>;
  186. removed --varargs switch (this is now the default)
  187. lpd 94-10-10 removed CONFIG_BROKETS conditional
  188. lpd 94-07-16 added some conditionals to help GNU `configure',
  189. suggested by Francois Pinard <pinard@iro.umontreal.ca>;
  190. properly erase prototype args in function parameters,
  191. contributed by Jim Avera <jima@netcom.com>;
  192. correct error in writeblanks (it shouldn't erase EOLs)
  193. lpd 89-xx-xx original version
  194. */
  195. /* Most of the conditionals here are to make ansi2knr work with */
  196. /* or without the GNU configure machinery. */
  197. #if HAVE_CONFIG_H
  198. # include <config.h>
  199. #endif
  200. #include <stdio.h>
  201. #include <ctype.h>
  202. #if HAVE_CONFIG_H
  203. /*
  204. For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
  205. This will define HAVE_CONFIG_H and so, activate the following lines.
  206. */
  207. # if STDC_HEADERS || HAVE_STRING_H
  208. # include <string.h>
  209. # else
  210. # include <strings.h>
  211. # endif
  212. #else /* not HAVE_CONFIG_H */
  213. /* Otherwise do it the hard way */
  214. # ifdef BSD
  215. # include <strings.h>
  216. # else
  217. # ifdef VMS
  218. extern int strlen(), strncmp();
  219. # else
  220. # include <string.h>
  221. # endif
  222. # endif
  223. #endif /* not HAVE_CONFIG_H */
  224. #if STDC_HEADERS
  225. # include <stdlib.h>
  226. #else
  227. /*
  228. malloc and free should be declared in stdlib.h,
  229. but if you've got a K&R compiler, they probably aren't.
  230. */
  231. # ifdef MSDOS
  232. # include <malloc.h>
  233. # else
  234. # ifdef VMS
  235. extern char *malloc();
  236. extern void free();
  237. # else
  238. extern char *malloc();
  239. extern int free();
  240. # endif
  241. # endif
  242. #endif
  243. /*
  244. * The ctype macros don't always handle 8-bit characters correctly.
  245. * Compensate for this here.
  246. */
  247. #ifdef isascii
  248. # undef HAVE_ISASCII /* just in case */
  249. # define HAVE_ISASCII 1
  250. #else
  251. #endif
  252. #if STDC_HEADERS || !HAVE_ISASCII
  253. # define is_ascii(c) 1
  254. #else
  255. # define is_ascii(c) isascii(c)
  256. #endif
  257. #define is_space(c) (is_ascii(c) && isspace(c))
  258. #define is_alpha(c) (is_ascii(c) && isalpha(c))
  259. #define is_alnum(c) (is_ascii(c) && isalnum(c))
  260. /* Scanning macros */
  261. #define isidchar(ch) (is_alnum(ch) || (ch) == '_')
  262. #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
  263. /* Forward references */
  264. char *skipspace();
  265. int writeblanks();
  266. int test1();
  267. int convert1();
  268. /* The main program */
  269. int
  270. main(argc, argv)
  271. int argc;
  272. char *argv[];
  273. { FILE *in, *out;
  274. #define bufsize 5000 /* arbitrary size */
  275. char *buf;
  276. char *line;
  277. char *more;
  278. /*
  279. * In previous versions, ansi2knr recognized a --varargs switch.
  280. * If this switch was supplied, ansi2knr would attempt to convert
  281. * a ... argument to va_alist and va_dcl; if this switch was not
  282. * supplied, ansi2knr would simply drop any such arguments.
  283. * Now, ansi2knr always does this conversion, and we only
  284. * check for this switch for backward compatibility.
  285. */
  286. int convert_varargs = 1;
  287. if ( argc > 1 && argv[1][0] == '-' )
  288. { if ( !strcmp(argv[1], "--varargs") )
  289. { convert_varargs = 1;
  290. argc--;
  291. argv++;
  292. }
  293. else
  294. { fprintf(stderr, "Unrecognized switch: %s\n", argv[1]);
  295. exit(1);
  296. }
  297. }
  298. switch ( argc )
  299. {
  300. default:
  301. printf("Usage: ansi2knr input_file [output_file]\n");
  302. exit(0);
  303. case 2:
  304. out = stdout;
  305. break;
  306. case 3:
  307. out = fopen(argv[2], "w");
  308. if ( out == NULL )
  309. { fprintf(stderr, "Cannot open output file %s\n", argv[2]);
  310. exit(1);
  311. }
  312. }
  313. in = fopen(argv[1], "r");
  314. if ( in == NULL )
  315. { fprintf(stderr, "Cannot open input file %s\n", argv[1]);
  316. exit(1);
  317. }
  318. fprintf(out, "#line 1 \"%s\"\n", argv[1]);
  319. buf = malloc(bufsize);
  320. line = buf;
  321. while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL )
  322. {
  323. test: line += strlen(line);
  324. switch ( test1(buf) )
  325. {
  326. case 2: /* a function header */
  327. convert1(buf, out, 1, convert_varargs);
  328. break;
  329. case 1: /* a function */
  330. /* Check for a { at the start of the next line. */
  331. more = ++line;
  332. f: if ( line >= buf + (bufsize - 1) ) /* overflow check */
  333. goto wl;
  334. if ( fgets(line, (unsigned)(buf + bufsize - line), in) == NULL )
  335. goto wl;
  336. switch ( *skipspace(more, 1) )
  337. {
  338. case '{':
  339. /* Definitely a function header. */
  340. convert1(buf, out, 0, convert_varargs);
  341. fputs(more, out);
  342. break;
  343. case 0:
  344. /* The next line was blank or a comment: */
  345. /* keep scanning for a non-comment. */
  346. line += strlen(line);
  347. goto f;
  348. default:
  349. /* buf isn't a function header, but */
  350. /* more might be. */
  351. fputs(buf, out);
  352. strcpy(buf, more);
  353. line = buf;
  354. goto test;
  355. }
  356. break;
  357. case -1: /* maybe the start of a function */
  358. if ( line != buf + (bufsize - 1) ) /* overflow check */
  359. continue;
  360. /* falls through */
  361. default: /* not a function */
  362. wl: fputs(buf, out);
  363. break;
  364. }
  365. line = buf;
  366. }
  367. if ( line != buf )
  368. fputs(buf, out);
  369. free(buf);
  370. fclose(out);
  371. fclose(in);
  372. return 0;
  373. }
  374. /* Skip over space and comments, in either direction. */
  375. char *
  376. skipspace(p, dir)
  377. register char *p;
  378. register int dir; /* 1 for forward, -1 for backward */
  379. { for ( ; ; )
  380. { while ( is_space(*p) )
  381. p += dir;
  382. if ( !(*p == '/' && p[dir] == '*') )
  383. break;
  384. p += dir; p += dir;
  385. while ( !(*p == '*' && p[dir] == '/') )
  386. { if ( *p == 0 )
  387. return p; /* multi-line comment?? */
  388. p += dir;
  389. }
  390. p += dir; p += dir;
  391. }
  392. return p;
  393. }
  394. /*
  395. * Write blanks over part of a string.
  396. * Don't overwrite end-of-line characters.
  397. */
  398. int
  399. writeblanks(start, end)
  400. char *start;
  401. char *end;
  402. { char *p;
  403. for ( p = start; p < end; p++ )
  404. if ( *p != '\r' && *p != '\n' )
  405. *p = ' ';
  406. return 0;
  407. }
  408. /*
  409. * Test whether the string in buf is a function definition.
  410. * The string may contain and/or end with a newline.
  411. * Return as follows:
  412. * 0 - definitely not a function definition;
  413. * 1 - definitely a function definition;
  414. * 2 - definitely a function prototype (NOT USED);
  415. * -1 - may be the beginning of a function definition,
  416. * append another line and look again.
  417. * The reason we don't attempt to convert function prototypes is that
  418. * Ghostscript's declaration-generating macros look too much like
  419. * prototypes, and confuse the algorithms.
  420. */
  421. int
  422. test1(buf)
  423. char *buf;
  424. { register char *p = buf;
  425. char *bend;
  426. char *endfn;
  427. int contin;
  428. if ( !isidfirstchar(*p) )
  429. return 0; /* no name at left margin */
  430. bend = skipspace(buf + strlen(buf) - 1, -1);
  431. switch ( *bend )
  432. {
  433. case ';': contin = 0 /*2*/; break;
  434. case ')': contin = 1; break;
  435. case '{': return 0; /* not a function */
  436. case '}': return 0; /* not a function */
  437. default: contin = -1;
  438. }
  439. while ( isidchar(*p) )
  440. p++;
  441. endfn = p;
  442. p = skipspace(p, 1);
  443. if ( *p++ != '(' )
  444. return 0; /* not a function */
  445. p = skipspace(p, 1);
  446. if ( *p == ')' )
  447. return 0; /* no parameters */
  448. /* Check that the apparent function name isn't a keyword. */
  449. /* We only need to check for keywords that could be followed */
  450. /* by a left parenthesis (which, unfortunately, is most of them). */
  451. { static char *words[] =
  452. { "asm", "auto", "case", "char", "const", "double",
  453. "extern", "float", "for", "if", "int", "long",
  454. "register", "return", "short", "signed", "sizeof",
  455. "static", "switch", "typedef", "unsigned",
  456. "void", "volatile", "while", 0
  457. };
  458. char **key = words;
  459. char *kp;
  460. int len = endfn - buf;
  461. while ( (kp = *key) != 0 )
  462. { if ( strlen(kp) == len && !strncmp(kp, buf, len) )
  463. return 0; /* name is a keyword */
  464. key++;
  465. }
  466. }
  467. return contin;
  468. }
  469. /* Convert a recognized function definition or header to K&R syntax. */
  470. int
  471. convert1(buf, out, header, convert_varargs)
  472. char *buf;
  473. FILE *out;
  474. int header; /* Boolean */
  475. int convert_varargs; /* Boolean */
  476. { char *endfn;
  477. register char *p;
  478. char **breaks;
  479. unsigned num_breaks = 2; /* for testing */
  480. char **btop;
  481. char **bp;
  482. char **ap;
  483. char *vararg = 0;
  484. /* Pre-ANSI implementations don't agree on whether strchr */
  485. /* is called strchr or index, so we open-code it here. */
  486. for ( endfn = buf; *(endfn++) != '('; )
  487. ;
  488. top: p = endfn;
  489. breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
  490. if ( breaks == 0 )
  491. { /* Couldn't allocate break table, give up */
  492. fprintf(stderr, "Unable to allocate break table!\n");
  493. fputs(buf, out);
  494. return -1;
  495. }
  496. btop = breaks + num_breaks * 2 - 2;
  497. bp = breaks;
  498. /* Parse the argument list */
  499. do
  500. { int level = 0;
  501. char *lp = NULL;
  502. char *rp;
  503. char *end = NULL;
  504. if ( bp >= btop )
  505. { /* Filled up break table. */
  506. /* Allocate a bigger one and start over. */
  507. free((char *)breaks);
  508. num_breaks <<= 1;
  509. goto top;
  510. }
  511. *bp++ = p;
  512. /* Find the end of the argument */
  513. for ( ; end == NULL; p++ )
  514. { switch(*p)
  515. {
  516. case ',':
  517. if ( !level ) end = p;
  518. break;
  519. case '(':
  520. if ( !level ) lp = p;
  521. level++;
  522. break;
  523. case ')':
  524. if ( --level < 0 ) end = p;
  525. else rp = p;
  526. break;
  527. case '/':
  528. p = skipspace(p, 1) - 1;
  529. break;
  530. default:
  531. ;
  532. }
  533. }
  534. /* Erase any embedded prototype parameters. */
  535. if ( lp )
  536. writeblanks(lp + 1, rp);
  537. p--; /* back up over terminator */
  538. /* Find the name being declared. */
  539. /* This is complicated because of procedure and */
  540. /* array modifiers. */
  541. for ( ; ; )
  542. { p = skipspace(p - 1, -1);
  543. switch ( *p )
  544. {
  545. case ']': /* skip array dimension(s) */
  546. case ')': /* skip procedure args OR name */
  547. { int level = 1;
  548. while ( level )
  549. switch ( *--p )
  550. {
  551. case ']': case ')': level++; break;
  552. case '[': case '(': level--; break;
  553. case '/': p = skipspace(p, -1) + 1; break;
  554. default: ;
  555. }
  556. }
  557. if ( *p == '(' && *skipspace(p + 1, 1) == '*' )
  558. { /* We found the name being declared */
  559. while ( !isidfirstchar(*p) )
  560. p = skipspace(p, 1) + 1;
  561. goto found;
  562. }
  563. break;
  564. default:
  565. goto found;
  566. }
  567. }
  568. found: if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )
  569. { if ( convert_varargs )
  570. { *bp++ = "va_alist";
  571. vararg = p-2;
  572. }
  573. else
  574. { p++;
  575. if ( bp == breaks + 1 ) /* sole argument */
  576. writeblanks(breaks[0], p);
  577. else
  578. writeblanks(bp[-1] - 1, p);
  579. bp--;
  580. }
  581. }
  582. else
  583. { while ( isidchar(*p) ) p--;
  584. *bp++ = p+1;
  585. }
  586. p = end;
  587. }
  588. while ( *p++ == ',' );
  589. *bp = p;
  590. /* Make a special check for 'void' arglist */
  591. if ( bp == breaks+2 )
  592. { p = skipspace(breaks[0], 1);
  593. if ( !strncmp(p, "void", 4) )
  594. { p = skipspace(p+4, 1);
  595. if ( p == breaks[2] - 1 )
  596. { bp = breaks; /* yup, pretend arglist is empty */
  597. writeblanks(breaks[0], p + 1);
  598. }
  599. }
  600. }
  601. /* Put out the function name and left parenthesis. */
  602. p = buf;
  603. while ( p != endfn ) putc(*p, out), p++;
  604. /* Put out the declaration. */
  605. if ( header )
  606. { fputs(");", out);
  607. for ( p = breaks[0]; *p; p++ )
  608. if ( *p == '\r' || *p == '\n' )
  609. putc(*p, out);
  610. }
  611. else
  612. { for ( ap = breaks+1; ap < bp; ap += 2 )
  613. { p = *ap;
  614. while ( isidchar(*p) )
  615. putc(*p, out), p++;
  616. if ( ap < bp - 1 )
  617. fputs(", ", out);
  618. }
  619. fputs(") ", out);
  620. /* Put out the argument declarations */
  621. for ( ap = breaks+2; ap <= bp; ap += 2 )
  622. (*ap)[-1] = ';';
  623. if ( vararg != 0 )
  624. { *vararg = 0;
  625. fputs(breaks[0], out); /* any prior args */
  626. fputs("va_dcl", out); /* the final arg */
  627. fputs(bp[0], out);
  628. }
  629. else
  630. fputs(breaks[0], out);
  631. }
  632. free((char *)breaks);
  633. return 0;
  634. }