-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
393 lines (362 loc) · 8.03 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/* Copyright 1976, Bell Telephone Laboratories, Inc. */
/* from OpenSolaris "main.c 6.16 05/06/08 SMI" */
/*
* Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
*
* Sccsid @(#)main.c 1.9 (gritter) 11/26/05
*/
#include <wchar.h>
#include <string.h>
#include "once.h"
#include "sgs.h"
#include <locale.h>
#include <limits.h>
#include <unistd.h>
#include "nceucform.h"
#include "ncform.h"
#ifdef WITH_RATFOR
#include "nrform.h"
#endif
#define LZ_DECOMPRESSOR
#define LZ_UNSAFE
#include "lzcomp.h"
static char* decomp(const unsigned char *in,
unsigned inlen, unsigned outlen) {
char *buf = myalloc(1, outlen);
lz_uncompress(in, buf, inlen);
return buf;
}
#define DECOMP(X) decomp((X).data, (X).clen, (X).ulen)
#ifdef WHOLE_PROGRAM
#include "sub1.c"
#include "sub2.c"
#include "sub3.c"
#include "header.c"
#include "parser.c"
#include "lsearch.c"
#endif
static wchar_t L_INITIAL[] = {'I', 'N', 'I', 'T', 'I', 'A', 'L', 0};
static void get1core(void);
static void free1core(void);
static void get2core(void);
static void free2core(void);
static void get3core(void);
#ifdef DEBUG
static void free3core(void);
#endif
int
main(int argc, char **argv)
{
int i;
int c;
Boolean eoption = 0, woption = 0;
const char *driver = 0;
sargv = argv;
sargc = argc;
errorf = stderr;
setlocale(LC_CTYPE, "");
#ifdef DEBUG
while ((c = getopt(argc, argv, "dyctvnewVQ:o:")) != EOF) {
#else
while ((c = getopt(argc, argv, "ctvnewVQ:o:")) != EOF) {
#endif
switch (c) {
#ifdef DEBUG
case 'd':
debug++;
break;
case 'y':
yydebug = TRUE;
break;
#endif
case 'V':
fprintf(stderr, "lex:%s, %s\n", pkg, rel);
return 0;
case 'Q':
v_stmp = optarg;
if (*v_stmp != 'y' && *v_stmp != 'n')
error(
"lex: -Q should be followed by [y/n]");
break;
case 'o':
fout = fopen(optarg, "w");
if (!fout)
error(
"lex: could not open %s for writing",
optarg);
break;
case 'c':
#ifdef WITH_RATFOR
ratfor = FALSE;
#endif
break;
case 't':
fout = stdout;
break;
case 'v':
report = 1;
break;
case 'n':
report = 0;
break;
case 'w':
case 'W':
woption = 1;
handleeuc = 1;
widecio = 1;
break;
case 'e':
case 'E':
eoption = 1;
handleeuc = 1;
widecio = 0;
break;
default:
fprintf(stderr,
"Usage: lex [-ewctvnV] [-o outfile] [-Q(y/n)] [file]\n");
exit(1);
}
}
if (woption && eoption) {
error(
"You may not specify both -w and -e simultaneously.");
}
no_input = argc - optind;
if (no_input) {
/* XCU4: recognize "-" file operand for stdin */
if (strcmp(argv[optind], "-") == 0)
fin = stdin;
else {
fin = fopen(argv[optind], "r");
if (fin == NULL)
error(
"Can't open input file -- %s", argv[optind]);
}
} else
fin = stdin;
/* may be gotten: def, subs, sname, schar, ccl, dchar */
gch();
/* may be gotten: name, left, right, nullstr, parent */
get1core();
scopy(L_INITIAL, sp);
sname[0] = sp;
sp += slength(L_INITIAL) + 1;
sname[1] = 0;
/* XCU4: %x exclusive start */
exclusive[0] = 0;
if (!handleeuc) {
/*
* Set ZCH and ncg to their default values
* as they may be needed to handle %t directive.
*/
ZCH = ncg = NCH; /* ncg behaves as constant in this mode. */
}
/* may be disposed of: def, subs, dchar */
if (yyparse())
exit(1); /* error return code */
if (handleeuc) {
ncg = ncgidtbl * 2;
ZCH = ncg;
if (ncg >= MAXNCG)
error(
"Too complex rules -- requires too many char groups.");
sortcgidtbl();
}
repbycgid(); /* Call this even in ASCII compat. mode. */
/*
* maybe get:
* tmpstat, foll, positions, gotof, nexts,
* nchar, state, atable, sfall, cpackflg
*/
free1core();
get2core();
ptail();
mkmatch();
#ifdef DEBUG
if (debug)
pccl();
#endif
sect = ENDSECTION;
if (tptr > 0)
cfoll(tptr-1);
#ifdef DEBUG
if (debug)
pfoll();
#endif
cgoto();
#ifdef DEBUG
if (debug) {
printf("Print %d states:\n", stnum + 1);
for (i = 0; i <= stnum; i++)
stprt(i);
}
#endif
/*
* may be disposed of:
* positions, tmpstat, foll, state, name,
* left, right, parent, ccl, schar, sname
* maybe get: verify, advance, stoff
*/
free2core();
get3core();
layout();
/*
* may be disposed of:
* verify, advance, stoff, nexts, nchar,
* gotof, atable, ccpackflg, sfall
*/
#ifdef DEBUG
free3core();
#endif
if (handleeuc) {
#ifdef WITH_RATFOR
if (ratfor)
error("Ratfor is not supported by -w or -e option.");
#endif
driver = DECOMP(nceucform);
}
else
#ifdef WITH_RATFOR
driver = ratfor ? DECOMP(nrform) : DECOMP(ncform);
#else
driver = DECOMP(ncform);
#endif
fprintf(fout, "%s", driver);
fclose(fout);
if (report == 1)
statistics();
fclose(stdout);
fclose(stderr);
return (0); /* success return code */
}
static void
get1core(void)
{
ccptr = ccl = myalloc(CCLSIZE, sizeof (*ccl));
pcptr = pchar = myalloc(pchlen, sizeof (*pchar));
def = myalloc(DEFSIZE, sizeof (*def));
subs = myalloc(DEFSIZE, sizeof (*subs));
dp = dchar = myalloc(DEFCHAR, sizeof (*dchar));
sname = myalloc(STARTSIZE, sizeof (*sname));
/* XCU4: exclusive start array */
exclusive = myalloc(STARTSIZE, sizeof (*exclusive));
sp = schar = myalloc(STARTCHAR, sizeof (*schar));
if (ccl == 0 || def == 0 ||
pchar == 0 || subs == 0 || dchar == 0 ||
sname == 0 || exclusive == 0 || schar == 0)
error("Too little core to begin");
}
static void
free1core(void)
{
free(def);
free(subs);
free(dchar);
}
static void
get2core(void)
{
int i;
gotof = myalloc(nstates, sizeof (*gotof));
nexts = myalloc(ntrans, sizeof (*nexts));
nchar = myalloc(ntrans, sizeof (*nchar));
state = myalloc(nstates, sizeof (*state));
atable =myalloc(nstates, sizeof (*atable));
sfall = myalloc(nstates, sizeof (*sfall));
cpackflg = myalloc(nstates, sizeof (*cpackflg));
tmpstat = myalloc(tptr+1, sizeof (*tmpstat));
foll = myalloc(tptr+1, sizeof (*foll));
nxtpos = positions = myalloc(maxpos, sizeof (*positions));
if (tmpstat == 0 || foll == 0 || positions == 0 ||
gotof == 0 || nexts == 0 || nchar == 0 ||
state == 0 || atable == 0 || sfall == 0 || cpackflg == 0)
error("Too little core for state generation");
for (i = 0; i <= tptr; i++)
foll[i] = 0;
}
static void
free2core(void)
{
free(positions);
free(tmpstat);
free(foll);
free(name);
free(left);
free(right);
free(parent);
free(nullstr);
free(state);
free(sname);
/* XCU4: exclusive start array */
free(exclusive);
free(schar);
free(ccl);
}
static void
get3core(void)
{
verify = myalloc(outsize, sizeof (*verify));
advance = myalloc(outsize, sizeof (*advance));
stoff = myalloc(stnum+2, sizeof (*stoff));
if (verify == 0 || advance == 0 || stoff == 0)
error("Too little core for final packing");
}
#ifdef DEBUG
static void
free3core(void)
{
free(advance);
free(verify);
free(stoff);
free(gotof);
free(nexts);
free(nchar);
free(atable);
free(sfall);
free(cpackflg);
}
#endif
void *
myalloc(int a, int b)
{
void *i;
i = calloc(a, b);
if (i == NULL)
warning("calloc returns a 0");
return (i);
}
void
yyerror(char *s)
{
fprintf(stderr,
"\"%s\":line %d: Error: %s\n", sargv[optind], yyline, s);
}
#ifdef WHOLE_PROGRAM
#include "wcio.c"
#endif