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_define
, /* macro definition */
185 c_i
, /* visible index mark */
186 c_ii
, /* uncapitalised visible index mark */
187 c_k
, /* uncapitalised cross-reference */
188 c_n
, /* numbered list */
189 c_nocite
, /* bibliography trickery */
190 c_preamble
, /* document preamble text */
191 c_q
, /* quote marks */
192 c_rule
, /* horizontal rule */
193 c_title
, /* document title */
194 c_u
, /* aux field is char code */
195 c_versionid
/* document RCS id */
198 /* Perhaps whitespace should be defined in a more Unicode-friendly way? */
199 #define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 )
200 #define isnl(c) ( (c)==10 )
201 #define isdec(c) ( ((c)>='0'&&(c)<='9') )
202 #define fromdec(c) ( (c)-'0' )
203 #define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f'))
204 #define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) )
205 #define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
208 * Keyword comparison function. Like strcmp, but between a wchar_t *
211 static int kwcmp(wchar_t const *p
, char const *q
) {
215 } while (*p
++ && *q
++ && !i
);
222 static void match_kw(token
*tok
) {
224 * FIXME. The ids are explicit in here so as to allow long-name
225 * equivalents to the various very short keywords.
227 static const struct { char const *name
; int id
; } keywords
[] = {
228 {"#", c__comment
}, /* comment command (\#) */
229 {"-", c__escaped
}, /* nonbreaking hyphen */
230 {"A", c_A
}, /* appendix heading */
231 {"B", c_B
}, /* bibliography entry */
232 {"BR", c_BR
}, /* bibliography rewrite */
233 {"C", c_C
}, /* chapter heading */
234 {"H", c_H
}, /* heading */
235 {"I", c_I
}, /* invisible index mark */
236 {"IM", c_IM
}, /* index merge/rewrite */
237 {"K", c_K
}, /* capitalised cross-reference */
238 {"U", c_U
}, /* unnumbered-chapter heading */
239 {"W", c_W
}, /* Web hyperlink */
240 {"\\", c__escaped
}, /* escaped backslash (\\) */
241 {"_", c__nbsp
}, /* nonbreaking space (\_) */
242 {"b", c_b
}, /* bulletted list */
243 {"c", c_c
}, /* code */
244 {"cfg", c_cfg
}, /* configuration directive */
245 {"copyright", c_copyright
}, /* copyright statement */
246 {"cw", c_cw
}, /* weak code */
247 {"date", c_date
}, /* document processing date */
248 {"define", c_define
}, /* macro definition */
249 {"e", c_e
}, /* emphasis */
250 {"i", c_i
}, /* visible index mark */
251 {"ii", c_ii
}, /* uncapitalised visible index mark */
252 {"k", c_k
}, /* uncapitalised cross-reference */
253 {"n", c_n
}, /* numbered list */
254 {"nocite", c_nocite
}, /* bibliography trickery */
255 {"preamble", c_preamble
}, /* document preamble text */
256 {"q", c_q
}, /* quote marks */
257 {"rule", c_rule
}, /* horizontal rule */
258 {"title", c_title
}, /* document title */
259 {"versionid", c_versionid
}, /* document RCS id */
260 {"{", c__escaped
}, /* escaped lbrace (\{) */
261 {"}", c__escaped
}, /* escaped rbrace (\}) */
266 * Special cases: \S{0,1,2,...} and \uABCD. If the syntax
267 * doesn't match correctly, we just fall through to the
268 * binary-search phase.
270 if (tok
->text
[0] == 'S') {
271 /* We expect numeric characters thereafter. */
272 wchar_t *p
= tok
->text
+1;
278 while (*p
&& isdec(*p
)) {
279 n
= 10 * n
+ fromdec(*p
);
288 } else if (tok
->text
[0] == 'u') {
289 /* We expect hex characters thereafter. */
290 wchar_t *p
= tok
->text
+1;
292 while (*p
&& ishex(*p
)) {
293 n
= 16 * n
+ fromhex(*p
);
304 j
= sizeof(keywords
)/sizeof(*keywords
);
307 c
= kwcmp(tok
->text
, keywords
[k
].name
);
313 tok
->cmd
= keywords
[k
].id
;
318 tok
->cmd
= c__invalid
;
323 * Read a token from the input file, in the normal way (`normal' in
324 * the sense that code paragraphs work a different way).
326 token
get_token(input
*in
) {
330 rdstring rs
= { 0, 0, NULL
};
333 ret
.text
= NULL
; /* default */
336 if (iswhite(c
)) { /* tok_white or tok_eop */
341 } while ((c
= get(in
, &cpos
)) != EOF
&& iswhite(c
));
347 ret
.type
= (nls
> 1 ? tok_eop
: tok_white
);
349 } else if (c
== EOF
) { /* tok_eof */
352 } else if (c
== '\\') { /* tok_cmd */
354 if (c
== '-' || c
== '\\' || c
== '_' ||
355 c
== '#' || c
== '{' || c
== '}') {
356 /* single-char command */
358 } else if (c
== 'u') {
364 } while (ishex(c
) && len
< 5);
366 } else if (iscmd(c
)) {
374 * Now match the command against the list of available
378 ret
.text
= ustrdup(rs
.text
);
382 } else if (c
== '{') { /* tok_lbrace */
383 ret
.type
= tok_lbrace
;
385 } else if (c
== '}') { /* tok_rbrace */
386 ret
.type
= tok_rbrace
;
388 } else { /* tok_word */
390 * Read a word: the longest possible contiguous sequence of
391 * things other than whitespace, backslash, braces and
392 * hyphen. A hyphen terminates the word but is returned as
393 * part of it; everything else is pushed back for the next
394 * token. The `aux' field contains TRUE if the word ends in
397 ret
.aux
= FALSE
; /* assumed for now */
399 if (iswhite(c
) || c
=='{' || c
=='}' || c
=='\\' || c
==EOF
) {
400 /* Put back the character that caused termination */
407 break; /* hyphen terminates word */
413 ret
.text
= ustrdup(rs
.text
);
420 * Determine whether the next input character is an open brace (for
421 * telling code paragraphs from paragraphs which merely start with
424 int isbrace(input
*in
) {
434 * Read the rest of a line that starts `\c'. Including nothing at
435 * all (tok_word with empty text).
437 token
get_codepar_token(input
*in
) {
440 rdstring rs
= { 0, 0, NULL
};
444 c
= get(in
, &cpos
); /* expect (and discard) one space */
450 while (!isnl(c
) && c
!= EOF
) {
453 /* Discard \r just before \n. */
454 if (c2
!= 13 || !isnl(c
))
458 ret
.text
= ustrdup(rs
.text
);
464 * Adds a new word to a linked list
466 static word
*addword(word newword
, word
***hptrptr
) {
470 mnewword
= mknew(word
);
471 *mnewword
= newword
; /* structure copy */
472 mnewword
->next
= NULL
;
473 **hptrptr
= mnewword
;
474 *hptrptr
= &mnewword
->next
;
479 * Adds a new paragraph to a linked list
481 static paragraph
*addpara(paragraph newpara
, paragraph
***hptrptr
) {
482 paragraph
*mnewpara
= mknew(paragraph
);
483 *mnewpara
= newpara
; /* structure copy */
484 mnewpara
->next
= NULL
;
485 **hptrptr
= mnewpara
;
486 *hptrptr
= &mnewpara
->next
;
491 * Destructor before token is reassigned; should catch most memory
494 #define dtor(t) ( sfree(t.text) )
497 * Reads a single file (ie until get() returns EOF)
499 static void read_file(paragraph
***ret
, input
*in
, indexdata
*idx
) {
502 word wd
, **whptr
, **idximplicit
;
504 wchar_t utext
[2], *wdtext
;
507 int iswhite
, seenwhite
;
511 stack_nop
= 0, /* do nothing (for error recovery) */
512 stack_ualt
= 1, /* \u alternative */
513 stack_style
= 2, /* \e, \c, \cw */
514 stack_idx
= 4, /* \I, \i, \ii */
515 stack_hyper
= 8, /* \W */
516 stack_quote
= 16, /* \q */
518 word
**whptr
; /* to restore from \u alternatives */
519 word
**idximplicit
; /* to restore from \u alternatives */
522 word
*indexword
, *uword
, *iword
;
525 int index_downcase
, index_visible
, indexing
;
526 const rdstring nullrs
= { 0, 0, NULL
};
530 macros
= newtree234(macrocmp
);
533 * Loop on each paragraph.
543 dtor(t
), t
= get_token(in
);
544 if (t
.type
== tok_eof
)
548 * Parse code paragraphs separately.
550 if (t
.type
== tok_cmd
&& t
.cmd
== c_c
&& !isbrace(in
)) {
551 par
.type
= para_Code
;
554 dtor(t
), t
= get_codepar_token(in
);
555 wd
.type
= word_WeakCode
;
556 wd
.breaks
= FALSE
; /* shouldn't need this... */
557 wd
.text
= ustrdup(t
.text
);
561 dtor(t
), t
= get_token(in
);
562 if (t
.type
== tok_white
) {
564 * The newline after a code-paragraph line
566 dtor(t
), t
= get_token(in
);
568 if (t
.type
== tok_eop
|| t
.type
== tok_eof
)
570 else if (t
.type
!= tok_cmd
|| t
.cmd
!= c_c
) {
571 error(err_brokencodepara
, &t
.pos
);
573 while (t
.type
!= tok_eop
) /* error recovery: */
574 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
575 goto codeparabroken
; /* ick, but such is life */
584 * This token begins a paragraph. See if it's one of the
585 * special commands that define a paragraph type.
587 * (note that \# is special in a way, and \nocite takes no
590 par
.type
= para_Normal
;
591 if (t
.type
== tok_cmd
) {
593 int is_macro
= FALSE
;
601 error(err_badparatype
, t
.text
, &t
.pos
);
606 break; /* `\#{': isn't a comment para */
608 dtor(t
), t
= get_token(in
);
609 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
610 continue; /* next paragraph */
614 * 1 -- exactly one keyword
615 * 2 -- at least one keyword
616 * 4 -- any number of keywords including zero
617 * 8 -- at least one keyword and then nothing else
618 * 16 -- nothing at all! no keywords, no body
619 * 32 -- no keywords at all
621 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
622 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
623 case c_BR
: needkw
= 1; par
.type
= para_BR
; break;
624 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
625 case c_H
: needkw
= 2; par
.type
= para_Heading
;
628 case c_IM
: needkw
= 2; par
.type
= para_IM
; break;
629 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
630 par
.aux
= t
.aux
; break;
631 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
632 /* For \b and \n the keyword is optional */
633 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
634 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
635 case c_cfg
: needkw
= 8; par
.type
= para_Config
; break;
636 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
637 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
638 /* For \nocite the keyword is _everything_ */
639 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
640 case c_preamble
: needkw
= 32; par
.type
= para_Preamble
; break;
641 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
642 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
643 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
647 rdstring rs
= { 0, 0, NULL
};
652 dtor(t
), t
= get_token(in
);
654 while (t
.type
== tok_lbrace
) {
655 /* This is a keyword. */
657 /* FIXME: there will be bugs if anyone specifies an
658 * empty keyword (\foo{}), so trap this case. */
659 while (dtor(t
), t
= get_token(in
),
660 t
.type
== tok_word
||
661 t
.type
== tok_white
||
662 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
663 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
)) {
664 if (t
.type
== tok_white
||
665 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
))
670 if (t
.type
!= tok_rbrace
) {
671 error(err_kwunclosed
, &t
.pos
);
674 rdadd(&rs
, 0); /* add string terminator */
675 dtor(t
), t
= get_token(in
); /* eat right brace */
678 rdadd(&rs
, 0); /* add string terminator */
680 /* See whether we have the right number of keywords. */
681 if ((needkw
& 48) && nkeys
> 0)
682 error(err_kwillegal
, &fp
);
683 if ((needkw
& 11) && nkeys
== 0)
684 error(err_kwexpected
, &fp
);
685 if ((needkw
& 5) && nkeys
> 1)
686 error(err_kwtoomany
, &fp
);
690 * Macro definition. Get the rest of the line
691 * as a code-paragraph token, repeatedly until
692 * there's nothing more left of it. Separate
695 rdstring macrotext
= { 0, 0, NULL
};
697 dtor(t
), t
= get_codepar_token(in
);
698 if (macrotext
.pos
> 0)
699 rdadd(¯otext
, L
'\n');
700 rdadds(¯otext
, t
.text
);
701 dtor(t
), t
= get_token(in
);
702 if (t
.type
== tok_eop
) break;
704 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
705 continue; /* next paragraph */
708 par
.keyword
= rdtrim(&rs
);
710 /* Move to EOP in case of needkw==8 or 16 (no body) */
712 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
713 error(err_bodyillegal
, &t
.pos
);
714 /* Error recovery: eat the rest of the paragraph */
715 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
)
716 dtor(t
), t
= get_token(in
);
719 continue; /* next paragraph */
725 * Now read the actual paragraph, word by word, adding to
726 * the paragraph list.
728 * Mid-paragraph commands:
740 parsestk
= stk_new();
742 spcstyle
= word_WhiteSpace
;
745 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
748 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
749 t
.type
= tok_word
; /* nice and simple */
750 t
.aux
= 0; /* even if `\-' - nonbreaking! */
752 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
753 t
.type
= tok_word
; /* nice and simple */
755 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
756 t
.aux
= 0; /* (nonbreaking) */
760 if (whptr
== &par
.words
)
761 break; /* strip whitespace at start of para */
769 rdadd(&indexstr
, ' ');
770 if (!indexing
|| index_visible
)
773 addword(wd
, &idximplicit
);
778 rdadds(&indexstr
, t
.text
);
784 if (!indexing
|| index_visible
) {
785 wd
.text
= ustrdup(t
.text
);
789 wd
.text
= ustrdup(t
.text
);
790 addword(wd
, &idximplicit
);
794 error(err_unexbrace
, &t
.pos
);
795 /* Error recovery: push nop */
796 sitem
= mknew(struct stack_item
);
797 sitem
->type
= stack_nop
;
798 stk_push(parsestk
, sitem
);
801 sitem
= stk_pop(parsestk
);
803 error(err_unexbrace
, &t
.pos
);
805 if (sitem
->type
& stack_ualt
) {
806 whptr
= sitem
->whptr
;
807 idximplicit
= sitem
->idximplicit
;
809 if (sitem
->type
& stack_style
) {
811 spcstyle
= word_WhiteSpace
;
813 if (sitem
->type
& stack_idx
) {
814 indexword
->text
= ustrdup(indexstr
.text
);
816 ustrlow(indexword
->text
);
818 rdadd(&indexstr
, L
'\0');
819 index_merge(idx
, FALSE
, indexstr
.text
, idxwordlist
);
820 sfree(indexstr
.text
);
822 if (sitem
->type
& stack_hyper
) {
824 wd
.type
= word_HyperEnd
;
829 if (!indexing
|| index_visible
)
832 addword(wd
, &idximplicit
);
834 if (sitem
->type
& stack_quote
) {
836 wd
.type
= toquotestyle(style
);
838 wd
.aux
= quote_Close
;
841 if (!indexing
|| index_visible
)
844 rdadd(&indexstr
, L
'"');
845 addword(wd
, &idximplicit
);
855 * In-paragraph comment: \#{ balanced braces }
857 * Anything goes here; even tok_eop. We should
858 * eat whitespace after the close brace _if_
859 * there was whitespace before the \#.
861 dtor(t
), t
= get_token(in
);
862 if (t
.type
!= tok_lbrace
) {
863 error(err_explbr
, &t
.pos
);
867 dtor(t
), t
= get_token(in
);
868 if (t
.type
== tok_lbrace
)
870 else if (t
.type
== tok_rbrace
)
872 else if (t
.type
== tok_eof
) {
873 error(err_commenteof
, &t
.pos
);
880 dtor(t
), t
= get_token(in
);
881 if (t
.type
== tok_white
) {
888 dtor(t
), t
= get_token(in
);
889 if (t
.type
!= tok_lbrace
) {
890 error(err_explbr
, &t
.pos
);
893 wd
.type
= toquotestyle(style
);
898 if (!indexing
|| index_visible
)
901 rdadd(&indexstr
, L
'"');
902 addword(wd
, &idximplicit
);
904 sitem
= mknew(struct stack_item
);
905 sitem
->type
= stack_quote
;
906 stk_push(parsestk
, sitem
);
914 * Keyword, hyperlink, or \date. We expect a
915 * left brace, some text, and then a right
916 * brace. No nesting; no arguments.
921 wd
.type
= word_UpperXref
;
922 else if (t
.cmd
== c_k
)
923 wd
.type
= word_LowerXref
;
924 else if (t
.cmd
== c_W
)
925 wd
.type
= word_HyperLink
;
927 wd
.type
= word_Normal
;
928 dtor(t
), t
= get_token(in
);
929 if (t
.type
!= tok_lbrace
) {
930 if (wd
.type
== word_Normal
) {
931 time_t thetime
= time(NULL
);
932 struct tm
*broken
= localtime(&thetime
);
934 wdtext
= ustrftime(NULL
, broken
);
937 error(err_explbr
, &t
.pos
);
941 rdstring rs
= { 0, 0, NULL
};
942 while (dtor(t
), t
= get_token(in
),
943 t
.type
== tok_word
|| t
.type
== tok_white
) {
944 if (t
.type
== tok_white
)
949 if (wd
.type
== word_Normal
) {
950 time_t thetime
= time(NULL
);
951 struct tm
*broken
= localtime(&thetime
);
952 wdtext
= ustrftime(rs
.text
, broken
);
955 wdtext
= ustrdup(rs
.text
);
958 if (t
.type
!= tok_rbrace
) {
959 error(err_kwexprbr
, &t
.pos
);
964 if (!indexing
|| index_visible
) {
965 wd
.text
= ustrdup(wdtext
);
969 wd
.text
= ustrdup(wdtext
);
970 addword(wd
, &idximplicit
);
973 if (wd
.type
== word_HyperLink
) {
975 * Hyperlinks are different: they then
976 * expect another left brace, to begin
977 * delimiting the text marked by the link.
979 dtor(t
), t
= get_token(in
);
981 * Special cases: \W{}\c, \W{}\e, \W{}\cw
983 sitem
= mknew(struct stack_item
);
984 sitem
->type
= stack_hyper
;
985 if (t
.type
== tok_cmd
&&
986 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
987 if (style
!= word_Normal
)
988 error(err_nestedstyles
, &t
.pos
);
990 style
= (t
.cmd
== c_c ? word_Code
:
991 t
.cmd
== c_cw ? word_WeakCode
:
993 spcstyle
= tospacestyle(style
);
994 sitem
->type
|= stack_style
;
996 dtor(t
), t
= get_token(in
);
998 if (t
.type
!= tok_lbrace
) {
999 error(err_explbr
, &t
.pos
);
1002 stk_push(parsestk
, sitem
);
1010 if (style
!= word_Normal
) {
1011 error(err_nestedstyles
, &t
.pos
);
1012 /* Error recovery: eat lbrace, push nop. */
1013 dtor(t
), t
= get_token(in
);
1014 sitem
= mknew(struct stack_item
);
1015 sitem
->type
= stack_nop
;
1016 stk_push(parsestk
, sitem
);
1018 dtor(t
), t
= get_token(in
);
1019 if (t
.type
!= tok_lbrace
) {
1020 error(err_explbr
, &t
.pos
);
1022 style
= (type
== c_c ? word_Code
:
1023 type
== c_cw ? word_WeakCode
:
1025 spcstyle
= tospacestyle(style
);
1026 sitem
= mknew(struct stack_item
);
1027 sitem
->type
= stack_style
;
1028 stk_push(parsestk
, sitem
);
1036 error(err_nestedindex
, &t
.pos
);
1037 /* Error recovery: eat lbrace, push nop. */
1038 dtor(t
), t
= get_token(in
);
1039 sitem
= mknew(struct stack_item
);
1040 sitem
->type
= stack_nop
;
1041 stk_push(parsestk
, sitem
);
1043 sitem
= mknew(struct stack_item
);
1044 sitem
->type
= stack_idx
;
1045 dtor(t
), t
= get_token(in
);
1047 * Special cases: \i\c, \i\e, \i\cw
1050 if (t
.type
== tok_cmd
&&
1051 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1052 if (style
!= word_Normal
)
1053 error(err_nestedstyles
, &t
.pos
);
1055 style
= (t
.cmd
== c_c ? word_Code
:
1056 t
.cmd
== c_cw ? word_WeakCode
:
1058 spcstyle
= tospacestyle(style
);
1059 sitem
->type
|= stack_style
;
1061 dtor(t
), t
= get_token(in
);
1063 if (t
.type
!= tok_lbrace
) {
1065 error(err_explbr
, &t
.pos
);
1067 /* Add an index-reference word with no text as yet */
1068 wd
.type
= word_IndexRef
;
1073 indexword
= addword(wd
, &whptr
);
1074 /* Set up a rdstring to read the index text */
1076 /* Flags so that we do the Right Things with text */
1077 index_visible
= (type
!= c_I
);
1078 index_downcase
= (type
== c_ii
);
1081 idximplicit
= &idxwordlist
;
1082 /* Stack item to close the indexing on exit */
1083 stk_push(parsestk
, sitem
);
1088 utext
[0] = uchr
; utext
[1] = 0;
1094 if (!indexing
|| index_visible
) {
1095 wd
.text
= ustrdup(utext
);
1096 uword
= addword(wd
, &whptr
);
1100 wd
.text
= ustrdup(utext
);
1101 iword
= addword(wd
, &idximplicit
);
1104 dtor(t
), t
= get_token(in
);
1105 if (t
.type
== tok_lbrace
) {
1107 * \u with a left brace. Until the brace
1108 * closes, all further words go on a
1109 * sidetrack from the main thread of the
1112 sitem
= mknew(struct stack_item
);
1113 sitem
->type
= stack_ualt
;
1114 sitem
->whptr
= whptr
;
1115 sitem
->idximplicit
= idximplicit
;
1116 stk_push(parsestk
, sitem
);
1117 whptr
= uword ?
&uword
->alt
: NULL
;
1118 idximplicit
= iword ?
&iword
->alt
: NULL
;
1121 rdadd(&indexstr
, uchr
);
1126 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1127 error(err_badmidcmd
, t
.text
, &t
.pos
);
1132 dtor(t
), t
= get_token(in
);
1133 seenwhite
= iswhite
;
1135 /* Check the stack is empty */
1136 if (NULL
!= (sitem
= stk_pop(parsestk
))) {
1139 sitem
= stk_pop(parsestk
);
1141 error(err_missingrbrace
, &t
.pos
);
1147 macrocleanup(macros
);
1150 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1151 paragraph
*head
= NULL
;
1152 paragraph
**hptr
= &head
;
1154 while (in
->currindex
< in
->nfiles
) {
1155 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1157 setpos(in
, in
->filenames
[in
->currindex
]);
1158 read_file(&hptr
, in
, idx
);