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 */
534 struct crossparaitem
{
535 int type
; /* currently c_lcont, c_quote or -1 */
536 int seen_lcont
, seen_quote
;
539 word
*indexword
, *uword
, *iword
;
542 int index_downcase
, index_visible
, indexing
;
543 const rdstring nullrs
= { 0, 0, NULL
};
547 macros
= newtree234(macrocmp
);
550 crossparastk
= stk_new();
553 * Loop on each paragraph.
556 int start_cmd
= c__invalid
;
566 dtor(t
), t
= get_token(in
);
569 } while (t
.type
== tok_eop
);
570 if (t
.type
== tok_eof
)
574 * Parse code paragraphs separately.
576 if (t
.type
== tok_cmd
&& t
.cmd
== c_c
&& !isbrace(in
)) {
577 int wtype
= word_WeakCode
;
579 par
.type
= para_Code
;
582 dtor(t
), t
= get_codepar_token(in
);
584 wd
.breaks
= FALSE
; /* shouldn't need this... */
585 wd
.text
= ustrdup(t
.text
);
589 dtor(t
), t
= get_token(in
);
590 if (t
.type
== tok_white
) {
592 * The newline after a code-paragraph line
594 dtor(t
), t
= get_token(in
);
596 if (t
.type
== tok_eop
|| t
.type
== tok_eof
||
597 t
.type
== tok_rbrace
) { /* might be } terminating \lcont */
598 if (t
.type
== tok_rbrace
)
601 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_c
) {
602 wtype
= word_WeakCode
;
603 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_e
&&
604 wtype
== word_WeakCode
) {
607 error(err_brokencodepara
, &t
.pos
);
608 prev_para_type
= par
.type
;
610 while (t
.type
!= tok_eop
) /* error recovery: */
611 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
612 goto codeparabroken
; /* ick, but such is life */
615 prev_para_type
= par
.type
;
622 * Spot the special commands that define a grouping of more
623 * than one paragraph, and also the closing braces that
626 if (t
.type
== tok_cmd
&&
627 (t
.cmd
== c_lcont
|| t
.cmd
== c_quote
)) {
628 struct crossparaitem
*sitem
, *stop
;
632 * Expect, and swallow, an open brace.
634 dtor(t
), t
= get_token(in
);
635 if (t
.type
!= tok_lbrace
) {
636 error(err_explbr
, &t
.pos
);
641 * Also expect, and swallow, any whitespace after that
642 * (a newline before a code paragraph wouldn't be
646 dtor(t
), t
= get_token(in
);
647 } while (t
.type
== tok_white
);
650 if (cmd
== c_lcont
) {
652 * \lcont causes a continuation of a list item into
653 * multiple paragraphs (which may in turn contain
654 * nested lists, code paras etc). Hence, the previous
655 * paragraph must be of a list type.
657 sitem
= mknew(struct crossparaitem
);
658 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
662 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
664 if (prev_para_type
== para_Bullet
||
665 prev_para_type
== para_NumberedList
||
666 prev_para_type
== para_Description
) {
667 sitem
->type
= c_lcont
;
668 sitem
->seen_lcont
= 1;
669 par
.type
= para_LcontPush
;
670 prev_para_type
= par
.type
;
674 * Push a null item on the cross-para stack so that
675 * when we see the corresponding closing brace we
676 * don't give a cascade error.
679 error(err_misplacedlcont
, &t
.pos
);
683 * \quote causes a group of paragraphs to be
684 * block-quoted (typically they will be indented a
687 sitem
= mknew(struct crossparaitem
);
688 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
692 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
693 sitem
->type
= c_quote
;
694 sitem
->seen_quote
= 1;
695 par
.type
= para_QuotePush
;
696 prev_para_type
= par
.type
;
699 stk_push(crossparastk
, sitem
);
701 } else if (t
.type
== tok_rbrace
) {
702 struct crossparaitem
*sitem
= stk_pop(crossparastk
);
704 error(err_unexbrace
, &t
.pos
);
706 switch (sitem
->type
) {
708 par
.type
= para_LcontPop
;
709 prev_para_type
= par
.type
;
713 par
.type
= para_QuotePop
;
714 prev_para_type
= par
.type
;
724 * This token begins a paragraph. See if it's one of the
725 * special commands that define a paragraph type.
727 * (note that \# is special in a way, and \nocite takes no
730 par
.type
= para_Normal
;
731 if (t
.type
== tok_cmd
) {
733 int is_macro
= FALSE
;
741 error(err_badparatype
, t
.text
, &t
.pos
);
746 break; /* `\#{': isn't a comment para */
748 dtor(t
), t
= get_token(in
);
749 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
750 continue; /* next paragraph */
754 * 1 -- exactly one keyword
755 * 2 -- at least one keyword
756 * 4 -- any number of keywords including zero
757 * 8 -- at least one keyword and then nothing else
758 * 16 -- nothing at all! no keywords, no body
759 * 32 -- no keywords at all
761 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
762 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
763 case c_BR
: needkw
= 1; par
.type
= para_BR
;
764 start_cmd
= c_BR
; break;
765 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
766 case c_H
: needkw
= 2; par
.type
= para_Heading
;
769 case c_IM
: needkw
= 2; par
.type
= para_IM
;
770 start_cmd
= c_IM
; break;
771 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
772 par
.aux
= t
.aux
; break;
773 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
774 /* For \b and \n the keyword is optional */
775 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
776 case c_dt
: needkw
= 4; par
.type
= para_DescribedThing
; break;
777 case c_dd
: needkw
= 4; par
.type
= para_Description
; break;
778 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
779 case c_cfg
: needkw
= 8; par
.type
= para_Config
;
780 start_cmd
= c_cfg
; break;
781 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
782 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
783 /* For \nocite the keyword is _everything_ */
784 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
785 case c_preamble
: needkw
= 32; par
.type
= para_Normal
; break;
786 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
787 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
788 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
791 if (par
.type
== para_Chapter
||
792 par
.type
== para_Heading
||
793 par
.type
== para_Subsect
||
794 par
.type
== para_Appendix
||
795 par
.type
== para_UnnumberedChapter
) {
796 struct crossparaitem
*sitem
= stk_top(crossparastk
);
797 if (sitem
&& (sitem
->seen_lcont
|| sitem
->seen_quote
)) {
798 error(err_sectmarkerinblock
,
800 (sitem
->seen_lcont ?
"lcont" : "quote"));
805 rdstring rs
= { 0, 0, NULL
};
810 dtor(t
), t
= get_token(in
);
812 while (t
.type
== tok_lbrace
) {
813 /* This is a keyword. */
815 /* FIXME: there will be bugs if anyone specifies an
816 * empty keyword (\foo{}), so trap this case. */
817 while (dtor(t
), t
= get_token(in
),
818 t
.type
== tok_word
||
819 t
.type
== tok_white
||
820 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
821 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
)) {
822 if (t
.type
== tok_white
||
823 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
))
828 if (t
.type
!= tok_rbrace
) {
829 error(err_kwunclosed
, &t
.pos
);
832 rdadd(&rs
, 0); /* add string terminator */
833 dtor(t
), t
= get_token(in
); /* eat right brace */
836 rdadd(&rs
, 0); /* add string terminator */
838 /* See whether we have the right number of keywords. */
839 if ((needkw
& 48) && nkeys
> 0)
840 error(err_kwillegal
, &fp
);
841 if ((needkw
& 11) && nkeys
== 0)
842 error(err_kwexpected
, &fp
);
843 if ((needkw
& 5) && nkeys
> 1)
844 error(err_kwtoomany
, &fp
);
848 * Macro definition. Get the rest of the line
849 * as a code-paragraph token, repeatedly until
850 * there's nothing more left of it. Separate
853 rdstring macrotext
= { 0, 0, NULL
};
855 dtor(t
), t
= get_codepar_token(in
);
856 if (macrotext
.pos
> 0)
857 rdadd(¯otext
, L
'\n');
858 rdadds(¯otext
, t
.text
);
859 dtor(t
), t
= get_token(in
);
860 if (t
.type
== tok_eop
) break;
862 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
863 continue; /* next paragraph */
866 par
.keyword
= rdtrim(&rs
);
868 /* Move to EOP in case of needkw==8 or 16 (no body) */
870 /* We allow whitespace even when we expect no para body */
871 while (t
.type
== tok_white
)
872 dtor(t
), t
= get_token(in
);
873 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
874 (start_cmd
== c__invalid
||
875 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
)) {
876 error(err_bodyillegal
, &t
.pos
);
877 /* Error recovery: eat the rest of the paragraph */
878 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
879 (start_cmd
== c__invalid
||
880 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
))
881 dtor(t
), t
= get_token(in
);
883 if (t
.type
== tok_cmd
)
884 already
= TRUE
;/* inhibit get_token at top of loop */
885 prev_para_type
= par
.type
;
887 continue; /* next paragraph */
893 * Now read the actual paragraph, word by word, adding to
894 * the paragraph list.
896 * Mid-paragraph commands:
908 parsestk
= stk_new();
910 spcstyle
= word_WhiteSpace
;
913 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
917 /* Handle implicit paragraph breaks after \IM, \BR etc */
918 if (start_cmd
!= c__invalid
&&
919 t
.type
== tok_cmd
&& t
.cmd
== start_cmd
) {
920 already
= TRUE
; /* inhibit get_token at top of loop */
924 if (t
.type
== tok_cmd
&& t
.cmd
== c__nop
) {
925 dtor(t
), t
= get_token(in
);
926 continue; /* do nothing! */
929 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
930 t
.type
= tok_word
; /* nice and simple */
931 t
.aux
= 0; /* even if `\-' - nonbreaking! */
933 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
934 t
.type
= tok_word
; /* nice and simple */
936 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
937 t
.aux
= 0; /* (nonbreaking) */
941 if (whptr
== &par
.words
)
942 break; /* strip whitespace at start of para */
951 * Inhibit use of whitespace if it's (probably the
952 * newline) before a repeat \IM / \BR type
955 if (start_cmd
!= c__invalid
) {
956 dtor(t
), t
= get_token(in
);
958 if (t
.type
== tok_cmd
&& t
.cmd
== start_cmd
)
963 rdadd(&indexstr
, ' ');
964 if (!indexing
|| index_visible
)
967 addword(wd
, &idximplicit
);
972 rdadds(&indexstr
, t
.text
);
978 if (!indexing
|| index_visible
) {
979 wd
.text
= ustrdup(t
.text
);
983 wd
.text
= ustrdup(t
.text
);
984 addword(wd
, &idximplicit
);
988 error(err_unexbrace
, &t
.pos
);
989 /* Error recovery: push nop */
990 sitem
= mknew(struct stack_item
);
991 sitem
->type
= stack_nop
;
993 stk_push(parsestk
, sitem
);
996 sitem
= stk_pop(parsestk
);
999 * This closing brace could have been an
1000 * indication that the cross-paragraph stack
1001 * wants popping. Accordingly, we treat it here
1002 * as an indication that the paragraph is over.
1007 if (sitem
->type
& stack_ualt
) {
1008 whptr
= sitem
->whptr
;
1009 idximplicit
= sitem
->idximplicit
;
1011 if (sitem
->type
& stack_style
) {
1012 style
= word_Normal
;
1013 spcstyle
= word_WhiteSpace
;
1015 if (sitem
->type
& stack_idx
) {
1016 indexword
->text
= ustrdup(indexstr
.text
);
1017 if (index_downcase
) {
1020 ustrlow(indexword
->text
);
1021 ustrlow(indexstr
.text
);
1023 for (w
= idxwordlist
; w
; w
= w
->next
)
1028 rdadd(&indexstr
, L
'\0');
1029 index_merge(idx
, FALSE
, indexstr
.text
,
1030 idxwordlist
, &sitem
->fpos
);
1031 sfree(indexstr
.text
);
1033 if (sitem
->type
& stack_hyper
) {
1035 wd
.type
= word_HyperEnd
;
1040 if (!indexing
|| index_visible
)
1041 addword(wd
, &whptr
);
1043 addword(wd
, &idximplicit
);
1045 if (sitem
->type
& stack_quote
) {
1047 wd
.type
= toquotestyle(style
);
1049 wd
.aux
= quote_Close
;
1052 if (!indexing
|| index_visible
)
1053 addword(wd
, &whptr
);
1055 rdadd(&indexstr
, L
'"');
1056 addword(wd
, &idximplicit
);
1066 * In-paragraph comment: \#{ balanced braces }
1068 * Anything goes here; even tok_eop. We should
1069 * eat whitespace after the close brace _if_
1070 * there was whitespace before the \#.
1072 dtor(t
), t
= get_token(in
);
1073 if (t
.type
!= tok_lbrace
) {
1074 error(err_explbr
, &t
.pos
);
1077 while (braces
> 0) {
1078 dtor(t
), t
= get_token(in
);
1079 if (t
.type
== tok_lbrace
)
1081 else if (t
.type
== tok_rbrace
)
1083 else if (t
.type
== tok_eof
) {
1084 error(err_commenteof
, &t
.pos
);
1091 dtor(t
), t
= get_token(in
);
1092 if (t
.type
== tok_white
) {
1099 dtor(t
), t
= get_token(in
);
1100 if (t
.type
!= tok_lbrace
) {
1101 error(err_explbr
, &t
.pos
);
1104 wd
.type
= toquotestyle(style
);
1106 wd
.aux
= quote_Open
;
1109 if (!indexing
|| index_visible
)
1110 addword(wd
, &whptr
);
1112 rdadd(&indexstr
, L
'"');
1113 addword(wd
, &idximplicit
);
1115 sitem
= mknew(struct stack_item
);
1116 sitem
->fpos
= t
.pos
;
1117 sitem
->type
= stack_quote
;
1118 stk_push(parsestk
, sitem
);
1126 * Keyword, hyperlink, or \date. We expect a
1127 * left brace, some text, and then a right
1128 * brace. No nesting; no arguments.
1133 wd
.type
= word_UpperXref
;
1134 else if (t
.cmd
== c_k
)
1135 wd
.type
= word_LowerXref
;
1136 else if (t
.cmd
== c_W
)
1137 wd
.type
= word_HyperLink
;
1139 wd
.type
= word_Normal
;
1140 dtor(t
), t
= get_token(in
);
1141 if (t
.type
!= tok_lbrace
) {
1142 if (wd
.type
== word_Normal
) {
1143 time_t thetime
= time(NULL
);
1144 struct tm
*broken
= localtime(&thetime
);
1146 wdtext
= ustrftime(NULL
, broken
);
1149 error(err_explbr
, &t
.pos
);
1153 rdstring rs
= { 0, 0, NULL
};
1154 while (dtor(t
), t
= get_token(in
),
1155 t
.type
== tok_word
|| t
.type
== tok_white
) {
1156 if (t
.type
== tok_white
)
1159 rdadds(&rs
, t
.text
);
1161 if (wd
.type
== word_Normal
) {
1162 time_t thetime
= time(NULL
);
1163 struct tm
*broken
= localtime(&thetime
);
1164 wdtext
= ustrftime(rs
.text
, broken
);
1167 wdtext
= ustrdup(rs
.text
);
1170 if (t
.type
!= tok_rbrace
) {
1171 error(err_kwexprbr
, &t
.pos
);
1176 if (!indexing
|| index_visible
) {
1177 wd
.text
= ustrdup(wdtext
);
1178 addword(wd
, &whptr
);
1181 wd
.text
= ustrdup(wdtext
);
1182 addword(wd
, &idximplicit
);
1185 if (wd
.type
== word_HyperLink
) {
1187 * Hyperlinks are different: they then
1188 * expect another left brace, to begin
1189 * delimiting the text marked by the link.
1191 dtor(t
), t
= get_token(in
);
1192 sitem
= mknew(struct stack_item
);
1193 sitem
->fpos
= wd
.fpos
;
1194 sitem
->type
= stack_hyper
;
1196 * Special cases: \W{}\i, \W{}\ii
1198 if (t
.type
== tok_cmd
&&
1199 (t
.cmd
== c_i
|| t
.cmd
== c_ii
)) {
1201 error(err_nestedindex
, &t
.pos
);
1203 /* Add an index-reference word with no
1205 wd
.type
= word_IndexRef
;
1210 indexword
= addword(wd
, &whptr
);
1211 /* Set up a rdstring to read the
1214 /* Flags so that we do the Right
1215 * Things with text */
1216 index_visible
= (type
!= c_I
);
1217 index_downcase
= (type
== c_ii
);
1220 idximplicit
= &idxwordlist
;
1222 sitem
->type
|= stack_idx
;
1224 dtor(t
), t
= get_token(in
);
1227 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1229 if (t
.type
== tok_cmd
&&
1230 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1231 if (style
!= word_Normal
)
1232 error(err_nestedstyles
, &t
.pos
);
1234 style
= (t
.cmd
== c_c ? word_Code
:
1235 t
.cmd
== c_cw ? word_WeakCode
:
1237 spcstyle
= tospacestyle(style
);
1238 sitem
->type
|= stack_style
;
1240 dtor(t
), t
= get_token(in
);
1242 if (t
.type
!= tok_lbrace
) {
1243 error(err_explbr
, &t
.pos
);
1246 stk_push(parsestk
, sitem
);
1254 if (style
!= word_Normal
) {
1255 error(err_nestedstyles
, &t
.pos
);
1256 /* Error recovery: eat lbrace, push nop. */
1257 dtor(t
), t
= get_token(in
);
1258 sitem
= mknew(struct stack_item
);
1259 sitem
->fpos
= t
.pos
;
1260 sitem
->type
= stack_nop
;
1261 stk_push(parsestk
, sitem
);
1263 dtor(t
), t
= get_token(in
);
1264 if (t
.type
!= tok_lbrace
) {
1265 error(err_explbr
, &t
.pos
);
1267 style
= (type
== c_c ? word_Code
:
1268 type
== c_cw ? word_WeakCode
:
1270 spcstyle
= tospacestyle(style
);
1271 sitem
= mknew(struct stack_item
);
1272 sitem
->fpos
= t
.pos
;
1273 sitem
->type
= stack_style
;
1274 stk_push(parsestk
, sitem
);
1282 error(err_nestedindex
, &t
.pos
);
1283 /* Error recovery: eat lbrace, push nop. */
1284 dtor(t
), t
= get_token(in
);
1285 sitem
= mknew(struct stack_item
);
1286 sitem
->fpos
= t
.pos
;
1287 sitem
->type
= stack_nop
;
1288 stk_push(parsestk
, sitem
);
1290 sitem
= mknew(struct stack_item
);
1291 sitem
->fpos
= t
.pos
;
1292 sitem
->type
= stack_idx
;
1293 dtor(t
), t
= get_token(in
);
1295 * Special cases: \i\c, \i\e, \i\cw
1298 if (t
.type
== tok_cmd
&&
1299 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1300 if (style
!= word_Normal
)
1301 error(err_nestedstyles
, &t
.pos
);
1303 style
= (t
.cmd
== c_c ? word_Code
:
1304 t
.cmd
== c_cw ? word_WeakCode
:
1306 spcstyle
= tospacestyle(style
);
1307 sitem
->type
|= stack_style
;
1309 dtor(t
), t
= get_token(in
);
1311 if (t
.type
!= tok_lbrace
) {
1313 error(err_explbr
, &t
.pos
);
1315 /* Add an index-reference word with no text as yet */
1316 wd
.type
= word_IndexRef
;
1321 indexword
= addword(wd
, &whptr
);
1322 /* Set up a rdstring to read the index text */
1324 /* Flags so that we do the Right Things with text */
1325 index_visible
= (type
!= c_I
);
1326 index_downcase
= (type
== c_ii
);
1329 idximplicit
= &idxwordlist
;
1330 /* Stack item to close the indexing on exit */
1331 stk_push(parsestk
, sitem
);
1336 utext
[0] = uchr
; utext
[1] = 0;
1342 if (!indexing
|| index_visible
) {
1343 wd
.text
= ustrdup(utext
);
1344 uword
= addword(wd
, &whptr
);
1348 wd
.text
= ustrdup(utext
);
1349 iword
= addword(wd
, &idximplicit
);
1352 dtor(t
), t
= get_token(in
);
1353 if (t
.type
== tok_lbrace
) {
1355 * \u with a left brace. Until the brace
1356 * closes, all further words go on a
1357 * sidetrack from the main thread of the
1360 sitem
= mknew(struct stack_item
);
1361 sitem
->fpos
= t
.pos
;
1362 sitem
->type
= stack_ualt
;
1363 sitem
->whptr
= whptr
;
1364 sitem
->idximplicit
= idximplicit
;
1365 stk_push(parsestk
, sitem
);
1366 whptr
= uword ?
&uword
->alt
: NULL
;
1367 idximplicit
= iword ?
&iword
->alt
: NULL
;
1370 rdadd(&indexstr
, uchr
);
1375 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1376 error(err_badmidcmd
, t
.text
, &t
.pos
);
1381 dtor(t
), t
= get_token(in
);
1382 seenwhite
= iswhite
;
1385 /* Check the stack is empty */
1386 if (stk_top(parsestk
)) {
1387 while ((sitem
= stk_pop(parsestk
)))
1389 error(err_missingrbrace
, &t
.pos
);
1392 prev_para_type
= par
.type
;
1394 if (t
.type
== tok_eof
)
1398 if (stk_top(crossparastk
)) {
1401 error(err_missingrbrace2
, &t
.pos
);
1402 while ((p
= stk_pop(crossparastk
)))
1407 * We break to here rather than returning, because otherwise
1408 * this cleanup doesn't happen.
1411 macrocleanup(macros
);
1413 stk_free(crossparastk
);
1416 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1417 paragraph
*head
= NULL
;
1418 paragraph
**hptr
= &head
;
1420 while (in
->currindex
< in
->nfiles
) {
1421 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1423 setpos(in
, in
->filenames
[in
->currindex
]);
1424 read_file(&hptr
, in
, idx
);