| 1 |
% patgen.ch for C compilation with web2c. |
| 2 |
% |
| 3 |
% 07/01/83 (HWT) Original version, made to work with patgen released with |
| 4 |
% version 0.99 of TeX in July 1983. It may not work |
| 5 |
% properly---it is hard to test without more information. |
| 6 |
% 03/23/88 (ETM) Brought up to date, converted for use with WEB to C. |
| 7 |
|
| 8 |
@x [0] Extend program title. |
| 9 |
\def\title{PATGEN} |
| 10 |
@y |
| 11 |
\def\title{PATGEN changes for C} |
| 12 |
@z |
| 13 |
|
| 14 |
@x [0] WEAVE: print changes only |
| 15 |
\pageno=\contentspagenumber \advance\pageno by 1 |
| 16 |
@y |
| 17 |
\pageno=\contentspagenumber \advance\pageno by 1 |
| 18 |
\let\maybe=\iffalse |
| 19 |
@z |
| 20 |
|
| 21 |
@x [1] Define my_name |
| 22 |
@d banner=='This is PATGEN, Version 2.4' {printed when the program starts} |
| 23 |
@y |
| 24 |
@d my_name=='patgen' |
| 25 |
@d banner=='This is PATGEN, Version 2.4' {printed when the program starts} |
| 26 |
@z |
| 27 |
|
| 28 |
@x [3] Terminal I/O, Need standard input. |
| 29 |
@d get_input(#)==read(input,#) |
| 30 |
@d get_input_ln(#)== |
| 31 |
begin if eoln(input) then read_ln(input); |
| 32 |
read(input,#); |
| 33 |
end |
| 34 |
@# |
| 35 |
@y |
| 36 |
@d get_input(#)==#:=input_int(std_input) |
| 37 |
@d get_input_ln(#)==begin #:=getc(std_input); read_ln(std_input); end |
| 38 |
@# |
| 39 |
@d std_input==stdin |
| 40 |
@z |
| 41 |
|
| 42 |
@x [3] Eliminate the |end_of_PATGEN| label. |
| 43 |
@d end_of_PATGEN=9999 |
| 44 |
@y |
| 45 |
@z |
| 46 |
@x [3] |
| 47 |
label end_of_PATGEN; |
| 48 |
@y |
| 49 |
@z |
| 50 |
|
| 51 |
@x [3] Add file opening to initialization |
| 52 |
procedure initialize; {this procedure gets things started properly} |
| 53 |
var @<Local variables for initialization@>@/ |
| 54 |
begin print_ln(banner);@/ |
| 55 |
@y |
| 56 |
@<Define \(|parse_arguments|@> |
| 57 |
procedure initialize; {this procedure gets things started properly} |
| 58 |
var @<Local variables for initialization@>@/ |
| 59 |
begin |
| 60 |
kpse_set_program_name (argv[0], my_name); |
| 61 |
parse_arguments; |
| 62 |
print (banner); |
| 63 |
print_ln (version_string); |
| 64 |
@z |
| 65 |
|
| 66 |
@x [10] Purge 'jump_out' and 'end_of_PATGEN'. |
| 67 |
error message about what caused the error. Such errors might be |
| 68 |
discovered inside of subroutines inside of subroutines, so a \.{WEB} |
| 69 |
macro called |jump_out| has been introduced. This macro, which transfers |
| 70 |
control to the label |end_of_PATGEN| at the end of the program, contains |
| 71 |
the only non-local |@!goto| statement in \.{PATGEN}. Some \PASCAL\ |
| 72 |
compilers do not implement non-local |goto| statements. In such cases |
| 73 |
the |goto end_of_PATGEN| in the definition of |jump_out| should simply |
| 74 |
be replaced by a call on some system procedure that quietly terminates |
| 75 |
the program. |
| 76 |
@y |
| 77 |
error message about what caused the error. |
| 78 |
@z |
| 79 |
@x [10] Error handling |
| 80 |
@d jump_out==goto end_of_PATGEN {terminates \.{PATGEN}} |
| 81 |
@# |
| 82 |
@d error(#)==begin print_ln(#); jump_out; end |
| 83 |
@y |
| 84 |
@d error(#)==begin write_ln(stderr, #); uexit(1); end; |
| 85 |
@z |
| 86 |
|
| 87 |
@x [12] Fix signed char problem |
| 88 |
@!text_char=char; {the data type of characters in text files} |
| 89 |
@!ASCII_code=0..last_ASCII_code; {internal representation of input characters} |
| 90 |
@y |
| 91 |
@!ASCII_code=0..last_ASCII_code; {internal representation of input characters} |
| 92 |
@!text_char=ASCII_code; {the data type of characters in text files} |
| 93 |
@z |
| 94 |
|
| 95 |
% Much larger values requested from Keno Wehr, 17 May 2019 16:03:00, |
| 96 |
% but reduced (76000000/38000000) due to lack of memory on other |
| 97 |
% machines, from Johannes Hielscher, 10 Jul 2019 00:00:03 (tex-live list), |
| 98 |
% and later from Mojca Miklavec, 23 Sep 2019 21:21:42. |
| 99 |
% It seems hopeless, so went back to the original values (10000000/500000). |
| 100 |
% |
| 101 |
% The real solution is to provide a way to allocate the arrays |
| 102 |
% dynamically, so that the large arrays can be used by those who need |
| 103 |
% them but other are not affected. |
| 104 |
% |
| 105 |
% If the values here are still too big, you can probably get it to work |
| 106 |
% by adding swap or zram; or write a patch to allocate the arrays dynamically. |
| 107 |
@x [27] |
| 108 |
@!trie_size=55000; {space for pattern trie} |
| 109 |
@!triec_size=26000; {space for pattern count trie, must be less than |
| 110 |
|trie_size| and greater than the number of occurrences of any pattern in |
| 111 |
the dictionary} |
| 112 |
@y |
| 113 |
@!trie_size=10000000; {space for pattern trie} |
| 114 |
@!triec_size=5000000; {space for pattern count trie, must be less than |
| 115 |
|trie_size| and greater than the number of occurrences of any pattern in |
| 116 |
the dictionary} |
| 117 |
@z |
| 118 |
@x [27] |
| 119 |
@!max_buf_len=80; {maximum length of input lines, must be at least |max_len|} |
| 120 |
@y |
| 121 |
@!max_buf_len=3000; {maximum length of input lines, must be at least |max_len|} |
| 122 |
@z |
| 123 |
|
| 124 |
@x [51] Close both input and output files. |
| 125 |
@d close_out(#)==close(#) {close an output file} |
| 126 |
@d close_in(#)==do_nothing {close an input file} |
| 127 |
@y |
| 128 |
@d close_out(#)==xfclose(#, 'outputfile') {close an output file} |
| 129 |
@d close_in(#)==xfclose(#, 'inputfile') {close an input file} |
| 130 |
@z |
| 131 |
|
| 132 |
@x [51] Add f_name declaration, and temporaries for efficiency printing. |
| 133 |
@!dictionary, @!patterns, @!translate, @!patout, @!pattmp: text_file; |
| 134 |
@y |
| 135 |
@!dictionary, @!patterns, @!translate, @!patout, @!pattmp: text_file; |
| 136 |
@!f_name: ^char; |
| 137 |
@!bad_frac, @!denom, @!eff: real; |
| 138 |
@z |
| 139 |
|
| 140 |
@x [54] Get translate filename from command line. |
| 141 |
reset(translate); |
| 142 |
@y |
| 143 |
f_name := cmdline (4); |
| 144 |
reset (translate, f_name); |
| 145 |
@z |
| 146 |
|
| 147 |
@x [57] Input kludge. |
| 148 |
repeat print('left_hyphen_min, right_hyphen_min: '); get_input(n1,n2);@/ |
| 149 |
@y |
| 150 |
repeat print('left_hyphen_min, right_hyphen_min: '); input_2ints(n1,n2);@/ |
| 151 |
@z |
| 152 |
|
| 153 |
@x [67] Floating point output kludge for Web2c. |
| 154 |
print_ln(', efficiency = ', |
| 155 |
good_count/(good_pat_count+bad_count/bad_eff):1:2) |
| 156 |
@y |
| 157 |
begin |
| 158 |
print(', efficiency = '); |
| 159 |
print_real(good_count/(good_pat_count+bad_count/bad_eff),1,2); |
| 160 |
write_ln(output); |
| 161 |
end |
| 162 |
@z |
| 163 |
|
| 164 |
@x [88] Get dictionary filename from command line. |
| 165 |
reset(dictionary);@/ |
| 166 |
@y |
| 167 |
f_name := cmdline(1); |
| 168 |
reset (dictionary, f_name); |
| 169 |
@z |
| 170 |
|
| 171 |
% Fix file name initialization, since can't assign a constant string |
| 172 |
% that we're going to write into. |
| 173 |
@x [88] |
| 174 |
begin filnam:='pattmp. '; |
| 175 |
filnam[8]:=xdig[hyph_level]; |
| 176 |
@y |
| 177 |
begin strcpy (filnam, 'pattmp. '); |
| 178 |
filnam[7]:=xdig[hyph_level]; |
| 179 |
@z |
| 180 |
|
| 181 |
@x [88] Work around floating point I/O deficiency; reorder to avoid overflow. |
| 182 |
if (good_count+miss_count)>0 then |
| 183 |
print_ln((100*good_count/(good_count+miss_count)):1:2,' %, ', |
| 184 |
(100*bad_count/(good_count+miss_count)):1:2,' %, ', |
| 185 |
(100*miss_count/(good_count+miss_count)):1:2,' %'); |
| 186 |
@y |
| 187 |
if (good_count+miss_count)>0 then |
| 188 |
begin print_real((100*(good_count/(good_count+miss_count))),1,2); |
| 189 |
print(' %, '); |
| 190 |
print_real((100*(bad_count/(good_count+miss_count))),1,2); |
| 191 |
print(' %, '); |
| 192 |
print_real((100*(miss_count/(good_count+miss_count))),1,2); |
| 193 |
print_ln(' %'); |
| 194 |
end; |
| 195 |
@z |
| 196 |
|
| 197 |
@x [90] Get pattern filename from command line. |
| 198 |
reset(patterns); |
| 199 |
@y |
| 200 |
f_name := cmdline (2); |
| 201 |
reset (patterns, f_name); |
| 202 |
@z |
| 203 |
|
| 204 |
@x [94] Fix reading of multiple variables in the same line |
| 205 |
repeat print('hyph_start, hyph_finish: '); get_input(n1,n2);@/ |
| 206 |
@y |
| 207 |
repeat print('hyph_start, hyph_finish: '); input_2ints(n1,n2);@/ |
| 208 |
@z |
| 209 |
@x [94] |
| 210 |
repeat print('pat_start, pat_finish: '); get_input(n1,n2);@/ |
| 211 |
@y |
| 212 |
repeat print('pat_start, pat_finish: '); input_2ints(n1,n2);@/ |
| 213 |
@z |
| 214 |
@x [94] |
| 215 |
get_input(n1,n2,n3);@/ |
| 216 |
@y |
| 217 |
input_3ints(n1,n2,n3);@/ |
| 218 |
@z |
| 219 |
|
| 220 |
@x [94] Get output file name from command line. |
| 221 |
rewrite(patout); |
| 222 |
@y |
| 223 |
f_name := cmdline (3); |
| 224 |
rewrite (patout, f_name); |
| 225 |
@z |
| 226 |
|
| 227 |
@x [94] Eliminate the |end_of_PATGEN| label. |
| 228 |
end_of_PATGEN: |
| 229 |
@y |
| 230 |
@z |
| 231 |
|
| 232 |
@x [98] System-dependent changes. |
| 233 |
This section should be replaced, if necessary, by changes to the program |
| 234 |
that are necessary to make \.{PATGEN} work at a particular installation. |
| 235 |
It is usually best to design your change file so that all changes to |
| 236 |
previous sections preserve the section numbering; then everybody's version |
| 237 |
will be consistent with the printed program. More extensive changes, |
| 238 |
which introduce new sections, can be inserted here; then only the index |
| 239 |
itself will get a new section number. |
| 240 |
@^system dependencies@> |
| 241 |
@y |
| 242 |
Parse a Unix-style command line. |
| 243 |
|
| 244 |
@d argument_is (#) == (strcmp (long_options[option_index].name, #) = 0) |
| 245 |
|
| 246 |
@<Define \(|parse_arguments|@> = |
| 247 |
procedure parse_arguments; |
| 248 |
const n_options = 2; {Pascal won't count array lengths for us.} |
| 249 |
var @!long_options: array[0..n_options] of getopt_struct; |
| 250 |
@!getopt_return_val: integer; |
| 251 |
@!option_index: c_int_type; |
| 252 |
@!current_option: 0..n_options; |
| 253 |
begin |
| 254 |
@<Define the option table@>; |
| 255 |
repeat |
| 256 |
getopt_return_val := getopt_long_only (argc, argv, '', long_options, |
| 257 |
address_of (option_index)); |
| 258 |
if getopt_return_val = -1 then begin |
| 259 |
do_nothing; |
| 260 |
|
| 261 |
end else if getopt_return_val = '?' then begin |
| 262 |
usage (my_name); |
| 263 |
|
| 264 |
end else if argument_is ('help') then begin |
| 265 |
usage_help (PATGEN_HELP, nil); |
| 266 |
|
| 267 |
end else if argument_is ('version') then begin |
| 268 |
print_version_and_exit (banner, nil, |
| 269 |
'Frank M. Liang and Peter Breitenlohner', |
| 270 |
nil); |
| 271 |
|
| 272 |
end; {Else it was just a flag; |getopt| has already done the assignment.} |
| 273 |
until getopt_return_val = -1; |
| 274 |
|
| 275 |
{Now |optind| is the index of first non-option on the command line.} |
| 276 |
if (optind + 4 <> argc) then begin |
| 277 |
write_ln (stderr, my_name, ': Need exactly four arguments.'); |
| 278 |
usage (my_name); |
| 279 |
end; |
| 280 |
end; |
| 281 |
|
| 282 |
@ Here are the options we allow. The first is one of the standard GNU options. |
| 283 |
@.-help@> |
| 284 |
|
| 285 |
@<Define the option...@> = |
| 286 |
current_option := 0; |
| 287 |
long_options[current_option].name := 'help'; |
| 288 |
long_options[current_option].has_arg := 0; |
| 289 |
long_options[current_option].flag := 0; |
| 290 |
long_options[current_option].val := 0; |
| 291 |
incr (current_option); |
| 292 |
|
| 293 |
@ Another of the standard options. |
| 294 |
@.-version@> |
| 295 |
|
| 296 |
@<Define the option...@> = |
| 297 |
long_options[current_option].name := 'version'; |
| 298 |
long_options[current_option].has_arg := 0; |
| 299 |
long_options[current_option].flag := 0; |
| 300 |
long_options[current_option].val := 0; |
| 301 |
incr (current_option); |
| 302 |
|
| 303 |
@ An element with all zeros always ends the list. |
| 304 |
|
| 305 |
@<Define the option...@> = |
| 306 |
long_options[current_option].name := 0; |
| 307 |
long_options[current_option].has_arg := 0; |
| 308 |
long_options[current_option].flag := 0; |
| 309 |
long_options[current_option].val := 0; |
| 310 |
@z |