Skip to content

Commit 2c9e3ed

Browse files
committed
cmdline
1 parent dbf0c3b commit 2c9e3ed

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

src/cmdline.c

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ extern int optind; /* for getopt() */
6060
#endif
6161

6262
#define EXTRA_UNDOC "(undoc)"
63-
#define EXTRA_DEBUG "debug:"
63+
#define EXTRA_DEBUG "debug"
6464

6565

6666

@@ -111,7 +111,7 @@ void usage_long(FILE *st)
111111
fprintf(st, " -t, --tabs <str> Tab stop distance and expansion [default: %de]\n", DEF_TABSTOP);
112112
fprintf(st, " -v, --version Print version information\n");
113113
/* fprintf(st, " -x, --extra <arg> If <arg> starts with "debug:", activate debug logging for specified log
114-
areas which follow in a comma-separated list [default area: MAIN]. If <arg> is "undoc", trigger
114+
areas which follow in a comma-separated list [default area: MAIN]. If <arg> is "(undoc)", trigger
115115
undocumented behavior of design detail lister."); // undocumented */
116116

117117
bxs_free(config_file);
@@ -562,6 +562,48 @@ static int tab_handling(opt_t *result, char *optarg)
562562

563563

564564

565+
/**
566+
* Handle undocumented options (-x).
567+
* @param result the options struct we are building
568+
* @param optarg the argument to `-x` on the command line
569+
* @returns 0 on success, anything else on error
570+
*/
571+
static int undocumented_options(opt_t *result, char *optarg)
572+
{
573+
char *s = optarg;
574+
575+
if (strncmp(s, EXTRA_UNDOC, strlen(EXTRA_UNDOC)) == 0) {
576+
result->qundoc = 1;
577+
s += strlen(EXTRA_UNDOC);
578+
}
579+
580+
if (strncasecmp(s, EXTRA_DEBUG, strlen(EXTRA_DEBUG)) == 0) {
581+
s += strlen(EXTRA_DEBUG);
582+
if (*s == ':') {
583+
if (debug_areas(result, s + 1) != 0) {
584+
return 1;
585+
}
586+
}
587+
else if (*s == '\0') {
588+
/* default to MAIN */
589+
debug_areas(result, NULL);
590+
}
591+
else {
592+
bx_fprintf(stderr, "%s: invalid option -x %s\n", PROJECT, optarg);
593+
return 2;
594+
}
595+
activate_debug_logging(result->debug);
596+
}
597+
598+
if (!result->qundoc && !is_debug_activated()) {
599+
bx_fprintf(stderr, "%s: invalid option -x %s\n", PROJECT, optarg);
600+
return 3;
601+
}
602+
return 0;
603+
}
604+
605+
606+
565607
/**
566608
* Set *stdout* to binary mode, so that we can control the line terminator. This function only ever does anything on
567609
* Windows, because on Linux, we already do have control over line terminators.
@@ -733,7 +775,6 @@ opt_t *process_commandline(int argc, char *argv[])
733775
};
734776
const char *short_options = "a:c:d:e:f:hi:k:lmn:p:q:rs:t:vx:";
735777

736-
char *s;
737778
int oc; /* option character */
738779
do {
739780
oc = getopt_long(argc, argv, short_options, long_options, &option_index);
@@ -866,18 +907,9 @@ opt_t *process_commandline(int argc, char *argv[])
866907
return result;
867908

868909
case 'x':
869-
s = optarg;
870-
if (strncmp(s, EXTRA_UNDOC, strlen(EXTRA_UNDOC)) == 0) {
871-
result->qundoc = 1;
872-
s += strlen(EXTRA_UNDOC);
873-
}
874-
if (strncasecmp(s, EXTRA_DEBUG, strlen(EXTRA_DEBUG)) == 0) {
875-
s += strlen(EXTRA_DEBUG);
876-
if (debug_areas(result, s) != 0) {
877-
BFREE(result);
878-
return NULL;
879-
}
880-
activate_debug_logging(result->debug);
910+
if (undocumented_options(result, optarg) != 0) {
911+
BFREE(result);
912+
return NULL;
881913
}
882914
break;
883915

0 commit comments

Comments
 (0)