1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski #include "options.hxx"
25*b1cdbd2cSJim Jagielski
26*b1cdbd2cSJim Jagielski #include "osl/diagnose.h"
27*b1cdbd2cSJim Jagielski
28*b1cdbd2cSJim Jagielski #include <stdio.h>
29*b1cdbd2cSJim Jagielski #include <string.h>
30*b1cdbd2cSJim Jagielski
31*b1cdbd2cSJim Jagielski namespace registry
32*b1cdbd2cSJim Jagielski {
33*b1cdbd2cSJim Jagielski namespace tools
34*b1cdbd2cSJim Jagielski {
35*b1cdbd2cSJim Jagielski
Options(char const * program)36*b1cdbd2cSJim Jagielski Options::Options (char const * program)
37*b1cdbd2cSJim Jagielski : m_program (program)
38*b1cdbd2cSJim Jagielski {}
39*b1cdbd2cSJim Jagielski
~Options()40*b1cdbd2cSJim Jagielski Options::~Options()
41*b1cdbd2cSJim Jagielski {}
42*b1cdbd2cSJim Jagielski
43*b1cdbd2cSJim Jagielski // static
checkArgument(std::vector<std::string> & rArgs,char const * arg,size_t len)44*b1cdbd2cSJim Jagielski bool Options::checkArgument(std::vector< std::string> & rArgs, char const * arg, size_t len)
45*b1cdbd2cSJim Jagielski {
46*b1cdbd2cSJim Jagielski bool result = ((arg != 0) && (len > 0));
47*b1cdbd2cSJim Jagielski OSL_PRECOND(result, "registry::tools::Options::checkArgument(): invalid arguments");
48*b1cdbd2cSJim Jagielski if (result)
49*b1cdbd2cSJim Jagielski {
50*b1cdbd2cSJim Jagielski OSL_TRACE("registry::tools:Options::checkArgument(): \"%s\"", arg);
51*b1cdbd2cSJim Jagielski switch (arg[0])
52*b1cdbd2cSJim Jagielski {
53*b1cdbd2cSJim Jagielski case '@':
54*b1cdbd2cSJim Jagielski if ((result = (len > 1)) == true)
55*b1cdbd2cSJim Jagielski {
56*b1cdbd2cSJim Jagielski // "@<cmdfile>"
57*b1cdbd2cSJim Jagielski result = Options::checkCommandFile(rArgs, &(arg[1]));
58*b1cdbd2cSJim Jagielski }
59*b1cdbd2cSJim Jagielski break;
60*b1cdbd2cSJim Jagielski case '-':
61*b1cdbd2cSJim Jagielski if ((result = (len > 1)) == true)
62*b1cdbd2cSJim Jagielski {
63*b1cdbd2cSJim Jagielski // "-<option>"
64*b1cdbd2cSJim Jagielski std::string option (&(arg[0]), 2);
65*b1cdbd2cSJim Jagielski rArgs.push_back(option);
66*b1cdbd2cSJim Jagielski if (len > 2)
67*b1cdbd2cSJim Jagielski {
68*b1cdbd2cSJim Jagielski // "-<option><param>"
69*b1cdbd2cSJim Jagielski std::string param(&(arg[2]), len - 2);
70*b1cdbd2cSJim Jagielski rArgs.push_back(param);
71*b1cdbd2cSJim Jagielski }
72*b1cdbd2cSJim Jagielski }
73*b1cdbd2cSJim Jagielski break;
74*b1cdbd2cSJim Jagielski default:
75*b1cdbd2cSJim Jagielski rArgs.push_back(std::string(arg, len));
76*b1cdbd2cSJim Jagielski break;
77*b1cdbd2cSJim Jagielski }
78*b1cdbd2cSJim Jagielski }
79*b1cdbd2cSJim Jagielski return (result);
80*b1cdbd2cSJim Jagielski }
81*b1cdbd2cSJim Jagielski
82*b1cdbd2cSJim Jagielski // static
checkCommandFile(std::vector<std::string> & rArgs,char const * filename)83*b1cdbd2cSJim Jagielski bool Options::checkCommandFile(std::vector< std::string > & rArgs, char const * filename)
84*b1cdbd2cSJim Jagielski {
85*b1cdbd2cSJim Jagielski FILE * fp = fopen(filename, "r");
86*b1cdbd2cSJim Jagielski if (fp == 0)
87*b1cdbd2cSJim Jagielski {
88*b1cdbd2cSJim Jagielski fprintf(stderr, "ERROR: Can't open command file \"%s\"\n", filename);
89*b1cdbd2cSJim Jagielski return (false);
90*b1cdbd2cSJim Jagielski }
91*b1cdbd2cSJim Jagielski
92*b1cdbd2cSJim Jagielski std::string buffer;
93*b1cdbd2cSJim Jagielski buffer.reserve(256);
94*b1cdbd2cSJim Jagielski
95*b1cdbd2cSJim Jagielski bool quoted = false;
96*b1cdbd2cSJim Jagielski int c = EOF;
97*b1cdbd2cSJim Jagielski while ((c = fgetc(fp)) != EOF)
98*b1cdbd2cSJim Jagielski {
99*b1cdbd2cSJim Jagielski switch(c)
100*b1cdbd2cSJim Jagielski {
101*b1cdbd2cSJim Jagielski case '\"':
102*b1cdbd2cSJim Jagielski quoted = !quoted;
103*b1cdbd2cSJim Jagielski break;
104*b1cdbd2cSJim Jagielski case ' ':
105*b1cdbd2cSJim Jagielski case '\t':
106*b1cdbd2cSJim Jagielski case '\r':
107*b1cdbd2cSJim Jagielski case '\n':
108*b1cdbd2cSJim Jagielski if (!quoted)
109*b1cdbd2cSJim Jagielski {
110*b1cdbd2cSJim Jagielski if (!buffer.empty())
111*b1cdbd2cSJim Jagielski {
112*b1cdbd2cSJim Jagielski if (!checkArgument(rArgs, buffer.c_str(), buffer.size()))
113*b1cdbd2cSJim Jagielski {
114*b1cdbd2cSJim Jagielski // failure.
115*b1cdbd2cSJim Jagielski (void) fclose(fp);
116*b1cdbd2cSJim Jagielski return false;
117*b1cdbd2cSJim Jagielski }
118*b1cdbd2cSJim Jagielski buffer.clear();
119*b1cdbd2cSJim Jagielski }
120*b1cdbd2cSJim Jagielski break;
121*b1cdbd2cSJim Jagielski }
122*b1cdbd2cSJim Jagielski default:
123*b1cdbd2cSJim Jagielski // quoted white-space fall through
124*b1cdbd2cSJim Jagielski buffer.push_back(sal::static_int_cast<char>(c));
125*b1cdbd2cSJim Jagielski break;
126*b1cdbd2cSJim Jagielski }
127*b1cdbd2cSJim Jagielski }
128*b1cdbd2cSJim Jagielski return (fclose(fp) == 0);
129*b1cdbd2cSJim Jagielski }
130*b1cdbd2cSJim Jagielski
initOptions(std::vector<std::string> & rArgs)131*b1cdbd2cSJim Jagielski bool Options::initOptions (std::vector< std::string > & rArgs)
132*b1cdbd2cSJim Jagielski {
133*b1cdbd2cSJim Jagielski return initOptions_Impl (rArgs);
134*b1cdbd2cSJim Jagielski }
135*b1cdbd2cSJim Jagielski
badOption(char const * reason,char const * option) const136*b1cdbd2cSJim Jagielski bool Options::badOption (char const * reason, char const * option) const
137*b1cdbd2cSJim Jagielski {
138*b1cdbd2cSJim Jagielski (void) fprintf(stderr, "%s: %s option '%s'\n", m_program.c_str(), reason, option);
139*b1cdbd2cSJim Jagielski return printUsage();
140*b1cdbd2cSJim Jagielski }
141*b1cdbd2cSJim Jagielski
printUsage() const142*b1cdbd2cSJim Jagielski bool Options::printUsage() const
143*b1cdbd2cSJim Jagielski {
144*b1cdbd2cSJim Jagielski printUsage_Impl();
145*b1cdbd2cSJim Jagielski return false;
146*b1cdbd2cSJim Jagielski }
147*b1cdbd2cSJim Jagielski
148*b1cdbd2cSJim Jagielski } // namespace tools
149*b1cdbd2cSJim Jagielski } // namespace registry
150