Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <stdio.h>
21 : #include <string.h>
22 : #include "javaoptions.hxx"
23 : #include "osl/process.h"
24 : #include "osl/thread.h"
25 :
26 :
27 : #ifdef SAL_UNX
28 : #define SEPARATOR '/'
29 : #else
30 : #define SEPARATOR '\\'
31 : #endif
32 :
33 4 : sal_Bool JavaOptions::initOptions(int ac, char* av[], sal_Bool bCmdFile)
34 : throw( IllegalArgument )
35 : {
36 4 : sal_Bool ret = sal_True;
37 4 : sal_uInt16 i=0;
38 :
39 4 : if (!bCmdFile)
40 : {
41 4 : bCmdFile = sal_True;
42 :
43 4 : OString name(av[0]);
44 4 : sal_Int32 index = name.lastIndexOf(SEPARATOR);
45 4 : m_program = name.copy((index > 0 ? index+1 : 0));
46 :
47 4 : if (ac < 2)
48 : {
49 0 : fprintf(stderr, "%s", prepareHelp().getStr());
50 0 : ret = sal_False;
51 : }
52 :
53 4 : i = 1;
54 : } else
55 : {
56 0 : i = 0;
57 : }
58 :
59 4 : char *s=NULL;
60 17 : for( ; i < ac; i++)
61 : {
62 13 : if (av[i][0] == '-')
63 : {
64 9 : switch (av[i][1])
65 : {
66 : case 'O':
67 4 : if (av[i][2] == '\0')
68 : {
69 0 : if (i < ac - 1 && av[i+1][0] != '-')
70 : {
71 0 : i++;
72 0 : s = av[i];
73 : } else
74 : {
75 0 : OString tmp("'-O', please check");
76 0 : if (i <= ac - 1)
77 : {
78 0 : tmp += " your input '" + OString(av[i+1]) + "'";
79 : }
80 :
81 0 : throw IllegalArgument(tmp);
82 : }
83 : } else
84 : {
85 4 : s = av[i] + 2;
86 : }
87 :
88 4 : m_options["-O"] = OString(s);
89 4 : break;
90 : case 'n':
91 2 : if (av[i][2] != 'D' || av[i][3] != '\0')
92 : {
93 0 : OString tmp("'-nD', please check");
94 0 : tmp += " your input '" + OString(av[i]) + "'";
95 0 : throw IllegalArgument(tmp);
96 : }
97 :
98 2 : m_options["-nD"] = OString("");
99 2 : break;
100 : case 'T':
101 0 : if (av[i][2] == '\0')
102 : {
103 0 : if (i < ac - 1 && av[i+1][0] != '-')
104 : {
105 0 : i++;
106 0 : s = av[i];
107 : } else
108 : {
109 0 : OString tmp("'-T', please check");
110 0 : if (i <= ac - 1)
111 : {
112 0 : tmp += " your input '" + OString(av[i+1]) + "'";
113 : }
114 :
115 0 : throw IllegalArgument(tmp);
116 : }
117 : } else
118 : {
119 0 : s = av[i] + 2;
120 : }
121 :
122 0 : if (m_options.count("-T") > 0)
123 : {
124 0 : OString tmp(m_options["-T"]);
125 0 : tmp = tmp + ";" + s;
126 0 : m_options["-T"] = tmp;
127 : } else
128 : {
129 0 : m_options["-T"] = OString(s);
130 : }
131 0 : break;
132 : case 'G':
133 0 : if (av[i][2] == 'c')
134 : {
135 0 : if (av[i][3] != '\0')
136 : {
137 0 : OString tmp("'-Gc', please check");
138 0 : if (i <= ac - 1)
139 : {
140 0 : tmp += " your input '" + OString(av[i]) + "'";
141 : }
142 :
143 0 : throw IllegalArgument(tmp);
144 : }
145 :
146 0 : m_options["-Gc"] = OString("");
147 0 : break;
148 0 : } else if (av[i][2] != '\0')
149 : {
150 0 : OString tmp("'-G', please check");
151 0 : if (i <= ac - 1)
152 : {
153 0 : tmp += " your input '" + OString(av[i]) + "'";
154 : }
155 :
156 0 : throw IllegalArgument(tmp);
157 : }
158 :
159 0 : m_options["-G"] = OString("");
160 0 : break;
161 : case 'X': // support for eXtra type rdbs
162 : {
163 3 : if (av[i][2] == '\0')
164 : {
165 0 : if (i < ac - 1 && av[i+1][0] != '-')
166 : {
167 0 : i++;
168 0 : s = av[i];
169 : } else
170 : {
171 0 : OString tmp("'-X', please check");
172 0 : if (i <= ac - 1)
173 : {
174 0 : tmp += " your input '" + OString(av[i+1]) + "'";
175 : }
176 :
177 0 : throw IllegalArgument(tmp);
178 : }
179 : } else
180 : {
181 3 : s = av[i] + 2;
182 : }
183 :
184 3 : m_extra_input_files.push_back( s );
185 3 : break;
186 : }
187 :
188 : default:
189 0 : throw IllegalArgument("the option is unknown" + OString(av[i]));
190 : }
191 : } else
192 : {
193 4 : if (av[i][0] == '@')
194 : {
195 0 : FILE* cmdFile = fopen(av[i]+1, "r");
196 0 : if( cmdFile == NULL )
197 : {
198 0 : fprintf(stderr, "%s", prepareHelp().getStr());
199 0 : ret = sal_False;
200 : } else
201 : {
202 0 : int rargc=0;
203 : char* rargv[512];
204 : char buffer[512];
205 :
206 0 : while ( fscanf(cmdFile, "%s", buffer) != EOF )
207 : {
208 0 : rargv[rargc]= strdup(buffer);
209 0 : rargc++;
210 : }
211 0 : fclose(cmdFile);
212 :
213 0 : ret = initOptions(rargc, rargv, bCmdFile);
214 :
215 0 : for (long j=0; j < rargc; j++)
216 : {
217 0 : free(rargv[j]);
218 : }
219 : }
220 : } else
221 : {
222 4 : if (bCmdFile)
223 : {
224 4 : m_inputFiles.push_back(av[i]);
225 : } else
226 : {
227 0 : OUString system_filepath;
228 0 : if (osl_getCommandArg( i-1, &system_filepath.pData )
229 : != osl_Process_E_None)
230 : {
231 : OSL_ASSERT(false);
232 : }
233 0 : m_inputFiles.push_back(OUStringToOString(system_filepath, osl_getThreadTextEncoding()));
234 : }
235 : }
236 : }
237 : }
238 :
239 4 : return ret;
240 : }
241 :
242 0 : OString JavaOptions::prepareHelp()
243 : {
244 0 : OString help("\nusing: ");
245 0 : help += m_program + " [-options] file_1 ... file_n -Xfile_n+1 -Xfile_n+2\nOptions:\n";
246 0 : help += " -O<path> = path describes the root directory for the generated output.\n";
247 0 : help += " The output directory tree is generated under this directory.\n";
248 0 : help += " -T<name> = name specifies a type or a list of types. The output for this\n";
249 0 : help += " [t1;...] type and all dependent types are generated. If no '-T' option is \n";
250 0 : help += " specified, then output for all types is generated.\n";
251 0 : help += " Example: 'com.sun.star.uno.XInterface' is a valid type.\n";
252 0 : help += " -nD = no dependent types are generated.\n";
253 0 : help += " -G = generate only target files which does not exists.\n";
254 0 : help += " -Gc = generate only target files which content will be changed.\n";
255 0 : help += " -X<file> = extra types which will not be taken into account for generation.\n\n";
256 0 : help += prepareVersion();
257 :
258 0 : return help;
259 : }
260 :
261 0 : OString JavaOptions::prepareVersion()
262 : {
263 0 : OString version(m_program);
264 0 : version += " Version 2.0\n\n";
265 0 : return version;
266 : }
267 :
268 :
269 :
270 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|