Debian Codegraph @

texk/web2c/cwebdir/common.h

raw
1 % This file is part of CWEB.
2 % This program by Silvio Levy and Donald E. Knuth
3 % is based on a program by Knuth.
4 % It is distributed WITHOUT ANY WARRANTY, express or implied.
5 % Version 4.12.2 --- July 2025
6
7 % Copyright (C) 1987,1990,1993 Silvio Levy and Donald E. Knuth
8
9 % Permission is granted to make and distribute verbatim copies of this
10 % document provided that the copyright notice and this permission notice
11 % are preserved on all copies.
12
13 % Permission is granted to copy and distribute modified versions of this
14 % document under the conditions for verbatim copying, provided that the
15 % entire resulting derived work is given a different name and distributed
16 % under the terms of a permission notice identical to this one.
17
18 % Amendments to 'common.h' resulting in this updated version were created
19 % by numerous collaborators over the course of many years.
20
21 % Please send comments, suggestions, etc. to tex-k@@tug.org.
22
23 % The next few sections contain stuff from the file |"common.w"| that has
24 % to be included in both |"ctangle.w"| and |"cweave.w"|. It appears in this
25 % file |"common.h"|, which is also included in |"common.w"| to propagate
26 % possible changes from this single source consistently.
27
28 First comes general stuff:
29
30 @i iso_types.w
31
32 @d ctangle false
33 @d cweave true
34
35 @<Common code...@>=@^system dependencies@>
36 typedef uint8_t eight_bits;
37 typedef uint16_t sixteen_bits;
38 extern bool program; /* \.{CWEAVE} or \.{CTANGLE}? */
39 extern int phase; /* which phase are we in? */
40
41 @ The procedure that gets everything rolling:
42 @<Predecl...@>=
43 extern void common_init(void);
44
45 @ Interface to the standard \CEE/ library:
46
47 @<Include files@>=
48 #include <ctype.h> /* definition of |@!isalpha|, |@!isdigit| and so on */
49 #include <stdbool.h> /* definition of |@!bool|, |@!true| and |@!false| */
50 #include <stddef.h> /* definition of |@!ptrdiff_t| */
51 #include <stdint.h> /* definition of |@!uint8_t| and |@!uint16_t| */
52 #include <stdio.h> /* definition of |@!printf| and friends */
53 #include <stdlib.h> /* definition of |@!getenv| and |@!exit| */
54 #include <string.h> /* definition of |@!strlen|, |@!strcmp| and so on */
55
56 @ Code related to the character set:
57 @^ASCII code dependencies@>
58
59 @d and_and 04 /* `\.{\&\&}'\,; corresponds to MIT's {\tentex\char'4}
60 and ASCII~\.{EOT} */
61 @d lt_lt 020 /* `\.{<<}'\,; corresponds to MIT's {\tentex\char'20}
62 and ASCII~\.{DLE} */
63 @d gt_gt 021 /* `\.{>>}'\,; corresponds to MIT's {\tentex\char'21}
64 and ASCII~\.{DC1} */
65 @d plus_plus 013 /* `\.{++}'\,; corresponds to MIT's {\tentex\char'13}
66 and ASCII~\.{VT} aka~|'\v'| */
67 @d minus_minus 01 /* `\.{--}'\,; corresponds to MIT's {\tentex\char'1}
68 and ASCII~\.{SOH} */
69 @d minus_gt 031 /* `\.{->}'\,; corresponds to MIT's {\tentex\char'31}
70 and ASCII~\.{EM} */
71 @d non_eq 032 /* `\.{!=}'\,; corresponds to MIT's {\tentex\char'32}
72 and ASCII~\.{SUB} */
73 @d lt_eq 034 /* `\.{<=}'\,; corresponds to MIT's {\tentex\char'34}
74 and ASCII~\.{FS} */
75 @d gt_eq 035 /* `\.{>=}'\,; corresponds to MIT's {\tentex\char'35}
76 and ASCII~\.{GS} */
77 @d eq_eq 036 /* `\.{==}'\,; corresponds to MIT's {\tentex\char'36}
78 and ASCII~\.{RS} */
79 @d or_or 037 /* `\.{\v\v}'\,; corresponds to MIT's {\tentex\char'37}
80 and ASCII~\.{US} */
81 @d dot_dot_dot 016 /* `\.{...}'\,; corresponds to MIT's {\tentex\char'16}
82 and ASCII~\.{SO} */
83 @d colon_colon 06 /* `\.{::}'\,; corresponds to MIT's {\tentex\char'6}
84 and ASCII~\.{ACK} */
85 @d period_ast 026 /* `\.{.*}'\,; corresponds to MIT's {\tentex\char'26}
86 and ASCII~\.{SYN} */
87 @d minus_gt_ast 027 /* `\.{->*}'\,; corresponds to MIT's {\tentex\char'27}
88 and ASCII~\.{ETB} */
89 @#
90 @d compress(c) if (loc++<=limit) return c
91
92 @<Common code...@>=
93 extern char section_text[]; /* text being sought for */
94 extern char *section_text_end; /* end of |section_text| */
95 extern char *id_first; /* where the current identifier begins in the buffer */
96 extern char *id_loc; /* just after the current identifier in the buffer */
97
98 @ Code related to input routines:
99 @d xisalpha(c) (isalpha((int)(c))&&!ishigh(c))
100 @d xisdigit(c) (isdigit((int)(c))&&!ishigh(c))
101 @d xisspace(c) (isspace((int)(c))&&!ishigh(c))
102 @d xislower(c) (islower((int)(c))&&!ishigh(c))
103 @d xisupper(c) (isupper((int)(c))&&!ishigh(c))
104 @d xisxdigit(c) (isxdigit((int)(c))&&!ishigh(c))
105 @d isxalpha(c) ((c)=='_' || (c)=='$')
106 /* non-alpha characters allowed in identifier */
107 @d ishigh(c) ((eight_bits)(c)>0177)
108 @^high-bit character handling@>
109
110 @<Common code...@>=
111 extern char buffer[]; /* where each line of input goes */
112 extern char *buffer_end; /* end of |buffer| */
113 extern char *loc; /* points to the next character to be read from the buffer */
114 extern char *limit; /* points to the last character in the buffer */
115
116 @ Code related to file handling:
117 @f line x /* make |line| an unreserved word */
118 @d max_include_depth 10 /* maximum number of source files open
119 simultaneously, not counting the change file */
120 @d max_file_name_length 60
121 @d cur_file file[include_depth] /* current file */
122 @d cur_file_name file_name[include_depth] /* current file name */
123 @d cur_line line[include_depth] /* number of current line in current file */
124 @d web_file file[0] /* main source file */
125 @d web_file_name file_name[0] /* main source file name */
126
127 @<Common code...@>=
128 extern int include_depth; /* current level of nesting */
129 extern FILE *file[]; /* stack of non-change files */
130 extern FILE *change_file; /* change file */
131 extern char file_name[][max_file_name_length];
132 /* stack of non-change file names */
133 extern char change_file_name[]; /* name of change file */
134 extern int line[]; /* number of current line in the stacked files */
135 extern int change_line; /* number of current line in change file */
136 extern int change_depth; /* where \.{@@y} originated during a change */
137 extern bool input_has_ended; /* if there is no more input */
138 extern bool changing; /* if the current line is from |change_file| */
139 extern bool web_file_open; /* if the web file is being read */
140
141 @ @<Predecl...@>=
142 extern bool get_line(void); /* inputs the next line */
143 extern void check_complete(void); /* checks that all changes were picked up */
144 extern void reset_input(void); /* initialize to read the web file and change file */
145
146 @ Code related to section numbers:
147 @<Common code...@>=
148 extern sixteen_bits section_count; /* the current section number */
149 extern bool changed_section[]; /* is the section changed? */
150 extern bool change_pending; /* is a decision about change still unclear? */
151 extern bool print_where; /* tells \.{CTANGLE} to print line and file info */
152
153 @ Code related to identifier and section name storage:
154 @d length(c) (size_t)((c+1)->byte_start-(c)->byte_start) /* the length of a name */
155 @d print_id(c) term_write((c)->byte_start,length(c)) /* print identifier */
156 @d llink link /* left link in binary search tree for section names */
157 @d rlink dummy.Rlink /* right link in binary search tree for section names */
158 @d root name_dir->rlink /* the root of the binary search tree
159 for section names */
160 @d ilk dummy.Ilk /* used by \.{CWEAVE} only */
161
162 @<Common code...@>=
163 typedef struct name_info {
164 char *byte_start; /* beginning of the name in |byte_mem| */
165 struct name_info *link;
166 union {
167 struct name_info *Rlink; /* right link in binary search tree for section
168 names */
169 eight_bits Ilk; /* used by identifiers in \.{CWEAVE} only */
170 } dummy;
171 void *equiv_or_xref; /* info corresponding to names */
172 } name_info; /* contains information about an identifier or section name */
173 typedef name_info *name_pointer; /* pointer into array of \&{name\_info}s */
174 typedef name_pointer *hash_pointer;
175 extern char byte_mem[]; /* characters of names */
176 extern char *byte_mem_end; /* end of |byte_mem| */
177 extern char *byte_ptr; /* first unused position in |byte_mem| */
178 extern name_info name_dir[]; /* information about names */
179 extern name_pointer name_dir_end; /* end of |name_dir| */
180 extern name_pointer name_ptr; /* first unused position in |name_dir| */
181 extern name_pointer hash[]; /* heads of hash lists */
182 extern hash_pointer hash_end; /* end of |hash| */
183 extern hash_pointer hash_ptr; /* index into hash-head array */
184
185 @ @<Predecl...@>=
186 extern name_pointer id_lookup(const char *,const char *,eight_bits);
187 /* looks up a string in the identifier table */
188 extern name_pointer section_lookup(char *,char *,bool); /* finds section name */
189 extern void print_prefix_name(name_pointer);@/
190 extern void print_section_name(name_pointer);@/
191 extern void sprint_section_name(char *,name_pointer);
192 @#
193 extern bool names_match(name_pointer,const char *,size_t,eight_bits);
194 /* two routines defined in \.{ctangle.w} and \.{cweave.w} */
195 extern void init_node(name_pointer);
196
197 @ Code related to error handling:
198 @d spotless 0 /* |history| value for normal jobs */
199 @d harmless_message 1 /* |history| value when non-serious info was printed */
200 @d error_message 2 /* |history| value when an error was noted */
201 @d fatal_message 3 /* |history| value when we had to stop prematurely */
202 @d mark_harmless() if (history==spotless) history=harmless_message
203 @d mark_error() history=error_message
204 @d confusion(s) fatal("! This can't happen: ",s)
205 @.This can't happen@>
206
207 @<Common code...@>=
208 extern int history; /* indicates how bad this run was */
209
210 @ @<Predecl...@>=
211 extern int wrap_up(void); /* indicate |history| and exit */
212 extern void err_print(const char *); /* print error message and context */
213 extern void fatal(const char *,const char *); /* issue error message and die */
214 extern void overflow(const char *); /* succumb because a table has overflowed */
215 @#
216 extern void print_stats(void); /* defined in \.{ctangle.w} and \.{cweave.w} */
217
218 @ Code related to command line arguments:
219 @d show_banner flags['b'] /* should the banner line be printed? */
220 @d show_progress flags['p'] /* should progress reports be printed? */
221 @d show_happiness flags['h'] /* should lack of errors be announced? */
222 @d show_stats flags['s'] /* should statistics be printed at end of run? */
223 @d make_xrefs flags['x'] /* should cross references be output? */
224
225 @<Common code...@>=
226 extern int argc; /* copy of |ac| parameter to |main| */
227 extern char **argv; /* copy of |av| parameter to |main| */
228 extern char C_file_name[]; /* name of |C_file| */
229 extern char tex_file_name[]; /* name of |tex_file| */
230 extern char idx_file_name[]; /* name of |idx_file| */
231 extern char scn_file_name[]; /* name of |scn_file| */
232 extern bool flags[]; /* an option for each 7-bit code */
233
234 @ Code related to output:
235 @d update_terminal() fflush(stdout) /* empty the terminal output buffer */
236 @d new_line() putchar('\n')
237 @d term_write(a,b) fflush(stdout),fwrite(a,sizeof(char),b,stdout)
238
239 @<Common code...@>=
240 extern FILE *C_file; /* where output of \.{CTANGLE} goes */
241 extern FILE *tex_file; /* where output of \.{CWEAVE} goes */
242 extern FILE *idx_file; /* where index from \.{CWEAVE} goes */
243 extern FILE *scn_file; /* where list of sections from \.{CWEAVE} goes */
244 extern FILE *active_file; /* currently active file for \.{CWEAVE} output */
245
246 @ The following parameters are sufficient to handle \TEX/ (converted to
247 \.{CWEB}), so they should be sufficient for most applications of \.{CWEB}.
248
249 @d buf_size 200 /* maximum length of input line, plus one */
250 @d longest_name 10000 /* file names, section names, and section texts
251 shouldn't be longer than this */
252 @d long_buf_size (buf_size+longest_name) /* for \.{CWEAVE} */
253 @d max_bytes 100000 /* the number of bytes in identifiers,
254 index entries, and section names; must be less than $2^{24}$ */
255 @d max_names 5000 /* number of identifiers, strings, section names;
256 must be less than 10240 */
257 @d max_sections 2000 /* greater than the total number of sections */
258
259 @ End of \.{COMMON} interface.