Debian Codegraph @

tex_read.c

raw symbols SCIP
1 /*** tex_read.c ***************************************************************
2 **
3 ** This file is part of BibTool.
4 ** It is distributed under the GNU General Public License.
5 ** See the file COPYING for details.
6 **
7 ** (c) 1996-2019 Gerd Neugebauer
8 **
9 ** Net: [email protected]
10 **
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License as published by
13 ** the Free Software Foundation; either version 2, or (at your option)
14 ** any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **
25 **
26 ** Purpose: Simulate the TeX reading mechanism.
27 **
28 ** Compilation: cc tex_read.c -o tex_read -DSTANDALONE
29 **
30 **-----------------------------------------------------------------------------
31 ** Description:
32 ** This module contains functions which immitate the reading
33 ** apparatus of \TeX{}. Macro expansion can be performed.
34 **
35 **
36 ******************************************************************************/
37
38 #include <bibtool/general.h>
39 #include <ctype.h>
40
41 #ifndef STANDALONE
42 #include <bibtool/error.h>
43 #include <bibtool/symbols.h>
44 #else
45 #define Err(X) (void)fputs(X,stderr)
46 #define ERROR_EXIT(X) Err(X); exit(1)
47 #define OUT_OF_MEMORY(X) Err("Out of memory for "); Err(X); exit(1)
48 #endif
49
50 /*****************************************************************************/
51 /* External Programs */
52 /*===========================================================================*/
53
54 #define CARET_CARET /* enable ^^A type specification of unprintables */
55 #undef TRANSLATE_NEWLINE /* Translate nl to space or \par */
56
57 typedef struct tOKEN /* */
58 { short int to_char; /* */
59 String to_s; /* */
60 struct tOKEN *to_next; /* */
61 } *Token, SToken; /* */
62
63 #define TokenChar(X) ((X)->to_char)
64 #define TokenSeq(X) ((X)->to_s)
65 #define NextToken(X) ((X)->to_next)
66
67 #define TokenNULL (Token)0
68
69 typedef struct mACdEF /* */
70 { short int md_arity; /* */
71 String md_name; /* */
72 Token md_token; /* */
73 struct mACdEF *md_next; /* */
74 } *MacDef, SMacDef; /* */
75
76 #define MacroArity(X) ((X)->md_arity)
77 #define MacroName(X) ((X)->md_name)
78 #define MacroToken(X) ((X)->md_token)
79 #define NextMacro(X) ((X)->md_next)
80 #define MacDefNULL (MacDef)0
81
82 /*****************************************************************************/
83 /* Internal Programs */
84 /*===========================================================================*/
85
86 #ifdef __STDC__
87 #define _ARG(A) A
88 #else
89 #define _ARG(A) ()
90 #endif
91 bool TeX_read _ARG((String cp,String *sp)); /* tex_read.c */
92 static MacDef find_macro _ARG((String name,MacDef md));/* tex_read.c */
93 static MacDef new_macdef _ARG((String name,int arity,Token tokens));/* tex_read.c*/
94 static Token TeX_get_token _ARG((int (*get_fct)_ARG((void))));/* tex_read.c*/
95 static Token new_token _ARG((int type,String string));/* tex_read.c */
96 static Token token_list_copy _ARG((Token t,Token nt,Token *argp));/* tex_read.c*/
97 static Token tokenize _ARG((String s,int arity)); /* tex_read.c */
98 static bool fill_token _ARG((Token*tp)); /* tex_read.c */
99 static int do_get _ARG((void)); /* tex_read.c */
100 static int TeX_fill_line _ARG((int (*get_fct)_ARG((void))));/* tex_read.c */
101 static int get_EOF _ARG((void)); /* tex_read.c */
102 static int get_file _ARG((void)); /* tex_read.c */
103 static int get_string _ARG((void)); /* tex_read.c */
104 static String tokens_to_string _ARG((Token t)); /* tex_read.c */
105 static void free_1_token _ARG((Token t)); /* tex_read.c */
106 static void free_macdef _ARG((MacDef mac)); /* tex_read.c */
107 static void free_tokens _ARG((Token t)); /* tex_read.c */
108 static void init_TeX _ARG((void)); /* tex_read.c */
109 static void init_get _ARG((String s)); /* tex_read.c */
110 void TeX_active _ARG((int c,int arity,String s));/* tex_read.c */
111 void TeX_close _ARG((void)); /* tex_read.c */
112 void TeX_def _ARG((String s)); /* tex_read.c */
113 void TeX_define _ARG((String name,int arity,String body));/* tex_read.c */
114 void TeX_open_file _ARG((FILE * file)); /* tex_read.c */
115 void TeX_open_string _ARG((String s)); /* tex_read.c */
116 void TeX_reset _ARG((void)); /* tex_read.c */
117
118 #ifdef STANDALONE
119 int main _ARG((int argc,char *argv[])); /* tex_read.c */
120 static char * new_string _ARG((char * s)); /* tex-read.c */
121 static void show_tokens _ARG((Token t)); /* tex-read.c */
122 #else
123 extern char * new_string _ARG((char * s));
124 #endif
125
126 /*---------------------------------------------------------------------------*/
127
128 /*****************************************************************************/
129 /*** catcode Management. ***/
130 /*****************************************************************************/
131
132 #define CHAR_ESCAPE '\\'
133 #define CHAR_BEG_GROUP '{'
134 #define CHAR_END_GROUP '}'
135 #define CHAR_MATH '$'
136 #define CHAR_ALIGN '&'
137 #define CHAR_EOL '\n'
138 #define CHAR_PARAMETER '#'
139 #define CHAR_SUPERSCRIPT '^'
140 #define CHAR_SUBSCRIPT '_'
141 #define CHAR_IGNORED '\0'
142 #define CHAR_SPACE ' '
143 #define CHAR_COMMENT '%'
144
145 #define CATCODE_ESCAPE 0x0000
146 #define CATCODE_BEG_GROUP 0x0100
147 #define CATCODE_END_GROUP 0x0200
148 #define CATCODE_MATH 0x0300
149 #define CATCODE_ALIGN 0x0400
150 #define CATCODE_EOL 0x0500
151 #define CATCODE_PARAMETER 0x0600
152 #define CATCODE_SUPERSCRIPT 0x0700
153 #define CATCODE_SUBSCRIPT 0x0800
154 #define CATCODE_IGNORED 0x0900
155 #define CATCODE_SPACE 0x0A00
156 #define CATCODE_LETTER 0x0B00
157 #define CATCODE_OTHER 0x0C00
158 #define CATCODE_ACTIVE 0x0D00
159 #define CATCODE_COMMENT 0x0E00
160 #define CATCODE_INVALID 0x0F00
161
162 #define CATCODE_MASK 0x0F00
163
164 static short int catcode[256];
165
166 static MacDef macro = MacDefNULL;
167 static MacDef active[256];
168
169 #define EnsureInit init_TeX()
170
171 /*-----------------------------------------------------------------------------
172 ** Function*: init_TeX()
173 ** Purpose: Initialize the \TeX{} reading apparatus.
174 ** Mainly the catcodes are assigned and the macros are
175 ** cleared. This function has to be called before the
176 ** other functions in this module. Thus this function is
177 ** called automatically from different places to ensure
178 ** that the reading apparatus is initialized.
179 ** Arguments: none
180 ** Returns: nothing
181 **___________________________________________________ */
182 static void init_TeX() /* */
183 { register int i; /* */
184 static bool initialized = false; /* */
185 /* */
186 if (initialized) return; /* */
187 initialized = true; /* */
188 /* */
189 for (i = 0; i < 256; ++i) /* */
190 { active[i] = MacDefNULL; /* */
191 catcode[i] = ( isalpha(i) /* */
192 ? CATCODE_LETTER /* */
193 : CATCODE_OTHER ); /* */
194 } /* */
195 catcode[CHAR_ESCAPE] = CATCODE_ESCAPE; /* */
196 catcode[CHAR_BEG_GROUP] = CATCODE_BEG_GROUP; /* */
197 catcode[CHAR_END_GROUP] = CATCODE_END_GROUP; /* */
198 catcode[CHAR_MATH] = CATCODE_MATH; /* */
199 catcode[CHAR_ALIGN] = CATCODE_ALIGN; /* */
200 catcode[CHAR_EOL] = CATCODE_EOL; /* */
201 catcode[CHAR_PARAMETER] = CATCODE_PARAMETER; /* */
202 catcode[CHAR_SUPERSCRIPT] = CATCODE_SUPERSCRIPT; /* */
203 catcode[CHAR_SUBSCRIPT] = CATCODE_SUBSCRIPT; /* */
204 catcode[CHAR_IGNORED] = CATCODE_IGNORED; /* */
205 catcode[CHAR_SPACE] = CATCODE_SPACE; /* */
206 catcode[CHAR_COMMENT] = CATCODE_COMMENT; /* */
207 catcode['~'] = CATCODE_ACTIVE; /* */
208 } /*------------------------*/
209
210 /*****************************************************************************/
211 /*** Token Management. ***/
212 /*****************************************************************************/
213
214 static Token token_free_list = TokenNULL;
215
216 /*-----------------------------------------------------------------------------
217 ** Function*: new_token()
218 ** Purpose: Allocate a new token cell and fill it with values.
219 ** Arguments:
220 ** type the type
221 ** string the strign value
222 ** Returns: the new token
223 **___________________________________________________ */
224 static Token new_token(type, string) /* */
225 int type; /* */
226 String string; /* */
227 { Token new; /* */
228 /* */
229 if ( token_free_list != TokenNULL ) /* */
230 { new = token_free_list; /* */
231 token_free_list = NextToken(token_free_list); /* */
232 } /* */
233 else if ( (new=(Token)malloc(sizeof(SToken))) == TokenNULL )/* */
234 { OUT_OF_MEMORY("TeX token."); } /* */
235 /* */
236 TokenChar(new) = type; /* */
237 TokenSeq(new) = string; /* */
238 NextToken(new) = TokenNULL; /* */
239 return new; /* */
240 } /*------------------------*/
241
242 #define CopyToken(t) new_token(TokenChar(t), \
243 TokenSeq(t) ? newString(TokenSeq(t)) : NULL)
244
245 #define NewToken(C) new_token(C, StringNULL)
246
247 /*-----------------------------------------------------------------------------
248 ** Function*: token_list_copy()
249 ** Purpose: Copy a list of tokens.
250 ** Arguments:
251 ** t
252 ** nt
253 ** argp
254 ** Returns:
255 **___________________________________________________ */
256 static Token token_list_copy(t,nt,argp) /* */
257 register Token t; /* */
258 Token nt; /* */
259 Token *argp; /* */
260 { register Token a,p; /* */
261 int i; /* */
262 /* */
263 if (t == TokenNULL) return TokenNULL; /* */
264 if ((i=TokenChar(t)) > 0xff) /* */
265 { a = p = token_list_copy(argp[i>>8], /* */
266 TokenNULL, /* */
267 argp); /* */
268 while (NextToken(p) != TokenNULL) /* */
269 { p = NextToken(p); } /* */
270 } /* */
271 else { a = p = CopyToken(t); } /* */
272 /* */
273 while ( (t=NextToken(t)) != TokenNULL ) /* */
274 { /* */
275 if ( (i=TokenChar(t)) > 0xff ) /* */
276 { NextToken(p) = token_list_copy(argp[i>>8], /* */
277 TokenNULL, /* */
278 argp); /* */
279 while ( NextToken(p) != TokenNULL ) /* */
280 { p = NextToken(p); } /* */
281 } /* */
282 else /* */
283 { NextToken(p) = CopyToken(t); /* */
284 p = NextToken(p); /* */
285 } /* */
286 } /* */
287 NextToken(p) = nt; /* */
288 return a; /* */
289 } /*------------------------*/
290
291 /*-----------------------------------------------------------------------------
292 ** Function*: free_1_token()
293 ** Purpose: Free a single Token.
294 ** Free the string and place it in the free list.
295 ** Arguments:
296 ** t
297 ** Returns: nothing
298 **___________________________________________________ */
299 static void free_1_token(t) /* */
300 register Token t; /* */
301 { NextToken(t) = token_free_list; /* */
302 token_free_list = t; /* */
303 if ( TokenSeq(t) ) free(TokenSeq(t)); /* */
304 } /*------------------------*/
305
306 /*-----------------------------------------------------------------------------
307 ** Function*: free_tokens()
308 ** Purpose: Free a list of Tokens.
309 ** Walk along to the end and free the strings on the way.
310 ** Link it into the free list.
311 ** Arguments:
312 ** t
313 ** Returns: nothing
314 **___________________________________________________ */
315 static void free_tokens(t) /* */
316 register Token t; /* */
317 { register Token t0; /* */
318 /* */
319 for ( t0 = t; /* */
320 NextToken(t0) != TokenNULL; /* */
321 t0 = NextToken(t0) ) /* */
322 { if ( TokenSeq(t0) ) free(TokenSeq(t0)); } /* */
323 /* */
324 if ( TokenSeq(t0) ) free(TokenSeq(t0)); /* */
325 NextToken(t0) = token_free_list; /* */
326 token_free_list = t; /* */
327 } /*------------------------*/
328
329 /*-----------------------------------------------------------------------------
330 ** Function*: tokens_to_string()
331 ** Purpose: Make a string out of a list of tokens.
332 ** Arguments:
333 ** t
334 ** Returns:
335 **___________________________________________________ */
336 static String tokens_to_string(t) /* */
337 Token t; /* */
338 { register Token t1; /* */
339 register String s, /* */
340 sp; /* */
341 register int len; /* */
342 /* */
343 for (len = 1, t1 = t; /* Count the elements of */
344 t1 != TokenNULL; /* the token list. */
345 t1 = NextToken(t1)) /* (+1 for '\0') */
346 { ++len; } /* */
347 /* */
348 if ( (s=(String)malloc(len * sizeof(Uchar))) /* */
349 == NULL ) /* Try to get */
350 { OUT_OF_MEMORY("TeX string."); } /* new memory. */
351 /* */
352 for (sp = s, t1 = t; /* Transfer the characters*/
353 t1 != TokenNULL; /* from the token list */
354 t1 = NextToken(t1)) /* to the string. */
355 { *(sp++) = TokenChar(t1); } /* */
356 *sp = '\0'; /* */
357 return s; /* Return the string. */
358 } /*------------------------*/
359
360 /*****************************************************************************/
361 /*** Reading Apparatus. ***/
362 /*****************************************************************************/
363
364 #define StateN 0
365 #define StateM 1
366 #define StateS 2
367
368 static int tex_state = StateN; /* */
369 static Token tex_line = TokenNULL; /* */
370
371 #define tex_clear tex_state = StateN; tex_line = TokenNULL
372
373 /*-----------------------------------------------------------------------------
374 ** Function*: TeX_fill_line()
375 ** Purpose:
376 **
377 **
378 ** Arguments:
379 ** get_fct
380 ** Returns:
381 **___________________________________________________ */
382 static int TeX_fill_line(get_fct) /* */
383 int (*get_fct)_ARG((void)); /* function * to get char */
384 { register int c; /* */
385 register Token t; /* */
386 register int spaces = 0; /* */
387 /* */
388 if ( (c=(*get_fct)()) == EOF ) return 1; /* */
389 tex_line = t = NewToken(c); /* */
390 /* */
391 while ( (c=(*get_fct)()) != EOF && c != '\n' ) /* */
392 { if ( isspace(c) ) { ++spaces; } /* Spaces are delayed. */
393 else /* */
394 { while ( spaces > 0 ) /* */
395 { NextToken(t) = NewToken(CHAR_SPACE); /* */
396 t = NextToken(t); /* */
397 spaces--; /* */
398 } /* */
399 NextToken(t) = NewToken(c); /* */
400 t = NextToken(t); /* */
401 } /* */
402 } /* */
403 if ( TokenChar(t) == CHAR_SPACE ) TokenChar(t) = CHAR_EOL;/* */
404 else NextToken(t) = NewToken(CHAR_EOL); /* */
405 /* */
406 #ifdef CARET_CARET
407 { register Token *tp; /* */
408 for ( tp= &tex_line; /* Loop over the read */
409 *tp!=TokenNULL; /* line. */
410 tp= &NextToken(*tp) ) /* */
411 { t = NextToken(*tp); /* */
412 if ( TokenChar(*tp) == '^' /* Translate ^^. */
413 && TokenChar(t=NextToken(*tp)) == '^' /* to single characters */
414 && (c=TokenChar(NextToken(t))) != '\n' ) /* */
415 { NextToken(*tp) = NextToken(NextToken(t)); /* */
416 free_1_token(NextToken(t)); /* */
417 free_1_token(t); /* */
418 TokenChar(*tp) = (c >= 64 ? c - 64 : c + 64);/* */
419 } /* */
420 } /* */
421 } /* */
422 #endif
423 /* */
424 return 0; /* */
425 } /*------------------------*/
426
427 /*-----------------------------------------------------------------------------
428 ** Function: TeX_get_token()
429 ** Purpose: Get characters and pack them into a Token structure.
430 ** The argument is a function called to get the next character.
431 ** It returns the next character of |EOF| if no more can be read.
432 ** Arguments:
433 ** get_fct the function to acquire a character
434 ** Returns: the token read
435 **___________________________________________________ */
436 static Token TeX_get_token(get_fct) /* */
437 int (*get_fct)_ARG((void)); /* function * to get char */
438 { register Token t, t1, t2; /* */
439 /* */
440 for (;;) /* */
441 { /* */
442 if ( tex_line == TokenNULL /* */
443 && TeX_fill_line(get_fct) ) /* */
444 { return TokenNULL; } /* */
445 /* */
446 t = tex_line; /* */
447 tex_line = NextToken(t); /* */
448 NextToken(t) = TokenNULL; /* */
449 /* */
450 switch ( TokenChar(t) ) /* */
451 { case CHAR_IGNORED: /* */
452 break; /* */
453 #ifdef TRANSLATE_NEWLINE
454 case CHAR_EOL: /* */
455 switch (tex_state) /* */
456 { case StateN: /* */
457 TokenChar(t) = CHAR_ESCAPE; /* */
458 TokenSeq(t) = newString("par"); /* */
459 return t; /* */
460 case StateM: /* */
461 TokenChar(t) = CHAR_SPACE; /* */
462 return t; /* */
463 } /* */
464 #endif
465 case CHAR_COMMENT: /* */
466 free_tokens(tex_line); /* */
467 tex_line = TokenNULL; /* */
468 break; /* */
469 case CHAR_SPACE: /* */
470 if ( tex_state == StateM ) /* */
471 { tex_state = StateS; /* */
472 return t; /* */
473 } /* */
474 break; /* */
475 case CHAR_ESCAPE: /* */
476 t1 = tex_line; /* */
477 if ( isalpha(TokenChar(tex_line)) ) /* */
478 { while ( isalpha(TokenChar(NextToken(tex_line))) )/* */
479 { tex_line = NextToken(tex_line); } /* */
480 } /* */
481 else if ( TokenChar(tex_line) == CHAR_EOL )/* */
482 { tex_state = StateM; /* */
483 TokenSeq(t) = newString(""); /* */
484 return t; /* */
485 } /* */
486 t2 = tex_line; /* */
487 tex_line = NextToken(tex_line); /* */
488 NextToken(t2) = TokenNULL; /* */
489 TokenSeq(t) = tokens_to_string(t1); /* */
490 free_tokens(t1); /* */
491 tex_state = StateM; /* */
492 return t; /* */
493 /* */
494 default: /* */
495 tex_state = StateM; /* */
496 return t; /* */
497 } /* */
498 free_1_token(t); /* */
499 } /* */
500 } /*------------------------*/
501
502 static String g_s; /* */
503 static String g_p; /* */
504
505 /*-----------------------------------------------------------------------------
506 ** Function: init_get()
507 ** Purpose:
508 **
509 **
510 ** Arguments:
511 ** s the string to read from
512 ** Returns: nothing
513 **___________________________________________________ */
514 static void init_get(s) /* */
515 register String s; /* */
516 { g_p = g_s = s; /* */
517 } /*------------------------*/
518
519 /*-----------------------------------------------------------------------------
520 ** Function: do_get()
521 ** Purpose:
522 **
523 **
524 ** Arguments: none
525 ** Returns:
526 **___________________________________________________ */
527 static int do_get() /* */
528 { return (*g_p == '\0' ? EOF : *(g_p++)); /* */
529 } /*------------------------*/
530
531 /*-----------------------------------------------------------------------------
532 ** Function: tokenize()
533 ** Purpose:
534 **
535 **
536 ** Arguments:
537 ** s
538 ** arity
539 ** Returns:
540 **___________________________________________________ */
541 static Token tokenize(s, arity) /* */
542 String s; /* */
543 int arity; /* */
544 { Token t = TokenNULL, /* */
545 t_ret = TokenNULL, /* */
546 nt, t0; /* */
547 int a; /* */
548 /* */
549 EnsureInit; /* */
550 init_get(s); /* */
551 /* */
552 while ( (nt=TeX_get_token(do_get)) != TokenNULL )/* */
553 { if ( TokenChar(nt) =='\n' ) /* */
554 { free_1_token(nt); } /* */
555 else /* */
556 { if ( TokenChar(nt) == CHAR_PARAMETER /* */
557 && (t0=TeX_get_token(do_get)) != TokenNULL )/* */
558 { a = TokenChar(t0) - '0'; /* */
559 if ( 0<a && a<=arity ) TokenChar(nt) |= a<<8;/* */
560 else { Err("*** Argument error.\n"); } /* */
561 free_1_token(t0); /* */
562 } /* */
563 if ( t_ret == TokenNULL ) { t_ret = t = nt; }/* */
564 else { NextToken(t) = nt; t = nt; } /* */
565 } /* */
566 } /* */
567 return t_ret; /* */
568 } /*------------------------*/
569
570 /*-----------------------------------------------------------------------------
571 ** Function: new_macdef()
572 ** Purpose: Allocate a new macro definition.
573 **
574 ** Arguments:
575 ** name the name
576 ** arity the arity
577 ** tokens the tokens
578 ** Returns:
579 **___________________________________________________ */
580 static MacDef new_macdef(name, arity, tokens) /* */
581 String name; /* */
582 int arity; /* */
583 Token tokens; /* */
584 { MacDef new; /* */
585 /* */
586 if ( (new=(MacDef)malloc(sizeof(SMacDef))) == NULL )/* */
587 { OUT_OF_MEMORY("TeX macro."); } /* */
588 /* */
589 MacroName(new) = name; /* */
590 MacroArity(new) = arity; /* */
591 MacroToken(new) = tokens; /* */
592 NextMacro(new) = MacDefNULL; /* */
593 return new; /* */
594 } /*------------------------*/
595
596 /*-----------------------------------------------------------------------------
597 ** Function: free_macdef()
598 ** Purpose:
599 **
600 **
601 ** Arguments:
602 ** mac
603 ** Returns: nothing
604 **___________________________________________________ */
605 static void free_macdef(mac) /* */
606 MacDef mac; /* */
607 { /* */
608 if ( mac == MacDefNULL ) return; /* */
609 free(MacroToken(mac)); /* */
610 free(mac); /* */
611 } /*------------------------*/
612
613 /*-----------------------------------------------------------------------------
614 ** Function: find_macro()
615 ** Purpose:
616 **
617 **
618 ** Arguments:
619 ** name the name of the macro
620 ** md the macro to start searching at
621 ** Returns:
622 **___________________________________________________ */
623 static MacDef find_macro(name, md) /* */
624 register String name; /* */
625 register MacDef md; /* */
626 { /* */
627 for ( ; md != MacDefNULL; md = NextMacro(md) ) /* */
628 { if ( strcmp((char*)MacroName(md), /* */
629 (char*)name) == 0 ) /* */
630 return md; /* */
631 } /* */
632 return MacDefNULL; /* */
633 } /*------------------------*/
634
635 /*-----------------------------------------------------------------------------
636 ** Function: TeX_define()
637 ** Purpose: Add a new \TeX{} macro definition.
638 **
639 **
640 ** Arguments:
641 ** name
642 ** arity
643 ** body
644 ** Returns: nothing
645 **___________________________________________________ */
646 void TeX_define(name, arity, body) /* */
647 String name; /* */
648 int arity; /* */
649 String body; /* */
650 { register MacDef md; /* */
651 /* */
652 if ( 0 > arity || arity > 9 ) return; /* */
653 /* */
654 md = new_macdef(name, /* */
655 arity, /* */
656 tokenize(body,arity));/* */
657 NextMacro(md) = macro; /* */
658 macro = md; /* */
659 } /*------------------------*/
660
661 /*-----------------------------------------------------------------------------
662 ** Function: TeX_def()
663 ** Purpose: Define a macro.
664 ** The argument is a string specification of the following form:
665 **
666 ** |\name[arity]=replacement text|\\
667 ** |\name=replacement text|
668 **
669 ** \(0 <= arity <= 9\)
670 ** Arguments:
671 ** s
672 ** Returns: nothing
673 **___________________________________________________ */
674 void TeX_def(s) /* */
675 String s; /* */
676 { String name, /* */
677 ep = NULL; /* */
678 int arity = 0; /* */
679 /* */
680 while( isspace(*s) ) s++; /* */
681 name = s; /* */
682 while ( *s && *s != '=' ) /* */
683 { if ( *s == '[' ) /* */
684 { arity = *(s+1) - '0'; ep = s; } /* */
685 ++s; /* */
686 } /* */
687 /* */
688 if ( ep == NULL ) ep = s; /* */
689 /* */
690 if ( *name == '\\' ) /* */
691 { char c; /* */
692 while (ep > name + 2 && /* */
693 (*(ep-1) == ' ' || *(ep-1) =='\t')) /* */
694 { ep--; } /* */
695 c = *ep; *ep = '\0'; /* */
696 TeX_define(newString(name + 1), /* */
697 arity, /* */
698 newString(s + 1)); /* */
699 *ep = c; /* */
700 } /* */
701 else /* */
702 { TeX_active((unsigned int)*name, /* */
703 arity, /* */
704 newString(s+1)); /* */
705 } /* */
706 } /*------------------------*/
707
708 /*-----------------------------------------------------------------------------
709 ** Function: TeX_active()
710 ** Purpose: Assign a macro to an active character. If the
711 ** character is not active then the catcode is changed.
712 ** Arguments:
713 ** c Character to make active.
714 ** arity Arity of the macro assigned to the active character.
715 ** s Body of the definition as string.
716 ** Returns: nothing
717 **___________________________________________________ */
718 void TeX_active(c, arity, s) /* */
719 int c; /* */
720 int arity; /* */
721 String s; /* */
722 { /* */
723 EnsureInit; /* */
724 if ( active[c] ) free_macdef(active[c]); /* */
725 /* */
726 active[c] = new_macdef(StringNULL, /* */
727 arity, /* */
728 tokenize(s,arity)); /* */
729 } /*------------------------*/
730
731 /*-----------------------------------------------------------------------------
732 ** Function: TeX_reset()
733 ** Purpose: Reset the \TeX{} reading apparatus to its initial
734 ** state. All macros and active characters are cleared
735 ** and the memory is released. Thus this function can
736 ** also be used for this purpose.
737 ** Arguments: none
738 ** Returns: nothing
739 **___________________________________________________ */
740 void TeX_reset() /* */
741 { int i; /* */
742 MacDef md, next; /* */
743 /* */
744 for (i = 0; i < 256; i++) /* */
745 { free_macdef(active[i]); } /* */
746 /* */
747 md = macro; /* */
748 macro = MacDefNULL; /* */
749 while ( md ) /* */
750 { next = NextMacro(md); /* */
751 free_macdef(md); /* */
752 md = next; /* */
753 } /* */
754 } /*------------------------*/
755
756 /*-----------------------------------------------------------------------------
757 ** Function: get_EOF()
758 ** Purpose: Character getter function to indicate an empty stream.
759 ** Arguments: none
760 ** Returns: |EOF| in any case
761 **___________________________________________________ */
762 static int get_EOF() /* */
763 { return EOF; /* */
764 } /*------------------------*/
765
766 static FILE *src_file; /* */
767 static String src_string; /* */
768 static String src_ptr; /* */
769 static int (*src_get)() = get_EOF; /* */
770
771 /*-----------------------------------------------------------------------------
772 ** Function: get_string()
773 ** Purpose: Character getter function to access the stored string..
774 ** Arguments: none
775 ** Returns: the next character or |EOF| in case of end-of-string
776 **___________________________________________________ */
777 static int get_string() /* */
778 { return (*src_ptr ? *(src_ptr++) : EOF); /* */
779 } /*------------------------*/
780
781 /*-----------------------------------------------------------------------------
782 ** Function: get_file()
783 ** Purpose:
784 **
785 **
786 ** Arguments:
787 **
788 ** Returns:
789 **___________________________________________________ */
790 static int get_file() /* */
791 { return getc(src_file); /* */
792 } /*------------------------*/
793
794 /*-----------------------------------------------------------------------------
795 ** Function: TeX_open_file()
796 ** Purpose: Prepare things to parse from a file.
797 ** Arguments:
798 ** file File pointer of the file to read from.
799 ** Returns: nothing
800 **___________________________________________________ */
801 void TeX_open_file(file) /* */
802 FILE * file; /* */
803 { src_file = file; /* */
804 src_get = get_file; /* */
805 tex_clear; /* */
806 } /*------------------------*/
807
808 /*-----------------------------------------------------------------------------
809 ** Function: TeX_open_string()
810 ** Purpose: Prepare things to parse from a string.
811 ** Arguments:
812 ** s String to read from.
813 ** Returns: nothing
814 **___________________________________________________ */
815 void TeX_open_string(s) /* */
816 String s; /* */
817 { src_ptr = src_string = s; /* */
818 src_get = get_string; /* */
819 tex_clear; /* */
820 } /*------------------------*/
821
822 /*-----------------------------------------------------------------------------
823 ** Function: TeX_close()
824 ** Purpose: Gracefully terminate the reading of \TeX{} tokens. Any
825 ** remaining pieces of text which have already been
826 ** consumed are discarted.
827 ** Arguments: none
828 ** Returns: nothing
829 **___________________________________________________ */
830 void TeX_close() /* */
831 { Uchar c; /* */
832 String s; /* */
833 src_get = get_EOF; /* */
834 while (TeX_read(&c, &s)) ; /* */
835 } /*------------------------*/
836
837 /*-----------------------------------------------------------------------------
838 ** Function: fill_token()
839 ** Purpose: Check if *tp contains a token.
840 ** Otherwise try to get one and store it there.
841 ** Arguments:
842 ** tp the pointer to the token
843 ** Returns:
844 **___________________________________________________ */
845 static bool fill_token(tp) /* */
846 register Token *tp; /* */
847 { /* */
848 if ( *tp == TokenNULL /* */
849 && (*tp=TeX_get_token(src_get)) == TokenNULL)/* */
850 { return false; } /* */
851 return true; /* */
852 } /*------------------------*/
853
854 #define UnlinkToken(T,T2) T2 = T; T = NextToken(T)
855 #define UnlinkAndFreeToken(T,T2) UnlinkToken(T,T2); free_1_token(T2)
856
857 /*-----------------------------------------------------------------------------
858 ** Function: TeX_read()
859 ** Purpose: Read a single Token and return it as a pair consisting
860 ** of an ASCII code and possibly a string in case of a
861 ** macro token.
862 ** Arguments:
863 ** cp Pointer to position where the character is stored.
864 ** sp Pointer to position where the string is stored.
865 ** Returns: |false| iff everything went right.
866 **___________________________________________________ */
867 bool TeX_read(cp, sp) /* */
868 String cp; /* */
869 String *sp; /* */
870 { static Token t = TokenNULL; /* */
871 static Token old_t = TokenNULL; /* */
872 static Token arg[10]; /* */
873 Token t2, tp; /* */
874 int i, d; /* */
875 /* */
876 EnsureInit; /* */
877 if ( old_t != TokenNULL ) free_1_token(old_t); /* */
878 /* */
879 while ( fill_token(&t) ) /* One token left or */
880 { /* I got a new one. */
881 MacDef mac = active[TokenChar(t)]; /* */
882 if ( mac != MacDefNULL ) /* Is it active? */
883 { UnlinkAndFreeToken(t, t2); /* Delete active token */
884 } /* */
885 else if ( TokenChar(t) == CHAR_ESCAPE && /* */
886 (mac=find_macro(TokenSeq(t),macro)) /* or an undefined */
887 != MacDefNULL ) /* */
888 { UnlinkAndFreeToken(t,t2); /* Delete macro name token*/
889 while ( fill_token(&t) /* While there are more */
890 && TokenChar(t) == CHAR_SPACE) /* tokens and == ' ' */
891 { UnlinkAndFreeToken(t,t2); } /* unlink space */
892 } /* */
893 else /* */
894 { *cp = TokenChar(t); /* */
895 *sp = TokenSeq(t); /* */
896 UnlinkToken(t,old_t); /* */
897 return true; /* */
898 } /* */
899 /* */
900 for (i = 1; i <= MacroArity(mac); ++i) /* Fill the argument */
901 { /* vector */
902 if ( !fill_token(&t) ) /* */
903 { arg[i] = TokenNULL; /* Not enough tokens */
904 Err("*** Unexpected EOF\n"); /* */
905 } /* */
906 else if (TokenChar(t) == CHAR_BEG_GROUP) /* If there is a group */
907 { tp = t; /* */
908 d = 0; /* */
909 while ( fill_token(&NextToken(tp)) && /* While there are more */
910 (TokenChar(NextToken(tp)) /* tokens */
911 != CHAR_END_GROUP /* and */
912 || d != 0 ) ) /* no matching '}' */
913 { tp = NextToken(tp); /* walk ahead and */
914 switch ( TokenChar(tp) ) /* count braces */
915 { case CHAR_BEG_GROUP: ++d; break; /* */
916 case CHAR_END_GROUP: d--; break; /* */
917 } /* */
918 } /* */
919 t2 = NextToken(NextToken(tp)); /* */
920 free_1_token(NextToken(tp)); /* remove final '}' */
921 NextToken(tp) = TokenNULL; /* */
922 tp = t; t = t2; /* */
923 UnlinkAndFreeToken(tp,t2); /* remove initial '{' */
924 arg[i] = tp; /* */
925 } /* */
926 else /* */
927 { arg[i] = t; /* Move one token */
928 t = NextToken(t); /* to argument vector */
929 NextToken(arg[i]) = TokenNULL; /* */
930 } /* */
931 } /* */
932 /* */
933 t = token_list_copy(MacroToken(mac),t,arg); /* */
934 /* */
935 for (i = 1; i < MacroArity(mac); ++i) /* free the arg vector */
936 { if ( arg[i] != TokenNULL ) /* */
937 { free_tokens(arg[i]); /* */
938 arg[i] = TokenNULL; /* */
939 } /* */
940 } /* */
941 } /* */
942 old_t = TokenNULL; /* */
943 return false; /* */
944 } /*------------------------*/
945
946
947 /*****************************************************************************/
948 /*** Stand alone version ***/
949 /*****************************************************************************/
950 #ifdef STANDALONE
951
952 /*-----------------------------------------------------------------------------
953 ** Function*: new_string()
954 ** Purpose: Allocate a space for a string and copy the argument
955 ** there. Note this is just a new copy of the memory not
956 ** a symbol!
957 **
958 ** If no more memory is available then an error is raised
959 ** and the program is terminated.
960 ** Arguments:
961 ** s String to duplicate
962 ** Returns: Pointer to newly allocated memory containing a
963 ** duplicate of the argument string.
964 **___________________________________________________ */
965 static char * new_string(s) /* */
966 register char * s; /* */
967 { register char * t; /* */
968 if ( (t=malloc(strlen(s)+1)) == NULL ) /* */
969 { OUT_OF_MEMORY("string"); } /* */
970 (void)strcpy(t,s); return(t); /* */
971 } /*------------------------*/
972
973 /*-----------------------------------------------------------------------------
974 ** Function*: main()
975 ** Purpose: Main routine for the stand alone version.
976 ** Arguments:
977 ** argc Number of arguments (+1)
978 ** argv Array of command line arguments (and program name)
979 ** Returns:
980 **___________________________________________________ */
981 int main(argc,argv) /* */
982 int argc; /* */
983 char *argv[]; /* */
984 { FILE *file; /* */
985 int kept = 0; /* */
986 char buffer[1024]; /* */
987 char c; /* */
988 char *s; /* */
989 /* */
990 /* */
991 if ( argc > 3 ) /* */
992 { Err("\nTeX macro expander. gene 1/95\n\n");
993 Err("Usage: tex_read [macro_file [input_file]]\n\n");
994 Err("\tRead the macros and expand them in the input file\n");
995 Err("\tafterwards. Comments are also eliminated.\n\n");
996 Err("\tThe macros are made up of lines of the following form:\n");
997 Err("\t\tmacname[args]=replacement text\n");
998 Err("\twhere 0<=args<=9. If args=0 then [0] can be omitted.\n");
999 Err("\treplacement text may also contain macros which are expanded.\n\n");
1000 Err("\tThe program mimics the reading mechanism of TeX.\n");
1001 Err("\t\n");
1002 return; /* */
1003 } /* */
1004 if ( argc > 1 ) /* */
1005 { if ( (file = fopen(argv[1],"r")) == NULL ) /* */
1006 { ERROR_EXIT("File open error"); } /* */
1007 while ( fgets(buffer,1024,file) ) /* */
1008 { for(s=buffer;*s;++s) if(*s=='\n') *s = '\0'; /* */
1009 for(s=buffer;*s&&isspace(*s);++s) ; /* */
1010 if ( *s == '\\' ) TeX_def(s); /* */
1011 } /* */
1012 TeX_close(); /* */
1013 (void)fclose(file); /* */
1014 } /* */
1015 file = stdin; /* */
1016 if ( argc > 2 && /* */
1017 (file = fopen(argv[2],"r")) == NULL ) /* */
1018 { ERROR_EXIT("File open error"); } /* */
1019 /* */
1020 TeX_open_file(file); /* */
1021 /* */
1022 while ( TeX_read(&c,&s) ) /* */
1023 { if ( kept ) /* */
1024 { if ( isalpha(c) ) (void)putchar(' '); /* */
1025 kept = 0; /* */
1026 } /* */
1027 (void)putchar(c); /* */
1028 if ( s ) /* */
1029 { (void)fputs(s,stdout); /* */
1030 kept = isalpha(*s); /* */
1031 } /* */
1032 } /* */
1033 /* */
1034 TeX_close(); /* */
1035 /* */
1036 if ( file != stdin ) (void)fclose(file); /* */
1037 return 0; /* */
1038 } /*------------------------*/
1039
1040 #endif /*STANDALONE*/
1041
1042 #ifdef DEBUG
1043 /*-----------------------------------------------------------------------------
1044 ** Function*: show_tokens()
1045 ** Purpose:
1046 **
1047 **
1048 ** Arguments:
1049 ** t the token
1050 ** Returns: nothing
1051 **___________________________________________________ */
1052 static void show_tokens(t) /* */
1053 register Token t; /* */
1054 { /* */
1055 while ( t != TokenNULL ) /* */
1056 { if ( TokenChar(t) == CHAR_ESCAPE ) /* */
1057 { (void)printf("_\\%s",TokenSeq(t)); } /* */
1058 else if ( TokenChar(t) > 0xff ) /* */
1059 { (void)printf("_#%d",TokenChar(t)>>8); /* */
1060 } /* */
1061 else /* */
1062 { (void)printf("_%c",TokenChar(t)); } /* */
1063 t = NextToken(t); /* */
1064 } /* */
1065 } /*------------------------*/
1066 #endif /*DEBUG*/