1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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__))
31 #if defined(__IBMC__) || defined(__EMX__) || defined(_MSC_VER)
34 # define PATH_MAX _MAX_PATH
41 Includelist includelist
[NINCLUDE
];
42 Wraplist wraplist
[NINCLUDE
];
45 doinclude(Tokenrow
* trp
, int depth
, int import
)
47 char fname
[PATH_MAX
], iname
[PATH_MAX
];
53 if (trp
->tp
>= trp
->lp
)
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
);
71 if (trp
->tp
->type
== LT
)
75 while (trp
->tp
->type
!= GT
)
77 if (trp
->tp
> trp
->lp
|| len
+ trp
->tp
->len
+ 2 >= sizeof(fname
))
79 strncpy(fname
+ len
, (char *) trp
->tp
->t
, trp
->tp
->len
);
89 if (trp
->tp
< trp
->lp
|| len
== 0)
94 fd
= open(fname
, O_RDONLY
);
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))
104 if (strlen(fname
) + strlen(ip
->file
) + 2 > sizeof(iname
))
106 strcpy(iname
, ip
->file
);
108 strcat(iname
, fname
);
109 if ((fd
= open(iname
, O_RDONLY
)) >= 0)
116 if (++incdepth
> NINC
)
117 error(FATAL
, "#%s too deeply nested", import
? "import" : "include");
119 genimport(fname
, angled
, iname
, import
);
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))
130 setsource((char *) newstring((uchar
*) iname
, strlen(iname
), 0), i
, fd
, NULL
, (i
>= 0) ? 1 : 0);
137 trp
->tp
= trp
->bp
+ 2;
138 error(ERROR
, "Could not find %s file %r", import
? "import" : "include", trp
);
142 error(ERROR
, "Syntax error in #%s", import
? "import" : "include");
147 * Generate a line directive for cursource
152 static Token ta
= {UNCLASS
, 0, 0, NULL
, 0};
153 static Tokenrow tr
= {&ta
, &ta
, &ta
+ 1, 1};
156 ta
.t
= p
= (uchar
*) outptr
;
157 strcpy((char *) p
, "#line ");
158 p
+= sizeof("#line ") - 1;
159 p
= (uchar
*) outnum((char *) p
, cursource
->line
);
162 if (cursource
->filename
[0] != '/' && wd
[0])
164 strcpy((char *) p
, wd
);
168 strcpy((char *) p
, cursource
->filename
);
169 p
+= strlen((char *) p
);
172 ta
.len
= (char *) p
- outptr
;
179 * Generate a pragma import/include directive
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};
188 ta
.t
= p
= (uchar
*) outptr
;
191 strcpy((char *) p
, "#pragma import");
193 strcpy((char *) p
, "#pragma include");
195 p
+= strlen((char *) p
);
199 *p
++ = angled
? '<' : '"';
200 strcpy((char *) p
, fname
);
202 *p
++ = angled
? '>' : '"';
207 strcpy((char *) p
, iname
);
214 ta
.len
= (char *) p
- outptr
;
221 * Generate an extern C directive
226 static Token ta
= {UNCLASS
, 0, 0, NULL
, 0};
227 static Tokenrow tr
= {&ta
, &ta
, &ta
+ 1, 1};
233 ta
.t
= p
= (uchar
*) outptr
;
236 strcpy((char *) p
, "extern \"C\" {");
238 strcpy((char *) p
, "}");
240 p
+= strlen((char *) p
);
244 ta
.len
= (char *) p
- outptr
;
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */