%{ #ifndef lint static char rcsid[] = "$Header: /p/src/local/bin/detex/RCS/detex.l,v 2.19 1997/09/10 18:12:37 trinkle Exp $"; #endif /* * detex [-e environment-list] [-c] [-l] [-n] [-s] [-t] [-w] [file[.tex] ] * * This program is used to remove TeX or LaTeX constructs from a text * file. * * Written by: * Daniel Trinkle * Department of Computer Science * Purdue University * */ #ifdef FPTEX #include #undef ERROR #undef IGNORE #endif #include "detex.h" #ifndef KPATHSEA #ifdef HAVE_STRING_H #include #define index strchr #define rindex strrchr #else #include #endif #ifndef MAXPATHLEN #include #endif #define PATH_MAX MAXPATHLEN #ifdef OS2 #include #endif #ifndef NO_MALLOC_DECL char *malloc(); #endif #else #include "c-auto.h" #include "kpathsea/c-auto.h" #include "kpathsea/config.h" #include "kpathsea/c-memstr.h" #include "kpathsea/c-pathmx.h" #include "kpathsea/c-std.h" #include "kpathsea/tex-file.h" #ifdef HAVE_SYS_PARAM_H #include #endif #endif void SetEnvIgnore(const char *sbEnvList); int BeginEnv(const char *sbEnv); int EndEnv(const char *sbEnv); void InputFile(char *sbFile); void IncludeFile(char *sbFile); void AddInclude(char *sbFile); int InList(char *sbFile); void SetInputPaths(void); int SeparateList(char *sbList, char **rgsbList ,char chSep,int csbMax); FILE * TexOpen(char *sbFile); char * SafeMalloc(int cch, const char *sbMessage); void Warning(const char *sb1, const char *sb2); void ErrorExit(const char *sb1); #define LaBEGIN if (fLatex) BEGIN #define CITEBEGIN if (fLatex && !fCite) BEGIN #define IGNORE if (fSpace && !fWord) putchar(' ') #define SPACE if (!fWord) putchar(' ') #define NEWLINE if (!fWord) putchar('\n') #ifdef OS2 void yyless(int); void OS2UsageExit(void); #endif char *rgsbEnvIgnore[MAXENVS]; /* list of environments ignored */ char *rgsbIncList[MAXINCLIST]; /* list of includeonly files */ char *rgsbInputPaths[MAXINPUTPATHS]; /* list of input paths in order */ char sbCurrentEnv[CCHMAXENV]; /* current environment being ignored */ char *sbProgName; /* name we were invoked with */ FILE *rgfp[NOFILE+1]; /* stack of input/include files */ int cfp = 0; /* count of files in stack */ int cOpenBrace = 0; /* count of `{' in */ int csbEnvIgnore; /* count of environments ignored */ int csbIncList = 0; /* count of includeonly files */ int csbInputPaths; /* count of input paths */ int fLatex = 0; /* flag to indicated delatex */ int fWord = 0; /* flag for -w option */ int fFollow = 1; /* flag to follow input/include */ int fCite = 0; /* flag to echo \cite and \ref args */ int fSpace = 0; /* flag to replace \cs with space */ int fForcetex = 0; /* flag to inhibit latex mode */ %} S [ \t\n]* W [a-zA-Z]+ %Start Define Display IncludeOnly Input Math Normal Control %Start LaBegin LaDisplay LaEnd LaEnv LaFormula LaInclude %Start LaMacro LaMacro2 LaVerbatim %% "%".* /* ignore comments */ ; "\\begin"{S}"{"{S}"document"{S}"}" {fLatex = !fForcetex; IGNORE;} "\\begin" /* environment start */ {LaBEGIN LaBegin; IGNORE;} {S}"{"{S}"verbatim"{S}"}" { if (BeginEnv("verbatim")) BEGIN LaEnv; else BEGIN LaVerbatim; IGNORE; } "\\end"{S}"{"{S}"verbatim"{S}"}" /* verbatim mode */ {BEGIN Normal; IGNORE;} . ECHO; {W} { if (BeginEnv(yytext)) BEGIN LaEnv; else BEGIN LaMacro; IGNORE; } "\n" NEWLINE; . ; "\\end" /* absorb some environments */ {LaBEGIN LaEnd; IGNORE;} "\n" NEWLINE; . ; {W} /* end environment */ { if (EndEnv(yytext)) BEGIN Normal; IGNORE; } "}" {BEGIN LaEnv; IGNORE;} "\n" NEWLINE; . ; "\\bibitem" /* ignore args */ {LaBEGIN LaMacro2; IGNORE;} "\\bibliography" /* of these \cs */ {LaBEGIN LaMacro; IGNORE;} "\\bibstyle" {LaBEGIN LaMacro; IGNORE;} "\\cite" {CITEBEGIN LaMacro2; IGNORE;} "\\documentstyle" {LaBEGIN LaMacro; IGNORE;} "\\end" {LaBEGIN LaMacro; IGNORE;} "\\footnote" {SPACE;} "\\index" {LaBEGIN LaMacro2; SPACE;} "\\label" {LaBEGIN LaMacro; IGNORE;} "\\pageref" {CITEBEGIN LaMacro; IGNORE;} "\\pagestyle" {LaBEGIN LaMacro; IGNORE;} "\\ref" {CITEBEGIN LaMacro; IGNORE;} "\\setcounter" {LaBEGIN LaMacro; IGNORE;} "\\verb" /* ignore \verb... */ { if (fLatex) { char verbchar, c; verbchar = input(); while ((c = input()) != verbchar) if (c == '\n') NEWLINE; } IGNORE; } "}" BEGIN Normal; "\n" NEWLINE; . ; "{" { cOpenBrace++; } "}" { cOpenBrace--; if (cOpenBrace == 0) BEGIN Normal; } "\n" NEWLINE; . ; "\\def" /* ignore def begin */ {BEGIN Define; IGNORE;} "{" BEGIN Normal; "\n" NEWLINE; . ; "\\(" /* formula mode */ {LaBEGIN LaFormula; IGNORE;} "\\)" BEGIN Normal; "\n" NEWLINE; . ; "\\[" /* display mode */ {LaBEGIN LaDisplay; IGNORE;} "\\]" BEGIN Normal; "\n" NEWLINE; . ; "$$" /* display mode */ {BEGIN Display; IGNORE;} "$$" BEGIN Normal; "\n" NEWLINE; . ; "$" /* math mode */ {BEGIN Math; IGNORE;} "$" BEGIN Normal; "\n" NEWLINE; "\\$" ; . ; "\\include" /* process files */ {LaBEGIN LaInclude; IGNORE;} [^{ \t\n}]+ { IncludeFile(yytext); BEGIN Normal; } "\n" NEWLINE; . ; "\\includeonly" {BEGIN IncludeOnly; IGNORE;} [^{ \t,\n}]+ AddInclude(yytext); "}" { if (csbIncList == 0) rgsbIncList[csbIncList++] = '\0'; BEGIN Normal; } "\n" NEWLINE; . ; "\\input" {BEGIN Input; IGNORE;}