merge the formfield patch from ooo-build
[ooovba.git] / soltools / cpp / _include.c
blob6056f8372affd140b7f3113c1edaaeffd8d67f57
1 #if (defined(_WIN32) || defined(_MSDOS) || defined(__IBMC__))
2 # include <io.h>
3 #else
4 # include <unistd.h>
5 #endif
7 #ifdef _MSC_VER
8 # define _POSIX_
9 #endif
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <fcntl.h>
16 #ifdef __hpux
17 # define _HPUX_SOURCE
18 #endif
19 #if defined(__IBMC__) || defined(__EMX__)
20 # include <fcntl.h>
21 # define PATH_MAX _MAX_PATH
22 #endif
23 #include <limits.h>
25 #include "cpp.h"
27 Includelist includelist[NINCLUDE];
28 Wraplist wraplist[NINCLUDE];
30 void
31 doinclude(Tokenrow * trp, int depth, int import)
33 char fname[PATH_MAX], iname[PATH_MAX];
34 Includelist *ip;
35 int angled, fd, i;
36 size_t len;
38 trp->tp += 1;
39 if (trp->tp >= trp->lp)
40 goto syntax;
41 if (trp->tp->type != STRING && trp->tp->type != LT)
43 len = trp->tp - trp->bp;
44 expandrow(trp, "<include>");
45 trp->tp = trp->bp + len;
47 if (trp->tp->type == STRING)
49 len = trp->tp->len - 2;
50 if (len > sizeof(fname) - 1)
51 len = sizeof(fname) - 1;
52 strncpy(fname, (char *) trp->tp->t + 1, len);
53 angled = 0;
55 else
57 if (trp->tp->type == LT)
59 len = 0;
60 trp->tp++;
61 while (trp->tp->type != GT)
63 if (trp->tp > trp->lp || len + trp->tp->len + 2 >= sizeof(fname))
64 goto syntax;
65 strncpy(fname + len, (char *) trp->tp->t, trp->tp->len);
66 len += trp->tp->len;
67 trp->tp++;
69 angled = 1;
71 else
72 goto syntax;
74 trp->tp += 2;
75 if (trp->tp < trp->lp || len == 0)
76 goto syntax;
77 fname[len] = '\0';
78 if (fname[0] == '/')
80 fd = open(fname, O_RDONLY);
81 strcpy(iname, fname);
83 else
85 for (fd = -1, i = (depth < 0) ? (NINCLUDE - 1) : (depth - 1); i >= 0; i--)
87 ip = &includelist[i];
88 if (ip->file == NULL || ip->deleted || (angled && ip->always == 0))
89 continue;
90 if (strlen(fname) + strlen(ip->file) + 2 > sizeof(iname))
91 continue;
92 strcpy(iname, ip->file);
93 strcat(iname, "/");
94 strcat(iname, fname);
95 if ((fd = open(iname, O_RDONLY)) >= 0)
96 break;
100 if (fd >= 0)
102 if (++incdepth > NINC )
103 error(FATAL, "#%s too deeply nested", import ? "import" : "include");
104 if (Xflag)
105 genimport(fname, angled, iname, import);
106 if (Iflag)
107 error(INFO, "Open %s file [%s]", import ? "import" : "include", iname );
109 for (i = NINCLUDE - 1; i >= 0; i--)
111 if ((wraplist[i].file != NULL) &&
112 (strncmp(wraplist[i].file, iname, strlen(wraplist[i].file)) == 0))
113 break;
116 setsource((char *) newstring((uchar *) iname, strlen(iname), 0), i, fd, NULL, (i >= 0) ? 1 : 0);
118 if (!Pflag)
119 genline();
121 else
123 trp->tp = trp->bp + 2;
124 error(ERROR, "Could not find %s file %r", import ? "import" : "include", trp);
126 return;
127 syntax:
128 error(ERROR, "Syntax error in #%s", import ? "import" : "include");
129 return;
133 * Generate a line directive for cursource
135 void
136 genline(void)
138 static Token ta = {UNCLASS, 0, 0, 0, NULL, 0};
139 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
140 uchar *p;
142 ta.t = p = (uchar *) outptr;
143 strcpy((char *) p, "#line ");
144 p += sizeof("#line ") - 1;
145 p = (uchar *) outnum((char *) p, cursource->line);
146 *p++ = ' ';
147 *p++ = '"';
148 if (cursource->filename[0] != '/' && wd[0])
150 strcpy((char *) p, wd);
151 p += strlen(wd);
152 *p++ = '/';
154 strcpy((char *) p, cursource->filename);
155 p += strlen((char *) p);
156 *p++ = '"';
157 *p++ = '\n';
158 ta.len = (char *) p - outptr;
159 outptr = (char *) p;
160 tr.tp = tr.bp;
161 puttokens(&tr);
165 * Generate a pragma import/include directive
167 void
168 genimport(char *fname, int angled, char *iname, int import)
170 static Token ta = {UNCLASS, 0, 0, 0, NULL, 0};
171 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
172 uchar *p;
174 ta.t = p = (uchar *) outptr;
176 if (import)
177 strcpy((char *) p, "#pragma import");
178 else
179 strcpy((char *) p, "#pragma include");
181 p += strlen((char *) p);
183 *p++ = '(';
185 *p++ = angled ? '<' : '"';
186 strcpy((char *) p, fname);
187 p += strlen(fname);
188 *p++ = angled ? '>' : '"';
190 *p++ = ',';
192 *p++ = '"';
193 strcpy((char *) p, iname);
194 p += strlen(iname);
195 *p++ = '"';
197 *p++ = ')';
198 *p++ = '\n';
200 ta.len = (char *) p - outptr;
201 outptr = (char *) p;
202 tr.tp = tr.bp;
203 puttokens(&tr);
207 * Generate a extern C directive
209 void
210 genwrap(int end)
212 static Token ta = {UNCLASS, 0, 0, 0, NULL, 0};
213 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
214 uchar *p;
216 if (Cplusplus)
218 ta.t = p = (uchar *) outptr;
220 if (! end)
221 strcpy((char *) p, "extern \"C\" {");
222 else
223 strcpy((char *) p, "}");
225 p += strlen((char *) p);
227 *p++ = '\n';
229 ta.len = (char *) p - outptr;
230 outptr = (char *) p;
231 tr.tp = tr.bp;
232 puttokens(&tr);