home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / text / detex-1.0.lha / detex / detex.l (.txt) < prev    next >
LaTeX Document  |  1994-05-20  |  17KB  |  480 lines

  1. #ifndef lint
  2. static char    rcsid[] = "$Header: /usr/src/local/bin/detex/RCS/detex.l,v 2.16 1993/01/14 16:48:25 trinkle Exp $";
  3. #endif
  4.  * detex [-e environment-list] [-c] [-l] [-n] [-s] [-t] [-w] [file[.tex]]
  5.  *    This program is used to remove TeX or LaTeX constructs from a text
  6.  *    file.
  7.  * Written by:
  8.  *    Daniel Trinkle
  9.  *    Department of Computer Science
  10.  *    Purdue University
  11. #ifdef _DCC
  12. #include <string.h>
  13. #define index    strchr
  14. #define rindex    strrchr
  15. #endif /* _DCC */
  16. #include "detex.h"
  17. #ifdef USG
  18. #include <string.h>
  19. #define index    strchr
  20. #define rindex    strrchr
  21. #else
  22. #include <strings.h>
  23. #endif
  24. #ifndef MAXPATHLEN
  25. #include <sys/param.h>
  26. #endif
  27. #define LaBEGIN     if (fLatex) BEGIN
  28. #define CITEBEGIN    if (fLatex && !fCite) BEGIN
  29. #define IGNORE        if (fSpace && !fWord) putchar(' ')
  30. #define SPACE        if (!fWord) putchar(' ')
  31. #define NEWLINE     if (!fWord) putchar('\n')
  32. #ifndef NO_MALLOC_DECL
  33. char    *malloc();
  34. #endif
  35. char    *rgsbEnvIgnore[MAXENVS];    /* list of environments ignored */
  36. char    *rgsbIncList[MAXINCLIST];    /* list of includeonly files */
  37. char    *rgsbInputPaths[MAXINPUTPATHS]; /* list of input paths in order */
  38. char    sbCurrentEnv[CCHMAXENV];    /* current environment being ignored */
  39. char    *sbProgName;            /* name we were invoked with */
  40. FILE    *rgfp[NOFILE+1];        /* stack of input/include files */
  41. int    cfp = 0;            /* count of files in stack */
  42. int    cOpenBrace = 0;         /* count of `{' in <LaMacro2> */
  43. int    csbEnvIgnore;            /* count of environments ignored */
  44. int    csbIncList = 0;         /* count of includeonly files */
  45. int    csbInputPaths;            /* count of input paths */
  46. int    fLatex = 0;            /* flag to indicated delatex */
  47. int    fWord = 0;            /* flag for -w option */
  48. int    fFollow = 1;            /* flag to follow input/include */
  49. int    fCite = 0;            /* flag to echo \cite and \ref args */
  50. int    fSpace = 0;            /* flag to replace \cs with space */
  51. int    fForcetex = 0;            /* flag to inhibit latex mode */
  52. S    [ \t\n]*
  53. W    [a-zA-Z]+
  54. %Start Define Display IncludeOnly Input Math Normal Control
  55. %Start LaBegin LaDisplay LaEnd LaEnv LaFormula LaInclude
  56. %Start LaMacro LaMacro2 LaVerbatim
  57. <Normal>"%".*           /* ignore comments */   ;
  58. <Normal>"\\begin"{S}"{"{S}"document"{S}"}"      {fLatex = !fForcetex; IGNORE;}
  59. <Normal>"\\begin"     /* environment start */   {LaBEGIN LaBegin; IGNORE;}
  60. <LaBegin>{S}"{"{S}"verbatim"{S}"}"              {   if (BeginEnv("verbatim"))
  61.                             BEGIN LaEnv;
  62.                             else
  63.                             BEGIN LaVerbatim;
  64.                             IGNORE;
  65.                         }
  66. <LaVerbatim>"\\end"{S}"{"{S}"verbatim"{S}"}" /* verbatim mode */
  67.                         {BEGIN Normal; IGNORE;}
  68. <LaVerbatim>.                    ECHO;
  69. <LaBegin>{W}                    {   if (BeginEnv(yytext))
  70.                             BEGIN LaEnv;
  71.                             else
  72.                             BEGIN LaMacro;
  73.                             IGNORE;
  74.                         }
  75. <LaBegin>"\n"                                   NEWLINE;
  76. <LaBegin>.                    ;
  77. <LaEnv>"\\end"  /* absorb some environments */  {LaBEGIN LaEnd; IGNORE;}
  78. <LaEnv>"\n"                                     NEWLINE;
  79. <LaEnv>.                    ;
  80. <LaEnd>{W}         /* end environment */    {   if (EndEnv(yytext))
  81.                             BEGIN Normal;
  82.                             IGNORE;
  83.                         }
  84. <LaEnd>"}"                                      {BEGIN LaEnv; IGNORE;}
  85. <LaEnd>"\n"                                     NEWLINE;
  86. <LaEnd>.                    ;
  87. <Normal>"\\bibitem"         /* ignore args  */  {LaBEGIN LaMacro2; IGNORE;}
  88. <Normal>"\\bibliography"    /* of these \cs */  {LaBEGIN LaMacro; IGNORE;}
  89. <Normal>"\\bibstyle"                            {LaBEGIN LaMacro; IGNORE;}
  90. <Normal>"\\cite"                                {CITEBEGIN LaMacro2; IGNORE;}
  91. <Normal>"\\documentstyle"                       {LaBEGIN LaMacro; IGNORE;}
  92. <Normal>"\\end"                                 {LaBEGIN LaMacro; IGNORE;}
  93. <Normal>"\\index"                               {LaBEGIN LaMacro2; SPACE;}
  94. <Normal>"\\label"                               {LaBEGIN LaMacro; IGNORE;}
  95. <Normal>"\\pageref"                             {CITEBEGIN LaMacro; IGNORE;}
  96. <Normal>"\\pagestyle"                           {LaBEGIN LaMacro; IGNORE;}
  97. <Normal>"\\ref"                                 {CITEBEGIN LaMacro; IGNORE;}
  98. <Normal>"\\setcounter"                          {LaBEGIN LaMacro; IGNORE;}
  99. <Normal>"\\verb" /* ignore \verb<char>...<char> */
  100.                         {   if (fLatex) {
  101.                             char verbchar, c;
  102.                             verbchar = input();
  103.                             while ((c = input()) != verbchar)
  104.                                 if (c == '\n')
  105.                                 NEWLINE;
  106.                             }
  107.                             IGNORE;
  108.                         }
  109. <LaMacro>"}"                                    BEGIN Normal;
  110. <LaMacro>"\n"                                   NEWLINE;
  111. <LaMacro>.                    ;
  112. <LaMacro2>"{"                                   {   cOpenBrace++; }
  113. <LaMacro2>"}"                                   {   cOpenBrace--;
  114.                             if (cOpenBrace == 0)
  115.                             BEGIN Normal;
  116.                         }
  117. <LaMacro2>"\n"                                  NEWLINE;
  118. <LaMacro2>.                    ;
  119. <Normal>"\\def"         /* ignore def begin */  {BEGIN Define; IGNORE;}
  120. <Define>"{"                                     BEGIN Normal;
  121. <Define>"\n"                                    NEWLINE;
  122. <Define>.                    ;
  123. <Normal>"\\("           /* formula mode */      {LaBEGIN LaFormula; IGNORE;}
  124. <LaFormula>"\\)"                                BEGIN Normal;
  125. <LaFormula>"\n"                                 NEWLINE;
  126. <LaFormula>.                    ;
  127. <Normal>"\\["           /* display mode */      {LaBEGIN LaDisplay; IGNORE;}
  128. <LaDisplay>"\\]"                                BEGIN Normal;
  129. <LaDisplay>"\n"                                 NEWLINE;
  130. <LaDisplay>.                    ;
  131. <Normal>"$$"            /* display mode */      {BEGIN Display; IGNORE;}
  132. <Display>"$$"                                   BEGIN Normal;
  133. <Display>"\n"                                   NEWLINE;
  134. <Display>.                    ;
  135. <Normal>"$"             /* math mode */         {BEGIN Math; IGNORE;}
  136. <Math>"$"                                       BEGIN Normal;
  137. <Math>"\n"                                      NEWLINE;
  138. <Math>"\\$"                                     ;
  139. <Math>.                     ;
  140. <Normal>"\\include"     /* process files */     {LaBEGIN LaInclude; IGNORE;}
  141. <LaInclude>[^{ \t\n}]+                {   IncludeFile(yytext);
  142.                             BEGIN Normal;
  143.                         }
  144. <LaInclude>"\n"                                 NEWLINE;
  145. <LaInclude>.                    ;
  146. <Normal>"\\includeonly"                         {BEGIN IncludeOnly; IGNORE;}
  147. <IncludeOnly>[^{ \t,\n}]+            AddInclude(yytext);
  148. <IncludeOnly>"}"                                {   if (csbIncList == 0)
  149.                             rgsbIncList[csbIncList++] = '\0';
  150.                             BEGIN Normal;
  151.                         }
  152. <IncludeOnly>"\n"                               NEWLINE;
  153. <IncludeOnly>.                    ;
  154. <Normal>"\\input"                               {BEGIN Input; IGNORE;}
  155. <Input>[^{ \t\n}]+                {   InputFile(yytext);
  156.                             BEGIN Normal;
  157.                         }
  158. <Input>"\n"                                     NEWLINE;
  159. <Input>.                    ;
  160. <Normal>\\(aa|AA|ae|AE|oe|OE|ss)[ \t]*[ \t\n}] /* handle ligatures */
  161.                         {printf("%.2s", yytext+1);}
  162. <Normal>\\[OoijLl][ \t]*[ \t\n}]        {printf("%.1s", yytext+1);}
  163. <Normal>\\[a-zA-Z@]+    /* ignore other \cs */    {BEGIN Control; IGNORE;}
  164. <Normal>"\\ "                                   SPACE;
  165. <Normal>\\.                    IGNORE;
  166. <Control>\\[a-zA-Z@]+                IGNORE;
  167. <Control>[a-zA-Z@0-9]*[-'=`][^ \t\n{]*          IGNORE;
  168. <Control>"\n"                                   {BEGIN Normal; NEWLINE;}
  169. <Control>[ \t]*[{]*                {BEGIN Normal; IGNORE;}
  170. <Control>.                    {yyless(0);BEGIN Normal;}
  171. <Normal>[{}\\|] /* special characters */    IGNORE;
  172. <Normal>[!?]"`"                                 IGNORE;
  173. <Normal>~                    SPACE;
  174. <Normal>{W}[']*{W}                              {   if (fWord)
  175.                             printf("%s\n", yytext);
  176.                             else
  177.                             ECHO;
  178.                         }
  179. <Normal>[0-9]+                    if (!fWord) ECHO;
  180. <Normal>(.|\n)                                  if (!fWord) ECHO;
  181. /******
  182. ** main --
  183. **    Set sbProgName to the base of arg 0.
  184. **    Set the input paths.
  185. **    Check for options
  186. **        -c        echo LaTeX \cite, \ref, and \pageref values
  187. **        -e <env-list>    list of LaTeX environments to ignore
  188. **        -l        force latex mode
  189. **        -n        do not follow \input and \include
  190. **        -s        replace control sequences with space
  191. **        -t        force tex mode
  192. **        -w        word only output
  193. **    Set the list of LaTeX environments to ignore.
  194. **    Process each input file.
  195. **    If no input files are specified on the command line, process stdin.
  196. ******/
  197. main(cArgs,rgsbArgs)
  198. int    cArgs;
  199. char    *rgsbArgs[];
  200.     char    *pch, *sbEnvList = DEFAULTENV, sbBadOpt[2];
  201.     FILE    *TexOpen();
  202.     int    fSawFile = 0, iArgs = 1;
  203.     /* get base nam