forked from Kitware/VTK
/
vtkParseMain.c
545 lines (486 loc) · 13.8 KB
/
vtkParseMain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*=========================================================================
Program: Visualization Toolkit
Module: vtkParseMain.c
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*
This file provides a unified front-end for the wrapper generators.
*/
#include "vtkParse.h"
#include "vtkParseData.h"
#include "vtkParseMain.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* This is the struct that contains the options */
OptionInfo options;
/* Get the base filename */
static const char *parse_exename(const char *cmd)
{
const char *exename;
/* remove directory part of exe name */
for (exename = cmd + strlen(cmd); exename > cmd; --exename)
{
char pc = exename[-1];
if (pc == ':' || pc == '/' || pc == '\\')
{
break;
}
}
return exename;
}
/* Print the help */
static void parse_print_help(FILE *fp, const char *cmd, int multi)
{
fprintf(fp,
"Usage: %s [options] infile... \n"
" --help print this help message\n"
" --version print the VTK version\n"
" -o <file> the output file\n"
" -I <dir> add an include directory\n"
" -D <macro[=def]> define a preprocessor macro\n"
" -U <macro> undefine a preprocessor macro\n"
" @<file> read arguments from a file\n",
parse_exename(cmd));
/* args for describing a singe header file input */
if (!multi)
{
fprintf(fp,
" --hints <file> the hints file to use\n"
" --types <file> the type hierarchy file to use\n"
" --concrete force concrete class (ignored, deprecated)\n"
" --abstract force abstract class (ignored, deprecated)\n"
" --vtkobject vtkObjectBase-derived class (ignored, deprecated)\n"
" --special non-vtkObjectBase class (ignored, deprecated)\n");
}
}
/* append an arg to the arglist */
static void parse_append_arg(int *argn, char ***args, char *arg)
{
/* if argn is a power of two, allocate more space */
if (*argn > 0 && (*argn & (*argn - 1)) == 0)
{
*args = (char **)realloc(*args, 2*(*argn)*sizeof(char *));
}
/* append argument to list */
(*args)[*argn] = arg;
(*argn)++;
}
/* read options from a file, return zero on error */
static int read_option_file(
StringCache *strings, const char *filename, int *argn, char ***args)
{
static int option_file_stack_max = 10;
static int option_file_stack_size = 0;
static const char *option_file_stack[10];
FILE *fp;
char *line;
const char *ccp;
char *argstring;
char *arg;
size_t maxlen = 15;
size_t i, n;
int j;
int in_string;
fp = fopen(filename, "r");
if (fp == NULL)
{
return 0;
}
line = (char *)malloc(maxlen);
/* read the file line by line */
while (fgets(line, (int)maxlen, fp))
{
n = strlen(line);
/* if buffer not long enough, increase it */
while (n == maxlen-1 && line[n-1] != '\n' && !feof(fp))
{
char *oldline = line;
maxlen *= 2;
line = (char *)realloc(line, maxlen);
if (!line)
{
free(oldline);
fclose(fp);
return 0;
}
if (!fgets(&line[n], (int)(maxlen-n), fp)) { break; }
n += strlen(&line[n]);
}
/* allocate a string to hold the parsed arguments */
argstring = vtkParse_NewString(strings, n);
arg = argstring;
i = 0;
/* break the line into individual options */
ccp = line;
in_string = 0;
while (*ccp != '\0')
{
for (;;)
{
if (*ccp == '\\')
{
ccp++;
}
else if (*ccp == '\"' || *ccp == '\'')
{
if (!in_string)
{
in_string = *ccp++;
continue;
}
else if (*ccp == in_string)
{
in_string = 0;
ccp++;
continue;
}
}
else if (!in_string && isspace(*ccp))
{
do { ccp++; } while (isspace(*ccp));
break;
}
if (*ccp == '\0')
{
break;
}
/* append character to argument */
arg[i++] = *ccp++;
}
arg[i++] = '\0';
if (arg[0] == '@')
{
/* recursively expand '@file' option */
if (option_file_stack_size == option_file_stack_max)
{
fprintf(stderr, "%s: @file recursion is too deep.\n",
(*args)[0]);
exit(1);
}
/* avoid reading the same file recursively */
option_file_stack[option_file_stack_size++] = filename;
for (j = 0; j < option_file_stack_size; j++)
{
if (strcmp(&arg[1], option_file_stack[j]) == 0)
{
break;
}
}
if (j < option_file_stack_size)
{
parse_append_arg(argn, args, arg);
}
else if (read_option_file(strings, &arg[1], argn, args) == 0)
{
parse_append_arg(argn, args, arg);
}
option_file_stack_size--;
}
else if (arg[0] != '\0')
{
parse_append_arg(argn, args, arg);
}
/* prepare for next arg */
arg += i;
i = 0;
}
}
fclose(fp);
return 1;
}
/* expand any "@file" args that occur in the command-line args */
static void parse_expand_args(
StringCache *strings, int argc, char *argv[], int *argn, char ***args)
{
int i;
*argn = 0;
*args = (char **)malloc(sizeof(char *));
for (i = 0; i < argc; i++)
{
/* check for "@file" unless this is the command name */
if (i > 0 || argv[i][0] == '@')
{
/* if read_option_file returns null, add "@file" to the args */
/* (this mimics the way that gcc expands @file arguments) */
if (read_option_file(strings, &argv[i][1], argn, args) == 0)
{
parse_append_arg(argn, args, argv[i]);
}
}
else
{
/* append any other arg */
parse_append_arg(argn, args, argv[i]);
}
}
}
/* Check the options: "multi" should be zero for wrapper tools that
* only take one input file, or one for wrapper tools that take multiple
* input files. Returns zero for "--version" or "--help", or returns -1
* if an error occurred. Otherwise, it returns the number of args
* that were successfully parsed. */
static int parse_check_options(int argc, char *argv[], int multi)
{
int i;
size_t j;
char *cp;
char c;
options.NumberOfFiles = 0;
options.Files = NULL;
options.InputFileName = NULL;
options.OutputFileName = NULL;
options.HierarchyFileName = 0;
options.NumberOfHierarchyFileNames = 0;
options.HierarchyFileNames = NULL;
options.NumberOfHintFileNames = 0;
options.HintFileNames = NULL;
for (i = 1; i < argc; i++)
{
if (strcmp(argv[i], "--help") == 0)
{
parse_print_help(stdout, argv[0], multi);
return 0;
}
else if (strcmp(argv[i], "--version") == 0)
{
const char *ver = VTK_PARSE_VERSION;
fprintf(stdout, "%s %s\n", parse_exename(argv[0]), ver);
return 0;
}
else if (argv[i][0] != '-')
{
if (options.NumberOfFiles == 0)
{
options.Files = (char **)malloc(sizeof(char *));
}
else if ((options.NumberOfFiles & (options.NumberOfFiles - 1)) == 0)
{
options.Files = (char **)realloc(
options.Files, 2*options.NumberOfFiles*sizeof(char *));
}
options.Files[options.NumberOfFiles++] = argv[i];
}
else if (argv[i][0] == '-' && isalpha(argv[i][1]))
{
c = argv[i][1];
cp = &argv[i][2];
if (*cp == '\0')
{
i++;
if (i >= argc || argv[i][0] == '-')
{
return -1;
}
cp = argv[i];
}
if (c == 'o')
{
options.OutputFileName = cp;
}
else if (c == 'I')
{
vtkParse_IncludeDirectory(cp);
}
else if (c == 'D')
{
j = 0;
while (cp[j] != '\0' && cp[j] != '=') { j++; }
if (cp[j] == '=') { j++; }
vtkParse_DefineMacro(cp, &cp[j]);
}
else if (c == 'U')
{
vtkParse_UndefineMacro(cp);
}
}
else if (!multi && strcmp(argv[i], "--hints") == 0)
{
i++;
if (i >= argc || argv[i][0] == '-')
{
return -1;
}
if (options.NumberOfHintFileNames == 0)
{
options.HintFileNames = (char **)malloc(sizeof(char *));
}
else if ((options.NumberOfHintFileNames & (options.NumberOfHintFileNames - 1)) == 0)
{
options.HintFileNames = (char **)realloc(
options.HintFileNames, 2 * options.NumberOfHintFileNames*sizeof(char *));
}
options.HintFileNames[options.NumberOfHintFileNames++] = argv[i];
}
else if (!multi && strcmp(argv[i], "--types") == 0)
{
i++;
if (i >= argc || argv[i][0] == '-')
{
return -1;
}
if (options.NumberOfHierarchyFileNames == 0)
{
options.HierarchyFileNames = (char **)malloc(sizeof(char *));
options.HierarchyFileName = argv[i]; // legacy
}
else if ((options.NumberOfHierarchyFileNames & (options.NumberOfHierarchyFileNames - 1)) == 0)
{
options.HierarchyFileNames = (char **)realloc(
options.HierarchyFileNames, 2*options.NumberOfHierarchyFileNames*sizeof(char *));
}
options.HierarchyFileNames[options.NumberOfHierarchyFileNames++] = argv[i];
}
else if (strcmp(argv[i], "--vtkobject") == 0 ||
strcmp(argv[i], "--special") == 0 ||
strcmp(argv[i], "--abstract") == 0 ||
strcmp(argv[i], "--concrete") == 0)
{
fprintf(stderr, "Warning: the %s option is deprecated "
"and will be ignored.\n", argv[i]);
}
}
return i;
}
/* Return a pointer to the static OptionInfo struct */
OptionInfo *vtkParse_GetCommandLineOptions()
{
return &options;
}
/* Command-line argument handler for wrapper tools */
FileInfo *vtkParse_Main(int argc, char *argv[])
{
int argi;
int expected_files;
FILE *ifile;
FILE *hfile = 0;
int nhfiles;
int ihfiles;
const char *hfilename;
FileInfo *data;
StringCache strings;
int argn;
char **args;
/* set the command name for diagnostics */
vtkParse_SetCommandName(parse_exename(argv[0]));
/* pre-define the __VTK_WRAP__ macro */
vtkParse_DefineMacro("__VTK_WRAP__", 0);
/* expand any "@file" args */
vtkParse_InitStringCache(&strings);
parse_expand_args(&strings, argc, argv, &argn, &args);
/* read the args into the static OptionInfo struct */
argi = parse_check_options(argn, args, 0);
/* was output file already specified by the "-o" option? */
expected_files = (options.OutputFileName == NULL ? 2 : 1);
/* verify number of args, print usage if not valid */
if (argi == 0)
{
free(args);
exit(0);
}
else if (argi < 0 || options.NumberOfFiles != expected_files)
{
parse_print_help(stderr, args[0], 0);
exit(1);
}
/* open the input file */
options.InputFileName = options.Files[0];
if (!(ifile = fopen(options.InputFileName, "r")))
{
fprintf(stderr, "Error opening input file %s\n", options.InputFileName);
exit(1);
}
if (options.OutputFileName == NULL &&
options.NumberOfFiles > 1)
{
/* allow outfile to be given after infile, if "-o" option not used */
options.OutputFileName = options.Files[1];
fprintf(stderr, "Deprecated: specify output file with \"-o\".\n");
}
/* free the expanded args */
free(args);
/* make sure than an output file was given on the command line */
if (options.OutputFileName == NULL)
{
fprintf(stderr, "No output file was specified\n");
fclose(ifile);
exit(1);
}
/* parse the input file */
data = vtkParse_ParseFile(options.InputFileName, ifile, stderr);
if (!data)
{
exit(1);
}
/* open and parse each hint file, if given on the command line */
nhfiles = options.NumberOfHintFileNames;
for (ihfiles = 0; ihfiles < nhfiles; ihfiles++)
{
hfilename = options.HintFileNames[ihfiles];
if (hfilename && hfilename[0] != '\0')
{
if (!(hfile = fopen(hfilename, "r")))
{
fprintf(stderr, "Error opening hint file %s\n", hfilename);
fclose(ifile);
vtkParse_FreeFile(data);
exit(1);
}
/* fill in some blanks by using the hints file */
vtkParse_ReadHints(data, hfile, stderr);
}
}
if (data->MainClass)
{
/* mark class as abstract unless it has New() method */
int nfunc = data->MainClass->NumberOfFunctions;
int ifunc;
for (ifunc = 0; ifunc < nfunc; ifunc++)
{
FunctionInfo *func = data->MainClass->Functions[ifunc];
if (func && func->Access == VTK_ACCESS_PUBLIC &&
func->Name && strcmp(func->Name, "New") == 0 &&
func->NumberOfParameters == 0)
{
break;
}
}
data->MainClass->IsAbstract = ((ifunc == nfunc) ? 1 : 0);
}
return data;
}
/* Command-line argument handler for wrapper tools */
void vtkParse_MainMulti(int argc, char *argv[])
{
int argi;
int argn;
char **args;
StringCache strings;
/* set the command name for diagnostics */
vtkParse_SetCommandName(parse_exename(argv[0]));
/* pre-define the __VTK_WRAP__ macro */
vtkParse_DefineMacro("__VTK_WRAP__", 0);
/* expand any "@file" args */
vtkParse_InitStringCache(&strings);
parse_expand_args(&strings, argc, argv, &argn, &args);
/* read the args into the static OptionInfo struct */
argi = parse_check_options(argn, args, 1);
free(args);
if (argi == 0)
{
exit(0);
}
else if (argi < 0 || options.NumberOfFiles == 0)
{
parse_print_help(stderr, argv[0], 1);
exit(1);
}
/* the input file */
options.InputFileName = options.Files[0];
}