/* * **** FIND CMD **** * * This routine searches for a command entry for the current line * */ #include #include #include #include "pscover.h" struct keytab * find_cmd(char *str) { char cmd_name[BUFSIZ]; /* command name */ char *p, *q; /* pointers for parsing */ int i; /* loop index */ struct keytab *ret = NULL; /* return code */ /* first, build command name */ p = str; /* skip command character */ q = cmd_name; /* target string */ for (; *p != '\0' && ! isspace(*p); p++) { /* copy characters */ if (isupper(*p)) *p = tolower(*p); /* convert to lower case */ *q++ = *p; } *q = '\0'; /* terminate command name */ /* p points to after the command name */ if (*p != '\0') p++; /* skip first white space */ cmd_dat = p; /* save data for later */ /* now search for a prefix */ if (strcmp(cmd_name,"left") == 0) { cmd_type = KEY_LRF; cmd_just = CMD_LEFT; while (isspace(*p)) p++; /* find command name */ cmd_dat = get_data(cmd_name,p); /* return next word for data */ p = cmd_name; /* point to actual command name */ } else if (strcmp(cmd_name,"right") == 0) { cmd_type = KEY_LRF; cmd_just= CMD_RIGHT; while (isspace(*p)) p++; cmd_dat = get_data(cmd_name,p); p = cmd_name; /* point to actual command name */ } else if (strcmp(cmd_name,"full") == 0) { cmd_type = KEY_LRF; cmd_just= CMD_FULL; while (isspace(*p)) p++; cmd_dat = get_data(cmd_name,p); p = cmd_name; /* point to actual command name */ } else if (strcmp(cmd_name,"center") == 0) { cmd_type = KEY_LRF; cmd_just = CMD_CENT; while (isspace(*p)) p++; cmd_dat = get_data(cmd_name,p); p = cmd_name; /* point to actual command name */ } else if ((p = strbeg(cmd_name,"left")) != NULL) { /* left type */ cmd_type = KEY_LRF; cmd_just = CMD_LEFT; } else if ((p = strbeg(cmd_name,"right")) != NULL) { cmd_type = KEY_LRF; cmd_just = CMD_RIGHT; } else if ((p = strbeg(cmd_name,"full")) != NULL) { cmd_type = KEY_LRF; cmd_just = CMD_FULL; } else if ((p = strbeg(cmd_name,"center")) != NULL) { cmd_type = KEY_LRF; cmd_just = CMD_CENT; } else { /* nothing else, must be a regular command */ cmd_type = KEY_NO; cmd_just = CMD_NO; p = cmd_name; } /* now search for command string */ for (q = p; *q != '\0' && ! isspace(*q); q++); /* find end of command */ for (i = 0; KeyTab[i].key_word != NULL; i++) { /* see if this is a command */ if (stricmp(KeyTab[i].key_word,p) == 0) { /* match found - set return code */ ret = KeyTab + i; /* remember command */ break; /* quit the loop */ } } if (ret == NULL) ret = ErrTab + ERR_NOT; /* command not found */ else if (ret->key_type != KEY_NO && (ret->key_mask & cmd_just) == 0) ret = ErrTab + ERR_VAR; return ret; } char * strbeg(char *str1,char *str2) { /* see if str1 begins with str2 */ int len; /* length of str2 */ len = strlen(str2); /* length to compare */ if (strncmp(str1,str2,len) == 0) return str1 + len; return NULL; } char * get_data(char *cmd_name, char *buf) { /* get command data - p points to command name */ char *p; /* parsing pointer */ for (p = buf; *p != '\0' && ! isspace(*p); p++) { if (isupper(*p)) *p = tolower(*p); *cmd_name++ = *p; } *cmd_name = '\0'; if (*p != '\0') p++; return p; }