File: main.c

package info (click to toggle)
mmorph 2.3.4.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 912 kB
  • ctags: 904
  • sloc: ansic: 4,992; yacc: 1,215; lex: 417; makefile: 296; sh: 48; sed: 33; csh: 26
file content (418 lines) | stat: -rw-r--r-- 11,864 bytes parent folder | download | duplicates (5)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
    mmorph, MULTEXT morphology tool
    Version 2.3, October 1995
    Copyright (c) 1994,1995 ISSCO/SUISSETRA, Geneva, Switzerland
    Dominique Petitpierre, <petitp@divsun.unige.ch>
*/
/*
    main.c

*/

#include <locale.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "user.h"
#include "parse.h"
#include "y.tab.h"
#include "tokenstr.h"

static char rcsid[]
= "@(#)main.c,v 2.21 1995/11/01 11:47:43 petitp Exp";


extern char *optarg;
extern int  optind;
extern int  opterr;
extern void lookup();

static char *version_string = VERSION_STR;
static char *patch_version_string = PATCH_VERSION_STR;
char       *db_file_name;
char       *gram_file_name;
char       *augment_file_name;
int         trace_level = 0;
int         spell_trace_level = 0;
int         parsing = FALSE;
crc32file   crc;
unsigned char crc_hash[CRC32FILE_HASHLEN];

unsigned long debug = 0L;
static char *progname;
static char *fullprogname;

static char *db_suffix = ".db";
static char *tfs_suffix = ".tfs";
static FILE *infile;
t_boolean   want_segment_id;
t_boolean   want_flush;
t_boolean   parse_only;
t_boolean   normalize;
t_boolean   normalize_flag;
t_boolean   mark_unknown;
t_boolean   fold_case_always;
t_boolean   fold_case_fallback;
t_boolean   extend_morph_field;
t_boolean   overwrite_morph_field;
char       *begin_sentence_class;

void
yyerror(s)
char       *s;
{
    fatal_error("%s; lookahead is \"%s\" (token %s %d)",
		s, yytext,
		((yychar < FIRSTTOKEN || yychar > LASTTOKEN)
		 ? "code" : token[yychar - FIRSTTOKEN]),
		yychar
	);
}

static void
init()
{
    init_symbol();
    /* setlocale is useful for isprint() in output.c and fold_case() */
    (void) setlocale(LC_CTYPE, "");
/*
    if (setlocale(LC_CTYPE, "") == NULL)
	TODO: do something, locale will be probably default "C"
*/
}

static void
print_usage()
{
    print_log("Usage for %s:\n",
	      fullprogname);
    print_log("info:\t %s%s\n",
	      progname,
	      " [-vh]");
    print_log("parse:\t %s%s%s\n",
	      progname,
	      " -y | -z [-a addfile]\n",
	  "\t\t-m morphfile [-d debugmap] [-l logfile] [infile [outfile]]");
    print_log("gen:\t %s%s%s\n",
	      progname,
	      " -n | -c [-t gram_trace] [-s spell_trace] [-a addfile]\n",
	  "\t\t-m morphfile [-d debugmap] [-l logfile] [infile [outfile]]");
    print_log("lookup:\t %s%s%s\n",
	      progname,
	      " [-fi] [-b | -k] [-r rejectfile]\n",
	  "\t\t-m morphfile [-d debugmap] [-l logfile] [infile [outfile]]");
    print_log("r/f:\t %s%s%s\n",
	      progname,
	  " -C lookup_classes [-fU] [-E | -O] [-b | [-k] [-B bos_class]]\n",
	  "\t\t-m morphfile [-d debugmap] [-l logfile] [infile [outfile]]");
    print_log("dump:\t %s%s%s\n",
	      progname,
	      " -p | -q\n",
	  "\t\t-m morphfile [-d debugmap] [-l logfile] [infile [outfile]]");

    print_log("\n%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
	      "  -a addfile\n\tadd morphological entries from addfile\n",
	      "  -B bos_class\n\tbeginning of sentence class: which precedes the first word of a sentence\n",
	      "  -b\tfold case before lookup\n",
	      "  -C lookup_classes\n\tlist of lookup classes (any suitable separator will do)\n",
	      "  -c\tcreate full forms database\n",
	      "  -d debugmap\n\tprint debug messages\n",
	      "\tdebugmap is composed of the following bits:\n",
	      "\t0x1  debug initialisation\n",
	      "\t0x2  debug yacc parsing\n",
	      "\t0x4  debug rule combination\n",
	      "\t0x8  debug spelling application\n",
	      "\t0x10 print statistics with -p or -q options\n",
	      "  -E\textend existing morphological annotation\n",
	      "  -O\toverwrite existing morphological annotation\n",
	      "  -f\tflush output after each word lookup\n",
	      "  -h\tprint help and exit\n",
	      "  -i\tprint input segment sequential number before each lookup result\n",
	      "  -k\tfold case fallback\n",
	      "  -l logfile\n\tfile for trace and error messages\n",
	   "  -m morphfile\n\tfile containing the morphology description\n",
	      "  -n\tno database (test mode)\n",
	      "  -p\tdump tfs database\n",
	      "  -q\tdump forms database\n",
	      "  -r rejectfile\n\tfile for rejected words\n",
	      "");
    print_log("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
	      "  -s trace_level\n\ttrace spelling rule application\n",
	      "\ttrace_level can be (a level imply all preceding ones):\n",
	      "\t1  trace valid surface forms\n",
	      "\t2  trace rules whose lexical part match\n",
      "\t3  trace surface left context match (surface word construction)\n",
	    "\t4  trace surface right context mismatch and rule blocking\n",
	      "\t5  trace rule non blocking\n",
	      "  -t trace_level\n\ttrace grammar rule application\n",
	      "\ttrace_level can be (a level imply all preceding ones):\n",
	      "\t1  trace goal rules that apply\n",
	      "\t2  trace all rules that apply\n",
	      "\t10 trace rules that are tried but did not apply\n",
	      "  -U\tmark unknown words\n",
	      "  -v\tprint version and exit\n",
	      "  -y\tparse only\n",
	      "  -z\tparse and output normalized form\n",
	      "  infile\ttext input file, one word per line\n",
	      "  outfile\tambiguously tagged text output\n",
	      "");
}

int
main(argc, argv)
int         argc;
char       *argv[];

{
    int         c;
    int         rval;
    int         name_length;
    t_boolean   usage_error;
    t_boolean   nflag;
    t_boolean   cflag;
    t_boolean   dump_db;
    struct stat file_stat;
    char       *lookup_classes;

    extern int  optind;
    extern char *optarg;

    fullprogname = argv[0];
    progname = strrchr(fullprogname, (int) '/');
    if (progname == NULL)
	progname = fullprogname;
    else
	progname++;
    outfile = stdout;	/* fatal_error needs it */
    logfile = stderr;	/* fatal_error needs it */
    rejectfile = stderr;
    gram_file_name = NULL;
    augment_file_name = NULL;
    db_operation = Lookup;	/* default operation */
    usage_error = FALSE;
    nflag = FALSE;
    cflag = FALSE;
    want_segment_id = FALSE;
    want_flush = FALSE;
    parse_only = FALSE;
    normalize = FALSE;
    normalize_flag = FALSE;
    mark_unknown = FALSE;
    fold_case_always = FALSE;
    fold_case_fallback = FALSE;
    lookup_classes = NULL;
    begin_sentence_class = "\n";	/* cannot occur */
    extend_morph_field = FALSE;
    overwrite_morph_field = FALSE;
    dump_db = 0;
    while ((c = getopt(argc, argv, "a:bcd:fhikl:m:npqr:s:t:vyzB:C:EOU")) != -1) {
	switch (c) {
	    case 'd':
		if (sscanf(optarg, "%li", &debug) != 1) {
		    usage_error = TRUE;
		    print_log("error: bad debugmap: %s", optarg);
		}
		else {
		    if (debug & DEBUG_YACC)
			yydebug = 1;
		}
		break;
	    case 't':
		if (sscanf(optarg, "%d", &trace_level) != 1) {
		    usage_error = TRUE;
		    print_log("error: bad trace level: %s", optarg);
		}
		break;
	    case 's':
		if (sscanf(optarg, "%d", &spell_trace_level) != 1) {
		    usage_error = TRUE;
		    print_log("error: bad trace level: %s", optarg);
		}
		break;
	    case 'v':
		print_out("%s version %s\n%s\n", progname,
			  patch_version_string,
			  rcsid);
		EXIT(0);
		/* to shut up lint: */
		/* FALLTHROUGH */
	    case 'h':
		print_out("%s version %s\n", progname,
			  patch_version_string);
		print_usage();
		EXIT(0);
		/* to shut up lint: */
		/* FALLTHROUGH */
	    case 'l':
		if ((logfile = fopen(optarg, "w")) == NULL) {
		    logfile = stderr;
		    fatal_error("cannot open log file: %s", optarg);
		}
		break;
	    case 'r':
		if ((rejectfile = fopen(optarg, "w")) == NULL) {
		    fatal_error("cannot open reject file: %s", optarg);
		}
		break;
	    case 'm':
		gram_file_name = optarg;
		break;
	    case 'c':
		cflag = TRUE;
		if (nflag)
		    usage_error = TRUE;
		else
		    db_operation |= Create;	/* create new database */
		break;
	    case 'n':
		nflag = TRUE;
		if (cflag)
		    usage_error = TRUE;
		else
		    db_operation &= ~(Create | Lookup);	/* no database */
		break;
	    case 'a':
		db_operation |= Update;	/* augment */
		augment_file_name = optarg;
		break;
	    case 'i':
		want_segment_id = TRUE;
		break;
	    case 'b':	/* don't bother checking -b/-k/-B incompatibilities */
		fold_case_always = TRUE;
		fold_case_fallback = FALSE;
		break;
	    case 'k':
		fold_case_fallback = TRUE;
		fold_case_always = FALSE;
		break;
	    case 'f':
		want_flush = TRUE;
		break;
	    case 'p':
		dump_db = 1;
		break;
	    case 'q':
		dump_db = 2;
		break;
	    case 'z':
		normalize_flag = TRUE;
		/* FALLTHROUGH */
	    case 'y':
		parse_only = TRUE;
		db_operation &= ~(Create | Lookup);
		break;
	    case 'U':
		mark_unknown = TRUE;
		break;
	    case 'E':
		extend_morph_field = TRUE;
		overwrite_morph_field = FALSE;
		break;
	    case 'O':
		overwrite_morph_field = TRUE;
		extend_morph_field = FALSE;
		break;
	    case 'C':
		lookup_classes = optarg;
		break;
	    case 'B':
		begin_sentence_class = optarg;
		break;
	    default:
		usage_error = TRUE;
		break;
	}
    }
    if (usage_error) {
	print_usage();
	EXIT(2);
    }
    if (gram_file_name == NULL) {
	print_usage();
	fatal_error("error: no grammar file provided");
    }
    initfile(gram_file_name);
    if (!(db_operation & Lookup)) {
	if (spell_trace_level < 1)
	    spell_trace_level = 1;	/* don't be completely silent! */
    }
    else {	/* make the database name from the description file name */
	name_length = strlen(gram_file_name);
	MY_STRALLOC(db_file_name,
	name_length + MAX((int) strlen(db_suffix), (int) strlen(tfs_suffix))
		    + 1);
	(void) strcpy(db_file_name, gram_file_name);
	(void) strcpy(db_file_name + name_length, db_suffix);
	if (!(db_operation & Create)) {
	    if (stat(gram_file_name, &file_stat) < 0)
		fatal_error("cannot access file \"%s\" (errno=%d=%s)",
			    gram_file_name, errno,
			    (errno < sys_nerr ? sys_errlist[errno] : "?"));
#ifdef COMMENTED_OUT
	    time_t      gram_file_mtime;

	    /* this code is replaced by the crc checking */
	    gram_file_mtime = file_stat.st_mtime;
	    if (stat(db_file_name, &file_stat) < 0)
		fatal_error("cannot access file \"%s\" (errno=%d=%s)",
			    db_file_name, errno,
			    (errno < sys_nerr ? sys_errlist[errno] : "?"));
	    if (gram_file_mtime > file_stat.st_mtime)
		fatal_error("database file \"%s\" %s%s\"%s\": %s",
			    db_file_name,
			    "is out of date with respect to\n",
			    "the morphology description in",
			    gram_file_name,
			    "please recreate it");
#endif
	}
	db_forms_init(db_file_name, db_operation);
	(void) strcpy(db_file_name + name_length, tfs_suffix);
	open_tfs_file(db_file_name, db_operation);
    }
    if (optind >= argc) {
	infile = stdin;
    }
    else {
	if (!(db_operation & Lookup))
	    print_warning("input file \"%s\" will be ignored",
			  argv[optind]);
	if ((infile = fopen(argv[optind], "r")) == NULL)
	    fatal_error("cannot open input file: %s", argv[optind]);
	optind++;
    }
    if (optind >= argc) {
	outfile = stdout;
    }
    else {
	if ((outfile = fopen(argv[optind], "w")) == NULL)
	    fatal_error("cannot open output file: %s", argv[optind]);
	optind++;
    }
    init();
    parsing = TRUE;
    crc32file_clear(&crc);
    rval = yyparse();
    parsing = FALSE;	/* parse complete, error message format change */
    if (debug & DEBUG_INIT)
	print_log("parse = %d\n", rval);
    if (rval != 0)
	fatal_error("parsing aborted (code %d)", rval);
    if (db_operation & Lookup) {
	if (db_operation & (Create | Update)) {
	    db_forms_complete();	/* save what's done */
	    tfs_table_write(db_operation, db_file_name, crc_hash);
	}
	if (dump_db == 1)
	    dump_tfs_table();	/* dump on outfile, stat on logfile */
	else if (dump_db == 2)
	    db_forms_dump();	/* dump on outfile, stat on logfile */
	else if (lookup_classes)
	    lookup_tbl(infile, lookup_classes);
	else
	    lookup(infile);
	db_forms_close();
    }
    EXIT(0);
    /* NOTREACHED */
}