main.c 6.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/*
 * main.c
 *
 * (c) Copyright 1988-1994 Adobe Systems Incorporated.
 * All rights reserved.
 * 
 * Permission to use, copy, modify, distribute, and sublicense this software
 * and its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notices appear in all copies and that
 * both those copyright notices and this permission notice appear in
 * supporting documentation and that the name of Adobe Systems Incorporated
 * not be used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  No trademark license
 * to use the Adobe trademarks is hereby granted.  If the Adobe trademark
 * "Display PostScript"(tm) is used to describe this software, its
 * functionality or for any other purpose, such use shall be limited to a
 * statement that this software works in conjunction with the Display
 * PostScript system.  Proper trademark attribution to reflect Adobe's
 * ownership of the trademark shall be given whenever any such reference to
 * the Display PostScript system is made.
 * 
 * ADOBE MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE SOFTWARE FOR
 * ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
 * ADOBE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON- INFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO EVENT SHALL ADOBE BE LIABLE
 * TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE, STRICT LIABILITY OR ANY OTHER ACTION ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  ADOBE WILL NOT
 * PROVIDE ANY TRAINING OR OTHER SUPPORT FOR THE SOFTWARE.
 * 
 * Adobe, PostScript, and Display PostScript are trademarks of Adobe Systems
 * Incorporated which may be registered in certain jurisdictions
 * 
 * Author:  Adobe Systems Incorporated
 */
/* $XFree86: xc/config/pswrap/main.c,v 1.4 2000/06/07 19:50:47 tsi Exp $ */

#include <stdio.h>

#ifdef XENVIRONMENT
#include <X11/Xos.h>
#else
#include <string.h>
#endif

#define SLASH '/'
#include <ctype.h>
#include <stdlib.h>
#include "pswpriv.h"

#define MIN_MAXSTRING 80	/* min allowable value of maxstring */

/* global data */
char	*prog;			/* program name */
char	*special_h = NULL;	/* -f option */
char	*hfile = NULL;		/* name of -h file */
char	*ofile = NULL;		/* name of -o file */
char	*ifile = NULL;		/* name of input file */
int	gotInFile = 0;		/* got an explicit input file name */
int	doANSI = 0;		/* got -a (ansi) flag for -h file */
int 	pad = 0;		/* got -p (padding) flag */
boolean	noUserNames = false;	/* got -n (don't use usernames) flag */
int	reentrant = 0;		/* automatic vars for generated BOS */
int	bigFile = 0;		/* got -b flag => call free */
FILE	*header;		/* stream for -h file output */
char	headid[200];		/* id for header file #include */
int	maxstring = 2000;	/* -s max string length to scan */
char	*string_temp;		/* string buffer of above size */
int	outlineno = 1;		/* output line number */
int	nWraps = 0;		/* total number of wraps */
#ifdef __MACH__
char	*shlibInclude = NULL;	/* special file to be #included at top of */
                  		/* file.  Used only when building shlibs */
#endif /* __MACH__ */

static void Usage(void)
{
    fprintf(stderr,"Usage:  pswrap [options] [input-file]\n");
    fprintf(stderr,"    -a              produce ANSI C procedure prototypes\n");
    fprintf(stderr,"    -b              process a big file\n");
    fprintf(stderr,"    -f filename     include special header\n");
    fprintf(stderr,"    -h filename     specify header filename\n");
    fprintf(stderr,"    -o filename     specify output C filename\n");
    fprintf(stderr,"    -r              make wraps re-entrant\n");
    fprintf(stderr,"    -s length       set maximum string length\n");
    exit(1);
}

static void ScanArgs(int argc, char *argv[])
{
    char	*slash;		/* index of last / in hfile */
    char	*c;		/* pointer into headid for conversion */
    int 	i = 0;

    prog = argv[i++];
    slash = rindex(prog,SLASH);
    if (slash)
	prog = slash + 1;
    while (i < argc) {
	if (*argv[i] != '-') {
	    if (ifile != NULL) {
		fprintf(stderr, "%s:  Only one input file can be specified.\n", prog);
		Usage();
	    } else {
		ifile = argv[i];
	    }
	} else {
	    switch (*(argv[i]+1)) {
	    case 'a':
		doANSI++;
		reentrant++;
		break;
	    case 'b':
		bigFile++;
		break;
#ifdef PSWDEBUG
	    case 'd':
	    	lexdebug++;
		break;
#endif /* PSWDEBUG */
	    case 'f':
		special_h = argv[++i];
		break;
	    case 'h':
		hfile = argv[++i];
		slash = rindex(hfile,SLASH);
		strcpy(headid, slash ? slash+1 : hfile);
		for (c = headid; *c != '\0'; c++) {
		    if (*c == '.') *c = '_';
		    else if (isascii(*c) && islower(*c)) *c = toupper(*c);
		}
		break;
	    case 'o':
		ofile = argv[++i];
		break;
	    case 'r':
		reentrant++;
		break;
	    case 's':
		if ((maxstring = atoi(argv[++i])) < MIN_MAXSTRING) {
		    fprintf(stderr,"%s: -s %d is the minimum\n", prog, MIN_MAXSTRING);
		    maxstring = MIN_MAXSTRING;
		}
		break;
	    case 'w':
	    	break;
#ifdef __MACH__
 	    case 'S':
 		shlibInclude = argv[++i];
 		break;
#endif /* __MACH__ */
	     case 'n':
	     	noUserNames = true;
		break;
	    case 'p':
		pad++;
		break;
	    default:
		fprintf(stderr, "%s:  bad option '-%c'\n", prog, *(argv[i]+1));
		Usage();
		break;
	    } /* switch */
	} /* else */
	i++;
    } /* while */
} /* ScanArgs */

int main(int argc, char *argv[])
{
    int		retval;		/* return from yyparse */
	
    ScanArgs(argc, argv);

    if (ifile == NULL)
	ifile = "stdin";
    else {
	gotInFile = 1;
	if (freopen(ifile,"r",stdin) == NULL) {
	    fprintf(stderr, "%s:  can't open %s for input\n", prog, ifile);
	    exit(1);
	}
    }
    if ((string_temp = (char *) malloc((unsigned) (maxstring+1))) == 0) {
	fprintf(stderr, "%s:  can't allocate %d char string; try a smaller -s value\n", prog, maxstring);
	exit(1);
    }
    if (ofile == NULL)
		ofile = "stdout";
    else {
#ifdef __MACH__
 		(void)unlink(ofile);
#endif /* __MACH__ */
    	if (freopen(ofile,"w",stdout) == NULL) {
	    fprintf(stderr, "%s:  can't open %s for output\n", prog, ofile);
	    exit(1);
	}
    }
    InitOFile();

    if (hfile != NULL) {
#ifdef __MACH__
 		(void)unlink(hfile);
#endif /* __MACH__ */
	if ((header = fopen(hfile,"w")) == NULL) {
	    fprintf(stderr, "%s:  can't open %s for output\n", prog, hfile);
	    exit(1);
	}
    }
    if (header != NULL)	InitHFile();

    InitWellKnownPSNames();

    if ((retval = yyparse()) != 0)
	fprintf(stderr,"%s: error in parsing %s\n",prog,ifile);
    else if (errorCount != 0) {
	fprintf(stderr,"%s: errors were encountered\n",prog);
	retval = errorCount;
    }

    if (hfile != NULL) FinishHFile();

    exit (retval);
}