home *** CD-ROM | disk | FTP | other *** search
- /* detex: strips TeX's and LaTeX's commands */
- /* to compile: cc detex.c -o detex */
-
-
- char *documentation[] = {
- " SYNTAX",
- " detex [-iw] [parameters] [inputfiles]",
- "",
- " flags:",
- " -i ignores TeX's and LaTeX's \input and \include commands",
- " -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 */
- /* Modified: 6/30/86 */
-
- int doclength = { sizeof documentation/sizeof documentation[0] };
-
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <sgtty.h>
- #define MAXLEN 100000
-
- char string[100],filename[100];
- FILE *out_file;
- struct sgttyb ttystat;
- extern char *strcpy(), *mktemp();
- char scratch_file[100];
-
- int wflag;
- int xargc;
- char **xargv;
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- char big[MAXLEN];
- FILE *temp,*scr;
- register char *cptr;
- int piped_in;
- int iflag,i;
-
- /* If no arguments, and not in a pipeline, self document */
- piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
- if (argc == 1 && !piped_in)
- {
- for( i=0; i<doclength; i++)
- printf("%s\n",documentation[i]);
- exit (0);
- }
-
- out_file = stdout; /* default output */
-
- /* process option flags */
- xargc = argc;
- xargv = argv;
- for (xargc--,xargv++; xargc; xargc--,xargv++)
- {
- cptr = *xargv;
- if( *cptr=='-' )
- {
- while( *(++cptr))
- {
- switch( *cptr )
- {
- case 'i':
- iflag=1;
- break;
- case 'w':
- wflag=1;
- break;
- default:
- fprintf(stderr,
- "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,"detex: Cannot open output file %s\n",filename);
- else
- out_file = temp;
- }
-
- /* first process pipe input */
- if(piped_in)
- {
- /* need to buffer; can't seek in pipes */
- /* make a temporary and volatile file in /tmp */
- strcpy(scratch_file,"/tmp/texXXXXXX");
- mktemp(scratch_file);
- scr=fopen(scratch_file,"w");
- scrbuf(stdin,scr);
- fclose(scr);
- scr=fopen(scratch_file,"r");
- unlink(scratch_file);
- if (wflag != 1)
- {
- fprintf(stderr,"Checking matching...\n");
- TeXMatch(scr);
- fseek(scr,0,0);
- }
- /* either expand or buffer */
- if (iflag != 1)
- { TeXExpand(scr,big,MAXLEN); fclose(scr); }
- else
- { tmpbuf(scr,big); fclose(scr); }
- if (wflag != 1)
- fprintf(stderr,"Checking matching done\n\n");
- DeTeX(big,out_file);
- fclose(scr);
- }
-
- /* 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",cptr);
- TeXMatch(temp);
- fprintf(stderr,"\n");
- fseek(temp,0,0);
- }
- /* either expand or buffer */
- if (iflag != 1)
- { TeXExpand(temp,big,MAXLEN); fclose(temp); }
- else
- { tmpbuf(temp,big); fclose(temp); }
- if (wflag != 1)
- fprintf(stderr,"Checking matching done\n\n");
- DeTeX(big,out_file);
- fclose(temp);
- }
- else
- fprintf(stderr,"detex: Cannot open %s\n",filename);
- }
-
- /* then process input line for arguments and assume they are input files */
- xargc = argc;
- xargv = argv;
-
- 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")) != NULL)
- {
- if (wflag != 1)
- {
- fprintf(stderr,"Checking matching...\n");
- fprintf(stderr,"%s:\n",cptr);
- TeXMatch(temp);
- fprintf(stderr,"\n");
- fseek(temp,0,0);
- }
- /* either expand or buffer */
- if (iflag != 1)
- { TeXExpand(temp,big,MAXLEN); fclose(temp); }
- else
- { tmpbuf(temp,big); fclose(temp); }
- if (wflag != 1)
- fprintf(stderr,"Checking matching done\n\n");
- DeTeX(big,out_file);
- fclose(temp);
- }
- else
- fprintf(stderr,"detex: Cannot open %s\n",cptr);
- }
-
- }
-
- DeTeX(big,out_file) /* stripping TEX commands */
-
- char *big;
- FILE *out_file;
- {
- int c,c1,be;
-
- while ((c = *big++) != '\0')
- {
- switch (c)
- {
- /* detect TeX commands (backslash) */
- case '\\':
- c1=c ; /* c1 is needed to see if there is \$ */
- c=' ' ; /* "erase" the backslash */
- putc(c,out_file);
- if ((c = *big++) =='b')
- if (begin_end_buf(big,&be) == 0)
- {
- if (be == 1)
- {
- big += 14;
- while ( *big++ != '\0')
- {
- if(begin_end_buf(big,&be) == 0)
- if (be == 2)
- {
- big += 13;
- break;
- }
- }
- }
- }
- while ((c = *big++) != '\0')
- {
- if (c == '$') goto dollar;
- if (c == '%') goto comm;
- if (c == '{' || c == '#') break;
- if (c == ' ' || c == '\n' || c == '(' || c == ')')
- {
- putc(c,out_file);
- break;
- }
- c1=c; /* reinitialize c1 */
- }
- break;
- /* come here if the character is dollar sign */
- dollar:
- case '$':
- c=' ' ; /* "erase" the dollar sign */
- putc (c,out_file);
- if (c1 == '\\') break; /* means \$ */
- c = *big++;
- if(c == '$') goto dollar2;
- else goto dollar1;
- break;
-
- /* see if it is an in-line equation (delimeter is one dollar sign) */
- dollar1:
- while((c = *big++) != '\0')
- if(c == '$') break;
- break;
-
- /* see if it is a displayed equation (delimeter is two dollar signs) */
- dollar2:
- while((c = *big++) != '\0')
- {
- if(c == '$')
- {
- c = *big++;
- if (c != '$') putc(c,out_file);
- break;
- }
- }
- break;
-
- /* ignore commented text */
- comm:
- case '%':
- if (c1 == '\\') break; /* means \% */
- while((c = *big++) != '\0')
- if (c == '\n')
- {
- putc(c,out_file);
- break;
- }
- break;
- /* erase these character */
- case '{':
- c=' ';
- case '}':
- c=' ';
- case '_':
- c=' ';
- case '^':
- c=' ';
- case '&':
- c=' ';
- case '#':
- c=' ';
- /* default is doing nothing: pass the character to the output */
- default:
- putc(c,out_file);
- break;
- }
- c1=c; /* reinitialize c1 */
- }
- }
-