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 OString message
= OString::Concat(reason
) + " option '" + rArg
.c_str() + "'";
174 throw IllegalArgument(message
);
179 bool Options::setOption(char const * option
, std::string
const & rArg
)
181 bool result
= (0 == strcmp(option
, rArg
.c_str()));
183 m_options
[rArg
.c_str()] = OString(rArg
.c_str(), rArg
.size());
188 /* Helper function to convert windows paths including spaces, brackets etc. into
189 a windows short Url. The ucpp preprocessor has problems with such paths and returns
192 static OString
convertIncPathtoShortWindowsPath(const OString
& incPath
) {
193 OUString path
= OStringToOUString(incPath
, RTL_TEXTENCODING_UTF8
);
195 std::vector
<sal_Unicode
> vec(path
.getLength() + 1);
196 //GetShortPathNameW only works if the file can be found!
197 const DWORD len
= GetShortPathNameW(
198 o3tl::toW(path
.getStr()), o3tl::toW(vec
.data()), path
.getLength() + 1);
202 OUString
ret(vec
.data(), len
);
203 return OUStringToOString(ret
, RTL_TEXTENCODING_UTF8
);
210 bool Options::initOptions(std::vector
< std::string
> & rArgs
)
212 std::vector
< std::string
>::const_iterator first
= rArgs
.begin(), last
= rArgs
.end();
213 for (; first
!= last
; ++first
)
215 if ((*first
)[0] != '-')
217 OString
filename((*first
).c_str(), (*first
).size());
218 OString
tmp(filename
.toAsciiLowerCase());
219 if (tmp
.lastIndexOf(".idl") != (tmp
.getLength() - 4))
221 throw IllegalArgument("'" + filename
+ "' is not a valid input file, only '*.idl' files will be accepted");
223 m_inputFiles
.push_back(filename
);
227 std::string
const option(*first
);
232 if ((++first
== last
) || ((*first
)[0] == '-'))
234 return badOption("invalid", option
);
236 OString
param((*first
).c_str(), (*first
).size());
237 m_options
["-O"] = param
;
242 if ((++first
== last
) || ((*first
)[0] == '-'))
244 return badOption("invalid", option
);
246 OString
param((*first
).c_str(), (*first
).size());
247 m_options
["-M"] = param
;
252 if ((++first
== last
) || ((*first
)[0] == '-'))
254 return badOption("invalid", option
);
256 OString
param((*first
).c_str(), (*first
).size());
258 // quote param token(s).
259 OStringBuffer buffer
;
263 if (!buffer
.isEmpty())
265 // buffer.append("-I\"");
267 OString incpath
= convertIncPathtoShortWindowsPath(param
.getToken(0, ';', k
));
269 OString incpath
= param
.getToken(0, ';', k
);
271 buffer
.append(incpath
);
272 // buffer.append("\"");
274 param
= buffer
.makeStringAndClear();
276 if (m_options
.count("-I") > 0)
279 param
= m_options
["-I"] + " " + param
;
281 m_options
["-I"] = param
;
286 if ((++first
== last
) || ((*first
)[0] == '-'))
288 return badOption("invalid", option
);
290 OString param
= OString::Concat("-D") + std::string_view((*first
).c_str(), (*first
).size());
291 if (m_options
.count("-D") > 0)
293 param
= m_options
["-D"] + " " + param
;
295 m_options
["-D"] = param
;
300 if (!setOption("-C", option
))
302 return badOption("invalid", option
);
308 if (!setOption("-cid", option
))
310 return badOption("invalid", option
);
316 if (!setOption("-quiet", option
))
318 return badOption("invalid", option
);
325 if (!setOption("-verbose", option
))
327 return badOption("invalid", option
);
334 if (!(setOption("-w", option
) || setOption("-we", option
)))
336 return badOption("invalid", option
);
343 if (!(setOption("-h", option
) || setOption("-?", option
)))
345 return badOption("invalid", option
);
348 (void) fprintf(stdout
, "%s", prepareHelp().getStr());
351 // break; // Unreachable
355 if (!setOption("-stdin", option
))
357 return badOption("invalid", option
);
363 return badOption("unknown", option
);
369 OString
Options::prepareHelp() const
371 OString help
= "\nusing: " +
372 m_program
+ " [-options] <file_1> ... <file_n> | @<filename> | -stdin\n"
373 " <file_n> = file_n specifies one or more idl files.\n"
374 " Only files with the extension '.idl' are valid.\n"
375 " @<filename> = filename specifies the name of a command file.\n"
376 " -stdin = read idl file from standard input.\n"
378 " -O<path> = path specifies the output directory.\n"
379 " The generated output is a registry file with\n"
380 " the same name as the idl input file (or 'stdin'\n"
382 " -M<path> = path specifies the output directory for deps.\n"
383 " Generate GNU make dependency files with the\n"
384 " same name as the idl input file.\n"
385 " -I<path> = path specifies a directory where include\n"
386 " files will be searched by the preprocessor.\n"
387 " Multiple directories can be combined with ';'.\n"
388 " -D<name> = name defines a macro for the preprocessor.\n"
389 " -C = generate complete type information, including\n"
391 " -cid = check if identifiers fulfill the UNO naming\n"
393 " -quiet = no output.\n"
394 " -verbose = verbose output.\n"
395 " -w = display warning messages.\n"
396 " -we = treat warnings as errors.\n"
397 " -h|-? = print this help message and exit.\n\n" +
403 OString
Options::prepareVersion() const
405 return m_program
+ " Version 1.1\n\n";
409 bool Options::isValid(const OString
& option
) const
411 return (m_options
.count(option
) > 0);
414 const OString
& Options::getOption(const OString
& option
)
416 if (!isValid(option
))
418 throw IllegalArgument("Option is not valid or currently not set.");
420 return m_options
[option
];
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */