2 * input.c: read the source form
10 #define TAB_STOP 8 /* for column number tracking */
12 static void setpos(input
*in
, char *fname
) {
13 in
->pos
.filename
= fname
;
15 in
->pos
.col
= (in
->reportcols ?
1 : -1);
18 static void unget(input
*in
, int c
, filepos
*pos
) {
19 if (in
->npushback
>= in
->pushbacksize
) {
20 in
->pushbacksize
= in
->npushback
+ 16;
21 in
->pushback
= resize(in
->pushback
, in
->pushbacksize
);
23 in
->pushback
[in
->npushback
].chr
= c
;
24 in
->pushback
[in
->npushback
].pos
= *pos
; /* structure copy */
28 /* ---------------------------------------------------------------------- */
32 typedef struct macro_Tag macro
;
36 struct macrostack_Tag
{
42 static int macrocmp(void *av
, void *bv
) {
43 macro
*a
= (macro
*)av
, *b
= (macro
*)bv
;
44 return ustrcmp(a
->name
, b
->name
);
46 static void macrodef(tree234
*macros
, wchar_t *name
, wchar_t *text
,
48 macro
*m
= mknew(macro
);
51 if (add234(macros
, m
) != m
) {
52 error(err_macroexists
, &fpos
, name
);
57 static int macrolookup(tree234
*macros
, input
*in
, wchar_t *name
,
61 gotit
= find234(macros
, &m
, NULL
);
63 macrostack
*expansion
= mknew(macrostack
);
64 expansion
->next
= in
->stack
;
65 expansion
->text
= gotit
->text
;
66 expansion
->pos
= *pos
; /* structure copy */
68 expansion
->npushback
= in
->npushback
;
69 in
->stack
= expansion
;
74 static void macrocleanup(tree234
*macros
) {
77 for (ti
= 0; (m
= (macro
*)index234(macros
, ti
)) != NULL
; ti
++) {
88 static int get(input
*in
, filepos
*pos
) {
89 int pushbackpt
= in
->stack ? in
->stack
->npushback
: 0;
90 if (in
->npushback
> pushbackpt
) {
93 *pos
= in
->pushback
[in
->npushback
].pos
; /* structure copy */
94 return in
->pushback
[in
->npushback
].chr
;
97 wchar_t c
= in
->stack
->text
[in
->stack
->ptr
];
98 if (in
->stack
->text
[++in
->stack
->ptr
] == L
'\0') {
99 macrostack
*tmp
= in
->stack
;
100 in
->stack
= tmp
->next
;
105 else if (in
->currfp
) {
106 int c
= getc(in
->currfp
);
112 /* Track line numbers, for error reporting */
115 if (in
->reportcols
) {
118 in
->pos
.col
= 1 + (in
->pos
.col
+ TAB_STOP
-1) % TAB_STOP
;
133 /* FIXME: do input charmap translation. We should be returning
141 * Lexical analysis of source files.
143 typedef struct token_Tag token
;
151 tok_eof
, /* end of file */
152 tok_eop
, /* end of paragraph */
153 tok_white
, /* whitespace */
154 tok_word
, /* a word or word fragment */
155 tok_cmd
, /* \command */
160 /* Halibut command keywords. */
162 c__invalid
, /* invalid command */
163 c__comment
, /* comment command (\#) */
164 c__escaped
, /* escaped character */
165 c__nbsp
, /* nonbreaking space */
166 c_A
, /* appendix heading */
167 c_B
, /* bibliography entry */
168 c_BR
, /* bibliography rewrite */
169 c_C
, /* chapter heading */
171 c_I
, /* invisible index mark */
172 c_IM
, /* index merge/rewrite */
173 c_K
, /* capitalised cross-reference */
174 c_S
, /* aux field is 0, 1, 2, ... */
175 c_U
, /* unnumbered-chapter heading */
176 c_W
, /* Web hyperlink */
177 c_b
, /* bulletted list */
179 c_cfg
, /* configuration directive */
180 c_copyright
, /* copyright statement */
181 c_cw
, /* weak code */
182 c_date
, /* document processing date */
183 c_dd
, /* description list: description */
184 c_define
, /* macro definition */
185 c_dt
, /* description list: described thing */
187 c_i
, /* visible index mark */
188 c_ii
, /* uncapitalised visible index mark */
189 c_k
, /* uncapitalised cross-reference */
190 c_lcont
, /* continuation para(s) for list item */
191 c_n
, /* numbered list */
192 c_nocite
, /* bibliography trickery */
193 c_preamble
, /* document preamble text */
194 c_q
, /* quote marks */
195 c_rule
, /* horizontal rule */
196 c_title
, /* document title */
197 c_u
, /* aux field is char code */
198 c_versionid
/* document RCS id */
201 /* Perhaps whitespace should be defined in a more Unicode-friendly way? */
202 #define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 )
203 #define isnl(c) ( (c)==10 )
204 #define isdec(c) ( ((c)>='0'&&(c)<='9') )
205 #define fromdec(c) ( (c)-'0' )
206 #define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f'))
207 #define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) )
208 #define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
211 * Keyword comparison function. Like strcmp, but between a wchar_t *
214 static int kwcmp(wchar_t const *p
, char const *q
) {
218 } while (*p
++ && *q
++ && !i
);
225 static void match_kw(token
*tok
) {
227 * FIXME. The ids are explicit in here so as to allow long-name
228 * equivalents to the various very short keywords.
230 static const struct { char const *name
; int id
; } keywords
[] = {
231 {"#", c__comment
}, /* comment command (\#) */
232 {"-", c__escaped
}, /* nonbreaking hyphen */
233 {"A", c_A
}, /* appendix heading */
234 {"B", c_B
}, /* bibliography entry */
235 {"BR", c_BR
}, /* bibliography rewrite */
236 {"C", c_C
}, /* chapter heading */
237 {"H", c_H
}, /* heading */
238 {"I", c_I
}, /* invisible index mark */
239 {"IM", c_IM
}, /* index merge/rewrite */
240 {"K", c_K
}, /* capitalised cross-reference */
241 {"U", c_U
}, /* unnumbered-chapter heading */
242 {"W", c_W
}, /* Web hyperlink */
243 {"\\", c__escaped
}, /* escaped backslash (\\) */
244 {"_", c__nbsp
}, /* nonbreaking space (\_) */
245 {"b", c_b
}, /* bulletted list */
246 {"c", c_c
}, /* code */
247 {"cfg", c_cfg
}, /* configuration directive */
248 {"copyright", c_copyright
}, /* copyright statement */
249 {"cw", c_cw
}, /* weak code */
250 {"date", c_date
}, /* document processing date */
251 {"dd", c_dd
}, /* description list: description */
252 {"define", c_define
}, /* macro definition */
253 {"dt", c_dt
}, /* description list: described thing */
254 {"e", c_e
}, /* emphasis */
255 {"i", c_i
}, /* visible index mark */
256 {"ii", c_ii
}, /* uncapitalised visible index mark */
257 {"k", c_k
}, /* uncapitalised cross-reference */
258 {"lcont", c_lcont
}, /* continuation para(s) for list item */
259 {"n", c_n
}, /* numbered list */
260 {"nocite", c_nocite
}, /* bibliography trickery */
261 {"preamble", c_preamble
}, /* document preamble text */
262 {"q", c_q
}, /* quote marks */
263 {"rule", c_rule
}, /* horizontal rule */
264 {"title", c_title
}, /* document title */
265 {"versionid", c_versionid
}, /* document RCS id */
266 {"{", c__escaped
}, /* escaped lbrace (\{) */
267 {"}", c__escaped
}, /* escaped rbrace (\}) */
272 * Special cases: \S{0,1,2,...} and \uABCD. If the syntax
273 * doesn't match correctly, we just fall through to the
274 * binary-search phase.
276 if (tok
->text
[0] == 'S') {
277 /* We expect numeric characters thereafter. */
278 wchar_t *p
= tok
->text
+1;
284 while (*p
&& isdec(*p
)) {
285 n
= 10 * n
+ fromdec(*p
);
294 } else if (tok
->text
[0] == 'u') {
295 /* We expect hex characters thereafter. */
296 wchar_t *p
= tok
->text
+1;
298 while (*p
&& ishex(*p
)) {
299 n
= 16 * n
+ fromhex(*p
);
310 j
= sizeof(keywords
)/sizeof(*keywords
);
313 c
= kwcmp(tok
->text
, keywords
[k
].name
);
319 tok
->cmd
= keywords
[k
].id
;
324 tok
->cmd
= c__invalid
;
329 * Read a token from the input file, in the normal way (`normal' in
330 * the sense that code paragraphs work a different way).
332 token
get_token(input
*in
) {
336 rdstring rs
= { 0, 0, NULL
};
339 ret
.text
= NULL
; /* default */
342 if (iswhite(c
)) { /* tok_white or tok_eop */
347 } while ((c
= get(in
, &cpos
)) != EOF
&& iswhite(c
));
353 ret
.type
= (nls
> 1 ? tok_eop
: tok_white
);
355 } else if (c
== EOF
) { /* tok_eof */
358 } else if (c
== '\\') { /* tok_cmd */
360 if (c
== '-' || c
== '\\' || c
== '_' ||
361 c
== '#' || c
== '{' || c
== '}') {
362 /* single-char command */
364 } else if (c
== 'u') {
370 } while (ishex(c
) && len
< 5);
372 } else if (iscmd(c
)) {
380 * Now match the command against the list of available
384 ret
.text
= ustrdup(rs
.text
);
388 } else if (c
== '{') { /* tok_lbrace */
389 ret
.type
= tok_lbrace
;
391 } else if (c
== '}') { /* tok_rbrace */
392 ret
.type
= tok_rbrace
;
394 } else { /* tok_word */
396 * Read a word: the longest possible contiguous sequence of
397 * things other than whitespace, backslash, braces and
398 * hyphen. A hyphen terminates the word but is returned as
399 * part of it; everything else is pushed back for the next
400 * token. The `aux' field contains TRUE if the word ends in
403 ret
.aux
= FALSE
; /* assumed for now */
405 if (iswhite(c
) || c
=='{' || c
=='}' || c
=='\\' || c
==EOF
) {
406 /* Put back the character that caused termination */
413 break; /* hyphen terminates word */
419 ret
.text
= ustrdup(rs
.text
);
426 * Determine whether the next input character is an open brace (for
427 * telling code paragraphs from paragraphs which merely start with
430 int isbrace(input
*in
) {
440 * Read the rest of a line that starts `\c'. Including nothing at
441 * all (tok_word with empty text).
443 token
get_codepar_token(input
*in
) {
446 rdstring rs
= { 0, 0, NULL
};
450 c
= get(in
, &cpos
); /* expect (and discard) one space */
456 while (!isnl(c
) && c
!= EOF
) {
459 /* Discard \r just before \n. */
460 if (c2
!= 13 || !isnl(c
))
464 ret
.text
= ustrdup(rs
.text
);
470 * Adds a new word to a linked list
472 static word
*addword(word newword
, word
***hptrptr
) {
476 mnewword
= mknew(word
);
477 *mnewword
= newword
; /* structure copy */
478 mnewword
->next
= NULL
;
479 **hptrptr
= mnewword
;
480 *hptrptr
= &mnewword
->next
;
485 * Adds a new paragraph to a linked list
487 static paragraph
*addpara(paragraph newpara
, paragraph
***hptrptr
) {
488 paragraph
*mnewpara
= mknew(paragraph
);
489 *mnewpara
= newpara
; /* structure copy */
490 mnewpara
->next
= NULL
;
491 **hptrptr
= mnewpara
;
492 *hptrptr
= &mnewpara
->next
;
497 * Destructor before token is reassigned; should catch most memory
500 #define dtor(t) ( sfree(t.text) )
503 * Reads a single file (ie until get() returns EOF)
505 static void read_file(paragraph
***ret
, input
*in
, indexdata
*idx
) {
508 word wd
, **whptr
, **idximplicit
;
510 wchar_t utext
[2], *wdtext
;
513 int iswhite
, seenwhite
;
518 stack_nop
= 0, /* do nothing (for error recovery) */
519 stack_ualt
= 1, /* \u alternative */
520 stack_style
= 2, /* \e, \c, \cw */
521 stack_idx
= 4, /* \I, \i, \ii */
522 stack_hyper
= 8, /* \W */
523 stack_quote
= 16, /* \q */
525 word
**whptr
; /* to restore from \u alternatives */
526 word
**idximplicit
; /* to restore from \u alternatives */
529 struct crossparaitem
{
530 int type
; /* currently c_lcont or -1 */
534 word
*indexword
, *uword
, *iword
;
537 int index_downcase
, index_visible
, indexing
;
538 const rdstring nullrs
= { 0, 0, NULL
};
542 macros
= newtree234(macrocmp
);
545 crossparastk
= stk_new();
548 * Loop on each paragraph.
551 int start_cmd
= c__invalid
;
561 dtor(t
), t
= get_token(in
);
564 } while (t
.type
== tok_eop
);
565 if (t
.type
== tok_eof
)
569 * Parse code paragraphs separately.
571 if (t
.type
== tok_cmd
&& t
.cmd
== c_c
&& !isbrace(in
)) {
572 int wtype
= word_WeakCode
;
574 par
.type
= para_Code
;
577 dtor(t
), t
= get_codepar_token(in
);
579 wd
.breaks
= FALSE
; /* shouldn't need this... */
580 wd
.text
= ustrdup(t
.text
);
584 dtor(t
), t
= get_token(in
);
585 if (t
.type
== tok_white
) {
587 * The newline after a code-paragraph line
589 dtor(t
), t
= get_token(in
);
591 if (t
.type
== tok_eop
|| t
.type
== tok_eof
)
593 else if (t
.type
== tok_cmd
&& t
.cmd
== c_c
)
594 wtype
= word_WeakCode
;
595 else if (t
.type
== tok_cmd
&& t
.cmd
== c_e
&&
596 wtype
== word_WeakCode
)
599 error(err_brokencodepara
, &t
.pos
);
600 prev_para_type
= par
.type
;
602 while (t
.type
!= tok_eop
) /* error recovery: */
603 dtor(t
), t
= get_token(in
); /* eat rest of paragraph */
604 goto codeparabroken
; /* ick, but such is life */
607 prev_para_type
= par
.type
;
614 * Spot the special commands that define a grouping of more
615 * than one paragraph, and also the closing braces that
618 if (t
.type
== tok_cmd
&&
620 struct crossparaitem
*sitem
, *stop
;
623 * Expect, and swallow, an open brace.
625 dtor(t
), t
= get_token(in
);
626 if (t
.type
!= tok_lbrace
) {
627 error(err_explbr
, &t
.pos
);
632 * \lcont causes a continuation of a list item into
633 * multiple paragraphs (which may in turn contain
634 * nested lists, code paras etc). Hence, the previous
635 * paragraph must be of a list type.
637 sitem
= mknew(struct crossparaitem
);
638 stop
= (struct crossparaitem
*)stk_top(crossparastk
);
639 if (prev_para_type
== para_Bullet
||
640 prev_para_type
== para_NumberedList
||
641 prev_para_type
== para_Description
) {
642 sitem
->type
= c_lcont
;
643 sitem
->seen_lcont
= 1;
644 par
.type
= para_LcontPush
;
645 prev_para_type
= par
.type
;
649 * Push a null item on the cross-para stack so that
650 * when we see the corresponding closing brace we
651 * don't give a cascade error.
654 sitem
->seen_lcont
= (stop ? stop
->seen_lcont
: 0);
655 error(err_misplacedlcont
, &t
.pos
);
657 stk_push(crossparastk
, sitem
);
659 } else if (t
.type
== tok_rbrace
) {
660 struct crossparaitem
*sitem
= stk_pop(crossparastk
);
662 error(err_unexbrace
, &t
.pos
);
664 switch (sitem
->type
) {
666 par
.type
= para_LcontPop
;
667 prev_para_type
= par
.type
;
677 * This token begins a paragraph. See if it's one of the
678 * special commands that define a paragraph type.
680 * (note that \# is special in a way, and \nocite takes no
683 par
.type
= para_Normal
;
684 if (t
.type
== tok_cmd
) {
686 int is_macro
= FALSE
;
694 error(err_badparatype
, t
.text
, &t
.pos
);
699 break; /* `\#{': isn't a comment para */
701 dtor(t
), t
= get_token(in
);
702 } while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
);
703 continue; /* next paragraph */
707 * 1 -- exactly one keyword
708 * 2 -- at least one keyword
709 * 4 -- any number of keywords including zero
710 * 8 -- at least one keyword and then nothing else
711 * 16 -- nothing at all! no keywords, no body
712 * 32 -- no keywords at all
714 case c_A
: needkw
= 2; par
.type
= para_Appendix
; break;
715 case c_B
: needkw
= 2; par
.type
= para_Biblio
; break;
716 case c_BR
: needkw
= 1; par
.type
= para_BR
;
717 start_cmd
= c_BR
; break;
718 case c_C
: needkw
= 2; par
.type
= para_Chapter
; break;
719 case c_H
: needkw
= 2; par
.type
= para_Heading
;
722 case c_IM
: needkw
= 2; par
.type
= para_IM
;
723 start_cmd
= c_IM
; break;
724 case c_S
: needkw
= 2; par
.type
= para_Subsect
;
725 par
.aux
= t
.aux
; break;
726 case c_U
: needkw
= 32; par
.type
= para_UnnumberedChapter
; break;
727 /* For \b and \n the keyword is optional */
728 case c_b
: needkw
= 4; par
.type
= para_Bullet
; break;
729 case c_dt
: needkw
= 4; par
.type
= para_DescribedThing
; break;
730 case c_dd
: needkw
= 4; par
.type
= para_Description
; break;
731 case c_n
: needkw
= 4; par
.type
= para_NumberedList
; break;
732 case c_cfg
: needkw
= 8; par
.type
= para_Config
;
733 start_cmd
= c_cfg
; break;
734 case c_copyright
: needkw
= 32; par
.type
= para_Copyright
; break;
735 case c_define
: is_macro
= TRUE
; needkw
= 1; break;
736 /* For \nocite the keyword is _everything_ */
737 case c_nocite
: needkw
= 8; par
.type
= para_NoCite
; break;
738 case c_preamble
: needkw
= 32; par
.type
= para_Preamble
; break;
739 case c_rule
: needkw
= 16; par
.type
= para_Rule
; break;
740 case c_title
: needkw
= 32; par
.type
= para_Title
; break;
741 case c_versionid
: needkw
= 32; par
.type
= para_VersionID
; break;
744 if (par
.type
== para_Chapter
||
745 par
.type
== para_Heading
||
746 par
.type
== para_Subsect
||
747 par
.type
== para_Appendix
||
748 par
.type
== para_UnnumberedChapter
) {
749 struct crossparaitem
*sitem
= stk_top(crossparastk
);
750 if (sitem
&& sitem
->seen_lcont
) {
751 error(err_sectmarkerinlcont
, &t
.pos
);
756 rdstring rs
= { 0, 0, NULL
};
761 dtor(t
), t
= get_token(in
);
763 while (t
.type
== tok_lbrace
) {
764 /* This is a keyword. */
766 /* FIXME: there will be bugs if anyone specifies an
767 * empty keyword (\foo{}), so trap this case. */
768 while (dtor(t
), t
= get_token(in
),
769 t
.type
== tok_word
||
770 t
.type
== tok_white
||
771 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) ||
772 (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
)) {
773 if (t
.type
== tok_white
||
774 (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
))
779 if (t
.type
!= tok_rbrace
) {
780 error(err_kwunclosed
, &t
.pos
);
783 rdadd(&rs
, 0); /* add string terminator */
784 dtor(t
), t
= get_token(in
); /* eat right brace */
787 rdadd(&rs
, 0); /* add string terminator */
789 /* See whether we have the right number of keywords. */
790 if ((needkw
& 48) && nkeys
> 0)
791 error(err_kwillegal
, &fp
);
792 if ((needkw
& 11) && nkeys
== 0)
793 error(err_kwexpected
, &fp
);
794 if ((needkw
& 5) && nkeys
> 1)
795 error(err_kwtoomany
, &fp
);
799 * Macro definition. Get the rest of the line
800 * as a code-paragraph token, repeatedly until
801 * there's nothing more left of it. Separate
804 rdstring macrotext
= { 0, 0, NULL
};
806 dtor(t
), t
= get_codepar_token(in
);
807 if (macrotext
.pos
> 0)
808 rdadd(¯otext
, L
'\n');
809 rdadds(¯otext
, t
.text
);
810 dtor(t
), t
= get_token(in
);
811 if (t
.type
== tok_eop
) break;
813 macrodef(macros
, rs
.text
, macrotext
.text
, fp
);
814 continue; /* next paragraph */
817 par
.keyword
= rdtrim(&rs
);
819 /* Move to EOP in case of needkw==8 or 16 (no body) */
821 /* We allow whitespace even when we expect no para body */
822 while (t
.type
== tok_white
)
823 dtor(t
), t
= get_token(in
);
824 if (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
825 (start_cmd
== c__invalid
||
826 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
)) {
827 error(err_bodyillegal
, &t
.pos
);
828 /* Error recovery: eat the rest of the paragraph */
829 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
&&
830 (start_cmd
== c__invalid
||
831 t
.type
!= tok_cmd
|| t
.cmd
!= start_cmd
))
832 dtor(t
), t
= get_token(in
);
834 if (t
.type
== tok_cmd
)
835 already
= TRUE
;/* inhibit get_token at top of loop */
836 prev_para_type
= par
.type
;
838 continue; /* next paragraph */
844 * Now read the actual paragraph, word by word, adding to
845 * the paragraph list.
847 * Mid-paragraph commands:
859 parsestk
= stk_new();
861 spcstyle
= word_WhiteSpace
;
864 while (t
.type
!= tok_eop
&& t
.type
!= tok_eof
) {
868 /* Handle implicit paragraph breaks after \IM, \BR etc */
869 if (start_cmd
!= c__invalid
&&
870 t
.type
== tok_cmd
&& t
.cmd
== start_cmd
) {
871 already
= TRUE
; /* inhibit get_token at top of loop */
875 if (t
.type
== tok_cmd
&& t
.cmd
== c__escaped
) {
876 t
.type
= tok_word
; /* nice and simple */
877 t
.aux
= 0; /* even if `\-' - nonbreaking! */
879 if (t
.type
== tok_cmd
&& t
.cmd
== c__nbsp
) {
880 t
.type
= tok_word
; /* nice and simple */
882 t
.text
= ustrdup(L
" "); /* text is ` ' not `_' */
883 t
.aux
= 0; /* (nonbreaking) */
887 if (whptr
== &par
.words
)
888 break; /* strip whitespace at start of para */
897 * Inhibit use of whitespace if it's (probably the
898 * newline) before a repeat \IM / \BR type
901 if (start_cmd
!= c__invalid
) {
902 dtor(t
), t
= get_token(in
);
904 if (t
.type
== tok_cmd
&& t
.cmd
== start_cmd
)
909 rdadd(&indexstr
, ' ');
910 if (!indexing
|| index_visible
)
913 addword(wd
, &idximplicit
);
918 rdadds(&indexstr
, t
.text
);
924 if (!indexing
|| index_visible
) {
925 wd
.text
= ustrdup(t
.text
);
929 wd
.text
= ustrdup(t
.text
);
930 addword(wd
, &idximplicit
);
934 error(err_unexbrace
, &t
.pos
);
935 /* Error recovery: push nop */
936 sitem
= mknew(struct stack_item
);
937 sitem
->type
= stack_nop
;
938 stk_push(parsestk
, sitem
);
941 sitem
= stk_pop(parsestk
);
944 * This closing brace could have been an
945 * indication that the cross-paragraph stack
946 * wants popping. Accordingly, we treat it here
947 * as an indication that the paragraph is over.
952 if (sitem
->type
& stack_ualt
) {
953 whptr
= sitem
->whptr
;
954 idximplicit
= sitem
->idximplicit
;
956 if (sitem
->type
& stack_style
) {
958 spcstyle
= word_WhiteSpace
;
960 if (sitem
->type
& stack_idx
) {
961 indexword
->text
= ustrdup(indexstr
.text
);
963 ustrlow(indexword
->text
);
965 rdadd(&indexstr
, L
'\0');
966 index_merge(idx
, FALSE
, indexstr
.text
, idxwordlist
);
967 sfree(indexstr
.text
);
969 if (sitem
->type
& stack_hyper
) {
971 wd
.type
= word_HyperEnd
;
976 if (!indexing
|| index_visible
)
979 addword(wd
, &idximplicit
);
981 if (sitem
->type
& stack_quote
) {
983 wd
.type
= toquotestyle(style
);
985 wd
.aux
= quote_Close
;
988 if (!indexing
|| index_visible
)
991 rdadd(&indexstr
, L
'"');
992 addword(wd
, &idximplicit
);
1002 * In-paragraph comment: \#{ balanced braces }
1004 * Anything goes here; even tok_eop. We should
1005 * eat whitespace after the close brace _if_
1006 * there was whitespace before the \#.
1008 dtor(t
), t
= get_token(in
);
1009 if (t
.type
!= tok_lbrace
) {
1010 error(err_explbr
, &t
.pos
);
1013 while (braces
> 0) {
1014 dtor(t
), t
= get_token(in
);
1015 if (t
.type
== tok_lbrace
)
1017 else if (t
.type
== tok_rbrace
)
1019 else if (t
.type
== tok_eof
) {
1020 error(err_commenteof
, &t
.pos
);
1027 dtor(t
), t
= get_token(in
);
1028 if (t
.type
== tok_white
) {
1035 dtor(t
), t
= get_token(in
);
1036 if (t
.type
!= tok_lbrace
) {
1037 error(err_explbr
, &t
.pos
);
1040 wd
.type
= toquotestyle(style
);
1042 wd
.aux
= quote_Open
;
1045 if (!indexing
|| index_visible
)
1046 addword(wd
, &whptr
);
1048 rdadd(&indexstr
, L
'"');
1049 addword(wd
, &idximplicit
);
1051 sitem
= mknew(struct stack_item
);
1052 sitem
->type
= stack_quote
;
1053 stk_push(parsestk
, sitem
);
1061 * Keyword, hyperlink, or \date. We expect a
1062 * left brace, some text, and then a right
1063 * brace. No nesting; no arguments.
1068 wd
.type
= word_UpperXref
;
1069 else if (t
.cmd
== c_k
)
1070 wd
.type
= word_LowerXref
;
1071 else if (t
.cmd
== c_W
)
1072 wd
.type
= word_HyperLink
;
1074 wd
.type
= word_Normal
;
1075 dtor(t
), t
= get_token(in
);
1076 if (t
.type
!= tok_lbrace
) {
1077 if (wd
.type
== word_Normal
) {
1078 time_t thetime
= time(NULL
);
1079 struct tm
*broken
= localtime(&thetime
);
1081 wdtext
= ustrftime(NULL
, broken
);
1084 error(err_explbr
, &t
.pos
);
1088 rdstring rs
= { 0, 0, NULL
};
1089 while (dtor(t
), t
= get_token(in
),
1090 t
.type
== tok_word
|| t
.type
== tok_white
) {
1091 if (t
.type
== tok_white
)
1094 rdadds(&rs
, t
.text
);
1096 if (wd
.type
== word_Normal
) {
1097 time_t thetime
= time(NULL
);
1098 struct tm
*broken
= localtime(&thetime
);
1099 wdtext
= ustrftime(rs
.text
, broken
);
1102 wdtext
= ustrdup(rs
.text
);
1105 if (t
.type
!= tok_rbrace
) {
1106 error(err_kwexprbr
, &t
.pos
);
1111 if (!indexing
|| index_visible
) {
1112 wd
.text
= ustrdup(wdtext
);
1113 addword(wd
, &whptr
);
1116 wd
.text
= ustrdup(wdtext
);
1117 addword(wd
, &idximplicit
);
1120 if (wd
.type
== word_HyperLink
) {
1122 * Hyperlinks are different: they then
1123 * expect another left brace, to begin
1124 * delimiting the text marked by the link.
1126 dtor(t
), t
= get_token(in
);
1128 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1130 sitem
= mknew(struct stack_item
);
1131 sitem
->type
= stack_hyper
;
1132 if (t
.type
== tok_cmd
&&
1133 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1134 if (style
!= word_Normal
)
1135 error(err_nestedstyles
, &t
.pos
);
1137 style
= (t
.cmd
== c_c ? word_Code
:
1138 t
.cmd
== c_cw ? word_WeakCode
:
1140 spcstyle
= tospacestyle(style
);
1141 sitem
->type
|= stack_style
;
1143 dtor(t
), t
= get_token(in
);
1145 if (t
.type
!= tok_lbrace
) {
1146 error(err_explbr
, &t
.pos
);
1149 stk_push(parsestk
, sitem
);
1157 if (style
!= word_Normal
) {
1158 error(err_nestedstyles
, &t
.pos
);
1159 /* Error recovery: eat lbrace, push nop. */
1160 dtor(t
), t
= get_token(in
);
1161 sitem
= mknew(struct stack_item
);
1162 sitem
->type
= stack_nop
;
1163 stk_push(parsestk
, sitem
);
1165 dtor(t
), t
= get_token(in
);
1166 if (t
.type
!= tok_lbrace
) {
1167 error(err_explbr
, &t
.pos
);
1169 style
= (type
== c_c ? word_Code
:
1170 type
== c_cw ? word_WeakCode
:
1172 spcstyle
= tospacestyle(style
);
1173 sitem
= mknew(struct stack_item
);
1174 sitem
->type
= stack_style
;
1175 stk_push(parsestk
, sitem
);
1183 error(err_nestedindex
, &t
.pos
);
1184 /* Error recovery: eat lbrace, push nop. */
1185 dtor(t
), t
= get_token(in
);
1186 sitem
= mknew(struct stack_item
);
1187 sitem
->type
= stack_nop
;
1188 stk_push(parsestk
, sitem
);
1190 sitem
= mknew(struct stack_item
);
1191 sitem
->type
= stack_idx
;
1192 dtor(t
), t
= get_token(in
);
1194 * Special cases: \i\c, \i\e, \i\cw
1197 if (t
.type
== tok_cmd
&&
1198 (t
.cmd
== c_e
|| t
.cmd
== c_c
|| t
.cmd
== c_cw
)) {
1199 if (style
!= word_Normal
)
1200 error(err_nestedstyles
, &t
.pos
);
1202 style
= (t
.cmd
== c_c ? word_Code
:
1203 t
.cmd
== c_cw ? word_WeakCode
:
1205 spcstyle
= tospacestyle(style
);
1206 sitem
->type
|= stack_style
;
1208 dtor(t
), t
= get_token(in
);
1210 if (t
.type
!= tok_lbrace
) {
1212 error(err_explbr
, &t
.pos
);
1214 /* Add an index-reference word with no text as yet */
1215 wd
.type
= word_IndexRef
;
1220 indexword
= addword(wd
, &whptr
);
1221 /* Set up a rdstring to read the index text */
1223 /* Flags so that we do the Right Things with text */
1224 index_visible
= (type
!= c_I
);
1225 index_downcase
= (type
== c_ii
);
1228 idximplicit
= &idxwordlist
;
1229 /* Stack item to close the indexing on exit */
1230 stk_push(parsestk
, sitem
);
1235 utext
[0] = uchr
; utext
[1] = 0;
1241 if (!indexing
|| index_visible
) {
1242 wd
.text
= ustrdup(utext
);
1243 uword
= addword(wd
, &whptr
);
1247 wd
.text
= ustrdup(utext
);
1248 iword
= addword(wd
, &idximplicit
);
1251 dtor(t
), t
= get_token(in
);
1252 if (t
.type
== tok_lbrace
) {
1254 * \u with a left brace. Until the brace
1255 * closes, all further words go on a
1256 * sidetrack from the main thread of the
1259 sitem
= mknew(struct stack_item
);
1260 sitem
->type
= stack_ualt
;
1261 sitem
->whptr
= whptr
;
1262 sitem
->idximplicit
= idximplicit
;
1263 stk_push(parsestk
, sitem
);
1264 whptr
= uword ?
&uword
->alt
: NULL
;
1265 idximplicit
= iword ?
&iword
->alt
: NULL
;
1268 rdadd(&indexstr
, uchr
);
1273 if (!macrolookup(macros
, in
, t
.text
, &t
.pos
))
1274 error(err_badmidcmd
, t
.text
, &t
.pos
);
1279 dtor(t
), t
= get_token(in
);
1280 seenwhite
= iswhite
;
1283 /* Check the stack is empty */
1284 if (stk_top(parsestk
)) {
1285 while ((sitem
= stk_pop(parsestk
)))
1287 error(err_missingrbrace
, &t
.pos
);
1290 prev_para_type
= par
.type
;
1292 if (t
.type
== tok_eof
)
1296 if (stk_top(crossparastk
)) {
1299 error(err_missingrbrace2
, &t
.pos
);
1300 while ((p
= stk_pop(crossparastk
)))
1305 * We break to here rather than returning, because otherwise
1306 * this cleanup doesn't happen.
1309 macrocleanup(macros
);
1311 stk_free(crossparastk
);
1314 paragraph
*read_input(input
*in
, indexdata
*idx
) {
1315 paragraph
*head
= NULL
;
1316 paragraph
**hptr
= &head
;
1318 while (in
->currindex
< in
->nfiles
) {
1319 in
->currfp
= fopen(in
->filenames
[in
->currindex
], "r");
1321 setpos(in
, in
->filenames
[in
->currindex
]);
1322 read_file(&hptr
, in
, idx
);