Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / soltools / cpp / _unix.c
blobac4b7624a00a1fdacedf55308b21e6415bfa001d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <stdio.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #if (defined(_WIN32) || defined(__IBMC__))
27 #include <io.h>
28 #else
29 #include <unistd.h>
30 #endif
32 #include "cpp.h"
34 #if defined(MACOSX) || defined(AIX) || defined(_WIN32)
35 #include "_getopt.h"
36 #else
37 #include <getopt.h>
38 #endif
40 int Pflag = 0; /* print no line information */
41 int Iflag = 0; /* print includes */
42 int Mflag = 0; /* print macro expansion */
43 int Aflag = 0; /* translate character sets */
44 int Xflag = 0; /* print pragma for include/import */
45 int Vflag = 0; /* verbose flag */
46 int Cflag = 0; /* do not remove any comments */
47 int Dflag = 0; /* add parameter check to delete op */
48 int Cplusplus = 0;
50 void
51 setup(int argc, char **argv)
53 int c, fd, i, n;
54 char *fp, *dp;
55 Tokenrow tr;
57 setup_kwtab();
58 #if defined(MACOSX) || defined(AIX) || defined(_WIN32)
59 while ((c = stgetopt(argc, argv, "NOPV:I:D:U:F:A:X:u:l:+")) != -1)
60 #else
61 while ((c = getopt(argc, argv, "NOPV:I:D:U:F:A:X:u:l:+")) != -1)
62 #endif
63 switch (c)
65 case 'N':
66 for (i = 0; i < NINCLUDE; i++)
67 if (includelist[i].always == 1)
68 includelist[i].deleted = 1;
69 break;
71 case 'I':
72 for (i = NINCLUDE - 2; i >= 0; i--)
74 if (includelist[i].file == NULL)
76 includelist[i].always = 1;
77 includelist[i].file = optarg;
78 break;
81 if (i < 0)
82 error(FATAL, "Too many -I directives");
83 break;
85 case 'D':
86 case 'U':
87 case 'A':
88 setsource("<cmdarg>", -1, -1, optarg, 0);
89 maketokenrow(3, &tr);
90 // coverity[overrun-buffer-arg: FALSE] - a multiple of trp->max is allocated, not trp->max itself
91 gettokens(&tr, 1);
92 doadefine(&tr, c);
93 dofree(tr.bp);
94 unsetsource();
95 break;
97 case 'P': /* Lineinfo */
98 Pflag++;
99 break;
101 case 'V':
102 for (n = 0; (c = optarg[n]) != '\0'; n++)
103 switch (c)
105 case 'i':
106 Iflag++;
107 break;
109 case 'm':
110 Mflag = 1;
111 break;
113 case 'x':
114 Mflag = 2;
115 break;
117 case 't':
118 Vflag++;
119 break;
121 case 'v':
122 fprintf(stderr, "%s\n", argv[0]);
123 break;
125 default:
126 error(WARNING, "Unknown verbose option %c", c);
128 break;
130 case 'X':
131 for (n = 0; (c = optarg[n]) != '\0'; n++)
132 switch (c)
134 case 'a':
135 Aflag++;
136 break;
138 case 'i':
139 Xflag++;
140 break;
142 case 'c':
143 Cflag++;
144 break;
146 case 'd':
147 Dflag++;
148 break;
150 case 'w':
151 dp = &optarg[n + 1];
152 n += (int)strlen(dp);
153 while (isspace((unsigned char)*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 for GCC (dummy) */
177 case 'l': /* -lang-c++ for 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 = (int)(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 int fdo = creat(argv[optind + 1], 0666);
204 if (fdo < 0)
205 error(FATAL, "Can't open output file %s", argv[optind + 1]);
207 dup2(fdo, 1);
208 // coverity[leaked_handle] - on purpose
210 includelist[NINCLUDE - 1].always = 0;
211 includelist[NINCLUDE - 1].file = dp;
212 setsource(fp, -1, fd, NULL, 0);
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */