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