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
];
106 if (in
->stack
->text
[++in
->stack
->ptr
] == L
'\0') {
107 macrostack
*tmp
= in
->stack
;
108 in
->stack
= tmp
->next
;
113 else if (in
->currfp
) {
115 while (in
->wcpos
>= in
->nwc
) {
117 int c
= getc(in
->currfp
);
128 /* Track line numbers, for error reporting */
131 if (in
->reportcols
) {
134 in
->pos
.col
= 1 + (in
->pos
.col
+ TAB_STOP
-1) % TAB_STOP
;
151 * Do input character set translation, so that we return
163 in
->nwc
= charset_to_unicode(&p
, &inlen
,
164 in
->wc
, lenof(in
->wc
),
165 in
->charset
, &in
->csstate
,
167 assert(p
== buf
+1 && inlen
== 0);
173 return in
->wc
[in
->wcpos
++];
180 * Lexical analysis of source files.
182 typedef struct token_Tag token
;
191 tok_eof
, /* end of file */
192 tok_eop
, /* end of paragraph */
193 tok_white
, /* whitespace */
194 tok_word
, /* a word or word fragment */
195 tok_cmd
, /* \command */
200 /* Halibut command keywords. */
202 c__invalid
, /* invalid command */
203 c__comment
, /* comment command (\#) */
204 c__escaped
, /* escaped character */
206 c__nbsp
, /* nonbreaking space */
207 c_A
, /* appendix heading */
208 c_B
, /* bibliography entry */
209 c_BR
, /* bibliography rewrite */
210 c_C
, /* chapter heading */
212 c_I
, /* invisible index mark */
213 c_IM
, /* index merge/rewrite */
214 c_K
, /* capitalised cross-reference */
215 c_S
, /* aux field is 0, 1, 2, ... */
216 c_U
, /* unnumbered-chapter heading */
217 c_W
, /* Web hyperlink */
218 c_b
, /* bulletted list */
220 c_cfg
, /* configuration directive */
221 c_copyright
, /* copyright statement */
222 c_cw
, /* weak code */
223 c_date
, /* document processing date */
224 c_dd
, /* description list: description */
225 c_define
, /* macro definition */
226 c_dt
, /* description list: described thing */
228 c_i
, /* visible index mark */
229 c_ii
, /* uncapitalised visible index mark */
230 c_k
, /* uncapitalised cross-reference */
231 c_lcont
, /* continuation para(s) for list item */
232 c_n
, /* numbered list */
233 c_nocite
, /* bibliography trickery */
234 c_preamble
, /* (obsolete) preamble text */
235 c_q
, /* quote marks */
236 c_quote
, /* block-quoted paragraphs */
237 c_rule
, /* horizontal rule */
238 c_title
, /* document title */
239 c_u
, /* aux field is char code */
240 c_versionid
/* document RCS id */
243 /* Perhaps whitespace should be defined in a more Unicode-friendly way? */
244 #define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 )
245 #define isnl(c) ( (c)==10 )
246 #define isdec(c) ( ((c)>='0'&&(c)<='9') )
247 #define fromdec(c) ( (c)-'0' )
248 #define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f'))
249 #define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) )
250 #define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
253 * Keyword comparison function. Like strcmp, but between a wchar_t *
256 static int kwcmp(wchar_t const *p
, char const *q
) {
260 } while (*p
++ && *q
++ && !i
);
267 static void match_kw(token
*tok
) {
269 * FIXME. The ids are explicit in here so as to allow long-name
270 * equivalents to the various very short keywords.
272 static const struct { char const *name
; int id
; } keywords
[] = {
273 {"#", c__comment
}, /* comment command (\#) */
274 {"-", c__escaped
}, /* nonbreaking hyphen */
275 {".", c__nop
}, /* no-op */
276 {"A", c_A
}, /* appendix heading */
277 {"B", c_B
}, /* bibliography entry */
278 {"BR", c_BR
}, /* bibliography rewrite */
279 {"C", c_C
}, /* chapter heading */
280 {"H", c_H
}, /* heading */
281 {"I", c_I
}, /* invisible index mark */
282 {"IM", c_IM
}, /* index merge/rewrite */
283 {"K", c_K
}, /* capitalised cross-reference */
284 {"U", c_U
}, /* unnumbered-chapter heading */
285 {"W", c_W
}, /* Web hyperlink */
286 {"\\", c__escaped
}, /* escaped backslash (\\) */
287 {"_", c__nbsp
}, /* nonbreaking space (\_) */
288 {"b", c_b
}, /* bulletted list */
289 {"c", c_c
}, /* code */
290 {"cfg", c_cfg
}, /* configuration directive */
291 {"copyright", c_copyright
}, /* copyright statement */
292 {"cw", c_cw
}, /* weak code */
293 {"date", c_date
}, /* document processing date */
294 {"dd", c_dd
}, /* description list: description */
295 {"define", c_define
}, /* macro definition */
296 {"dt", c_dt
}, /* description list: described thing */
297 {"e", c_e
}, /* emphasis */
298 {"i", c_i
}, /* visible index mark */
299 {"ii", c_ii
}, /* uncapitalised visible index mark */
300 {"k", c_k
}, /* uncapitalised cross-reference */
301 {"lcont", c_lcont
}, /* continuation para(s) for list item */
302 {"n", c_n
}, /* numbered list */
303 {"nocite", c_nocite
}, /* bibliography trickery */
304 {"preamble", c_preamble
}, /* (obsolete) preamble text */
305 {"q", c_q
}, /* quote marks */
306 {"quote", c_quote
}, /* block-quoted paragraphs */
307 {"rule", c_rule
}, /* horizontal rule */
308 {"title", c_title
}, /* document title */
309 {"versionid", c_versionid
}, /* document RCS id */
310 {"{", c__escaped
}, /* escaped lbrace (\{) */
311 {"}", c__escaped
}, /* escaped rbrace (\}) */
316 * Special cases: \S{0,1,2,...} and \uABCD. If the syntax
317 * doesn't match correctly, we just fall through to the
318 * binary-search phase.
320 if (tok
->text
[0] == 'S') {
321 /* We expect numeric characters thereafter. */
322 wchar_t *p
= tok
->text
+1;
328 while (*p
&& isdec(*p
)) {
329 n
= 10 * n
+ fromdec(*p
);
338 } else if (tok
->text
[0] == 'u') {
339 /* We expect hex characters thereafter. */
340 wchar_t *p
= tok
->text
+1;
342 while (*p
&& ishex(*p
)) {
343 n
= 16 * n
+ fromhex(*p
);
354 j
= sizeof(keywords
)/sizeof(*keywords
);
357 c
= kwcmp(tok
->text
, keywords
[k
].name
);
363 tok
->cmd
= keywords
[k
].id
;
368 tok
->cmd
= c__invalid
;
373 * Read a token from the input file, in the normal way (`normal' in
374 * the sense that code paragraphs work a different way).
376 token
get_token(input
*in
) {
381 rdstring rs
= { 0, 0, NULL
};
382 rdstringc rsc
= { 0, 0, NULL
};
385 ret
.text
= NULL
; /* default */
386 ret
.origtext
= NULL
; /* default */
387 if (in
->pushback_chars
) {
388 rdaddsc(&rsc
, in
->pushback_chars
);
389 sfree(in
->pushback_chars
);
390 in
->pushback_chars
= NULL
;
392 c
= get(in
, &cpos
, &rsc
);
394 if (iswhite(c
)) { /* tok_white or tok_eop */
401 } while ((c
= get(in
, &cpos
, &rsc
)) != EOF
&& iswhite(c
));
408 in
->pushback_chars
= dupstr(rsc
.text
+ prevpos
);
412 ret
.type
= (nls
> 1 ? tok_eop
: tok_white
);
414 } else if (c
== EOF
) { /* tok_eof */
418 } else if (c
== '\\') { /* tok_cmd */
419 rsc
.pos
= prevpos
= 0;
420 c
= get(in
, &cpos
, &rsc
);
421 if (c
== '-' || c
== '\\' || c
== '_' ||
422 c
== '#' || c
== '{' || c
== '}' || c
== '.') {
423 /* single-char command */
425 } else if (c
== 'u') {
431 c
= get(in
, &cpos
, &rsc
);
432 } while (ishex(c
) && len
< 5);
434 } else if (iscmd(c
)) {
438 c
= get(in
, &cpos
, &rsc
);
443 * Now match the command against the list of available
447 ret
.text
= ustrdup(rs
.text
);
449 in
->pushback_chars
= dupstr(rsc
.text
+ prevpos
);
450 rsc
.text
[prevpos
] = '\0';
451 ret
.origtext
= dupstr(rsc
.text
);
453 ret
.origtext
= dupstr("");
459 } else if (c
== '{') { /* tok_lbrace */
460 ret
.type
= tok_lbrace
;
463 } else if (c
== '}') { /* tok_rbrace */
464 ret
.type
= tok_rbrace
;
467 } else { /* tok_word */
469 * Read a word: the longest possible contiguous sequence of
470 * things other than whitespace, backslash, braces and
471 * hyphen. A hyphen terminates the word but is returned as
472 * part of it; everything else is pushed back for the next
473 * token. The `aux' field contains TRUE if the word ends in
476 ret
.aux
= FALSE
; /* assumed for now */
479 if (iswhite(c
) || c
=='{' || c
=='}' || c
=='\\' || c
==EOF
) {
480 /* Put back the character that caused termination */
488 break; /* hyphen terminates word */
492 c
= get(in
, &cpos
, &rsc
);
495 ret
.text
= ustrdup(rs
.text
);
497 in
->pushback_chars
= dupstr(rsc
.text
+ prevpos
);
498 rsc
.text
[prevpos
] = '\0';
499 ret
.origtext
= dupstr(rsc
.text
);
501 ret
.origtext
= dupstr("");
510 * Determine whether the next input character is an open brace (for
511 * telling code paragraphs from paragraphs which merely start with
514 int isbrace(input
*in
) {
518 c
= get(in
, &cpos
, NULL
);
524 * Read the rest of a line that starts `\c'. Including nothing at
525 * all (tok_word with empty text).
527 token
get_codepar_token(input
*in
) {
530 rdstring rs
= { 0, 0, NULL
};
535 c
= get(in
, &cpos
, NULL
); /* expect (and discard) one space */
538 c
= get(in
, &cpos
, NULL
);
541 while (!isnl(c
) && c
!= EOF
) {
543 c
= get(in
, &cpos
, NULL
);
544 /* Discard \r just before \n. */
545 if (c2
!= 13 || !isnl(c
))
549 ret
.text
= ustrdup(rs
.text
);
555 * Adds a new word to a linked list
557 static word
*addword(word newword
, word
***hptrptr
) {
561 mnewword
= snew(word
);
562 *mnewword
= newword
; /* structure copy */
563 mnewword
->next
= NULL
;
564 **hptrptr
= mnewword
;
565 *hptrptr
= &mnewword
->next
;
570 * Adds a new paragraph to a linked list
572 static paragraph
*addpara(paragraph newpara
, paragraph
***hptrptr
) {
573 paragraph
*mnewpara
= snew(paragraph
);
574 *mnewpara
= newpara
; /* structure copy */
575 mnewpara
->next
= NULL
;
576 **hptrptr
= mnewpara
;
577 *hptrptr
= &mnewpara
->next
;
582 * Destructor before token is reassigned; should catch most memory
585 #define dtor(t) ( sfree(t.text), sfree(t.origtext) )
588 * Reads a single file (ie until get() returns EOF)
590 static void read_file(paragraph
***ret
, input
*in
, indexdata
*idx
) {
593 word wd
, **whptr
, **idximplicit
;
595 wchar_t utext
[2], *wdtext
;
598 int iswhite
, seenwhite
;
603 stack_nop
= 0, /* do nothing (for error recovery) */
604 stack_ualt
= 1, /* \u alternative */
605 stack_style
= 2, /* \e, \c, \cw */
606 stack_idx
= 4, /* \I, \i, \ii */
607 stack_hyper
= 8, /* \W */
608 stack_quote
= 16, /* \q */
610 word
**whptr
; /* to restore from \u alternatives */
611 word
**idximplicit
; /* to restore from \u alternatives */
616 struct crossparaitem
{
617 int type
; /* currently c_lcont, c_quote or -1 */
618 int seen_lcont
, seen_quote
;
621 word
*indexword
, *uword
, *iword
;
624 int index_downcase
, index_visible
, indexing
;
625 const rdstring nullrs
= { 0, 0, NULL
};
630 macros
= newtree234(macrocmp
);
633 crossparastk
= stk_new();
636 * Loop on each paragraph.
639 int start_cmd
= c__invalid
;
642 par
.origkeyword
= NULL
;
650 dtor(t
), t
= get_token(in
);
653 } while (t
.type
== tok_eop
);
654 if (t
.type
== tok_eof
)
658 * Parse code paragraphs separately.
660 if (t
.type
== tok_cmd
&& t
.cmd
== c_c
&& !isbrace(in
)) {
661 int wtype
= word_WeakCode
;
663 par
.type
= para_Code
;
666 dtor(t
), t
= get_codepar_token(in
);
668 wd
.breaks
= FALSE
; /* shouldn't need this... */
669 wd
.text
= ustrdup(t
.text
);
673 dtor(t
), t
= get_token(in
);
674 if (t
.type
== tok_white
) {
676 * The newline after a code-paragraph line
678 dtor(t
), t
= get_token(in
);
680 if (t
.type
== tok_eop
|| t
.type
== tok_eof
||
681 t
.type
== tok_rbrace
) { /* might be } terminating \lcont */
682 if (t
.type
== tok_rbrace
)
685 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_c
) {
686 wtype
= word_WeakCode
;
687 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_e
&&
688 wtype
== word_WeakCode
) {
691 error(err_brokencodepara
, &t
.pos
);
692 prev_para_type
= par
.type
;
694 while (t
.type
!= tok_eop
) /* error recovery: */
695 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
696 goto codeparabroken
; /* ick, but such is life */
699 prev_para_type
= par
.type
;
706 * Spot the special commands that define a grouping of more
707 * than one paragraph, and also the closing braces that
710 if (t
.type
== tok_cmd
&&
711 (t
.cmd
== c_lcont
|| t
.cmd
== c_quote
)) {
712 struct crossparaitem
*sitem
, *stop
;
716 * Expect, and swallow, an open brace.
718 dtor(t
), t
= get_token(in
);
719 if (t
.type
!= tok_lbrace
) {
720 error(err_explbr
, &t
.pos
);
725 * Also expect, and swallow, any whitespace after that
726 * (a newline before a code paragraph wouldn't be
730 dtor(t
), t
= get_token(in
);
731 } while (t
.type
== tok_white
);
734 if (cmd
== c_lcont
) {
736 * \lcont causes a continuation of a list item into
737 * multiple paragraphs (which may in turn contain
738 * nested lists, code paras etc). Hence, the previous
739 * paragraph must be of a list type.
741 sitem
= snew(struct crossparaitem
);
742 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
746 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
748 if (prev_para_type
== para_Bullet
||
749 prev_para_type
== para_NumberedList
||
750 prev_para_type
== para_Description
) {
751 sitem
->type
= c_lcont
;
752 sitem
->seen_lcont
= 1;
753 par
.type
= para_LcontPush
;
754 prev_para_type
= par
.type
;
758 * Push a null item on the cross-para stack so that
759 * when we see the corresponding closing brace we
760 * don't give a cascade error.
763 error(err_misplacedlcont
, &t
.pos
);
767 * \quote causes a group of paragraphs to be
768 * block-quoted (typically they will be indented a
771 sitem
= snew(struct crossparaitem
);
772 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
776 sitem
->seen_quote
= sitem
->seen_lcont
= 0;
777 sitem
->type
= c_quote
;
778 sitem
->seen_quote
= 1;
779 par
.type
= para_QuotePush
;
780 prev_para_type
= par
.type
;
783 stk_push(crossparastk
, sitem
);
785 } else if (t
.type
== tok_rbrace
) {
786 struct crossparaitem
*sitem
= stk_pop(crossparastk
);
788 error(err_unexbrace
, &t
.pos
);
790 switch (sitem
->type
) {
792 par
.type
= para_LcontPop
;
793 prev_para_type
= par
.type
;
797 par
.type
= para_QuotePop
;
798 prev_para_type
= par
.type
;
808 * This token begins a paragraph. See if it's one of the
809 * special commands that define a paragraph type.
811 * (note that \# is special in a way, and \nocite takes no
814 par
.type
= para_Normal
;
815 if (t
.type
== tok_cmd
) {
817 int is_macro
= FALSE
;
825 error(err_badparatype
, t
.text
, &t
.pos
);
830 break; /* `\#{': isn't a comment para */
832 dtor(t
), t
= get_token(in
);
833 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
834 continue; /* next paragraph */
838 * 1 -- exactly one keyword
839 * 2 -- at least one keyword
840 * 4 -- any number of keywords including zero
841 * 8 -- at least one keyword and then nothing else
842 * 16 -- nothing at all! no keywords, no body
843 * 32 -- no keywords at all
845 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
846 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
847 case c_BR
: needkw
= 1; par
.type
= para_BR
;
848 start_cmd
= c_BR
; break;
849 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
850 case c_H
: needkw
= 2; par
.type
= para_Heading
;
853 case c_IM
: needkw
= 2; par
.type
= para_IM
;
854 start_cmd
= c_IM
; break;
855 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
856 par
.aux
= t
.aux
; break;
857 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
858 /* For \b and \n the keyword is optional */
859 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
860 case c_dt
: needkw
= 4; par
.type
= para_DescribedThing
; break;
861 case c_dd
: needkw
= 4; par
.type
= para_Description
; break;
862 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
863 case c_cfg
: needkw
= 8; par
.type
= para_Config
;
864 start_cmd
= c_cfg
; break;
865 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
866 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
867 /* For \nocite the keyword is _everything_ */
868 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
869 case c_preamble
: needkw
= 32; par
.type
= para_Normal
; break;
870 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
871 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
872 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
875 if (par
.type
== para_Chapter
||
876 par
.type
== para_Heading
||
877 par
.type
== para_Subsect
||
878 par
.type
== para_Appendix
||
879 par
.type
== para_UnnumberedChapter
) {
880 struct crossparaitem
*sitem
= stk_top(crossparastk
);
881 if (sitem
&& (sitem
->seen_lcont
|| sitem
->seen_quote
)) {
882 error(err_sectmarkerinblock
,
884 (sitem
->seen_lcont ?
"lcont" : "quote"));
889 rdstring rs
= { 0, 0, NULL
};
890 rdstringc rsc
= { 0, 0, NULL
};
895 dtor(t
), t
= get_token(in
);
897 while (t
.type
== tok_lbrace
) {
898 /* This is a keyword. */
900 /* FIXME: there will be bugs if anyone specifies an
901 * empty keyword (\foo{}), so trap this case. */
902 while (dtor(t
), t
= get_token(in
),
903 t
.type
== tok_word
||
904 t
.type
== tok_white
||
905 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
906 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) ||
907 (t
.type
== tok_cmd
&& t
.cmd
== c_u
)) {
908 if (t
.type
== tok_white
||
909 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
)) {
912 } else if (t
.type
== tok_cmd
&& t
.cmd
== c_u
) {
915 rdaddsc(&rsc
, t
.origtext
);
918 rdaddsc(&rsc
, t
.origtext
);
921 if (t
.type
!= tok_rbrace
) {
922 error(err_kwunclosed
, &t
.pos
);
925 rdadd(&rs
, 0); /* add string terminator */
926 rdaddc(&rsc
, 0); /* add string terminator */
927 dtor(t
), t
= get_token(in
); /* eat right brace */
930 rdadd(&rs
, 0); /* add string terminator */
931 rdaddc(&rsc
, 0); /* add string terminator */
933 /* See whether we have the right number of keywords. */
934 if ((needkw
& 48) && nkeys
> 0)
935 error(err_kwillegal
, &fp
);
936 if ((needkw
& 11) && nkeys
== 0)
937 error(err_kwexpected
, &fp
);
938 if ((needkw
& 5) && nkeys
> 1)
939 error(err_kwtoomany
, &fp
);
943 * Macro definition. Get the rest of the line
944 * as a code-paragraph token, repeatedly until
945 * there's nothing more left of it. Separate
948 rdstring macrotext
= { 0, 0, NULL
};
950 dtor(t
), t
= get_codepar_token(in
);
951 if (macrotext
.pos
> 0)
952 rdadd(¯otext
, L
'\n');
953 rdadds(¯otext
, t
.text
);
954 dtor(t
), t
= get_token(in
);
955 if (t
.type
== tok_eop
) break;
957 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
958 continue; /* next paragraph */
961 par
.keyword
= rdtrim(&rs
);
962 par
.origkeyword
= rdtrimc(&rsc
);
964 /* Move to EOP in case of needkw==8 or 16 (no body) */
966 /* We allow whitespace even when we expect no para body */
967 while (t
.type
== tok_white
)
968 dtor(t
), t
= get_token(in
);
969 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
970 (start_cmd
== c__invalid
||
971 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
)) {
972 error(err_bodyillegal
, &t
.pos
);
973 /* Error recovery: eat the rest of the paragraph */
974 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
975 (start_cmd
== c__invalid
||
976 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
))
977 dtor(t
), t
= get_token(in
);
979 if (t
.type
== tok_cmd
)
980 already
= TRUE
;/* inhibit get_token at top of loop */
981 prev_para_type
= par
.type
;
984 if (par
.type
== para_Config
) {
985 input_configure(in
, &par
);
987 continue; /* next paragraph */
993 * Now read the actual paragraph, word by word, adding to
994 * the paragraph list.
996 * Mid-paragraph commands:
1008 parsestk
= stk_new();
1009 style
= word_Normal
;
1010 spcstyle
= word_WhiteSpace
;
1013 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
1017 /* Handle implicit paragraph breaks after \IM, \BR etc */
1018 if (start_cmd
!= c__invalid
&&
1019 t
.type
== tok_cmd
&& t
.cmd
== start_cmd
) {
1020 already
= TRUE
; /* inhibit get_token at top of loop */
1024 if (t
.type
== tok_cmd
&& t
.cmd
== c__nop
) {
1025 dtor(t
), t
= get_token(in
);
1026 continue; /* do nothing! */
1029 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
1030 t
.type
= tok_word
; /* nice and simple */
1031 t
.aux
= 0; /* even if `\-' - nonbreaking! */
1033 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
1034 t
.type
= tok_word
; /* nice and simple */
1036 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
1037 t
.aux
= 0; /* (nonbreaking) */
1041 if (whptr
== &par
.words
)
1042 break; /* strip whitespace at start of para */
1051 * Inhibit use of whitespace if it's (probably the
1052 * newline) before a repeat \IM / \BR type
1055 if (start_cmd
!= c__invalid
) {
1056 dtor(t
), t
= get_token(in
);
1058 if (t
.type
== tok_cmd
&& t
.cmd
== start_cmd
)
1063 rdadd(&indexstr
, ' ');
1064 if (!indexing
|| index_visible
)
1065 addword(wd
, &whptr
);
1067 addword(wd
, &idximplicit
);
1072 rdadds(&indexstr
, t
.text
);
1078 if (!indexing
|| index_visible
) {
1079 wd
.text
= ustrdup(t
.text
);
1080 addword(wd
, &whptr
);
1083 wd
.text
= ustrdup(t
.text
);
1084 addword(wd
, &idximplicit
);
1088 error(err_unexbrace
, &t
.pos
);
1089 /* Error recovery: push nop */
1090 sitem
= snew(struct stack_item
);
1091 sitem
->type
= stack_nop
;
1092 sitem
->fpos
= t
.pos
;
1093 stk_push(parsestk
, sitem
);
1096 sitem
= stk_pop(parsestk
);
1099 * This closing brace could have been an
1100 * indication that the cross-paragraph stack
1101 * wants popping. Accordingly, we treat it here
1102 * as an indication that the paragraph is over.
1107 if (sitem
->type
& stack_ualt
) {
1108 whptr
= sitem
->whptr
;
1109 idximplicit
= sitem
->idximplicit
;
1111 if (sitem
->type
& stack_style
) {
1112 style
= word_Normal
;
1113 spcstyle
= word_WhiteSpace
;
1115 if (sitem
->type
& stack_idx
) {
1116 indexword
->text
= ustrdup(indexstr
.text
);
1117 if (index_downcase
) {
1120 ustrlow(indexword
->text
);
1121 ustrlow(indexstr
.text
);
1123 for (w
= idxwordlist
; w
; w
= w
->next
)
1128 rdadd(&indexstr
, L
'\0');
1129 index_merge(idx
, FALSE
, indexstr
.text
,
1130 idxwordlist
, &sitem
->fpos
);
1131 sfree(indexstr
.text
);
1133 if (sitem
->type
& stack_hyper
) {
1135 wd
.type
= word_HyperEnd
;
1140 if (!indexing
|| index_visible
)
1141 addword(wd
, &whptr
);
1143 addword(wd
, &idximplicit
);
1145 if (sitem
->type
& stack_quote
) {
1147 wd
.type
= toquotestyle(style
);
1149 wd
.aux
= quote_Close
;
1152 if (!indexing
|| index_visible
)
1153 addword(wd
, &whptr
);
1155 rdadd(&indexstr
, L
'"');
1156 addword(wd
, &idximplicit
);
1166 * In-paragraph comment: \#{ balanced braces }
1168 * Anything goes here; even tok_eop. We should
1169 * eat whitespace after the close brace _if_
1170 * there was whitespace before the \#.
1172 dtor(t
), t
= get_token(in
);
1173 if (t
.type
!= tok_lbrace
) {
1174 error(err_explbr
, &t
.pos
);
1177 while (braces
> 0) {
1178 dtor(t
), t
= get_token(in
);
1179 if (t
.type
== tok_lbrace
)
1181 else if (t
.type
== tok_rbrace
)
1183 else if (t
.type
== tok_eof
) {
1184 error(err_commenteof
, &t
.pos
);
1191 dtor(t
), t
= get_token(in
);
1192 if (t
.type
== tok_white
) {
1199 dtor(t
), t
= get_token(in
);
1200 if (t
.type
!= tok_lbrace
) {
1201 error(err_explbr
, &t
.pos
);
1204 * Enforce that \q may not be used anywhere
1205 * within \c. (It shouldn't be necessary
1206 * since the whole point of \c should be
1207 * that the user wants to exercise exact
1208 * control over the glyphs used, and
1209 * forbidding it has the useful effect of
1210 * relieving some backends of having to
1211 * make difficult decisions.)
1215 if (style
!= word_Code
&& style
!= word_WeakCode
) {
1217 wd
.type
= toquotestyle(style
);
1219 wd
.aux
= quote_Open
;
1222 if (!indexing
|| index_visible
)
1223 addword(wd
, &whptr
);
1225 rdadd(&indexstr
, L
'"');
1226 addword(wd
, &idximplicit
);
1228 stype
= stack_quote
;
1230 error(err_codequote
, &t
.pos
);
1233 sitem
= snew(struct stack_item
);
1234 sitem
->fpos
= t
.pos
;
1235 sitem
->type
= stype
;
1236 stk_push(parsestk
, sitem
);
1244 * Keyword, hyperlink, or \date. We expect a
1245 * left brace, some text, and then a right
1246 * brace. No nesting; no arguments.
1251 wd
.type
= word_UpperXref
;
1252 else if (t
.cmd
== c_k
)
1253 wd
.type
= word_LowerXref
;
1254 else if (t
.cmd
== c_W
)
1255 wd
.type
= word_HyperLink
;
1257 wd
.type
= word_Normal
;
1258 dtor(t
), t
= get_token(in
);
1259 if (t
.type
!= tok_lbrace
) {
1260 if (wd
.type
== word_Normal
) {
1261 time_t thetime
= time(NULL
);
1262 struct tm
*broken
= localtime(&thetime
);
1264 wdtext
= ustrftime(NULL
, broken
);
1267 error(err_explbr
, &t
.pos
);
1271 rdstring rs
= { 0, 0, NULL
};
1272 while (dtor(t
), t
= get_token(in
),
1273 t
.type
== tok_word
|| t
.type
== tok_white
) {
1274 if (t
.type
== tok_white
)
1277 rdadds(&rs
, t
.text
);
1279 if (wd
.type
== word_Normal
) {
1280 time_t thetime
= time(NULL
);
1281 struct tm
*broken
= localtime(&thetime
);
1282 wdtext
= ustrftime(rs
.text
, broken
);
1285 wdtext
= ustrdup(rs
.text
);
1288 if (t
.type
!= tok_rbrace
) {
1289 error(err_kwexprbr
, &t
.pos
);
1294 if (!indexing
|| index_visible
) {
1295 wd
.text
= ustrdup(wdtext
);
1296 addword(wd
, &whptr
);
1299 wd
.text
= ustrdup(wdtext
);
1300 addword(wd
, &idximplicit
);
1303 if (wd
.type
== word_HyperLink
) {
1305 * Hyperlinks are different: they then
1306 * expect another left brace, to begin
1307 * delimiting the text marked by the link.
1309 dtor(t
), t
= get_token(in
);
1310 sitem
= snew(struct stack_item
);
1311 sitem
->fpos
= wd
.fpos
;
1312 sitem
->type
= stack_hyper
;
1314 * Special cases: \W{}\i, \W{}\ii
1316 if (t
.type
== tok_cmd
&&
1317 (t
.cmd
== c_i
|| t
.cmd
== c_ii
)) {
1319 error(err_nestedindex
, &t
.pos
);
1321 /* Add an index-reference word with no
1323 wd
.type
= word_IndexRef
;
1328 indexword
= addword(wd
, &whptr
);
1329 /* Set up a rdstring to read the
1332 /* Flags so that we do the Right
1333 * Things with text */
1334 index_visible
= (type
!= c_I
);
1335 index_downcase
= (type
== c_ii
);
1338 idximplicit
= &idxwordlist
;
1340 sitem
->type
|= stack_idx
;
1342 dtor(t
), t
= get_token(in
);
1345 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1347 if (t
.type
== tok_cmd
&&
1348 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1349 if (style
!= word_Normal
)
1350 error(err_nestedstyles
, &t
.pos
);
1352 style
= (t
.cmd
== c_c ? word_Code
:
1353 t
.cmd
== c_cw ? word_WeakCode
:
1355 spcstyle
= tospacestyle(style
);
1356 sitem
->type
|= stack_style
;
1358 dtor(t
), t
= get_token(in
);
1360 if (t
.type
!= tok_lbrace
) {
1361 error(err_explbr
, &t
.pos
);
1364 stk_push(parsestk
, sitem
);
1372 if (style
!= word_Normal
) {
1373 error(err_nestedstyles
, &t
.pos
);
1374 /* Error recovery: eat lbrace, push nop. */
1375 dtor(t
), t
= get_token(in
);
1376 sitem
= snew(struct stack_item
);
1377 sitem
->fpos
= t
.pos
;
1378 sitem
->type
= stack_nop
;
1379 stk_push(parsestk
, sitem
);
1381 dtor(t
), t
= get_token(in
);
1382 if (t
.type
!= tok_lbrace
) {
1383 error(err_explbr
, &t
.pos
);
1385 style
= (type
== c_c ? word_Code
:
1386 type
== c_cw ? word_WeakCode
:
1388 spcstyle
= tospacestyle(style
);
1389 sitem
= snew(struct stack_item
);
1390 sitem
->fpos
= t
.pos
;
1391 sitem
->type
= stack_style
;
1392 stk_push(parsestk
, sitem
);
1400 error(err_nestedindex
, &t
.pos
);
1401 /* Error recovery: eat lbrace, push nop. */
1402 dtor(t
), t
= get_token(in
);
1403 sitem
= snew(struct stack_item
);
1404 sitem
->fpos
= t
.pos
;
1405 sitem
->type
= stack_nop
;
1406 stk_push(parsestk
, sitem
);
1408 sitem
= snew(struct stack_item
);
1409 sitem
->fpos
= t
.pos
;
1410 sitem
->type
= stack_idx
;
1411 dtor(t
), t
= get_token(in
);
1413 * Special cases: \i\c, \i\e, \i\cw
1416 if (t
.type
== tok_cmd
&&
1417 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1418 if (style
!= word_Normal
)
1419 error(err_nestedstyles
, &t
.pos
);
1421 style
= (t
.cmd
== c_c ? word_Code
:
1422 t
.cmd
== c_cw ? word_WeakCode
:
1424 spcstyle
= tospacestyle(style
);
1425 sitem
->type
|= stack_style
;
1427 dtor(t
), t
= get_token(in
);
1429 if (t
.type
!= tok_lbrace
) {
1431 error(err_explbr
, &t
.pos
);
1433 /* Add an index-reference word with no text as yet */
1434 wd
.type
= word_IndexRef
;
1439 indexword
= addword(wd
, &whptr
);
1440 /* Set up a rdstring to read the index text */
1442 /* Flags so that we do the Right Things with text */
1443 index_visible
= (type
!= c_I
);
1444 index_downcase
= (type
== c_ii
);
1447 idximplicit
= &idxwordlist
;
1448 /* Stack item to close the indexing on exit */
1449 stk_push(parsestk
, sitem
);
1454 utext
[0] = uchr
; utext
[1] = 0;
1460 if (!indexing
|| index_visible
) {
1461 wd
.text
= ustrdup(utext
);
1462 uword
= addword(wd
, &whptr
);
1466 wd
.text
= ustrdup(utext
);
1467 iword
= addword(wd
, &idximplicit
);
1470 dtor(t
), t
= get_token(in
);
1471 if (t
.type
== tok_lbrace
) {
1473 * \u with a left brace. Until the brace
1474 * closes, all further words go on a
1475 * sidetrack from the main thread of the
1478 sitem
= snew(struct stack_item
);
1479 sitem
->fpos
= t
.pos
;
1480 sitem
->type
= stack_ualt
;
1481 sitem
->whptr
= whptr
;
1482 sitem
->idximplicit
= idximplicit
;
1483 stk_push(parsestk
, sitem
);
1484 whptr
= uword ?
&uword
->alt
: NULL
;
1485 idximplicit
= iword ?
&iword
->alt
: NULL
;
1488 rdadd(&indexstr
, uchr
);
1493 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1494 error(err_badmidcmd
, t
.text
, &t
.pos
);
1499 dtor(t
), t
= get_token(in
);
1500 seenwhite
= iswhite
;
1503 /* Check the stack is empty */
1504 if (stk_top(parsestk
)) {
1505 while ((sitem
= stk_pop(parsestk
)))
1507 error(err_missingrbrace
, &t
.pos
);
1510 prev_para_type
= par
.type
;
1512 if (t
.type
== tok_eof
)
1516 if (stk_top(crossparastk
)) {
1519 error(err_missingrbrace2
, &t
.pos
);
1520 while ((p
= stk_pop(crossparastk
)))
1525 * We break to here rather than returning, because otherwise
1526 * this cleanup doesn't happen.
1529 macrocleanup(macros
);
1531 stk_free(crossparastk
);
1534 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1535 paragraph
*head
= NULL
;
1536 paragraph
**hptr
= &head
;
1538 while (in
->currindex
< in
->nfiles
) {
1539 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1541 setpos(in
, in
->filenames
[in
->currindex
]);
1542 in
->charset
= in
->defcharset
;
1543 in
->csstate
= charset_init_state
;
1544 in
->wcpos
= in
->nwc
= 0;
1545 in
->pushback_chars
= NULL
;
1546 read_file(&hptr
, in
, idx
);