#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
char *prog;
char *special_h = NULL;
char *hfile = NULL;
char *ofile = NULL;
char *ifile = NULL;
int gotInFile = 0;
int doANSI = 0;
int pad = 0;
boolean noUserNames = false;
int reentrant = 0;
int bigFile = 0;
FILE *header;
char headid[200];
int maxstring = 2000;
char *string_temp;
int outlineno = 1;
int nWraps = 0;
#ifdef __MACH__
char *shlibInclude = NULL;
#endif
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;
char *c;
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
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
case 'n':
noUserNames = true;
break;
case 'p':
pad++;
break;
default:
fprintf(stderr, "%s: bad option '-%c'\n", prog, *(argv[i]+1));
Usage();
break;
}
}
i++;
}
}
int main(int argc, char *argv[])
{
int retval;
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
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
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);
}