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 */
429 } else if (c
== 'u') {
435 c
= get(in
, &cpos
, &rsc
);
436 } while (ishex(c
) && len
< 5);
438 } else if (iscmd(c
)) {
442 c
= get(in
, &cpos
, &rsc
);
447 * Now match the command against the list of available
451 ret
.text
= ustrdup(rs
.text
);
453 in
->pushback_chars
= dupstr(rsc
.text
+ prevpos
);
454 rsc
.text
[prevpos
] = '\0';
455 ret
.origtext
= dupstr(rsc
.text
);
457 ret
.origtext
= dupstr("");
463 } else if (c
== '{') { /* tok_lbrace */
464 ret
.type
= tok_lbrace
;
467 } else if (c
== '}') { /* tok_rbrace */
468 ret
.type
= tok_rbrace
;
471 } else { /* tok_word */
473 * Read a word: the longest possible contiguous sequence of
474 * things other than whitespace, backslash, braces and
475 * hyphen. A hyphen terminates the word but is returned as
476 * part of it; everything else is pushed back for the next
477 * token. The `aux' field contains TRUE if the word ends in
480 ret
.aux
= FALSE
; /* assumed for now */
483 if (iswhite(c
) || c
=='{' || c
=='}' || c
=='\\' || c
==EOF
) {
484 /* Put back the character that caused termination */
492 break; /* hyphen terminates word */
496 c
= get(in
, &cpos
, &rsc
);
499 ret
.text
= ustrdup(rs
.text
);
501 in
->pushback_chars
= dupstr(rsc
.text
+ prevpos
);
502 rsc
.text
[prevpos
] = '\0';
503 ret
.origtext
= dupstr(rsc
.text
);
505 ret
.origtext
= dupstr("");
514 * Determine whether the next input character is an open brace (for
515 * telling code paragraphs from paragraphs which merely start with
518 int isbrace(input
*in
) {
522 c
= get(in
, &cpos
, NULL
);
528 * Read the rest of a line that starts `\c'. Including nothing at
529 * all (tok_word with empty text).
531 token
get_codepar_token(input
*in
) {
534 rdstring rs
= { 0, 0, NULL
};
539 c
= get(in
, &cpos
, NULL
); /* expect (and discard) one space */
542 c
= get(in
, &cpos
, NULL
);
545 while (!isnl(c
) && c
!= EOF
) {
547 c
= get(in
, &cpos
, NULL
);
548 /* Discard \r just before \n. */
549 if (c2
!= 13 || !isnl(c
))
553 ret
.text
= ustrdup(rs
.text
);
559 * Adds a new word to a linked list
561 static word
*addword(word newword
, word
***hptrptr
) {
565 mnewword
= snew(word
);
566 *mnewword
= newword
; /* structure copy */
567 mnewword
->next
= NULL
;
568 **hptrptr
= mnewword
;
569 *hptrptr
= &mnewword
->next
;
574 * Adds a new paragraph to a linked list
576 static paragraph
*addpara(paragraph newpara
, paragraph
***hptrptr
) {
577 paragraph
*mnewpara
= snew(paragraph
);
578 *mnewpara
= newpara
; /* structure copy */
579 mnewpara
->next
= NULL
;
580 **hptrptr
= mnewpara
;
581 *hptrptr
= &mnewpara
->next
;
586 * Destructor before token is reassigned; should catch most memory
589 #define dtor(t) ( sfree(t.text), sfree(t.origtext) )
592 * Reads a single file (ie until get() returns EOF)
594 static void read_file(paragraph
***ret
, input
*in
, indexdata
*idx
,
598 word wd
, **whptr
, **idximplicit
;
599 wchar_t utext
[2], *wdtext
;
602 int iswhite
, seenwhite
;
607 stack_nop
= 0, /* do nothing (for error recovery) */
608 stack_ualt
= 1, /* \u alternative */
609 stack_style
= 2, /* \e, \c, \cw */
610 stack_idx
= 4, /* \I, \i, \ii */
611 stack_hyper
= 8, /* \W */
612 stack_quote
= 16, /* \q */
614 word
**whptr
; /* to restore from \u alternatives */
615 word
**idximplicit
; /* to restore from \u alternatives */
620 struct crossparaitem
{
621 int type
; /* currently c_lcont, c_quote or -1 */
622 int seen_lcont
, seen_quote
;
625 word
*indexword
, *uword
, *iword
;
628 int index_downcase
, index_visible
, indexing
;
629 const rdstring nullrs
= { 0, 0, NULL
};
636 crossparastk
= stk_new();
639 * Loop on each paragraph.
642 int start_cmd
= c__invalid
;
645 par
.origkeyword
= NULL
;
653 dtor(t
), t
= get_token(in
);
656 } while (t
.type
== tok_eop
);
657 if (t
.type
== tok_eof
)
661 * Parse code paragraphs separately.
663 if (t
.type
== tok_cmd
&& t
.cmd
== c_c
&& !isbrace(in
)) {
664 int wtype
= word_WeakCode
;
666 par
.type
= para_Code
;
669 dtor(t
), t
= get_codepar_token(in
);
671 wd
.breaks
= FALSE
; /* shouldn't need this... */
672 wd
.text
= ustrdup(t
.text
);
676 dtor(t
), t
= get_token(in
);
677 if (t
.type
== tok_white
) {
679 * The newline after a code-paragraph line
681 dtor(t
), t
= get_token(in
);
683 if (t
.type
== tok_eop
|| t
.type
== tok_eof
||
684 t
.type
== tok_rbrace
) { /* might be } terminating \lcont */
685 if (t
.type
== tok_rbrace
)
688 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_c
) {
689 wtype
= word_WeakCode
;
690 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_e
&&
691 wtype
== word_WeakCode
) {
694 error(err_brokencodepara
, &t
.pos
);
695 prev_para_type
= par
.type
;
697 while (t
.type
!= tok_eop
) /* error recovery: */
698 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
699 goto codeparabroken
; /* ick, but such is life */
702 prev_para_type
= par
.type
;
709 * Spot the special commands that define a grouping of more
710 * than one paragraph, and also the closing braces that
713 if (t
.type
== tok_cmd
&&
714 (t
.cmd
== c_lcont
|| t
.cmd
== c_quote
)) {
715 struct crossparaitem
*sitem
, *stop
;
719 * Expect, and swallow, an open brace.
721 dtor(t
), t
= get_token(in
);
722 if (t
.type
!= tok_lbrace
) {
723 error(err_explbr
, &t
.pos
);
728 * Also expect, and swallow, any whitespace after that
729 * (a newline before a code paragraph wouldn't be
733 dtor(t
), t
= get_token(in
);
734 } while (t
.type
== tok_white
);
737 if (cmd
== c_lcont
) {
739 * \lcont causes a continuation of a list item into
740 * multiple paragraphs (which may in turn contain
741 * nested lists, code paras etc). Hence, the previous
742 * paragraph must be of a list type.
744 sitem
= snew(struct crossparaitem
);
745 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
749 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
751 if (prev_para_type
== para_Bullet
||
752 prev_para_type
== para_NumberedList
||
753 prev_para_type
== para_Description
) {
754 sitem
->type
= c_lcont
;
755 sitem
->seen_lcont
= 1;
756 par
.type
= para_LcontPush
;
757 prev_para_type
= par
.type
;
761 * Push a null item on the cross-para stack so that
762 * when we see the corresponding closing brace we
763 * don't give a cascade error.
766 error(err_misplacedlcont
, &t
.pos
);
770 * \quote causes a group of paragraphs to be
771 * block-quoted (typically they will be indented a
774 sitem
= snew(struct crossparaitem
);
775 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
779 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
780 sitem
->type
= c_quote
;
781 sitem
->seen_quote
= 1;
782 par
.type
= para_QuotePush
;
783 prev_para_type
= par
.type
;
786 stk_push(crossparastk
, sitem
);
788 } else if (t
.type
== tok_rbrace
) {
789 struct crossparaitem
*sitem
= stk_pop(crossparastk
);
791 error(err_unexbrace
, &t
.pos
);
793 switch (sitem
->type
) {
795 par
.type
= para_LcontPop
;
796 prev_para_type
= par
.type
;
800 par
.type
= para_QuotePop
;
801 prev_para_type
= par
.type
;
810 while (t
.type
== tok_cmd
&&
811 macrolookup(macros
, in
, t
.text
, &t
.pos
)) {
812 dtor(t
), t
= get_token(in
);
816 * This token begins a paragraph. See if it's one of the
817 * special commands that define a paragraph type.
819 * (note that \# is special in a way, and \nocite takes no
822 par
.type
= para_Normal
;
823 if (t
.type
== tok_cmd
) {
825 int is_macro
= FALSE
;
833 error(err_badparatype
, t
.text
, &t
.pos
);
838 break; /* `\#{': isn't a comment para */
840 dtor(t
), t
= get_token(in
);
841 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
842 continue; /* next paragraph */
846 * 1 -- exactly one keyword
847 * 2 -- at least one keyword
848 * 4 -- any number of keywords including zero
849 * 8 -- at least one keyword and then nothing else
850 * 16 -- nothing at all! no keywords, no body
851 * 32 -- no keywords at all
853 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
854 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
855 case c_BR
: needkw
= 1; par
.type
= para_BR
;
856 start_cmd
= c_BR
; break;
857 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
858 case c_H
: needkw
= 2; par
.type
= para_Heading
;
861 case c_IM
: needkw
= 2; par
.type
= para_IM
;
862 start_cmd
= c_IM
; break;
863 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
864 par
.aux
= t
.aux
; break;
865 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
866 /* For \b and \n the keyword is optional */
867 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
868 case c_dt
: needkw
= 4; par
.type
= para_DescribedThing
; break;
869 case c_dd
: needkw
= 4; par
.type
= para_Description
; break;
870 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
871 case c_cfg
: needkw
= 8; par
.type
= para_Config
;
872 start_cmd
= c_cfg
; break;
873 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
874 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
875 /* For \nocite the keyword is _everything_ */
876 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
877 case c_preamble
: needkw
= 32; par
.type
= para_Normal
; break;
878 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
879 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
880 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
883 if (par
.type
== para_Chapter
||
884 par
.type
== para_Heading
||
885 par
.type
== para_Subsect
||
886 par
.type
== para_Appendix
||
887 par
.type
== para_UnnumberedChapter
) {
888 struct crossparaitem
*sitem
= stk_top(crossparastk
);
889 if (sitem
&& (sitem
->seen_lcont
|| sitem
->seen_quote
)) {
890 error(err_sectmarkerinblock
,
892 (sitem
->seen_lcont ?
"lcont" : "quote"));
897 rdstring rs
= { 0, 0, NULL
};
898 rdstringc rsc
= { 0, 0, NULL
};
903 dtor(t
), t
= get_token(in
);
905 while (t
.type
== tok_lbrace
) {
906 /* This is a keyword. */
908 /* FIXME: there will be bugs if anyone specifies an
909 * empty keyword (\foo{}), so trap this case. */
910 while (dtor(t
), t
= get_token(in
),
911 t
.type
== tok_word
||
912 t
.type
== tok_white
||
913 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
914 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) ||
915 (t
.type
== tok_cmd
&& t
.cmd
== c_u
)) {
916 if (t
.type
== tok_white
||
917 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
)) {
920 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_u
) {
923 rdaddsc(&rsc
, t
.origtext
);
926 rdaddsc(&rsc
, t
.origtext
);
929 if (t
.type
!= tok_rbrace
) {
930 error(err_kwunclosed
, &t
.pos
);
933 rdadd(&rs
, 0); /* add string terminator */
934 rdaddc(&rsc
, 0); /* add string terminator */
935 dtor(t
), t
= get_token(in
); /* eat right brace */
938 rdadd(&rs
, 0); /* add string terminator */
939 rdaddc(&rsc
, 0); /* add string terminator */
941 /* See whether we have the right number of keywords. */
942 if ((needkw
& 48) && nkeys
> 0)
943 error(err_kwillegal
, &fp
);
944 if ((needkw
& 11) && nkeys
== 0)
945 error(err_kwexpected
, &fp
);
946 if ((needkw
& 5) && nkeys
> 1)
947 error(err_kwtoomany
, &fp
);
951 * Macro definition. Get the rest of the line
952 * as a code-paragraph token, repeatedly until
953 * there's nothing more left of it. Separate
956 rdstring macrotext
= { 0, 0, NULL
};
958 dtor(t
), t
= get_codepar_token(in
);
959 if (macrotext
.pos
> 0)
960 rdadd(¯otext
, L
'\n');
961 rdadds(¯otext
, t
.text
);
962 dtor(t
), t
= get_token(in
);
963 if (t
.type
== tok_eop
) break;
965 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
966 continue; /* next paragraph */
969 par
.keyword
= rdtrim(&rs
);
970 par
.origkeyword
= rdtrimc(&rsc
);
972 /* Move to EOP in case of needkw==8 or 16 (no body) */
974 /* We allow whitespace even when we expect no para body */
975 while (t
.type
== tok_white
)
976 dtor(t
), t
= get_token(in
);
977 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
978 (start_cmd
== c__invalid
||
979 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
)) {
980 error(err_bodyillegal
, &t
.pos
);
981 /* Error recovery: eat the rest of the paragraph */
982 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
983 (start_cmd
== c__invalid
||
984 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
))
985 dtor(t
), t
= get_token(in
);
987 if (t
.type
== tok_cmd
)
988 already
= TRUE
;/* inhibit get_token at top of loop */
989 prev_para_type
= par
.type
;
992 if (par
.type
== para_Config
) {
993 input_configure(in
, &par
);
995 continue; /* next paragraph */
1001 * Now read the actual paragraph, word by word, adding to
1002 * the paragraph list.
1004 * Mid-paragraph commands:
1017 parsestk
= stk_new();
1018 style
= word_Normal
;
1019 spcstyle
= word_WhiteSpace
;
1022 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
1026 /* Handle implicit paragraph breaks after \IM, \BR etc */
1027 if (start_cmd
!= c__invalid
&&
1028 t
.type
== tok_cmd
&& t
.cmd
== start_cmd
) {
1029 already
= TRUE
; /* inhibit get_token at top of loop */
1033 if (t
.type
== tok_cmd
&& t
.cmd
== c__nop
) {
1034 dtor(t
), t
= get_token(in
);
1035 continue; /* do nothing! */
1038 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
1039 t
.type
= tok_word
; /* nice and simple */
1040 t
.aux
= 0; /* even if `\-' - nonbreaking! */
1042 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
1043 t
.type
= tok_word
; /* nice and simple */
1045 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
1046 t
.aux
= 0; /* (nonbreaking) */
1050 if (whptr
== &par
.words
)
1051 break; /* strip whitespace at start of para */
1060 * Inhibit use of whitespace if it's (probably the
1061 * newline) before a repeat \IM / \BR type
1064 if (start_cmd
!= c__invalid
) {
1065 dtor(t
), t
= get_token(in
);
1067 if (t
.type
== tok_cmd
&& t
.cmd
== start_cmd
)
1072 rdadd(&indexstr
, ' ');
1073 if (!indexing
|| index_visible
)
1074 addword(wd
, &whptr
);
1076 addword(wd
, &idximplicit
);
1081 rdadds(&indexstr
, t
.text
);
1087 if (!indexing
|| index_visible
) {
1088 wd
.text
= ustrdup(t
.text
);
1089 addword(wd
, &whptr
);
1092 wd
.text
= ustrdup(t
.text
);
1093 addword(wd
, &idximplicit
);
1097 error(err_unexbrace
, &t
.pos
);
1098 /* Error recovery: push nop */
1099 sitem
= snew(struct stack_item
);
1100 sitem
->type
= stack_nop
;
1101 sitem
->fpos
= t
.pos
;
1102 stk_push(parsestk
, sitem
);
1105 sitem
= stk_pop(parsestk
);
1108 * This closing brace could have been an
1109 * indication that the cross-paragraph stack
1110 * wants popping. Accordingly, we treat it here
1111 * as an indication that the paragraph is over.
1116 if (sitem
->type
& stack_ualt
) {
1117 whptr
= sitem
->whptr
;
1118 idximplicit
= sitem
->idximplicit
;
1120 if (sitem
->type
& stack_style
) {
1121 style
= word_Normal
;
1122 spcstyle
= word_WhiteSpace
;
1124 if (sitem
->type
& stack_idx
) {
1125 indexword
->text
= ustrdup(indexstr
.text
);
1126 if (index_downcase
) {
1129 ustrlow(indexword
->text
);
1130 ustrlow(indexstr
.text
);
1132 for (w
= idxwordlist
; w
; w
= w
->next
)
1137 rdadd(&indexstr
, L
'\0');
1138 index_merge(idx
, FALSE
, indexstr
.text
,
1139 idxwordlist
, &sitem
->fpos
);
1140 sfree(indexstr
.text
);
1142 if (sitem
->type
& stack_hyper
) {
1144 wd
.type
= word_HyperEnd
;
1149 if (!indexing
|| index_visible
)
1150 addword(wd
, &whptr
);
1152 addword(wd
, &idximplicit
);
1154 if (sitem
->type
& stack_quote
) {
1156 wd
.type
= toquotestyle(style
);
1158 wd
.aux
= quote_Close
;
1161 if (!indexing
|| index_visible
)
1162 addword(wd
, &whptr
);
1164 rdadd(&indexstr
, L
'"');
1165 addword(wd
, &idximplicit
);
1175 * In-paragraph comment: \#{ balanced braces }
1177 * Anything goes here; even tok_eop. We should
1178 * eat whitespace after the close brace _if_
1179 * there was whitespace before the \#.
1181 dtor(t
), t
= get_token(in
);
1182 if (t
.type
!= tok_lbrace
) {
1183 error(err_explbr
, &t
.pos
);
1186 while (braces
> 0) {
1187 dtor(t
), t
= get_token(in
);
1188 if (t
.type
== tok_lbrace
)
1190 else if (t
.type
== tok_rbrace
)
1192 else if (t
.type
== tok_eof
) {
1193 error(err_commenteof
, &t
.pos
);
1200 dtor(t
), t
= get_token(in
);
1201 if (t
.type
== tok_white
) {
1210 dtor(t
), t
= get_token(in
);
1211 if (t
.type
!= tok_lbrace
) {
1212 error(err_explbr
, &t
.pos
);
1215 * Enforce that \q may not be used anywhere
1216 * within \c. (It shouldn't be necessary
1217 * since the whole point of \c should be
1218 * that the user wants to exercise exact
1219 * control over the glyphs used, and
1220 * forbidding it has the useful effect of
1221 * relieving some backends of having to
1222 * make difficult decisions.)
1226 if (style
!= word_Code
&& style
!= word_WeakCode
) {
1228 wd
.type
= toquotestyle(style
);
1230 wd
.aux
= quote_Open
;
1233 if (!indexing
|| index_visible
)
1234 addword(wd
, &whptr
);
1236 rdadd(&indexstr
, L
'"');
1237 addword(wd
, &idximplicit
);
1239 stype
= stack_quote
;
1241 error(err_codequote
, &t
.pos
);
1244 sitem
= snew(struct stack_item
);
1245 sitem
->fpos
= t
.pos
;
1246 sitem
->type
= stype
;
1248 if (style
!= word_Normal
) {
1249 error(err_nestedstyles
, &t
.pos
);
1251 style
= word_WeakCode
;
1252 spcstyle
= tospacestyle(style
);
1253 sitem
->type
|= stack_style
;
1256 stk_push(parsestk
, sitem
);
1264 * Keyword, hyperlink, or \date. We expect a
1265 * left brace, some text, and then a right
1266 * brace. No nesting; no arguments.
1271 wd
.type
= word_UpperXref
;
1272 else if (t
.cmd
== c_k
)
1273 wd
.type
= word_LowerXref
;
1274 else if (t
.cmd
== c_W
)
1275 wd
.type
= word_HyperLink
;
1277 wd
.type
= word_Normal
;
1278 dtor(t
), t
= get_token(in
);
1279 if (t
.type
!= tok_lbrace
) {
1280 if (wd
.type
== word_Normal
) {
1281 time_t thetime
= time(NULL
);
1282 struct tm
*broken
= localtime(&thetime
);
1284 wdtext
= ustrftime(NULL
, broken
);
1287 error(err_explbr
, &t
.pos
);
1291 rdstring rs
= { 0, 0, NULL
};
1292 while (dtor(t
), t
= get_token(in
),
1293 t
.type
== tok_word
|| t
.type
== tok_white
) {
1294 if (t
.type
== tok_white
)
1297 rdadds(&rs
, t
.text
);
1299 if (wd
.type
== word_Normal
) {
1300 time_t thetime
= time(NULL
);
1301 struct tm
*broken
= localtime(&thetime
);
1302 wdtext
= ustrftime(rs
.text
, broken
);
1305 wdtext
= ustrdup(rs
.text
);
1308 if (t
.type
!= tok_rbrace
) {
1309 error(err_kwexprbr
, &t
.pos
);
1314 if (!indexing
|| index_visible
) {
1315 wd
.text
= ustrdup(wdtext
);
1316 addword(wd
, &whptr
);
1319 wd
.text
= ustrdup(wdtext
);
1320 addword(wd
, &idximplicit
);
1323 if (wd
.type
== word_HyperLink
) {
1325 * Hyperlinks are different: they then
1326 * expect another left brace, to begin
1327 * delimiting the text marked by the link.
1329 dtor(t
), t
= get_token(in
);
1330 sitem
= snew(struct stack_item
);
1331 sitem
->fpos
= wd
.fpos
;
1332 sitem
->type
= stack_hyper
;
1334 * Special cases: \W{}\i, \W{}\ii
1336 if (t
.type
== tok_cmd
&&
1337 (t
.cmd
== c_i
|| t
.cmd
== c_ii
)) {
1339 error(err_nestedindex
, &t
.pos
);
1341 /* Add an index-reference word with no
1343 wd
.type
= word_IndexRef
;
1348 indexword
= addword(wd
, &whptr
);
1349 /* Set up a rdstring to read the
1352 /* Flags so that we do the Right
1353 * Things with text */
1354 index_visible
= (type
!= c_I
);
1355 index_downcase
= (type
== c_ii
);
1358 idximplicit
= &idxwordlist
;
1360 sitem
->type
|= stack_idx
;
1362 dtor(t
), t
= get_token(in
);
1365 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1367 if (t
.type
== tok_cmd
&&
1368 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1369 if (style
!= word_Normal
)
1370 error(err_nestedstyles
, &t
.pos
);
1372 style
= (t
.cmd
== c_c ? word_Code
:
1373 t
.cmd
== c_cw ? word_WeakCode
:
1375 spcstyle
= tospacestyle(style
);
1376 sitem
->type
|= stack_style
;
1378 dtor(t
), t
= get_token(in
);
1380 if (t
.type
!= tok_lbrace
) {
1381 error(err_explbr
, &t
.pos
);
1384 stk_push(parsestk
, sitem
);
1392 if (style
!= word_Normal
) {
1393 error(err_nestedstyles
, &t
.pos
);
1394 /* Error recovery: eat lbrace, push nop. */
1395 dtor(t
), t
= get_token(in
);
1396 sitem
= snew(struct stack_item
);
1397 sitem
->fpos
= t
.pos
;
1398 sitem
->type
= stack_nop
;
1399 stk_push(parsestk
, sitem
);
1401 dtor(t
), t
= get_token(in
);
1402 if (t
.type
!= tok_lbrace
) {
1403 error(err_explbr
, &t
.pos
);
1405 style
= (type
== c_c ? word_Code
:
1406 type
== c_cw ? word_WeakCode
:
1408 spcstyle
= tospacestyle(style
);
1409 sitem
= snew(struct stack_item
);
1410 sitem
->fpos
= t
.pos
;
1411 sitem
->type
= stack_style
;
1412 stk_push(parsestk
, sitem
);
1420 error(err_nestedindex
, &t
.pos
);
1421 /* Error recovery: eat lbrace, push nop. */
1422 dtor(t
), t
= get_token(in
);
1423 sitem
= snew(struct stack_item
);
1424 sitem
->fpos
= t
.pos
;
1425 sitem
->type
= stack_nop
;
1426 stk_push(parsestk
, sitem
);
1428 sitem
= snew(struct stack_item
);
1429 sitem
->fpos
= t
.pos
;
1430 sitem
->type
= stack_idx
;
1431 dtor(t
), t
= get_token(in
);
1433 * Special cases: \i\c, \i\e, \i\cw
1436 if (t
.type
== tok_cmd
&&
1437 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1438 if (style
!= word_Normal
)
1439 error(err_nestedstyles
, &t
.pos
);
1441 style
= (t
.cmd
== c_c ? word_Code
:
1442 t
.cmd
== c_cw ? word_WeakCode
:
1444 spcstyle
= tospacestyle(style
);
1445 sitem
->type
|= stack_style
;
1447 dtor(t
), t
= get_token(in
);
1449 if (t
.type
!= tok_lbrace
) {
1451 error(err_explbr
, &t
.pos
);
1453 /* Add an index-reference word with no text as yet */
1454 wd
.type
= word_IndexRef
;
1459 indexword
= addword(wd
, &whptr
);
1460 /* Set up a rdstring to read the index text */
1462 /* Flags so that we do the Right Things with text */
1463 index_visible
= (type
!= c_I
);
1464 index_downcase
= (type
== c_ii
);
1467 idximplicit
= &idxwordlist
;
1468 /* Stack item to close the indexing on exit */
1469 stk_push(parsestk
, sitem
);
1474 utext
[0] = uchr
; utext
[1] = 0;
1480 if (!indexing
|| index_visible
) {
1481 wd
.text
= ustrdup(utext
);
1482 uword
= addword(wd
, &whptr
);
1486 wd
.text
= ustrdup(utext
);
1487 iword
= addword(wd
, &idximplicit
);
1490 dtor(t
), t
= get_token(in
);
1491 if (t
.type
== tok_lbrace
) {
1493 * \u with a left brace. Until the brace
1494 * closes, all further words go on a
1495 * sidetrack from the main thread of the
1498 sitem
= snew(struct stack_item
);
1499 sitem
->fpos
= t
.pos
;
1500 sitem
->type
= stack_ualt
;
1501 sitem
->whptr
= whptr
;
1502 sitem
->idximplicit
= idximplicit
;
1503 stk_push(parsestk
, sitem
);
1504 whptr
= uword ?
&uword
->alt
: NULL
;
1505 idximplicit
= iword ?
&iword
->alt
: NULL
;
1508 rdadd(&indexstr
, uchr
);
1513 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1514 error(err_badmidcmd
, t
.text
, &t
.pos
);
1519 dtor(t
), t
= get_token(in
);
1520 seenwhite
= iswhite
;
1523 /* Check the stack is empty */
1524 if (stk_top(parsestk
)) {
1525 while ((sitem
= stk_pop(parsestk
)))
1527 error(err_missingrbrace
, &t
.pos
);
1530 prev_para_type
= par
.type
;
1532 if (t
.type
== tok_eof
)
1536 if (stk_top(crossparastk
)) {
1539 error(err_missingrbrace2
, &t
.pos
);
1540 while ((p
= stk_pop(crossparastk
)))
1545 * We break to here rather than returning, because otherwise
1546 * this cleanup doesn't happen.
1550 stk_free(crossparastk
);
1553 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1554 paragraph
*head
= NULL
;
1555 paragraph
**hptr
= &head
;
1558 macros
= newtree234(macrocmp
);
1560 while (in
->currindex
< in
->nfiles
) {
1561 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1563 setpos(in
, in
->filenames
[in
->currindex
]);
1564 in
->charset
= in
->defcharset
;
1565 in
->csstate
= charset_init_state
;
1566 in
->wcpos
= in
->nwc
= 0;
1567 in
->pushback_chars
= NULL
;
1568 read_file(&hptr
, in
, idx
, macros
);
1573 macrocleanup(macros
);