update credits
[LibreOffice.git] / soltools / cpp / _include.c
blob40ff092dd82c0cf3c8d7dd8f94e1d002bab650fa
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__)
35 # include <fcntl.h>
36 # define PATH_MAX _MAX_PATH
37 #endif
38 #include <limits.h>
40 #include "cpp.h"
42 Includelist includelist[NINCLUDE];
43 Wraplist wraplist[NINCLUDE];
45 void
46 doinclude(Tokenrow * trp, int depth, int import)
48 char fname[PATH_MAX], iname[PATH_MAX];
49 Includelist *ip;
50 int angled, fd, i;
51 size_t len;
53 trp->tp += 1;
54 if (trp->tp >= trp->lp)
55 goto syntax;
56 if (trp->tp->type != STRING && trp->tp->type != LT)
58 len = trp->tp - trp->bp;
59 expandrow(trp, "<include>");
60 trp->tp = trp->bp + len;
62 if (trp->tp->type == STRING)
64 len = trp->tp->len - 2;
65 if (len > sizeof(fname) - 1)
66 len = sizeof(fname) - 1;
67 strncpy(fname, (char *) trp->tp->t + 1, len);
68 angled = 0;
70 else
72 if (trp->tp->type == LT)
74 len = 0;
75 trp->tp++;
76 while (trp->tp->type != GT)
78 if (trp->tp > trp->lp || len + trp->tp->len + 2 >= sizeof(fname))
79 goto syntax;
80 strncpy(fname + len, (char *) trp->tp->t, trp->tp->len);
81 len += trp->tp->len;
82 trp->tp++;
84 angled = 1;
86 else
87 goto syntax;
89 trp->tp += 2;
90 if (trp->tp < trp->lp || len == 0)
91 goto syntax;
92 fname[len] = '\0';
93 if (fname[0] == '/')
95 fd = open(fname, O_RDONLY);
96 strcpy(iname, fname);
98 else
100 for (fd = -1, i = (depth < 0) ? (NINCLUDE - 1) : (depth - 1); i >= 0; i--)
102 ip = &includelist[i];
103 if (ip->file == NULL || ip->deleted || (angled && ip->always == 0))
104 continue;
105 if (strlen(fname) + strlen(ip->file) + 2 > sizeof(iname))
106 continue;
107 strcpy(iname, ip->file);
108 strcat(iname, "/");
109 strcat(iname, fname);
110 if ((fd = open(iname, O_RDONLY)) >= 0)
111 break;
115 if (fd >= 0)
117 if (++incdepth > NINC )
118 error(FATAL, "#%s too deeply nested", import ? "import" : "include");
119 if (Xflag)
120 genimport(fname, angled, iname, import);
121 if (Iflag)
122 error(INFO, "Open %s file [%s]", import ? "import" : "include", iname );
124 for (i = NINCLUDE - 1; i >= 0; i--)
126 if ((wraplist[i].file != NULL) &&
127 (strncmp(wraplist[i].file, iname, strlen(wraplist[i].file)) == 0))
128 break;
131 setsource((char *) newstring((uchar *) iname, strlen(iname), 0), i, fd, NULL, (i >= 0) ? 1 : 0);
133 if (!Pflag)
134 genline();
136 else
138 trp->tp = trp->bp + 2;
139 error(ERROR, "Could not find %s file %r", import ? "import" : "include", trp);
141 return;
142 syntax:
143 error(ERROR, "Syntax error in #%s", import ? "import" : "include");
144 return;
148 * Generate a line directive for cursource
150 void
151 genline(void)
153 static Token ta = {UNCLASS, 0, 0, 0, NULL, 0};
154 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
155 uchar *p;
157 ta.t = p = (uchar *) outptr;
158 strcpy((char *) p, "#line ");
159 p += sizeof("#line ") - 1;
160 p = (uchar *) outnum((char *) p, cursource->line);
161 *p++ = ' ';
162 *p++ = '"';
163 if (cursource->filename[0] != '/' && wd[0])
165 strcpy((char *) p, wd);
166 p += strlen(wd);
167 *p++ = '/';
169 strcpy((char *) p, cursource->filename);
170 p += strlen((char *) p);
171 *p++ = '"';
172 *p++ = '\n';
173 ta.len = (char *) p - outptr;
174 outptr = (char *) p;
175 tr.tp = tr.bp;
176 puttokens(&tr);
180 * Generate a pragma import/include directive
182 void
183 genimport(char *fname, int angled, char *iname, int import)
185 static Token ta = {UNCLASS, 0, 0, 0, NULL, 0};
186 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
187 uchar *p;
189 ta.t = p = (uchar *) outptr;
191 if (import)
192 strcpy((char *) p, "#pragma import");
193 else
194 strcpy((char *) p, "#pragma include");
196 p += strlen((char *) p);
198 *p++ = '(';
200 *p++ = angled ? '<' : '"';
201 strcpy((char *) p, fname);
202 p += strlen(fname);
203 *p++ = angled ? '>' : '"';
205 *p++ = ',';
207 *p++ = '"';
208 strcpy((char *) p, iname);
209 p += strlen(iname);
210 *p++ = '"';
212 *p++ = ')';
213 *p++ = '\n';
215 ta.len = (char *) p - outptr;
216 outptr = (char *) p;
217 tr.tp = tr.bp;
218 puttokens(&tr);
222 * Generate a extern C directive
224 void
225 genwrap(int end)
227 static Token ta = {UNCLASS, 0, 0, 0, NULL, 0};
228 static Tokenrow tr = {&ta, &ta, &ta + 1, 1};
229 uchar *p;
231 if (Cplusplus)
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);
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */