1: /* cmdhdl.c 1.12 85/03/24 */ 2: 3: #include <ctype.h> 4: #include <stdio.h> 5: #include "cpmio.h" 6: 7: /* 8: * Write prompt to stdout. 9: * Read command from stdin. 10: * return the number of characters read. 11: */ 12: 13: cmdinp(cmd) 14: char cmd[]; 15: { 16: 17: int cnt = 0; 18: 19: while (cnt == 0) { 20: printf("cpm> "); 21: while((cmd[cnt] = getchar()) != '\n') { 22: if (cmd[cnt] == EOF) 23: return(-1); 24: cnt++; 25: } 26: cmd[cnt] = '\0'; 27: } 28: return (cnt); 29: } 30: 31: /* 32: * Compare the command pointed to by cmd to the table of defined 33: * commands in defcmd, return the command index if found, null 34: * otherwise. 35: */ 36: 37: struct command { 38: char *cmd; 39: int lth, abbr; 40: } defcmd[] = { 41: "directory", 9, 1, /* 1 */ 42: "rename", 6, 1, /* 2 */ 43: "copyin", 6, 0, /* 3 */ 44: "delete", 6, 1, /* 4 */ 45: "erase", 5, 1, /* 5 */ 46: "exit", 4, 1, /* 6 */ 47: "type", 4, 1, /* 7 */ 48: "help", 4, 1, /* 8 */ 49: "ls ", 2, 0, /* 9 */ 50: "logout", 6, 1, /* 10 */ 51: "ccopyin", 7, 0, /* 11 */ 52: "ccopyout", 8, 0, /* 12 */ 53: "copyout", 7, 0, /* 13 */ 54: "dump", 4, 1, /* 14 */ 55: /*"!! ", 2, 0, /* 15 */ 56: '\0' , 0 }; 57: 58: chkcmd(cmd) 59: char *cmd; 60: 61: { 62: int index, len; 63: 64: len = strlen(cmd); 65: for (index=0; *defcmd[index].cmd != '\0' ; index++) { 66: if ((len == 3) && defcmd[index].abbr) { 67: if (strncmp(defcmd[index].cmd, cmd, 3) == 0) 68: goto ok; 69: } else { 70: if (strncmp(defcmd[index].cmd, cmd, defcmd[index].lth) 71: == 0) 72: goto ok; 73: } 74: } 75: return (0); 76: 77: ok: 78: if (len > defcmd[index].lth) 79: return (0); 80: else 81: return (++index); 82: } 83: 84: help() 85: { 86: 87: FILE *fd, *fopen(); 88: int c; 89: 90: if ((fd = fopen(HELPFILE, "r")) == NULL) 91: printf("Can't find help file (cpm.hlp) \n"); 92: else 93: while ( (c = getc(fd)) != EOF) 94: putchar(c); 95: } 96: 97: 98: /* 99: * Separate fname into name and extension, return NULL if 100: * bad file name, otherwise 1. 101: */ 102: 103: namesep(fname, name, ext) 104: char fname[], name[], ext[]; 105: { 106: 107: int i = 0; 108: 109: strncpy(name, " ", 9); 110: strncpy(ext, " ", 4); 111: while(i<8 && !(iscntrl(fname[i])) && fname[i] != '.') { 112: name[i] = fname[i]; 113: i++; 114: } 115: #ifdef DEBUG 116: printf("namesep: name=%s, len=%d ", name, i); 117: #endif 118: clean(name, 8); 119: if (fname[i] == '.') { 120: strncpy(ext, fname+i+1, 3); 121: clean(ext, 3); 122: } else { 123: if (fname[i] != ' ' && fname[i] != '\0') { 124: fprintf(stderr, "Illegal filename\n"); 125: return (NULL); 126: } 127: } 128: #ifdef DEBUG 129: printf("name: %s, ext: %s, combined: %s\n", name, ext, 130: fixname(name, ext)); 131: #endif 132: if (!(isalnum(name[0]))) { 133: fprintf(stderr, "Illegal filename\n"); 134: return(NULL); 135: } 136: return(1); 137: } 138: 139: clean(str, len) 140: char str[]; 141: int len; 142: { 143: str[len] = '\0'; 144: while (len-- > 0) { 145: if (!(isspace(str[len])) && !(iscntrl(str[len]))) break; 146: str[len] = ' '; 147: } 148: while (len >= 0) { 149: str[len] = islower(str[len]) ? toupper(str[len]):str[len] ; 150: len--; 151: } 152: }