File: parse_options_getopt.c

package info (click to toggle)
detox 1.2.0-9
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 712 kB
  • ctags: 554
  • sloc: ansic: 2,447; makefile: 377; yacc: 283; sh: 169; lex: 109
file content (204 lines) | stat: -rw-r--r-- 4,786 bytes parent folder | download
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
/*
 * Copyright (c) 2005-2006, Doug Harple.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of author nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: parse_options_getopt.c,v 1.8 2006/07/03 16:45:54 purgedhalo Exp $
 *
 */

#include "config.h"

#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "detox.h"
#include "config.h"
#include "parse_options_generic.h"

#ifdef HAVE_GETOPT_LONG
#include <getopt.h>
#endif

enum {
	LONG_OPTION_DRY_RUN = 1,
	LONG_OPTION_REMOVE_TRAILING,
	LONG_OPTION_SPECIAL
};

/* expect this to be overwritten! */
static int long_option = 0;

#ifdef HAVE_GETOPT_LONG
static struct option longopts[] = {

	/* long options with equivalents */
	{"help", no_argument, 0, 'h'},
	{"dry-run", no_argument, 0, 'n'},

	/* long options without */
	{"special", no_argument, &long_option, LONG_OPTION_SPECIAL},

	/* deprecated long opts without */
	{"remove-trailing", no_argument, &long_option, LONG_OPTION_REMOVE_TRAILING},

	/* done */
	{0, 0, 0, 0}
};

#endif

struct detox_options *parse_options_getopt(int argc, char **argv)
{
	int optcode;

	struct detox_options *main_options;

	int i;
	int max = 10;

	main_options = initialize_main_options();
	if (main_options == NULL) {
		return NULL;
	}

#ifdef HAVE_GETOPT_LONG
	while ((optcode = getopt_long(argc, argv, "hrvV?Ls:f:n", longopts, NULL)) != -1) {
#else
	while ((optcode = getopt(argc, argv, "hrvV?Ls:f:n")) != -1) {
#endif
		switch (optcode) {
			case 'h':
				printf("%s", usage_message);
				printf("\n");
				printf("%s", help_message);
				exit(EXIT_SUCCESS);

			case 'f':
				/*
				 * XXX - free multiple check_config_files
				 */
				main_options->check_config_file = strdup(optarg);
				break;

			case 'L':
				main_options->list_sequences = 1;
				break;

			case 'n':
				main_options->dry_run = 1;
				break;

			case 'r':
				main_options->recurse = 1;
				break;

			case 's':
				/*
				 * XXX - free multiple sequence name opts
				 */
				main_options->sequence_name = strdup(optarg);
				break;

			case 'v':
				main_options->verbose++;
				break;

			case 'V':
				printf("%s\n", PACKAGE_STRING);
				exit(EXIT_SUCCESS);

			case '?':
				printf("%s", usage_message);
				exit(EXIT_SUCCESS);

			case 0:
				switch (long_option) {
					case LONG_OPTION_REMOVE_TRAILING:
						main_options->remove_trailing = 1;
						break;

					case LONG_OPTION_SPECIAL:
						main_options->special = 1;
						break;

					default:
						/*
						 * getopt_long shouldn't let us get here...
						 * verify?
						 */
						printf("unknown option: %s\n", optarg);
						break;
				}
				long_option = 0;	/* clean up! */
				break;

			default:
				fprintf(stderr, "unknown option: %c\n", optcode);
				exit(EXIT_FAILURE);
		}
	}

	if (main_options->list_sequences) {
		/*
		 * Early Retirement
		 */
		return main_options;
	}

	main_options->files = malloc(sizeof(char *) * 10);

	i = 0;
	max = 0;

	if (optind < argc) {
		while (optind < argc) {
			main_options->files[i++] = strdup(argv[optind]);
			if (i > max) {
				main_options->files = realloc(main_options->files, sizeof(char *) * (10 + max));
				max += 10;
			}

			optind++;
		}

		main_options->files[i] = NULL;
	}
	else {
#ifndef INLINE_MODE
		printf("%s", usage_message);
		exit(EXIT_FAILURE);
#endif
	}

	return main_options;
}