forked from wilhelmtell/inspext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.c
156 lines (150 loc) · 5.52 KB
/
options.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
/******************************************************************************
* Copyright (C) 2010 Matan Nassau
*
* This file is part of INSPext.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include <stdlib.h>
#include "string.h"
#include <getopt.h>
#include "options.h"
#include "generators.h"
char* options_error_s(options_error err)
{
if( err == OPTS_ERR_UNRECOGNIZED_TARGET )
return "unrecognized target"; /* FIXME: what target? */
else if( err == OPTS_ERR_OPEN_WRITE )
return "can't open file for writing"; /* FIXME: what file? */
else if( err == OPTS_ERR_UNKNOWN_OPTION )
return "invalid option"; /* FIXME: what option? */
else if( err == OPTS_ERR_OPEN_READ )
return "can't open file for reading"; /* FIXME: what file? */
else return "unknown commandline option error";
}
/* TODO: specify defaults in help message. create config.h ? */
void print_usage(void)
{
printf(" Usage: inspc [options]\n\n"
"-t, --target <arg> Compile to the given tool/file-format\n"
" [plaintext, latex, html]\n"
"-v, --verbose[=n] Output some trace info\n"
"-o, --output <out> Output to file <out>. Use - to specify\n"
" stdout (default)\n"
);
}
options_error parse_cl_opts(int argc, char* argv[], conf* opts)
{
int c;
extern int opterr;
options_error err = 0;
input_file* file, *pos;
const char * const plaintext = "plaintext";
const char * const latex = "latex";
const char * const html = "html";
const char * filename;
int filename_len = 0;
char* number_end; /* for strtol() */
opterr = 0;
opts->output_file = stdout; /* default */
while( 1 ) {
static struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "target", required_argument, 0, 't' },
{ "verbose", optional_argument, 0, 'v' },
{ "output", required_argument, 0, 'o' },
{ 0, 0, 0, 0 }
};
int option_index = 0;
int optarg_len = 0;
c = getopt_long(argc, argv, "ht:v::o:", long_options, &option_index);
if( c == -1 ) break;
switch( c ) {
case 'h':
print_usage();
exit(0);
break;
case 't':
optarg_len = strlen(optarg);
if( stristr(plaintext, optarg) == plaintext ) {
opts->gen = &gen_plain;
} else if( stristr(latex, optarg) == latex ) {
opts->gen = &gen_latex;
} else if( stristr(html, optarg) == html ) {
opts->gen = &gen_html;
} else {
err = OPTS_ERR_UNRECOGNIZED_TARGET;
opts->gen = NULL;
}
break;
case 'v':
/* verbose level. --vN or -v[v[...]] */
if( optarg == NULL ) { /* no arg given, but still -v given */
opts->verbose = VERBOSE_DEFAULT + 1;
} else {
opts->verbose = strtol(optarg, &number_end, 10);
if( number_end == optarg ) { /* strtol failed: count -vvv... */
optarg_len = strlen(optarg);
opts->verbose = 1 + optarg_len;
}
}
if( opts->verbose > VERBOSE_DEBUG )
opts->verbose = VERBOSE_DEBUG;
else if( opts->verbose < VERBOSE_FATAL )
opts->verbose = VERBOSE_FATAL;
break;
case 'o':
filename = optarg;
optarg_len = strlen(optarg);
if( strncmp(filename, "-", optarg_len) != 0 )
if( (opts->output_file = fopen(filename, "w")) == NULL )
err = OPTS_ERR_OPEN_WRITE;
break;
case '?':
err = OPTS_ERR_UNKNOWN_OPTION;
break;
default:
abort();
}
}
opts->input_files = (input_file*)malloc(sizeof(input_file));
opts->input_files->next = NULL;
opts->input_files->filename = NULL;
opts->input_files->stream = NULL;
pos = opts->input_files;
while( optind < argc ) { /* files to compile */
filename = argv[optind];
filename_len = strlen(filename);
file = (input_file*)malloc(sizeof(input_file));
file->filename = (char*)malloc(filename_len+1);
file->next = NULL;
strncpy(file->filename, filename, filename_len);
if( strncmp(filename, "-", filename_len) == 0 ) {
file->stream = stdin;
} else {
file->stream = fopen(filename, "r");
if( file->stream == NULL )
err = OPTS_ERR_OPEN_READ;
}
++optind;
if( file->stream != NULL ) {
pos->next = file;
pos = pos->next;
}
}
pos = opts->input_files;
opts->input_files = opts->input_files->next;
free(pos);
return err;
}