all: Fix command like parsing when the argument has an option.

This was a longstanding bug.  Oops.
This commit is contained in:
Solomon Peachy 2015-06-30 21:33:02 -04:00
parent 14af6acbb9
commit 992e551a02
9 changed files with 139 additions and 88 deletions

3
README
View File

@ -450,7 +450,8 @@
-N [ A | B | M ] Reset Counter A/B/M [2] -N [ A | B | M ] Reset Counter A/B/M [2]
-p num Set 'P' counter to 'num' -p num Set 'P' counter to 'num'
-s Query printer status -s Query printer status
-S num Specify standby delay (1-99 minutes, 0 disables) [1] -k num Specify standby delay (1-99 minutes, 0 disables) [1]
-K num Keep Media Status Across Power Cycles (1 yes, 0 no) [1]
Notes: Notes:

View File

@ -847,7 +847,7 @@ static int cw01_cmdline_arg(void *vctx, int argc, char **argv)
j = cw01_clear_counter(ctx, optarg[0]); j = cw01_clear_counter(ctx, optarg[0]);
break; break;
} }
return 1; return 2;
case 's': case 's':
if (ctx) { if (ctx) {
j = cw01_get_status(ctx); j = cw01_get_status(ctx);

View File

@ -27,7 +27,7 @@
#include "backend_common.h" #include "backend_common.h"
#define BACKEND_VERSION "0.54" #define BACKEND_VERSION "0.55"
#ifndef URI_PREFIX #ifndef URI_PREFIX
#error "Must Define URI_PREFIX" #error "Must Define URI_PREFIX"
#endif #endif
@ -799,7 +799,14 @@ int main (int argc, char **argv)
/* Check to see if it is claimed by the backend */ /* Check to see if it is claimed by the backend */
if (backend && backend->cmdline_arg) { if (backend && backend->cmdline_arg) {
int keep = optind; int keep = optind;
backend_cmd += backend->cmdline_arg(NULL, argc, argv); int boo;
boo = backend->cmdline_arg(NULL, argc, argv);
backend_cmd += boo;
if (boo > 1)
keep++;
optind = keep; optind = keep;
} }
break; break;

View File

@ -1507,6 +1507,23 @@ static int dnpds620_standby_mode(struct dnpds40_ctx *ctx, int delay)
return 0; return 0;
} }
static int dnpds620_media_keep_mode(struct dnpds40_ctx *ctx, int delay)
{
struct dnpds40_cmd cmd;
char msg[9];
int ret;
/* Generate command */
dnpds40_build_cmd(&cmd, "MNT_WT", "END_KEEP_MODE", 4);
snprintf(msg, sizeof(msg), "%02d\r", delay);
if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)msg, 4)))
return ret;
return 0;
}
static int dnpds40_set_counter_p(struct dnpds40_ctx *ctx, char *arg) static int dnpds40_set_counter_p(struct dnpds40_ctx *ctx, char *arg)
{ {
struct dnpds40_cmd cmd; struct dnpds40_cmd cmd;
@ -1531,8 +1548,8 @@ static void dnpds40_cmdline(void)
DEBUG("\t\t[ -n ] # Query counters\n"); DEBUG("\t\t[ -n ] # Query counters\n");
DEBUG("\t\t[ -N A|B|M ] # Clear counter A/B/M\n"); DEBUG("\t\t[ -N A|B|M ] # Clear counter A/B/M\n");
DEBUG("\t\t[ -p num ] # Set counter P\n"); DEBUG("\t\t[ -p num ] # Set counter P\n");
DEBUG("\t\t[ -S num ] # Set standby time (1-99 minutes, 0 disables)\n"); DEBUG("\t\t[ -S num ] # Set standby time (1-99 minutes, 0 disables)\n");
DEBUG("\t\t[ -K num ] # Keep Media Status Across Power Cycles (1 on, 0 off)\n");
} }
static int dnpds40_cmdline_arg(void *vctx, int argc, char **argv) static int dnpds40_cmdline_arg(void *vctx, int argc, char **argv)
@ -1543,7 +1560,7 @@ static int dnpds40_cmdline_arg(void *vctx, int argc, char **argv)
/* Reset arg parsing */ /* Reset arg parsing */
optind = 1; optind = 1;
opterr = 0; opterr = 0;
while ((i = getopt(argc, argv, "iInN:p:sS:")) >= 0) { while ((i = getopt(argc, argv, "iInN:p:sK:k:")) >= 0) {
switch(i) { switch(i) {
case 'i': case 'i':
if (ctx) { if (ctx) {
@ -1556,6 +1573,7 @@ static int dnpds40_cmdline_arg(void *vctx, int argc, char **argv)
j = dnpds40_get_sensors(ctx); j = dnpds40_get_sensors(ctx);
break; break;
} }
return 1;
case 'n': case 'n':
if (ctx) { if (ctx) {
j = dnpds40_get_counters(ctx); j = dnpds40_get_counters(ctx);
@ -1575,20 +1593,20 @@ static int dnpds40_cmdline_arg(void *vctx, int argc, char **argv)
j = dnpds40_clear_counter(ctx, optarg[0]); j = dnpds40_clear_counter(ctx, optarg[0]);
break; break;
} }
return 1; return 2;
case 'p': case 'p':
if (ctx) { if (ctx) {
j = dnpds40_set_counter_p(ctx, optarg); j = dnpds40_set_counter_p(ctx, optarg);
break; break;
} }
return 1; return 2;
case 's': case 's':
if (ctx) { if (ctx) {
j = dnpds40_get_status(ctx); j = dnpds40_get_status(ctx);
break; break;
} }
return 1; return 1;
case 'S': case 'k':
if (ctx) { if (ctx) {
int time = atoi(optarg); int time = atoi(optarg);
if (!ctx->supports_standby) { if (!ctx->supports_standby) {
@ -1603,6 +1621,23 @@ static int dnpds40_cmdline_arg(void *vctx, int argc, char **argv)
} }
j = dnpds620_standby_mode(ctx, time); j = dnpds620_standby_mode(ctx, time);
} }
return 2;
case 'K':
if (ctx) {
int keep = atoi(optarg);
if (!ctx->supports_standby) {
ERROR("Printer does not support media keep mode\n");
j = -1;
break;
}
if (keep < 0 || keep > 1) {
ERROR("Value out of range (0-1)");
j = -1;
break;
}
j = dnpds620_media_keep_mode(ctx, keep);
}
return 2;
default: default:
break; /* Ignore completely */ break; /* Ignore completely */
} }

View File

@ -268,7 +268,7 @@ int kodak1400_cmdline_arg(void *vctx, int argc, char **argv)
j = kodak1400_set_tonecurve(ctx, optarg); j = kodak1400_set_tonecurve(ctx, optarg);
break; break;
} }
return 1; return 2;
default: default:
break; /* Ignore completely */ break; /* Ignore completely */
} }

View File

@ -517,7 +517,7 @@ static int kodak605_cmdline_arg(void *vctx, int argc, char **argv)
j = kodak605_set_tonecurve(ctx, optarg); j = kodak605_set_tonecurve(ctx, optarg);
break; break;
} }
return 1; return 2;
case 'm': case 'm':
if (ctx) { if (ctx) {
j = kodak605_get_media(ctx); j = kodak605_get_media(ctx);

View File

@ -592,13 +592,13 @@ static int kodak6800_cmdline_arg(void *vctx, int argc, char **argv)
j = kodak6800_get_tonecurve(ctx, optarg); j = kodak6800_get_tonecurve(ctx, optarg);
break; break;
} }
return 1; return 2;
case 'C': case 'C':
if (ctx) { if (ctx) {
j = kodak6800_set_tonecurve(ctx, optarg); j = kodak6800_set_tonecurve(ctx, optarg);
break; break;
} }
return 1; return 2;
case 'm': case 'm':
if (ctx) { if (ctx) {
uint8_t mediabuf[MAX_MEDIA_LEN]; uint8_t mediabuf[MAX_MEDIA_LEN];

View File

@ -1148,75 +1148,83 @@ int shinkos1245_cmdline_arg(void *vctx, int argc, char **argv)
while ((i = getopt(argc, argv, "c:C:l:L:FfmsuU:X:")) >= 0) { while ((i = getopt(argc, argv, "c:C:l:L:FfmsuU:X:")) >= 0) {
switch(i) { switch(i) {
case 'F': case 'F':
if (!ctx) if (ctx) {
return 1; ctx->tonecurve = PARAM_TABLE_FINE;
ctx->tonecurve = PARAM_TABLE_FINE; break;
break;
case 'c':
if (!ctx)
return 1;
j = get_tonecurve(ctx, TONE_TABLE_USER, ctx->tonecurve, optarg);
break;
case 'C':
if (!ctx)
return 1;
j = set_tonecurve(ctx, TONE_TABLE_USER, ctx->tonecurve, optarg);
break;
case 'l':
if (!ctx)
return 1;
j = get_tonecurve(ctx, TONE_TABLE_CURRENT, ctx->tonecurve, optarg);
break;
case 'L':
if (!ctx)
return 1;
j = set_tonecurve(ctx, TONE_TABLE_CURRENT, ctx->tonecurve, optarg);
break;
case 'f':
if (!ctx)
return 1;
ctx->fast_return = 1;
break;
case 'm':
if (!ctx)
return 1;
j = shinkos1245_get_media(ctx);
if (!j)
shinkos1245_dump_media(ctx->medias, ctx->num_medias);
break;
case 's': {
if (!ctx)
return 1;
struct shinkos1245_resp_status sts;
j = shinkos1245_get_status(ctx, &sts);
if (!j)
shinkos1245_dump_status(&sts);
break;
}
case 'u': {
if (!ctx)
return 1;
struct shinkos1245_resp_getid resp;
j = shinkos1245_get_printerid(ctx, &resp);
if (!j) {
char buffer[sizeof(resp.data)+1];
memcpy(buffer, resp.data, sizeof(resp.data));
buffer[sizeof(resp.data)] = 0;
INFO("Printer ID: %02x '%s'\n", resp.id, buffer);
} }
break; return 1;
} case 'c':
if (ctx) {
j = get_tonecurve(ctx, TONE_TABLE_USER, ctx->tonecurve, optarg);
break;
}
return 2;
case 'C':
if (ctx) {
j = set_tonecurve(ctx, TONE_TABLE_USER, ctx->tonecurve, optarg);
break;
}
return 2;
case 'l':
if (ctx) {
j = get_tonecurve(ctx, TONE_TABLE_CURRENT, ctx->tonecurve, optarg);
break;
}
return 2;
case 'L':
if (ctx) {
j = set_tonecurve(ctx, TONE_TABLE_CURRENT, ctx->tonecurve, optarg);
break;
}
return 2;
case 'f':
if (!ctx) {
ctx->fast_return = 1;
break;
}
return 1;
case 'm':
if (ctx) {
j = shinkos1245_get_media(ctx);
if (!j)
shinkos1245_dump_media(ctx->medias, ctx->num_medias);
break;
}
return 1;
case 's':
if (ctx) {
struct shinkos1245_resp_status sts;
j = shinkos1245_get_status(ctx, &sts);
if (!j)
shinkos1245_dump_status(&sts);
break;
}
return 1;
case 'u':
if (ctx) {
struct shinkos1245_resp_getid resp;
j = shinkos1245_get_printerid(ctx, &resp);
if (!j) {
char buffer[sizeof(resp.data)+1];
memcpy(buffer, resp.data, sizeof(resp.data));
buffer[sizeof(resp.data)] = 0;
INFO("Printer ID: %02x '%s'\n", resp.id, buffer);
break;
}
}
return 1;
case 'U': case 'U':
if (!ctx) if (ctx) {
return 1; j = shinkos1245_set_printerid(ctx, optarg);
j = shinkos1245_set_printerid(ctx, optarg); break;
break; }
return 1;
case 'X': case 'X':
if (!ctx) if (ctx) {
return 1; j = shinkos1245_canceljob(ctx, atoi(optarg));
j = shinkos1245_canceljob(ctx, atoi(optarg)); break;
break; }
return 1;
default: default:
break; /* Ignore completely */ break; /* Ignore completely */
} }
@ -1607,7 +1615,7 @@ static int shinkos1245_query_serno(struct libusb_device_handle *dev, uint8_t end
struct dyesub_backend shinkos1245_backend = { struct dyesub_backend shinkos1245_backend = {
.name = "Shinko/Sinfonia CHC-S1245", .name = "Shinko/Sinfonia CHC-S1245",
.version = "0.02WIP", .version = "0.03WIP",
.uri_prefix = "shinkos1245", .uri_prefix = "shinkos1245",
.cmdline_usage = shinkos1245_cmdline, .cmdline_usage = shinkos1245_cmdline,
.cmdline_arg = shinkos1245_cmdline_arg, .cmdline_arg = shinkos1245_cmdline_arg,

View File

@ -1306,19 +1306,19 @@ int shinkos2145_cmdline_arg(void *vctx, int argc, char **argv)
return -1; return -1;
break; break;
} }
return 1; return 2;
case 'c': case 'c':
if (ctx) { if (ctx) {
j = get_tonecurve(ctx, TONECURVE_USER, optarg); j = get_tonecurve(ctx, TONECURVE_USER, optarg);
break; break;
} }
return 1; return 2;
case 'C': case 'C':
if (ctx) { if (ctx) {
j = set_tonecurve(ctx, TONECURVE_USER, optarg); j = set_tonecurve(ctx, TONECURVE_USER, optarg);
break; break;
} }
return 1; return 2;
case 'e': case 'e':
if (ctx) { if (ctx) {
j = get_errorlog(ctx); j = get_errorlog(ctx);
@ -1348,13 +1348,13 @@ int shinkos2145_cmdline_arg(void *vctx, int argc, char **argv)
j = get_tonecurve(ctx, TONECURVE_CURRENT, optarg); j = get_tonecurve(ctx, TONECURVE_CURRENT, optarg);
break; break;
} }
return 1; return 2;
case 'L': case 'L':
if (ctx) { if (ctx) {
j = set_tonecurve(ctx, TONECURVE_CURRENT, optarg); j = set_tonecurve(ctx, TONECURVE_CURRENT, optarg);
break; break;
} }
return 1; return 2;
case 'm': case 'm':
if (ctx) { if (ctx) {
j = get_mediainfo(ctx); j = get_mediainfo(ctx);
@ -1390,13 +1390,13 @@ int shinkos2145_cmdline_arg(void *vctx, int argc, char **argv)
j = set_user_string(ctx, optarg); j = set_user_string(ctx, optarg);
break; break;
} }
return 1; return 2;
case 'X': case 'X':
if (ctx) { if (ctx) {
j = cancel_job(ctx, optarg); j = cancel_job(ctx, optarg);
break; break;
} }
return 1; return 2;
default: default:
break; /* Ignore completely */ break; /* Ignore completely */
} }