NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / util / dgn_main.c
blob453d5a2ef759b95b63c1c8eb11b3ed56b90d4ced
1 /* aNetHack 0.0.1 dgn_main.c $ANH-Date: 1432512785 2015/05/25 00:13:05 $ $ANH-Branch: master $:$ANH-Revision: 1.11 $ */
2 /* Copyright (c) 1989 by Jean-Christophe Collet */
3 /* Copyright (c) 1990 by M. Stephenson */
4 /* aNetHack may be freely redistributed. See license for details. */
6 /*
7 * This file contains the main function for the parser
8 * and some useful functions needed by yacc
9 */
11 #include "config.h"
12 #include "dlb.h"
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
29 #ifndef MPWTOOL
30 #define SpinCursor(x)
31 #endif
33 #define MAX_ERRORS 25
35 extern int NDECL(yyparse);
36 extern int nh_line_number;
37 const char *fname = "(stdin)";
38 int fatal_error = 0;
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 *));
47 #ifdef AZTEC_36
48 FILE *FDECL(freopen, (char *, char *, FILE *));
49 #endif
50 #define Fprintf (void) fprintf
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;
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" };
69 argc = SIZE(mac_argv);
70 argv = mac_argv;
71 #endif
73 Strcpy(infile, "(stdin)");
74 fin = stdin;
75 Strcpy(outfile, "(stdout)");
76 fout = stdout;
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;
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. */
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
114 outfile[0] = '\0';
115 #ifdef PREFIX
116 (void) strcat(outfile, PREFIX);
117 #endif
118 (void) strcat(outfile, basename);
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;
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;
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;
144 if (fout && fclose(fout) < 0) {
145 Fprintf(stderr, "Can't finish output file.");
146 perror(outfile);
147 errors_encountered = TRUE;
149 exit(errors_encountered ? EXIT_FAILURE : EXIT_SUCCESS);
150 /*NOTREACHED*/
151 return 0;
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.
160 void
161 yyerror(s)
162 const char *s;
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);
172 * Just display a warning (that is : a non fatal error)
175 void
176 yywarning(s)
177 const char *s;
179 (void) fprintf(stderr, "%s : line %d : WARNING : %s\n", fname,
180 nh_line_number, s);
184 yywrap()
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;
193 /*dgn_main.c*/