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 */
166 c__nbsp
, /* nonbreaking space */
167 c_A
, /* appendix heading */
168 c_B
, /* bibliography entry */
169 c_BR
, /* bibliography rewrite */
170 c_C
, /* chapter heading */
172 c_I
, /* invisible index mark */
173 c_IM
, /* index merge/rewrite */
174 c_K
, /* capitalised cross-reference */
175 c_S
, /* aux field is 0, 1, 2, ... */
176 c_U
, /* unnumbered-chapter heading */
177 c_W
, /* Web hyperlink */
178 c_b
, /* bulletted list */
180 c_cfg
, /* configuration directive */
181 c_copyright
, /* copyright statement */
182 c_cw
, /* weak code */
183 c_date
, /* document processing date */
184 c_dd
, /* description list: description */
185 c_define
, /* macro definition */
186 c_dt
, /* description list: described thing */
188 c_i
, /* visible index mark */
189 c_ii
, /* uncapitalised visible index mark */
190 c_k
, /* uncapitalised cross-reference */
191 c_lcont
, /* continuation para(s) for list item */
192 c_n
, /* numbered list */
193 c_nocite
, /* bibliography trickery */
194 c_preamble
, /* (obsolete) preamble text */
195 c_q
, /* quote marks */
196 c_quote
, /* block-quoted paragraphs */
197 c_rule
, /* horizontal rule */
198 c_title
, /* document title */
199 c_u
, /* aux field is char code */
200 c_versionid
/* document RCS id */
203 /* Perhaps whitespace should be defined in a more Unicode-friendly way? */
204 #define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 )
205 #define isnl(c) ( (c)==10 )
206 #define isdec(c) ( ((c)>='0'&&(c)<='9') )
207 #define fromdec(c) ( (c)-'0' )
208 #define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f'))
209 #define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) )
210 #define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
213 * Keyword comparison function. Like strcmp, but between a wchar_t *
216 static int kwcmp(wchar_t const *p
, char const *q
) {
220 } while (*p
++ && *q
++ && !i
);
227 static void match_kw(token
*tok
) {
229 * FIXME. The ids are explicit in here so as to allow long-name
230 * equivalents to the various very short keywords.
232 static const struct { char const *name
; int id
; } keywords
[] = {
233 {"#", c__comment
}, /* comment command (\#) */
234 {"-", c__escaped
}, /* nonbreaking hyphen */
235 {".", c__nop
}, /* no-op */
236 {"A", c_A
}, /* appendix heading */
237 {"B", c_B
}, /* bibliography entry */
238 {"BR", c_BR
}, /* bibliography rewrite */
239 {"C", c_C
}, /* chapter heading */
240 {"H", c_H
}, /* heading */
241 {"I", c_I
}, /* invisible index mark */
242 {"IM", c_IM
}, /* index merge/rewrite */
243 {"K", c_K
}, /* capitalised cross-reference */
244 {"U", c_U
}, /* unnumbered-chapter heading */
245 {"W", c_W
}, /* Web hyperlink */
246 {"\\", c__escaped
}, /* escaped backslash (\\) */
247 {"_", c__nbsp
}, /* nonbreaking space (\_) */
248 {"b", c_b
}, /* bulletted list */
249 {"c", c_c
}, /* code */
250 {"cfg", c_cfg
}, /* configuration directive */
251 {"copyright", c_copyright
}, /* copyright statement */
252 {"cw", c_cw
}, /* weak code */
253 {"date", c_date
}, /* document processing date */
254 {"dd", c_dd
}, /* description list: description */
255 {"define", c_define
}, /* macro definition */
256 {"dt", c_dt
}, /* description list: described thing */
257 {"e", c_e
}, /* emphasis */
258 {"i", c_i
}, /* visible index mark */
259 {"ii", c_ii
}, /* uncapitalised visible index mark */
260 {"k", c_k
}, /* uncapitalised cross-reference */
261 {"lcont", c_lcont
}, /* continuation para(s) for list item */
262 {"n", c_n
}, /* numbered list */
263 {"nocite", c_nocite
}, /* bibliography trickery */
264 {"preamble", c_preamble
}, /* (obsolete) preamble text */
265 {"q", c_q
}, /* quote marks */
266 {"quote", c_quote
}, /* block-quoted paragraphs */
267 {"rule", c_rule
}, /* horizontal rule */
268 {"title", c_title
}, /* document title */
269 {"versionid", c_versionid
}, /* document RCS id */
270 {"{", c__escaped
}, /* escaped lbrace (\{) */
271 {"}", c__escaped
}, /* escaped rbrace (\}) */
276 * Special cases: \S{0,1,2,...} and \uABCD. If the syntax
277 * doesn't match correctly, we just fall through to the
278 * binary-search phase.
280 if (tok
->text
[0] == 'S') {
281 /* We expect numeric characters thereafter. */
282 wchar_t *p
= tok
->text
+1;
288 while (*p
&& isdec(*p
)) {
289 n
= 10 * n
+ fromdec(*p
);
298 } else if (tok
->text
[0] == 'u') {
299 /* We expect hex characters thereafter. */
300 wchar_t *p
= tok
->text
+1;
302 while (*p
&& ishex(*p
)) {
303 n
= 16 * n
+ fromhex(*p
);
314 j
= sizeof(keywords
)/sizeof(*keywords
);
317 c
= kwcmp(tok
->text
, keywords
[k
].name
);
323 tok
->cmd
= keywords
[k
].id
;
328 tok
->cmd
= c__invalid
;
333 * Read a token from the input file, in the normal way (`normal' in
334 * the sense that code paragraphs work a different way).
336 token
get_token(input
*in
) {
340 rdstring rs
= { 0, 0, NULL
};
343 ret
.text
= NULL
; /* default */
346 if (iswhite(c
)) { /* tok_white or tok_eop */
351 } while ((c
= get(in
, &cpos
)) != EOF
&& iswhite(c
));
357 ret
.type
= (nls
> 1 ? tok_eop
: tok_white
);
359 } else if (c
== EOF
) { /* tok_eof */
362 } else if (c
== '\\') { /* tok_cmd */
364 if (c
== '-' || c
== '\\' || c
== '_' ||
365 c
== '#' || c
== '{' || c
== '}' || c
== '.') {
366 /* single-char command */
368 } else if (c
== 'u') {
374 } while (ishex(c
) && len
< 5);
376 } else if (iscmd(c
)) {
384 * Now match the command against the list of available
388 ret
.text
= ustrdup(rs
.text
);
392 } else if (c
== '{') { /* tok_lbrace */
393 ret
.type
= tok_lbrace
;
395 } else if (c
== '}') { /* tok_rbrace */
396 ret
.type
= tok_rbrace
;
398 } else { /* tok_word */
400 * Read a word: the longest possible contiguous sequence of
401 * things other than whitespace, backslash, braces and
402 * hyphen. A hyphen terminates the word but is returned as
403 * part of it; everything else is pushed back for the next
404 * token. The `aux' field contains TRUE if the word ends in
407 ret
.aux
= FALSE
; /* assumed for now */
409 if (iswhite(c
) || c
=='{' || c
=='}' || c
=='\\' || c
==EOF
) {
410 /* Put back the character that caused termination */
417 break; /* hyphen terminates word */
423 ret
.text
= ustrdup(rs
.text
);
430 * Determine whether the next input character is an open brace (for
431 * telling code paragraphs from paragraphs which merely start with
434 int isbrace(input
*in
) {
444 * Read the rest of a line that starts `\c'. Including nothing at
445 * all (tok_word with empty text).
447 token
get_codepar_token(input
*in
) {
450 rdstring rs
= { 0, 0, NULL
};
454 c
= get(in
, &cpos
); /* expect (and discard) one space */
460 while (!isnl(c
) && c
!= EOF
) {
463 /* Discard \r just before \n. */
464 if (c2
!= 13 || !isnl(c
))
468 ret
.text
= ustrdup(rs
.text
);
474 * Adds a new word to a linked list
476 static word
*addword(word newword
, word
***hptrptr
) {
480 mnewword
= mknew(word
);
481 *mnewword
= newword
; /* structure copy */
482 mnewword
->next
= NULL
;
483 **hptrptr
= mnewword
;
484 *hptrptr
= &mnewword
->next
;
489 * Adds a new paragraph to a linked list
491 static paragraph
*addpara(paragraph newpara
, paragraph
***hptrptr
) {
492 paragraph
*mnewpara
= mknew(paragraph
);
493 *mnewpara
= newpara
; /* structure copy */
494 mnewpara
->next
= NULL
;
495 **hptrptr
= mnewpara
;
496 *hptrptr
= &mnewpara
->next
;
501 * Destructor before token is reassigned; should catch most memory
504 #define dtor(t) ( sfree(t.text) )
507 * Reads a single file (ie until get() returns EOF)
509 static void read_file(paragraph
***ret
, input
*in
, indexdata
*idx
) {
512 word wd
, **whptr
, **idximplicit
;
514 wchar_t utext
[2], *wdtext
;
517 int iswhite
, seenwhite
;
522 stack_nop
= 0, /* do nothing (for error recovery) */
523 stack_ualt
= 1, /* \u alternative */
524 stack_style
= 2, /* \e, \c, \cw */
525 stack_idx
= 4, /* \I, \i, \ii */
526 stack_hyper
= 8, /* \W */
527 stack_quote
= 16, /* \q */
529 word
**whptr
; /* to restore from \u alternatives */
530 word
**idximplicit
; /* to restore from \u alternatives */
533 struct crossparaitem
{
534 int type
; /* currently c_lcont, c_quote or -1 */
535 int seen_lcont
, seen_quote
;
538 word
*indexword
, *uword
, *iword
;
541 int index_downcase
, index_visible
, indexing
;
542 const rdstring nullrs
= { 0, 0, NULL
};
546 macros
= newtree234(macrocmp
);
549 crossparastk
= stk_new();
552 * Loop on each paragraph.
555 int start_cmd
= c__invalid
;
565 dtor(t
), t
= get_token(in
);
568 } while (t
.type
== tok_eop
);
569 if (t
.type
== tok_eof
)
573 * Parse code paragraphs separately.
575 if (t
.type
== tok_cmd
&& t
.cmd
== c_c
&& !isbrace(in
)) {
576 int wtype
= word_WeakCode
;
578 par
.type
= para_Code
;
581 dtor(t
), t
= get_codepar_token(in
);
583 wd
.breaks
= FALSE
; /* shouldn't need this... */
584 wd
.text
= ustrdup(t
.text
);
588 dtor(t
), t
= get_token(in
);
589 if (t
.type
== tok_white
) {
591 * The newline after a code-paragraph line
593 dtor(t
), t
= get_token(in
);
595 if (t
.type
== tok_eop
|| t
.type
== tok_eof
)
597 else if (t
.type
== tok_cmd
&& t
.cmd
== c_c
)
598 wtype
= word_WeakCode
;
599 else if (t
.type
== tok_cmd
&& t
.cmd
== c_e
&&
600 wtype
== word_WeakCode
)
603 error(err_brokencodepara
, &t
.pos
);
604 prev_para_type
= par
.type
;
606 while (t
.type
!= tok_eop
) /* error recovery: */
607 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
608 goto codeparabroken
; /* ick, but such is life */
611 prev_para_type
= par
.type
;
618 * Spot the special commands that define a grouping of more
619 * than one paragraph, and also the closing braces that
622 if (t
.type
== tok_cmd
&&
623 (t
.cmd
== c_lcont
|| t
.cmd
== c_quote
)) {
624 struct crossparaitem
*sitem
, *stop
;
628 * Expect, and swallow, an open brace.
630 dtor(t
), t
= get_token(in
);
631 if (t
.type
!= tok_lbrace
) {
632 error(err_explbr
, &t
.pos
);
636 if (cmd
== c_lcont
) {
638 * \lcont causes a continuation of a list item into
639 * multiple paragraphs (which may in turn contain
640 * nested lists, code paras etc). Hence, the previous
641 * paragraph must be of a list type.
643 sitem
= mknew(struct crossparaitem
);
644 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
648 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
650 if (prev_para_type
== para_Bullet
||
651 prev_para_type
== para_NumberedList
||
652 prev_para_type
== para_Description
) {
653 sitem
->type
= c_lcont
;
654 sitem
->seen_lcont
= 1;
655 par
.type
= para_LcontPush
;
656 prev_para_type
= par
.type
;
660 * Push a null item on the cross-para stack so that
661 * when we see the corresponding closing brace we
662 * don't give a cascade error.
665 error(err_misplacedlcont
, &t
.pos
);
669 * \quote causes a group of paragraphs to be
670 * block-quoted (typically they will be indented a
673 sitem
= mknew(struct crossparaitem
);
674 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
678 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
679 sitem
->type
= c_quote
;
680 sitem
->seen_quote
= 1;
681 par
.type
= para_QuotePush
;
682 prev_para_type
= par
.type
;
685 stk_push(crossparastk
, sitem
);
687 } else if (t
.type
== tok_rbrace
) {
688 struct crossparaitem
*sitem
= stk_pop(crossparastk
);
690 error(err_unexbrace
, &t
.pos
);
692 switch (sitem
->type
) {
694 par
.type
= para_LcontPop
;
695 prev_para_type
= par
.type
;
699 par
.type
= para_QuotePop
;
700 prev_para_type
= par
.type
;
710 * This token begins a paragraph. See if it's one of the
711 * special commands that define a paragraph type.
713 * (note that \# is special in a way, and \nocite takes no
716 par
.type
= para_Normal
;
717 if (t
.type
== tok_cmd
) {
719 int is_macro
= FALSE
;
727 error(err_badparatype
, t
.text
, &t
.pos
);
732 break; /* `\#{': isn't a comment para */
734 dtor(t
), t
= get_token(in
);
735 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
736 continue; /* next paragraph */
740 * 1 -- exactly one keyword
741 * 2 -- at least one keyword
742 * 4 -- any number of keywords including zero
743 * 8 -- at least one keyword and then nothing else
744 * 16 -- nothing at all! no keywords, no body
745 * 32 -- no keywords at all
747 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
748 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
749 case c_BR
: needkw
= 1; par
.type
= para_BR
;
750 start_cmd
= c_BR
; break;
751 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
752 case c_H
: needkw
= 2; par
.type
= para_Heading
;
755 case c_IM
: needkw
= 2; par
.type
= para_IM
;
756 start_cmd
= c_IM
; break;
757 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
758 par
.aux
= t
.aux
; break;
759 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
760 /* For \b and \n the keyword is optional */
761 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
762 case c_dt
: needkw
= 4; par
.type
= para_DescribedThing
; break;
763 case c_dd
: needkw
= 4; par
.type
= para_Description
; break;
764 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
765 case c_cfg
: needkw
= 8; par
.type
= para_Config
;
766 start_cmd
= c_cfg
; break;
767 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
768 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
769 /* For \nocite the keyword is _everything_ */
770 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
771 case c_preamble
: needkw
= 32; par
.type
= para_Normal
; break;
772 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
773 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
774 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
777 if (par
.type
== para_Chapter
||
778 par
.type
== para_Heading
||
779 par
.type
== para_Subsect
||
780 par
.type
== para_Appendix
||
781 par
.type
== para_UnnumberedChapter
) {
782 struct crossparaitem
*sitem
= stk_top(crossparastk
);
783 if (sitem
&& (sitem
->seen_lcont
|| sitem
->seen_quote
)) {
784 error(err_sectmarkerinblock
,
786 (sitem
->seen_lcont ?
"lcont" : "quote"));
791 rdstring rs
= { 0, 0, NULL
};
796 dtor(t
), t
= get_token(in
);
798 while (t
.type
== tok_lbrace
) {
799 /* This is a keyword. */
801 /* FIXME: there will be bugs if anyone specifies an
802 * empty keyword (\foo{}), so trap this case. */
803 while (dtor(t
), t
= get_token(in
),
804 t
.type
== tok_word
||
805 t
.type
== tok_white
||
806 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
807 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
)) {
808 if (t
.type
== tok_white
||
809 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
))
814 if (t
.type
!= tok_rbrace
) {
815 error(err_kwunclosed
, &t
.pos
);
818 rdadd(&rs
, 0); /* add string terminator */
819 dtor(t
), t
= get_token(in
); /* eat right brace */
822 rdadd(&rs
, 0); /* add string terminator */
824 /* See whether we have the right number of keywords. */
825 if ((needkw
& 48) && nkeys
> 0)
826 error(err_kwillegal
, &fp
);
827 if ((needkw
& 11) && nkeys
== 0)
828 error(err_kwexpected
, &fp
);
829 if ((needkw
& 5) && nkeys
> 1)
830 error(err_kwtoomany
, &fp
);
834 * Macro definition. Get the rest of the line
835 * as a code-paragraph token, repeatedly until
836 * there's nothing more left of it. Separate
839 rdstring macrotext
= { 0, 0, NULL
};
841 dtor(t
), t
= get_codepar_token(in
);
842 if (macrotext
.pos
> 0)
843 rdadd(¯otext
, L
'\n');
844 rdadds(¯otext
, t
.text
);
845 dtor(t
), t
= get_token(in
);
846 if (t
.type
== tok_eop
) break;
848 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
849 continue; /* next paragraph */
852 par
.keyword
= rdtrim(&rs
);
854 /* Move to EOP in case of needkw==8 or 16 (no body) */
856 /* We allow whitespace even when we expect no para body */
857 while (t
.type
== tok_white
)
858 dtor(t
), t
= get_token(in
);
859 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
860 (start_cmd
== c__invalid
||
861 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
)) {
862 error(err_bodyillegal
, &t
.pos
);
863 /* Error recovery: eat the rest of the paragraph */
864 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
865 (start_cmd
== c__invalid
||
866 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
))
867 dtor(t
), t
= get_token(in
);
869 if (t
.type
== tok_cmd
)
870 already
= TRUE
;/* inhibit get_token at top of loop */
871 prev_para_type
= par
.type
;
873 continue; /* next paragraph */
879 * Now read the actual paragraph, word by word, adding to
880 * the paragraph list.
882 * Mid-paragraph commands:
894 parsestk
= stk_new();
896 spcstyle
= word_WhiteSpace
;
899 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
903 /* Handle implicit paragraph breaks after \IM, \BR etc */
904 if (start_cmd
!= c__invalid
&&
905 t
.type
== tok_cmd
&& t
.cmd
== start_cmd
) {
906 already
= TRUE
; /* inhibit get_token at top of loop */
910 if (t
.type
== tok_cmd
&& t
.cmd
== c__nop
) {
911 dtor(t
), t
= get_token(in
);
912 continue; /* do nothing! */
915 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
916 t
.type
= tok_word
; /* nice and simple */
917 t
.aux
= 0; /* even if `\-' - nonbreaking! */
919 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
920 t
.type
= tok_word
; /* nice and simple */
922 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
923 t
.aux
= 0; /* (nonbreaking) */
927 if (whptr
== &par
.words
)
928 break; /* strip whitespace at start of para */
937 * Inhibit use of whitespace if it's (probably the
938 * newline) before a repeat \IM / \BR type
941 if (start_cmd
!= c__invalid
) {
942 dtor(t
), t
= get_token(in
);
944 if (t
.type
== tok_cmd
&& t
.cmd
== start_cmd
)
949 rdadd(&indexstr
, ' ');
950 if (!indexing
|| index_visible
)
953 addword(wd
, &idximplicit
);
958 rdadds(&indexstr
, t
.text
);
964 if (!indexing
|| index_visible
) {
965 wd
.text
= ustrdup(t
.text
);
969 wd
.text
= ustrdup(t
.text
);
970 addword(wd
, &idximplicit
);
974 error(err_unexbrace
, &t
.pos
);
975 /* Error recovery: push nop */
976 sitem
= mknew(struct stack_item
);
977 sitem
->type
= stack_nop
;
978 stk_push(parsestk
, sitem
);
981 sitem
= stk_pop(parsestk
);
984 * This closing brace could have been an
985 * indication that the cross-paragraph stack
986 * wants popping. Accordingly, we treat it here
987 * as an indication that the paragraph is over.
992 if (sitem
->type
& stack_ualt
) {
993 whptr
= sitem
->whptr
;
994 idximplicit
= sitem
->idximplicit
;
996 if (sitem
->type
& stack_style
) {
998 spcstyle
= word_WhiteSpace
;
1000 if (sitem
->type
& stack_idx
) {
1001 indexword
->text
= ustrdup(indexstr
.text
);
1003 ustrlow(indexword
->text
);
1005 rdadd(&indexstr
, L
'\0');
1006 index_merge(idx
, FALSE
, indexstr
.text
, idxwordlist
);
1007 sfree(indexstr
.text
);
1009 if (sitem
->type
& stack_hyper
) {
1011 wd
.type
= word_HyperEnd
;
1016 if (!indexing
|| index_visible
)
1017 addword(wd
, &whptr
);
1019 addword(wd
, &idximplicit
);
1021 if (sitem
->type
& stack_quote
) {
1023 wd
.type
= toquotestyle(style
);
1025 wd
.aux
= quote_Close
;
1028 if (!indexing
|| index_visible
)
1029 addword(wd
, &whptr
);
1031 rdadd(&indexstr
, L
'"');
1032 addword(wd
, &idximplicit
);
1042 * In-paragraph comment: \#{ balanced braces }
1044 * Anything goes here; even tok_eop. We should
1045 * eat whitespace after the close brace _if_
1046 * there was whitespace before the \#.
1048 dtor(t
), t
= get_token(in
);
1049 if (t
.type
!= tok_lbrace
) {
1050 error(err_explbr
, &t
.pos
);
1053 while (braces
> 0) {
1054 dtor(t
), t
= get_token(in
);
1055 if (t
.type
== tok_lbrace
)
1057 else if (t
.type
== tok_rbrace
)
1059 else if (t
.type
== tok_eof
) {
1060 error(err_commenteof
, &t
.pos
);
1067 dtor(t
), t
= get_token(in
);
1068 if (t
.type
== tok_white
) {
1075 dtor(t
), t
= get_token(in
);
1076 if (t
.type
!= tok_lbrace
) {
1077 error(err_explbr
, &t
.pos
);
1080 wd
.type
= toquotestyle(style
);
1082 wd
.aux
= quote_Open
;
1085 if (!indexing
|| index_visible
)
1086 addword(wd
, &whptr
);
1088 rdadd(&indexstr
, L
'"');
1089 addword(wd
, &idximplicit
);
1091 sitem
= mknew(struct stack_item
);
1092 sitem
->type
= stack_quote
;
1093 stk_push(parsestk
, sitem
);
1101 * Keyword, hyperlink, or \date. We expect a
1102 * left brace, some text, and then a right
1103 * brace. No nesting; no arguments.
1108 wd
.type
= word_UpperXref
;
1109 else if (t
.cmd
== c_k
)
1110 wd
.type
= word_LowerXref
;
1111 else if (t
.cmd
== c_W
)
1112 wd
.type
= word_HyperLink
;
1114 wd
.type
= word_Normal
;
1115 dtor(t
), t
= get_token(in
);
1116 if (t
.type
!= tok_lbrace
) {
1117 if (wd
.type
== word_Normal
) {
1118 time_t thetime
= time(NULL
);
1119 struct tm
*broken
= localtime(&thetime
);
1121 wdtext
= ustrftime(NULL
, broken
);
1124 error(err_explbr
, &t
.pos
);
1128 rdstring rs
= { 0, 0, NULL
};
1129 while (dtor(t
), t
= get_token(in
),
1130 t
.type
== tok_word
|| t
.type
== tok_white
) {
1131 if (t
.type
== tok_white
)
1134 rdadds(&rs
, t
.text
);
1136 if (wd
.type
== word_Normal
) {
1137 time_t thetime
= time(NULL
);
1138 struct tm
*broken
= localtime(&thetime
);
1139 wdtext
= ustrftime(rs
.text
, broken
);
1142 wdtext
= ustrdup(rs
.text
);
1145 if (t
.type
!= tok_rbrace
) {
1146 error(err_kwexprbr
, &t
.pos
);
1151 if (!indexing
|| index_visible
) {
1152 wd
.text
= ustrdup(wdtext
);
1153 addword(wd
, &whptr
);
1156 wd
.text
= ustrdup(wdtext
);
1157 addword(wd
, &idximplicit
);
1160 if (wd
.type
== word_HyperLink
) {
1162 * Hyperlinks are different: they then
1163 * expect another left brace, to begin
1164 * delimiting the text marked by the link.
1166 dtor(t
), t
= get_token(in
);
1168 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1170 sitem
= mknew(struct stack_item
);
1171 sitem
->type
= stack_hyper
;
1172 if (t
.type
== tok_cmd
&&
1173 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1174 if (style
!= word_Normal
)
1175 error(err_nestedstyles
, &t
.pos
);
1177 style
= (t
.cmd
== c_c ? word_Code
:
1178 t
.cmd
== c_cw ? word_WeakCode
:
1180 spcstyle
= tospacestyle(style
);
1181 sitem
->type
|= stack_style
;
1183 dtor(t
), t
= get_token(in
);
1185 if (t
.type
!= tok_lbrace
) {
1186 error(err_explbr
, &t
.pos
);
1189 stk_push(parsestk
, sitem
);
1197 if (style
!= word_Normal
) {
1198 error(err_nestedstyles
, &t
.pos
);
1199 /* Error recovery: eat lbrace, push nop. */
1200 dtor(t
), t
= get_token(in
);
1201 sitem
= mknew(struct stack_item
);
1202 sitem
->type
= stack_nop
;
1203 stk_push(parsestk
, sitem
);
1205 dtor(t
), t
= get_token(in
);
1206 if (t
.type
!= tok_lbrace
) {
1207 error(err_explbr
, &t
.pos
);
1209 style
= (type
== c_c ? word_Code
:
1210 type
== c_cw ? word_WeakCode
:
1212 spcstyle
= tospacestyle(style
);
1213 sitem
= mknew(struct stack_item
);
1214 sitem
->type
= stack_style
;
1215 stk_push(parsestk
, sitem
);
1223 error(err_nestedindex
, &t
.pos
);
1224 /* Error recovery: eat lbrace, push nop. */
1225 dtor(t
), t
= get_token(in
);
1226 sitem
= mknew(struct stack_item
);
1227 sitem
->type
= stack_nop
;
1228 stk_push(parsestk
, sitem
);
1230 sitem
= mknew(struct stack_item
);
1231 sitem
->type
= stack_idx
;
1232 dtor(t
), t
= get_token(in
);
1234 * Special cases: \i\c, \i\e, \i\cw
1237 if (t
.type
== tok_cmd
&&
1238 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1239 if (style
!= word_Normal
)
1240 error(err_nestedstyles
, &t
.pos
);
1242 style
= (t
.cmd
== c_c ? word_Code
:
1243 t
.cmd
== c_cw ? word_WeakCode
:
1245 spcstyle
= tospacestyle(style
);
1246 sitem
->type
|= stack_style
;
1248 dtor(t
), t
= get_token(in
);
1250 if (t
.type
!= tok_lbrace
) {
1252 error(err_explbr
, &t
.pos
);
1254 /* Add an index-reference word with no text as yet */
1255 wd
.type
= word_IndexRef
;
1260 indexword
= addword(wd
, &whptr
);
1261 /* Set up a rdstring to read the index text */
1263 /* Flags so that we do the Right Things with text */
1264 index_visible
= (type
!= c_I
);
1265 index_downcase
= (type
== c_ii
);
1268 idximplicit
= &idxwordlist
;
1269 /* Stack item to close the indexing on exit */
1270 stk_push(parsestk
, sitem
);
1275 utext
[0] = uchr
; utext
[1] = 0;
1281 if (!indexing
|| index_visible
) {
1282 wd
.text
= ustrdup(utext
);
1283 uword
= addword(wd
, &whptr
);
1287 wd
.text
= ustrdup(utext
);
1288 iword
= addword(wd
, &idximplicit
);
1291 dtor(t
), t
= get_token(in
);
1292 if (t
.type
== tok_lbrace
) {
1294 * \u with a left brace. Until the brace
1295 * closes, all further words go on a
1296 * sidetrack from the main thread of the
1299 sitem
= mknew(struct stack_item
);
1300 sitem
->type
= stack_ualt
;
1301 sitem
->whptr
= whptr
;
1302 sitem
->idximplicit
= idximplicit
;
1303 stk_push(parsestk
, sitem
);
1304 whptr
= uword ?
&uword
->alt
: NULL
;
1305 idximplicit
= iword ?
&iword
->alt
: NULL
;
1308 rdadd(&indexstr
, uchr
);
1313 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1314 error(err_badmidcmd
, t
.text
, &t
.pos
);
1319 dtor(t
), t
= get_token(in
);
1320 seenwhite
= iswhite
;
1323 /* Check the stack is empty */
1324 if (stk_top(parsestk
)) {
1325 while ((sitem
= stk_pop(parsestk
)))
1327 error(err_missingrbrace
, &t
.pos
);
1330 prev_para_type
= par
.type
;
1332 if (t
.type
== tok_eof
)
1336 if (stk_top(crossparastk
)) {
1339 error(err_missingrbrace2
, &t
.pos
);
1340 while ((p
= stk_pop(crossparastk
)))
1345 * We break to here rather than returning, because otherwise
1346 * this cleanup doesn't happen.
1349 macrocleanup(macros
);
1351 stk_free(crossparastk
);
1354 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1355 paragraph
*head
= NULL
;
1356 paragraph
**hptr
= &head
;
1358 while (in
->currindex
< in
->nfiles
) {
1359 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1361 setpos(in
, in
->filenames
[in
->currindex
]);
1362 read_file(&hptr
, in
, idx
);