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
++) {
88 static int get(input
*in
, filepos
*pos
) {
89 int pushbackpt
= in
->stack ? in
->stack
->npushback
: 0;
90 if (in
->npushback
> pushbackpt
) {
93 *pos
= in
->pushback
[in
->npushback
].pos
; /* structure copy */
94 return in
->pushback
[in
->npushback
].chr
;
97 wchar_t c
= in
->stack
->text
[in
->stack
->ptr
];
98 if (in
->stack
->text
[++in
->stack
->ptr
] == L
'\0') {
99 macrostack
*tmp
= in
->stack
;
100 in
->stack
= tmp
->next
;
105 else if (in
->currfp
) {
106 int c
= getc(in
->currfp
);
112 /* Track line numbers, for error reporting */
115 if (in
->reportcols
) {
118 in
->pos
.col
= 1 + (in
->pos
.col
+ TAB_STOP
-1) % TAB_STOP
;
133 /* FIXME: do input charmap translation. We should be returning
141 * Lexical analysis of source files.
143 typedef struct token_Tag token
;
151 tok_eof
, /* end of file */
152 tok_eop
, /* end of paragraph */
153 tok_white
, /* whitespace */
154 tok_word
, /* a word or word fragment */
155 tok_cmd
, /* \command */
160 /* Halibut command keywords. */
162 c__invalid
, /* invalid command */
163 c__comment
, /* comment command (\#) */
164 c__escaped
, /* escaped character */
165 c__nbsp
, /* nonbreaking space */
166 c_A
, /* appendix heading */
167 c_B
, /* bibliography entry */
168 c_BR
, /* bibliography rewrite */
169 c_C
, /* chapter heading */
171 c_I
, /* invisible index mark */
172 c_IM
, /* index merge/rewrite */
173 c_K
, /* capitalised cross-reference */
174 c_S
, /* aux field is 0, 1, 2, ... */
175 c_U
, /* unnumbered-chapter heading */
176 c_W
, /* Web hyperlink */
177 c_b
, /* bulletted list */
179 c_cfg
, /* configuration directive */
180 c_copyright
, /* copyright statement */
181 c_cw
, /* weak code */
182 c_date
, /* document processing date */
183 c_dd
, /* description list: description */
184 c_define
, /* macro definition */
185 c_dt
, /* description list: described thing */
187 c_i
, /* visible index mark */
188 c_ii
, /* uncapitalised visible index mark */
189 c_k
, /* uncapitalised cross-reference */
190 c_lcont
, /* continuation para(s) for list item */
191 c_n
, /* numbered list */
192 c_nocite
, /* bibliography trickery */
193 c_preamble
, /* (obsolete) preamble text */
194 c_q
, /* quote marks */
195 c_quote
, /* block-quoted paragraphs */
196 c_rule
, /* horizontal rule */
197 c_title
, /* document title */
198 c_u
, /* aux field is char code */
199 c_versionid
/* document RCS id */
202 /* Perhaps whitespace should be defined in a more Unicode-friendly way? */
203 #define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 )
204 #define isnl(c) ( (c)==10 )
205 #define isdec(c) ( ((c)>='0'&&(c)<='9') )
206 #define fromdec(c) ( (c)-'0' )
207 #define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f'))
208 #define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) )
209 #define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
212 * Keyword comparison function. Like strcmp, but between a wchar_t *
215 static int kwcmp(wchar_t const *p
, char const *q
) {
219 } while (*p
++ && *q
++ && !i
);
226 static void match_kw(token
*tok
) {
228 * FIXME. The ids are explicit in here so as to allow long-name
229 * equivalents to the various very short keywords.
231 static const struct { char const *name
; int id
; } keywords
[] = {
232 {"#", c__comment
}, /* comment command (\#) */
233 {"-", c__escaped
}, /* nonbreaking hyphen */
234 {"A", c_A
}, /* appendix heading */
235 {"B", c_B
}, /* bibliography entry */
236 {"BR", c_BR
}, /* bibliography rewrite */
237 {"C", c_C
}, /* chapter heading */
238 {"H", c_H
}, /* heading */
239 {"I", c_I
}, /* invisible index mark */
240 {"IM", c_IM
}, /* index merge/rewrite */
241 {"K", c_K
}, /* capitalised cross-reference */
242 {"U", c_U
}, /* unnumbered-chapter heading */
243 {"W", c_W
}, /* Web hyperlink */
244 {"\\", c__escaped
}, /* escaped backslash (\\) */
245 {"_", c__nbsp
}, /* nonbreaking space (\_) */
246 {"b", c_b
}, /* bulletted list */
247 {"c", c_c
}, /* code */
248 {"cfg", c_cfg
}, /* configuration directive */
249 {"copyright", c_copyright
}, /* copyright statement */
250 {"cw", c_cw
}, /* weak code */
251 {"date", c_date
}, /* document processing date */
252 {"dd", c_dd
}, /* description list: description */
253 {"define", c_define
}, /* macro definition */
254 {"dt", c_dt
}, /* description list: described thing */
255 {"e", c_e
}, /* emphasis */
256 {"i", c_i
}, /* visible index mark */
257 {"ii", c_ii
}, /* uncapitalised visible index mark */
258 {"k", c_k
}, /* uncapitalised cross-reference */
259 {"lcont", c_lcont
}, /* continuation para(s) for list item */
260 {"n", c_n
}, /* numbered list */
261 {"nocite", c_nocite
}, /* bibliography trickery */
262 {"preamble", c_preamble
}, /* (obsolete) preamble text */
263 {"q", c_q
}, /* quote marks */
264 {"quote", c_quote
}, /* block-quoted paragraphs */
265 {"rule", c_rule
}, /* horizontal rule */
266 {"title", c_title
}, /* document title */
267 {"versionid", c_versionid
}, /* document RCS id */
268 {"{", c__escaped
}, /* escaped lbrace (\{) */
269 {"}", c__escaped
}, /* escaped rbrace (\}) */
274 * Special cases: \S{0,1,2,...} and \uABCD. If the syntax
275 * doesn't match correctly, we just fall through to the
276 * binary-search phase.
278 if (tok
->text
[0] == 'S') {
279 /* We expect numeric characters thereafter. */
280 wchar_t *p
= tok
->text
+1;
286 while (*p
&& isdec(*p
)) {
287 n
= 10 * n
+ fromdec(*p
);
296 } else if (tok
->text
[0] == 'u') {
297 /* We expect hex characters thereafter. */
298 wchar_t *p
= tok
->text
+1;
300 while (*p
&& ishex(*p
)) {
301 n
= 16 * n
+ fromhex(*p
);
312 j
= sizeof(keywords
)/sizeof(*keywords
);
315 c
= kwcmp(tok
->text
, keywords
[k
].name
);
321 tok
->cmd
= keywords
[k
].id
;
326 tok
->cmd
= c__invalid
;
331 * Read a token from the input file, in the normal way (`normal' in
332 * the sense that code paragraphs work a different way).
334 token
get_token(input
*in
) {
338 rdstring rs
= { 0, 0, NULL
};
341 ret
.text
= NULL
; /* default */
344 if (iswhite(c
)) { /* tok_white or tok_eop */
349 } while ((c
= get(in
, &cpos
)) != EOF
&& iswhite(c
));
355 ret
.type
= (nls
> 1 ? tok_eop
: tok_white
);
357 } else if (c
== EOF
) { /* tok_eof */
360 } else if (c
== '\\') { /* tok_cmd */
362 if (c
== '-' || c
== '\\' || c
== '_' ||
363 c
== '#' || c
== '{' || c
== '}') {
364 /* single-char command */
366 } else if (c
== 'u') {
372 } while (ishex(c
) && len
< 5);
374 } else if (iscmd(c
)) {
382 * Now match the command against the list of available
386 ret
.text
= ustrdup(rs
.text
);
390 } else if (c
== '{') { /* tok_lbrace */
391 ret
.type
= tok_lbrace
;
393 } else if (c
== '}') { /* tok_rbrace */
394 ret
.type
= tok_rbrace
;
396 } else { /* tok_word */
398 * Read a word: the longest possible contiguous sequence of
399 * things other than whitespace, backslash, braces and
400 * hyphen. A hyphen terminates the word but is returned as
401 * part of it; everything else is pushed back for the next
402 * token. The `aux' field contains TRUE if the word ends in
405 ret
.aux
= FALSE
; /* assumed for now */
407 if (iswhite(c
) || c
=='{' || c
=='}' || c
=='\\' || c
==EOF
) {
408 /* Put back the character that caused termination */
415 break; /* hyphen terminates word */
421 ret
.text
= ustrdup(rs
.text
);
428 * Determine whether the next input character is an open brace (for
429 * telling code paragraphs from paragraphs which merely start with
432 int isbrace(input
*in
) {
442 * Read the rest of a line that starts `\c'. Including nothing at
443 * all (tok_word with empty text).
445 token
get_codepar_token(input
*in
) {
448 rdstring rs
= { 0, 0, NULL
};
452 c
= get(in
, &cpos
); /* expect (and discard) one space */
458 while (!isnl(c
) && c
!= EOF
) {
461 /* Discard \r just before \n. */
462 if (c2
!= 13 || !isnl(c
))
466 ret
.text
= ustrdup(rs
.text
);
472 * Adds a new word to a linked list
474 static word
*addword(word newword
, word
***hptrptr
) {
478 mnewword
= mknew(word
);
479 *mnewword
= newword
; /* structure copy */
480 mnewword
->next
= NULL
;
481 **hptrptr
= mnewword
;
482 *hptrptr
= &mnewword
->next
;
487 * Adds a new paragraph to a linked list
489 static paragraph
*addpara(paragraph newpara
, paragraph
***hptrptr
) {
490 paragraph
*mnewpara
= mknew(paragraph
);
491 *mnewpara
= newpara
; /* structure copy */
492 mnewpara
->next
= NULL
;
493 **hptrptr
= mnewpara
;
494 *hptrptr
= &mnewpara
->next
;
499 * Destructor before token is reassigned; should catch most memory
502 #define dtor(t) ( sfree(t.text) )
505 * Reads a single file (ie until get() returns EOF)
507 static void read_file(paragraph
***ret
, input
*in
, indexdata
*idx
) {
510 word wd
, **whptr
, **idximplicit
;
512 wchar_t utext
[2], *wdtext
;
515 int iswhite
, seenwhite
;
520 stack_nop
= 0, /* do nothing (for error recovery) */
521 stack_ualt
= 1, /* \u alternative */
522 stack_style
= 2, /* \e, \c, \cw */
523 stack_idx
= 4, /* \I, \i, \ii */
524 stack_hyper
= 8, /* \W */
525 stack_quote
= 16, /* \q */
527 word
**whptr
; /* to restore from \u alternatives */
528 word
**idximplicit
; /* to restore from \u alternatives */
531 struct crossparaitem
{
532 int type
; /* currently c_lcont, c_quote or -1 */
533 int seen_lcont
, seen_quote
;
536 word
*indexword
, *uword
, *iword
;
539 int index_downcase
, index_visible
, indexing
;
540 const rdstring nullrs
= { 0, 0, NULL
};
544 macros
= newtree234(macrocmp
);
547 crossparastk
= stk_new();
550 * Loop on each paragraph.
553 int start_cmd
= c__invalid
;
563 dtor(t
), t
= get_token(in
);
566 } while (t
.type
== tok_eop
);
567 if (t
.type
== tok_eof
)
571 * Parse code paragraphs separately.
573 if (t
.type
== tok_cmd
&& t
.cmd
== c_c
&& !isbrace(in
)) {
574 int wtype
= word_WeakCode
;
576 par
.type
= para_Code
;
579 dtor(t
), t
= get_codepar_token(in
);
581 wd
.breaks
= FALSE
; /* shouldn't need this... */
582 wd
.text
= ustrdup(t
.text
);
586 dtor(t
), t
= get_token(in
);
587 if (t
.type
== tok_white
) {
589 * The newline after a code-paragraph line
591 dtor(t
), t
= get_token(in
);
593 if (t
.type
== tok_eop
|| t
.type
== tok_eof
)
595 else if (t
.type
== tok_cmd
&& t
.cmd
== c_c
)
596 wtype
= word_WeakCode
;
597 else if (t
.type
== tok_cmd
&& t
.cmd
== c_e
&&
598 wtype
== word_WeakCode
)
601 error(err_brokencodepara
, &t
.pos
);
602 prev_para_type
= par
.type
;
604 while (t
.type
!= tok_eop
) /* error recovery: */
605 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
606 goto codeparabroken
; /* ick, but such is life */
609 prev_para_type
= par
.type
;
616 * Spot the special commands that define a grouping of more
617 * than one paragraph, and also the closing braces that
620 if (t
.type
== tok_cmd
&&
621 (t
.cmd
== c_lcont
|| t
.cmd
== c_quote
)) {
622 struct crossparaitem
*sitem
, *stop
;
626 * Expect, and swallow, an open brace.
628 dtor(t
), t
= get_token(in
);
629 if (t
.type
!= tok_lbrace
) {
630 error(err_explbr
, &t
.pos
);
634 if (cmd
== c_lcont
) {
636 * \lcont causes a continuation of a list item into
637 * multiple paragraphs (which may in turn contain
638 * nested lists, code paras etc). Hence, the previous
639 * paragraph must be of a list type.
641 sitem
= mknew(struct crossparaitem
);
642 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
646 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
648 if (prev_para_type
== para_Bullet
||
649 prev_para_type
== para_NumberedList
||
650 prev_para_type
== para_Description
) {
651 sitem
->type
= c_lcont
;
652 sitem
->seen_lcont
= 1;
653 par
.type
= para_LcontPush
;
654 prev_para_type
= par
.type
;
658 * Push a null item on the cross-para stack so that
659 * when we see the corresponding closing brace we
660 * don't give a cascade error.
663 error(err_misplacedlcont
, &t
.pos
);
667 * \quote causes a group of paragraphs to be
668 * block-quoted (typically they will be indented a
671 sitem
= mknew(struct crossparaitem
);
672 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
676 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
677 sitem
->type
= c_quote
;
678 sitem
->seen_quote
= 1;
679 par
.type
= para_QuotePush
;
680 prev_para_type
= par
.type
;
683 stk_push(crossparastk
, sitem
);
685 } else if (t
.type
== tok_rbrace
) {
686 struct crossparaitem
*sitem
= stk_pop(crossparastk
);
688 error(err_unexbrace
, &t
.pos
);
690 switch (sitem
->type
) {
692 par
.type
= para_LcontPop
;
693 prev_para_type
= par
.type
;
697 par
.type
= para_QuotePop
;
698 prev_para_type
= par
.type
;
708 * This token begins a paragraph. See if it's one of the
709 * special commands that define a paragraph type.
711 * (note that \# is special in a way, and \nocite takes no
714 par
.type
= para_Normal
;
715 if (t
.type
== tok_cmd
) {
717 int is_macro
= FALSE
;
725 error(err_badparatype
, t
.text
, &t
.pos
);
730 break; /* `\#{': isn't a comment para */
732 dtor(t
), t
= get_token(in
);
733 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
734 continue; /* next paragraph */
738 * 1 -- exactly one keyword
739 * 2 -- at least one keyword
740 * 4 -- any number of keywords including zero
741 * 8 -- at least one keyword and then nothing else
742 * 16 -- nothing at all! no keywords, no body
743 * 32 -- no keywords at all
745 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
746 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
747 case c_BR
: needkw
= 1; par
.type
= para_BR
;
748 start_cmd
= c_BR
; break;
749 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
750 case c_H
: needkw
= 2; par
.type
= para_Heading
;
753 case c_IM
: needkw
= 2; par
.type
= para_IM
;
754 start_cmd
= c_IM
; break;
755 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
756 par
.aux
= t
.aux
; break;
757 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
758 /* For \b and \n the keyword is optional */
759 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
760 case c_dt
: needkw
= 4; par
.type
= para_DescribedThing
; break;
761 case c_dd
: needkw
= 4; par
.type
= para_Description
; break;
762 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
763 case c_cfg
: needkw
= 8; par
.type
= para_Config
;
764 start_cmd
= c_cfg
; break;
765 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
766 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
767 /* For \nocite the keyword is _everything_ */
768 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
769 case c_preamble
: needkw
= 32; par
.type
= para_Normal
; break;
770 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
771 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
772 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
775 if (par
.type
== para_Chapter
||
776 par
.type
== para_Heading
||
777 par
.type
== para_Subsect
||
778 par
.type
== para_Appendix
||
779 par
.type
== para_UnnumberedChapter
) {
780 struct crossparaitem
*sitem
= stk_top(crossparastk
);
781 if (sitem
&& (sitem
->seen_lcont
|| sitem
->seen_quote
)) {
782 error(err_sectmarkerinblock
,
784 (sitem
->seen_lcont ?
"lcont" : "quote"));
789 rdstring rs
= { 0, 0, NULL
};
794 dtor(t
), t
= get_token(in
);
796 while (t
.type
== tok_lbrace
) {
797 /* This is a keyword. */
799 /* FIXME: there will be bugs if anyone specifies an
800 * empty keyword (\foo{}), so trap this case. */
801 while (dtor(t
), t
= get_token(in
),
802 t
.type
== tok_word
||
803 t
.type
== tok_white
||
804 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
805 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
)) {
806 if (t
.type
== tok_white
||
807 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
))
812 if (t
.type
!= tok_rbrace
) {
813 error(err_kwunclosed
, &t
.pos
);
816 rdadd(&rs
, 0); /* add string terminator */
817 dtor(t
), t
= get_token(in
); /* eat right brace */
820 rdadd(&rs
, 0); /* add string terminator */
822 /* See whether we have the right number of keywords. */
823 if ((needkw
& 48) && nkeys
> 0)
824 error(err_kwillegal
, &fp
);
825 if ((needkw
& 11) && nkeys
== 0)
826 error(err_kwexpected
, &fp
);
827 if ((needkw
& 5) && nkeys
> 1)
828 error(err_kwtoomany
, &fp
);
832 * Macro definition. Get the rest of the line
833 * as a code-paragraph token, repeatedly until
834 * there's nothing more left of it. Separate
837 rdstring macrotext
= { 0, 0, NULL
};
839 dtor(t
), t
= get_codepar_token(in
);
840 if (macrotext
.pos
> 0)
841 rdadd(¯otext
, L
'\n');
842 rdadds(¯otext
, t
.text
);
843 dtor(t
), t
= get_token(in
);
844 if (t
.type
== tok_eop
) break;
846 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
847 continue; /* next paragraph */
850 par
.keyword
= rdtrim(&rs
);
852 /* Move to EOP in case of needkw==8 or 16 (no body) */
854 /* We allow whitespace even when we expect no para body */
855 while (t
.type
== tok_white
)
856 dtor(t
), t
= get_token(in
);
857 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
858 (start_cmd
== c__invalid
||
859 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
)) {
860 error(err_bodyillegal
, &t
.pos
);
861 /* Error recovery: eat the rest of the paragraph */
862 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
863 (start_cmd
== c__invalid
||
864 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
))
865 dtor(t
), t
= get_token(in
);
867 if (t
.type
== tok_cmd
)
868 already
= TRUE
;/* inhibit get_token at top of loop */
869 prev_para_type
= par
.type
;
871 continue; /* next paragraph */
877 * Now read the actual paragraph, word by word, adding to
878 * the paragraph list.
880 * Mid-paragraph commands:
892 parsestk
= stk_new();
894 spcstyle
= word_WhiteSpace
;
897 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
901 /* Handle implicit paragraph breaks after \IM, \BR etc */
902 if (start_cmd
!= c__invalid
&&
903 t
.type
== tok_cmd
&& t
.cmd
== start_cmd
) {
904 already
= TRUE
; /* inhibit get_token at top of loop */
908 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
909 t
.type
= tok_word
; /* nice and simple */
910 t
.aux
= 0; /* even if `\-' - nonbreaking! */
912 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
913 t
.type
= tok_word
; /* nice and simple */
915 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
916 t
.aux
= 0; /* (nonbreaking) */
920 if (whptr
== &par
.words
)
921 break; /* strip whitespace at start of para */
930 * Inhibit use of whitespace if it's (probably the
931 * newline) before a repeat \IM / \BR type
934 if (start_cmd
!= c__invalid
) {
935 dtor(t
), t
= get_token(in
);
937 if (t
.type
== tok_cmd
&& t
.cmd
== start_cmd
)
942 rdadd(&indexstr
, ' ');
943 if (!indexing
|| index_visible
)
946 addword(wd
, &idximplicit
);
951 rdadds(&indexstr
, t
.text
);
957 if (!indexing
|| index_visible
) {
958 wd
.text
= ustrdup(t
.text
);
962 wd
.text
= ustrdup(t
.text
);
963 addword(wd
, &idximplicit
);
967 error(err_unexbrace
, &t
.pos
);
968 /* Error recovery: push nop */
969 sitem
= mknew(struct stack_item
);
970 sitem
->type
= stack_nop
;
971 stk_push(parsestk
, sitem
);
974 sitem
= stk_pop(parsestk
);
977 * This closing brace could have been an
978 * indication that the cross-paragraph stack
979 * wants popping. Accordingly, we treat it here
980 * as an indication that the paragraph is over.
985 if (sitem
->type
& stack_ualt
) {
986 whptr
= sitem
->whptr
;
987 idximplicit
= sitem
->idximplicit
;
989 if (sitem
->type
& stack_style
) {
991 spcstyle
= word_WhiteSpace
;
993 if (sitem
->type
& stack_idx
) {
994 indexword
->text
= ustrdup(indexstr
.text
);
996 ustrlow(indexword
->text
);
998 rdadd(&indexstr
, L
'\0');
999 index_merge(idx
, FALSE
, indexstr
.text
, idxwordlist
);
1000 sfree(indexstr
.text
);
1002 if (sitem
->type
& stack_hyper
) {
1004 wd
.type
= word_HyperEnd
;
1009 if (!indexing
|| index_visible
)
1010 addword(wd
, &whptr
);
1012 addword(wd
, &idximplicit
);
1014 if (sitem
->type
& stack_quote
) {
1016 wd
.type
= toquotestyle(style
);
1018 wd
.aux
= quote_Close
;
1021 if (!indexing
|| index_visible
)
1022 addword(wd
, &whptr
);
1024 rdadd(&indexstr
, L
'"');
1025 addword(wd
, &idximplicit
);
1035 * In-paragraph comment: \#{ balanced braces }
1037 * Anything goes here; even tok_eop. We should
1038 * eat whitespace after the close brace _if_
1039 * there was whitespace before the \#.
1041 dtor(t
), t
= get_token(in
);
1042 if (t
.type
!= tok_lbrace
) {
1043 error(err_explbr
, &t
.pos
);
1046 while (braces
> 0) {
1047 dtor(t
), t
= get_token(in
);
1048 if (t
.type
== tok_lbrace
)
1050 else if (t
.type
== tok_rbrace
)
1052 else if (t
.type
== tok_eof
) {
1053 error(err_commenteof
, &t
.pos
);
1060 dtor(t
), t
= get_token(in
);
1061 if (t
.type
== tok_white
) {
1068 dtor(t
), t
= get_token(in
);
1069 if (t
.type
!= tok_lbrace
) {
1070 error(err_explbr
, &t
.pos
);
1073 wd
.type
= toquotestyle(style
);
1075 wd
.aux
= quote_Open
;
1078 if (!indexing
|| index_visible
)
1079 addword(wd
, &whptr
);
1081 rdadd(&indexstr
, L
'"');
1082 addword(wd
, &idximplicit
);
1084 sitem
= mknew(struct stack_item
);
1085 sitem
->type
= stack_quote
;
1086 stk_push(parsestk
, sitem
);
1094 * Keyword, hyperlink, or \date. We expect a
1095 * left brace, some text, and then a right
1096 * brace. No nesting; no arguments.
1101 wd
.type
= word_UpperXref
;
1102 else if (t
.cmd
== c_k
)
1103 wd
.type
= word_LowerXref
;
1104 else if (t
.cmd
== c_W
)
1105 wd
.type
= word_HyperLink
;
1107 wd
.type
= word_Normal
;
1108 dtor(t
), t
= get_token(in
);
1109 if (t
.type
!= tok_lbrace
) {
1110 if (wd
.type
== word_Normal
) {
1111 time_t thetime
= time(NULL
);
1112 struct tm
*broken
= localtime(&thetime
);
1114 wdtext
= ustrftime(NULL
, broken
);
1117 error(err_explbr
, &t
.pos
);
1121 rdstring rs
= { 0, 0, NULL
};
1122 while (dtor(t
), t
= get_token(in
),
1123 t
.type
== tok_word
|| t
.type
== tok_white
) {
1124 if (t
.type
== tok_white
)
1127 rdadds(&rs
, t
.text
);
1129 if (wd
.type
== word_Normal
) {
1130 time_t thetime
= time(NULL
);
1131 struct tm
*broken
= localtime(&thetime
);
1132 wdtext
= ustrftime(rs
.text
, broken
);
1135 wdtext
= ustrdup(rs
.text
);
1138 if (t
.type
!= tok_rbrace
) {
1139 error(err_kwexprbr
, &t
.pos
);
1144 if (!indexing
|| index_visible
) {
1145 wd
.text
= ustrdup(wdtext
);
1146 addword(wd
, &whptr
);
1149 wd
.text
= ustrdup(wdtext
);
1150 addword(wd
, &idximplicit
);
1153 if (wd
.type
== word_HyperLink
) {
1155 * Hyperlinks are different: they then
1156 * expect another left brace, to begin
1157 * delimiting the text marked by the link.
1159 dtor(t
), t
= get_token(in
);
1161 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1163 sitem
= mknew(struct stack_item
);
1164 sitem
->type
= stack_hyper
;
1165 if (t
.type
== tok_cmd
&&
1166 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1167 if (style
!= word_Normal
)
1168 error(err_nestedstyles
, &t
.pos
);
1170 style
= (t
.cmd
== c_c ? word_Code
:
1171 t
.cmd
== c_cw ? word_WeakCode
:
1173 spcstyle
= tospacestyle(style
);
1174 sitem
->type
|= stack_style
;
1176 dtor(t
), t
= get_token(in
);
1178 if (t
.type
!= tok_lbrace
) {
1179 error(err_explbr
, &t
.pos
);
1182 stk_push(parsestk
, sitem
);
1190 if (style
!= word_Normal
) {
1191 error(err_nestedstyles
, &t
.pos
);
1192 /* Error recovery: eat lbrace, push nop. */
1193 dtor(t
), t
= get_token(in
);
1194 sitem
= mknew(struct stack_item
);
1195 sitem
->type
= stack_nop
;
1196 stk_push(parsestk
, sitem
);
1198 dtor(t
), t
= get_token(in
);
1199 if (t
.type
!= tok_lbrace
) {
1200 error(err_explbr
, &t
.pos
);
1202 style
= (type
== c_c ? word_Code
:
1203 type
== c_cw ? word_WeakCode
:
1205 spcstyle
= tospacestyle(style
);
1206 sitem
= mknew(struct stack_item
);
1207 sitem
->type
= stack_style
;
1208 stk_push(parsestk
, sitem
);
1216 error(err_nestedindex
, &t
.pos
);
1217 /* Error recovery: eat lbrace, push nop. */
1218 dtor(t
), t
= get_token(in
);
1219 sitem
= mknew(struct stack_item
);
1220 sitem
->type
= stack_nop
;
1221 stk_push(parsestk
, sitem
);
1223 sitem
= mknew(struct stack_item
);
1224 sitem
->type
= stack_idx
;
1225 dtor(t
), t
= get_token(in
);
1227 * Special cases: \i\c, \i\e, \i\cw
1230 if (t
.type
== tok_cmd
&&
1231 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1232 if (style
!= word_Normal
)
1233 error(err_nestedstyles
, &t
.pos
);
1235 style
= (t
.cmd
== c_c ? word_Code
:
1236 t
.cmd
== c_cw ? word_WeakCode
:
1238 spcstyle
= tospacestyle(style
);
1239 sitem
->type
|= stack_style
;
1241 dtor(t
), t
= get_token(in
);
1243 if (t
.type
!= tok_lbrace
) {
1245 error(err_explbr
, &t
.pos
);
1247 /* Add an index-reference word with no text as yet */
1248 wd
.type
= word_IndexRef
;
1253 indexword
= addword(wd
, &whptr
);
1254 /* Set up a rdstring to read the index text */
1256 /* Flags so that we do the Right Things with text */
1257 index_visible
= (type
!= c_I
);
1258 index_downcase
= (type
== c_ii
);
1261 idximplicit
= &idxwordlist
;
1262 /* Stack item to close the indexing on exit */
1263 stk_push(parsestk
, sitem
);
1268 utext
[0] = uchr
; utext
[1] = 0;
1274 if (!indexing
|| index_visible
) {
1275 wd
.text
= ustrdup(utext
);
1276 uword
= addword(wd
, &whptr
);
1280 wd
.text
= ustrdup(utext
);
1281 iword
= addword(wd
, &idximplicit
);
1284 dtor(t
), t
= get_token(in
);
1285 if (t
.type
== tok_lbrace
) {
1287 * \u with a left brace. Until the brace
1288 * closes, all further words go on a
1289 * sidetrack from the main thread of the
1292 sitem
= mknew(struct stack_item
);
1293 sitem
->type
= stack_ualt
;
1294 sitem
->whptr
= whptr
;
1295 sitem
->idximplicit
= idximplicit
;
1296 stk_push(parsestk
, sitem
);
1297 whptr
= uword ?
&uword
->alt
: NULL
;
1298 idximplicit
= iword ?
&iword
->alt
: NULL
;
1301 rdadd(&indexstr
, uchr
);
1306 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1307 error(err_badmidcmd
, t
.text
, &t
.pos
);
1312 dtor(t
), t
= get_token(in
);
1313 seenwhite
= iswhite
;
1316 /* Check the stack is empty */
1317 if (stk_top(parsestk
)) {
1318 while ((sitem
= stk_pop(parsestk
)))
1320 error(err_missingrbrace
, &t
.pos
);
1323 prev_para_type
= par
.type
;
1325 if (t
.type
== tok_eof
)
1329 if (stk_top(crossparastk
)) {
1332 error(err_missingrbrace2
, &t
.pos
);
1333 while ((p
= stk_pop(crossparastk
)))
1338 * We break to here rather than returning, because otherwise
1339 * this cleanup doesn't happen.
1342 macrocleanup(macros
);
1344 stk_free(crossparastk
);
1347 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1348 paragraph
*head
= NULL
;
1349 paragraph
**hptr
= &head
;
1351 while (in
->currindex
< in
->nfiles
) {
1352 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1354 setpos(in
, in
->filenames
[in
->currindex
]);
1355 read_file(&hptr
, in
, idx
);