1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

Move get_test_opt to util/test.h and use throughout test programs

This commit is contained in:
Jonas Fonseca 2007-08-28 20:32:10 +02:00
parent c6a223c8dd
commit bbd7c8354a
5 changed files with 40 additions and 104 deletions

View File

@ -37,41 +37,14 @@ main(int argc, char *argv[])
arg += 2;
if (!strncmp(arg, "uri", 3)) {
arg += 3;
if (*arg == '=') {
arg++;
set_dom_string(&uri, arg, strlen(arg));
} else {
i++;
if (i >= argc)
die("--uri expects a URI");
set_dom_string(&uri, argv[i], strlen(argv[i]));
}
if (get_test_opt(&arg, "uri", &i, argc, argv, "a URI")) {
set_dom_string(&uri, arg, strlen(arg));
} else if (!strncmp(arg, "src", 3)) {
arg += 3;
if (*arg == '=') {
arg++;
set_dom_string(&source, arg, strlen(arg));
} else {
i++;
if (i >= argc)
die("--src expects a string");
set_dom_string(&source, argv[i], strlen(argv[i]));
}
} else if (get_test_opt(&arg, "src", &i, argc, argv, "a string")) {
set_dom_string(&source, arg, strlen(arg));
} else if (!strncmp(arg, "selector", 3)) {
arg += 8;
if (*arg == '=') {
arg++;
set_dom_string(&selector, arg, strlen(arg));
} else {
i++;
if (i >= argc)
die("--selector expects a string");
set_dom_string(&selector, argv[i], strlen(argv[i]));
}
} else if (get_test_opt(&arg, "selector", &i, argc, argv, "a string")) {
set_dom_string(&selector, arg, strlen(arg));
} else if (!strcmp(arg, "help")) {
die(NULL);

View File

@ -243,33 +243,6 @@ sgml_error_function(struct sgml_parser *parser, struct dom_string *string,
return DOM_CODE_OK;
}
static int
get_test_opt(char **argref, const char *name, int *argi, int argc, char *argv[],
const char *expect_msg)
{
char *arg = *argref;
int namelen = strlen(name);
if (strncmp(arg, name, namelen))
return 0;
arg += namelen;
if (*arg == '=') {
(*argref) = arg + 1;
} else if (!*arg) {
(*argi)++;
if ((*argi) >= argc)
die("--%s expects %s", name, expect_msg);
(*argref) = argv[(*argi)];
} else {
return 0;
}
return 1;
}
int
main(int argc, char *argv[])
{

View File

@ -703,44 +703,16 @@ main(int argc, char *argv[])
arg += 2;
if (!strncmp(arg, "path", 4)) {
arg += 4;
if (*arg == '=') {
arg++;
get_mailcap_path() = arg;
} else {
i++;
if (i >= argc)
die("--path expects a parameter");
get_mailcap_path() = argv[i];
}
if (get_test_opt(&arg, "path", &i, argc, argv, "a string")) {
get_mailcap_path() = arg;
done_mailcap(NULL);
} else if (!strncmp(arg, "format", 6)) {
arg += 6;
if (*arg == '=') {
arg++;
format = arg;
} else {
i++;
if (i >= argc)
die("--format expects a parameter");
format = argv[i];
}
} else if (get_test_opt(&arg, "format", &i, argc, argv, "a string")) {
format = arg;
} else if (!strncmp(arg, "get", 3)) {
} else if (get_test_opt(&arg, "get", &i, argc, argv, "a string")) {
struct mime_handler *handler;
arg += 3;
if (*arg == '=') {
arg++;
} else {
i++;
if (i >= argc)
die("--get expects a parameter");
arg = argv[i];
}
if (has_gotten)
printf("\n");
has_gotten = 1;

View File

@ -31,17 +31,8 @@ main(int argc, char *argv[])
arg += 2;
if (!strncmp(arg, "response", 8)) {
arg += 8;
if (*arg == '=') {
arg++;
response = arg;
} else {
i++;
if (i >= argc)
die("--response expects a string");
response = argv[i];
}
if (get_test_opt(&arg, "response", &i, argc, argv, "a string")) {
response = arg;
responselen = strlen(response);
} else {

View File

@ -20,4 +20,31 @@ die(const char *msg, ...)
exit(EXIT_FAILURE);
}
static inline int
get_test_opt(char **argref, const char *name, int *argi, int argc, char *argv[],
const char *expect_msg)
{
char *arg = *argref;
int namelen = strlen(name);
if (strncmp(arg, name, namelen))
return 0;
arg += namelen;
if (*arg == '=') {
(*argref) = arg + 1;
} else if (!*arg) {
(*argi)++;
if ((*argi) >= argc)
die("--%s expects %s", name, expect_msg);
(*argref) = argv[(*argi)];
} else {
return 0;
}
return 1;
}
#endif