update dev300-m58
[ooovba.git] / idlc / source / preproc / include.c
blobdf93c0f08178acc7f0cad867c97f50492b485bb1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: include.c,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #if (defined(_WIN32) || defined(_MSDOS) || defined(__IBMC__))
31 # include <io.h>
32 #else
33 # include <unistd.h>
34 #endif
35 #ifdef _MSC_VER
36 # define _POSIX_
37 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <fcntl.h>
43 #ifdef __hpux
44 # define _HPUX_SOURCE
45 #endif
46 #ifdef SCO
47 #define _IBCS2
48 #endif
49 #include <limits.h>
51 #include "cpp.h"
53 Includelist includelist[NINCLUDE];
54 Wraplist wraplist[NINCLUDE];
56 void
57 doinclude(Tokenrow * trp, int depth, int import)
59 #ifdef OS2
60 char fname[_MAX_PATH], iname[_MAX_PATH];
61 #else
62 char fname[PATH_MAX], iname[PATH_MAX];
63 #endif
64 Includelist *ip;
65 int angled, len, fd, i;
67 trp->tp += 1;
68 if (trp->tp >= trp->lp)
69 goto syntax;
70 if (trp->tp->type != STRING && trp->tp->type != LT)
72 len = trp->tp - trp->bp;
73 expandrow(trp, "<include>");
74 trp->tp = trp->bp + len;
76 if (trp->tp->type == STRING)
78 len = trp->tp->len - 2;
79 if (len > (int)sizeof(fname) - 1)
80 len = sizeof(fname) - 1;
81 strncpy(fname, (char *) trp->tp->t + 1, len);
82 angled = 0;
84 else
85 if (trp->tp->type == LT)
87 len = 0;
88 trp->tp++;
89 while (trp->tp->type != GT)
91 if (trp->tp > trp->lp || len + trp->tp->len + 2 >= sizeof(fname))
92 goto syntax;
93 strncpy(fname + len, (char *) trp->tp->t, trp->tp->len);
94 len += trp->tp->len;
95 trp->tp++;
97 angled = 1;
99 else
100 goto syntax;
101 trp->tp += 2;
102 if (trp->tp < trp->lp || len == 0)
103 goto syntax;
104 fname[len] = '\0';
105 if (fname[0] == '/')
107 fd = open(fname, O_RDONLY);
108 strcpy(iname, fname);
110 else
111 for (fd = -1, i = (depth < 0) ? (NINCLUDE - 1) : (depth - 1); i >= 0; i--)
113 ip = &includelist[i];
114 if (ip->file == NULL || ip->deleted || (angled && ip->always == 0))
115 continue;
116 if (strlen(fname) + strlen(ip->file) + 2 > sizeof(iname))
117 continue;
118 strcpy(iname, ip->file);
119 strcat(iname, "/");
120 strcat(iname, fname);
121 if ((fd = open(iname, O_RDONLY)) >= 0)
122 break;
124 if (fd >= 0)
126 if (++incdepth > NINC )
127 error(FATAL, "#%s too deeply nested", import ? "import" : "include");
128 if (Xflag)
129 genimport(fname, angled, iname, import);
130 if (Iflag)
131 error(INFO, "Open %s file [%s]", import ? "import" : "include", iname );
133 for (i = NINCLUDE - 1; i >= 0; i--)
135 if ((wraplist[i].file != NULL) &&
136 (strncmp(wraplist[i].file, iname, strlen(wraplist[i].file)) == 0))
137 break;
140 setsource((char *) newstring((uchar *) iname, strlen(iname), 0), i, fd, NULL, (i >= 0) ? 1 : 0);
142 if (!Pflag)
143 genline();
145 else
147 trp->tp = trp->bp + 2;
148 error(ERROR, "Could not find %s file %r", import ? "import" : "include", trp);
150 return;
151 syntax:
152 error(ERROR, "Syntax error in #%s", import ? "import" : "include");
153 return;
157 * Generate a line directive for cursource
159 void
160 genline(void)
162 static Token ta = {UNCLASS, 0, 0, 0, NULL };
163 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
164 uchar *p;
166 ta.t = p = (uchar *) outptr;
167 strcpy((char *) p, "#line ");
168 p += sizeof("#line ") - 1;
169 p = (uchar *) outnum((char *) p, cursource->line);
170 *p++ = ' ';
171 *p++ = '"';
172 if (cursource->filename[0] != '/' && wd[0])
174 strcpy((char *) p, wd);
175 p += strlen(wd);
176 *p++ = '/';
178 strcpy((char *) p, cursource->filename);
179 p += strlen((char *) p);
180 *p++ = '"';
181 *p++ = '\n';
182 ta.len = (char *) p - outptr;
183 outptr = (char *) p;
184 tr.tp = tr.bp;
185 puttokens(&tr);
189 * Generate a pragma import/include directive
191 void
192 genimport(char *fname, int angled, char *iname, int import)
194 static Token ta = {UNCLASS, 0, 0, 0, NULL };
195 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
196 uchar *p;
198 ta.t = p = (uchar *) outptr;
200 if (import)
201 strcpy((char *) p, "#pragma import");
202 else
203 strcpy((char *) p, "#pragma include");
205 p += strlen((char *) p);
207 *p++ = '(';
209 *p++ = angled ? '<' : '"';
210 strcpy((char *) p, fname);
211 p += strlen(fname);
212 *p++ = angled ? '>' : '"';
214 *p++ = ',';
216 *p++ = '"';
217 strcpy((char *) p, iname);
218 p += strlen(iname);
219 *p++ = '"';
221 *p++ = ')';
222 *p++ = '\n';
224 ta.len = (char *) p - outptr;
225 outptr = (char *) p;
226 tr.tp = tr.bp;
227 puttokens(&tr);
231 * Generate a extern C directive
233 void
234 genwrap(int end)
236 static Token ta = {UNCLASS, 0, 0, 0, NULL};
237 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
238 uchar *p;
240 if (Cplusplus)
242 ta.t = p = (uchar *) outptr;
244 if (! end)
245 strcpy((char *) p, "extern \"C\" {");
246 else
247 strcpy((char *) p, "}");
249 p += strlen((char *) p);
251 *p++ = '\n';
253 ta.len = (char *) p - outptr;
254 outptr = (char *) p;
255 tr.tp = tr.bp;
256 puttokens(&tr);