1*29527Ssam /* cmd.c 1.1 86/07/05 */ 2*29527Ssam 3*29527Ssam #include "vdfmt.h" 4*29527Ssam #include "cmd.h" 5*29527Ssam 6*29527Ssam #define TRUE 1 7*29527Ssam #define FALSE 0 8*29527Ssam 9*29527Ssam #define HELP 1 10*29527Ssam #define STATUS 2 11*29527Ssam #define KILL 3 12*29527Ssam #define KQUIT 4 13*29527Ssam 14*29527Ssam static cmd_text_element primary[] = { 15*29527Ssam { STATUS, "!", "" }, 16*29527Ssam { HELP, "HElp", "" }, 17*29527Ssam { KILL, "KILL", "" }, 18*29527Ssam { KQUIT, "QUIT", "" }, 19*29527Ssam { STATUS, "STATus", "" }, 20*29527Ssam { HELP, "?", "" }, 21*29527Ssam { 0, "", "" } 22*29527Ssam }; 23*29527Ssam 24*29527Ssam 25*29527Ssam /* 26*29527Ssam */ 27*29527Ssam 28*29527Ssam boolean confirm(token) 29*29527Ssam int token; 30*29527Ssam { 31*29527Ssam char *action; 32*29527Ssam char query[50]; 33*29527Ssam 34*29527Ssam if(token == KILL) 35*29527Ssam action = "kill"; 36*29527Ssam else 37*29527Ssam action = "quit"; 38*29527Ssam sprintf(query, "Confirm %s operations", action); 39*29527Ssam return get_yes_no(query); 40*29527Ssam } 41*29527Ssam 42*29527Ssam 43*29527Ssam /* 44*29527Ssam ** 45*29527Ssam */ 46*29527Ssam 47*29527Ssam get_text_cmd(table, tokens) 48*29527Ssam cmd_text_element *table; 49*29527Ssam int *tokens; 50*29527Ssam { 51*29527Ssam extern boolean get_yes_no(); 52*29527Ssam int *t_ptr; 53*29527Ssam char line[133]; 54*29527Ssam 55*29527Ssam gets(line); 56*29527Ssam /* Check for help, status, or kill */ 57*29527Ssam cmd_parse(primary, line, tokens); 58*29527Ssam t_ptr = tokens; 59*29527Ssam while(*t_ptr) { 60*29527Ssam switch (*t_ptr) { 61*29527Ssam case STATUS : 62*29527Ssam cmd_status(); 63*29527Ssam break; 64*29527Ssam case KQUIT : 65*29527Ssam case KILL : 66*29527Ssam if(confirm(*t_ptr) == true) { 67*29527Ssam kill_processes = true; 68*29527Ssam return 0; 69*29527Ssam } 70*29527Ssam break; 71*29527Ssam default: 72*29527Ssam help_text(table); 73*29527Ssam break; 74*29527Ssam } 75*29527Ssam t_ptr++; 76*29527Ssam } 77*29527Ssam /* Now parse all the operator's commands */ 78*29527Ssam cmd_parse(table, line, tokens); 79*29527Ssam return strlen(line); 80*29527Ssam } 81*29527Ssam 82*29527Ssam 83*29527Ssam /* 84*29527Ssam ** 85*29527Ssam */ 86*29527Ssam 87*29527Ssam cmd_intcmp(a, b) 88*29527Ssam int *a, *b; 89*29527Ssam { 90*29527Ssam if(*a==*b) 91*29527Ssam return 0; 92*29527Ssam if(*a<*b) 93*29527Ssam return -1; 94*29527Ssam return 1; 95*29527Ssam } 96*29527Ssam 97*29527Ssam 98*29527Ssam /* 99*29527Ssam ** 100*29527Ssam */ 101*29527Ssam 102*29527Ssam condition_list(tokens, sentinal) 103*29527Ssam int *tokens, sentinal; 104*29527Ssam { 105*29527Ssam register int *t_ptr = tokens; 106*29527Ssam register int num_tok; 107*29527Ssam 108*29527Ssam for(num_tok=0; *t_ptr++ != sentinal; num_tok++) 109*29527Ssam ; 110*29527Ssam qsort(tokens, num_tok, sizeof(int), cmd_intcmp); 111*29527Ssam /* compress out dups */ 112*29527Ssam while(*tokens != sentinal) { 113*29527Ssam if(*tokens == *(tokens+1)) { 114*29527Ssam for(t_ptr=tokens+1; *t_ptr != sentinal; t_ptr++) { 115*29527Ssam *t_ptr = *(t_ptr+1); 116*29527Ssam } 117*29527Ssam continue; 118*29527Ssam } 119*29527Ssam tokens++; 120*29527Ssam } 121*29527Ssam } 122*29527Ssam 123*29527Ssam 124*29527Ssam /* 125*29527Ssam ** 126*29527Ssam */ 127*29527Ssam 128*29527Ssam cmd_parse(table, line, tokens) 129*29527Ssam cmd_text_element *table; 130*29527Ssam char *line; 131*29527Ssam int *tokens; 132*29527Ssam { 133*29527Ssam char *seperators = "\t ,.;:-~+/\\"; 134*29527Ssam register char *tok_start; 135*29527Ssam register int *tok = tokens; 136*29527Ssam char save_buf[133]; 137*29527Ssam 138*29527Ssam strcpy(save_buf, line); 139*29527Ssam tok_start = (char *)strtok((char *)save_buf, seperators); 140*29527Ssam while(tok_start != NULL) { 141*29527Ssam if(strlen(tok_start)) { 142*29527Ssam if(*tok = cmd_search(table, tok_start)) { 143*29527Ssam tok++; 144*29527Ssam } 145*29527Ssam } 146*29527Ssam tok_start = (char *)strtok((char *)NULL, seperators); 147*29527Ssam } 148*29527Ssam *tok = 0; 149*29527Ssam condition_list(tokens, 0); 150*29527Ssam } 151*29527Ssam 152*29527Ssam 153*29527Ssam /* 154*29527Ssam ** 155*29527Ssam */ 156*29527Ssam 157*29527Ssam cmd_search(table, command) 158*29527Ssam cmd_text_element *table; 159*29527Ssam char *command; 160*29527Ssam { 161*29527Ssam register char *tbl_ptr; 162*29527Ssam register char *cmd_ptr; 163*29527Ssam 164*29527Ssam while(table->cmd_token != 0) { 165*29527Ssam cmd_ptr = command; 166*29527Ssam tbl_ptr = table->cmd_text; 167*29527Ssam while(ismustmatch(*tbl_ptr)) { 168*29527Ssam if(toupper(*cmd_ptr) != *tbl_ptr) 169*29527Ssam break; 170*29527Ssam cmd_ptr++; 171*29527Ssam tbl_ptr++; 172*29527Ssam } 173*29527Ssam if((*tbl_ptr == 0) || !ismustmatch(*tbl_ptr)) 174*29527Ssam return table->cmd_token; 175*29527Ssam table++; 176*29527Ssam } 177*29527Ssam return 0; 178*29527Ssam } 179*29527Ssam 180*29527Ssam 181*29527Ssam /* 182*29527Ssam ** 183*29527Ssam */ 184*29527Ssam 185*29527Ssam is_in_digit_table(table, token) 186*29527Ssam int *table, token; 187*29527Ssam { 188*29527Ssam while(*table != -1) { 189*29527Ssam if(token == *table) 190*29527Ssam return TRUE; 191*29527Ssam table++; 192*29527Ssam } 193*29527Ssam return FALSE; 194*29527Ssam } 195*29527Ssam 196*29527Ssam 197*29527Ssam /* 198*29527Ssam ** 199*29527Ssam */ 200*29527Ssam 201*29527Ssam int *fill_in(tokens, table, start, end) 202*29527Ssam int *tokens, *table, start, end; 203*29527Ssam { 204*29527Ssam 205*29527Ssam if(start > end) { 206*29527Ssam register int temp = end; 207*29527Ssam 208*29527Ssam end = start; 209*29527Ssam start = temp; 210*29527Ssam } 211*29527Ssam while((*table != -1) && (*table < start)) 212*29527Ssam table++; 213*29527Ssam while((*table != -1) && (*table <= end)) { 214*29527Ssam *tokens++ = *table++; 215*29527Ssam } 216*29527Ssam return tokens; 217*29527Ssam } 218*29527Ssam 219*29527Ssam /* 220*29527Ssam ** 221*29527Ssam */ 222*29527Ssam 223*29527Ssam get_digit_list(tokens, table, help) 224*29527Ssam int *tokens, *table, (*help)(); 225*29527Ssam { 226*29527Ssam int *tok_ptr; 227*29527Ssam char *ptr, line[133]; 228*29527Ssam 229*29527Ssam condition_list(table, -1); 230*29527Ssam gets(line); 231*29527Ssam if(!line[0]) { 232*29527Ssam *tokens = -1; 233*29527Ssam return; 234*29527Ssam } 235*29527Ssam /* Check for help, status, or kill */ 236*29527Ssam cmd_parse(primary, line, tokens); 237*29527Ssam tok_ptr = tokens; 238*29527Ssam while(*tok_ptr) { 239*29527Ssam switch (*tok_ptr) { 240*29527Ssam case STATUS : 241*29527Ssam cmd_status(); 242*29527Ssam break; 243*29527Ssam case KQUIT : 244*29527Ssam case KILL : 245*29527Ssam if(confirm(*tok_ptr)) { 246*29527Ssam kill_processes = true; 247*29527Ssam return; 248*29527Ssam } 249*29527Ssam break; 250*29527Ssam default: 251*29527Ssam (help)(); 252*29527Ssam break; 253*29527Ssam } 254*29527Ssam tok_ptr++; 255*29527Ssam } 256*29527Ssam tok_ptr = tokens; 257*29527Ssam ptr = line; 258*29527Ssam while(*ptr) { 259*29527Ssam finddigit(ptr); 260*29527Ssam if(sscanf(ptr, "%d", tok_ptr) > 0) { 261*29527Ssam skipdigits(ptr); 262*29527Ssam skip_junk(ptr); 263*29527Ssam if((*ptr == '~') || (*ptr == '-')) { 264*29527Ssam register int start = *tok_ptr; 265*29527Ssam 266*29527Ssam finddigit(ptr); 267*29527Ssam if(sscanf(ptr, "%d", tok_ptr) > 0) { 268*29527Ssam skipdigits(ptr); 269*29527Ssam tok_ptr = fill_in(tok_ptr, 270*29527Ssam table, start, *tok_ptr); 271*29527Ssam continue; 272*29527Ssam } 273*29527Ssam else 274*29527Ssam *tok_ptr = start; 275*29527Ssam } 276*29527Ssam if(is_in_digit_table(table, *tok_ptr)) 277*29527Ssam tok_ptr++; 278*29527Ssam } 279*29527Ssam } 280*29527Ssam *tok_ptr = -1; 281*29527Ssam condition_list(tokens, -1); 282*29527Ssam } 283*29527Ssam 284*29527Ssam 285*29527Ssam 286*29527Ssam /* 287*29527Ssam ** 288*29527Ssam */ 289*29527Ssam 290*29527Ssam get_digit_cmd(help) 291*29527Ssam int (*help)(); 292*29527Ssam { 293*29527Ssam int tokens[20], *t_ptr; 294*29527Ssam char line[80]; 295*29527Ssam int results; 296*29527Ssam 297*29527Ssam gets(line); 298*29527Ssam if(!*line) 299*29527Ssam return -1; 300*29527Ssam /* Check for help, status, or kill */ 301*29527Ssam cmd_parse(primary, line, tokens); 302*29527Ssam t_ptr = tokens; 303*29527Ssam while(*t_ptr) { 304*29527Ssam switch (*t_ptr) { 305*29527Ssam case STATUS : 306*29527Ssam cmd_status(); 307*29527Ssam break; 308*29527Ssam case KQUIT : 309*29527Ssam case KILL : 310*29527Ssam if(confirm(*t_ptr)) { 311*29527Ssam kill_processes = true; 312*29527Ssam return -1; 313*29527Ssam } 314*29527Ssam break; 315*29527Ssam default: 316*29527Ssam (*help)(); 317*29527Ssam break; 318*29527Ssam } 319*29527Ssam t_ptr++; 320*29527Ssam } 321*29527Ssam if(sscanf(line, "%d", &results) > 0) 322*29527Ssam return results; 323*29527Ssam return -1; 324*29527Ssam } 325*29527Ssam 326*29527Ssam 327*29527Ssam /* 328*29527Ssam ** 329*29527Ssam */ 330*29527Ssam 331*29527Ssam get_string_cmd(line, help) 332*29527Ssam char *line; 333*29527Ssam int (*help)(); 334*29527Ssam { 335*29527Ssam int tokens[20], *t_ptr; 336*29527Ssam 337*29527Ssam gets(line); 338*29527Ssam if(!*line) 339*29527Ssam return; 340*29527Ssam /* Check for help, status, or kill */ 341*29527Ssam cmd_parse(primary, line, tokens); 342*29527Ssam t_ptr = tokens; 343*29527Ssam while(*t_ptr) { 344*29527Ssam switch (*t_ptr) { 345*29527Ssam case STATUS : 346*29527Ssam cmd_status(); 347*29527Ssam break; 348*29527Ssam case KQUIT : 349*29527Ssam case KILL : 350*29527Ssam if(confirm(*t_ptr)) { 351*29527Ssam kill_processes = true; 352*29527Ssam return; 353*29527Ssam } 354*29527Ssam break; 355*29527Ssam default: 356*29527Ssam (*help)(); 357*29527Ssam break; 358*29527Ssam } 359*29527Ssam t_ptr++; 360*29527Ssam } 361*29527Ssam while(*line) { 362*29527Ssam *line = tolower(*line); 363*29527Ssam line++; 364*29527Ssam } 365*29527Ssam return; 366*29527Ssam } 367*29527Ssam 368*29527Ssam 369*29527Ssam /* 370*29527Ssam ** 371*29527Ssam */ 372*29527Ssam 373*29527Ssam cmd_status() 374*29527Ssam { 375*29527Ssam indent(); 376*29527Ssam switch (cur.state) { 377*29527Ssam case cmd : 378*29527Ssam print("Waiting for operator input.\n\n"); 379*29527Ssam break; 380*29527Ssam default : 381*29527Ssam status(); 382*29527Ssam break; 383*29527Ssam } 384*29527Ssam exdent(1); 385*29527Ssam } 386*29527Ssam 387*29527Ssam 388*29527Ssam /* 389*29527Ssam ** Vdget_yes_no is used to ask simple yes or no questions. The question 390*29527Ssam ** prompt is supplied by the caller, The question mark, possible responses, 391*29527Ssam ** and the default response is printed at the end of the prompt. The routine 392*29527Ssam ** then reads the answer and returns a 1 if a 'y' is typed or no response was 393*29527Ssam ** given, otherwise, a zero is returned. 394*29527Ssam */ 395*29527Ssam 396*29527Ssam boolean get_yes_no(str) 397*29527Ssam register char *str; 398*29527Ssam { 399*29527Ssam char answer[80]; 400*29527Ssam boolean retval; 401*29527Ssam 402*29527Ssam for(;;) { 403*29527Ssam if(*str) 404*29527Ssam print("%s", str); 405*29527Ssam printf("? [Yes/No] "); 406*29527Ssam gets(answer); 407*29527Ssam if((answer[0] == 'Y') || (answer[0] == 'y')) { 408*29527Ssam retval = true; 409*29527Ssam break; 410*29527Ssam } 411*29527Ssam if((answer[0] == 'N') || (answer[0] == 'n')) { 412*29527Ssam retval = false; 413*29527Ssam break; 414*29527Ssam } 415*29527Ssam print("\n"); 416*29527Ssam print("A 'Yes' or 'No' must be entered!\n\n"); 417*29527Ssam } 418*29527Ssam return retval; 419*29527Ssam } 420*29527Ssam 421*29527Ssam 422*29527Ssam /* 423*29527Ssam ** 424*29527Ssam */ 425*29527Ssam 426*29527Ssam get_next_digit(ptr) 427*29527Ssam char *ptr; 428*29527Ssam { 429*29527Ssam int results; 430*29527Ssam 431*29527Ssam finddigit(ptr); 432*29527Ssam if(sscanf(ptr, "%d", &results) <= 0) 433*29527Ssam return -1; 434*29527Ssam return results; 435*29527Ssam } 436