| 1 |
/** |
| 2 |
* This file is part of the Detox package. |
| 3 |
* |
| 4 |
* Copyright (c) Doug Harple <[email protected]> |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "config.h" |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
#include <libgen.h> |
| 16 |
|
| 17 |
#include "detox_struct.h" |
| 18 |
|
| 19 |
#include "filelist.h" |
| 20 |
#include "parse_options.h" |
| 21 |
#include "wrapped.h" |
| 22 |
|
| 23 |
#ifdef HAVE_GETOPT_LONG |
| 24 |
#include <getopt.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
enum { |
| 28 |
LONG_OPTION_INLINE = 1, |
| 29 |
LONG_OPTION_RECURSIVE, |
| 30 |
LONG_OPTION_SPECIAL |
| 31 |
}; |
| 32 |
|
| 33 |
#define INLINE_DETOX_BIN "inline-detox" |
| 34 |
|
| 35 |
/* expect this to be overwritten! */ |
| 36 |
static int long_option = 0; |
| 37 |
|
| 38 |
#ifdef HAVE_GETOPT_LONG |
| 39 |
static struct option longopts[] = { |
| 40 |
|
| 41 |
/* long options with equivalents */ |
| 42 |
{"dry-run", no_argument, NULL, 'n'}, |
| 43 |
{"help", no_argument, NULL, 'h'}, |
| 44 |
{"verbose", no_argument, NULL, 'v'}, |
| 45 |
|
| 46 |
/* long options without */ |
| 47 |
{"inline", no_argument, &long_option, LONG_OPTION_INLINE}, |
| 48 |
{"recursive", no_argument, &long_option, LONG_OPTION_RECURSIVE}, |
| 49 |
{"special", no_argument, &long_option, LONG_OPTION_SPECIAL}, |
| 50 |
|
| 51 |
/* done */ |
| 52 |
{NULL, 0, NULL, 0} |
| 53 |
}; |
| 54 |
|
| 55 |
#endif |
| 56 |
|
| 57 |
/* *INDENT-OFF* */ |
| 58 |
|
| 59 |
char usage_message[] = { |
| 60 |
"usage: detox [-hLnrvV] [-f configfile] [-s sequence]" |
| 61 |
#ifdef HAVE_GETOPT_LONG |
| 62 |
" [--dry-run] [--help] [--inline] [--recursive] [--special] [--verbose]" |
| 63 |
"\n\t " |
| 64 |
#endif |
| 65 |
" file [file ...]\n" |
| 66 |
}; |
| 67 |
|
| 68 |
char help_message[] = { |
| 69 |
" -f configfile choose which config file to use\n" |
| 70 |
#ifdef HAVE_GETOPT_LONG |
| 71 |
" -h --help this message\n" |
| 72 |
#else |
| 73 |
" -h this message\n" |
| 74 |
#endif |
| 75 |
#ifdef HAVE_GETOPT_LONG |
| 76 |
" --inline run inline mode\n" |
| 77 |
#endif |
| 78 |
" -L list available sequences and exit\n" |
| 79 |
" with -v ... dump sequence contents\n" |
| 80 |
#ifdef HAVE_GETOPT_LONG |
| 81 |
" -n --dry-run do a dry run (don't actually do anything)\n" |
| 82 |
" -r --recursive be recursive (descend into subdirectories)\n" |
| 83 |
#else |
| 84 |
" -n do a dry run (don't actually do anything)\n" |
| 85 |
" -r be recursive (descend into subdirectories)\n" |
| 86 |
#endif |
| 87 |
" -s sequence choose which sequence to detox with\n" |
| 88 |
#ifdef HAVE_GETOPT_LONG |
| 89 |
" --special work on links and special files\n" |
| 90 |
#endif |
| 91 |
" -v --verbose be verbose\n" |
| 92 |
" -V show the current version\n" |
| 93 |
}; |
| 94 |
|
| 95 |
char usage_message_inline[] = { |
| 96 |
"usage: inline-detox [-hLvV] [-f configfile] [-s sequence] [file]\n" |
| 97 |
}; |
| 98 |
|
| 99 |
char help_message_inline[] = { |
| 100 |
" -f configfile choose which config file to use\n" |
| 101 |
#ifdef HAVE_GETOPT_LONG |
| 102 |
" -h --help this message\n" |
| 103 |
#else |
| 104 |
" -h this message\n" |
| 105 |
#endif |
| 106 |
" -L list available sequences and exit\n" |
| 107 |
" with -v ... dump sequence contents\n" |
| 108 |
" -s sequence choose which sequence to detox with\n" |
| 109 |
" -v --verbose be verbose\n" |
| 110 |
" -V show the current version\n" |
| 111 |
}; |
| 112 |
|
| 113 |
/* *INDENT-ON* */ |
| 114 |
|
| 115 |
options_t *options_init(void) |
| 116 |
{ |
| 117 |
options_t *ret; |
| 118 |
|
| 119 |
ret = wrapped_malloc(sizeof(options_t)); |
| 120 |
|
| 121 |
memset(ret, 0, sizeof(options_t)); |
| 122 |
|
| 123 |
return ret; |
| 124 |
} |
| 125 |
|
| 126 |
options_t *parse_options_getopt(int argc, char **argv) |
| 127 |
{ |
| 128 |
int optcode; |
| 129 |
options_t *main_options; |
| 130 |
char *binname; |
| 131 |
|
| 132 |
main_options = options_init(); |
| 133 |
main_options->sequence_name = getenv("DETOX_SEQUENCE"); |
| 134 |
|
| 135 |
binname = basename(argv[0]); |
| 136 |
main_options->is_inline_bin = main_options->is_inline_mode = |
| 137 |
(strcmp(binname, INLINE_DETOX_BIN) == 0); |
| 138 |
|
| 139 |
#ifdef HAVE_GETOPT_LONG |
| 140 |
while ((optcode = getopt_long(argc, argv, "hrvV?Ls:f:n", longopts, NULL)) != -1) { |
| 141 |
#else |
| 142 |
while ((optcode = getopt(argc, argv, "hrvV?Ls:f:n")) != -1) { |
| 143 |
#endif |
| 144 |
switch (optcode) { |
| 145 |
case 'h': |
| 146 |
printf("%s", !main_options->is_inline_bin ? usage_message : |
| 147 |
usage_message_inline); |
| 148 |
printf("\n"); |
| 149 |
printf("%s", !main_options->is_inline_bin ? help_message : |
| 150 |
help_message_inline); |
| 151 |
exit(EXIT_SUCCESS); |
| 152 |
|
| 153 |
case 'f': |
| 154 |
/* |
| 155 |
* XXX - free multiple check_config_files |
| 156 |
*/ |
| 157 |
main_options->check_config_file = wrapped_strdup(optarg); |
| 158 |
break; |
| 159 |
|
| 160 |
case 'L': |
| 161 |
main_options->list_sequences = 1; |
| 162 |
break; |
| 163 |
|
| 164 |
case 'n': |
| 165 |
main_options->dry_run = 1; |
| 166 |
break; |
| 167 |
|
| 168 |
case 'r': |
| 169 |
main_options->recurse = 1; |
| 170 |
break; |
| 171 |
|
| 172 |
case 's': |
| 173 |
/* |
| 174 |
* XXX - free multiple sequence name opts |
| 175 |
*/ |
| 176 |
main_options->sequence_name = wrapped_strdup(optarg); |
| 177 |
break; |
| 178 |
|
| 179 |
case 'v': |
| 180 |
main_options->verbose++; |
| 181 |
break; |
| 182 |
|
| 183 |
case 'V': |
| 184 |
printf("%s\n", PACKAGE_STRING); |
| 185 |
exit(EXIT_SUCCESS); |
| 186 |
|
| 187 |
case '?': |
| 188 |
printf("%s", !main_options->is_inline_bin ? usage_message : |
| 189 |
usage_message_inline); |
| 190 |
exit(EXIT_SUCCESS); |
| 191 |
|
| 192 |
case 0: |
| 193 |
switch (long_option) { |
| 194 |
case LONG_OPTION_INLINE: |
| 195 |
main_options->is_inline_mode = 1; |
| 196 |
break; |
| 197 |
|
| 198 |
case LONG_OPTION_RECURSIVE: |
| 199 |
main_options->recurse = 1; |
| 200 |
break; |
| 201 |
|
| 202 |
case LONG_OPTION_SPECIAL: |
| 203 |
main_options->special = 1; |
| 204 |
break; |
| 205 |
|
| 206 |
default: |
| 207 |
/* |
| 208 |
* getopt_long shouldn't let us get here... |
| 209 |
* verify? |
| 210 |
*/ |
| 211 |
printf("unknown option: %s\n", optarg); |
| 212 |
break; |
| 213 |
} |
| 214 |
long_option = 0; /* clean up! */ |
| 215 |
break; |
| 216 |
|
| 217 |
default: |
| 218 |
fprintf(stderr, "unknown option: %c\n", optcode); |
| 219 |
exit(EXIT_FAILURE); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if (main_options->list_sequences) { |
| 224 |
/* |
| 225 |
* Early Retirement |
| 226 |
*/ |
| 227 |
return main_options; |
| 228 |
} |
| 229 |
|
| 230 |
main_options->files = filelist_init(); |
| 231 |
|
| 232 |
if (optind < argc) { |
| 233 |
while (optind < argc) { |
| 234 |
filelist_put(main_options->files, argv[optind]); |
| 235 |
optind++; |
| 236 |
} |
| 237 |
} else if (!main_options->is_inline_mode) { |
| 238 |
printf("%s", !main_options->is_inline_bin ? usage_message : |
| 239 |
usage_message_inline); |
| 240 |
exit(EXIT_FAILURE); |
| 241 |
} |
| 242 |
|
| 243 |
return main_options; |
| 244 |
} |