| 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 |
@<Common code...@>=@^system dependencies@> |
| 33 |
typedef uint8_t eight_bits; |
| 34 |
typedef uint16_t sixteen_bits; |
| 35 |
typedef enum { |
| 36 |
@!ctangle, @!cweave, @!ctwill |
| 37 |
} cweb; |
| 38 |
extern cweb program; /* \.{CTANGLE} or \.{CWEAVE} or \.{CTWILL}? */ |
| 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 |
@ You may have noticed that almost all \.{"strings"} in the \.{CWEB} sources |
| 46 |
are placed in the context of the `|_|'~macro. This is just a shortcut for the |
| 47 |
`|@!gettext|' function from the ``GNU~gettext utilities.'' For systems that do |
| 48 |
not have this library installed, we wrap things for neutral behavior without |
| 49 |
internationalization. |
| 50 |
|
| 51 |
@d _(s) gettext(s) |
| 52 |
|
| 53 |
@<Include files@>= |
| 54 |
#include <ctype.h> /* definition of |@!isalpha|, |@!isdigit| and so on */ |
| 55 |
#include <stdbool.h> /* definition of |@!bool|, |@!true| and |@!false| */ |
| 56 |
#include <stddef.h> /* definition of |@!ptrdiff_t| */ |
| 57 |
#include <stdint.h> /* definition of |@!uint8_t| and |@!uint16_t| */ |
| 58 |
#include <stdio.h> /* definition of |@!printf| and friends */ |
| 59 |
#include <stdlib.h> /* definition of |@!getenv| and |@!exit| */ |
| 60 |
#include <string.h> /* definition of |@!strlen|, |@!strcmp| and so on */ |
| 61 |
@# |
| 62 |
#ifndef HAVE_GETTEXT |
| 63 |
#define HAVE_GETTEXT 0 |
| 64 |
#endif |
| 65 |
@# |
| 66 |
#if HAVE_GETTEXT |
| 67 |
#include <libintl.h> |
| 68 |
#else |
| 69 |
#define gettext(a) a |
| 70 |
#endif |
| 71 |
|
| 72 |
@ Code related to the character set: |
| 73 |
@^ASCII code dependencies@> |
| 74 |
|
| 75 |
@d and_and 04 /* `\.{\&\&}'\,; corresponds to MIT's {\tentex\char'4} |
| 76 |
and ASCII~\.{EOT} */ |
| 77 |
@d lt_lt 020 /* `\.{<<}'\,; corresponds to MIT's {\tentex\char'20} |
| 78 |
and ASCII~\.{DLE} */ |
| 79 |
@d gt_gt 021 /* `\.{>>}'\,; corresponds to MIT's {\tentex\char'21} |
| 80 |
and ASCII~\.{DC1} */ |
| 81 |
@d plus_plus 013 /* `\.{++}'\,; corresponds to MIT's {\tentex\char'13} |
| 82 |
and ASCII~\.{VT} aka~|'\v'| */ |
| 83 |
@d minus_minus 01 /* `\.{--}'\,; corresponds to MIT's {\tentex\char'1} |
| 84 |
and ASCII~\.{SOH} */ |
| 85 |
@d minus_gt 031 /* `\.{->}'\,; corresponds to MIT's {\tentex\char'31} |
| 86 |
and ASCII~\.{EM} */ |
| 87 |
@d non_eq 032 /* `\.{!=}'\,; corresponds to MIT's {\tentex\char'32} |
| 88 |
and ASCII~\.{SUB} */ |
| 89 |
@d lt_eq 034 /* `\.{<=}'\,; corresponds to MIT's {\tentex\char'34} |
| 90 |
and ASCII~\.{FS} */ |
| 91 |
@d gt_eq 035 /* `\.{>=}'\,; corresponds to MIT's {\tentex\char'35} |
| 92 |
and ASCII~\.{GS} */ |
| 93 |
@d eq_eq 036 /* `\.{==}'\,; corresponds to MIT's {\tentex\char'36} |
| 94 |
and ASCII~\.{RS} */ |
| 95 |
@d or_or 037 /* `\.{\v\v}'\,; corresponds to MIT's {\tentex\char'37} |
| 96 |
and ASCII~\.{US} */ |
| 97 |
@d dot_dot_dot 016 /* `\.{...}'\,; corresponds to MIT's {\tentex\char'16} |
| 98 |
and ASCII~\.{SO} */ |
| 99 |
@d colon_colon 06 /* `\.{::}'\,; corresponds to MIT's {\tentex\char'6} |
| 100 |
and ASCII~\.{ACK} */ |
| 101 |
@d period_ast 026 /* `\.{.*}'\,; corresponds to MIT's {\tentex\char'26} |
| 102 |
and ASCII~\.{SYN} */ |
| 103 |
@d minus_gt_ast 027 /* `\.{->*}'\,; corresponds to MIT's {\tentex\char'27} |
| 104 |
and ASCII~\.{ETB} */ |
| 105 |
@# |
| 106 |
@d compress(c) if (loc++<=limit) return c |
| 107 |
|
| 108 |
@<Common code...@>= |
| 109 |
extern char section_text[]; /* text being sought for */ |
| 110 |
extern char *section_text_end; /* end of |section_text| */ |
| 111 |
extern char *id_first; /* where the current identifier begins in the buffer */ |
| 112 |
extern char *id_loc; /* just after the current identifier in the buffer */ |
| 113 |
|
| 114 |
@ Code related to input routines: |
| 115 |
@d xisalpha(c) (isalpha((int)(c))&&!ishigh(c)) |
| 116 |
@d xisdigit(c) (isdigit((int)(c))&&!ishigh(c)) |
| 117 |
@d xisspace(c) (isspace((int)(c))&&!ishigh(c)) |
| 118 |
@d xislower(c) (islower((int)(c))&&!ishigh(c)) |
| 119 |
@d xisupper(c) (isupper((int)(c))&&!ishigh(c)) |
| 120 |
@d xisxdigit(c) (isxdigit((int)(c))&&!ishigh(c)) |
| 121 |
@d isxalpha(c) ((c)=='_' || (c)=='$') |
| 122 |
/* non-alpha characters allowed in identifier */ |
| 123 |
@d ishigh(c) ((eight_bits)(c)>0177) |
| 124 |
@^high-bit character handling@> |
| 125 |
|
| 126 |
@<Common code...@>= |
| 127 |
extern char buffer[]; /* where each line of input goes */ |
| 128 |
extern char *buffer_end; /* end of |buffer| */ |
| 129 |
extern char *loc; /* points to the next character to be read from the buffer */ |
| 130 |
extern char *limit; /* points to the last character in the buffer */ |
| 131 |
|
| 132 |
@ Code related to file handling: |
| 133 |
@f line x /* make |line| an unreserved word */ |
| 134 |
@d max_include_depth 10 /* maximum number of source files open |
| 135 |
simultaneously, not counting the change file */ |
| 136 |
@d max_file_name_length 1024 |
| 137 |
@d cur_file file[include_depth] /* current file */ |
| 138 |
@d cur_file_name file_name[include_depth] /* current file name */ |
| 139 |
@d cur_line line[include_depth] /* number of current line in current file */ |
| 140 |
@d web_file file[0] /* main source file */ |
| 141 |
@d web_file_name file_name[0] /* main source file name */ |
| 142 |
|
| 143 |
@<Common code...@>= |
| 144 |
extern int include_depth; /* current level of nesting */ |
| 145 |
extern FILE *file[]; /* stack of non-change files */ |
| 146 |
extern FILE *change_file; /* change file */ |
| 147 |
extern char file_name[][max_file_name_length]; |
| 148 |
/* stack of non-change file names */ |
| 149 |
extern char change_file_name[]; /* name of change file */ |
| 150 |
extern char *found_filename; /* filename found by |kpse_find_file| */ |
| 151 |
extern int line[]; /* number of current line in the stacked files */ |
| 152 |
extern int change_line; /* number of current line in change file */ |
| 153 |
extern int change_depth; /* where \.{@@y} originated during a change */ |
| 154 |
extern bool input_has_ended; /* if there is no more input */ |
| 155 |
extern bool changing; /* if the current line is from |change_file| */ |
| 156 |
extern bool web_file_open; /* if the web file is being read */ |
| 157 |
|
| 158 |
@ @<Predecl...@>= |
| 159 |
extern bool get_line(void); /* inputs the next line */ |
| 160 |
extern void check_complete(void); /* checks that all changes were picked up */ |
| 161 |
extern void reset_input(void); /* initialize to read the web file and change file */ |
| 162 |
|
| 163 |
@ Code related to section numbers: |
| 164 |
@<Common code...@>= |
| 165 |
extern sixteen_bits section_count; /* the current section number */ |
| 166 |
extern bool changed_section[]; /* is the section changed? */ |
| 167 |
extern bool change_pending; /* is a decision about change still unclear? */ |
| 168 |
extern bool print_where; /* tells \.{CTANGLE} to print line and file info */ |
| 169 |
|
| 170 |
@ Code related to identifier and section name storage: |
| 171 |
@d length(c) (size_t)((c+1)->byte_start-(c)->byte_start) /* the length of a name */ |
| 172 |
@d print_id(c) term_write((c)->byte_start,length(c)) /* print identifier */ |
| 173 |
@d llink link /* left link in binary search tree for section names */ |
| 174 |
@d rlink dummy.Rlink /* right link in binary search tree for section names */ |
| 175 |
@d root name_dir->rlink /* the root of the binary search tree |
| 176 |
for section names */ |
| 177 |
@d ilk dummy.Ilk /* used by \.{CWEAVE} only */ |
| 178 |
|
| 179 |
@<Common code...@>= |
| 180 |
typedef struct name_info { |
| 181 |
char *byte_start; /* beginning of the name in |byte_mem| */ |
| 182 |
struct name_info *link; |
| 183 |
union { |
| 184 |
struct name_info *Rlink; /* right link in binary search tree for section |
| 185 |
names */ |
| 186 |
eight_bits Ilk; /* used by identifiers in \.{CWEAVE} only */ |
| 187 |
} dummy; |
| 188 |
void *equiv_or_xref; /* info corresponding to names */ |
| 189 |
} name_info; /* contains information about an identifier or section name */ |
| 190 |
typedef name_info *name_pointer; /* pointer into array of \&{name\_info}s */ |
| 191 |
typedef name_pointer *hash_pointer; |
| 192 |
extern char byte_mem[]; /* characters of names */ |
| 193 |
extern char *byte_mem_end; /* end of |byte_mem| */ |
| 194 |
extern char *byte_ptr; /* first unused position in |byte_mem| */ |
| 195 |
extern name_info name_dir[]; /* information about names */ |
| 196 |
extern name_pointer name_dir_end; /* end of |name_dir| */ |
| 197 |
extern name_pointer name_ptr; /* first unused position in |name_dir| */ |
| 198 |
extern name_pointer hash[]; /* heads of hash lists */ |
| 199 |
extern hash_pointer hash_end; /* end of |hash| */ |
| 200 |
extern hash_pointer hash_ptr; /* index into hash-head array */ |
| 201 |
|
| 202 |
@ @<Predecl...@>= |
| 203 |
extern name_pointer id_lookup(const char *,const char *,eight_bits); |
| 204 |
/* looks up a string in the identifier table */ |
| 205 |
extern name_pointer section_lookup(char *,char *,bool); /* finds section name */ |
| 206 |
extern void print_prefix_name(name_pointer);@/ |
| 207 |
extern void print_section_name(name_pointer);@/ |
| 208 |
extern void sprint_section_name(char *,name_pointer); |
| 209 |
@# |
| 210 |
extern bool names_match(name_pointer,const char *,size_t,eight_bits); |
| 211 |
/* two routines defined in \.{ctangle.w} and \.{cweave.w} */ |
| 212 |
extern void init_node(name_pointer); |
| 213 |
|
| 214 |
@ Code related to error handling: |
| 215 |
@d spotless 0 /* |history| value for normal jobs */ |
| 216 |
@d harmless_message 1 /* |history| value when non-serious info was printed */ |
| 217 |
@d error_message 2 /* |history| value when an error was noted */ |
| 218 |
@d fatal_message 3 /* |history| value when we had to stop prematurely */ |
| 219 |
@d mark_harmless() if (history==spotless) history=harmless_message |
| 220 |
@d mark_error() history=error_message |
| 221 |
@d confusion(s) fatal(_("! This can't happen: "),s) |
| 222 |
@.This can't happen@> |
| 223 |
|
| 224 |
@<Common code...@>= |
| 225 |
extern int history; /* indicates how bad this run was */ |
| 226 |
|
| 227 |
@ @<Predecl...@>= |
| 228 |
extern int wrap_up(void); /* indicate |history| and exit */ |
| 229 |
extern void err_print(const char *); /* print error message and context */ |
| 230 |
extern void fatal(const char *,const char *); /* issue error message and die */ |
| 231 |
extern void overflow(const char *); /* succumb because a table has overflowed */ |
| 232 |
@# |
| 233 |
extern void cb_show_banner(void); /* copy |banner| back to \.{common.w} */ |
| 234 |
@# |
| 235 |
extern void print_stats(void); /* defined in \.{ctangle.w} and \.{cweave.w} */ |
| 236 |
|
| 237 |
@ Code related to command line arguments: |
| 238 |
@d show_banner flags['b'] /* should the banner line be printed? */ |
| 239 |
@d show_progress flags['p'] /* should progress reports be printed? */ |
| 240 |
@d show_happiness flags['h'] /* should lack of errors be announced? */ |
| 241 |
@d show_stats flags['s'] /* should statistics be printed at end of run? */ |
| 242 |
@d make_xrefs flags['x'] /* should cross references be output? */ |
| 243 |
@d check_for_change flags['c'] /* check temporary output for changes */ |
| 244 |
|
| 245 |
@<Common code...@>= |
| 246 |
extern int argc; /* copy of |ac| parameter to |main| */ |
| 247 |
extern char **argv; /* copy of |av| parameter to |main| */ |
| 248 |
extern char C_file_name[]; /* name of |C_file| */ |
| 249 |
extern char tex_file_name[]; /* name of |tex_file| */ |
| 250 |
extern char idx_file_name[]; /* name of |idx_file| */ |
| 251 |
extern char scn_file_name[]; /* name of |scn_file| */ |
| 252 |
extern char check_file_name[]; /* name of |check_file| */ |
| 253 |
extern bool flags[]; /* an option for each 7-bit code */ |
| 254 |
extern const char *use_language; /* prefix to \.{cwebmac.tex} in \TEX/ output */ |
| 255 |
|
| 256 |
@ Code related to output: |
| 257 |
@d update_terminal() fflush(stdout) /* empty the terminal output buffer */ |
| 258 |
@d new_line() putchar('\n') |
| 259 |
@d term_write(a,b) fflush(stdout),fwrite(a,sizeof(char),b,stdout) |
| 260 |
|
| 261 |
@<Common code...@>= |
| 262 |
extern FILE *C_file; /* where output of \.{CTANGLE} goes */ |
| 263 |
extern FILE *tex_file; /* where output of \.{CWEAVE} goes */ |
| 264 |
extern FILE *idx_file; /* where index from \.{CWEAVE} goes */ |
| 265 |
extern FILE *scn_file; /* where list of sections from \.{CWEAVE} goes */ |
| 266 |
extern FILE *active_file; /* currently active file for \.{CWEAVE} output */ |
| 267 |
extern FILE *check_file; /* temporary output file */ |
| 268 |
|
| 269 |
@ The following parameters are sufficient to handle \TEX/ (converted to |
| 270 |
\.{CWEB}), so they should be sufficient for most applications of \.{CWEB}. |
| 271 |
|
| 272 |
@d buf_size 1000 /* maximum length of input line, plus one */ |
| 273 |
@d longest_name 10000 /* file names, section names, and section texts |
| 274 |
shouldn't be longer than this */ |
| 275 |
@d long_buf_size (buf_size+longest_name) /* for \.{CWEAVE} */ |
| 276 |
@d max_bytes 1000000 /* the number of bytes in identifiers, |
| 277 |
index entries, and section names; must be less than $2^{24}$ */ |
| 278 |
@d max_names 10239 /* number of identifiers, strings, section names; |
| 279 |
must be less than 10240 */ |
| 280 |
@d max_sections 4000 /* greater than the total number of sections */ |
| 281 |
|
| 282 |
@ End of \.{COMMON} interface. |