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
);
534 * Loop on each paragraph.
537 int start_cmd
= c__invalid
;
546 dtor(t
), t
= get_token(in
);
549 if (t
.type
== tok_eof
)
553 * Parse code paragraphs separately.
555 if (t
.type
== tok_cmd
&& t
.cmd
== c_c
&& !isbrace(in
)) {
556 par
.type
= para_Code
;
559 dtor(t
), t
= get_codepar_token(in
);
560 wd
.type
= word_WeakCode
;
561 wd
.breaks
= FALSE
; /* shouldn't need this... */
562 wd
.text
= ustrdup(t
.text
);
566 dtor(t
), t
= get_token(in
);
567 if (t
.type
== tok_white
) {
569 * The newline after a code-paragraph line
571 dtor(t
), t
= get_token(in
);
573 if (t
.type
== tok_eop
|| t
.type
== tok_eof
)
575 else if (t
.type
!= tok_cmd
|| t
.cmd
!= c_c
) {
576 error(err_brokencodepara
, &t
.pos
);
578 while (t
.type
!= tok_eop
) /* error recovery: */
579 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
580 goto codeparabroken
; /* ick, but such is life */
589 * This token begins a paragraph. See if it's one of the
590 * special commands that define a paragraph type.
592 * (note that \# is special in a way, and \nocite takes no
595 par
.type
= para_Normal
;
596 if (t
.type
== tok_cmd
) {
598 int is_macro
= FALSE
;
606 error(err_badparatype
, t
.text
, &t
.pos
);
611 break; /* `\#{': isn't a comment para */
613 dtor(t
), t
= get_token(in
);
614 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
615 continue; /* next paragraph */
619 * 1 -- exactly one keyword
620 * 2 -- at least one keyword
621 * 4 -- any number of keywords including zero
622 * 8 -- at least one keyword and then nothing else
623 * 16 -- nothing at all! no keywords, no body
624 * 32 -- no keywords at all
626 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
627 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
628 case c_BR
: needkw
= 1; par
.type
= para_BR
;
629 start_cmd
= c_BR
; break;
630 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
631 case c_H
: needkw
= 2; par
.type
= para_Heading
;
634 case c_IM
: needkw
= 2; par
.type
= para_IM
;
635 start_cmd
= c_IM
; break;
636 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
637 par
.aux
= t
.aux
; break;
638 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
639 /* For \b and \n the keyword is optional */
640 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
641 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
642 case c_cfg
: needkw
= 8; par
.type
= para_Config
;
643 start_cmd
= c_cfg
; break;
644 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
645 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
646 /* For \nocite the keyword is _everything_ */
647 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
648 case c_preamble
: needkw
= 32; par
.type
= para_Preamble
; break;
649 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
650 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
651 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
655 rdstring rs
= { 0, 0, NULL
};
660 dtor(t
), t
= get_token(in
);
662 while (t
.type
== tok_lbrace
) {
663 /* This is a keyword. */
665 /* FIXME: there will be bugs if anyone specifies an
666 * empty keyword (\foo{}), so trap this case. */
667 while (dtor(t
), t
= get_token(in
),
668 t
.type
== tok_word
||
669 t
.type
== tok_white
||
670 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
671 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
)) {
672 if (t
.type
== tok_white
||
673 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
))
678 if (t
.type
!= tok_rbrace
) {
679 error(err_kwunclosed
, &t
.pos
);
682 rdadd(&rs
, 0); /* add string terminator */
683 dtor(t
), t
= get_token(in
); /* eat right brace */
686 rdadd(&rs
, 0); /* add string terminator */
688 /* See whether we have the right number of keywords. */
689 if ((needkw
& 48) && nkeys
> 0)
690 error(err_kwillegal
, &fp
);
691 if ((needkw
& 11) && nkeys
== 0)
692 error(err_kwexpected
, &fp
);
693 if ((needkw
& 5) && nkeys
> 1)
694 error(err_kwtoomany
, &fp
);
698 * Macro definition. Get the rest of the line
699 * as a code-paragraph token, repeatedly until
700 * there's nothing more left of it. Separate
703 rdstring macrotext
= { 0, 0, NULL
};
705 dtor(t
), t
= get_codepar_token(in
);
706 if (macrotext
.pos
> 0)
707 rdadd(¯otext
, L
'\n');
708 rdadds(¯otext
, t
.text
);
709 dtor(t
), t
= get_token(in
);
710 if (t
.type
== tok_eop
) break;
712 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
713 continue; /* next paragraph */
716 par
.keyword
= rdtrim(&rs
);
718 /* Move to EOP in case of needkw==8 or 16 (no body) */
720 /* We allow whitespace even when we expect no para body */
721 while (t
.type
== tok_white
)
722 dtor(t
), t
= get_token(in
);
723 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
724 (start_cmd
== c__invalid
||
725 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
)) {
726 error(err_bodyillegal
, &t
.pos
);
727 /* Error recovery: eat the rest of the paragraph */
728 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
729 (start_cmd
== c__invalid
||
730 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
))
731 dtor(t
), t
= get_token(in
);
733 if (t
.type
== tok_cmd
)
734 already
= TRUE
;/* inhibit get_token at top of loop */
736 continue; /* next paragraph */
742 * Now read the actual paragraph, word by word, adding to
743 * the paragraph list.
745 * Mid-paragraph commands:
757 parsestk
= stk_new();
759 spcstyle
= word_WhiteSpace
;
762 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
766 /* Handle implicit paragraph breaks after \IM, \BR etc */
767 if (start_cmd
!= c__invalid
&&
768 t
.type
== tok_cmd
&& t
.cmd
== start_cmd
) {
769 already
= TRUE
; /* inhibit get_token at top of loop */
773 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
774 t
.type
= tok_word
; /* nice and simple */
775 t
.aux
= 0; /* even if `\-' - nonbreaking! */
777 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
778 t
.type
= tok_word
; /* nice and simple */
780 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
781 t
.aux
= 0; /* (nonbreaking) */
785 if (whptr
== &par
.words
)
786 break; /* strip whitespace at start of para */
795 * Inhibit use of whitespace if it's (probably the
796 * newline) before a repeat \IM / \BR type
799 if (start_cmd
!= c__invalid
) {
800 dtor(t
), t
= get_token(in
);
802 if (t
.type
== tok_cmd
&& t
.cmd
== start_cmd
)
807 rdadd(&indexstr
, ' ');
808 if (!indexing
|| index_visible
)
811 addword(wd
, &idximplicit
);
816 rdadds(&indexstr
, t
.text
);
822 if (!indexing
|| index_visible
) {
823 wd
.text
= ustrdup(t
.text
);
827 wd
.text
= ustrdup(t
.text
);
828 addword(wd
, &idximplicit
);
832 error(err_unexbrace
, &t
.pos
);
833 /* Error recovery: push nop */
834 sitem
= mknew(struct stack_item
);
835 sitem
->type
= stack_nop
;
836 stk_push(parsestk
, sitem
);
839 sitem
= stk_pop(parsestk
);
841 error(err_unexbrace
, &t
.pos
);
843 if (sitem
->type
& stack_ualt
) {
844 whptr
= sitem
->whptr
;
845 idximplicit
= sitem
->idximplicit
;
847 if (sitem
->type
& stack_style
) {
849 spcstyle
= word_WhiteSpace
;
851 if (sitem
->type
& stack_idx
) {
852 indexword
->text
= ustrdup(indexstr
.text
);
854 ustrlow(indexword
->text
);
856 rdadd(&indexstr
, L
'\0');
857 index_merge(idx
, FALSE
, indexstr
.text
, idxwordlist
);
858 sfree(indexstr
.text
);
860 if (sitem
->type
& stack_hyper
) {
862 wd
.type
= word_HyperEnd
;
867 if (!indexing
|| index_visible
)
870 addword(wd
, &idximplicit
);
872 if (sitem
->type
& stack_quote
) {
874 wd
.type
= toquotestyle(style
);
876 wd
.aux
= quote_Close
;
879 if (!indexing
|| index_visible
)
882 rdadd(&indexstr
, L
'"');
883 addword(wd
, &idximplicit
);
893 * In-paragraph comment: \#{ balanced braces }
895 * Anything goes here; even tok_eop. We should
896 * eat whitespace after the close brace _if_
897 * there was whitespace before the \#.
899 dtor(t
), t
= get_token(in
);
900 if (t
.type
!= tok_lbrace
) {
901 error(err_explbr
, &t
.pos
);
905 dtor(t
), t
= get_token(in
);
906 if (t
.type
== tok_lbrace
)
908 else if (t
.type
== tok_rbrace
)
910 else if (t
.type
== tok_eof
) {
911 error(err_commenteof
, &t
.pos
);
918 dtor(t
), t
= get_token(in
);
919 if (t
.type
== tok_white
) {
926 dtor(t
), t
= get_token(in
);
927 if (t
.type
!= tok_lbrace
) {
928 error(err_explbr
, &t
.pos
);
931 wd
.type
= toquotestyle(style
);
936 if (!indexing
|| index_visible
)
939 rdadd(&indexstr
, L
'"');
940 addword(wd
, &idximplicit
);
942 sitem
= mknew(struct stack_item
);
943 sitem
->type
= stack_quote
;
944 stk_push(parsestk
, sitem
);
952 * Keyword, hyperlink, or \date. We expect a
953 * left brace, some text, and then a right
954 * brace. No nesting; no arguments.
959 wd
.type
= word_UpperXref
;
960 else if (t
.cmd
== c_k
)
961 wd
.type
= word_LowerXref
;
962 else if (t
.cmd
== c_W
)
963 wd
.type
= word_HyperLink
;
965 wd
.type
= word_Normal
;
966 dtor(t
), t
= get_token(in
);
967 if (t
.type
!= tok_lbrace
) {
968 if (wd
.type
== word_Normal
) {
969 time_t thetime
= time(NULL
);
970 struct tm
*broken
= localtime(&thetime
);
972 wdtext
= ustrftime(NULL
, broken
);
975 error(err_explbr
, &t
.pos
);
979 rdstring rs
= { 0, 0, NULL
};
980 while (dtor(t
), t
= get_token(in
),
981 t
.type
== tok_word
|| t
.type
== tok_white
) {
982 if (t
.type
== tok_white
)
987 if (wd
.type
== word_Normal
) {
988 time_t thetime
= time(NULL
);
989 struct tm
*broken
= localtime(&thetime
);
990 wdtext
= ustrftime(rs
.text
, broken
);
993 wdtext
= ustrdup(rs
.text
);
996 if (t
.type
!= tok_rbrace
) {
997 error(err_kwexprbr
, &t
.pos
);
1002 if (!indexing
|| index_visible
) {
1003 wd
.text
= ustrdup(wdtext
);
1004 addword(wd
, &whptr
);
1007 wd
.text
= ustrdup(wdtext
);
1008 addword(wd
, &idximplicit
);
1011 if (wd
.type
== word_HyperLink
) {
1013 * Hyperlinks are different: they then
1014 * expect another left brace, to begin
1015 * delimiting the text marked by the link.
1017 dtor(t
), t
= get_token(in
);
1019 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1021 sitem
= mknew(struct stack_item
);
1022 sitem
->type
= stack_hyper
;
1023 if (t
.type
== tok_cmd
&&
1024 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1025 if (style
!= word_Normal
)
1026 error(err_nestedstyles
, &t
.pos
);
1028 style
= (t
.cmd
== c_c ? word_Code
:
1029 t
.cmd
== c_cw ? word_WeakCode
:
1031 spcstyle
= tospacestyle(style
);
1032 sitem
->type
|= stack_style
;
1034 dtor(t
), t
= get_token(in
);
1036 if (t
.type
!= tok_lbrace
) {
1037 error(err_explbr
, &t
.pos
);
1040 stk_push(parsestk
, sitem
);
1048 if (style
!= word_Normal
) {
1049 error(err_nestedstyles
, &t
.pos
);
1050 /* Error recovery: eat lbrace, push nop. */
1051 dtor(t
), t
= get_token(in
);
1052 sitem
= mknew(struct stack_item
);
1053 sitem
->type
= stack_nop
;
1054 stk_push(parsestk
, sitem
);
1056 dtor(t
), t
= get_token(in
);
1057 if (t
.type
!= tok_lbrace
) {
1058 error(err_explbr
, &t
.pos
);
1060 style
= (type
== c_c ? word_Code
:
1061 type
== c_cw ? word_WeakCode
:
1063 spcstyle
= tospacestyle(style
);
1064 sitem
= mknew(struct stack_item
);
1065 sitem
->type
= stack_style
;
1066 stk_push(parsestk
, sitem
);
1074 error(err_nestedindex
, &t
.pos
);
1075 /* Error recovery: eat lbrace, push nop. */
1076 dtor(t
), t
= get_token(in
);
1077 sitem
= mknew(struct stack_item
);
1078 sitem
->type
= stack_nop
;
1079 stk_push(parsestk
, sitem
);
1081 sitem
= mknew(struct stack_item
);
1082 sitem
->type
= stack_idx
;
1083 dtor(t
), t
= get_token(in
);
1085 * Special cases: \i\c, \i\e, \i\cw
1088 if (t
.type
== tok_cmd
&&
1089 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1090 if (style
!= word_Normal
)
1091 error(err_nestedstyles
, &t
.pos
);
1093 style
= (t
.cmd
== c_c ? word_Code
:
1094 t
.cmd
== c_cw ? word_WeakCode
:
1096 spcstyle
= tospacestyle(style
);
1097 sitem
->type
|= stack_style
;
1099 dtor(t
), t
= get_token(in
);
1101 if (t
.type
!= tok_lbrace
) {
1103 error(err_explbr
, &t
.pos
);
1105 /* Add an index-reference word with no text as yet */
1106 wd
.type
= word_IndexRef
;
1111 indexword
= addword(wd
, &whptr
);
1112 /* Set up a rdstring to read the index text */
1114 /* Flags so that we do the Right Things with text */
1115 index_visible
= (type
!= c_I
);
1116 index_downcase
= (type
== c_ii
);
1119 idximplicit
= &idxwordlist
;
1120 /* Stack item to close the indexing on exit */
1121 stk_push(parsestk
, sitem
);
1126 utext
[0] = uchr
; utext
[1] = 0;
1132 if (!indexing
|| index_visible
) {
1133 wd
.text
= ustrdup(utext
);
1134 uword
= addword(wd
, &whptr
);
1138 wd
.text
= ustrdup(utext
);
1139 iword
= addword(wd
, &idximplicit
);
1142 dtor(t
), t
= get_token(in
);
1143 if (t
.type
== tok_lbrace
) {
1145 * \u with a left brace. Until the brace
1146 * closes, all further words go on a
1147 * sidetrack from the main thread of the
1150 sitem
= mknew(struct stack_item
);
1151 sitem
->type
= stack_ualt
;
1152 sitem
->whptr
= whptr
;
1153 sitem
->idximplicit
= idximplicit
;
1154 stk_push(parsestk
, sitem
);
1155 whptr
= uword ?
&uword
->alt
: NULL
;
1156 idximplicit
= iword ?
&iword
->alt
: NULL
;
1159 rdadd(&indexstr
, uchr
);
1164 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1165 error(err_badmidcmd
, t
.text
, &t
.pos
);
1170 dtor(t
), t
= get_token(in
);
1171 seenwhite
= iswhite
;
1173 /* Check the stack is empty */
1174 if (NULL
!= (sitem
= stk_pop(parsestk
))) {
1177 sitem
= stk_pop(parsestk
);
1179 error(err_missingrbrace
, &t
.pos
);
1186 * We break to here rather than returning, because otherwise
1187 * this cleanup doesn't happen.
1190 macrocleanup(macros
);
1193 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1194 paragraph
*head
= NULL
;
1195 paragraph
**hptr
= &head
;
1197 while (in
->currindex
< in
->nfiles
) {
1198 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1200 setpos(in
, in
->filenames
[in
->currindex
]);
1201 read_file(&hptr
, in
, idx
);