bump product version to 5.0.4.1
[LibreOffice.git] / soltools / cpp / _include.c
blob2d4b919f21e07fd75d14eba852a7b5e6e06c25db
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #if (defined(_WIN32) || defined(__IBMC__))
21 # include <io.h>
22 #else
23 # include <unistd.h>
24 #endif
26 #ifdef _MSC_VER
27 # define _POSIX_
28 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
34 #if defined(__IBMC__) || defined(__EMX__) || defined(_MSC_VER)
35 # include <fcntl.h>
36 # ifndef PATH_MAX
37 # define PATH_MAX _MAX_PATH
38 # endif
39 #endif
40 #include <limits.h>
42 #include "cpp.h"
44 Includelist includelist[NINCLUDE];
45 Wraplist wraplist[NINCLUDE];
47 void
48 doinclude(Tokenrow * trp, int depth, int import)
50 char fname[PATH_MAX], iname[PATH_MAX];
51 Includelist *ip;
52 int angled, fd, i;
53 size_t len;
55 trp->tp += 1;
56 if (trp->tp >= trp->lp)
57 goto syntax;
58 if (trp->tp->type != STRING && trp->tp->type != LT)
60 len = trp->tp - trp->bp;
61 expandrow(trp, "<include>");
62 trp->tp = trp->bp + len;
64 if (trp->tp->type == STRING)
66 len = trp->tp->len - 2;
67 if (len > sizeof(fname) - 1)
68 len = sizeof(fname) - 1;
69 strncpy(fname, (char *) trp->tp->t + 1, len);
70 angled = 0;
72 else
74 if (trp->tp->type == LT)
76 len = 0;
77 trp->tp++;
78 while (trp->tp->type != GT)
80 if (trp->tp > trp->lp || len + trp->tp->len + 2 >= sizeof(fname))
81 goto syntax;
82 strncpy(fname + len, (char *) trp->tp->t, trp->tp->len);
83 len += trp->tp->len;
84 trp->tp++;
86 angled = 1;
88 else
89 goto syntax;
91 trp->tp += 2;
92 if (trp->tp < trp->lp || len == 0)
93 goto syntax;
94 fname[len] = '\0';
95 if (fname[0] == '/')
97 fd = open(fname, O_RDONLY);
98 strcpy(iname, fname);
100 else
102 for (fd = -1, i = (depth < 0) ? (NINCLUDE - 1) : (depth - 1); i >= 0; i--)
104 ip = &includelist[i];
105 if (ip->file == NULL || ip->deleted || (angled && ip->always == 0))
106 continue;
107 if (strlen(fname) + strlen(ip->file) + 2 > sizeof(iname))
108 continue;
109 strcpy(iname, ip->file);
110 strcat(iname, "/");
111 strcat(iname, fname);
112 if ((fd = open(iname, O_RDONLY)) >= 0)
113 break;
117 if (fd >= 0)
119 if (++incdepth > NINC )
120 error(FATAL, "#%s too deeply nested", import ? "import" : "include");
121 if (Xflag)
122 genimport(fname, angled, iname, import);
123 if (Iflag)
124 error(INFO, "Open %s file [%s]", import ? "import" : "include", iname );
126 for (i = NINCLUDE - 1; i >= 0; i--)
128 if ((wraplist[i].file != NULL) &&
129 (strncmp(wraplist[i].file, iname, strlen(wraplist[i].file)) == 0))
130 break;
133 setsource((char *) newstring((uchar *) iname, strlen(iname), 0), i, fd, NULL, (i >= 0) ? 1 : 0);
135 if (!Pflag)
136 genline();
138 else
140 trp->tp = trp->bp + 2;
141 error(ERROR, "Could not find %s file %r", import ? "import" : "include", trp);
143 return;
144 syntax:
145 error(ERROR, "Syntax error in #%s", import ? "import" : "include");
146 return;
150 * Generate a line directive for cursource
152 void
153 genline(void)
155 static Token ta = {UNCLASS, 0, 0, 0, NULL, 0};
156 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
157 uchar *p;
159 ta.t = p = (uchar *) outptr;
160 strcpy((char *) p, "#line ");
161 p += sizeof("#line ") - 1;
162 p = (uchar *) outnum((char *) p, cursource->line);
163 *p++ = ' ';
164 *p++ = '"';
165 if (cursource->filename[0] != '/' && wd[0])
167 strcpy((char *) p, wd);
168 p += strlen(wd);
169 *p++ = '/';
171 strcpy((char *) p, cursource->filename);
172 p += strlen((char *) p);
173 *p++ = '"';
174 *p++ = '\n';
175 ta.len = (char *) p - outptr;
176 outptr = (char *) p;
177 tr.tp = tr.bp;
178 puttokens(&tr);
182 * Generate a pragma import/include directive
184 void
185 genimport(char *fname, int angled, char *iname, int import)
187 static Token ta = {UNCLASS, 0, 0, 0, NULL, 0};
188 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
189 uchar *p;
191 ta.t = p = (uchar *) outptr;
193 if (import)
194 strcpy((char *) p, "#pragma import");
195 else
196 strcpy((char *) p, "#pragma include");
198 p += strlen((char *) p);
200 *p++ = '(';
202 *p++ = angled ? '<' : '"';
203 strcpy((char *) p, fname);
204 p += strlen(fname);
205 *p++ = angled ? '>' : '"';
207 *p++ = ',';
209 *p++ = '"';
210 strcpy((char *) p, iname);
211 p += strlen(iname);
212 *p++ = '"';
214 *p++ = ')';
215 *p++ = '\n';
217 ta.len = (char *) p - outptr;
218 outptr = (char *) p;
219 tr.tp = tr.bp;
220 puttokens(&tr);
224 * Generate a extern C directive
226 void
227 genwrap(int end)
229 static Token ta = {UNCLASS, 0, 0, 0, NULL, 0};
230 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
231 uchar *p;
233 if (Cplusplus)
235 ta.t = p = (uchar *) outptr;
237 if (! end)
238 strcpy((char *) p, "extern \"C\" {");
239 else
240 strcpy((char *) p, "}");
242 p += strlen((char *) p);
244 *p++ = '\n';
246 ta.len = (char *) p - outptr;
247 outptr = (char *) p;
248 tr.tp = tr.bp;
249 puttokens(&tr);
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */