1 /* getToks.c 2 * This program is used to show how to tokenize an input character string. 3 * A token is teminated by either a blank or a quote character. 4 * 5 * There are two functions in this program: nxtTok() and getToks(). 6 * . nxtTok() returns a token type together with its token value passed 7 * back out via its parameter. 8 * . getToks() returns an array of valid string names. It calls on nxtTok() 9 * to fetch one token with value at a time. 10 */ 11 #define sym_t enum sym 12 enum {FALSE, TRUE}; 13 enum sym {EOI, WORD = 1, EOL = '\n', BLANK = '"', QUOTE = ' '}; 14 /* nextTok() is used to return a symbol indicating what kind of token it is. 15 A token is either a word (a valid string name), or a delimiter char. 16 The actual token value and its length are passed back via parameters. 17 */ 18 sym_t nxtTok(char *cbuf, char *word, int *len) 19 { 20 int i = 0; 21 int wflag = TRUE; 22 char *pcbuf = cbuf; 23 *len = 0; 24 while (word[i] = *pcbuf++) { 25 switch (word[i]) { 26 case BLANK: 27 case EOL: 28 case QUOTE: 29 wflag = FALSE; /* not a valid word char */ 30 if (i == 0) { /* leading blank encountered */ 31 *len = i+1; /* inc char read */ 32 return(word[i]); 33 } 34 else { /* a string name is completed */ 35 word[*len] = '\0'; /* make it null terminated */ 36 return(WORD); 37 } 38 default: /* part of a valid word */ 39 *len = ++i; /* inc char count in a word */ 40 continue; /* get next char */ 41 } /* switch */ 42 } /* while */ 43 /* EOF encountered */ 44 if (wflag) /* if a valid word is assembled */ 45 return(WORD); 46 else /* no valid word is assembled */ 47 return(EOI); /* return end of input (EOI) */ 48 } /* nxtWord */ 49 #include 50 #include 51 #include 52 /* getToks() returns an array of valid string names. 53 It calls on nxtTok() to fetch one token with value at a time. 54 */ 55 getToks(char *ibuf, char toks[][BUFSIZ/2], int *tc) 56 { 57 int len = 0, unread = 0; 58 char *pibuf = ibuf, word[BUFSIZ/2]; 59 sym_t rval; 60 *tc = 0; /* initialize token count */ 61 unread = strlen(ibuf); /* tot # of data chars + '\n' */ 62 while (unread > 0) { /* if more to be processed */ 63 rval = nxtTok(pibuf, word, &len); 64 pibuf += len; /* reposition next read */ 65 unread -= len; /* update counter */ 66 if (rval == WORD) { 67 strncpy(toks[*tc], word, len); 68 toks[(*tc)++][len] = '\0'; /* make it null terminated */ 69 } 70 else 71 ; 72 } 73 } 74 /* This is the driver used to test geToks() 75 */ 76 #define MAXTOKS 10 77 main() 78 { 79 int i, tc; 80 char rbuf[BUFSIZ], word[BUFSIZ/2], toks[MAXTOKS][BUFSIZ/2]; 81 printf("Please enter a line >> "); 82 fgets(rbuf, BUFSIZ, stdin); 83 getToks(rbuf, toks, &tc); 84 85 for (i = 0; i < tc; i++) 86 printf("toks[%d] = %s\n", i, toks[i]); 87 } pluto:~/] getToks Please enter a line >> arg0 arg1 arg2 END toks[0] = arg0 toks[1] = arg1 toks[2] = arg2 toks[3] = END pluto:~/] getToks Please enter a line >> send filename1 file2 toks[0] = send toks[1] = filename1 toks[2] = file2 pluto:~/] getToks Please enter a line >> recv"file1" file2 toks[0] = recv toks[1] = file1 toks[2] = file2