2 * input.c: read the source form
10 #define TAB_STOP 8 /* for column number tracking */
12 static void setpos(input
*in
, char *fname
) {
13 in
->pos
.filename
= fname
;
15 in
->pos
.col
= (in
->reportcols ?
1 : -1);
18 static void unget(input
*in
, int c
, filepos
*pos
) {
19 if (in
->npushback
>= in
->pushbacksize
) {
20 in
->pushbacksize
= in
->npushback
+ 16;
21 in
->pushback
= resize(in
->pushback
, in
->pushbacksize
);
23 in
->pushback
[in
->npushback
].chr
= c
;
24 in
->pushback
[in
->npushback
].pos
= *pos
; /* structure copy */
28 /* ---------------------------------------------------------------------- */
32 typedef struct macro_Tag macro
;
36 struct macrostack_Tag
{
42 static int macrocmp(void *av
, void *bv
) {
43 macro
*a
= (macro
*)av
, *b
= (macro
*)bv
;
44 return ustrcmp(a
->name
, b
->name
);
46 static void macrodef(tree234
*macros
, wchar_t *name
, wchar_t *text
,
48 macro
*m
= mknew(macro
);
51 if (add234(macros
, m
) != m
) {
52 error(err_macroexists
, &fpos
, name
);
57 static int macrolookup(tree234
*macros
, input
*in
, wchar_t *name
,
61 gotit
= find234(macros
, &m
, NULL
);
63 macrostack
*expansion
= mknew(macrostack
);
64 expansion
->next
= in
->stack
;
65 expansion
->text
= gotit
->text
;
66 expansion
->pos
= *pos
; /* structure copy */
68 expansion
->npushback
= in
->npushback
;
69 in
->stack
= expansion
;
74 static void macrocleanup(tree234
*macros
) {
77 for (ti
= 0; (m
= (macro
*)index234(macros
, ti
)) != NULL
; ti
++) {
85 static void input_configure(input
*in
, paragraph
*cfg
) {
86 assert(cfg
->type
== para_Config
);
88 if (!ustricmp(cfg
->keyword
, L
"input-charset")) {
89 char *csname
= utoa_dup(uadv(cfg
->keyword
));
90 in
->charset
= charset_from_localenc(csname
);
98 static int get(input
*in
, filepos
*pos
) {
99 int pushbackpt
= in
->stack ? in
->stack
->npushback
: 0;
100 if (in
->npushback
> pushbackpt
) {
103 *pos
= in
->pushback
[in
->npushback
].pos
; /* structure copy */
104 return in
->pushback
[in
->npushback
].chr
;
106 else if (in
->stack
) {
107 wchar_t c
= in
->stack
->text
[in
->stack
->ptr
];
108 if (in
->stack
->text
[++in
->stack
->ptr
] == L
'\0') {
109 macrostack
*tmp
= in
->stack
;
110 in
->stack
= tmp
->next
;
115 else if (in
->currfp
) {
117 while (in
->wcpos
>= in
->nwc
) {
119 int c
= getc(in
->currfp
);
126 /* Track line numbers, for error reporting */
129 if (in
->reportcols
) {
132 in
->pos
.col
= 1 + (in
->pos
.col
+ TAB_STOP
-1) % TAB_STOP
;
149 * Do input character set translation, so that we return
161 in
->nwc
= charset_to_unicode(&p
, &inlen
,
162 in
->wc
, lenof(in
->wc
),
163 in
->charset
, &in
->csstate
,
165 assert(p
== buf
+1 && inlen
== 0);
171 return in
->wc
[in
->wcpos
++];
178 * Lexical analysis of source files.
180 typedef struct token_Tag token
;
188 tok_eof
, /* end of file */
189 tok_eop
, /* end of paragraph */
190 tok_white
, /* whitespace */
191 tok_word
, /* a word or word fragment */
192 tok_cmd
, /* \command */
197 /* Halibut command keywords. */
199 c__invalid
, /* invalid command */
200 c__comment
, /* comment command (\#) */
201 c__escaped
, /* escaped character */
203 c__nbsp
, /* nonbreaking space */
204 c_A
, /* appendix heading */
205 c_B
, /* bibliography entry */
206 c_BR
, /* bibliography rewrite */
207 c_C
, /* chapter heading */
209 c_I
, /* invisible index mark */
210 c_IM
, /* index merge/rewrite */
211 c_K
, /* capitalised cross-reference */
212 c_S
, /* aux field is 0, 1, 2, ... */
213 c_U
, /* unnumbered-chapter heading */
214 c_W
, /* Web hyperlink */
215 c_b
, /* bulletted list */
217 c_cfg
, /* configuration directive */
218 c_copyright
, /* copyright statement */
219 c_cw
, /* weak code */
220 c_date
, /* document processing date */
221 c_dd
, /* description list: description */
222 c_define
, /* macro definition */
223 c_dt
, /* description list: described thing */
225 c_i
, /* visible index mark */
226 c_ii
, /* uncapitalised visible index mark */
227 c_k
, /* uncapitalised cross-reference */
228 c_lcont
, /* continuation para(s) for list item */
229 c_n
, /* numbered list */
230 c_nocite
, /* bibliography trickery */
231 c_preamble
, /* (obsolete) preamble text */
232 c_q
, /* quote marks */
233 c_quote
, /* block-quoted paragraphs */
234 c_rule
, /* horizontal rule */
235 c_title
, /* document title */
236 c_u
, /* aux field is char code */
237 c_versionid
/* document RCS id */
240 /* Perhaps whitespace should be defined in a more Unicode-friendly way? */
241 #define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 )
242 #define isnl(c) ( (c)==10 )
243 #define isdec(c) ( ((c)>='0'&&(c)<='9') )
244 #define fromdec(c) ( (c)-'0' )
245 #define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f'))
246 #define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) )
247 #define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
250 * Keyword comparison function. Like strcmp, but between a wchar_t *
253 static int kwcmp(wchar_t const *p
, char const *q
) {
257 } while (*p
++ && *q
++ && !i
);
264 static void match_kw(token
*tok
) {
266 * FIXME. The ids are explicit in here so as to allow long-name
267 * equivalents to the various very short keywords.
269 static const struct { char const *name
; int id
; } keywords
[] = {
270 {"#", c__comment
}, /* comment command (\#) */
271 {"-", c__escaped
}, /* nonbreaking hyphen */
272 {".", c__nop
}, /* no-op */
273 {"A", c_A
}, /* appendix heading */
274 {"B", c_B
}, /* bibliography entry */
275 {"BR", c_BR
}, /* bibliography rewrite */
276 {"C", c_C
}, /* chapter heading */
277 {"H", c_H
}, /* heading */
278 {"I", c_I
}, /* invisible index mark */
279 {"IM", c_IM
}, /* index merge/rewrite */
280 {"K", c_K
}, /* capitalised cross-reference */
281 {"U", c_U
}, /* unnumbered-chapter heading */
282 {"W", c_W
}, /* Web hyperlink */
283 {"\\", c__escaped
}, /* escaped backslash (\\) */
284 {"_", c__nbsp
}, /* nonbreaking space (\_) */
285 {"b", c_b
}, /* bulletted list */
286 {"c", c_c
}, /* code */
287 {"cfg", c_cfg
}, /* configuration directive */
288 {"copyright", c_copyright
}, /* copyright statement */
289 {"cw", c_cw
}, /* weak code */
290 {"date", c_date
}, /* document processing date */
291 {"dd", c_dd
}, /* description list: description */
292 {"define", c_define
}, /* macro definition */
293 {"dt", c_dt
}, /* description list: described thing */
294 {"e", c_e
}, /* emphasis */
295 {"i", c_i
}, /* visible index mark */
296 {"ii", c_ii
}, /* uncapitalised visible index mark */
297 {"k", c_k
}, /* uncapitalised cross-reference */
298 {"lcont", c_lcont
}, /* continuation para(s) for list item */
299 {"n", c_n
}, /* numbered list */
300 {"nocite", c_nocite
}, /* bibliography trickery */
301 {"preamble", c_preamble
}, /* (obsolete) preamble text */
302 {"q", c_q
}, /* quote marks */
303 {"quote", c_quote
}, /* block-quoted paragraphs */
304 {"rule", c_rule
}, /* horizontal rule */
305 {"title", c_title
}, /* document title */
306 {"versionid", c_versionid
}, /* document RCS id */
307 {"{", c__escaped
}, /* escaped lbrace (\{) */
308 {"}", c__escaped
}, /* escaped rbrace (\}) */
313 * Special cases: \S{0,1,2,...} and \uABCD. If the syntax
314 * doesn't match correctly, we just fall through to the
315 * binary-search phase.
317 if (tok
->text
[0] == 'S') {
318 /* We expect numeric characters thereafter. */
319 wchar_t *p
= tok
->text
+1;
325 while (*p
&& isdec(*p
)) {
326 n
= 10 * n
+ fromdec(*p
);
335 } else if (tok
->text
[0] == 'u') {
336 /* We expect hex characters thereafter. */
337 wchar_t *p
= tok
->text
+1;
339 while (*p
&& ishex(*p
)) {
340 n
= 16 * n
+ fromhex(*p
);
351 j
= sizeof(keywords
)/sizeof(*keywords
);
354 c
= kwcmp(tok
->text
, keywords
[k
].name
);
360 tok
->cmd
= keywords
[k
].id
;
365 tok
->cmd
= c__invalid
;
370 * Read a token from the input file, in the normal way (`normal' in
371 * the sense that code paragraphs work a different way).
373 token
get_token(input
*in
) {
377 rdstring rs
= { 0, 0, NULL
};
380 ret
.text
= NULL
; /* default */
383 if (iswhite(c
)) { /* tok_white or tok_eop */
388 } while ((c
= get(in
, &cpos
)) != EOF
&& iswhite(c
));
394 ret
.type
= (nls
> 1 ? tok_eop
: tok_white
);
396 } else if (c
== EOF
) { /* tok_eof */
399 } else if (c
== '\\') { /* tok_cmd */
401 if (c
== '-' || c
== '\\' || c
== '_' ||
402 c
== '#' || c
== '{' || c
== '}' || c
== '.') {
403 /* single-char command */
405 } else if (c
== 'u') {
411 } while (ishex(c
) && len
< 5);
413 } else if (iscmd(c
)) {
421 * Now match the command against the list of available
425 ret
.text
= ustrdup(rs
.text
);
429 } else if (c
== '{') { /* tok_lbrace */
430 ret
.type
= tok_lbrace
;
432 } else if (c
== '}') { /* tok_rbrace */
433 ret
.type
= tok_rbrace
;
435 } else { /* tok_word */
437 * Read a word: the longest possible contiguous sequence of
438 * things other than whitespace, backslash, braces and
439 * hyphen. A hyphen terminates the word but is returned as
440 * part of it; everything else is pushed back for the next
441 * token. The `aux' field contains TRUE if the word ends in
444 ret
.aux
= FALSE
; /* assumed for now */
446 if (iswhite(c
) || c
=='{' || c
=='}' || c
=='\\' || c
==EOF
) {
447 /* Put back the character that caused termination */
454 break; /* hyphen terminates word */
460 ret
.text
= ustrdup(rs
.text
);
467 * Determine whether the next input character is an open brace (for
468 * telling code paragraphs from paragraphs which merely start with
471 int isbrace(input
*in
) {
481 * Read the rest of a line that starts `\c'. Including nothing at
482 * all (tok_word with empty text).
484 token
get_codepar_token(input
*in
) {
487 rdstring rs
= { 0, 0, NULL
};
491 c
= get(in
, &cpos
); /* expect (and discard) one space */
497 while (!isnl(c
) && c
!= EOF
) {
500 /* Discard \r just before \n. */
501 if (c2
!= 13 || !isnl(c
))
505 ret
.text
= ustrdup(rs
.text
);
511 * Adds a new word to a linked list
513 static word
*addword(word newword
, word
***hptrptr
) {
517 mnewword
= mknew(word
);
518 *mnewword
= newword
; /* structure copy */
519 mnewword
->next
= NULL
;
520 **hptrptr
= mnewword
;
521 *hptrptr
= &mnewword
->next
;
526 * Adds a new paragraph to a linked list
528 static paragraph
*addpara(paragraph newpara
, paragraph
***hptrptr
) {
529 paragraph
*mnewpara
= mknew(paragraph
);
530 *mnewpara
= newpara
; /* structure copy */
531 mnewpara
->next
= NULL
;
532 **hptrptr
= mnewpara
;
533 *hptrptr
= &mnewpara
->next
;
538 * Destructor before token is reassigned; should catch most memory
541 #define dtor(t) ( sfree(t.text) )
544 * Reads a single file (ie until get() returns EOF)
546 static void read_file(paragraph
***ret
, input
*in
, indexdata
*idx
) {
549 word wd
, **whptr
, **idximplicit
;
551 wchar_t utext
[2], *wdtext
;
554 int iswhite
, seenwhite
;
559 stack_nop
= 0, /* do nothing (for error recovery) */
560 stack_ualt
= 1, /* \u alternative */
561 stack_style
= 2, /* \e, \c, \cw */
562 stack_idx
= 4, /* \I, \i, \ii */
563 stack_hyper
= 8, /* \W */
564 stack_quote
= 16, /* \q */
566 word
**whptr
; /* to restore from \u alternatives */
567 word
**idximplicit
; /* to restore from \u alternatives */
571 struct crossparaitem
{
572 int type
; /* currently c_lcont, c_quote or -1 */
573 int seen_lcont
, seen_quote
;
576 word
*indexword
, *uword
, *iword
;
579 int index_downcase
, index_visible
, indexing
;
580 const rdstring nullrs
= { 0, 0, NULL
};
584 macros
= newtree234(macrocmp
);
587 crossparastk
= stk_new();
590 * Loop on each paragraph.
593 int start_cmd
= c__invalid
;
603 dtor(t
), t
= get_token(in
);
606 } while (t
.type
== tok_eop
);
607 if (t
.type
== tok_eof
)
611 * Parse code paragraphs separately.
613 if (t
.type
== tok_cmd
&& t
.cmd
== c_c
&& !isbrace(in
)) {
614 int wtype
= word_WeakCode
;
616 par
.type
= para_Code
;
619 dtor(t
), t
= get_codepar_token(in
);
621 wd
.breaks
= FALSE
; /* shouldn't need this... */
622 wd
.text
= ustrdup(t
.text
);
626 dtor(t
), t
= get_token(in
);
627 if (t
.type
== tok_white
) {
629 * The newline after a code-paragraph line
631 dtor(t
), t
= get_token(in
);
633 if (t
.type
== tok_eop
|| t
.type
== tok_eof
||
634 t
.type
== tok_rbrace
) { /* might be } terminating \lcont */
635 if (t
.type
== tok_rbrace
)
638 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_c
) {
639 wtype
= word_WeakCode
;
640 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_e
&&
641 wtype
== word_WeakCode
) {
644 error(err_brokencodepara
, &t
.pos
);
645 prev_para_type
= par
.type
;
647 while (t
.type
!= tok_eop
) /* error recovery: */
648 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
649 goto codeparabroken
; /* ick, but such is life */
652 prev_para_type
= par
.type
;
659 * Spot the special commands that define a grouping of more
660 * than one paragraph, and also the closing braces that
663 if (t
.type
== tok_cmd
&&
664 (t
.cmd
== c_lcont
|| t
.cmd
== c_quote
)) {
665 struct crossparaitem
*sitem
, *stop
;
669 * Expect, and swallow, an open brace.
671 dtor(t
), t
= get_token(in
);
672 if (t
.type
!= tok_lbrace
) {
673 error(err_explbr
, &t
.pos
);
678 * Also expect, and swallow, any whitespace after that
679 * (a newline before a code paragraph wouldn't be
683 dtor(t
), t
= get_token(in
);
684 } while (t
.type
== tok_white
);
687 if (cmd
== c_lcont
) {
689 * \lcont causes a continuation of a list item into
690 * multiple paragraphs (which may in turn contain
691 * nested lists, code paras etc). Hence, the previous
692 * paragraph must be of a list type.
694 sitem
= mknew(struct crossparaitem
);
695 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
699 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
701 if (prev_para_type
== para_Bullet
||
702 prev_para_type
== para_NumberedList
||
703 prev_para_type
== para_Description
) {
704 sitem
->type
= c_lcont
;
705 sitem
->seen_lcont
= 1;
706 par
.type
= para_LcontPush
;
707 prev_para_type
= par
.type
;
711 * Push a null item on the cross-para stack so that
712 * when we see the corresponding closing brace we
713 * don't give a cascade error.
716 error(err_misplacedlcont
, &t
.pos
);
720 * \quote causes a group of paragraphs to be
721 * block-quoted (typically they will be indented a
724 sitem
= mknew(struct crossparaitem
);
725 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
729 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
730 sitem
->type
= c_quote
;
731 sitem
->seen_quote
= 1;
732 par
.type
= para_QuotePush
;
733 prev_para_type
= par
.type
;
736 stk_push(crossparastk
, sitem
);
738 } else if (t
.type
== tok_rbrace
) {
739 struct crossparaitem
*sitem
= stk_pop(crossparastk
);
741 error(err_unexbrace
, &t
.pos
);
743 switch (sitem
->type
) {
745 par
.type
= para_LcontPop
;
746 prev_para_type
= par
.type
;
750 par
.type
= para_QuotePop
;
751 prev_para_type
= par
.type
;
761 * This token begins a paragraph. See if it's one of the
762 * special commands that define a paragraph type.
764 * (note that \# is special in a way, and \nocite takes no
767 par
.type
= para_Normal
;
768 if (t
.type
== tok_cmd
) {
770 int is_macro
= FALSE
;
778 error(err_badparatype
, t
.text
, &t
.pos
);
783 break; /* `\#{': isn't a comment para */
785 dtor(t
), t
= get_token(in
);
786 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
787 continue; /* next paragraph */
791 * 1 -- exactly one keyword
792 * 2 -- at least one keyword
793 * 4 -- any number of keywords including zero
794 * 8 -- at least one keyword and then nothing else
795 * 16 -- nothing at all! no keywords, no body
796 * 32 -- no keywords at all
798 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
799 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
800 case c_BR
: needkw
= 1; par
.type
= para_BR
;
801 start_cmd
= c_BR
; break;
802 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
803 case c_H
: needkw
= 2; par
.type
= para_Heading
;
806 case c_IM
: needkw
= 2; par
.type
= para_IM
;
807 start_cmd
= c_IM
; break;
808 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
809 par
.aux
= t
.aux
; break;
810 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
811 /* For \b and \n the keyword is optional */
812 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
813 case c_dt
: needkw
= 4; par
.type
= para_DescribedThing
; break;
814 case c_dd
: needkw
= 4; par
.type
= para_Description
; break;
815 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
816 case c_cfg
: needkw
= 8; par
.type
= para_Config
;
817 start_cmd
= c_cfg
; break;
818 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
819 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
820 /* For \nocite the keyword is _everything_ */
821 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
822 case c_preamble
: needkw
= 32; par
.type
= para_Normal
; break;
823 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
824 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
825 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
828 if (par
.type
== para_Chapter
||
829 par
.type
== para_Heading
||
830 par
.type
== para_Subsect
||
831 par
.type
== para_Appendix
||
832 par
.type
== para_UnnumberedChapter
) {
833 struct crossparaitem
*sitem
= stk_top(crossparastk
);
834 if (sitem
&& (sitem
->seen_lcont
|| sitem
->seen_quote
)) {
835 error(err_sectmarkerinblock
,
837 (sitem
->seen_lcont ?
"lcont" : "quote"));
842 rdstring rs
= { 0, 0, NULL
};
847 dtor(t
), t
= get_token(in
);
849 while (t
.type
== tok_lbrace
) {
850 /* This is a keyword. */
852 /* FIXME: there will be bugs if anyone specifies an
853 * empty keyword (\foo{}), so trap this case. */
854 while (dtor(t
), t
= get_token(in
),
855 t
.type
== tok_word
||
856 t
.type
== tok_white
||
857 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
858 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
)) {
859 if (t
.type
== tok_white
||
860 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
))
865 if (t
.type
!= tok_rbrace
) {
866 error(err_kwunclosed
, &t
.pos
);
869 rdadd(&rs
, 0); /* add string terminator */
870 dtor(t
), t
= get_token(in
); /* eat right brace */
873 rdadd(&rs
, 0); /* add string terminator */
875 /* See whether we have the right number of keywords. */
876 if ((needkw
& 48) && nkeys
> 0)
877 error(err_kwillegal
, &fp
);
878 if ((needkw
& 11) && nkeys
== 0)
879 error(err_kwexpected
, &fp
);
880 if ((needkw
& 5) && nkeys
> 1)
881 error(err_kwtoomany
, &fp
);
885 * Macro definition. Get the rest of the line
886 * as a code-paragraph token, repeatedly until
887 * there's nothing more left of it. Separate
890 rdstring macrotext
= { 0, 0, NULL
};
892 dtor(t
), t
= get_codepar_token(in
);
893 if (macrotext
.pos
> 0)
894 rdadd(¯otext
, L
'\n');
895 rdadds(¯otext
, t
.text
);
896 dtor(t
), t
= get_token(in
);
897 if (t
.type
== tok_eop
) break;
899 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
900 continue; /* next paragraph */
903 par
.keyword
= rdtrim(&rs
);
905 /* Move to EOP in case of needkw==8 or 16 (no body) */
907 /* We allow whitespace even when we expect no para body */
908 while (t
.type
== tok_white
)
909 dtor(t
), t
= get_token(in
);
910 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
911 (start_cmd
== c__invalid
||
912 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
)) {
913 error(err_bodyillegal
, &t
.pos
);
914 /* Error recovery: eat the rest of the paragraph */
915 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
916 (start_cmd
== c__invalid
||
917 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
))
918 dtor(t
), t
= get_token(in
);
920 if (t
.type
== tok_cmd
)
921 already
= TRUE
;/* inhibit get_token at top of loop */
922 prev_para_type
= par
.type
;
925 if (par
.type
== para_Config
) {
926 input_configure(in
, &par
);
928 continue; /* next paragraph */
934 * Now read the actual paragraph, word by word, adding to
935 * the paragraph list.
937 * Mid-paragraph commands:
949 parsestk
= stk_new();
951 spcstyle
= word_WhiteSpace
;
954 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
958 /* Handle implicit paragraph breaks after \IM, \BR etc */
959 if (start_cmd
!= c__invalid
&&
960 t
.type
== tok_cmd
&& t
.cmd
== start_cmd
) {
961 already
= TRUE
; /* inhibit get_token at top of loop */
965 if (t
.type
== tok_cmd
&& t
.cmd
== c__nop
) {
966 dtor(t
), t
= get_token(in
);
967 continue; /* do nothing! */
970 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
971 t
.type
= tok_word
; /* nice and simple */
972 t
.aux
= 0; /* even if `\-' - nonbreaking! */
974 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
975 t
.type
= tok_word
; /* nice and simple */
977 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
978 t
.aux
= 0; /* (nonbreaking) */
982 if (whptr
== &par
.words
)
983 break; /* strip whitespace at start of para */
992 * Inhibit use of whitespace if it's (probably the
993 * newline) before a repeat \IM / \BR type
996 if (start_cmd
!= c__invalid
) {
997 dtor(t
), t
= get_token(in
);
999 if (t
.type
== tok_cmd
&& t
.cmd
== start_cmd
)
1004 rdadd(&indexstr
, ' ');
1005 if (!indexing
|| index_visible
)
1006 addword(wd
, &whptr
);
1008 addword(wd
, &idximplicit
);
1013 rdadds(&indexstr
, t
.text
);
1019 if (!indexing
|| index_visible
) {
1020 wd
.text
= ustrdup(t
.text
);
1021 addword(wd
, &whptr
);
1024 wd
.text
= ustrdup(t
.text
);
1025 addword(wd
, &idximplicit
);
1029 error(err_unexbrace
, &t
.pos
);
1030 /* Error recovery: push nop */
1031 sitem
= mknew(struct stack_item
);
1032 sitem
->type
= stack_nop
;
1033 sitem
->fpos
= t
.pos
;
1034 stk_push(parsestk
, sitem
);
1037 sitem
= stk_pop(parsestk
);
1040 * This closing brace could have been an
1041 * indication that the cross-paragraph stack
1042 * wants popping. Accordingly, we treat it here
1043 * as an indication that the paragraph is over.
1048 if (sitem
->type
& stack_ualt
) {
1049 whptr
= sitem
->whptr
;
1050 idximplicit
= sitem
->idximplicit
;
1052 if (sitem
->type
& stack_style
) {
1053 style
= word_Normal
;
1054 spcstyle
= word_WhiteSpace
;
1056 if (sitem
->type
& stack_idx
) {
1057 indexword
->text
= ustrdup(indexstr
.text
);
1058 if (index_downcase
) {
1061 ustrlow(indexword
->text
);
1062 ustrlow(indexstr
.text
);
1064 for (w
= idxwordlist
; w
; w
= w
->next
)
1069 rdadd(&indexstr
, L
'\0');
1070 index_merge(idx
, FALSE
, indexstr
.text
,
1071 idxwordlist
, &sitem
->fpos
);
1072 sfree(indexstr
.text
);
1074 if (sitem
->type
& stack_hyper
) {
1076 wd
.type
= word_HyperEnd
;
1081 if (!indexing
|| index_visible
)
1082 addword(wd
, &whptr
);
1084 addword(wd
, &idximplicit
);
1086 if (sitem
->type
& stack_quote
) {
1088 wd
.type
= toquotestyle(style
);
1090 wd
.aux
= quote_Close
;
1093 if (!indexing
|| index_visible
)
1094 addword(wd
, &whptr
);
1096 rdadd(&indexstr
, L
'"');
1097 addword(wd
, &idximplicit
);
1107 * In-paragraph comment: \#{ balanced braces }
1109 * Anything goes here; even tok_eop. We should
1110 * eat whitespace after the close brace _if_
1111 * there was whitespace before the \#.
1113 dtor(t
), t
= get_token(in
);
1114 if (t
.type
!= tok_lbrace
) {
1115 error(err_explbr
, &t
.pos
);
1118 while (braces
> 0) {
1119 dtor(t
), t
= get_token(in
);
1120 if (t
.type
== tok_lbrace
)
1122 else if (t
.type
== tok_rbrace
)
1124 else if (t
.type
== tok_eof
) {
1125 error(err_commenteof
, &t
.pos
);
1132 dtor(t
), t
= get_token(in
);
1133 if (t
.type
== tok_white
) {
1140 dtor(t
), t
= get_token(in
);
1141 if (t
.type
!= tok_lbrace
) {
1142 error(err_explbr
, &t
.pos
);
1145 wd
.type
= toquotestyle(style
);
1147 wd
.aux
= quote_Open
;
1150 if (!indexing
|| index_visible
)
1151 addword(wd
, &whptr
);
1153 rdadd(&indexstr
, L
'"');
1154 addword(wd
, &idximplicit
);
1156 sitem
= mknew(struct stack_item
);
1157 sitem
->fpos
= t
.pos
;
1158 sitem
->type
= stack_quote
;
1159 stk_push(parsestk
, sitem
);
1167 * Keyword, hyperlink, or \date. We expect a
1168 * left brace, some text, and then a right
1169 * brace. No nesting; no arguments.
1174 wd
.type
= word_UpperXref
;
1175 else if (t
.cmd
== c_k
)
1176 wd
.type
= word_LowerXref
;
1177 else if (t
.cmd
== c_W
)
1178 wd
.type
= word_HyperLink
;
1180 wd
.type
= word_Normal
;
1181 dtor(t
), t
= get_token(in
);
1182 if (t
.type
!= tok_lbrace
) {
1183 if (wd
.type
== word_Normal
) {
1184 time_t thetime
= time(NULL
);
1185 struct tm
*broken
= localtime(&thetime
);
1187 wdtext
= ustrftime(NULL
, broken
);
1190 error(err_explbr
, &t
.pos
);
1194 rdstring rs
= { 0, 0, NULL
};
1195 while (dtor(t
), t
= get_token(in
),
1196 t
.type
== tok_word
|| t
.type
== tok_white
) {
1197 if (t
.type
== tok_white
)
1200 rdadds(&rs
, t
.text
);
1202 if (wd
.type
== word_Normal
) {
1203 time_t thetime
= time(NULL
);
1204 struct tm
*broken
= localtime(&thetime
);
1205 wdtext
= ustrftime(rs
.text
, broken
);
1208 wdtext
= ustrdup(rs
.text
);
1211 if (t
.type
!= tok_rbrace
) {
1212 error(err_kwexprbr
, &t
.pos
);
1217 if (!indexing
|| index_visible
) {
1218 wd
.text
= ustrdup(wdtext
);
1219 addword(wd
, &whptr
);
1222 wd
.text
= ustrdup(wdtext
);
1223 addword(wd
, &idximplicit
);
1226 if (wd
.type
== word_HyperLink
) {
1228 * Hyperlinks are different: they then
1229 * expect another left brace, to begin
1230 * delimiting the text marked by the link.
1232 dtor(t
), t
= get_token(in
);
1233 sitem
= mknew(struct stack_item
);
1234 sitem
->fpos
= wd
.fpos
;
1235 sitem
->type
= stack_hyper
;
1237 * Special cases: \W{}\i, \W{}\ii
1239 if (t
.type
== tok_cmd
&&
1240 (t
.cmd
== c_i
|| t
.cmd
== c_ii
)) {
1242 error(err_nestedindex
, &t
.pos
);
1244 /* Add an index-reference word with no
1246 wd
.type
= word_IndexRef
;
1251 indexword
= addword(wd
, &whptr
);
1252 /* Set up a rdstring to read the
1255 /* Flags so that we do the Right
1256 * Things with text */
1257 index_visible
= (type
!= c_I
);
1258 index_downcase
= (type
== c_ii
);
1261 idximplicit
= &idxwordlist
;
1263 sitem
->type
|= stack_idx
;
1265 dtor(t
), t
= get_token(in
);
1268 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1270 if (t
.type
== tok_cmd
&&
1271 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1272 if (style
!= word_Normal
)
1273 error(err_nestedstyles
, &t
.pos
);
1275 style
= (t
.cmd
== c_c ? word_Code
:
1276 t
.cmd
== c_cw ? word_WeakCode
:
1278 spcstyle
= tospacestyle(style
);
1279 sitem
->type
|= stack_style
;
1281 dtor(t
), t
= get_token(in
);
1283 if (t
.type
!= tok_lbrace
) {
1284 error(err_explbr
, &t
.pos
);
1287 stk_push(parsestk
, sitem
);
1295 if (style
!= word_Normal
) {
1296 error(err_nestedstyles
, &t
.pos
);
1297 /* Error recovery: eat lbrace, push nop. */
1298 dtor(t
), t
= get_token(in
);
1299 sitem
= mknew(struct stack_item
);
1300 sitem
->fpos
= t
.pos
;
1301 sitem
->type
= stack_nop
;
1302 stk_push(parsestk
, sitem
);
1304 dtor(t
), t
= get_token(in
);
1305 if (t
.type
!= tok_lbrace
) {
1306 error(err_explbr
, &t
.pos
);
1308 style
= (type
== c_c ? word_Code
:
1309 type
== c_cw ? word_WeakCode
:
1311 spcstyle
= tospacestyle(style
);
1312 sitem
= mknew(struct stack_item
);
1313 sitem
->fpos
= t
.pos
;
1314 sitem
->type
= stack_style
;
1315 stk_push(parsestk
, sitem
);
1323 error(err_nestedindex
, &t
.pos
);
1324 /* Error recovery: eat lbrace, push nop. */
1325 dtor(t
), t
= get_token(in
);
1326 sitem
= mknew(struct stack_item
);
1327 sitem
->fpos
= t
.pos
;
1328 sitem
->type
= stack_nop
;
1329 stk_push(parsestk
, sitem
);
1331 sitem
= mknew(struct stack_item
);
1332 sitem
->fpos
= t
.pos
;
1333 sitem
->type
= stack_idx
;
1334 dtor(t
), t
= get_token(in
);
1336 * Special cases: \i\c, \i\e, \i\cw
1339 if (t
.type
== tok_cmd
&&
1340 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1341 if (style
!= word_Normal
)
1342 error(err_nestedstyles
, &t
.pos
);
1344 style
= (t
.cmd
== c_c ? word_Code
:
1345 t
.cmd
== c_cw ? word_WeakCode
:
1347 spcstyle
= tospacestyle(style
);
1348 sitem
->type
|= stack_style
;
1350 dtor(t
), t
= get_token(in
);
1352 if (t
.type
!= tok_lbrace
) {
1354 error(err_explbr
, &t
.pos
);
1356 /* Add an index-reference word with no text as yet */
1357 wd
.type
= word_IndexRef
;
1362 indexword
= addword(wd
, &whptr
);
1363 /* Set up a rdstring to read the index text */
1365 /* Flags so that we do the Right Things with text */
1366 index_visible
= (type
!= c_I
);
1367 index_downcase
= (type
== c_ii
);
1370 idximplicit
= &idxwordlist
;
1371 /* Stack item to close the indexing on exit */
1372 stk_push(parsestk
, sitem
);
1377 utext
[0] = uchr
; utext
[1] = 0;
1383 if (!indexing
|| index_visible
) {
1384 wd
.text
= ustrdup(utext
);
1385 uword
= addword(wd
, &whptr
);
1389 wd
.text
= ustrdup(utext
);
1390 iword
= addword(wd
, &idximplicit
);
1393 dtor(t
), t
= get_token(in
);
1394 if (t
.type
== tok_lbrace
) {
1396 * \u with a left brace. Until the brace
1397 * closes, all further words go on a
1398 * sidetrack from the main thread of the
1401 sitem
= mknew(struct stack_item
);
1402 sitem
->fpos
= t
.pos
;
1403 sitem
->type
= stack_ualt
;
1404 sitem
->whptr
= whptr
;
1405 sitem
->idximplicit
= idximplicit
;
1406 stk_push(parsestk
, sitem
);
1407 whptr
= uword ?
&uword
->alt
: NULL
;
1408 idximplicit
= iword ?
&iword
->alt
: NULL
;
1411 rdadd(&indexstr
, uchr
);
1416 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1417 error(err_badmidcmd
, t
.text
, &t
.pos
);
1422 dtor(t
), t
= get_token(in
);
1423 seenwhite
= iswhite
;
1426 /* Check the stack is empty */
1427 if (stk_top(parsestk
)) {
1428 while ((sitem
= stk_pop(parsestk
)))
1430 error(err_missingrbrace
, &t
.pos
);
1433 prev_para_type
= par
.type
;
1435 if (t
.type
== tok_eof
)
1439 if (stk_top(crossparastk
)) {
1442 error(err_missingrbrace2
, &t
.pos
);
1443 while ((p
= stk_pop(crossparastk
)))
1448 * We break to here rather than returning, because otherwise
1449 * this cleanup doesn't happen.
1452 macrocleanup(macros
);
1454 stk_free(crossparastk
);
1457 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1458 paragraph
*head
= NULL
;
1459 paragraph
**hptr
= &head
;
1461 while (in
->currindex
< in
->nfiles
) {
1462 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1464 setpos(in
, in
->filenames
[in
->currindex
]);
1465 in
->charset
= in
->defcharset
;
1466 in
->csstate
= charset_init_state
;
1467 read_file(&hptr
, in
, idx
);