1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 01:48:00 +00:00

Fixes for more robust handling of command-line parse errors.

[originally from svn r2236]
This commit is contained in:
Simon Tatham 2002-11-20 20:09:02 +00:00
parent 75ebfa28f0
commit 0c110dcd10
5 changed files with 35 additions and 9 deletions

View File

@ -256,6 +256,7 @@ int main(int argc, char **argv)
int skcount, sksize; int skcount, sksize;
int connopen; int connopen;
int exitcode; int exitcode;
int errors;
ssh_get_line = console_get_line; ssh_get_line = console_get_line;
@ -275,6 +276,7 @@ int main(int argc, char **argv)
do_defaults(NULL, &cfg); do_defaults(NULL, &cfg);
default_protocol = cfg.protocol; default_protocol = cfg.protocol;
default_port = cfg.port; default_port = cfg.port;
errors = 0;
{ {
/* /*
* Override the default protocol if PLINK_PROTOCOL is set. * Override the default protocol if PLINK_PROTOCOL is set.
@ -299,12 +301,16 @@ int main(int argc, char **argv)
if (ret == -2) { if (ret == -2) {
fprintf(stderr, fprintf(stderr,
"plink: option \"%s\" requires an argument\n", p); "plink: option \"%s\" requires an argument\n", p);
errors = 1;
} else if (ret == 2) { } else if (ret == 2) {
--argc, ++argv; --argc, ++argv;
} else if (ret == 1) { } else if (ret == 1) {
continue; continue;
} else if (!strcmp(p, "-batch")) { } else if (!strcmp(p, "-batch")) {
console_batch_mode = 1; console_batch_mode = 1;
} else {
fprintf(stderr, "plink: unknown option \"%s\"\n", p);
errors = 1;
} }
} else if (*p) { } else if (*p) {
if (!*cfg.host) { if (!*cfg.host) {
@ -426,6 +432,9 @@ int main(int argc, char **argv)
} }
} }
if (errors)
return 1;
if (!*cfg.host) { if (!*cfg.host) {
usage(); usage();
} }

View File

@ -1844,11 +1844,11 @@ static int psftp_connect(char *userhost, char *user, int portnumber)
void cmdline_error(char *p, ...) void cmdline_error(char *p, ...)
{ {
va_list ap; va_list ap;
fprintf(stderr, "pscp: "); fprintf(stderr, "psftp: ");
va_start(ap, p); va_start(ap, p);
vfprintf(stderr, p, ap); vfprintf(stderr, p, ap);
va_end(ap); va_end(ap);
fputc('\n', stderr); fprintf(stderr, "\n try typing \"psftp -h\" for help\n");
exit(1); exit(1);
} }
@ -1863,6 +1863,7 @@ int main(int argc, char *argv[])
int mode = 0; int mode = 0;
int modeflags = 0; int modeflags = 0;
char *batchfile = NULL; char *batchfile = NULL;
int errors = 0;
flags = FLAG_STDERR | FLAG_INTERACTIVE; flags = FLAG_STDERR | FLAG_INTERACTIVE;
cmdline_tooltype = TOOLTYPE_FILETRANSFER; cmdline_tooltype = TOOLTYPE_FILETRANSFER;
@ -1872,6 +1873,7 @@ int main(int argc, char *argv[])
userhost = user = NULL; userhost = user = NULL;
errors = 0;
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
int ret; int ret;
if (argv[i][0] != '-') { if (argv[i][0] != '-') {
@ -1906,7 +1908,7 @@ int main(int argc, char *argv[])
i++; i++;
break; break;
} else { } else {
usage(); cmdline_error("unknown option \"%s\"", argv[i]);
} }
} }
argc -= i; argc -= i;

7
scp.c
View File

@ -2166,7 +2166,7 @@ void cmdline_error(char *p, ...)
va_start(ap, p); va_start(ap, p);
vfprintf(stderr, p, ap); vfprintf(stderr, p, ap);
va_end(ap); va_end(ap);
fputc('\n', stderr); fprintf(stderr, "\n try typing just \"pscp\" for help\n");
exit(1); exit(1);
} }
@ -2219,8 +2219,9 @@ int main(int argc, char *argv[])
} else if (strcmp(argv[i], "--") == 0) { } else if (strcmp(argv[i], "--") == 0) {
i++; i++;
break; break;
} else } else {
usage(); cmdline_error("unknown option \"%s\"", argv[i]);
}
} }
argc -= i; argc -= i;
argv += i; argv += i;

View File

@ -186,6 +186,7 @@ int main(int argc, char **argv)
int i, skcount, sksize, socketstate; int i, skcount, sksize, socketstate;
int connopen; int connopen;
int exitcode; int exitcode;
int errors;
void *ldisc; void *ldisc;
ssh_get_line = console_get_line; ssh_get_line = console_get_line;
@ -206,6 +207,7 @@ int main(int argc, char **argv)
do_defaults(NULL, &cfg); do_defaults(NULL, &cfg);
default_protocol = cfg.protocol; default_protocol = cfg.protocol;
default_port = cfg.port; default_port = cfg.port;
errors = 0;
{ {
/* /*
* Override the default protocol if PLINK_PROTOCOL is set. * Override the default protocol if PLINK_PROTOCOL is set.
@ -230,6 +232,7 @@ int main(int argc, char **argv)
if (ret == -2) { if (ret == -2) {
fprintf(stderr, fprintf(stderr,
"plink: option \"%s\" requires an argument\n", p); "plink: option \"%s\" requires an argument\n", p);
errors = 1;
} else if (ret == 2) { } else if (ret == 2) {
--argc, ++argv; --argc, ++argv;
} else if (ret == 1) { } else if (ret == 1) {
@ -237,11 +240,17 @@ int main(int argc, char **argv)
} else if (!strcmp(p, "-batch")) { } else if (!strcmp(p, "-batch")) {
console_batch_mode = 1; console_batch_mode = 1;
} else if (!strcmp(p, "-o")) { } else if (!strcmp(p, "-o")) {
if (argc <= 1) if (argc <= 1) {
fprintf(stderr, fprintf(stderr,
"plink: option \"-o\" requires an argument\n"); "plink: option \"-o\" requires an argument\n");
else errors = 1;
--argc, provide_xrm_string(*++argv); } else {
--argc;
provide_xrm_string(*++argv);
}
} else {
fprintf(stderr, "plink: unknown option \"%s\"\n", p);
errors = 1;
} }
} else if (*p) { } else if (*p) {
if (!*cfg.host) { if (!*cfg.host) {
@ -366,6 +375,9 @@ int main(int argc, char **argv)
} }
} }
if (errors)
return 1;
if (!*cfg.host) { if (!*cfg.host) {
usage(); usage();
} }

View File

@ -406,6 +406,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
cfg.host[sizeof(cfg.host) - 1] = '\0'; cfg.host[sizeof(cfg.host) - 1] = '\0';
got_host = 1; got_host = 1;
} }
} else {
cmdline_error("unknown option \"%s\"", p);
} }
} }
} }