1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
35 # if !defined WIN32_LEAN_AND_MEAN
36 # define WIN32_LEAN_AND_MEAN
45 Options::Options(char const * progname
)
46 : m_program(progname
), m_stdin(false), m_verbose(false), m_quiet(false)
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");
68 result
= Options::checkCommandFile (rArgs
, &(arg
[1]));
83 // "-<option>[<param>]
84 std::string
option(&(arg
[0]), 2);
85 rArgs
.push_back(option
);
89 std::string
param(&(arg
[2]), len
- 2);
90 rArgs
.push_back(param
);
95 // "-<option>" ([long] option, w/o param)
96 rArgs
.emplace_back(arg
, len
);
103 rArgs
.emplace_back(arg
, len
);
111 bool Options::checkCommandFile (std::vector
< std::string
> & rArgs
, char const * filename
)
113 FILE * fp
= fopen(filename
, "r");
116 fprintf(stderr
, "ERROR: can't open command file \"%s\"\n", filename
);
125 while ((c
= fgetc(fp
)) != EOF
)
140 // append current argument.
141 if (!Options::checkArgument(rArgs
, buffer
.c_str(), buffer
.size()))
152 buffer
.push_back(sal::static_int_cast
<char>(c
));
158 // append unterminated argument.
159 if (!Options::checkArgument(rArgs
, buffer
.c_str(), buffer
.size()))
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());
180 bool Options::setOption(char const * option
, std::string
const & rArg
)
182 bool result
= (0 == strcmp(option
, rArg
.c_str()));
184 m_options
[rArg
.c_str()] = OString(rArg
.c_str(), rArg
.size());
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
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);
203 OUString
ret(vec
.data(), len
);
204 return OUStringToOString(ret
, RTL_TEXTENCODING_UTF8
);
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
);
228 std::string
const option(*first
);
233 if ((++first
== last
) || ((*first
)[0] == '-'))
235 return badOption("invalid", option
);
237 OString
param((*first
).c_str(), (*first
).size());
238 m_options
["-O"] = param
;
243 if ((++first
== last
) || ((*first
)[0] == '-'))
245 return badOption("invalid", option
);
247 OString
param((*first
).c_str(), (*first
).size());
248 m_options
["-M"] = param
;
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
;
264 if (!buffer
.isEmpty())
266 // buffer.append("-I\"");
268 OString incpath
= convertIncPathtoShortWindowsPath(param
.getToken(0, ';', k
));
270 OString incpath
= param
.getToken(0, ';', k
);
272 buffer
.append(incpath
);
273 // buffer.append("\"");
275 param
= buffer
.makeStringAndClear();
277 if (m_options
.count("-I") > 0)
280 param
= m_options
["-I"] + " " + param
;
282 m_options
["-I"] = param
;
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
;
301 if (!setOption("-C", option
))
303 return badOption("invalid", option
);
309 if (!setOption("-cid", option
))
311 return badOption("invalid", option
);
317 if (!setOption("-quiet", option
))
319 return badOption("invalid", option
);
326 if (!setOption("-verbose", option
))
328 return badOption("invalid", option
);
335 if (!(setOption("-w", option
) || setOption("-we", option
)))
337 return badOption("invalid", option
);
344 if (!(setOption("-h", option
) || setOption("-?", option
)))
346 return badOption("invalid", option
);
349 (void) fprintf(stdout
, "%s", prepareHelp().getStr());
352 // break; // Unreachable
356 if (!setOption("-stdin", option
))
358 return badOption("invalid", option
);
364 return badOption("unknown", option
);
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"
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"
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"
392 " -cid = check if identifiers fulfill the UNO naming\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" +
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: */