31
|
1 |
/************************************************************************
|
|
2 |
*
|
|
3 |
* opt_lines.cpp - definitions of line option handlers
|
|
4 |
*
|
|
5 |
* $Id: opt_lines.cpp 290026 2005-09-19 00:12:38Z sebor $
|
|
6 |
*
|
|
7 |
************************************************************************
|
|
8 |
*
|
|
9 |
* Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave
|
|
10 |
* Software division. Licensed under the Apache License, Version 2.0 (the
|
|
11 |
* "License"); you may not use this file except in compliance with the
|
|
12 |
* License. You may obtain a copy of the License at
|
|
13 |
* http://www.apache.org/licenses/LICENSE-2.0. Unless required by
|
|
14 |
* applicable law or agreed to in writing, software distributed under
|
|
15 |
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
16 |
* CONDITIONS OF ANY KIND, either express or implied. See the License
|
|
17 |
* for the specific language governing permissions and limitations under
|
|
18 |
* the License.
|
|
19 |
*
|
|
20 |
**************************************************************************/
|
|
21 |
|
|
22 |
// expand _TEST_EXPORT macros
|
|
23 |
#define _RWSTD_TEST_SRC
|
|
24 |
|
|
25 |
#include "opt_lines.h"
|
|
26 |
|
|
27 |
#include <cmdopt.h>
|
|
28 |
|
|
29 |
#include <assert.h>
|
|
30 |
#include <stdio.h>
|
|
31 |
#include <stdlib.h>
|
|
32 |
#include <string.h>
|
|
33 |
|
|
34 |
|
|
35 |
enum {
|
|
36 |
l_enabled = 1, // line is enabled
|
|
37 |
l_disabled = 2, // line is disabled
|
|
38 |
l_expected = 4 // diagnostic on line is expected to be active
|
|
39 |
};
|
|
40 |
|
|
41 |
struct linerange_t {
|
|
42 |
int first;
|
|
43 |
int last;
|
|
44 |
unsigned flags: 8;
|
|
45 |
char id [80];
|
|
46 |
};
|
|
47 |
|
|
48 |
static size_t nlineranges;
|
|
49 |
static linerange_t *lineranges;
|
|
50 |
static size_t rangebufsize;
|
|
51 |
|
|
52 |
/**************************************************************************/
|
|
53 |
|
|
54 |
static int
|
|
55 |
_rw_enable_lines (int first, int last, int flags)
|
|
56 |
{
|
|
57 |
if (nlineranges == rangebufsize) {
|
|
58 |
const size_t newbufsize = 2 * nlineranges + 1;
|
|
59 |
|
|
60 |
linerange_t* const newranges =
|
|
61 |
(linerange_t*)malloc (newbufsize * sizeof (linerange_t));
|
|
62 |
|
|
63 |
if (0 == newranges) {
|
|
64 |
abort ();
|
|
65 |
}
|
|
66 |
|
|
67 |
memcpy (newranges, lineranges, nlineranges * sizeof (linerange_t));
|
|
68 |
|
|
69 |
free (lineranges);
|
|
70 |
|
|
71 |
lineranges = newranges;
|
|
72 |
rangebufsize = newbufsize;
|
|
73 |
}
|
|
74 |
|
|
75 |
lineranges [nlineranges].first = first;
|
|
76 |
lineranges [nlineranges].last = last;
|
|
77 |
|
|
78 |
lineranges [nlineranges].flags = flags;
|
|
79 |
|
|
80 |
++nlineranges;
|
|
81 |
|
|
82 |
return 0;
|
|
83 |
}
|
|
84 |
|
|
85 |
|
|
86 |
static int
|
|
87 |
_rw_enable_line (int argc, char *argv[], int flags)
|
|
88 |
{
|
|
89 |
char *parg = strchr (argv [0], '=');
|
|
90 |
assert (0 != parg);
|
|
91 |
|
|
92 |
const char* const argbeg = ++parg;
|
|
93 |
|
|
94 |
// the lower bound of a range of lines to be enabled or disabled
|
|
95 |
// negative values are not valid and denote an implicit lower bound
|
|
96 |
// of 1 (such as in "-3" which is a shorthand for "1-3")
|
|
97 |
long first = -1;
|
|
98 |
|
|
99 |
for ( ; '\0' != *parg ; ) {
|
|
100 |
|
|
101 |
// skip any leading whitespace
|
|
102 |
for ( ; ' ' == *parg; ++parg);
|
|
103 |
|
|
104 |
if ('-' == *parg) {
|
|
105 |
if (first < 0) {
|
|
106 |
first = 0;
|
|
107 |
++parg;
|
|
108 |
}
|
|
109 |
else {
|
|
110 |
fprintf (stderr,
|
|
111 |
"invalid character '%c' at position %d: \"%s\"\n",
|
|
112 |
*parg, int (parg - argbeg), argv [0]);
|
|
113 |
return 2;
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
// parse a numeric argument
|
|
118 |
char *end;
|
|
119 |
long line = strtol (parg, &end, 0);
|
|
120 |
|
|
121 |
// skip any trailing whitespace
|
|
122 |
for ( ; ' ' == *end; ++end);
|
|
123 |
|
|
124 |
if (end == parg || '-' != *end && ',' != *end && '\0' != *end) {
|
|
125 |
fprintf (stderr,
|
|
126 |
"invalid character '%c' at position %d: \"%s\"\n",
|
|
127 |
*end, int (parg - argbeg), argv [0]);
|
|
128 |
return 2;
|
|
129 |
}
|
|
130 |
|
|
131 |
if (0 <= first) {
|
|
132 |
if (line < 0) {
|
|
133 |
fprintf (stderr,
|
|
134 |
"invalid value %ld at position %d: \"%s\"\n",
|
|
135 |
line, int (parg - argbeg), argv [0]);
|
|
136 |
return 2;
|
|
137 |
}
|
|
138 |
|
|
139 |
++line;
|
|
140 |
|
|
141 |
if ((',' == *end || '-' == *end) && end [1])
|
|
142 |
++end;
|
|
143 |
}
|
|
144 |
else if (',' == *end) {
|
|
145 |
first = line++;
|
|
146 |
if ('\0' == end [1]) {
|
|
147 |
fprintf (stderr,
|
|
148 |
"invalid character '%c' at position %d: \"%s\"\n",
|
|
149 |
*end, int (parg - argbeg), argv [0]);
|
|
150 |
return 2;
|
|
151 |
}
|
|
152 |
|
|
153 |
++end;
|
|
154 |
}
|
|
155 |
else if ('-' == *end) {
|
|
156 |
first = line;
|
|
157 |
while (' ' == *++end);
|
|
158 |
|
|
159 |
#ifdef __ARMCC__
|
|
160 |
#pragma diag_suppress 61
|
|
161 |
#endif
|
|
162 |
if ('\0' == *end) {
|
|
163 |
line = _RWSTD_INT_MAX;
|
|
164 |
}
|
|
165 |
else if (',' == *end) {
|
|
166 |
line = _RWSTD_INT_MAX;
|
|
167 |
++end;
|
|
168 |
}
|
|
169 |
else
|
|
170 |
line = -1;
|
|
171 |
|
|
172 |
}
|
|
173 |
else if ('\0' == *end) {
|
|
174 |
first = line++;
|
|
175 |
}
|
|
176 |
else {
|
|
177 |
fprintf (stderr,
|
|
178 |
"invalid character '%c' at position %d: \"%s\"\n",
|
|
179 |
*end, int (parg - argbeg), argv [0]);
|
|
180 |
return 2;
|
|
181 |
}
|
|
182 |
|
|
183 |
parg = end;
|
|
184 |
|
|
185 |
if (0 <= first && first < line) {
|
|
186 |
_rw_enable_lines (first, line, flags);
|
|
187 |
first = -1;
|
|
188 |
}
|
|
189 |
}
|
|
190 |
|
|
191 |
return 0;
|
|
192 |
}
|
|
193 |
|
|
194 |
/**************************************************************************/
|
|
195 |
|
|
196 |
static int
|
|
197 |
_rw_opt_enable_line (int argc, char *argv[])
|
|
198 |
{
|
|
199 |
if (1 == argc && argv && 0 == argv [0]) {
|
|
200 |
|
|
201 |
static const char helpstr[] = {
|
|
202 |
"Enables the line or lines specified by <arg>.\n"
|
|
203 |
"The syntax of <arg> is as follows: \n"
|
|
204 |
"<arg> ::= <range> [ , <range> ]\n"
|
|
205 |
"<range> ::= [ - ] <number> | <number> - [ <number> ]\n"
|
|
206 |
};
|
|
207 |
|
|
208 |
argv [0] = _RWSTD_CONST_CAST (char*, helpstr);
|
|
209 |
|
|
210 |
return 0;
|
|
211 |
}
|
|
212 |
|
|
213 |
return _rw_enable_line (argc, argv, l_enabled);
|
|
214 |
}
|
|
215 |
|
|
216 |
/**************************************************************************/
|
|
217 |
|
|
218 |
static int
|
|
219 |
_rw_opt_no_line (int argc, char *argv[])
|
|
220 |
{
|
|
221 |
if (1 == argc && argv && 0 == argv [0]) {
|
|
222 |
|
|
223 |
static const char helpstr[] = {
|
|
224 |
"Disables the line or lines specified by <arg>.\n"
|
|
225 |
"The syntax of <arg> is as follows: \n"
|
|
226 |
"<arg> ::= <range> [ , <range> ]\n"
|
|
227 |
"<range> ::= [ - ] <number> | <number> - [ <number> ]\n"
|
|
228 |
};
|
|
229 |
|
|
230 |
argv [0] = _RWSTD_CONST_CAST (char*, helpstr);
|
|
231 |
|
|
232 |
return 0;
|
|
233 |
}
|
|
234 |
|
|
235 |
return _rw_enable_line (argc, argv, l_disabled);
|
|
236 |
}
|
|
237 |
|
|
238 |
/**************************************************************************/
|
|
239 |
|
|
240 |
static int
|
|
241 |
_rw_opt_expect_line (int argc, char *argv[])
|
|
242 |
{
|
|
243 |
if (1 == argc && argv && 0 == argv [0]) {
|
|
244 |
|
|
245 |
static const char helpstr[] = {
|
|
246 |
"Marks the line or lines specified by <arg> as \"expected\".\n"
|
|
247 |
"Inactive diagnostics on such lines will be issued as unexpected.\n"
|
|
248 |
"The syntax of <arg> is as follows: \n"
|
|
249 |
"<arg> ::= <range> [ , <range> ]\n"
|
|
250 |
"<range> ::= [ - ] <number> | <number> - [ <number> ]\n"
|
|
251 |
};
|
|
252 |
|
|
253 |
argv [0] = _RWSTD_CONST_CAST (char*, helpstr);
|
|
254 |
|
|
255 |
return 0;
|
|
256 |
}
|
|
257 |
|
|
258 |
return _rw_enable_line (argc, argv, l_expected);
|
|
259 |
}
|
|
260 |
|
|
261 |
/************************************************************************/
|
|
262 |
|
|
263 |
/* extern */ int
|
|
264 |
_rw_setopts_lines ()
|
|
265 |
{
|
|
266 |
const int result =
|
|
267 |
rw_setopts ("|-enable-line=" // argument required
|
|
268 |
"|-expect= " // argument required
|
|
269 |
"|-no-line= ", // argument required
|
|
270 |
_rw_opt_enable_line,
|
|
271 |
_rw_opt_expect_line,
|
|
272 |
_rw_opt_no_line);
|
|
273 |
|
|
274 |
return result;
|
|
275 |
}
|
|
276 |
|
|
277 |
/************************************************************************/
|
|
278 |
|
|
279 |
/* extern */ int
|
|
280 |
_rw_expected (int line)
|
|
281 |
{
|
|
282 |
int line_expected = 0;
|
|
283 |
|
|
284 |
for (size_t i = 0; i != nlineranges; ++i) {
|
|
285 |
|
|
286 |
const int first = lineranges [i].first;
|
|
287 |
const int last = lineranges [i].last;
|
|
288 |
|
|
289 |
if (lineranges [i].flags & (l_disabled | l_enabled)) {
|
|
290 |
continue;
|
|
291 |
}
|
|
292 |
|
|
293 |
if (first <= line && line < last)
|
|
294 |
line_expected = 0 != (lineranges [i].flags & l_expected);
|
|
295 |
}
|
|
296 |
|
|
297 |
return line_expected;
|
|
298 |
}
|
|
299 |
|
|
300 |
/************************************************************************/
|
|
301 |
|
|
302 |
_TEST_EXPORT int
|
|
303 |
rw_enabled (int line)
|
|
304 |
{
|
|
305 |
int nenabled = 0;
|
|
306 |
int ndisabled = 0;
|
|
307 |
|
|
308 |
int line_enabled = -1;
|
|
309 |
|
|
310 |
for (size_t i = 0; i != nlineranges; ++i) {
|
|
311 |
|
|
312 |
const int first = lineranges [i].first;
|
|
313 |
const int last = lineranges [i].last;
|
|
314 |
|
|
315 |
if (lineranges [i].flags & l_disabled) {
|
|
316 |
++ndisabled;
|
|
317 |
}
|
|
318 |
else if (lineranges [i].flags & l_enabled) {
|
|
319 |
++nenabled;
|
|
320 |
}
|
|
321 |
else {
|
|
322 |
continue;
|
|
323 |
}
|
|
324 |
|
|
325 |
if (first <= line && line < last)
|
|
326 |
line_enabled = 0 != (lineranges [i].flags & l_enabled);
|
|
327 |
}
|
|
328 |
|
|
329 |
if (nenabled && -1 == line_enabled)
|
|
330 |
line_enabled = 0;
|
|
331 |
|
|
332 |
return line_enabled;
|
|
333 |
}
|