diff options
Diffstat (limited to 'idlc')
-rw-r--r-- | idlc/inc/idlc/options.hxx | 17 | ||||
-rw-r--r-- | idlc/source/astexpression.cxx | 21 | ||||
-rw-r--r-- | idlc/source/idlcmain.cxx | 14 | ||||
-rw-r--r-- | idlc/source/options.cxx | 535 | ||||
-rw-r--r-- | idlc/source/preproc/eval.c | 15 | ||||
-rw-r--r-- | idlc/source/preproc/lex.c | 10 | ||||
-rw-r--r-- | idlc/source/preproc/unix.c | 1 |
7 files changed, 324 insertions, 289 deletions
diff --git a/idlc/inc/idlc/options.hxx b/idlc/inc/idlc/options.hxx index 7eba788a4ea9..383150bd4b40 100644 --- a/idlc/inc/idlc/options.hxx +++ b/idlc/inc/idlc/options.hxx @@ -50,18 +50,29 @@ public: class Options { public: - Options(); + explicit Options(char const * progname); ~Options(); + static bool checkArgument(std::vector< std::string > & rArgs, char const * arg, size_t len); + static bool checkCommandFile(std::vector< std::string > & rArgs, char const * filename); + + bool initOptions(std::vector< std::string > & rArgs) + throw(IllegalArgument); + bool badOption(char const * reason, std::string const & rArg) + throw(IllegalArgument); + bool setOption(char const * option, std::string const & rArg); + +#if 0 /* @@@ */ sal_Bool initOptions(int ac, char* av[], sal_Bool bCmdFile=sal_False) throw( IllegalArgument ); +#endif /* @@@ */ ::rtl::OString prepareHelp(); ::rtl::OString prepareVersion(); const ::rtl::OString& getProgramName() const; - sal_Bool isValid(const ::rtl::OString& option); - const ::rtl::OString getOption(const ::rtl::OString& option) + bool isValid(const ::rtl::OString& option); + const ::rtl::OString& getOption(const ::rtl::OString& option) throw( IllegalArgument ); const StringVector& getInputFiles() const { return m_inputFiles; } diff --git a/idlc/source/astexpression.cxx b/idlc/source/astexpression.cxx index a93c13ecf8ba..357da1ab362e 100644 --- a/idlc/source/astexpression.cxx +++ b/idlc/source/astexpression.cxx @@ -27,6 +27,7 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_idlc.hxx" + #include <idlc/astexpression.hxx> #include <idlc/astconstant.hxx> #include <idlc/astscope.hxx> @@ -34,6 +35,7 @@ #include <limits.h> #include <float.h> +#include <memory> // auto_ptr<> #undef MAXCHAR #define MAXCHAR 127 @@ -927,7 +929,6 @@ AstExprValue* AstExpression::eval_internal(EvalKind ek) AstExprValue* AstExpression::eval_bin_op(EvalKind ek) { - AstExprValue *retval = NULL; ExprType eType = ET_double; if ( m_combOperator == EC_mod ) @@ -950,7 +951,7 @@ AstExprValue* AstExpression::eval_bin_op(EvalKind ek) if (m_subExpr2->getExprValue() == NULL) return NULL; - retval = new AstExprValue(); + std::auto_ptr< AstExprValue > retval(new AstExprValue()); retval->et = eType; switch (m_combOperator) @@ -971,20 +972,18 @@ AstExprValue* AstExpression::eval_bin_op(EvalKind ek) break; case EC_div: if (m_subExpr2->getExprValue()->u.dval == 0.0) - return NULL; + return NULL; retval->u.dval = m_subExpr1->getExprValue()->u.dval / m_subExpr2->getExprValue()->u.dval; break; default: return NULL; } - return retval; + return retval.release(); } AstExprValue* AstExpression::eval_bit_op(EvalKind ek) { - AstExprValue *retval = NULL; - if (ek != EK_const && ek != EK_positive_int) return NULL; if (m_subExpr1 == NULL || m_subExpr2 == NULL) @@ -1002,7 +1001,7 @@ AstExprValue* AstExpression::eval_bit_op(EvalKind ek) if (m_subExpr2->getExprValue() == NULL) return NULL; - retval = new AstExprValue; + std::auto_ptr< AstExprValue > retval(new AstExprValue()); retval->et = ET_long; switch (m_combOperator) @@ -1026,13 +1025,11 @@ AstExprValue* AstExpression::eval_bit_op(EvalKind ek) return NULL; } - return retval; + return retval.release(); } AstExprValue* AstExpression::eval_un_op(EvalKind ek) { - AstExprValue *retval = NULL; - if (m_exprValue != NULL) return m_exprValue; @@ -1047,7 +1044,7 @@ AstExprValue* AstExpression::eval_un_op(EvalKind ek) if (m_subExpr1->getExprValue() == NULL) return NULL; - retval = new AstExprValue(); + std::auto_ptr< AstExprValue > retval(new AstExprValue()); retval->et = ET_double; switch (m_combOperator) @@ -1068,7 +1065,7 @@ AstExprValue* AstExpression::eval_un_op(EvalKind ek) return NULL; } - return retval; + return retval.release(); } AstExprValue* AstExpression::eval_symbol(EvalKind ek) diff --git a/idlc/source/idlcmain.cxx b/idlc/source/idlcmain.cxx index 006131f727df..6e3af1baca73 100644 --- a/idlc/source/idlcmain.cxx +++ b/idlc/source/idlcmain.cxx @@ -34,19 +34,25 @@ using namespace ::rtl; SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv) { - Options options; + std::vector< std::string > args; + for (int i = 1; i < argc; i++) + { + if (!Options::checkArgument (args, argv[i], strlen(argv[i]))) + return (1); + } + Options options(argv[0]); try { - if (!options.initOptions(argc, argv)) - exit(1); + if (!options.initOptions(args)) + return (0); } catch( IllegalArgument& e) { fprintf(stderr, "Illegal argument: %s\n%s", e.m_message.getStr(), options.prepareVersion().getStr()); - exit(99); + return (99); } setIdlc(&options); diff --git a/idlc/source/options.cxx b/idlc/source/options.cxx index c90bce43b3bc..42b97237e34f 100644 --- a/idlc/source/options.cxx +++ b/idlc/source/options.cxx @@ -28,13 +28,20 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_idlc.hxx" +#include "idlc/options.hxx" + +#include "osl/diagnose.h" +#include "rtl/string.hxx" +#include "rtl/strbuf.hxx" + #include <stdio.h> -#include /*MSVC trouble: <cstring>*/ <string.h> -#include <idlc/options.hxx> +#include <string.h> -using namespace rtl; +using rtl::OString; +using rtl::OStringBuffer; -Options::Options(): m_stdin(false), m_verbose(false), m_quiet(false) +Options::Options(char const * progname) + : m_program(progname), m_stdin(false), m_verbose(false), m_quiet(false) { } @@ -42,273 +49,282 @@ Options::~Options() { } -sal_Bool Options::initOptions(int ac, char* av[], sal_Bool bCmdFile) - throw( IllegalArgument ) +// static +bool Options::checkArgument (std::vector< std::string > & rArgs, char const * arg, size_t len) { - sal_Bool ret = sal_True; - sal_uInt16 j=0; - - if (!bCmdFile) + bool result = ((arg != 0) && (len > 0)); + OSL_PRECOND(result, "idlc::Options::checkArgument(): invalid arguments"); + if (result) + { + switch(arg[0]) { - bCmdFile = sal_True; - - m_program = av[0]; - - if (ac < 2) + case '@': + if ((result = (len > 1)) == true) + { + // "@<cmdfile>" + result = Options::checkCommandFile (rArgs, &(arg[1])); + } + break; + case '-': + if ((result = (len > 1)) == true) + { + // "-<option>" + switch (arg[1]) { - fprintf(stderr, "%s", prepareHelp().getStr()); - ret = sal_False; + case 'O': + case 'I': + case 'D': + { + // "-<option>[<param>] + std::string option(&(arg[0]), 2); + rArgs.push_back(option); + if (len > 2) + { + // "-<option><param>" + std::string param(&(arg[2]), len - 2); + rArgs.push_back(param); + } + break; + } + default: + // "-<option>" ([long] option, w/o param) + rArgs.push_back(std::string(arg, len)); + break; } + } + break; + default: + // "<param>" + rArgs.push_back(std::string(arg, len)); + break; + } + } + return (result); +} - j = 1; - } else +// static +bool Options::checkCommandFile (std::vector< std::string > & rArgs, char const * filename) +{ + FILE * fp = fopen(filename, "r"); + if (fp == 0) { - j = 0; + fprintf(stderr, "ERROR: can't open command file \"%s\"\n", filename); + return (false); } - char *s=NULL; - for (; j < ac; j++) + std::string buffer; + buffer.reserve(256); + + bool quoted = false; + int c = EOF; + while ((c = fgetc(fp)) != EOF) { - if (av[j][0] == '-') + switch(c) { - switch (av[j][1]) - { - case 'O': - if (av[j][2] == '\0') - { - if (j < ac - 1 && av[j+1][0] != '-') - { - j++; - s = av[j]; - } else - { - OString tmp("'-O', please check"); - if (j <= ac - 1) - { - tmp += " your input '" + OString(av[j+1]) + "'"; - } - - throw IllegalArgument(tmp); - } - } else - { - s = av[j] + 2; - } - - m_options["-O"] = OString(s); - break; - case 'I': - { - if (av[j][2] == '\0') - { - if (j < ac - 1 && av[j+1][0] != '-') - { - j++; - s = av[j]; - } else - { - OString tmp("'-I', please check"); - if (j <= ac - 1) - { - tmp += " your input '" + OString(av[j+1]) + "'"; - } - - throw IllegalArgument(tmp); - } - } else - { - s = av[j] + 2; - } + case '\"': + quoted = !quoted; + break; + case ' ': + case '\t': + case '\r': + case '\n': + if (!quoted) + { + if (!buffer.empty()) + { + // append current argument. + if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size())) + { + (void) fclose(fp); + return (false); + } + buffer.clear(); + } + break; + } + default: + // quoted white-space fall through + buffer.push_back(sal::static_int_cast<char>(c)); + break; + } + } + if (!buffer.empty()) + { + // append unterminated argument. + if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size())) + { + (void) fclose(fp); + return (false); + } + buffer.clear(); + } + return (fclose(fp) == 0); +} - OString inc(s); - if ( inc.indexOf(';') > 0 ) - { - OString tmp(s); - sal_Int32 nIndex = 0; - inc = OString(); - do inc = inc + " -I\"" + tmp.getToken( 0, ';', nIndex ) +"\""; while( nIndex != -1 ); - } else - inc = OString("-I\"") + s + "\""; +bool Options::badOption(char const * reason, std::string const & rArg) throw(IllegalArgument) +{ + OStringBuffer message; + if (reason != 0) + { + message.append(reason); message.append(" option '"); message.append(rArg.c_str()); message.append("'"); + throw IllegalArgument(message.makeStringAndClear()); + } + return false; +} - if (m_options.count("-I") > 0) - { - OString tmp(m_options["-I"]); - tmp = tmp + " " + inc; - m_options["-I"] = tmp; - } else - { - m_options["-I"] = inc; - } - } - break; - case 'D': - if (av[j][2] == '\0') - { - if (j < ac - 1 && av[j+1][0] != '-') - { - j++; - s = av[j]; - } else - { - OString tmp("'-D', please check"); - if (j <= ac - 1) - { - tmp += " your input '" + OString(av[j+1]) + "'"; - } +bool Options::setOption(char const * option, std::string const & rArg) +{ + bool result = (0 == strcmp(option, rArg.c_str())); + if (result) + m_options[rArg.c_str()] = OString(rArg.c_str(), rArg.size()); + return (result); +} - throw IllegalArgument(tmp); - } - } else - { - s = av[j]; - } +bool Options::initOptions(std::vector< std::string > & rArgs) throw(IllegalArgument) +{ + std::vector< std::string >::const_iterator first = rArgs.begin(), last = rArgs.end(); + for (; first != last; ++first) + { + if ((*first)[0] != '-') + { + OString filename((*first).c_str(), (*first).size()); + OString tmp(filename.toAsciiLowerCase()); + if (tmp.lastIndexOf(".idl") != (tmp.getLength() - 4)) + { + throw IllegalArgument("'" + filename + "' is not a valid input file, only '*.idl' files will be accepted"); + } + m_inputFiles.push_back(filename); + continue; + } - if (m_options.count("-D") > 0) - { - OString tmp(m_options["-D"]); - tmp = tmp + " " + s; - m_options["-D"] = tmp; - } else - m_options["-D"] = OString(s); - break; - case 'C': - if (av[j][2] != '\0') - { - throw IllegalArgument(OString(av[j]) + ", please check your input"); - } - if (m_options.count("-C") == 0) - m_options["-C"] = OString(av[j]); - break; - case 'c': - if (av[j][2] == 'i' && av[j][3] == 'd' && av[j][4] == '\0') - { - if (m_options.count("-cid") == 0) - m_options["-cid"] = OString(av[j]); - } else - throw IllegalArgument(OString(av[j]) + ", please check your input"); - break; - case 'v': - if ( 0 == strcmp( &av[j][1], "verbose" ) ) - { - m_verbose = true; - } - else - throw IllegalArgument(OString(av[j]) + ", please check your input"); - break; - case 'q': - if ( 0 == strcmp( &av[j][1], "quiet" ) ) - { - m_quiet = true; - } - else - throw IllegalArgument(OString(av[j]) + ", please check your input"); - break; - case 'w': - if (av[j][2] == 'e' && av[j][3] == '\0') { - if (m_options.count("-we") == 0) - m_options["-we"] = OString(av[j]); - } else { - if (av[j][2] == '\0') { - if (m_options.count("-w") == 0) - m_options["-w"] = OString(av[j]); - } else - throw IllegalArgument(OString(av[j]) + ", please check your input"); - } - break; - case 'h': - case '?': - if (av[j][2] != '\0') - { - throw IllegalArgument(OString(av[j]) + ", please check your input"); - } else - { - fprintf(stdout, "%s", prepareHelp().getStr()); - exit(0); - } - case 's': - if (/*MSVC trouble: std::*/strcmp(&av[j][2], "tdin") == 0) - { - m_stdin = true; - break; - } - // fall through - default: - throw IllegalArgument("the option is unknown" + OString(av[j])); - } - } else + std::string const option(*first); + switch((*first)[1]) + { + case 'O': + { + if (!((++first != last) && ((*first)[0] != '-'))) { - if (av[j][0] == '@') - { - FILE* cmdFile = fopen(av[j]+1, "r"); - if( cmdFile == NULL ) - { - fprintf(stderr, "%s", prepareHelp().getStr()); - ret = sal_False; - } else - { - int rargc=0; - char* rargv[512]; - char buffer[512]=""; - - int i=0; - int found = 0; - char c; - while ( fscanf(cmdFile, "%c", &c) != EOF ) - { - if (c=='\"') { - if (found) { - found=0; - } else { - found=1; - continue; - } - } else { - if (c!=13 && c!=10) { - if (found || c!=' ') { - buffer[i++]=c; - continue; - } - } - if (i==0) - continue; - } - buffer[i]='\0'; - found=0; - i=0; - rargv[rargc]= strdup(buffer); - rargc++; - buffer[0]='\0'; - } - if (buffer[0] != '\0') { - buffer[i]='\0'; - rargv[rargc]= strdup(buffer); - rargc++; - } - fclose(cmdFile); - - ret = initOptions(rargc, rargv, bCmdFile); - - long ii = 0; - for (ii=0; ii < rargc; ii++) - { - free(rargv[ii]); - } - } - } else - { - OString name(av[j]); - name = name.toAsciiLowerCase(); - if ( name.lastIndexOf(".idl") != (name.getLength() - 4) ) - { - throw IllegalArgument("'" + OString(av[j]) + - "' is not a valid input file, only '*.idl' files will be accepted"); - } - m_inputFiles.push_back(av[j]); - } + return badOption("invalid", option); + } + OString param((*first).c_str(), (*first).size()); + m_options["-O"] = param; + break; + } + case 'I': + { + if (!((++first != last) && ((*first)[0] != '-'))) + { + return badOption("invalid", option); + } + OString param((*first).c_str(), (*first).size()); + { + // quote param token(s). + OStringBuffer buffer; + sal_Int32 k = 0; + do + { + OStringBuffer token; token.append("-I\""); token.append(param.getToken(0, ';', k)); token.append("\""); + if (buffer.getLength() > 0) + buffer.append(' '); + buffer.append(token); + } while (k != -1); + param = buffer.makeStringAndClear(); + } + if (m_options.count("-I") > 0) + { + // append param. + OStringBuffer buffer(m_options["-I"]); + buffer.append(' '); buffer.append(param); + param = buffer.makeStringAndClear(); + } + m_options["-I"] = param; + break; + } + case 'D': + { + if (!((++first != last) && ((*first)[0] != '-'))) + { + return badOption("invalid", option); + } + OString param("-D"); param += OString((*first).c_str(), (*first).size()); + if (m_options.count("-D") > 0) + { + OStringBuffer buffer(m_options["-D"]); + buffer.append(' '); buffer.append(param); + param = buffer.makeStringAndClear(); } + m_options["-D"] = param; + break; + } + case 'C': + { + if (!setOption("-C", option)) + { + return badOption("invalid", option); + } + break; + } + case 'c': + { + if (!setOption("-cid", option)) + { + return badOption("invalid", option); + } + break; + } + case 'q': + { + if (!setOption("-quiet", option)) + { + return badOption("invalid", option); + } + m_quiet = true; + break; + } + case 'v': + { + if (!setOption("-verbose", option)) + { + return badOption("invalid", option); + } + m_verbose = true; + break; + } + case 'w': + { + if (!(setOption("-w", option) || setOption("-we", option))) + { + return badOption("invalid", option); + } + break; + } + case 'h': + case '?': + { + if (!(setOption("-h", option) || setOption("-?", option))) + { + return badOption("invalid", option); + } + { + (void) fprintf(stdout, "%s", prepareHelp().getStr()); + return (false); + } + break; + } + case 's': + { + if (!setOption("-stdin", option)) + { + return badOption("invalid", option); + } + m_stdin = true; + break; + } + default: + return badOption("unknown", option); } - - return ret; + } + return (true); } OString Options::prepareHelp() @@ -353,19 +369,18 @@ const OString& Options::getProgramName() const return m_program; } -sal_Bool Options::isValid(const OString& option) +bool Options::isValid(const OString& option) { return (m_options.count(option) > 0); } -const OString Options::getOption(const OString& option) +const OString& Options::getOption(const OString& option) throw( IllegalArgument ) { - if (m_options.count(option) > 0) - { - return m_options[option]; - } else + if (!isValid(option)) { throw IllegalArgument("Option is not valid or currently not set."); } + return m_options[option]; } +/* vi:set tabstop=4 shiftwidth=4 expandtab: */ diff --git a/idlc/source/preproc/eval.c b/idlc/source/preproc/eval.c index cd3adc2204c7..bed61eb95f00 100644 --- a/idlc/source/preproc/eval.c +++ b/idlc/source/preproc/eval.c @@ -24,9 +24,11 @@ * for a copy of the LGPLv3 License. * ************************************************************************/ + +#include "cpp.h" + #include <stdlib.h> #include <string.h> -#include "cpp.h" #define NSTAK 32 #define SGN 0 @@ -736,10 +738,10 @@ struct value } else { - static char cvcon[] - = "b\bf\fn\nr\rt\tv\v''\"\"??\\\\"; + static char cvcon[] = "b\bf\fn\nr\rt\tv\v''\"\"??\\\\"; + static int cvlen = sizeof(cvcon) - 1; - for (i = 0; i < (int)sizeof(cvcon); i += 2) + for (i = 0; i < cvlen; i += 2) { if (*p == cvcon[i]) { @@ -748,9 +750,8 @@ struct value } } p += 1; - if (i >= (int)sizeof(cvcon)) - error(WARNING, - "Undefined escape in character constant"); + if (i >= cvlen) + error(WARNING,"Undefined escape in character constant"); } } else diff --git a/idlc/source/preproc/lex.c b/idlc/source/preproc/lex.c index fd6d00792984..856ee72bfc53 100644 --- a/idlc/source/preproc/lex.c +++ b/idlc/source/preproc/lex.c @@ -290,7 +290,7 @@ void bigfsm[j][fp->state] = (short) nstate; continue; case C_ALPH: - for (j = 0; j <= 256; j++) + for (j = 0; j < 256; j++) if (('a' <= j && j <= 'z') || ('A' <= j && j <= 'Z') || j == '_') bigfsm[j][fp->state] = (short) nstate; @@ -687,9 +687,13 @@ void if (s->fd >= 0) { - close(s->fd); - dofree(s->inb); + (void) close(s->fd); + dofree(s->filename); } + + if (s->inb) + dofree(s->inb); + cursource = s->next; dofree(s); } diff --git a/idlc/source/preproc/unix.c b/idlc/source/preproc/unix.c index 4997e317e697..808b1d90dbd3 100644 --- a/idlc/source/preproc/unix.c +++ b/idlc/source/preproc/unix.c @@ -91,6 +91,7 @@ void maketokenrow(3, &tr); gettokens(&tr, 1); doadefine(&tr, c); + dofree(tr.bp); tr.bp = 0; unsetsource(); break; |