Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/vncdll/winvnc/libjpeg/ansi2knr.c
Views: 11784
/* ansi2knr.c */1/* Convert ANSI C function definitions to K&R ("traditional C") syntax */23/*4ansi2knr is distributed in the hope that it will be useful, but WITHOUT ANY5WARRANTY. No author or distributor accepts responsibility to anyone for the6consequences of using it or for whether it serves any particular purpose or7works at all, unless he says so in writing. Refer to the GNU General Public8License (the "GPL") for full details.910Everyone is granted permission to copy, modify and redistribute ansi2knr,11but only under the conditions described in the GPL. A copy of this license12is supposed to have been given to you along with ansi2knr so you can know13your rights and responsibilities. It should be in a file named COPYLEFT.14[In the IJG distribution, the GPL appears below, not in a separate file.]15Among other things, the copyright notice and this notice must be preserved16on all copies.1718We explicitly state here what we believe is already implied by the GPL: if19the ansi2knr program is distributed as a separate set of sources and a20separate executable file which are aggregated on a storage medium together21with another program, this in itself does not bring the other program under22the GPL, nor does the mere fact that such a program or the procedures for23constructing it invoke the ansi2knr executable bring any other part of the24program under the GPL.25*/2627/*28---------- Here is the GNU GPL file COPYLEFT, referred to above ----------29----- These terms do NOT apply to the JPEG software itself; see README ------3031GHOSTSCRIPT GENERAL PUBLIC LICENSE32(Clarified 11 Feb 1988)3334Copyright (C) 1988 Richard M. Stallman35Everyone is permitted to copy and distribute verbatim copies of this36license, but changing it is not allowed. You can also use this wording37to make the terms for other programs.3839The license agreements of most software companies keep you at the40mercy of those companies. By contrast, our general public license is41intended to give everyone the right to share Ghostscript. To make sure42that you get the rights we want you to have, we need to make43restrictions that forbid anyone to deny you these rights or to ask you44to surrender the rights. Hence this license agreement.4546Specifically, we want to make sure that you have the right to give47away copies of Ghostscript, that you receive source code or else can get48it if you want it, that you can change Ghostscript or use pieces of it49in new free programs, and that you know you can do these things.5051To make sure that everyone has such rights, we have to forbid you to52deprive anyone else of these rights. For example, if you distribute53copies of Ghostscript, you must give the recipients all the rights that54you have. You must make sure that they, too, receive or can get the55source code. And you must tell them their rights.5657Also, for our own protection, we must make certain that everyone finds58out that there is no warranty for Ghostscript. If Ghostscript is59modified by someone else and passed on, we want its recipients to know60that what they have is not what we distributed, so that any problems61introduced by others will not reflect on our reputation.6263Therefore we (Richard M. Stallman and the Free Software Foundation,64Inc.) make the following terms which say what you must do to be allowed65to distribute or change Ghostscript.666768COPYING POLICIES69701. You may copy and distribute verbatim copies of Ghostscript source71code as you receive it, in any medium, provided that you conspicuously72and appropriately publish on each copy a valid copyright and license73notice "Copyright (C) 1989 Aladdin Enterprises. All rights reserved.74Distributed by Free Software Foundation, Inc." (or with whatever year is75appropriate); keep intact the notices on all files that refer to this76License Agreement and to the absence of any warranty; and give any other77recipients of the Ghostscript program a copy of this License Agreement78along with the program. You may charge a distribution fee for the79physical act of transferring a copy.80812. You may modify your copy or copies of Ghostscript or any portion of82it, and copy and distribute such modifications under the terms of83Paragraph 1 above, provided that you also do the following:8485a) cause the modified files to carry prominent notices stating86that you changed the files and the date of any change; and8788b) cause the whole of any work that you distribute or publish,89that in whole or in part contains or is a derivative of Ghostscript90or any part thereof, to be licensed at no charge to all third91parties on terms identical to those contained in this License92Agreement (except that you may choose to grant more extensive93warranty protection to some or all third parties, at your option).9495c) You may charge a distribution fee for the physical act of96transferring a copy, and you may at your option offer warranty97protection in exchange for a fee.9899Mere aggregation of another unrelated program with this program (or its100derivative) on a volume of a storage or distribution medium does not bring101the other program under the scope of these terms.1021033. You may copy and distribute Ghostscript (or a portion or derivative104of it, under Paragraph 2) in object code or executable form under the105terms of Paragraphs 1 and 2 above provided that you also do one of the106following:107108a) accompany it with the complete corresponding machine-readable109source code, which must be distributed under the terms of110Paragraphs 1 and 2 above; or,111112b) accompany it with a written offer, valid for at least three113years, to give any third party free (except for a nominal114shipping charge) a complete machine-readable copy of the115corresponding source code, to be distributed under the terms of116Paragraphs 1 and 2 above; or,117118c) accompany it with the information you received as to where the119corresponding source code may be obtained. (This alternative is120allowed only for noncommercial distribution and only if you121received the program in object code or executable form alone.)122123For an executable file, complete source code means all the source code for124all modules it contains; but, as a special exception, it need not include125source code for modules which are standard libraries that accompany the126operating system on which the executable file runs.1271284. You may not copy, sublicense, distribute or transfer Ghostscript129except as expressly provided under this License Agreement. Any attempt130otherwise to copy, sublicense, distribute or transfer Ghostscript is131void and your rights to use the program under this License agreement132shall be automatically terminated. However, parties who have received133computer software programs from you with this License Agreement will not134have their licenses terminated so long as such parties remain in full135compliance.1361375. If you wish to incorporate parts of Ghostscript into other free138programs whose distribution conditions are different, write to the Free139Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not140yet worked out a simple rule that can be stated here, but we will often141permit this. We will be guided by the two goals of preserving the free142status of all derivatives of our free software and of promoting the143sharing and reuse of software.144145Your comments and suggestions about our licensing policies and our146software are welcome! Please contact the Free Software Foundation,147Inc., 675 Mass Ave, Cambridge, MA 02139, or call (617) 876-3296.148149NO WARRANTY150151BECAUSE GHOSTSCRIPT IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY152NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT153WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC, RICHARD154M. STALLMAN, ALADDIN ENTERPRISES, L. PETER DEUTSCH, AND/OR OTHER PARTIES155PROVIDE GHOSTSCRIPT "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER156EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED157WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE158ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF GHOSTSCRIPT IS WITH159YOU. SHOULD GHOSTSCRIPT PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL160NECESSARY SERVICING, REPAIR OR CORRECTION.161162IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.163STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., L. PETER DEUTSCH, ALADDIN164ENTERPRISES, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND REDISTRIBUTE165GHOSTSCRIPT AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING166ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, INCIDENTAL OR167CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE168(INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED169INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A FAILURE OF THE170PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) GHOSTSCRIPT, EVEN IF YOU171HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM172BY ANY OTHER PARTY.173174-------------------- End of file COPYLEFT ------------------------------175*/176177/*178* Usage:179ansi2knr input_file [output_file]180* If no output_file is supplied, output goes to stdout.181* There are no error messages.182*183* ansi2knr recognizes function definitions by seeing a non-keyword184* identifier at the left margin, followed by a left parenthesis,185* with a right parenthesis as the last character on the line,186* and with a left brace as the first token on the following line187* (ignoring possible intervening comments).188* It will recognize a multi-line header provided that no intervening189* line ends with a left or right brace or a semicolon.190* These algorithms ignore whitespace and comments, except that191* the function name must be the first thing on the line.192* The following constructs will confuse it:193* - Any other construct that starts at the left margin and194* follows the above syntax (such as a macro or function call).195* - Some macros that tinker with the syntax of the function header.196*/197198/*199* The original and principal author of ansi2knr is L. Peter Deutsch200* <[email protected]>. Other authors are noted in the change history201* that follows (in reverse chronological order):202lpd 96-01-21 added code to cope with not HAVE_CONFIG_H and with203compilers that don't understand void, as suggested by204Tom Lane205lpd 96-01-15 changed to require that the first non-comment token206on the line following a function header be a left brace,207to reduce sensitivity to macros, as suggested by Tom Lane208<[email protected]>209lpd 95-06-22 removed #ifndefs whose sole purpose was to define210undefined preprocessor symbols as 0; changed all #ifdefs211for configuration symbols to #ifs212lpd 95-04-05 changed copyright notice to make it clear that213including ansi2knr in a program does not bring the entire214program under the GPL215lpd 94-12-18 added conditionals for systems where ctype macros216don't handle 8-bit characters properly, suggested by217Francois Pinard <[email protected]>;218removed --varargs switch (this is now the default)219lpd 94-10-10 removed CONFIG_BROKETS conditional220lpd 94-07-16 added some conditionals to help GNU `configure',221suggested by Francois Pinard <[email protected]>;222properly erase prototype args in function parameters,223contributed by Jim Avera <[email protected]>;224correct error in writeblanks (it shouldn't erase EOLs)225lpd 89-xx-xx original version226*/227228/* Most of the conditionals here are to make ansi2knr work with */229/* or without the GNU configure machinery. */230231#if HAVE_CONFIG_H232# include <config.h>233#endif234235#include <stdio.h>236#include <ctype.h>237238#if HAVE_CONFIG_H239240/*241For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).242This will define HAVE_CONFIG_H and so, activate the following lines.243*/244245# if STDC_HEADERS || HAVE_STRING_H246# include <string.h>247# else248# include <strings.h>249# endif250251#else /* not HAVE_CONFIG_H */252253/* Otherwise do it the hard way */254255# ifdef BSD256# include <strings.h>257# else258# ifdef VMS259extern int strlen(), strncmp();260# else261# include <string.h>262# endif263# endif264265#endif /* not HAVE_CONFIG_H */266267#if STDC_HEADERS268# include <stdlib.h>269#else270/*271malloc and free should be declared in stdlib.h,272but if you've got a K&R compiler, they probably aren't.273*/274# ifdef MSDOS275# include <malloc.h>276# else277# ifdef VMS278extern char *malloc();279extern void free();280# else281extern char *malloc();282extern int free();283# endif284# endif285286#endif287288/*289* The ctype macros don't always handle 8-bit characters correctly.290* Compensate for this here.291*/292#ifdef isascii293# undef HAVE_ISASCII /* just in case */294# define HAVE_ISASCII 1295#else296#endif297#if STDC_HEADERS || !HAVE_ISASCII298# define is_ascii(c) 1299#else300# define is_ascii(c) isascii(c)301#endif302303#define is_space(c) (is_ascii(c) && isspace(c))304#define is_alpha(c) (is_ascii(c) && isalpha(c))305#define is_alnum(c) (is_ascii(c) && isalnum(c))306307/* Scanning macros */308#define isidchar(ch) (is_alnum(ch) || (ch) == '_')309#define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')310311/* Forward references */312char *skipspace();313int writeblanks();314int test1();315int convert1();316317/* The main program */318int319main(argc, argv)320int argc;321char *argv[];322{ FILE *in, *out;323#define bufsize 5000 /* arbitrary size */324char *buf;325char *line;326char *more;327/*328* In previous versions, ansi2knr recognized a --varargs switch.329* If this switch was supplied, ansi2knr would attempt to convert330* a ... argument to va_alist and va_dcl; if this switch was not331* supplied, ansi2knr would simply drop any such arguments.332* Now, ansi2knr always does this conversion, and we only333* check for this switch for backward compatibility.334*/335int convert_varargs = 1;336337if ( argc > 1 && argv[1][0] == '-' )338{ if ( !strcmp(argv[1], "--varargs") )339{ convert_varargs = 1;340argc--;341argv++;342}343else344{ fprintf(stderr, "Unrecognized switch: %s\n", argv[1]);345exit(1);346}347}348switch ( argc )349{350default:351printf("Usage: ansi2knr input_file [output_file]\n");352exit(0);353case 2:354out = stdout;355break;356case 3:357out = fopen(argv[2], "w");358if ( out == NULL )359{ fprintf(stderr, "Cannot open output file %s\n", argv[2]);360exit(1);361}362}363in = fopen(argv[1], "r");364if ( in == NULL )365{ fprintf(stderr, "Cannot open input file %s\n", argv[1]);366exit(1);367}368fprintf(out, "#line 1 \"%s\"\n", argv[1]);369buf = malloc(bufsize);370line = buf;371while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL )372{373test: line += strlen(line);374switch ( test1(buf) )375{376case 2: /* a function header */377convert1(buf, out, 1, convert_varargs);378break;379case 1: /* a function */380/* Check for a { at the start of the next line. */381more = ++line;382f: if ( line >= buf + (bufsize - 1) ) /* overflow check */383goto wl;384if ( fgets(line, (unsigned)(buf + bufsize - line), in) == NULL )385goto wl;386switch ( *skipspace(more, 1) )387{388case '{':389/* Definitely a function header. */390convert1(buf, out, 0, convert_varargs);391fputs(more, out);392break;393case 0:394/* The next line was blank or a comment: */395/* keep scanning for a non-comment. */396line += strlen(line);397goto f;398default:399/* buf isn't a function header, but */400/* more might be. */401fputs(buf, out);402strcpy(buf, more);403line = buf;404goto test;405}406break;407case -1: /* maybe the start of a function */408if ( line != buf + (bufsize - 1) ) /* overflow check */409continue;410/* falls through */411default: /* not a function */412wl: fputs(buf, out);413break;414}415line = buf;416}417if ( line != buf )418fputs(buf, out);419free(buf);420fclose(out);421fclose(in);422return 0;423}424425/* Skip over space and comments, in either direction. */426char *427skipspace(p, dir)428register char *p;429register int dir; /* 1 for forward, -1 for backward */430{ for ( ; ; )431{ while ( is_space(*p) )432p += dir;433if ( !(*p == '/' && p[dir] == '*') )434break;435p += dir; p += dir;436while ( !(*p == '*' && p[dir] == '/') )437{ if ( *p == 0 )438return p; /* multi-line comment?? */439p += dir;440}441p += dir; p += dir;442}443return p;444}445446/*447* Write blanks over part of a string.448* Don't overwrite end-of-line characters.449*/450int451writeblanks(start, end)452char *start;453char *end;454{ char *p;455for ( p = start; p < end; p++ )456if ( *p != '\r' && *p != '\n' )457*p = ' ';458return 0;459}460461/*462* Test whether the string in buf is a function definition.463* The string may contain and/or end with a newline.464* Return as follows:465* 0 - definitely not a function definition;466* 1 - definitely a function definition;467* 2 - definitely a function prototype (NOT USED);468* -1 - may be the beginning of a function definition,469* append another line and look again.470* The reason we don't attempt to convert function prototypes is that471* Ghostscript's declaration-generating macros look too much like472* prototypes, and confuse the algorithms.473*/474int475test1(buf)476char *buf;477{ register char *p = buf;478char *bend;479char *endfn;480int contin;481482if ( !isidfirstchar(*p) )483return 0; /* no name at left margin */484bend = skipspace(buf + strlen(buf) - 1, -1);485switch ( *bend )486{487case ';': contin = 0 /*2*/; break;488case ')': contin = 1; break;489case '{': return 0; /* not a function */490case '}': return 0; /* not a function */491default: contin = -1;492}493while ( isidchar(*p) )494p++;495endfn = p;496p = skipspace(p, 1);497if ( *p++ != '(' )498return 0; /* not a function */499p = skipspace(p, 1);500if ( *p == ')' )501return 0; /* no parameters */502/* Check that the apparent function name isn't a keyword. */503/* We only need to check for keywords that could be followed */504/* by a left parenthesis (which, unfortunately, is most of them). */505{ static char *words[] =506{ "asm", "auto", "case", "char", "const", "double",507"extern", "float", "for", "if", "int", "long",508"register", "return", "short", "signed", "sizeof",509"static", "switch", "typedef", "unsigned",510"void", "volatile", "while", 0511};512char **key = words;513char *kp;514int len = endfn - buf;515516while ( (kp = *key) != 0 )517{ if ( strlen(kp) == len && !strncmp(kp, buf, len) )518return 0; /* name is a keyword */519key++;520}521}522return contin;523}524525/* Convert a recognized function definition or header to K&R syntax. */526int527convert1(buf, out, header, convert_varargs)528char *buf;529FILE *out;530int header; /* Boolean */531int convert_varargs; /* Boolean */532{ char *endfn;533register char *p;534char **breaks;535unsigned num_breaks = 2; /* for testing */536char **btop;537char **bp;538char **ap;539char *vararg = 0;540541/* Pre-ANSI implementations don't agree on whether strchr */542/* is called strchr or index, so we open-code it here. */543for ( endfn = buf; *(endfn++) != '('; )544;545top: p = endfn;546breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);547if ( breaks == 0 )548{ /* Couldn't allocate break table, give up */549fprintf(stderr, "Unable to allocate break table!\n");550fputs(buf, out);551return -1;552}553btop = breaks + num_breaks * 2 - 2;554bp = breaks;555/* Parse the argument list */556do557{ int level = 0;558char *lp = NULL;559char *rp;560char *end = NULL;561562if ( bp >= btop )563{ /* Filled up break table. */564/* Allocate a bigger one and start over. */565free((char *)breaks);566num_breaks <<= 1;567goto top;568}569*bp++ = p;570/* Find the end of the argument */571for ( ; end == NULL; p++ )572{ switch(*p)573{574case ',':575if ( !level ) end = p;576break;577case '(':578if ( !level ) lp = p;579level++;580break;581case ')':582if ( --level < 0 ) end = p;583else rp = p;584break;585case '/':586p = skipspace(p, 1) - 1;587break;588default:589;590}591}592/* Erase any embedded prototype parameters. */593if ( lp )594writeblanks(lp + 1, rp);595p--; /* back up over terminator */596/* Find the name being declared. */597/* This is complicated because of procedure and */598/* array modifiers. */599for ( ; ; )600{ p = skipspace(p - 1, -1);601switch ( *p )602{603case ']': /* skip array dimension(s) */604case ')': /* skip procedure args OR name */605{ int level = 1;606while ( level )607switch ( *--p )608{609case ']': case ')': level++; break;610case '[': case '(': level--; break;611case '/': p = skipspace(p, -1) + 1; break;612default: ;613}614}615if ( *p == '(' && *skipspace(p + 1, 1) == '*' )616{ /* We found the name being declared */617while ( !isidfirstchar(*p) )618p = skipspace(p, 1) + 1;619goto found;620}621break;622default:623goto found;624}625}626found: if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )627{ if ( convert_varargs )628{ *bp++ = "va_alist";629vararg = p-2;630}631else632{ p++;633if ( bp == breaks + 1 ) /* sole argument */634writeblanks(breaks[0], p);635else636writeblanks(bp[-1] - 1, p);637bp--;638}639}640else641{ while ( isidchar(*p) ) p--;642*bp++ = p+1;643}644p = end;645}646while ( *p++ == ',' );647*bp = p;648/* Make a special check for 'void' arglist */649if ( bp == breaks+2 )650{ p = skipspace(breaks[0], 1);651if ( !strncmp(p, "void", 4) )652{ p = skipspace(p+4, 1);653if ( p == breaks[2] - 1 )654{ bp = breaks; /* yup, pretend arglist is empty */655writeblanks(breaks[0], p + 1);656}657}658}659/* Put out the function name and left parenthesis. */660p = buf;661while ( p != endfn ) putc(*p, out), p++;662/* Put out the declaration. */663if ( header )664{ fputs(");", out);665for ( p = breaks[0]; *p; p++ )666if ( *p == '\r' || *p == '\n' )667putc(*p, out);668}669else670{ for ( ap = breaks+1; ap < bp; ap += 2 )671{ p = *ap;672while ( isidchar(*p) )673putc(*p, out), p++;674if ( ap < bp - 1 )675fputs(", ", out);676}677fputs(") ", out);678/* Put out the argument declarations */679for ( ap = breaks+2; ap <= bp; ap += 2 )680(*ap)[-1] = ';';681if ( vararg != 0 )682{ *vararg = 0;683fputs(breaks[0], out); /* any prior args */684fputs("va_dcl", out); /* the final arg */685fputs(bp[0], out);686}687else688fputs(breaks[0], out);689}690free((char *)breaks);691return 0;692}693694695