diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/kamal/texexpand2.c |
Initial commit
Diffstat (limited to 'support/kamal/texexpand2.c')
-rw-r--r-- | support/kamal/texexpand2.c | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/support/kamal/texexpand2.c b/support/kamal/texexpand2.c new file mode 100644 index 0000000000..467dfd3cff --- /dev/null +++ b/support/kamal/texexpand2.c @@ -0,0 +1,203 @@ +/* COPYRIGHT (C) 1987 Kamal Al-Yahya */ +/* texexpand: to expand TeX and LaTeX \input and include files */ + +char *documentation[] = { +" SYNTAX", +" texexpand [-w] [parameters] [inputfiles]", +"", +" flags:", +" -w does not check matching", +"", +" parameters:", +" in=filename filename is the input file", +" (Default: in=stdin)", +"", +" out=filename filename is the output file", +" (Default: out=stdout)", +"" +}; + +/* Author: Kamal Al-Yahya, Stanford University, 11/1/83 */ +/* Last modified: 1/25/87 */ + +int doclength = { sizeof documentation/sizeof documentation[0] }; + +#include "setups.h" + +#ifdef tops20 +#define TEMPFILE "texXXXXXX" +#else +#define TEMPFILE "/tmp/texXXXXXX" +#endif + +#ifdef MSC +#else +struct sgttyb ttystat; +#endif + +extern char *mktemp(); +char string[MAXWORD], filename[MAXWORD], scratch_file[MAXWORD]; +FILE *out_file; + +int wflag; +int xargc; +char **xargv; + +main(argc,argv) +int argc; +char *argv[]; +{ +char *buf; +FILE *temp,*scr; +register char *cptr; +int piped_in; +int i; + +if (((buf = (char *)malloc(MAXLEN*sizeof(char))) == (char *)NULL)) + { + fprintf(stderr,"texexpand: Cannot malloc() internal buffer space\n\ +Need an array of %d characters\n",MAXLEN); + exit(-1); + } + +/* If no arguments, and not in a pipeline, self document */ +#ifdef MSC /* MS-DOS cannot distinguish piped input from no input */ +piped_in = (argc == 1); +#else +piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat); +#endif +if (argc == 1 && !piped_in) + { + for( i=0; i<doclength; i++) + printf("%s\n",documentation[i]); + exit (0); + } + +out_file=stdout; /* default standard output */ + +/* process option flags */ +xargc = argc; +xargv = argv; +for (xargc--,xargv++; xargc; xargc--,xargv++) + { + cptr = *xargv; + if( *cptr=='-' ) + { + while( *(++cptr)) + { + switch( *cptr ) + { + case 'w': + wflag=1; + break; + default: + fprintf(stderr, + "texexpand: unknown flag -%c\n",*cptr); + break; + } + } + } + } + +/* process getpar parameters */ +xargc = argc; +xargv = argv; + +if(getpar_("out","s",string)) + { + sscanf(string,"%s",filename); + if((temp=fopen(filename,"w")) == NULL) + fprintf(stderr,"texexpand: Cannot open output file %s\n",filename); + else + out_file = temp; + } + + +/* first process pipe input */ +if(piped_in) + { + if (wflag != 1) + { +/* need to buffer; can't seek in pipes */ +/* make a temporary and volatile file in /tmp */ + strcpy(scratch_file,TEMPFILE); + mktemp(scratch_file); + if ((scr=fopen(scratch_file,"w")) == (FILE *)NULL) + { + fprintf(stderr, + "texexpand: Cannot open scratch file [%s]\n",scratch_file); + exit(-1); + } + scrbuf(stdin,scr); + fclose(scr); + scr=fopen(scratch_file,"r"); + unlink(scratch_file); + fprintf(stderr,"Checking matching...\n"); + Match(scr); + fseek(scr,0,0); + Expand(scr,buf); + fprintf(stderr,"Checking matching done\n\n"); + fclose(scr); + } + else + Expand(stdin,buf); + fputs(buf,out_file); + } + +/* next process in=inputfiles */ +if(getpar_("in","s",string)) + { + sscanf(string,"%s",filename); + if((temp=fopen(filename,"r")) != NULL) + { + if (wflag != 1) + { + fprintf(stderr,"Checking matching...\n"); + fprintf(stderr,"%s:\n",filename); + Match(temp); + fprintf(stderr,"\n"); + fseek(temp,0,0); + } + Expand(temp,buf); + if (wflag != 1) + fprintf(stderr,"Checking matching done\n\n"); + fputs(buf,out_file); + fclose(temp); + } + else + fprintf(stderr,"texexpand: Cannot open %s\n",filename); + } + +/* then process input line for arguments and assume they are input files */ +for (xargc--,xargv++; xargc; xargc--,xargv++) + { + cptr = *xargv; + if( *cptr=='-' ) continue; /* this is a flag */ + while (*cptr) + { + if (*cptr == '=') break; /* this is for getpar */ + cptr++; + } + if (*cptr) continue; + cptr = *xargv; + if((temp=fopen(cptr,"r")) != (FILE *)NULL) + { + if (wflag != 1) + { + fprintf(stderr,"Checking matching...\n"); + fprintf(stderr,"%s:\n",cptr); + Match(temp); + fprintf(stderr,"\n"); + fseek(temp,0,0); + } + Expand(temp,buf); + if (wflag != 1) + fprintf(stderr,"Checking matching done\n\n"); + fputs(buf,out_file); + fclose(temp); + } + else + fprintf(stderr,"texexpand: Cannot open %s\n",cptr); + } + +} |