OSDN Git Service

import nethack-3.6.0
[jnethack/source.git] / util / dgn_main.c
1 /* NetHack 3.6  dgn_main.c      $NHDT-Date: 1432512785 2015/05/25 00:13:05 $  $NHDT-Branch: master $:$NHDT-Revision: 1.11 $ */
2 /*      Copyright (c) 1989 by Jean-Christophe Collet    */
3 /*      Copyright (c) 1990 by M. Stephenson             */
4 /* NetHack may be freely redistributed.  See license for details. */
5
6 /*
7  * This file contains the main function for the parser
8  * and some useful functions needed by yacc
9  */
10
11 #include "config.h"
12 #include "dlb.h"
13
14 /* Macintosh-specific code */
15 #if defined(__APPLE__) && defined(__MACH__)
16 /* MacOS X has Unix-style files and processes */
17 #undef MAC
18 #endif
19 #ifdef MAC
20 #if defined(__SC__) || defined(__MRC__)
21 #define MPWTOOL
22 #include <CursorCtl.h>
23 #else
24 /* put dungeon file in library location */
25 #define PREFIX ":lib:"
26 #endif
27 #endif
28
29 #ifndef MPWTOOL
30 #define SpinCursor(x)
31 #endif
32
33 #define MAX_ERRORS 25
34
35 extern int NDECL(yyparse);
36 extern int nh_line_number;
37 const char *fname = "(stdin)";
38 int fatal_error = 0;
39
40 int FDECL(main, (int, char **));
41 void FDECL(yyerror, (const char *));
42 void FDECL(yywarning, (const char *));
43 int NDECL(yywrap);
44 void FDECL(init_yyin, (FILE *));
45 void FDECL(init_yyout, (FILE *));
46
47 #ifdef AZTEC_36
48 FILE *FDECL(freopen, (char *, char *, FILE *));
49 #endif
50 #define Fprintf (void) fprintf
51
52 #if defined(__BORLANDC__) && !defined(_WIN32)
53 extern unsigned _stklen = STKSIZ;
54 #endif
55 int
56 main(argc, argv)
57 int argc;
58 char **argv;
59 {
60     char infile[64], outfile[64], basename[64];
61     FILE *fin, *fout;
62     int i, len;
63     boolean errors_encountered = FALSE;
64 #if defined(MAC) && (defined(THINK_C) || defined(__MWERKS__))
65     char *mark;
66     static char *mac_argv[] = { "dgn_comp", /* dummy argv[0] */
67                                 ":dat:dungeon.pdf" };
68
69     argc = SIZE(mac_argv);
70     argv = mac_argv;
71 #endif
72
73     Strcpy(infile, "(stdin)");
74     fin = stdin;
75     Strcpy(outfile, "(stdout)");
76     fout = stdout;
77
78     if (argc == 1) { /* Read standard input */
79         init_yyin(fin);
80         init_yyout(fout);
81         (void) yyparse();
82         if (fatal_error > 0)
83             errors_encountered = TRUE;
84     } else { /* Otherwise every argument is a filename */
85         for (i = 1; i < argc; i++) {
86             fname = strcpy(infile, argv[i]);
87             /* the input file had better be a .pdf file */
88             len = strlen(fname) - 4; /* length excluding suffix */
89             if (len < 0 || strncmp(".pdf", fname + len, 4)) {
90                 Fprintf(stderr, "Error - file name \"%s\" in wrong format.\n",
91                         fname);
92                 errors_encountered = TRUE;
93                 continue;
94             }
95
96 /* build output file name */
97 #if defined(MAC) && (defined(THINK_C) || defined(__MWERKS__))
98             /* extract basename from path to infile */
99             mark = strrchr(infile, ':');
100             strcpy(basename, mark ? mark + 1 : infile);
101             mark = strchr(basename, '.');
102             if (mark)
103                 *mark = '\0';
104 #else
105 /* Use the whole name - strip off the last 3 or 4 chars. */
106
107 #ifdef VMS /* avoid possible interaction with logical name */
108             len++; /* retain "." as trailing punctuation */
109 #endif
110             (void) strncpy(basename, infile, len);
111             basename[len] = '\0';
112 #endif
113
114             outfile[0] = '\0';
115 #ifdef PREFIX
116             (void) strcat(outfile, PREFIX);
117 #endif
118             (void) strcat(outfile, basename);
119
120             fin = freopen(infile, "r", stdin);
121             if (!fin) {
122                 Fprintf(stderr, "Can't open %s for input.\n", infile);
123                 perror(infile);
124                 errors_encountered = TRUE;
125                 continue;
126             }
127             fout = freopen(outfile, WRBMODE, stdout);
128             if (!fout) {
129                 Fprintf(stderr, "Can't open %s for output.\n", outfile);
130                 perror(outfile);
131                 errors_encountered = TRUE;
132                 continue;
133             }
134             init_yyin(fin);
135             init_yyout(fout);
136             (void) yyparse();
137             nh_line_number = 1;
138             if (fatal_error > 0) {
139                 errors_encountered = TRUE;
140                 fatal_error = 0;
141             }
142         }
143     }
144     if (fout && fclose(fout) < 0) {
145         Fprintf(stderr, "Can't finish output file.");
146         perror(outfile);
147         errors_encountered = TRUE;
148     }
149     exit(errors_encountered ? EXIT_FAILURE : EXIT_SUCCESS);
150     /*NOTREACHED*/
151     return 0;
152 }
153
154 /*
155  * Each time the parser detects an error, it uses this function.
156  * Here we take count of the errors. To continue farther than
157  * MAX_ERRORS wouldn't be reasonable.
158  */
159
160 void
161 yyerror(s)
162 const char *s;
163 {
164     (void) fprintf(stderr, "%s : line %d : %s\n", fname, nh_line_number, s);
165     if (++fatal_error > MAX_ERRORS) {
166         (void) fprintf(stderr, "Too many errors, good bye!\n");
167         exit(EXIT_FAILURE);
168     }
169 }
170
171 /*
172  * Just display a warning (that is : a non fatal error)
173  */
174
175 void
176 yywarning(s)
177 const char *s;
178 {
179     (void) fprintf(stderr, "%s : line %d : WARNING : %s\n", fname,
180                    nh_line_number, s);
181 }
182
183 int
184 yywrap()
185 {
186     SpinCursor(3); /*   Don't know if this is a good place to put it ?
187                                             Is it called for our grammar ?
188                       Often enough ?
189                                             Too often ? -- h+ */
190     return 1;
191 }
192
193 /*dgn_main.c*/