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
= sresize(in
->pushback
, in
->pushbacksize
, pushback
);
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
= snew(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
= snew(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
++) {
85 static void input_configure(input
*in
, paragraph
*cfg
) {
86 assert(cfg
->type
== para_Config
);
88 if (!ustricmp(cfg
->keyword
, L
"input-charset")) {
89 in
->charset
= charset_from_ustr(&cfg
->fpos
, uadv(cfg
->keyword
));
96 static int get(input
*in
, filepos
*pos
, rdstringc
*rsc
) {
97 int pushbackpt
= in
->stack ? in
->stack
->npushback
: 0;
98 if (in
->npushback
> pushbackpt
) {
101 *pos
= in
->pushback
[in
->npushback
].pos
; /* structure copy */
102 return in
->pushback
[in
->npushback
].chr
;
104 else if (in
->stack
) {
105 wchar_t c
= in
->stack
->text
[in
->stack
->ptr
];
107 *pos
= in
->stack
->pos
;
108 if (in
->stack
->text
[++in
->stack
->ptr
] == L
'\0') {
109 macrostack
*tmp
= in
->stack
;
110 in
->stack
= tmp
->next
;
115 else if (in
->currfp
) {
117 while (in
->wcpos
>= in
->nwc
) {
119 int c
= getc(in
->currfp
);
130 /* Track line numbers, for error reporting */
133 if (in
->reportcols
) {
136 in
->pos
.col
= 1 + (in
->pos
.col
+ TAB_STOP
-1) % TAB_STOP
;
153 * Do input character set translation, so that we return
165 in
->nwc
= charset_to_unicode(&p
, &inlen
,
166 in
->wc
, lenof(in
->wc
),
167 in
->charset
, &in
->csstate
,
169 assert(p
== buf
+1 && inlen
== 0);
175 return in
->wc
[in
->wcpos
++];
182 * Lexical analysis of source files.
184 typedef struct token_Tag token
;
193 tok_eof
, /* end of file */
194 tok_eop
, /* end of paragraph */
195 tok_white
, /* whitespace */
196 tok_word
, /* a word or word fragment */
197 tok_cmd
, /* \command */
202 /* Halibut command keywords. */
204 c__invalid
, /* invalid command */
205 c__comment
, /* comment command (\#) */
206 c__escaped
, /* escaped character */
208 c__nbsp
, /* nonbreaking space */
209 c_A
, /* appendix heading */
210 c_B
, /* bibliography entry */
211 c_BR
, /* bibliography rewrite */
212 c_C
, /* chapter heading */
214 c_I
, /* invisible index mark */
215 c_IM
, /* index merge/rewrite */
216 c_K
, /* capitalised cross-reference */
217 c_S
, /* aux field is 0, 1, 2, ... */
218 c_U
, /* unnumbered-chapter heading */
219 c_W
, /* Web hyperlink */
220 c_b
, /* bulletted list */
222 c_cfg
, /* configuration directive */
223 c_copyright
, /* copyright statement */
224 c_cq
, /* quoted code (sugar for \q{\cw{x}}) */
225 c_cw
, /* weak code */
226 c_date
, /* document processing date */
227 c_dd
, /* description list: description */
228 c_define
, /* macro definition */
229 c_dt
, /* description list: described thing */
231 c_i
, /* visible index mark */
232 c_ii
, /* uncapitalised visible index mark */
233 c_k
, /* uncapitalised cross-reference */
234 c_lcont
, /* continuation para(s) for list item */
235 c_n
, /* numbered list */
236 c_nocite
, /* bibliography trickery */
237 c_preamble
, /* (obsolete) preamble text */
238 c_q
, /* quote marks */
239 c_quote
, /* block-quoted paragraphs */
240 c_rule
, /* horizontal rule */
241 c_title
, /* document title */
242 c_u
, /* aux field is char code */
243 c_versionid
/* document RCS id */
246 /* Perhaps whitespace should be defined in a more Unicode-friendly way? */
247 #define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 )
248 #define isnl(c) ( (c)==10 )
249 #define isdec(c) ( ((c)>='0'&&(c)<='9') )
250 #define fromdec(c) ( (c)-'0' )
251 #define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f'))
252 #define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) )
253 #define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
256 * Keyword comparison function. Like strcmp, but between a wchar_t *
259 static int kwcmp(wchar_t const *p
, char const *q
) {
263 } while (*p
++ && *q
++ && !i
);
270 static void match_kw(token
*tok
) {
272 * FIXME. The ids are explicit in here so as to allow long-name
273 * equivalents to the various very short keywords.
275 static const struct { char const *name
; int id
; } keywords
[] = {
276 {"#", c__comment
}, /* comment command (\#) */
277 {"-", c__escaped
}, /* nonbreaking hyphen */
278 {".", c__nop
}, /* no-op */
279 {"A", c_A
}, /* appendix heading */
280 {"B", c_B
}, /* bibliography entry */
281 {"BR", c_BR
}, /* bibliography rewrite */
282 {"C", c_C
}, /* chapter heading */
283 {"H", c_H
}, /* heading */
284 {"I", c_I
}, /* invisible index mark */
285 {"IM", c_IM
}, /* index merge/rewrite */
286 {"K", c_K
}, /* capitalised cross-reference */
287 {"U", c_U
}, /* unnumbered-chapter heading */
288 {"W", c_W
}, /* Web hyperlink */
289 {"\\", c__escaped
}, /* escaped backslash (\\) */
290 {"_", c__nbsp
}, /* nonbreaking space (\_) */
291 {"b", c_b
}, /* bulletted list */
292 {"c", c_c
}, /* code */
293 {"cfg", c_cfg
}, /* configuration directive */
294 {"copyright", c_copyright
}, /* copyright statement */
295 {"cq", c_cq
}, /* quoted code (sugar for \q{\cw{x}}) */
296 {"cw", c_cw
}, /* weak code */
297 {"date", c_date
}, /* document processing date */
298 {"dd", c_dd
}, /* description list: description */
299 {"define", c_define
}, /* macro definition */
300 {"dt", c_dt
}, /* description list: described thing */
301 {"e", c_e
}, /* emphasis */
302 {"i", c_i
}, /* visible index mark */
303 {"ii", c_ii
}, /* uncapitalised visible index mark */
304 {"k", c_k
}, /* uncapitalised cross-reference */
305 {"lcont", c_lcont
}, /* continuation para(s) for list item */
306 {"n", c_n
}, /* numbered list */
307 {"nocite", c_nocite
}, /* bibliography trickery */
308 {"preamble", c_preamble
}, /* (obsolete) preamble text */
309 {"q", c_q
}, /* quote marks */
310 {"quote", c_quote
}, /* block-quoted paragraphs */
311 {"rule", c_rule
}, /* horizontal rule */
312 {"title", c_title
}, /* document title */
313 {"versionid", c_versionid
}, /* document RCS id */
314 {"{", c__escaped
}, /* escaped lbrace (\{) */
315 {"}", c__escaped
}, /* escaped rbrace (\}) */
320 * Special cases: \S{0,1,2,...} and \uABCD. If the syntax
321 * doesn't match correctly, we just fall through to the
322 * binary-search phase.
324 if (tok
->text
[0] == 'S') {
325 /* We expect numeric characters thereafter. */
326 wchar_t *p
= tok
->text
+1;
332 while (*p
&& isdec(*p
)) {
333 n
= 10 * n
+ fromdec(*p
);
342 } else if (tok
->text
[0] == 'u') {
343 /* We expect hex characters thereafter. */
344 wchar_t *p
= tok
->text
+1;
346 while (*p
&& ishex(*p
)) {
347 n
= 16 * n
+ fromhex(*p
);
358 j
= sizeof(keywords
)/sizeof(*keywords
);
361 c
= kwcmp(tok
->text
, keywords
[k
].name
);
367 tok
->cmd
= keywords
[k
].id
;
372 tok
->cmd
= c__invalid
;
377 * Read a token from the input file, in the normal way (`normal' in
378 * the sense that code paragraphs work a different way).
380 token
get_token(input
*in
) {
385 rdstring rs
= { 0, 0, NULL
};
386 rdstringc rsc
= { 0, 0, NULL
};
389 ret
.text
= NULL
; /* default */
390 ret
.origtext
= NULL
; /* default */
391 if (in
->pushback_chars
) {
392 rdaddsc(&rsc
, in
->pushback_chars
);
393 sfree(in
->pushback_chars
);
394 in
->pushback_chars
= NULL
;
396 c
= get(in
, &cpos
, &rsc
);
398 if (iswhite(c
)) { /* tok_white or tok_eop */
405 } while ((c
= get(in
, &cpos
, &rsc
)) != EOF
&& iswhite(c
));
412 in
->pushback_chars
= dupstr(rsc
.text
+ prevpos
);
416 ret
.type
= (nls
> 1 ? tok_eop
: tok_white
);
418 } else if (c
== EOF
) { /* tok_eof */
422 } else if (c
== '\\') { /* tok_cmd */
423 rsc
.pos
= prevpos
= 0;
424 c
= get(in
, &cpos
, &rsc
);
425 if (c
== '-' || c
== '\\' || c
== '_' ||
426 c
== '#' || c
== '{' || c
== '}' || c
== '.') {
427 /* single-char command */
430 } else if (c
== 'u') {
436 c
= get(in
, &cpos
, &rsc
);
437 } while (ishex(c
) && len
< 5);
439 } else if (iscmd(c
)) {
443 c
= get(in
, &cpos
, &rsc
);
448 * Now match the command against the list of available
452 ret
.text
= ustrdup(rs
.text
);
454 in
->pushback_chars
= dupstr(rsc
.text
+ prevpos
);
455 rsc
.text
[prevpos
] = '\0';
456 ret
.origtext
= dupstr(rsc
.text
);
458 ret
.origtext
= dupstr("");
464 } else if (c
== '{') { /* tok_lbrace */
465 ret
.type
= tok_lbrace
;
468 } else if (c
== '}') { /* tok_rbrace */
469 ret
.type
= tok_rbrace
;
472 } else { /* tok_word */
474 * Read a word: the longest possible contiguous sequence of
475 * things other than whitespace, backslash, braces and
476 * hyphen. A hyphen terminates the word but is returned as
477 * part of it; everything else is pushed back for the next
478 * token. The `aux' field contains TRUE if the word ends in
481 ret
.aux
= FALSE
; /* assumed for now */
484 if (iswhite(c
) || c
=='{' || c
=='}' || c
=='\\' || c
==EOF
) {
485 /* Put back the character that caused termination */
493 break; /* hyphen terminates word */
497 c
= get(in
, &cpos
, &rsc
);
500 ret
.text
= ustrdup(rs
.text
);
502 in
->pushback_chars
= dupstr(rsc
.text
+ prevpos
);
503 rsc
.text
[prevpos
] = '\0';
504 ret
.origtext
= dupstr(rsc
.text
);
506 ret
.origtext
= dupstr("");
515 * Determine whether the next input character is an open brace (for
516 * telling code paragraphs from paragraphs which merely start with
519 int isbrace(input
*in
) {
523 c
= get(in
, &cpos
, NULL
);
529 * Read the rest of a line that starts `\c'. Including nothing at
530 * all (tok_word with empty text).
532 token
get_codepar_token(input
*in
) {
535 rdstring rs
= { 0, 0, NULL
};
540 c
= get(in
, &cpos
, NULL
); /* expect (and discard) one space */
543 c
= get(in
, &cpos
, NULL
);
546 while (!isnl(c
) && c
!= EOF
) {
548 c
= get(in
, &cpos
, NULL
);
549 /* Discard \r just before \n. */
550 if (c2
!= 13 || !isnl(c
))
554 ret
.text
= ustrdup(rs
.text
);
560 * Adds a new word to a linked list
562 static word
*addword(word newword
, word
***hptrptr
) {
566 mnewword
= snew(word
);
567 *mnewword
= newword
; /* structure copy */
568 mnewword
->next
= NULL
;
569 **hptrptr
= mnewword
;
570 *hptrptr
= &mnewword
->next
;
575 * Adds a new paragraph to a linked list
577 static paragraph
*addpara(paragraph newpara
, paragraph
***hptrptr
) {
578 paragraph
*mnewpara
= snew(paragraph
);
579 *mnewpara
= newpara
; /* structure copy */
580 mnewpara
->next
= NULL
;
581 **hptrptr
= mnewpara
;
582 *hptrptr
= &mnewpara
->next
;
587 * Destructor before token is reassigned; should catch most memory
590 #define dtor(t) ( sfree(t.text), sfree(t.origtext) )
593 * Reads a single file (ie until get() returns EOF)
595 static void read_file(paragraph
***ret
, input
*in
, indexdata
*idx
,
599 word wd
, **whptr
, **idximplicit
;
600 wchar_t utext
[2], *wdtext
;
603 int iswhite
, seenwhite
;
608 stack_nop
= 0, /* do nothing (for error recovery) */
609 stack_ualt
= 1, /* \u alternative */
610 stack_style
= 2, /* \e, \c, \cw */
611 stack_idx
= 4, /* \I, \i, \ii */
612 stack_hyper
= 8, /* \W */
613 stack_quote
= 16, /* \q */
615 word
**whptr
; /* to restore from \u alternatives */
616 word
**idximplicit
; /* to restore from \u alternatives */
621 struct crossparaitem
{
622 int type
; /* currently c_lcont, c_quote or -1 */
623 int seen_lcont
, seen_quote
;
626 word
*indexword
, *uword
, *iword
;
629 int index_downcase
, index_visible
, indexing
;
630 const rdstring nullrs
= { 0, 0, NULL
};
637 crossparastk
= stk_new();
640 * Loop on each paragraph.
643 int start_cmd
= c__invalid
;
646 par
.origkeyword
= NULL
;
654 dtor(t
), t
= get_token(in
);
657 } while (t
.type
== tok_eop
);
658 if (t
.type
== tok_eof
)
662 * Parse code paragraphs separately.
664 if (t
.type
== tok_cmd
&& t
.cmd
== c_c
&& !isbrace(in
)) {
665 int wtype
= word_WeakCode
;
667 par
.type
= para_Code
;
670 dtor(t
), t
= get_codepar_token(in
);
672 wd
.breaks
= FALSE
; /* shouldn't need this... */
673 wd
.text
= ustrdup(t
.text
);
677 dtor(t
), t
= get_token(in
);
678 if (t
.type
== tok_white
) {
680 * The newline after a code-paragraph line
682 dtor(t
), t
= get_token(in
);
684 if (t
.type
== tok_eop
|| t
.type
== tok_eof
||
685 t
.type
== tok_rbrace
) { /* might be } terminating \lcont */
686 if (t
.type
== tok_rbrace
)
689 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_c
) {
690 wtype
= word_WeakCode
;
691 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_e
&&
692 wtype
== word_WeakCode
) {
695 error(err_brokencodepara
, &t
.pos
);
696 prev_para_type
= par
.type
;
698 while (t
.type
!= tok_eop
) /* error recovery: */
699 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
700 goto codeparabroken
; /* ick, but such is life */
703 prev_para_type
= par
.type
;
710 * Spot the special commands that define a grouping of more
711 * than one paragraph, and also the closing braces that
714 if (t
.type
== tok_cmd
&&
715 (t
.cmd
== c_lcont
|| t
.cmd
== c_quote
)) {
716 struct crossparaitem
*sitem
, *stop
;
720 * Expect, and swallow, an open brace.
722 dtor(t
), t
= get_token(in
);
723 if (t
.type
!= tok_lbrace
) {
724 error(err_explbr
, &t
.pos
);
729 * Also expect, and swallow, any whitespace after that
730 * (a newline before a code paragraph wouldn't be
734 dtor(t
), t
= get_token(in
);
735 } while (t
.type
== tok_white
);
738 if (cmd
== c_lcont
) {
740 * \lcont causes a continuation of a list item into
741 * multiple paragraphs (which may in turn contain
742 * nested lists, code paras etc). Hence, the previous
743 * paragraph must be of a list type.
745 sitem
= snew(struct crossparaitem
);
746 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
750 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
752 if (prev_para_type
== para_Bullet
||
753 prev_para_type
== para_NumberedList
||
754 prev_para_type
== para_Description
) {
755 sitem
->type
= c_lcont
;
756 sitem
->seen_lcont
= 1;
757 par
.type
= para_LcontPush
;
758 prev_para_type
= par
.type
;
762 * Push a null item on the cross-para stack so that
763 * when we see the corresponding closing brace we
764 * don't give a cascade error.
767 error(err_misplacedlcont
, &t
.pos
);
771 * \quote causes a group of paragraphs to be
772 * block-quoted (typically they will be indented a
775 sitem
= snew(struct crossparaitem
);
776 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
780 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
781 sitem
->type
= c_quote
;
782 sitem
->seen_quote
= 1;
783 par
.type
= para_QuotePush
;
784 prev_para_type
= par
.type
;
787 stk_push(crossparastk
, sitem
);
789 } else if (t
.type
== tok_rbrace
) {
790 struct crossparaitem
*sitem
= stk_pop(crossparastk
);
792 error(err_unexbrace
, &t
.pos
);
794 switch (sitem
->type
) {
796 par
.type
= para_LcontPop
;
797 prev_para_type
= par
.type
;
801 par
.type
= para_QuotePop
;
802 prev_para_type
= par
.type
;
811 while (t
.type
== tok_cmd
&&
812 macrolookup(macros
, in
, t
.text
, &t
.pos
)) {
813 dtor(t
), t
= get_token(in
);
817 * This token begins a paragraph. See if it's one of the
818 * special commands that define a paragraph type.
820 * (note that \# is special in a way, and \nocite takes no
823 par
.type
= para_Normal
;
824 if (t
.type
== tok_cmd
) {
826 int is_macro
= FALSE
;
834 error(err_badparatype
, t
.text
, &t
.pos
);
839 break; /* `\#{': isn't a comment para */
841 dtor(t
), t
= get_token(in
);
842 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
843 continue; /* next paragraph */
847 * 1 -- exactly one keyword
848 * 2 -- at least one keyword
849 * 4 -- any number of keywords including zero
850 * 8 -- at least one keyword and then nothing else
851 * 16 -- nothing at all! no keywords, no body
852 * 32 -- no keywords at all
854 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
855 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
856 case c_BR
: needkw
= 1; par
.type
= para_BR
;
857 start_cmd
= c_BR
; break;
858 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
859 case c_H
: needkw
= 2; par
.type
= para_Heading
;
862 case c_IM
: needkw
= 2; par
.type
= para_IM
;
863 start_cmd
= c_IM
; break;
864 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
865 par
.aux
= t
.aux
; break;
866 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
867 /* For \b and \n the keyword is optional */
868 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
869 case c_dt
: needkw
= 4; par
.type
= para_DescribedThing
; break;
870 case c_dd
: needkw
= 4; par
.type
= para_Description
; break;
871 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
872 case c_cfg
: needkw
= 8; par
.type
= para_Config
;
873 start_cmd
= c_cfg
; break;
874 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
875 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
876 /* For \nocite the keyword is _everything_ */
877 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
878 case c_preamble
: needkw
= 32; par
.type
= para_Normal
; break;
879 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
880 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
881 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
884 if (par
.type
== para_Chapter
||
885 par
.type
== para_Heading
||
886 par
.type
== para_Subsect
||
887 par
.type
== para_Appendix
||
888 par
.type
== para_UnnumberedChapter
) {
889 struct crossparaitem
*sitem
= stk_top(crossparastk
);
890 if (sitem
&& (sitem
->seen_lcont
|| sitem
->seen_quote
)) {
891 error(err_sectmarkerinblock
,
893 (sitem
->seen_lcont ?
"lcont" : "quote"));
898 rdstring rs
= { 0, 0, NULL
};
899 rdstringc rsc
= { 0, 0, NULL
};
904 dtor(t
), t
= get_token(in
);
906 while (t
.type
== tok_lbrace
) {
907 /* This is a keyword. */
909 /* FIXME: there will be bugs if anyone specifies an
910 * empty keyword (\foo{}), so trap this case. */
911 while (dtor(t
), t
= get_token(in
),
912 t
.type
== tok_word
||
913 t
.type
== tok_white
||
914 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
915 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) ||
916 (t
.type
== tok_cmd
&& t
.cmd
== c_u
)) {
917 if (t
.type
== tok_white
||
918 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
)) {
921 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_u
) {
924 rdaddsc(&rsc
, t
.origtext
);
927 rdaddsc(&rsc
, t
.origtext
);
930 if (t
.type
!= tok_rbrace
) {
931 error(err_kwunclosed
, &t
.pos
);
934 rdadd(&rs
, 0); /* add string terminator */
935 rdaddc(&rsc
, 0); /* add string terminator */
936 dtor(t
), t
= get_token(in
); /* eat right brace */
939 rdadd(&rs
, 0); /* add string terminator */
940 rdaddc(&rsc
, 0); /* add string terminator */
942 /* See whether we have the right number of keywords. */
943 if ((needkw
& 48) && nkeys
> 0)
944 error(err_kwillegal
, &fp
);
945 if ((needkw
& 11) && nkeys
== 0)
946 error(err_kwexpected
, &fp
);
947 if ((needkw
& 5) && nkeys
> 1)
948 error(err_kwtoomany
, &fp
);
952 * Macro definition. Get the rest of the line
953 * as a code-paragraph token, repeatedly until
954 * there's nothing more left of it. Separate
957 rdstring macrotext
= { 0, 0, NULL
};
959 dtor(t
), t
= get_codepar_token(in
);
960 if (macrotext
.pos
> 0)
961 rdadd(¯otext
, L
'\n');
962 rdadds(¯otext
, t
.text
);
963 dtor(t
), t
= get_token(in
);
964 if (t
.type
== tok_eop
) break;
966 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
967 continue; /* next paragraph */
970 par
.keyword
= rdtrim(&rs
);
971 par
.origkeyword
= rdtrimc(&rsc
);
973 /* Move to EOP in case of needkw==8 or 16 (no body) */
975 /* We allow whitespace even when we expect no para body */
976 while (t
.type
== tok_white
)
977 dtor(t
), t
= get_token(in
);
978 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
979 (start_cmd
== c__invalid
||
980 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
)) {
981 error(err_bodyillegal
, &t
.pos
);
982 /* Error recovery: eat the rest of the paragraph */
983 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
984 (start_cmd
== c__invalid
||
985 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
))
986 dtor(t
), t
= get_token(in
);
988 if (t
.type
== tok_cmd
)
989 already
= TRUE
;/* inhibit get_token at top of loop */
990 prev_para_type
= par
.type
;
993 if (par
.type
== para_Config
) {
994 input_configure(in
, &par
);
996 continue; /* next paragraph */
1002 * Now read the actual paragraph, word by word, adding to
1003 * the paragraph list.
1005 * Mid-paragraph commands:
1018 parsestk
= stk_new();
1019 style
= word_Normal
;
1020 spcstyle
= word_WhiteSpace
;
1023 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
1027 /* Handle implicit paragraph breaks after \IM, \BR etc */
1028 if (start_cmd
!= c__invalid
&&
1029 t
.type
== tok_cmd
&& t
.cmd
== start_cmd
) {
1030 already
= TRUE
; /* inhibit get_token at top of loop */
1034 if (t
.type
== tok_cmd
&& t
.cmd
== c__nop
) {
1035 dtor(t
), t
= get_token(in
);
1036 continue; /* do nothing! */
1039 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
1040 t
.type
= tok_word
; /* nice and simple */
1041 t
.aux
= 0; /* even if `\-' - nonbreaking! */
1043 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
1044 t
.type
= tok_word
; /* nice and simple */
1046 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
1047 t
.aux
= 0; /* (nonbreaking) */
1051 if (whptr
== &par
.words
)
1052 break; /* strip whitespace at start of para */
1061 * Inhibit use of whitespace if it's (probably the
1062 * newline) before a repeat \IM / \BR type
1065 if (start_cmd
!= c__invalid
) {
1066 dtor(t
), t
= get_token(in
);
1068 if (t
.type
== tok_cmd
&& t
.cmd
== start_cmd
)
1073 rdadd(&indexstr
, ' ');
1074 if (!indexing
|| index_visible
)
1075 addword(wd
, &whptr
);
1077 addword(wd
, &idximplicit
);
1082 rdadds(&indexstr
, t
.text
);
1088 if (!indexing
|| index_visible
) {
1089 wd
.text
= ustrdup(t
.text
);
1090 addword(wd
, &whptr
);
1093 wd
.text
= ustrdup(t
.text
);
1094 addword(wd
, &idximplicit
);
1098 error(err_unexbrace
, &t
.pos
);
1099 /* Error recovery: push nop */
1100 sitem
= snew(struct stack_item
);
1101 sitem
->type
= stack_nop
;
1102 sitem
->fpos
= t
.pos
;
1103 stk_push(parsestk
, sitem
);
1106 sitem
= stk_pop(parsestk
);
1109 * This closing brace could have been an
1110 * indication that the cross-paragraph stack
1111 * wants popping. Accordingly, we treat it here
1112 * as an indication that the paragraph is over.
1117 if (sitem
->type
& stack_ualt
) {
1118 whptr
= sitem
->whptr
;
1119 idximplicit
= sitem
->idximplicit
;
1121 if (sitem
->type
& stack_style
) {
1122 style
= word_Normal
;
1123 spcstyle
= word_WhiteSpace
;
1125 if (sitem
->type
& stack_idx
) {
1126 indexword
->text
= ustrdup(indexstr
.text
);
1127 if (index_downcase
) {
1130 ustrlow(indexword
->text
);
1131 ustrlow(indexstr
.text
);
1133 for (w
= idxwordlist
; w
; w
= w
->next
)
1138 rdadd(&indexstr
, L
'\0');
1139 index_merge(idx
, FALSE
, indexstr
.text
,
1140 idxwordlist
, &sitem
->fpos
);
1141 sfree(indexstr
.text
);
1143 if (sitem
->type
& stack_hyper
) {
1145 wd
.type
= word_HyperEnd
;
1150 if (!indexing
|| index_visible
)
1151 addword(wd
, &whptr
);
1153 addword(wd
, &idximplicit
);
1155 if (sitem
->type
& stack_quote
) {
1157 wd
.type
= toquotestyle(style
);
1159 wd
.aux
= quote_Close
;
1162 if (!indexing
|| index_visible
)
1163 addword(wd
, &whptr
);
1165 rdadd(&indexstr
, L
'"');
1166 addword(wd
, &idximplicit
);
1176 * In-paragraph comment: \#{ balanced braces }
1178 * Anything goes here; even tok_eop. We should
1179 * eat whitespace after the close brace _if_
1180 * there was whitespace before the \#.
1182 dtor(t
), t
= get_token(in
);
1183 if (t
.type
!= tok_lbrace
) {
1184 error(err_explbr
, &t
.pos
);
1187 while (braces
> 0) {
1188 dtor(t
), t
= get_token(in
);
1189 if (t
.type
== tok_lbrace
)
1191 else if (t
.type
== tok_rbrace
)
1193 else if (t
.type
== tok_eof
) {
1194 error(err_commenteof
, &t
.pos
);
1201 dtor(t
), t
= get_token(in
);
1202 if (t
.type
== tok_white
) {
1211 dtor(t
), t
= get_token(in
);
1212 if (t
.type
!= tok_lbrace
) {
1213 error(err_explbr
, &t
.pos
);
1216 * Enforce that \q may not be used anywhere
1217 * within \c. (It shouldn't be necessary
1218 * since the whole point of \c should be
1219 * that the user wants to exercise exact
1220 * control over the glyphs used, and
1221 * forbidding it has the useful effect of
1222 * relieving some backends of having to
1223 * make difficult decisions.)
1227 if (style
!= word_Code
&& style
!= word_WeakCode
) {
1229 wd
.type
= toquotestyle(style
);
1231 wd
.aux
= quote_Open
;
1234 if (!indexing
|| index_visible
)
1235 addword(wd
, &whptr
);
1237 rdadd(&indexstr
, L
'"');
1238 addword(wd
, &idximplicit
);
1240 stype
= stack_quote
;
1242 error(err_codequote
, &t
.pos
);
1245 sitem
= snew(struct stack_item
);
1246 sitem
->fpos
= t
.pos
;
1247 sitem
->type
= stype
;
1249 if (style
!= word_Normal
) {
1250 error(err_nestedstyles
, &t
.pos
);
1252 style
= word_WeakCode
;
1253 spcstyle
= tospacestyle(style
);
1254 sitem
->type
|= stack_style
;
1257 stk_push(parsestk
, sitem
);
1265 * Keyword, hyperlink, or \date. We expect a
1266 * left brace, some text, and then a right
1267 * brace. No nesting; no arguments.
1272 wd
.type
= word_UpperXref
;
1273 else if (t
.cmd
== c_k
)
1274 wd
.type
= word_LowerXref
;
1275 else if (t
.cmd
== c_W
)
1276 wd
.type
= word_HyperLink
;
1278 wd
.type
= word_Normal
;
1279 dtor(t
), t
= get_token(in
);
1280 if (t
.type
!= tok_lbrace
) {
1281 if (wd
.type
== word_Normal
) {
1282 time_t thetime
= time(NULL
);
1283 struct tm
*broken
= localtime(&thetime
);
1285 wdtext
= ustrftime(NULL
, broken
);
1288 error(err_explbr
, &t
.pos
);
1292 rdstring rs
= { 0, 0, NULL
};
1293 while (dtor(t
), t
= get_token(in
),
1294 t
.type
== tok_word
|| t
.type
== tok_white
) {
1295 if (t
.type
== tok_white
)
1298 rdadds(&rs
, t
.text
);
1300 if (wd
.type
== word_Normal
) {
1301 time_t thetime
= time(NULL
);
1302 struct tm
*broken
= localtime(&thetime
);
1303 wdtext
= ustrftime(rs
.text
, broken
);
1306 wdtext
= ustrdup(rs
.text
);
1309 if (t
.type
!= tok_rbrace
) {
1310 error(err_kwexprbr
, &t
.pos
);
1315 if (!indexing
|| index_visible
) {
1316 wd
.text
= ustrdup(wdtext
);
1317 addword(wd
, &whptr
);
1320 wd
.text
= ustrdup(wdtext
);
1321 addword(wd
, &idximplicit
);
1324 if (wd
.type
== word_HyperLink
) {
1326 * Hyperlinks are different: they then
1327 * expect another left brace, to begin
1328 * delimiting the text marked by the link.
1330 dtor(t
), t
= get_token(in
);
1331 sitem
= snew(struct stack_item
);
1332 sitem
->fpos
= wd
.fpos
;
1333 sitem
->type
= stack_hyper
;
1335 * Special cases: \W{}\i, \W{}\ii
1337 if (t
.type
== tok_cmd
&&
1338 (t
.cmd
== c_i
|| t
.cmd
== c_ii
)) {
1340 error(err_nestedindex
, &t
.pos
);
1342 /* Add an index-reference word with no
1344 wd
.type
= word_IndexRef
;
1349 indexword
= addword(wd
, &whptr
);
1350 /* Set up a rdstring to read the
1353 /* Flags so that we do the Right
1354 * Things with text */
1355 index_visible
= (type
!= c_I
);
1356 index_downcase
= (type
== c_ii
);
1359 idximplicit
= &idxwordlist
;
1361 sitem
->type
|= stack_idx
;
1363 dtor(t
), t
= get_token(in
);
1366 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1368 if (t
.type
== tok_cmd
&&
1369 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1370 if (style
!= word_Normal
)
1371 error(err_nestedstyles
, &t
.pos
);
1373 style
= (t
.cmd
== c_c ? word_Code
:
1374 t
.cmd
== c_cw ? word_WeakCode
:
1376 spcstyle
= tospacestyle(style
);
1377 sitem
->type
|= stack_style
;
1379 dtor(t
), t
= get_token(in
);
1381 if (t
.type
!= tok_lbrace
) {
1382 error(err_explbr
, &t
.pos
);
1385 stk_push(parsestk
, sitem
);
1393 if (style
!= word_Normal
) {
1394 error(err_nestedstyles
, &t
.pos
);
1395 /* Error recovery: eat lbrace, push nop. */
1396 dtor(t
), t
= get_token(in
);
1397 sitem
= snew(struct stack_item
);
1398 sitem
->fpos
= t
.pos
;
1399 sitem
->type
= stack_nop
;
1400 stk_push(parsestk
, sitem
);
1402 dtor(t
), t
= get_token(in
);
1403 if (t
.type
!= tok_lbrace
) {
1404 error(err_explbr
, &t
.pos
);
1406 style
= (type
== c_c ? word_Code
:
1407 type
== c_cw ? word_WeakCode
:
1409 spcstyle
= tospacestyle(style
);
1410 sitem
= snew(struct stack_item
);
1411 sitem
->fpos
= t
.pos
;
1412 sitem
->type
= stack_style
;
1413 stk_push(parsestk
, sitem
);
1421 error(err_nestedindex
, &t
.pos
);
1422 /* Error recovery: eat lbrace, push nop. */
1423 dtor(t
), t
= get_token(in
);
1424 sitem
= snew(struct stack_item
);
1425 sitem
->fpos
= t
.pos
;
1426 sitem
->type
= stack_nop
;
1427 stk_push(parsestk
, sitem
);
1429 sitem
= snew(struct stack_item
);
1430 sitem
->fpos
= t
.pos
;
1431 sitem
->type
= stack_idx
;
1432 dtor(t
), t
= get_token(in
);
1434 * Special cases: \i\c, \i\e, \i\cw
1437 if (t
.type
== tok_cmd
&&
1438 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1439 if (style
!= word_Normal
)
1440 error(err_nestedstyles
, &t
.pos
);
1442 style
= (t
.cmd
== c_c ? word_Code
:
1443 t
.cmd
== c_cw ? word_WeakCode
:
1445 spcstyle
= tospacestyle(style
);
1446 sitem
->type
|= stack_style
;
1448 dtor(t
), t
= get_token(in
);
1450 if (t
.type
!= tok_lbrace
) {
1452 error(err_explbr
, &t
.pos
);
1454 /* Add an index-reference word with no text as yet */
1455 wd
.type
= word_IndexRef
;
1460 indexword
= addword(wd
, &whptr
);
1461 /* Set up a rdstring to read the index text */
1463 /* Flags so that we do the Right Things with text */
1464 index_visible
= (type
!= c_I
);
1465 index_downcase
= (type
== c_ii
);
1468 idximplicit
= &idxwordlist
;
1469 /* Stack item to close the indexing on exit */
1470 stk_push(parsestk
, sitem
);
1475 utext
[0] = uchr
; utext
[1] = 0;
1481 if (!indexing
|| index_visible
) {
1482 wd
.text
= ustrdup(utext
);
1483 uword
= addword(wd
, &whptr
);
1487 wd
.text
= ustrdup(utext
);
1488 iword
= addword(wd
, &idximplicit
);
1491 dtor(t
), t
= get_token(in
);
1492 if (t
.type
== tok_lbrace
) {
1494 * \u with a left brace. Until the brace
1495 * closes, all further words go on a
1496 * sidetrack from the main thread of the
1499 sitem
= snew(struct stack_item
);
1500 sitem
->fpos
= t
.pos
;
1501 sitem
->type
= stack_ualt
;
1502 sitem
->whptr
= whptr
;
1503 sitem
->idximplicit
= idximplicit
;
1504 stk_push(parsestk
, sitem
);
1505 whptr
= uword ?
&uword
->alt
: NULL
;
1506 idximplicit
= iword ?
&iword
->alt
: NULL
;
1509 rdadd(&indexstr
, uchr
);
1514 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1515 error(err_badmidcmd
, t
.text
, &t
.pos
);
1520 dtor(t
), t
= get_token(in
);
1521 seenwhite
= iswhite
;
1524 /* Check the stack is empty */
1525 if (stk_top(parsestk
)) {
1526 while ((sitem
= stk_pop(parsestk
)))
1528 error(err_missingrbrace
, &t
.pos
);
1531 prev_para_type
= par
.type
;
1533 if (t
.type
== tok_eof
)
1537 if (stk_top(crossparastk
)) {
1540 error(err_missingrbrace2
, &t
.pos
);
1541 while ((p
= stk_pop(crossparastk
)))
1546 * We break to here rather than returning, because otherwise
1547 * this cleanup doesn't happen.
1551 stk_free(crossparastk
);
1554 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1555 paragraph
*head
= NULL
;
1556 paragraph
**hptr
= &head
;
1559 macros
= newtree234(macrocmp
);
1561 while (in
->currindex
< in
->nfiles
) {
1562 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1564 setpos(in
, in
->filenames
[in
->currindex
]);
1565 in
->charset
= in
->defcharset
;
1566 in
->csstate
= charset_init_state
;
1567 in
->wcpos
= in
->nwc
= 0;
1568 in
->pushback_chars
= NULL
;
1569 read_file(&hptr
, in
, idx
, macros
);
1574 macrocleanup(macros
);