~ubuntu-branches/ubuntu/jaunty/texlive-bin/jaunty

« back to all changes in this revision

Viewing changes to build/TeX/texk/detex/detex.l

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2008-06-26 23:14:59 UTC
  • mfrom: (2.1.30 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080626231459-y02rjsrgtafu83yr
Tags: 2007.dfsg.2-3
add missing source roadmap.fig of roadmap.eps in fontinst documentation
(Closes: #482915) (urgency medium due to RC bug)
(new patch add-missing-fontinst-source)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
%{
2
 
#ifndef lint
3
 
static char     rcsid[] = "$Header: /p/src/local/bin/detex/RCS/detex.l,v 2.19 1997/09/10 18:12:37 trinkle Exp $";
4
 
#endif
5
 
 
6
 
/*
7
 
 * detex [-e environment-list] [-c] [-l] [-n] [-s] [-t] [-w] [file[.tex] ]
8
 
 *
9
 
 *      This program is used to remove TeX or LaTeX constructs from a text
10
 
 *      file.
11
 
 *
12
 
 * Written by:
13
 
 *      Daniel Trinkle
14
 
 *      Department of Computer Science
15
 
 *      Purdue University
16
 
 *
17
 
 */
18
 
 
19
 
#ifdef FPTEX
20
 
#include <win32lib.h>
21
 
#undef ERROR
22
 
#undef IGNORE
23
 
#endif
24
 
 
25
 
#include "detex.h"
26
 
#ifndef KPATHSEA
27
 
 
28
 
#ifdef HAVE_STRING_H
29
 
#include <string.h>
30
 
#define index   strchr
31
 
#define rindex  strrchr
32
 
#else
33
 
#include <strings.h>
34
 
#endif
35
 
#ifndef MAXPATHLEN
36
 
#include <sys/param.h>
37
 
#endif
38
 
#define PATH_MAX MAXPATHLEN
39
 
#ifdef OS2
40
 
#include <stdlib.h>
41
 
#endif
42
 
#ifndef NO_MALLOC_DECL
43
 
char    *malloc();
44
 
#endif
45
 
 
46
 
#else
47
 
 
48
 
#include "c-auto.h"
49
 
#include "kpathsea/c-auto.h"
50
 
#include "kpathsea/config.h"
51
 
#include "kpathsea/c-memstr.h"
52
 
#include "kpathsea/c-pathmx.h"
53
 
#include "kpathsea/c-std.h"
54
 
#include "kpathsea/tex-file.h"
55
 
 
56
 
#ifdef HAVE_SYS_PARAM_H
57
 
#include <sys/param.h>
58
 
#endif
59
 
 
60
 
#endif
61
 
 
62
 
extern void SetEnvIgnore(char *sbEnvList);
63
 
extern int BeginEnv(char *sbEnv);
64
 
extern int EndEnv(char *sbEnv);
65
 
extern void InputFile(char *sbFile);
66
 
extern void IncludeFile(char *sbFile);
67
 
extern void AddInclude(char *sbFile);
68
 
extern int InList(char *sbFile);
69
 
extern void SetInputPaths();
70
 
extern int SeparateList(char *sbList,char **rgsbList ,char chSep,int csbMax);
71
 
extern FILE * TexOpen(char *sbFile);
72
 
extern char * SafeMalloc(int cch,char *sbMessage);
73
 
extern void Warning(char *sb1,char *sb2);
74
 
extern int ErrorExit(char *sb1);
75
 
 
76
 
#define LaBEGIN         if (fLatex) BEGIN
77
 
#define CITEBEGIN       if (fLatex && !fCite) BEGIN
78
 
#define IGNORE          if (fSpace && !fWord) putchar(' ')
79
 
#define SPACE           if (!fWord) putchar(' ')
80
 
#define NEWLINE         if (!fWord) putchar('\n')
81
 
 
82
 
char    *SafeMalloc();
83
 
#ifdef OS2
84
 
void    yyless(int);
85
 
#endif
86
 
 
87
 
char    *rgsbEnvIgnore[MAXENVS];        /* list of environments ignored */
88
 
char    *rgsbIncList[MAXINCLIST];       /* list of includeonly files */
89
 
char    *rgsbInputPaths[MAXINPUTPATHS]; /* list of input paths in order */
90
 
char    sbCurrentEnv[CCHMAXENV];        /* current environment being ignored */
91
 
char    *sbProgName;                    /* name we were invoked with */
92
 
FILE    *rgfp[NOFILE+1];                /* stack of input/include files */
93
 
int     cfp = 0;                        /* count of files in stack */
94
 
int     cOpenBrace = 0;                 /* count of `{' in <LaMacro2> */
95
 
int     csbEnvIgnore;                   /* count of environments ignored */
96
 
int     csbIncList = 0;                 /* count of includeonly files */
97
 
int     csbInputPaths;                  /* count of input paths */
98
 
int     fLatex = 0;                     /* flag to indicated delatex */
99
 
int     fWord = 0;                      /* flag for -w option */
100
 
int     fFollow = 1;                    /* flag to follow input/include */
101
 
int     fCite = 0;                      /* flag to echo \cite and \ref args */
102
 
int     fSpace = 0;                     /* flag to replace \cs with space */
103
 
int     fForcetex = 0;                  /* flag to inhibit latex mode */
104
 
%}
105
 
 
106
 
S       [ \t\n]*
107
 
W       [a-zA-Z]+
108
 
 
109
 
%Start Define Display IncludeOnly Input Math Normal Control
110
 
%Start LaBegin LaDisplay LaEnd LaEnv LaFormula LaInclude
111
 
%Start LaMacro LaMacro2 LaVerbatim
112
 
 
113
 
%%
114
 
<Normal>"%".*           /* ignore comments */   ;
115
 
 
116
 
<Normal>"\\begin"{S}"{"{S}"document"{S}"}"      {fLatex = !fForcetex; IGNORE;}
117
 
 
118
 
<Normal>"\\begin"     /* environment start */   {LaBEGIN LaBegin; IGNORE;}
119
 
 
120
 
<LaBegin>{S}"{"{S}"verbatim"{S}"}"              {   if (BeginEnv("verbatim"))
121
 
                                                        BEGIN LaEnv;
122
 
                                                    else
123
 
                                                        BEGIN LaVerbatim;
124
 
                                                    IGNORE;
125
 
                                                }
126
 
 
127
 
<LaVerbatim>"\\end"{S}"{"{S}"verbatim"{S}"}" /* verbatim mode */        {BEGIN Normal; IGNORE;}
128
 
<LaVerbatim>.                                   ECHO;
129
 
 
130
 
<LaBegin>{W}                                    {   if (BeginEnv(yytext))
131
 
                                                        BEGIN LaEnv;
132
 
                                                    else
133
 
                                                        BEGIN LaMacro;
134
 
                                                    IGNORE;
135
 
                                                }
136
 
<LaBegin>"\n"                                   NEWLINE;
137
 
<LaBegin>.                                      ;
138
 
 
139
 
<LaEnv>"\\end"  /* absorb some environments */  {LaBEGIN LaEnd; IGNORE;}
140
 
<LaEnv>"\n"                                     NEWLINE;
141
 
<LaEnv>.                                        ;
142
 
 
143
 
<LaEnd>{W}               /* end environment */  {   if (EndEnv(yytext))
144
 
                                                        BEGIN Normal;
145
 
                                                    IGNORE;
146
 
                                                }
147
 
<LaEnd>"}"                                      {BEGIN LaEnv; IGNORE;}
148
 
<LaEnd>"\n"                                     NEWLINE;
149
 
<LaEnd>.                                        ;
150
 
 
151
 
<Normal>"\\bibitem"         /* ignore args  */  {LaBEGIN LaMacro2; IGNORE;}
152
 
<Normal>"\\bibliography"    /* of these \cs */  {LaBEGIN LaMacro; IGNORE;}
153
 
<Normal>"\\bibstyle"                            {LaBEGIN LaMacro; IGNORE;}
154
 
<Normal>"\\cite"                                {CITEBEGIN LaMacro2; IGNORE;}
155
 
<Normal>"\\documentstyle"                       {LaBEGIN LaMacro; IGNORE;}
156
 
<Normal>"\\end"                                 {LaBEGIN LaMacro; IGNORE;}
157
 
<Normal>"\\footnote"                            {SPACE;}
158
 
<Normal>"\\index"                               {LaBEGIN LaMacro2; SPACE;}
159
 
<Normal>"\\label"                               {LaBEGIN LaMacro; IGNORE;}
160
 
<Normal>"\\pageref"                             {CITEBEGIN LaMacro; IGNORE;}
161
 
<Normal>"\\pagestyle"                           {LaBEGIN LaMacro; IGNORE;}
162
 
<Normal>"\\ref"                                 {CITEBEGIN LaMacro; IGNORE;}
163
 
<Normal>"\\setcounter"                          {LaBEGIN LaMacro; IGNORE;}
164
 
<Normal>"\\verb" /* ignore \verb<ch>...<ch> */  {   if (fLatex) {
165
 
                                                        char verbchar, c;
166
 
                                                        verbchar = input();
167
 
                                                        while ((c = input()) != verbchar)
168
 
                                                            if (c == '\n')
169
 
                                                                NEWLINE;
170
 
                                                    }
171
 
                                                    IGNORE;
172
 
                                                }
173
 
<LaMacro>"}"                                    BEGIN Normal;
174
 
<LaMacro>"\n"                                   NEWLINE;
175
 
<LaMacro>.                                      ;
176
 
<LaMacro2>"{"                                   {   cOpenBrace++; }
177
 
<LaMacro2>"}"                                   {   cOpenBrace--;
178
 
                                                    if (cOpenBrace == 0)
179
 
                                                        BEGIN Normal;
180
 
                                                }
181
 
<LaMacro2>"\n"                                  NEWLINE;
182
 
<LaMacro2>.                                     ;
183
 
 
184
 
<Normal>"\\def"         /* ignore def begin */  {BEGIN Define; IGNORE;}
185
 
<Define>"{"                                     BEGIN Normal;
186
 
<Define>"\n"                                    NEWLINE;
187
 
<Define>.                                       ;
188
 
 
189
 
<Normal>"\\("           /* formula mode */      {LaBEGIN LaFormula; IGNORE;}
190
 
<LaFormula>"\\)"                                BEGIN Normal;
191
 
<LaFormula>"\n"                                 NEWLINE;
192
 
<LaFormula>.                                    ;
193
 
 
194
 
<Normal>"\\["           /* display mode */      {LaBEGIN LaDisplay; IGNORE;}
195
 
<LaDisplay>"\\]"                                BEGIN Normal;
196
 
<LaDisplay>"\n"                                 NEWLINE;
197
 
<LaDisplay>.                                    ;
198
 
 
199
 
<Normal>"$$"            /* display mode */      {BEGIN Display; IGNORE;}
200
 
<Display>"$$"                                   BEGIN Normal;
201
 
<Display>"\n"                                   NEWLINE;
202
 
<Display>.                                      ;
203
 
 
204
 
<Normal>"$"             /* math mode */         {BEGIN Math; IGNORE;}
205
 
<Math>"$"                                       BEGIN Normal;
206
 
<Math>"\n"                                      NEWLINE;
207
 
<Math>"\\$"                                     ;
208
 
<Math>.                                         ;
209
 
 
210
 
<Normal>"\\include"     /* process files */     {LaBEGIN LaInclude; IGNORE;}
211
 
<LaInclude>[^{ \t\n}]+                          {   IncludeFile(yytext);
212
 
                                                    BEGIN Normal;
213
 
                                                }
214
 
<LaInclude>"\n"                                 NEWLINE;
215
 
<LaInclude>.                                    ;
216
 
 
217
 
<Normal>"\\includeonly"                         {BEGIN IncludeOnly; IGNORE;}
218
 
<IncludeOnly>[^{ \t,\n}]+                       AddInclude(yytext);
219
 
<IncludeOnly>"}"                                {   if (csbIncList == 0)
220
 
                                                        rgsbIncList[csbIncList++] = '\0';
221
 
                                                    BEGIN Normal;
222
 
                                                }
223
 
<IncludeOnly>"\n"                               NEWLINE;
224
 
<IncludeOnly>.                                  ;
225
 
 
226
 
<Normal>"\\input"                               {BEGIN Input; IGNORE;}
227
 
<Input>[^{ \t\n}]+                              {   InputFile(yytext);
228
 
                                                    BEGIN Normal;
229
 
                                                }
230
 
<Input>"\n"                                     NEWLINE;
231
 
<Input>.                                        ;
232
 
 
233
 
<Normal>\\(aa|AA|ae|AE|oe|OE|ss)[ \t]*[ \t\n}] /* handle ligatures */   {(void)printf("%.2s", yytext+1);}
234
 
<Normal>\\[OoijLl][ \t]*[ \t\n}]                {(void)printf("%.1s", yytext+1);}
235
 
 
236
 
<Normal>\\[a-zA-Z@]+    /* ignore other \cs */  {BEGIN Control; IGNORE;}
237
 
<Normal>"\\ "                                   SPACE;
238
 
<Normal>\\.                                     IGNORE;
239
 
<Control>\\[a-zA-Z@]+                           IGNORE;
240
 
<Control>[a-zA-Z@0-9]*[-'=`][^ \t\n{]*          IGNORE;
241
 
<Control>"\n"                                   {BEGIN Normal; NEWLINE;}
242
 
<Control>[ \t]*[{]*                             {BEGIN Normal; IGNORE;}
243
 
<Control>.                                      {yyless(0);BEGIN Normal;}
244
 
 
245
 
<Normal>[{}\\|] /* special characters */        IGNORE;
246
 
<Normal>[!?]"`"                                 IGNORE;
247
 
<Normal>~                                       SPACE;
248
 
 
249
 
<Normal>{W}[']*{W}                              {   if (fWord)
250
 
                                                        (void)printf("%s\n", yytext);
251
 
                                                    else
252
 
                                                        ECHO;
253
 
                                                }
254
 
<Normal>[0-9]+                                  if (!fWord) ECHO;
255
 
<Normal>(.|\n)                                  if (!fWord) ECHO;
256
 
%%
257
 
/******
258
 
** main --
259
 
**      Set sbProgName to the base of arg 0.
260
 
**      Set the input paths.
261
 
**      Check for options
262
 
**              -c              echo LaTeX \cite, \ref, and \pageref values
263
 
**              -e <env-list>   list of LaTeX environments to ignore
264
 
**              -l              force latex mode
265
 
**              -n              do not follow \input and \include
266
 
**              -s              replace control sequences with space
267
 
**              -t              force tex mode
268
 
**              -w              word only output
269
 
**      Set the list of LaTeX environments to ignore.
270
 
**      Process each input file.
271
 
**      If no input files are specified on the command line, process stdin.
272
 
******/
273
 
 
274
 
main(cArgs,rgsbArgs)
275
 
int     cArgs;
276
 
char    *rgsbArgs[];
277
 
{
278
 
        char    *pch, *sbEnvList = DEFAULTENV, sbBadOpt[2];
279
 
        FILE    *TexOpen();
280
 
        int     fSawFile = 0, iArgs = 1;
281
 
        
282
 
        /* get base name and decide what we are doing, detex or delatex */
283
 
#ifdef OS2
284
 
        char drive[_MAX_DRIVE], dir[_MAX_DIR];
285
 
        char fname[_MAX_FNAME], ext[_MAX_EXT];
286
 
#ifdef __EMX__
287
 
        _wildcard(&cArgs, &rgsbArgs);
288
 
        _response(&cArgs, &rgsbArgs);
289
 
#endif
290
 
        _splitpath (rgsbArgs[0], drive, dir, fname, ext);
291
 
        sbProgName = strlwr(fname);
292
 
#else
293
 
#ifdef KPATHSEA
294
 
        kpse_set_program_name (rgsbArgs[0], NULL);
295
 
#endif
296
 
        if ((sbProgName = rindex(rgsbArgs[0], '/')) != NULL)
297
 
            sbProgName++;
298
 
        else
299
 
            sbProgName = rgsbArgs[0];
300
 
#endif
301
 
        if (strcmp("delatex",sbProgName) == 0)
302
 
            fLatex = 1;
303
 
        
304
 
#ifndef KPATHSEA
305
 
        /* set rgsbInputPaths for use with TexOpen() */
306
 
        SetInputPaths();
307
 
#endif
308
 
 
309
 
        /* process command line options */
310
 
        while (iArgs < cArgs && *(pch = rgsbArgs[iArgs]) == CHOPT) {
311
 
                while (*++pch)
312
 
                    switch (*pch) {
313
 
                    case CHCITEOPT:
314
 
                        fCite = 1;
315
 
                        break;
316
 
                    case CHENVOPT:
317
 
                        sbEnvList = rgsbArgs[++iArgs];
318
 
                        break;
319
 
                    case CHLATEXOPT:
320
 
                        fLatex = 1;
321
 
                        break;
322
 
                    case CHNOFOLLOWOPT:
323
 
                        fFollow = 0;
324
 
                        break;
325
 
                    case CHSPACEOPT:
326
 
                        fSpace = 1;
327
 
                        break;
328
 
                    case CHTEXOPT:
329
 
                        fForcetex = 1;
330
 
                        break;
331
 
                    case CHWORDOPT:
332
 
                        fWord = 1;
333
 
                        break;
334
 
                    default:
335
 
#ifdef OS2
336
 
                        OS2UsageExit();
337
 
#else
338
 
                        sbBadOpt[0] = *pch;
339
 
                        sbBadOpt[1] = '\0';
340
 
                        Warning("unknown option ignored -", sbBadOpt);
341
 
#endif
342
 
                    }
343
 
                iArgs++;
344
 
        }
345
 
        SetEnvIgnore(sbEnvList);
346
 
 
347
 
        /* process input files */
348
 
        for (; iArgs < cArgs; iArgs++) {
349
 
            fSawFile++;
350
 
            if ((yyin = TexOpen(rgsbArgs[iArgs])) == NULL) {
351
 
                Warning("can't open file", rgsbArgs[iArgs]);
352
 
                continue;;
353
 
            }
354
 
            BEGIN Normal;
355
 
            (void)yylex();
356
 
        }
357
 
 
358
 
        /* if there were no input files, assume stdin */
359
 
        if (!fSawFile) {
360
 
            yyin = stdin;
361
 
#ifdef OS2
362
 
            if (isatty(fileno(stdin)))
363
 
                OS2UsageExit();
364
 
#endif
365
 
            BEGIN Normal;
366
 
            (void)yylex();
367
 
        }
368
 
#ifndef FLEX_SCANNER
369
 
        if (YYSTATE != Normal)
370
 
            ErrorExit("input contains an unterminated mode or environment");
371
 
#endif
372
 
        return(0);
373
 
}
374
 
 
375
 
#ifdef FLEX_SCANNER
376
 
#undef yywrap
377
 
#endif
378
 
 
379
 
/******
380
 
** yywrap -- handles EOF for lex.  Check to see if the stack of open files
381
 
**      has anything on it.  If it does, set yyin to the to value.  If not
382
 
**      return the termination signal for lex.
383
 
******/
384
 
 
385
 
yywrap()
386
 
{
387
 
        (void)fclose(yyin);
388
 
        if (cfp > 0) {
389
 
            yyin = rgfp[--cfp];
390
 
            return(0);
391
 
        }
392
 
        return(1);
393
 
}
394
 
 
395
 
#ifdef OS2
396
 
 
397
 
/******
398
 
** yyless -- return characters to the input stream.  Some systems don't have
399
 
**      a yyless routine
400
 
******/
401
 
 
402
 
void yyless(n)
403
 
int n;
404
 
{
405
 
        int     i = strlen(yytext);
406
 
 
407
 
        while (i > n) unput(yytext[--i]);
408
 
        yytext[yyleng = n] = '\0';
409
 
}
410
 
#endif
411
 
 
412
 
/******
413
 
** SetEnvIgnore -- sets rgsbEnvIgnore to the values indicated by the
414
 
**      sbEnvList.
415
 
******/
416
 
 
417
 
void
418
 
SetEnvIgnore(sbEnvList)
419
 
char    *sbEnvList;
420
 
{
421
 
        char *sb;
422
 
 
423
 
        sb = SafeMalloc(strlen(sbEnvList) + 1, "malloc for SetEnvIgnore failed");
424
 
        (void) strcpy(sb, sbEnvList);
425
 
        csbEnvIgnore = SeparateList(sb, rgsbEnvIgnore, CHENVSEP, MAXENVS);
426
 
        if (csbEnvIgnore == ERROR)
427
 
            ErrorExit("The environtment list contains too many environments");
428
 
}
429
 
 
430
 
/******
431
 
** BeginEnv -- checks to see if sbEnv is in the list rgsbEnvIgnore.  If it
432
 
**      is, sbCurrentEnv is set to sbEnv.
433
 
******/
434
 
 
435
 
BeginEnv(sbEnv)
436
 
char    *sbEnv;
437
 
{
438
 
        int     i;
439
 
 
440
 
        if (!fLatex) return(0);
441
 
        for (i = 0; i < csbEnvIgnore; i++)
442
 
            if (strcmp(sbEnv, rgsbEnvIgnore[i]) == 0) {
443
 
                (void)strcpy(sbCurrentEnv, sbEnv);
444
 
                return(1);
445
 
            }
446
 
        return(0);
447
 
}
448
 
 
449
 
/******
450
 
** EndEnv -- checks to see if sbEnv is the current environment being ignored.
451
 
******/
452
 
 
453
 
EndEnv(sbEnv)
454
 
char    *sbEnv;
455
 
{
456
 
        if (!fLatex) return(0);
457
 
        if (strcmp(sbEnv, sbCurrentEnv) == 0)
458
 
            return(1);
459
 
        return(0);
460
 
}
461
 
 
462
 
/******
463
 
** InputFile -- push the current yyin and open sbFile.  If the open fails,
464
 
**      the sbFile is ignored.
465
 
******/
466
 
 
467
 
void
468
 
InputFile(sbFile)
469
 
char    *sbFile;
470
 
{
471
 
        FILE    *TexOpen();
472
 
 
473
 
        if (!fFollow)
474
 
            return;
475
 
        rgfp[cfp++] = yyin;
476
 
        if ((yyin = TexOpen(sbFile)) == NULL) {
477
 
            Warning("can't open \\input file", sbFile);
478
 
            yyin = rgfp[--cfp];
479
 
        }
480
 
}
481
 
 
482
 
/******
483
 
** IncludeFile -- if sbFile is not in the rgsbIncList, push current yyin
484
 
**      and open sbFile.  If the open fails, the sbFile is ignored.
485
 
******/
486
 
 
487
 
void
488
 
IncludeFile(sbFile)
489
 
char    *sbFile;
490
 
{
491
 
        FILE    *TexOpen();
492
 
 
493
 
        if (!fFollow)
494
 
            return;
495
 
        if (!InList(sbFile))
496
 
            return;
497
 
        rgfp[cfp++] = yyin;
498
 
        if ((yyin = TexOpen(sbFile)) == NULL) {
499
 
            Warning("can't open \\include file", sbFile);
500
 
            yyin = rgfp[--cfp];
501
 
        }
502
 
}
503
 
 
504
 
/******
505
 
** AddInclude -- adds sbFile to the rgsbIncList and increments csbIncList.
506
 
**      If the include list is too long, sbFile is ignored.
507
 
******/
508
 
 
509
 
void
510
 
AddInclude(sbFile)
511
 
char    *sbFile;
512
 
{
513
 
        if (!fFollow)
514
 
            return;
515
 
        if (csbIncList >= MAXINCLIST)
516
 
            Warning("\\includeonly list is too long, ignoring", sbFile);
517
 
        rgsbIncList[csbIncList] = SafeMalloc(strlen(sbFile) + 1, "malloc for AddInclude failed");
518
 
        (void)strcpy(rgsbIncList[csbIncList++], sbFile);
519
 
}
520
 
 
521
 
/******
522
 
** InList -- checks to see if sbFile is in the rgsbIncList.  If there is
523
 
**      no list, all files are assumed to be "in the list".
524
 
******/
525
 
 
526
 
InList(sbFile)
527
 
char    *sbFile;
528
 
{
529
 
        char    *pch, sbBase[PATH_MAX];
530
 
        int     i;
531
 
 
532
 
        if (csbIncList == 0)    /* no list */
533
 
            return(1);
534
 
        (void)strcpy(sbBase, sbFile);
535
 
        if ((pch = rindex(sbBase, '.')) != NULL)
536
 
            *pch = '\0';
537
 
        i = 0;
538
 
        while ((i < csbIncList) && rgsbIncList[i])
539
 
            if (strcmp(rgsbIncList[i++], sbBase) == 0)
540
 
                return(1);
541
 
        return(0);
542
 
}
543
 
 
544
 
/******
545
 
** SetInputPaths -- sets rgsbInputPaths to the values indicated by the
546
 
**      TEXINPUTS environment variable if set or else DEFAULTINPUTS.  If
547
 
**      the user's TEXINPUTS has a leading ':' prepend the DEFAULTINPUTS
548
 
**      to the path, if there is a trailing ':' append the DEFAULTINPUTS.
549
 
**      This is consistent with the most recent TeX.  However, this
550
 
**      routine does not honor the '//' construct (expand subdirs).
551
 
******/
552
 
 
553
 
void
554
 
SetInputPaths()
555
 
{
556
 
        char *sb, *sbPaths;
557
 
#ifndef WIN32
558
 
        char *getenv();
559
 
#endif
560
 
        int cchDefaults, cchPaths;
561
 
 
562
 
        cchDefaults = strlen(DEFAULTINPUTS);
563
 
#ifdef OS2
564
 
        if ((sb = getenv("TEXINPUT")) == NULL)
565
 
#endif
566
 
            if ((sb = getenv("TEXINPUTS")) == NULL)
567
 
                sb = DEFAULTINPUTS;
568
 
        cchPaths = strlen(sb);
569
 
        if (sb[0] == CHPATHSEP)
570
 
            cchPaths += cchDefaults;
571
 
        if (sb[strlen(sb) - 1] == CHPATHSEP)
572
 
            cchPaths += cchDefaults;
573
 
        sbPaths = SafeMalloc(cchPaths + 1, "malloc for SetInputPaths failed");
574
 
        sbPaths[0] = '\0';
575
 
        if (sb[0] == CHPATHSEP)
576
 
            (void)strcat(sbPaths, DEFAULTINPUTS);
577
 
        (void)strcat(sbPaths, sb);
578
 
        if (sb[strlen(sb) - 1] == CHPATHSEP)
579
 
            (void)strcat(sbPaths, DEFAULTINPUTS);
580
 
 
581
 
        csbInputPaths = SeparateList(sbPaths, rgsbInputPaths, CHPATHSEP, MAXINPUTPATHS);
582
 
        if (csbInputPaths == ERROR)
583
 
#ifdef OS2
584
 
            ErrorExit("TEXINPUT(S) environment variable has too many paths");
585
 
#else
586
 
            ErrorExit("TEXINPUTS environment variable has too many paths");
587
 
#endif
588
 
}
589
 
 
590
 
/******
591
 
** SeparateList -- takes a chSep separated list sbList, replaces the
592
 
**      chSep's with NULLs and sets rgsbList[i] to the beginning of
593
 
**      the ith word in sbList.  The number of words is returned.  A
594
 
**      ERROR is returned if there are more than csbMax words.
595
 
******/
596
 
 
597
 
SeparateList(sbList, rgsbList, chSep, csbMax)
598
 
char    *sbList, *rgsbList[], chSep;
599
 
int     csbMax;
600
 
{
601
 
        int     csbList = 0;
602
 
 
603
 
        while (sbList && *sbList && csbList < csbMax) {
604
 
            rgsbList[csbList++] = sbList;
605
 
            if (sbList = index(sbList, chSep))
606
 
                *sbList++ = '\0';
607
 
        }
608
 
        return(sbList && *sbList ? ERROR : csbList);
609
 
}
610
 
 
611
 
/******
612
 
** TexOpen -- tries to open sbFile in each of the rgsbInputPaths in turn.
613
 
**      For each input path the following order is used:
614
 
**              file.tex - must be as named, if not there go to the next path
615
 
**              file.ext - random extension, try it
616
 
**              file     - base name, add .tex and try it
617
 
**              file     - try it as is
618
 
**      Notice that if file exists in the first path and file.tex exists in
619
 
**      one of the other paths, file in the first path is what is opened.
620
 
**      If the sbFile begins with a '/', no paths are searched.
621
 
******/
622
 
 
623
 
FILE *
624
 
TexOpen(sbFile)
625
 
char    *sbFile;
626
 
{
627
 
        char    *pch, *sbNew;
628
 
        FILE    *fp;
629
 
        int     iPath;
630
 
        static char     sbFullPath[PATH_MAX];
631
 
 
632
 
#ifndef KPATHSEA
633
 
        for (iPath = 0; iPath < csbInputPaths; iPath++) {
634
 
#ifdef OS2
635
 
            if (*sbFile == '/' || *sbFile == '\\' || strchr(sbFile, ':'))       /* absolute path */
636
 
#else
637
 
            if (*sbFile == '/') /* absolute path */
638
 
#endif
639
 
            {
640
 
                (void)sprintf(sbFullPath, "%s", sbFile);
641
 
                iPath = csbInputPaths;  /* only check once */
642
 
            } else
643
 
                (void)sprintf(sbFullPath, "%s/%s", rgsbInputPaths[iPath], sbFile);
644
 
#ifdef OS2
645
 
            pch = sbFullPath;
646
 
            while (pch = strchr(pch, '\\'))
647
 
                *pch = '/';
648
 
#endif
649
 
 
650
 
            /* If sbFile ends in .tex then it must be there */
651
 
            if ((pch = rindex(sbFullPath, '.')) != NULL
652
 
                        && (strcmp(pch, ".tex") == 0))
653
 
                if ((fp = fopen(sbFullPath, "r")) != NULL)
654
 
                    return(fp);
655
 
                else
656
 
                    continue;
657
 
 
658
 
            /* if .<ext> then try to open it.  the '.' represents   */
659
 
            /* the beginning of an extension if it is not the first */
660
 
            /* character and it does not follow a '.' or a '/'      */
661
 
            if (pch != NULL && pch > &(sbFullPath[0])
662
 
                    && *(pch - 1) != '.' && *(pch - 1) != '/'
663
 
                    && (fp = fopen(sbFullPath, "r")) != NULL)
664
 
                return(fp);
665
 
 
666
 
            /* just base name, add .tex to the name */
667
 
            sbNew = SafeMalloc(strlen(sbFullPath) + 5, "malloc for TexOpen failed");
668
 
            (void)strcpy(sbNew, sbFullPath);
669
 
            (void)strcat(sbNew, ".tex");
670
 
            if ((fp = fopen(sbNew, "r")) != NULL)
671
 
                return(fp);
672
 
 
673
 
            /* try sbFile regardless */
674
 
            if ((fp = fopen(sbFullPath, "r")) != NULL)
675
 
                return(fp);
676
 
        }
677
 
        return((FILE *)NULL);
678
 
#else
679
 
        sbNew = kpse_find_file (sbFile, kpse_tex_format, false);
680
 
 
681
 
        if (sbNew == NULL)
682
 
            return (FILE *)NULL;
683
 
 
684
 
        return fopen (sbNew, "r");
685
 
#endif
686
 
}
687
 
 
688
 
/******
689
 
** SafeMalloc -- wrapper around malloc() to check for failure.
690
 
******/
691
 
 
692
 
char *
693
 
SafeMalloc(cch, sbMessage)
694
 
int cch;
695
 
char *sbMessage;
696
 
{
697
 
        char *sb;
698
 
 
699
 
        if ((sb = (char *)malloc((unsigned)cch)) == NULL)
700
 
            ErrorExit(sbMessage);
701
 
        return(sb);
702
 
}
703
 
 
704
 
/******
705
 
** Warning -- print a warning message preceded by the program name.
706
 
******/
707
 
 
708
 
void
709
 
Warning(sb1, sb2)
710
 
char    *sb1, *sb2;
711
 
{
712
 
        (void)fprintf(stderr, "%s: warning: %s %s\n", sbProgName, sb1, sb2);
713
 
}
714
 
 
715
 
/******
716
 
** ErrorExit -- print an error message preceded by the program name.
717
 
**      Stdout is flushed and detex exits.
718
 
******/
719
 
 
720
 
ErrorExit(sb1)
721
 
char    *sb1;
722
 
{
723
 
        (void)fflush(stdout);
724
 
        (void)fprintf(stderr, "%s: error: %s\n", sbProgName, sb1);
725
 
        exit(1);
726
 
}
727
 
#ifdef OS2
728
 
 
729
 
/******
730
 
** OS2UsageExit -- print OS/2 usage message and exit.
731
 
******/
732
 
 
733
 
OS2UsageExit()
734
 
{
735
 
        (void)printf("\n%s [ -clnstw ] [ -e environment-list ] [ filename[.tex] ... ]\n",
736
 
                sbProgName);
737
 
        puts("  -c  echo LaTeX \\cite, \\ref, and \\pageref values\n  \
738
 
-e  <env-list> list of LaTeX environments to ignore\n  \
739
 
-l  force latex mode\n  \
740
 
-n  do not follow \\input and \\include\n  \
741
 
-s  replace control sequences with space\n  \
742
 
-t  force tex mode\n  \
743
 
-w  word only output");
744
 
        exit(0);
745
 
}
746
 
#endif