nss: upgrade to release 3.73
[LibreOffice.git] / idlc / source / options.cxx
blob1b74a2367578dd97f6d43cbe00b021d15103802d
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 <sal/config.h>
22 #include <string_view>
24 #include <options.hxx>
26 #include <osl/diagnose.h>
27 #include <rtl/string.hxx>
28 #include <rtl/strbuf.hxx>
30 #include <rtl/ustring.hxx>
31 #include <osl/file.hxx>
32 #include <o3tl/char16_t2wchar_t.hxx>
34 #ifdef _WIN32
35 # if !defined WIN32_LEAN_AND_MEAN
36 # define WIN32_LEAN_AND_MEAN
37 # endif
38 # include <windows.h>
39 #endif
41 #include <stdio.h>
42 #include <string.h>
45 Options::Options(char const * progname)
46 : m_program(progname), m_stdin(false), m_verbose(false), m_quiet(false)
50 Options::~Options()
54 // static
55 bool Options::checkArgument (std::vector< std::string > & rArgs, char const * arg, size_t len)
57 bool result = ((arg != nullptr) && (len > 0));
58 OSL_PRECOND(result, "idlc::Options::checkArgument(): invalid arguments");
59 if (result)
61 switch(arg[0])
63 case '@':
64 result = len > 1;
65 if (result)
67 // "@<cmdfile>"
68 result = Options::checkCommandFile (rArgs, &(arg[1]));
70 break;
71 case '-':
72 result = len > 1;
73 if (result)
75 // "-<option>"
76 switch (arg[1])
78 case 'O':
79 case 'M':
80 case 'I':
81 case 'D':
83 // "-<option>[<param>]
84 std::string option(&(arg[0]), 2);
85 rArgs.push_back(option);
86 if (len > 2)
88 // "-<option><param>"
89 std::string param(&(arg[2]), len - 2);
90 rArgs.push_back(param);
92 break;
94 default:
95 // "-<option>" ([long] option, w/o param)
96 rArgs.emplace_back(arg, len);
97 break;
100 break;
101 default:
102 // "<param>"
103 rArgs.emplace_back(arg, len);
104 break;
107 return result;
110 // static
111 bool Options::checkCommandFile (std::vector< std::string > & rArgs, char const * filename)
113 FILE * fp = fopen(filename, "r");
114 if (fp == nullptr)
116 fprintf(stderr, "ERROR: can't open command file \"%s\"\n", filename);
117 return false;
120 std::string buffer;
121 buffer.reserve(256);
123 bool quoted = false;
124 int c = EOF;
125 while ((c = fgetc(fp)) != EOF)
127 switch(c)
129 case '\"':
130 quoted = !quoted;
131 break;
132 case ' ':
133 case '\t':
134 case '\r':
135 case '\n':
136 if (!quoted)
138 if (!buffer.empty())
140 // append current argument.
141 if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size()))
143 (void) fclose(fp);
144 return false;
146 buffer.clear();
148 break;
150 [[fallthrough]];
151 default:
152 buffer.push_back(sal::static_int_cast<char>(c));
153 break;
156 if (!buffer.empty())
158 // append unterminated argument.
159 if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size()))
161 (void) fclose(fp);
162 return false;
164 buffer.clear();
166 return (fclose(fp) == 0);
169 bool Options::badOption(char const * reason, std::string const & rArg)
171 if (reason != nullptr)
173 OStringBuffer message;
174 message.append(reason); message.append(" option '"); message.append(rArg.c_str()); message.append("'");
175 throw IllegalArgument(message.makeStringAndClear());
177 return false;
180 bool Options::setOption(char const * option, std::string const & rArg)
182 bool result = (0 == strcmp(option, rArg.c_str()));
183 if (result)
184 m_options[rArg.c_str()] = OString(rArg.c_str(), rArg.size());
185 return result;
188 #ifdef _WIN32
189 /* Helper function to convert windows paths including spaces, brackets etc. into
190 a windows short Url. The ucpp preprocessor has problems with such paths and returns
191 with error.
193 static OString convertIncPathtoShortWindowsPath(const OString& incPath) {
194 OUString path = OStringToOUString(incPath, RTL_TEXTENCODING_UTF8);
196 std::vector<sal_Unicode> vec(path.getLength() + 1);
197 //GetShortPathNameW only works if the file can be found!
198 const DWORD len = GetShortPathNameW(
199 o3tl::toW(path.getStr()), o3tl::toW(vec.data()), path.getLength() + 1);
201 if (len > 0)
203 OUString ret(vec.data(), len);
204 return OUStringToOString(ret, RTL_TEXTENCODING_UTF8);
207 return incPath;
209 #endif
211 bool Options::initOptions(std::vector< std::string > & rArgs)
213 std::vector< std::string >::const_iterator first = rArgs.begin(), last = rArgs.end();
214 for (; first != last; ++first)
216 if ((*first)[0] != '-')
218 OString filename((*first).c_str(), (*first).size());
219 OString tmp(filename.toAsciiLowerCase());
220 if (tmp.lastIndexOf(".idl") != (tmp.getLength() - 4))
222 throw IllegalArgument("'" + filename + "' is not a valid input file, only '*.idl' files will be accepted");
224 m_inputFiles.push_back(filename);
225 continue;
228 std::string const option(*first);
229 switch((*first)[1])
231 case 'O':
233 if ((++first == last) || ((*first)[0] == '-'))
235 return badOption("invalid", option);
237 OString param((*first).c_str(), (*first).size());
238 m_options["-O"] = param;
239 break;
241 case 'M':
243 if ((++first == last) || ((*first)[0] == '-'))
245 return badOption("invalid", option);
247 OString param((*first).c_str(), (*first).size());
248 m_options["-M"] = param;
249 break;
251 case 'I':
253 if ((++first == last) || ((*first)[0] == '-'))
255 return badOption("invalid", option);
257 OString param((*first).c_str(), (*first).size());
259 // quote param token(s).
260 OStringBuffer buffer;
261 sal_Int32 k = 0;
264 if (!buffer.isEmpty())
265 buffer.append(' ');
266 // buffer.append("-I\"");
267 #ifdef _WIN32
268 OString incpath = convertIncPathtoShortWindowsPath(param.getToken(0, ';', k));
269 #else
270 OString incpath = param.getToken(0, ';', k);
271 #endif
272 buffer.append(incpath);
273 // buffer.append("\"");
274 } while (k != -1);
275 param = buffer.makeStringAndClear();
277 if (m_options.count("-I") > 0)
279 // append param.
280 param = m_options["-I"] + " " + param;
282 m_options["-I"] = param;
283 break;
285 case 'D':
287 if ((++first == last) || ((*first)[0] == '-'))
289 return badOption("invalid", option);
291 OString param = OString::Concat("-D") + std::string_view((*first).c_str(), (*first).size());
292 if (m_options.count("-D") > 0)
294 param = m_options["-D"] + " " + param;
296 m_options["-D"] = param;
297 break;
299 case 'C':
301 if (!setOption("-C", option))
303 return badOption("invalid", option);
305 break;
307 case 'c':
309 if (!setOption("-cid", option))
311 return badOption("invalid", option);
313 break;
315 case 'q':
317 if (!setOption("-quiet", option))
319 return badOption("invalid", option);
321 m_quiet = true;
322 break;
324 case 'v':
326 if (!setOption("-verbose", option))
328 return badOption("invalid", option);
330 m_verbose = true;
331 break;
333 case 'w':
335 if (!(setOption("-w", option) || setOption("-we", option)))
337 return badOption("invalid", option);
339 break;
341 case 'h':
342 case '?':
344 if (!(setOption("-h", option) || setOption("-?", option)))
346 return badOption("invalid", option);
349 (void) fprintf(stdout, "%s", prepareHelp().getStr());
350 return false;
352 // break; // Unreachable
354 case 's':
356 if (!setOption("-stdin", option))
358 return badOption("invalid", option);
360 m_stdin = true;
361 break;
363 default:
364 return badOption("unknown", option);
367 return true;
370 OString Options::prepareHelp() const
372 OString help = "\nusing: " +
373 m_program + " [-options] <file_1> ... <file_n> | @<filename> | -stdin\n"
374 " <file_n> = file_n specifies one or more idl files.\n"
375 " Only files with the extension '.idl' are valid.\n"
376 " @<filename> = filename specifies the name of a command file.\n"
377 " -stdin = read idl file from standard input.\n"
378 " Options:\n"
379 " -O<path> = path specifies the output directory.\n"
380 " The generated output is a registry file with\n"
381 " the same name as the idl input file (or 'stdin'\n"
382 " for -stdin).\n"
383 " -M<path> = path specifies the output directory for deps.\n"
384 " Generate GNU make dependency files with the\n"
385 " same name as the idl input file.\n"
386 " -I<path> = path specifies a directory where include\n"
387 " files will be searched by the preprocessor.\n"
388 " Multiple directories can be combined with ';'.\n"
389 " -D<name> = name defines a macro for the preprocessor.\n"
390 " -C = generate complete type information, including\n"
391 " documentation.\n"
392 " -cid = check if identifiers fulfill the UNO naming\n"
393 " requirements.\n"
394 " -quiet = no output.\n"
395 " -verbose = verbose output.\n"
396 " -w = display warning messages.\n"
397 " -we = treat warnings as errors.\n"
398 " -h|-? = print this help message and exit.\n\n" +
399 prepareVersion();
401 return help;
404 OString Options::prepareVersion() const
406 return m_program + " Version 1.1\n\n";
410 bool Options::isValid(const OString& option) const
412 return (m_options.count(option) > 0);
415 const OString& Options::getOption(const OString& option)
417 if (!isValid(option))
419 throw IllegalArgument("Option is not valid or currently not set.");
421 return m_options[option];
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */