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__))
34 #if defined(__IBMC__) || defined(__EMX__) || defined(_MSC_VER)
37 # define PATH_MAX _MAX_PATH
44 Includelist includelist
[NINCLUDE
];
45 Wraplist wraplist
[NINCLUDE
];
48 doinclude(Tokenrow
* trp
, int depth
, int import
)
50 char fname
[PATH_MAX
], iname
[PATH_MAX
];
56 if (trp
->tp
>= trp
->lp
)
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
);
74 if (trp
->tp
->type
== LT
)
78 while (trp
->tp
->type
!= GT
)
80 if (trp
->tp
> trp
->lp
|| len
+ trp
->tp
->len
+ 2 >= sizeof(fname
))
82 strncpy(fname
+ len
, (char *) trp
->tp
->t
, trp
->tp
->len
);
92 if (trp
->tp
< trp
->lp
|| len
== 0)
97 fd
= open(fname
, O_RDONLY
);
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))
107 if (strlen(fname
) + strlen(ip
->file
) + 2 > sizeof(iname
))
109 strcpy(iname
, ip
->file
);
111 strcat(iname
, fname
);
112 if ((fd
= open(iname
, O_RDONLY
)) >= 0)
119 if (++incdepth
> NINC
)
120 error(FATAL
, "#%s too deeply nested", import
? "import" : "include");
122 genimport(fname
, angled
, iname
, import
);
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))
133 setsource((char *) newstring((uchar
*) iname
, strlen(iname
), 0), i
, fd
, NULL
, (i
>= 0) ? 1 : 0);
140 trp
->tp
= trp
->bp
+ 2;
141 error(ERROR
, "Could not find %s file %r", import
? "import" : "include", trp
);
145 error(ERROR
, "Syntax error in #%s", import
? "import" : "include");
150 * Generate a line directive for cursource
155 static Token ta
= {UNCLASS
, 0, 0, 0, NULL
, 0};
156 static Tokenrow tr
= {&ta
, &ta
, &ta
+ 1, 1};
159 ta
.t
= p
= (uchar
*) outptr
;
160 strcpy((char *) p
, "#line ");
161 p
+= sizeof("#line ") - 1;
162 p
= (uchar
*) outnum((char *) p
, cursource
->line
);
165 if (cursource
->filename
[0] != '/' && wd
[0])
167 strcpy((char *) p
, wd
);
171 strcpy((char *) p
, cursource
->filename
);
172 p
+= strlen((char *) p
);
175 ta
.len
= (char *) p
- outptr
;
182 * Generate a pragma import/include directive
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};
191 ta
.t
= p
= (uchar
*) outptr
;
194 strcpy((char *) p
, "#pragma import");
196 strcpy((char *) p
, "#pragma include");
198 p
+= strlen((char *) p
);
202 *p
++ = angled
? '<' : '"';
203 strcpy((char *) p
, fname
);
205 *p
++ = angled
? '>' : '"';
210 strcpy((char *) p
, iname
);
217 ta
.len
= (char *) p
- outptr
;
224 * Generate a extern C directive
229 static Token ta
= {UNCLASS
, 0, 0, 0, NULL
, 0};
230 static Tokenrow tr
= {&ta
, &ta
, &ta
+ 1, 1};
235 ta
.t
= p
= (uchar
*) outptr
;
238 strcpy((char *) p
, "extern \"C\" {");
240 strcpy((char *) p
, "}");
242 p
+= strlen((char *) p
);
246 ta
.len
= (char *) p
- outptr
;
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */