merge the formfield patch from ooo-build
[ooovba.git] / idlc / source / preproc / unix.c
blob850a557c6ed28004faf74e108bd19f117625fae0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unix.c,v $
10 * $Revision: 1.9 $
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 #include <stdio.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <fcntl.h>
36 #if (defined(_WIN32) || defined(_MSDOS) || defined(__IBMC__))
37 #include <io.h>
38 #include <sys/stat.h>
39 #else
40 #include <unistd.h>
41 #endif
43 #include "cpp.h"
45 extern int cppgetopt(int, char *const *, const char *);
46 extern char *optarg, rcsid[];
47 extern int optind;
49 int Pflag = 0; /* print no line information */
50 int Iflag = 0; /* print includes */
51 int Mflag = 0; /* print macor expansion */
52 int Aflag = 0; /* translate character sets */
53 int Xflag = 0; /* print pragma for include/import */
54 int Vflag = 0; /* verbose flag */
55 int Cflag = 0; /* do not remove any comments */
56 int Cplusplus = 0;
58 extern void setup_kwtab(void);
60 void
61 setup(int argc, char **argv)
63 int c, fd, i, n;
64 char *fp, *dp;
65 Tokenrow tr;
67 setup_kwtab();
68 while ((c = cppgetopt(argc, argv, "NOPV:I:D:U:F:A:X:u:l:+")) != -1)
69 switch (c)
71 case 'N':
72 for (i = 0; i < NINCLUDE; i++)
73 if (includelist[i].always == 1)
74 includelist[i].deleted = 1;
75 break;
77 case 'I':
78 for (i = NINCLUDE - 2; i >= 0; i--)
80 if (includelist[i].file == NULL)
82 includelist[i].always = 1;
83 includelist[i].file = optarg;
84 break;
87 if (i < 0)
88 error(FATAL, "Too many -I directives");
89 break;
91 case 'D':
92 case 'U':
93 case 'A':
94 setsource("<cmdarg>", -1, -1, optarg, 0);
95 maketokenrow(3, &tr);
96 gettokens(&tr, 1);
97 doadefine(&tr, c);
98 unsetsource();
99 break;
101 case 'P': /* Lineinfo */
102 Pflag++;
103 break;
105 case 'V':
106 for (n = 0; (c = optarg[n]) != '\0'; n++)
107 switch (c)
109 case 'i':
110 Iflag++;
111 break;
113 case 'm':
114 Mflag = 1;
115 break;
117 case 'x':
118 Mflag = 2;
119 break;
121 case 't':
122 Vflag++;
123 break;
125 case 'v':
126 fprintf(stderr, "%s %s\n", argv[0], rcsid);
127 break;
129 default:
130 error(WARNING, "Unknown verbose option %c", c);
132 break;
134 case 'X':
135 for (n = 0; (c = optarg[n]) != '\0'; n++)
136 switch (c)
138 case 'a':
139 Aflag++;
140 break;
142 case 'i':
143 Xflag++;
144 break;
146 case 'c':
147 Cflag++;
148 break;
150 case 'w':
151 dp = &optarg[n + 1];
152 n += strlen(dp);
153 while (isspace(*dp)) dp++;
155 for (i = NINCLUDE - 1; i >= 0; i--)
157 if (wraplist[i].file == NULL)
159 wraplist[i].file = dp;
160 break;
163 if (i < 0)
164 error(WARNING, "Too many -Xw directives");
165 break;
167 default:
168 error(WARNING, "Unknown extension option %c", c);
170 break;
172 case '+':
173 Cplusplus++;
174 break;
176 case 'u': /* -undef fuer GCC (dummy) */
177 case 'l': /* -lang-c++ fuer GCC (dummy) */
178 break;
180 default:
181 break;
183 dp = ".";
184 fp = "<stdin>";
185 fd = 0;
186 if (optind < argc)
188 if ((fp = strrchr(argv[optind], '/')) != NULL)
190 int len = fp - argv[optind];
192 dp = (char *) newstring((uchar *) argv[optind], len + 1, 0);
193 dp[len] = '\0';
195 fp = (char *) newstring((uchar *) argv[optind], strlen(argv[optind]), 0);
196 if ((fd = open(fp, O_RDONLY)) < 0)
197 error(FATAL, "Can't open input file %s", fp);
200 if (optind + 1 < argc)
202 #if defined(WNT) && (_MSC_VER >= 1400)
203 int fdo = creat(argv[optind + 1], _S_IREAD | _S_IWRITE );
204 #else
205 int fdo = creat(argv[optind + 1], 0666 );
206 #endif
207 if (fdo < 0)
208 error(FATAL, "Can't open output file %s", argv[optind + 1]);
210 dup2(fdo, 1);
212 includelist[NINCLUDE - 1].always = 0;
213 includelist[NINCLUDE - 1].file = dp;
214 setsource(fp, -1, fd, NULL, 0);
218 /* memmove is defined here because some vendors don't provide it at
219 all and others do a terrible job (like calling malloc) */
221 #if !defined(__IBMC__) && !defined(WNT) && !defined(__GLIBC__)
223 void *
224 memmove(void *dp, const void *sp, size_t n)
226 unsigned char *cdp, *csp;
228 if (n <= 0)
229 return 0;
230 cdp = dp;
231 csp = (unsigned char *) sp;
232 if (cdp < csp)
236 *cdp++ = *csp++;
237 } while (--n);
239 else
241 cdp += n;
242 csp += n;
245 *--cdp = *--csp;
246 } while (--n);
248 return 0;
251 #endif