int main() { // The maximum size of command that a user can enter const int INPUT_SIZE = 256; char input[INPUT_SIZE]; // Get user input printf("Please enter a command (limit to 256 characters)\n"); fgets(input, INPUT_SIZE, stdin); // Initialize array stuff char** argv; int argc; argc = makearg(input, &argv); // Print arguments of array for (int i=0; i<argc; i++) { printf("Argument %i: %s\n", i+1, argv[i]); } }
/* * === FUNCTION ====================================================================== * Name: cmdloop * Description: 循环执行命令 * ===================================================================================== */ static void cmdloop (void) { struct cmd *c; int l; char line[MAXLINE] = ""; for (;;) { if (!getinput(line, sizeof(line))) { QUIT(); } l = strlen(line); if (l == 0) break; if (line[--l] == '\n') { if (l == 0) break; line[l] = '\0'; } else if (l == sizeof(line) - 2) { /* it was a line without a newline */ printf("Sorry, input line too long\n"); while ((l = getchar()) != '\n' && l != EOF) ; break; } /* 如果是大写命令,直接发送到服务器处理,只适合部分控制命令,慎用 */ if (isupper(line[0])){ command(line); continue; } makearg(line); c = getcmd(margv[0]); if (c == (struct cmd *)-1) { printf("?Ambiguous command\n"); continue; } if (c == NULL) { printf("?Invalid command\n"); continue; } if (c->c_conn && !connected) { /* 试图自动建立连接 */ if (automatic && strcmp(hostnm, "")){ setpeer(hostnm); } else{ printf("Not connected.\n"); continue; } } if (c->c_handler_1) c->c_handler_1(margc, margv); else c->c_handler_0(); } signal(SIGINT, SIG_IGN); signal(SIGPIPE, lostpeer); } /* ----- end of function cmdloop ----- */
static void addg(unsigned char *as1, unsigned char *as2, unsigned char *as3, unsigned char *as4) { register unsigned char *s1, *s2; register int c; int len; wchar_t wc; s2 = locstak() + BYTESPERWORD; s1 = as1; if (as4) { while (c = *s1++) { if (s2 >= brkend) growstak(s2); *s2++ = c; } /* * Restore first slash before the first metacharacter * if as1 is not "/" */ if (as4 + 1 == s1) { if (s2 >= brkend) growstak(s2); *s2++ = '/'; } } /* add matched entries, plus extra \\ to escape \\'s */ s1 = as2; for (;;) { if ((len = nextc(&wc, (char *)s1)) <= 0) { len = 1; wc = (unsigned char)*s1; } if (s2 >= brkend) growstak(s2); if (wc == 0) { *s2 = *s1++; break; } if (wc == '\\') { *s2++ = '\\'; if (s2 >= brkend) growstak(s2); *s2++ = '\\'; s1++; continue; } if ((s2 + len) >= brkend) growstak(s2 + len); memcpy(s2, s1, len); s2 += len; s1 += len; } if (s1 = as3) { if (s2 >= brkend) growstak(s2); *s2++ = '/'; do { if (s2 >= brkend) growstak(s2); } while (*s2++ = *++s1); } makearg((struct argnod *)endstak(s2)); }