merge the formfield patch from ooo-build
[ooovba.git] / soltools / cpp / _unix.c
blob627a29fc7d8766ab6a682931d57059046326183c
1 #include <stdio.h>
2 #include <stddef.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <fcntl.h>
7 #if (defined(_WIN32) || defined(_MSDOS) || defined(__IBMC__))
8 #include <io.h>
9 #else
10 #include <unistd.h>
11 #endif
13 #include "cpp.h"
15 extern int stgetopt(int, char *const *, const char *);
16 extern char *optarg, rcsid[];
17 extern int optind;
19 int Pflag = 0; /* print no line information */
20 int Iflag = 0; /* print includes */
21 int Mflag = 0; /* print macor expansion */
22 int Aflag = 0; /* translate character sets */
23 int Xflag = 0; /* print pragma for include/import */
24 int Vflag = 0; /* verbose flag */
25 int Cflag = 0; /* do not remove any comments */
26 int Dflag = 0; /* add parameter check to delete op */
27 int Cplusplus = 0;
29 extern void setup_kwtab(void);
31 void
32 setup(int argc, char **argv)
34 int c, fd, i, n;
35 char *fp, *dp;
36 Tokenrow tr;
38 setup_kwtab();
39 while ((c = stgetopt(argc, argv, "NOPV:I:D:U:F:A:X:u:l:+")) != -1)
40 switch (c)
42 case 'N':
43 for (i = 0; i < NINCLUDE; i++)
44 if (includelist[i].always == 1)
45 includelist[i].deleted = 1;
46 break;
48 case 'I':
49 for (i = NINCLUDE - 2; i >= 0; i--)
51 if (includelist[i].file == NULL)
53 includelist[i].always = 1;
54 includelist[i].file = optarg;
55 break;
58 if (i < 0)
59 error(FATAL, "Too many -I directives");
60 break;
62 case 'D':
63 case 'U':
64 case 'A':
65 setsource("<cmdarg>", -1, -1, optarg, 0);
66 maketokenrow(3, &tr);
67 gettokens(&tr, 1);
68 doadefine(&tr, c);
69 unsetsource();
70 break;
72 case 'P': /* Lineinfo */
73 Pflag++;
74 break;
76 case 'V':
77 for (n = 0; (c = optarg[n]) != '\0'; n++)
78 switch (c)
80 case 'i':
81 Iflag++;
82 break;
84 case 'm':
85 Mflag = 1;
86 break;
88 case 'x':
89 Mflag = 2;
90 break;
92 case 't':
93 Vflag++;
94 break;
96 case 'v':
97 fprintf(stderr, "%s %s\n", argv[0], rcsid);
98 break;
100 default:
101 error(WARNING, "Unknown verbose option %c", c);
103 break;
105 case 'X':
106 for (n = 0; (c = optarg[n]) != '\0'; n++)
107 switch (c)
109 case 'a':
110 Aflag++;
111 break;
113 case 'i':
114 Xflag++;
115 break;
117 case 'c':
118 Cflag++;
119 break;
121 case 'd':
122 Dflag++;
123 break;
125 case 'w':
126 dp = &optarg[n + 1];
127 n += strlen(dp);
128 while (isspace(*dp)) dp++;
130 for (i = NINCLUDE - 1; i >= 0; i--)
132 if (wraplist[i].file == NULL)
134 wraplist[i].file = dp;
135 break;
138 if (i < 0)
139 error(WARNING, "Too many -Xw directives");
140 break;
142 default:
143 error(WARNING, "Unknown extension option %c", c);
145 break;
147 case '+':
148 Cplusplus++;
149 break;
151 case 'u': /* -undef fuer GCC (dummy) */
152 case 'l': /* -lang-c++ fuer GCC (dummy) */
153 break;
155 default:
156 break;
158 dp = ".";
159 fp = "<stdin>";
160 fd = 0;
161 if (optind < argc)
163 if ((fp = strrchr(argv[optind], '/')) != NULL)
165 int len = fp - argv[optind];
167 dp = (char *) newstring((uchar *) argv[optind], len + 1, 0);
168 dp[len] = '\0';
170 fp = (char *) newstring((uchar *) argv[optind], strlen(argv[optind]), 0);
171 if ((fd = open(fp, O_RDONLY)) <= 0)
172 error(FATAL, "Can't open input file %s", fp);
175 if (optind + 1 < argc)
177 int fdo = creat(argv[optind + 1], 0666);
179 if (fdo < 0)
180 error(FATAL, "Can't open output file %s", argv[optind + 1]);
182 dup2(fdo, 1);
184 includelist[NINCLUDE - 1].always = 0;
185 includelist[NINCLUDE - 1].file = dp;
186 setsource(fp, -1, fd, NULL, 0);
190 /* memmove is defined here because some vendors don't provide it at
191 all and others do a terrible job (like calling malloc) */
193 #if !defined(__IBMC__) && !defined(_WIN32) && !defined(__GLIBC__)
195 void *
196 memmove(void *dp, const void *sp, size_t n)
198 unsigned char *cdp, *csp;
200 if (n <= 0)
201 return 0;
202 cdp = dp;
203 csp = (unsigned char *) sp;
204 if (cdp < csp)
208 *cdp++ = *csp++;
209 } while (--n);
211 else
213 cdp += n;
214 csp += n;
217 *--cdp = *--csp;
218 } while (--n);
220 return 0;
223 #endif