1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: include.c,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #if (defined(_WIN32) || defined(_MSDOS) || defined(__IBMC__))
53 Includelist includelist
[NINCLUDE
];
54 Wraplist wraplist
[NINCLUDE
];
57 doinclude(Tokenrow
* trp
, int depth
, int import
)
60 char fname
[_MAX_PATH
], iname
[_MAX_PATH
];
62 char fname
[PATH_MAX
], iname
[PATH_MAX
];
65 int angled
, len
, fd
, i
;
68 if (trp
->tp
>= trp
->lp
)
70 if (trp
->tp
->type
!= STRING
&& trp
->tp
->type
!= LT
)
72 len
= trp
->tp
- trp
->bp
;
73 expandrow(trp
, "<include>");
74 trp
->tp
= trp
->bp
+ len
;
76 if (trp
->tp
->type
== STRING
)
78 len
= trp
->tp
->len
- 2;
79 if (len
> (int)sizeof(fname
) - 1)
80 len
= sizeof(fname
) - 1;
81 strncpy(fname
, (char *) trp
->tp
->t
+ 1, len
);
85 if (trp
->tp
->type
== LT
)
89 while (trp
->tp
->type
!= GT
)
91 if (trp
->tp
> trp
->lp
|| len
+ trp
->tp
->len
+ 2 >= sizeof(fname
))
93 strncpy(fname
+ len
, (char *) trp
->tp
->t
, trp
->tp
->len
);
102 if (trp
->tp
< trp
->lp
|| len
== 0)
107 fd
= open(fname
, O_RDONLY
);
108 strcpy(iname
, fname
);
111 for (fd
= -1, i
= (depth
< 0) ? (NINCLUDE
- 1) : (depth
- 1); i
>= 0; i
--)
113 ip
= &includelist
[i
];
114 if (ip
->file
== NULL
|| ip
->deleted
|| (angled
&& ip
->always
== 0))
116 if (strlen(fname
) + strlen(ip
->file
) + 2 > sizeof(iname
))
118 strcpy(iname
, ip
->file
);
120 strcat(iname
, fname
);
121 if ((fd
= open(iname
, O_RDONLY
)) >= 0)
126 if (++incdepth
> NINC
)
127 error(FATAL
, "#%s too deeply nested", import
? "import" : "include");
129 genimport(fname
, angled
, iname
, import
);
131 error(INFO
, "Open %s file [%s]", import
? "import" : "include", iname
);
133 for (i
= NINCLUDE
- 1; i
>= 0; i
--)
135 if ((wraplist
[i
].file
!= NULL
) &&
136 (strncmp(wraplist
[i
].file
, iname
, strlen(wraplist
[i
].file
)) == 0))
140 setsource((char *) newstring((uchar
*) iname
, strlen(iname
), 0), i
, fd
, NULL
, (i
>= 0) ? 1 : 0);
147 trp
->tp
= trp
->bp
+ 2;
148 error(ERROR
, "Could not find %s file %r", import
? "import" : "include", trp
);
152 error(ERROR
, "Syntax error in #%s", import
? "import" : "include");
157 * Generate a line directive for cursource
162 static Token ta
= {UNCLASS
, 0, 0, 0, NULL
};
163 static Tokenrow tr
= {&ta
, &ta
, &ta
+ 1, 1};
166 ta
.t
= p
= (uchar
*) outptr
;
167 strcpy((char *) p
, "#line ");
168 p
+= sizeof("#line ") - 1;
169 p
= (uchar
*) outnum((char *) p
, cursource
->line
);
172 if (cursource
->filename
[0] != '/' && wd
[0])
174 strcpy((char *) p
, wd
);
178 strcpy((char *) p
, cursource
->filename
);
179 p
+= strlen((char *) p
);
182 ta
.len
= (char *) p
- outptr
;
189 * Generate a pragma import/include directive
192 genimport(char *fname
, int angled
, char *iname
, int import
)
194 static Token ta
= {UNCLASS
, 0, 0, 0, NULL
};
195 static Tokenrow tr
= {&ta
, &ta
, &ta
+ 1, 1};
198 ta
.t
= p
= (uchar
*) outptr
;
201 strcpy((char *) p
, "#pragma import");
203 strcpy((char *) p
, "#pragma include");
205 p
+= strlen((char *) p
);
209 *p
++ = angled
? '<' : '"';
210 strcpy((char *) p
, fname
);
212 *p
++ = angled
? '>' : '"';
217 strcpy((char *) p
, iname
);
224 ta
.len
= (char *) p
- outptr
;
231 * Generate a extern C directive
236 static Token ta
= {UNCLASS
, 0, 0, 0, NULL
};
237 static Tokenrow tr
= {&ta
, &ta
, &ta
+ 1, 1};
242 ta
.t
= p
= (uchar
*) outptr
;
245 strcpy((char *) p
, "extern \"C\" {");
247 strcpy((char *) p
, "}");
249 p
+= strlen((char *) p
);
253 ta
.len
= (char *) p
- outptr
;