OSDN Git Service

add copyright
[nethackexpress/trunk.git] / util / dgn_main.c
1 /*      SCCS Id: @(#)dgn_main.c 3.4     1994/09/23      */
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 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
70         argc = SIZE(mac_argv);
71         argv = mac_argv;
72 #endif
73
74         Strcpy(infile, "(stdin)");
75         fin = stdin;
76         Strcpy(outfile, "(stdout)");
77         fout = stdout;
78
79         if (argc == 1) {        /* Read standard input */
80             init_yyin(fin);
81             init_yyout(fout);
82             (void) yyparse();
83             if (fatal_error > 0)
84                 errors_encountered = TRUE;
85         } else {                /* Otherwise every argument is a filename */
86             for(i=1; i<argc; i++) {
87                 fname = strcpy(infile, argv[i]);
88                 /* the input file had better be a .pdf file */
89                 len = strlen(fname) - 4;        /* length excluding suffix */
90                 if (len < 0 || strncmp(".pdf", fname + len, 4)) {
91                     Fprintf(stderr,
92                             "Error - file name \"%s\" in wrong format.\n",
93                             fname);
94                     errors_encountered = TRUE;
95                     continue;
96                 }
97
98                 /* build output file name */
99 #if defined(MAC) && (defined(THINK_C) || defined(__MWERKS__))
100                 /* extract basename from path to infile */
101                 mark = strrchr(infile, ':');
102                 strcpy(basename, mark ? mark+1 : infile);
103                 mark = strchr(basename, '.');
104                 if (mark) *mark = '\0';
105 #else
106                 /* Use the whole name - strip off the last 3 or 4 chars. */
107
108 #ifdef VMS      /* avoid possible interaction with logical name */
109                 len++;  /* retain "." as trailing punctuation */
110 #endif
111                 (void) strncpy(basename, infile, len);
112                 basename[len] = '\0';
113 #endif
114
115                 outfile[0] = '\0';
116 #ifdef PREFIX
117                 (void) strcat(outfile, PREFIX);
118 #endif
119                 (void) strcat(outfile, basename);
120
121                 fin = freopen(infile, "r", stdin);
122                 if (!fin) {
123                     Fprintf(stderr, "Can't open %s for input.\n", infile);
124                     perror(infile);
125                     errors_encountered = TRUE;
126                     continue;
127                 }
128                 fout = freopen(outfile, WRBMODE, stdout);
129                 if (!fout) {
130                     Fprintf(stderr, "Can't open %s for output.\n", outfile);
131                     perror(outfile);
132                     errors_encountered = TRUE;
133                     continue;
134                 }
135                 init_yyin(fin);
136                 init_yyout(fout);
137                 (void) yyparse();
138                 line_number = 1;
139                 if (fatal_error > 0) {
140                         errors_encountered = TRUE;
141                         fatal_error = 0;
142                 }
143             }
144         }
145         if (fout && fclose(fout) < 0) {
146             Fprintf(stderr, "Can't finish output file.");
147             perror(outfile);
148             errors_encountered = TRUE;
149         }
150         exit(errors_encountered ? EXIT_FAILURE : EXIT_SUCCESS);
151         /*NOTREACHED*/
152         return 0;
153 }
154
155 /*
156  * Each time the parser detects an error, it uses this function.
157  * Here we take count of the errors. To continue farther than
158  * MAX_ERRORS wouldn't be reasonable.
159  */
160
161 void yyerror(s)
162 const char *s;
163 {
164         (void) fprintf(stderr,"%s : line %d : %s\n",fname,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 yywarning(s)
176 const char *s;
177 {
178         (void) fprintf(stderr,"%s : line %d : WARNING : %s\n",fname,line_number,s);
179 }
180
181 int yywrap()
182 {
183         SpinCursor(3); /*       Don't know if this is a good place to put it ?
184                                                 Is it called for our grammar ? Often enough ?
185                                                 Too often ? -- h+ */
186        return 1;
187 }
188
189 /*dgn_main.c*/