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
||
596 t
.type
== tok_rbrace
) { /* might be } terminating \lcont */
597 if (t
.type
== tok_rbrace
)
600 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_c
) {
601 wtype
= word_WeakCode
;
602 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_e
&&
603 wtype
== word_WeakCode
) {
606 error(err_brokencodepara
, &t
.pos
);
607 prev_para_type
= par
.type
;
609 while (t
.type
!= tok_eop
) /* error recovery: */
610 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
611 goto codeparabroken
; /* ick, but such is life */
614 prev_para_type
= par
.type
;
621 * Spot the special commands that define a grouping of more
622 * than one paragraph, and also the closing braces that
625 if (t
.type
== tok_cmd
&&
626 (t
.cmd
== c_lcont
|| t
.cmd
== c_quote
)) {
627 struct crossparaitem
*sitem
, *stop
;
631 * Expect, and swallow, an open brace.
633 dtor(t
), t
= get_token(in
);
634 if (t
.type
!= tok_lbrace
) {
635 error(err_explbr
, &t
.pos
);
640 * Also expect, and swallow, any whitespace after that
641 * (a newline before a code paragraph wouldn't be
645 dtor(t
), t
= get_token(in
);
646 } while (t
.type
== tok_white
);
649 if (cmd
== c_lcont
) {
651 * \lcont causes a continuation of a list item into
652 * multiple paragraphs (which may in turn contain
653 * nested lists, code paras etc). Hence, the previous
654 * paragraph must be of a list type.
656 sitem
= mknew(struct crossparaitem
);
657 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
661 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
663 if (prev_para_type
== para_Bullet
||
664 prev_para_type
== para_NumberedList
||
665 prev_para_type
== para_Description
) {
666 sitem
->type
= c_lcont
;
667 sitem
->seen_lcont
= 1;
668 par
.type
= para_LcontPush
;
669 prev_para_type
= par
.type
;
673 * Push a null item on the cross-para stack so that
674 * when we see the corresponding closing brace we
675 * don't give a cascade error.
678 error(err_misplacedlcont
, &t
.pos
);
682 * \quote causes a group of paragraphs to be
683 * block-quoted (typically they will be indented a
686 sitem
= mknew(struct crossparaitem
);
687 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
691 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
692 sitem
->type
= c_quote
;
693 sitem
->seen_quote
= 1;
694 par
.type
= para_QuotePush
;
695 prev_para_type
= par
.type
;
698 stk_push(crossparastk
, sitem
);
700 } else if (t
.type
== tok_rbrace
) {
701 struct crossparaitem
*sitem
= stk_pop(crossparastk
);
703 error(err_unexbrace
, &t
.pos
);
705 switch (sitem
->type
) {
707 par
.type
= para_LcontPop
;
708 prev_para_type
= par
.type
;
712 par
.type
= para_QuotePop
;
713 prev_para_type
= par
.type
;
723 * This token begins a paragraph. See if it's one of the
724 * special commands that define a paragraph type.
726 * (note that \# is special in a way, and \nocite takes no
729 par
.type
= para_Normal
;
730 if (t
.type
== tok_cmd
) {
732 int is_macro
= FALSE
;
740 error(err_badparatype
, t
.text
, &t
.pos
);
745 break; /* `\#{': isn't a comment para */
747 dtor(t
), t
= get_token(in
);
748 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
749 continue; /* next paragraph */
753 * 1 -- exactly one keyword
754 * 2 -- at least one keyword
755 * 4 -- any number of keywords including zero
756 * 8 -- at least one keyword and then nothing else
757 * 16 -- nothing at all! no keywords, no body
758 * 32 -- no keywords at all
760 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
761 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
762 case c_BR
: needkw
= 1; par
.type
= para_BR
;
763 start_cmd
= c_BR
; break;
764 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
765 case c_H
: needkw
= 2; par
.type
= para_Heading
;
768 case c_IM
: needkw
= 2; par
.type
= para_IM
;
769 start_cmd
= c_IM
; break;
770 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
771 par
.aux
= t
.aux
; break;
772 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
773 /* For \b and \n the keyword is optional */
774 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
775 case c_dt
: needkw
= 4; par
.type
= para_DescribedThing
; break;
776 case c_dd
: needkw
= 4; par
.type
= para_Description
; break;
777 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
778 case c_cfg
: needkw
= 8; par
.type
= para_Config
;
779 start_cmd
= c_cfg
; break;
780 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
781 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
782 /* For \nocite the keyword is _everything_ */
783 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
784 case c_preamble
: needkw
= 32; par
.type
= para_Normal
; break;
785 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
786 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
787 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
790 if (par
.type
== para_Chapter
||
791 par
.type
== para_Heading
||
792 par
.type
== para_Subsect
||
793 par
.type
== para_Appendix
||
794 par
.type
== para_UnnumberedChapter
) {
795 struct crossparaitem
*sitem
= stk_top(crossparastk
);
796 if (sitem
&& (sitem
->seen_lcont
|| sitem
->seen_quote
)) {
797 error(err_sectmarkerinblock
,
799 (sitem
->seen_lcont ?
"lcont" : "quote"));
804 rdstring rs
= { 0, 0, NULL
};
809 dtor(t
), t
= get_token(in
);
811 while (t
.type
== tok_lbrace
) {
812 /* This is a keyword. */
814 /* FIXME: there will be bugs if anyone specifies an
815 * empty keyword (\foo{}), so trap this case. */
816 while (dtor(t
), t
= get_token(in
),
817 t
.type
== tok_word
||
818 t
.type
== tok_white
||
819 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
820 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
)) {
821 if (t
.type
== tok_white
||
822 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
))
827 if (t
.type
!= tok_rbrace
) {
828 error(err_kwunclosed
, &t
.pos
);
831 rdadd(&rs
, 0); /* add string terminator */
832 dtor(t
), t
= get_token(in
); /* eat right brace */
835 rdadd(&rs
, 0); /* add string terminator */
837 /* See whether we have the right number of keywords. */
838 if ((needkw
& 48) && nkeys
> 0)
839 error(err_kwillegal
, &fp
);
840 if ((needkw
& 11) && nkeys
== 0)
841 error(err_kwexpected
, &fp
);
842 if ((needkw
& 5) && nkeys
> 1)
843 error(err_kwtoomany
, &fp
);
847 * Macro definition. Get the rest of the line
848 * as a code-paragraph token, repeatedly until
849 * there's nothing more left of it. Separate
852 rdstring macrotext
= { 0, 0, NULL
};
854 dtor(t
), t
= get_codepar_token(in
);
855 if (macrotext
.pos
> 0)
856 rdadd(¯otext
, L
'\n');
857 rdadds(¯otext
, t
.text
);
858 dtor(t
), t
= get_token(in
);
859 if (t
.type
== tok_eop
) break;
861 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
862 continue; /* next paragraph */
865 par
.keyword
= rdtrim(&rs
);
867 /* Move to EOP in case of needkw==8 or 16 (no body) */
869 /* We allow whitespace even when we expect no para body */
870 while (t
.type
== tok_white
)
871 dtor(t
), t
= get_token(in
);
872 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
873 (start_cmd
== c__invalid
||
874 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
)) {
875 error(err_bodyillegal
, &t
.pos
);
876 /* Error recovery: eat the rest of the paragraph */
877 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
878 (start_cmd
== c__invalid
||
879 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
))
880 dtor(t
), t
= get_token(in
);
882 if (t
.type
== tok_cmd
)
883 already
= TRUE
;/* inhibit get_token at top of loop */
884 prev_para_type
= par
.type
;
886 continue; /* next paragraph */
892 * Now read the actual paragraph, word by word, adding to
893 * the paragraph list.
895 * Mid-paragraph commands:
907 parsestk
= stk_new();
909 spcstyle
= word_WhiteSpace
;
912 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
916 /* Handle implicit paragraph breaks after \IM, \BR etc */
917 if (start_cmd
!= c__invalid
&&
918 t
.type
== tok_cmd
&& t
.cmd
== start_cmd
) {
919 already
= TRUE
; /* inhibit get_token at top of loop */
923 if (t
.type
== tok_cmd
&& t
.cmd
== c__nop
) {
924 dtor(t
), t
= get_token(in
);
925 continue; /* do nothing! */
928 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
929 t
.type
= tok_word
; /* nice and simple */
930 t
.aux
= 0; /* even if `\-' - nonbreaking! */
932 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
933 t
.type
= tok_word
; /* nice and simple */
935 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
936 t
.aux
= 0; /* (nonbreaking) */
940 if (whptr
== &par
.words
)
941 break; /* strip whitespace at start of para */
950 * Inhibit use of whitespace if it's (probably the
951 * newline) before a repeat \IM / \BR type
954 if (start_cmd
!= c__invalid
) {
955 dtor(t
), t
= get_token(in
);
957 if (t
.type
== tok_cmd
&& t
.cmd
== start_cmd
)
962 rdadd(&indexstr
, ' ');
963 if (!indexing
|| index_visible
)
966 addword(wd
, &idximplicit
);
971 rdadds(&indexstr
, t
.text
);
977 if (!indexing
|| index_visible
) {
978 wd
.text
= ustrdup(t
.text
);
982 wd
.text
= ustrdup(t
.text
);
983 addword(wd
, &idximplicit
);
987 error(err_unexbrace
, &t
.pos
);
988 /* Error recovery: push nop */
989 sitem
= mknew(struct stack_item
);
990 sitem
->type
= stack_nop
;
991 stk_push(parsestk
, sitem
);
994 sitem
= stk_pop(parsestk
);
997 * This closing brace could have been an
998 * indication that the cross-paragraph stack
999 * wants popping. Accordingly, we treat it here
1000 * as an indication that the paragraph is over.
1005 if (sitem
->type
& stack_ualt
) {
1006 whptr
= sitem
->whptr
;
1007 idximplicit
= sitem
->idximplicit
;
1009 if (sitem
->type
& stack_style
) {
1010 style
= word_Normal
;
1011 spcstyle
= word_WhiteSpace
;
1013 if (sitem
->type
& stack_idx
) {
1014 indexword
->text
= ustrdup(indexstr
.text
);
1015 if (index_downcase
) {
1018 ustrlow(indexword
->text
);
1019 ustrlow(indexstr
.text
);
1021 for (w
= idxwordlist
; w
; w
= w
->next
)
1026 rdadd(&indexstr
, L
'\0');
1027 index_merge(idx
, FALSE
, indexstr
.text
, idxwordlist
);
1028 sfree(indexstr
.text
);
1030 if (sitem
->type
& stack_hyper
) {
1032 wd
.type
= word_HyperEnd
;
1037 if (!indexing
|| index_visible
)
1038 addword(wd
, &whptr
);
1040 addword(wd
, &idximplicit
);
1042 if (sitem
->type
& stack_quote
) {
1044 wd
.type
= toquotestyle(style
);
1046 wd
.aux
= quote_Close
;
1049 if (!indexing
|| index_visible
)
1050 addword(wd
, &whptr
);
1052 rdadd(&indexstr
, L
'"');
1053 addword(wd
, &idximplicit
);
1063 * In-paragraph comment: \#{ balanced braces }
1065 * Anything goes here; even tok_eop. We should
1066 * eat whitespace after the close brace _if_
1067 * there was whitespace before the \#.
1069 dtor(t
), t
= get_token(in
);
1070 if (t
.type
!= tok_lbrace
) {
1071 error(err_explbr
, &t
.pos
);
1074 while (braces
> 0) {
1075 dtor(t
), t
= get_token(in
);
1076 if (t
.type
== tok_lbrace
)
1078 else if (t
.type
== tok_rbrace
)
1080 else if (t
.type
== tok_eof
) {
1081 error(err_commenteof
, &t
.pos
);
1088 dtor(t
), t
= get_token(in
);
1089 if (t
.type
== tok_white
) {
1096 dtor(t
), t
= get_token(in
);
1097 if (t
.type
!= tok_lbrace
) {
1098 error(err_explbr
, &t
.pos
);
1101 wd
.type
= toquotestyle(style
);
1103 wd
.aux
= quote_Open
;
1106 if (!indexing
|| index_visible
)
1107 addword(wd
, &whptr
);
1109 rdadd(&indexstr
, L
'"');
1110 addword(wd
, &idximplicit
);
1112 sitem
= mknew(struct stack_item
);
1113 sitem
->type
= stack_quote
;
1114 stk_push(parsestk
, sitem
);
1122 * Keyword, hyperlink, or \date. We expect a
1123 * left brace, some text, and then a right
1124 * brace. No nesting; no arguments.
1129 wd
.type
= word_UpperXref
;
1130 else if (t
.cmd
== c_k
)
1131 wd
.type
= word_LowerXref
;
1132 else if (t
.cmd
== c_W
)
1133 wd
.type
= word_HyperLink
;
1135 wd
.type
= word_Normal
;
1136 dtor(t
), t
= get_token(in
);
1137 if (t
.type
!= tok_lbrace
) {
1138 if (wd
.type
== word_Normal
) {
1139 time_t thetime
= time(NULL
);
1140 struct tm
*broken
= localtime(&thetime
);
1142 wdtext
= ustrftime(NULL
, broken
);
1145 error(err_explbr
, &t
.pos
);
1149 rdstring rs
= { 0, 0, NULL
};
1150 while (dtor(t
), t
= get_token(in
),
1151 t
.type
== tok_word
|| t
.type
== tok_white
) {
1152 if (t
.type
== tok_white
)
1155 rdadds(&rs
, t
.text
);
1157 if (wd
.type
== word_Normal
) {
1158 time_t thetime
= time(NULL
);
1159 struct tm
*broken
= localtime(&thetime
);
1160 wdtext
= ustrftime(rs
.text
, broken
);
1163 wdtext
= ustrdup(rs
.text
);
1166 if (t
.type
!= tok_rbrace
) {
1167 error(err_kwexprbr
, &t
.pos
);
1172 if (!indexing
|| index_visible
) {
1173 wd
.text
= ustrdup(wdtext
);
1174 addword(wd
, &whptr
);
1177 wd
.text
= ustrdup(wdtext
);
1178 addword(wd
, &idximplicit
);
1181 if (wd
.type
== word_HyperLink
) {
1183 * Hyperlinks are different: they then
1184 * expect another left brace, to begin
1185 * delimiting the text marked by the link.
1187 dtor(t
), t
= get_token(in
);
1189 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1191 sitem
= mknew(struct stack_item
);
1192 sitem
->type
= stack_hyper
;
1193 if (t
.type
== tok_cmd
&&
1194 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1195 if (style
!= word_Normal
)
1196 error(err_nestedstyles
, &t
.pos
);
1198 style
= (t
.cmd
== c_c ? word_Code
:
1199 t
.cmd
== c_cw ? word_WeakCode
:
1201 spcstyle
= tospacestyle(style
);
1202 sitem
->type
|= stack_style
;
1204 dtor(t
), t
= get_token(in
);
1206 if (t
.type
!= tok_lbrace
) {
1207 error(err_explbr
, &t
.pos
);
1210 stk_push(parsestk
, sitem
);
1218 if (style
!= word_Normal
) {
1219 error(err_nestedstyles
, &t
.pos
);
1220 /* Error recovery: eat lbrace, push nop. */
1221 dtor(t
), t
= get_token(in
);
1222 sitem
= mknew(struct stack_item
);
1223 sitem
->type
= stack_nop
;
1224 stk_push(parsestk
, sitem
);
1226 dtor(t
), t
= get_token(in
);
1227 if (t
.type
!= tok_lbrace
) {
1228 error(err_explbr
, &t
.pos
);
1230 style
= (type
== c_c ? word_Code
:
1231 type
== c_cw ? word_WeakCode
:
1233 spcstyle
= tospacestyle(style
);
1234 sitem
= mknew(struct stack_item
);
1235 sitem
->type
= stack_style
;
1236 stk_push(parsestk
, sitem
);
1244 error(err_nestedindex
, &t
.pos
);
1245 /* Error recovery: eat lbrace, push nop. */
1246 dtor(t
), t
= get_token(in
);
1247 sitem
= mknew(struct stack_item
);
1248 sitem
->type
= stack_nop
;
1249 stk_push(parsestk
, sitem
);
1251 sitem
= mknew(struct stack_item
);
1252 sitem
->type
= stack_idx
;
1253 dtor(t
), t
= get_token(in
);
1255 * Special cases: \i\c, \i\e, \i\cw
1258 if (t
.type
== tok_cmd
&&
1259 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1260 if (style
!= word_Normal
)
1261 error(err_nestedstyles
, &t
.pos
);
1263 style
= (t
.cmd
== c_c ? word_Code
:
1264 t
.cmd
== c_cw ? word_WeakCode
:
1266 spcstyle
= tospacestyle(style
);
1267 sitem
->type
|= stack_style
;
1269 dtor(t
), t
= get_token(in
);
1271 if (t
.type
!= tok_lbrace
) {
1273 error(err_explbr
, &t
.pos
);
1275 /* Add an index-reference word with no text as yet */
1276 wd
.type
= word_IndexRef
;
1281 indexword
= addword(wd
, &whptr
);
1282 /* Set up a rdstring to read the index text */
1284 /* Flags so that we do the Right Things with text */
1285 index_visible
= (type
!= c_I
);
1286 index_downcase
= (type
== c_ii
);
1289 idximplicit
= &idxwordlist
;
1290 /* Stack item to close the indexing on exit */
1291 stk_push(parsestk
, sitem
);
1296 utext
[0] = uchr
; utext
[1] = 0;
1302 if (!indexing
|| index_visible
) {
1303 wd
.text
= ustrdup(utext
);
1304 uword
= addword(wd
, &whptr
);
1308 wd
.text
= ustrdup(utext
);
1309 iword
= addword(wd
, &idximplicit
);
1312 dtor(t
), t
= get_token(in
);
1313 if (t
.type
== tok_lbrace
) {
1315 * \u with a left brace. Until the brace
1316 * closes, all further words go on a
1317 * sidetrack from the main thread of the
1320 sitem
= mknew(struct stack_item
);
1321 sitem
->type
= stack_ualt
;
1322 sitem
->whptr
= whptr
;
1323 sitem
->idximplicit
= idximplicit
;
1324 stk_push(parsestk
, sitem
);
1325 whptr
= uword ?
&uword
->alt
: NULL
;
1326 idximplicit
= iword ?
&iword
->alt
: NULL
;
1329 rdadd(&indexstr
, uchr
);
1334 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1335 error(err_badmidcmd
, t
.text
, &t
.pos
);
1340 dtor(t
), t
= get_token(in
);
1341 seenwhite
= iswhite
;
1344 /* Check the stack is empty */
1345 if (stk_top(parsestk
)) {
1346 while ((sitem
= stk_pop(parsestk
)))
1348 error(err_missingrbrace
, &t
.pos
);
1351 prev_para_type
= par
.type
;
1353 if (t
.type
== tok_eof
)
1357 if (stk_top(crossparastk
)) {
1360 error(err_missingrbrace2
, &t
.pos
);
1361 while ((p
= stk_pop(crossparastk
)))
1366 * We break to here rather than returning, because otherwise
1367 * this cleanup doesn't happen.
1370 macrocleanup(macros
);
1372 stk_free(crossparastk
);
1375 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1376 paragraph
*head
= NULL
;
1377 paragraph
**hptr
= &head
;
1379 while (in
->currindex
< in
->nfiles
) {
1380 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1382 setpos(in
, in
->filenames
[in
->currindex
]);
1383 read_file(&hptr
, in
, idx
);