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
|
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "xpp.h"
#include "../../bootProto.h"
#define import_spp
#define import_knames
#include <iraf.h>
/*
* Main routine for the XPP preprocessor (first pass of the SPP compiler).
*/
#define IRAFDEFS "host$hlib/iraf.h"
int errflag;
int foreigndefs;
int hbindefs = 0;
char irafdefs[SZ_PATHNAME];
char *pkgenv = NULL;
char v_pkgenv[SZ_FNAME];
extern FILE *yyin;
extern FILE *yyout;
extern char fname[][SZ_PATHNAME];
extern int linenum[];
extern char *vfn2osfn();
extern char *os_getenv();
char *dottor();
extern void ZZSTRT (void);
extern void ZZSTOP (void);
extern int yylex (void);
static int isxfile (char *fname);
int main (int argc, char *argv[])
{
int i, rfflag, nfiles;
FILE *fp_defs, *source;
char *p;
ZZSTRT();
errflag = XPP_OK;
linenum[0] = 1;
rfflag = NO;
nfiles = 0;
/* Process flags and count the number of files.
*/
for (i=1; argv[i] != NULL; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'R':
/* Write .r file. */
rfflag = YES;
break;
case 'r':
/* Not used anymore */
if ((p = argv[++i]) == NULL)
--i;
break;
case 'h':
/* Use custom irafdefs file. */
if ((p = argv[++i]) == NULL)
--i;
else {
foreigndefs++;
strcpy (irafdefs, p);
}
break;
case 'A':
/* Use architecture-specific include file. */
hbindefs++;
break;
case 'p':
/* Load the environment for the named package. */
if ((pkgenv = argv[++i]) == NULL)
--i;
else
loadpkgenv (pkgenv);
break;
default:
fprintf (stderr, "unknown option '%s'\n", argv[i]);
fflush (stderr);
}
} else if (isxfile (argv[i]))
nfiles++;
}
/* If no package environment was specified on the command line,
* check if the user has a default package set in their environment.
*/
if (!pkgenv) {
if ((pkgenv = os_getenv("PKGENV"))) {
strcpy (v_pkgenv, pkgenv);
loadpkgenv (pkgenv = v_pkgenv);
}
}
/* Generate pathname of <iraf.h>.
*/
if (!foreigndefs)
strcpy (irafdefs, vfn2osfn (IRAFDEFS,0));
/* Process either the standard input or a list of files.
*/
if (nfiles == 0) {
yyin = stdin;
yyout = stdout;
strcpy (fname[0], "STDIN");
yylex();
} else {
/* Preprocess each file.
*/
for (i=1; argv[i] != NULL; i++)
if (isxfile (argv[i])) {
if (nfiles > 1) {
fprintf (stderr, "%s:\n", argv[i]);
fflush (stderr);
}
/* Open source file.
*/
if ((source = fopen (vfn2osfn(argv[i],0), "r")) == NULL) {
fprintf (stderr, "cannot read file %s\n", argv[i]);
fflush (stderr);
errflag |= XPP_BADXFILE;
} else {
/* Open output file.
*/
if (rfflag) {
char *osfn;
osfn = vfn2osfn (dottor (argv[i]), 0);
if ((yyout = fopen (osfn, "w")) == NULL) {
fprintf (stderr,
"cannot write output file %s\n", osfn);
fflush (stderr);
errflag |= XPP_BADXFILE;
fclose (yyin);
continue;
}
} else
yyout = stdout;
/* Open and process hlib$iraf.h.
*/
if ((fp_defs = fopen (irafdefs, "r")) == NULL) {
fprintf (stderr, "cannot open %s\n", irafdefs);
ZZSTOP();
exit (XPP_COMPERR);
}
yyin = fp_defs;
yylex();
linenum[0] = 1;
fclose (fp_defs);
/* Process the source file.
*/
strcpy (fname[0], argv[i]);
yyin = source;
yylex();
fclose (source);
if (rfflag)
fclose (yyout);
}
}
}
ZZSTOP();
exit (errflag);
return (0);
}
/* ISXFILE -- Does the named file have a ".x" extension.
*/
static int
isxfile (char *fname)
{
char *p;
if (fname[0] != '-') {
for (p=fname; *p++ != EOS; )
;
while (*--p != '.' && p >= fname)
;
if (*p == '.' && *(p+1) == 'x')
return (YES);
}
return (NO);
}
/* DOTTOR -- Change the extension of the named file to ".r".
*/
char *
dottor (fname)
char *fname;
{
static char rfname[SZ_PATHNAME+1];
char *ip, *op, *lastdot;
lastdot = NULL;
for (ip=fname, op=rfname; (*op = *ip++); op++)
if (*op == '.')
lastdot = op;
if (lastdot) {
*(lastdot+1) = 'r';
*(lastdot+2) = EOS;
}
return (rfname);
}
|