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