int main(void) { struct cmd *cmd = NULL; char prompt[HOST_NAME_MAX+1], *line, **argp = NULL; int ncmd, ret, v = -1; u_int i; rl_readline_name = NAME; rl_completion_entry_function = (Function *)lg_completion; /* Ignore the whitespace character */ rl_basic_word_break_characters = "\t\n\"\\'`@$><=;|&{("; while (!quit) { v = -1; gethostname(prompt, sizeof(prompt) - 2); strlcat(prompt, "> ", sizeof(prompt)); if ((line = readline(prompt)) == NULL) { printf("\n"); lg_help(cmds, NULL); continue; } if (!lg_strip(line)) goto next; if (strcmp(line, "exit") == 0) { quit = 1; goto next; } add_history(line); if ((argp = lg_arg2argv(line, &ncmd)) == NULL) goto next; for (i = 0; cmds[i].name != NULL; i++) { ret = lg_checkcmd(ncmd, argp, &v, &cmds[i]); if (ret == 0) cmd = &cmds[i]; else if (ret == EINVAL) { printf("invalid number of arguments\n"); goto next; } } if (cmd == NULL) { printf("invalid command\n"); } else if (cmd->func != NULL) { cmd->func(cmds, argp); } else { if ((argp = lg_argextra(argp, ncmd, v, cmd)) == NULL) goto next; lg_exec(cmd->earg[0], argp); } next: free(argp); argp = NULL; free(line); line = NULL; cmd = NULL; } return (0); }
int main(void) { char *query, *self, *cmd = NULL, *req; char **argv = NULL; char myname[MAXHOSTNAMELEN]; int ret = 1, argc = 0, query_length = 0; struct stat st; u_int i; struct cmd *cmdp = NULL; if (gethostname(myname, sizeof(myname)) != 0) return (1); printf("Content-Type: %s\n" "Cache-Control: no-cache\n\n" "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" " "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n" "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" "<head>\n" "<title>%s: %s</title>\n", CONTENT_TYPE, NAME, myname); if (stat(INC_STYLE, &st) == 0) { printf("<style type='text/css'><!--\n"); lg_incl(INC_STYLE); printf("--></style>\n"); } if (stat(INC_HEAD, &st) != 0 || lg_incl(INC_HEAD) != 0) { printf("</head>\n" "<body>\n"); } printf("<h1>%s: %s</h1>\n", NAME, myname); printf("<h2>%s</h2>\n", BRIEF); /* print a form with possible options */ if ((self = lg_getenv("SCRIPT_NAME", NULL)) == NULL) { printf("fatal error: invalid request\n"); goto err; } if ((query = lg_getenv("QUERY_STRING", &query_length)) != NULL) cmd = lg_getarg("cmd=", query, query_length); printf( "<form action='%s'>\n" "<div class=\"command\">\n" "<select name='cmd'>\n", self); for (i = 0; cmds[i].name != NULL; i++) { if (!lg_checkperm(&cmds[i])) continue; if (cmd != NULL && strcmp(cmd, cmds[i].name) == 0) printf("<option value='%s' selected='selected'>%s" "</option>\n", cmds[i].name, cmds[i].name); else printf("<option value='%s'>%s</option>\n", cmds[i].name, cmds[i].name); } printf("</select>\n" "<input type='text' name='req'/>\n" "<input type='submit' value='submit'/>\n" "</div>\n" "</form>\n" "<pre>\n"); fflush(stdout); #ifdef DEBUG if (close(2) == -1 || dup2(1, 2) == -1) #else if (close(2) == -1) #endif { printf("fatal error: %s\n", strerror(errno)); goto err; } if (query == NULL) goto err; if (cmd == NULL) { printf("unspecified command\n"); goto err; } if ((req = lg_getarg("req=", query, query_length)) != NULL) { /* Could be NULL */ argv = lg_arg2argv(req, &argc); } for (i = 0; cmds[i].name != NULL; i++) { if (strcmp(cmd, cmds[i].name) == 0) { cmdp = &cmds[i]; break; } } if (cmdp == NULL) { printf("invalid command: %s\n", cmd); goto err; } if (argc > cmdp->maxargs) { printf("superfluous argument(s): %s %s\n", cmd, cmdp->args ? cmdp->args : ""); goto err; } if (argc < cmdp->minargs) { printf("missing argument(s): %s %s\n", cmd, cmdp->args); goto err; } if (cmdp->func != NULL) { ret = cmdp->func(cmds, argv); } else { if ((argv = lg_argextra(argv, argc, cmdp)) == NULL) goto err; ret = lg_exec(cmdp->earg[0], argv); } if (ret != 0) printf("\nfailed%s\n", ret == 127 ? ": file not found" : "."); else printf("\nsuccess.\n"); err: fflush(stdout); if (argv != NULL) free(argv); printf("</pre>\n"); if (stat(INC_FOOT, &st) != 0 || lg_incl(INC_FOOT) != 0) printf("<hr/>\n"); printf("<div class='footer'>\n" "<small>%s - %s<br/>Copyright (c) %s</small>\n" "</div>\n" "</body>\n" "</html>\n", NAME, BRIEF, COPYRIGHT); return (ret); }