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 .
21 #include <options.hxx>
23 #include <osl/diagnose.h>
24 #include <rtl/string.hxx>
25 #include <rtl/strbuf.hxx>
27 #include <rtl/ustring.hxx>
28 #include <osl/file.hxx>
29 #include <o3tl/char16_t2wchar_t.hxx>
32 # if !defined WIN32_LEAN_AND_MEAN
33 # define WIN32_LEAN_AND_MEAN
42 Options::Options(char const * progname
)
43 : m_program(progname
), m_stdin(false), m_verbose(false), m_quiet(false)
52 bool Options::checkArgument (std::vector
< std::string
> & rArgs
, char const * arg
, size_t len
)
54 bool result
= ((arg
!= nullptr) && (len
> 0));
55 OSL_PRECOND(result
, "idlc::Options::checkArgument(): invalid arguments");
65 result
= Options::checkCommandFile (rArgs
, &(arg
[1]));
80 // "-<option>[<param>]
81 std::string
option(&(arg
[0]), 2);
82 rArgs
.push_back(option
);
86 std::string
param(&(arg
[2]), len
- 2);
87 rArgs
.push_back(param
);
92 // "-<option>" ([long] option, w/o param)
93 rArgs
.emplace_back(arg
, len
);
100 rArgs
.emplace_back(arg
, len
);
108 bool Options::checkCommandFile (std::vector
< std::string
> & rArgs
, char const * filename
)
110 FILE * fp
= fopen(filename
, "r");
113 fprintf(stderr
, "ERROR: can't open command file \"%s\"\n", filename
);
122 while ((c
= fgetc(fp
)) != EOF
)
137 // append current argument.
138 if (!Options::checkArgument(rArgs
, buffer
.c_str(), buffer
.size()))
149 buffer
.push_back(sal::static_int_cast
<char>(c
));
155 // append unterminated argument.
156 if (!Options::checkArgument(rArgs
, buffer
.c_str(), buffer
.size()))
163 return (fclose(fp
) == 0);
166 bool Options::badOption(char const * reason
, std::string
const & rArg
)
168 OStringBuffer message
;
169 if (reason
!= nullptr)
171 message
.append(reason
); message
.append(" option '"); message
.append(rArg
.c_str()); message
.append("'");
172 throw IllegalArgument(message
.makeStringAndClear());
177 bool Options::setOption(char const * option
, std::string
const & rArg
)
179 bool result
= (0 == strcmp(option
, rArg
.c_str()));
181 m_options
[rArg
.c_str()] = OString(rArg
.c_str(), rArg
.size());
186 /* Helper function to convert windows paths including spaces, brackets etc. into
187 a windows short Url. The ucpp preprocessor has problems with such paths and returns
190 OString
convertIncPathtoShortWindowsPath(const OString
& incPath
) {
191 OUString path
= OStringToOUString(incPath
, RTL_TEXTENCODING_UTF8
);
193 std::vector
<sal_Unicode
> vec(path
.getLength() + 1);
194 //GetShortPathNameW only works if the file can be found!
195 const DWORD len
= GetShortPathNameW(
196 o3tl::toW(path
.getStr()), o3tl::toW(&vec
[0]), path
.getLength() + 1);
200 OUString
ret(&vec
[0], len
);
201 return OUStringToOString(ret
, RTL_TEXTENCODING_UTF8
);
208 bool Options::initOptions(std::vector
< std::string
> & rArgs
)
210 std::vector
< std::string
>::const_iterator first
= rArgs
.begin(), last
= rArgs
.end();
211 for (; first
!= last
; ++first
)
213 if ((*first
)[0] != '-')
215 OString
filename((*first
).c_str(), (*first
).size());
216 OString
tmp(filename
.toAsciiLowerCase());
217 if (tmp
.lastIndexOf(".idl") != (tmp
.getLength() - 4))
219 throw IllegalArgument("'" + filename
+ "' is not a valid input file, only '*.idl' files will be accepted");
221 m_inputFiles
.push_back(filename
);
225 std::string
const option(*first
);
230 if (!((++first
!= last
) && ((*first
)[0] != '-')))
232 return badOption("invalid", option
);
234 OString
param((*first
).c_str(), (*first
).size());
235 m_options
["-O"] = param
;
240 if (!((++first
!= last
) && ((*first
)[0] != '-')))
242 return badOption("invalid", option
);
244 OString
param((*first
).c_str(), (*first
).size());
245 m_options
["-M"] = param
;
250 if (!((++first
!= last
) && ((*first
)[0] != '-')))
252 return badOption("invalid", option
);
254 OString
param((*first
).c_str(), (*first
).size());
256 // quote param token(s).
257 OStringBuffer buffer
;
261 if (!buffer
.isEmpty())
263 // buffer.append("-I\"");
265 OString incpath
= convertIncPathtoShortWindowsPath(param
.getToken(0, ';', k
));
267 OString incpath
= param
.getToken(0, ';', k
);
269 buffer
.append(incpath
);
270 // buffer.append("\"");
272 param
= buffer
.makeStringAndClear();
274 if (m_options
.count("-I") > 0)
277 OStringBuffer
buffer(m_options
["-I"]);
278 buffer
.append(' '); buffer
.append(param
);
279 param
= buffer
.makeStringAndClear();
281 m_options
["-I"] = param
;
286 if (!((++first
!= last
) && ((*first
)[0] != '-')))
288 return badOption("invalid", option
);
290 OString
param("-D"); param
+= OString((*first
).c_str(), (*first
).size());
291 if (m_options
.count("-D") > 0)
293 OStringBuffer
buffer(m_options
["-D"]);
294 buffer
.append(' '); buffer
.append(param
);
295 param
= buffer
.makeStringAndClear();
297 m_options
["-D"] = param
;
302 if (!setOption("-C", option
))
304 return badOption("invalid", option
);
310 if (!setOption("-cid", option
))
312 return badOption("invalid", option
);
318 if (!setOption("-quiet", option
))
320 return badOption("invalid", option
);
327 if (!setOption("-verbose", option
))
329 return badOption("invalid", option
);
336 if (!(setOption("-w", option
) || setOption("-we", option
)))
338 return badOption("invalid", option
);
345 if (!(setOption("-h", option
) || setOption("-?", option
)))
347 return badOption("invalid", option
);
350 (void) fprintf(stdout
, "%s", prepareHelp().getStr());
353 // break; // Unreachable
357 if (!setOption("-stdin", option
))
359 return badOption("invalid", option
);
365 return badOption("unknown", option
);
371 OString
Options::prepareHelp() const
373 OString
help("\nusing: ");
374 help
+= m_program
+ " [-options] <file_1> ... <file_n> | @<filename> | -stdin\n";
375 help
+= " <file_n> = file_n specifies one or more idl files.\n";
376 help
+= " Only files with the extension '.idl' are valid.\n";
377 help
+= " @<filename> = filename specifies the name of a command file.\n";
378 help
+= " -stdin = read idl file from standard input.\n";
379 help
+= " Options:\n";
380 help
+= " -O<path> = path specifies the output directory.\n";
381 help
+= " The generated output is a registry file with\n";
382 help
+= " the same name as the idl input file (or 'stdin'\n";
383 help
+= " for -stdin).\n";
384 help
+= " -M<path> = path specifies the output directory for deps.\n";
385 help
+= " Generate GNU make dependency files with the\n";
386 help
+= " same name as the idl input file.\n";
387 help
+= " -I<path> = path specifies a directory where include\n";
388 help
+= " files will be searched by the preprocessor.\n";
389 help
+= " Multiple directories can be combined with ';'.\n";
390 help
+= " -D<name> = name defines a macro for the preprocessor.\n";
391 help
+= " -C = generate complete type information, including\n";
392 help
+= " documentation.\n";
393 help
+= " -cid = check if identifiers fulfill the UNO naming\n";
394 help
+= " requirements.\n";
395 help
+= " -quiet = no output.\n";
396 help
+= " -verbose = verbose output.\n";
397 help
+= " -w = display warning messages.\n";
398 help
+= " -we = treat warnings as errors.\n";
399 help
+= " -h|-? = print this help message and exit.\n\n";
400 help
+= prepareVersion();
405 OString
Options::prepareVersion() const
407 OString
version(m_program
);
408 version
+= " Version 1.1\n\n";
413 bool Options::isValid(const OString
& option
) const
415 return (m_options
.count(option
) > 0);
418 const OString
& Options::getOption(const OString
& option
)
420 if (!isValid(option
))
422 throw IllegalArgument("Option is not valid or currently not set.");
424 return m_options
[option
];
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */