Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / soltools / cpp / _include.c
blob19d14ae68f1283553731b8574247fe73f838d4b7
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 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <fcntl.h>
31 #if defined(__IBMC__) || defined(__EMX__) || defined(_MSC_VER)
32 # include <fcntl.h>
33 # ifndef PATH_MAX
34 # define PATH_MAX _MAX_PATH
35 # endif
36 #endif
37 #include <limits.h>
39 #include "cpp.h"
41 Includelist includelist[NINCLUDE];
42 Wraplist wraplist[NINCLUDE];
44 void
45 doinclude(Tokenrow * trp, int depth, int import)
47 char fname[PATH_MAX], iname[PATH_MAX];
48 Includelist *ip;
49 int angled, fd, i;
50 size_t len;
52 trp->tp += 1;
53 if (trp->tp >= trp->lp)
54 goto syntax;
55 if (trp->tp->type != STRING && trp->tp->type != LT)
57 len = trp->tp - trp->bp;
58 expandrow(trp, "<include>");
59 trp->tp = trp->bp + len;
61 if (trp->tp->type == STRING)
63 len = trp->tp->len - 2;
64 if (len > sizeof(fname) - 1)
65 len = sizeof(fname) - 1;
66 strncpy(fname, (char *) trp->tp->t + 1, len);
67 angled = 0;
69 else
71 if (trp->tp->type == LT)
73 len = 0;
74 trp->tp++;
75 while (trp->tp->type != GT)
77 if (trp->tp > trp->lp || len + trp->tp->len + 2 >= sizeof(fname))
78 goto syntax;
79 strncpy(fname + len, (char *) trp->tp->t, trp->tp->len);
80 len += trp->tp->len;
81 trp->tp++;
83 angled = 1;
85 else
86 goto syntax;
88 trp->tp += 2;
89 if (trp->tp < trp->lp || len == 0)
90 goto syntax;
91 fname[len] = '\0';
92 if (fname[0] == '/')
94 fd = open(fname, O_RDONLY);
95 strcpy(iname, fname);
97 else
99 for (fd = -1, i = (depth < 0) ? (NINCLUDE - 1) : (depth - 1); i >= 0; i--)
101 ip = &includelist[i];
102 if (ip->file == NULL || ip->deleted || (angled && ip->always == 0))
103 continue;
104 if (strlen(fname) + strlen(ip->file) + 2 > sizeof(iname))
105 continue;
106 strcpy(iname, ip->file);
107 strcat(iname, "/");
108 strcat(iname, fname);
109 if ((fd = open(iname, O_RDONLY)) >= 0)
110 break;
114 if (fd >= 0)
116 if (++incdepth > NINC )
117 error(FATAL, "#%s too deeply nested", import ? "import" : "include");
118 if (Xflag)
119 genimport(fname, angled, iname, import);
120 if (Iflag)
121 error(INFO, "Open %s file [%s]", import ? "import" : "include", iname );
123 for (i = NINCLUDE - 1; i >= 0; i--)
125 if ((wraplist[i].file != NULL) &&
126 (strncmp(wraplist[i].file, iname, strlen(wraplist[i].file)) == 0))
127 break;
130 setsource((char *) newstring((uchar *) iname, strlen(iname), 0), i, fd, NULL, (i >= 0) ? 1 : 0);
132 if (!Pflag)
133 genline();
135 else
137 trp->tp = trp->bp + 2;
138 error(ERROR, "Could not find %s file %r", import ? "import" : "include", trp);
140 return;
141 syntax:
142 error(ERROR, "Syntax error in #%s", import ? "import" : "include");
143 return;
147 * Generate a line directive for cursource
149 void
150 genline(void)
152 static Token ta = {UNCLASS, 0, 0, NULL, 0};
153 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
154 uchar *p;
156 ta.t = p = (uchar *) outptr;
157 strcpy((char *) p, "#line ");
158 p += sizeof("#line ") - 1;
159 p = (uchar *) outnum((char *) p, cursource->line);
160 *p++ = ' ';
161 *p++ = '"';
162 if (cursource->filename[0] != '/' && wd[0])
164 strcpy((char *) p, wd);
165 p += strlen(wd);
166 *p++ = '/';
168 strcpy((char *) p, cursource->filename);
169 p += strlen((char *) p);
170 *p++ = '"';
171 *p++ = '\n';
172 ta.len = (char *) p - outptr;
173 outptr = (char *) p;
174 tr.tp = tr.bp;
175 puttokens(&tr);
179 * Generate a pragma import/include directive
181 void
182 genimport(char const *fname, int angled, char const *iname, int import)
184 static Token ta = {UNCLASS, 0, 0, NULL, 0};
185 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
186 uchar *p;
188 ta.t = p = (uchar *) outptr;
190 if (import)
191 strcpy((char *) p, "#pragma import");
192 else
193 strcpy((char *) p, "#pragma include");
195 p += strlen((char *) p);
197 *p++ = '(';
199 *p++ = angled ? '<' : '"';
200 strcpy((char *) p, fname);
201 p += strlen(fname);
202 *p++ = angled ? '>' : '"';
204 *p++ = ',';
206 *p++ = '"';
207 strcpy((char *) p, iname);
208 p += strlen(iname);
209 *p++ = '"';
211 *p++ = ')';
212 *p++ = '\n';
214 ta.len = (char *) p - outptr;
215 outptr = (char *) p;
216 tr.tp = tr.bp;
217 puttokens(&tr);
221 * Generate an extern C directive
223 void
224 genwrap(int end)
226 static Token ta = {UNCLASS, 0, 0, NULL, 0};
227 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
228 uchar *p;
230 if (!Cplusplus)
231 return;
233 ta.t = p = (uchar *) outptr;
235 if (! end)
236 strcpy((char *) p, "extern \"C\" {");
237 else
238 strcpy((char *) p, "}");
240 p += strlen((char *) p);
242 *p++ = '\n';
244 ta.len = (char *) p - outptr;
245 outptr = (char *) p;
246 tr.tp = tr.bp;
247 puttokens(&tr);
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */