Skip to content

Commit

Permalink
Give better error message on bad arguments
Browse files Browse the repository at this point in the history
Instead of just printing the huge usage text and hoping the user figures
out what was wrong.
  • Loading branch information
CendioOssman committed Dec 3, 2024
1 parent 8800e39 commit 16a9529
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
24 changes: 20 additions & 4 deletions unix/vncconfig/vncconfig.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,34 @@ int main(int argc, char** argv)
if (i >= argc) usage();

char* equal = strchr(argv[i], '=');
if (!equal)
usage();
if (!equal) {
fprintf(stderr, "%s: Invalid parameter syntax '%s'\n",
programName, argv[i]);
fprintf(stderr, "See '%s -help' for more information.\n",
programName);
exit(1);
}

std::string name(argv[i], equal-argv[i]);
std::string value(equal+1);

if (!XVncExtSetParam(dpy, name.c_str(), value.c_str()))
fprintf(stderr, "Setting param %s failed\n",argv[i]);
} else if (argv[i][0] == '-') {
fprintf(stderr, "%s: Unrecognized option '%s'\n",
programName, argv[i]);
fprintf(stderr, "See '%s -help' for more information.\n",
programName);
exit(1);
} else {
char* equal = strchr(argv[i], '=');
if (!equal)
usage();
if (!equal) {
fprintf(stderr, "%s: Invalid parameter syntax '%s'\n",
programName, argv[i]);
fprintf(stderr, "See '%s -help' for more information.\n",
programName);
exit(1);
}

std::string name(argv[i], equal-argv[i]);
std::string value(equal+1);
Expand Down
8 changes: 6 additions & 2 deletions unix/vncpasswd/vncpasswd.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,19 @@ int main(int argc, char** argv)
} else if (strncmp(argv[i], "-f", 2) == 0) {
return encrypt_pipe();
} else if (argv[i][0] == '-') {
usage();
fprintf(stderr, "%s: Unrecognized option '%s'\n", prog, argv[i]);
fprintf(stderr, "See '%s --help' for more information.\n", prog);
exit(1);
} else if (fname[0] == '\0') {
if (strlen(argv[i]) >= sizeof(fname)) {
fprintf(stderr, "Too long filename specified\n");
return -1;
}
strcpy(fname, argv[i]);
} else {
usage();
fprintf(stderr, "%s: Extra argument '%s'\n", prog, argv[i]);
fprintf(stderr, "See '%s --help' for more information.\n", prog);
exit(1);
}
}

Expand Down
13 changes: 12 additions & 1 deletion unix/x0vncserver/x0vncserver.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,18 @@ int main(int argc, char** argv)
return 0;
}

usage();
if (argv[i][0] == '-') {
fprintf(stderr, "%s: Unrecognized option '%s'\n",
programName, argv[i]);
fprintf(stderr, "See '%s --help' for more information.\n",
programName);
exit(1);
}

fprintf(stderr, "%s: Extra argument '%s'\n", programName, argv[i]);
fprintf(stderr, "See '%s --help' for more information.\n",
programName);
exit(1);
}

if (!(dpy = XOpenDisplay(displayname))) {
Expand Down
19 changes: 15 additions & 4 deletions vncviewer/vncviewer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,22 @@ int main(int argc, char** argv)
return 0;
}

if (argv[i][0] == '-')
usage(argv[0]);
if (argv[i][0] == '-') {
fprintf(stderr, "\n");
fprintf(stderr, "%s: Unrecognized option '%s'\n",
argv[0], argv[i]);
fprintf(stderr, "See '%s --help' for more information.\n",
argv[0]);
exit(1);
}

if (vncServerName[0] != '\0')
usage(argv[0]);
if (vncServerName[0] != '\0') {
fprintf(stderr, "\n");
fprintf(stderr, "%s: Extra argument '%s'\n", argv[0], argv[i]);
fprintf(stderr, "See '%s --help' for more information.\n",
argv[0]);
exit(0);
}

strncpy(vncServerName, argv[i], VNCSERVERNAMELEN);
vncServerName[VNCSERVERNAMELEN - 1] = '\0';
Expand Down

0 comments on commit 16a9529

Please sign in to comment.