xref: /plan9/sys/src/cmd/unix/u9fs/tokenize.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include <plan9.h>
2 
3 int
getfields(char * str,char ** args,int max,int mflag,char * set)4 getfields(char *str, char **args, int max, int mflag, char *set)
5 {
6 	Rune r;
7 	int nr, intok, narg;
8 
9 	if(max <= 0)
10 		return 0;
11 
12 	narg = 0;
13 	args[narg] = str;
14 	if(!mflag)
15 		narg++;
16 	intok = 0;
17 	for(;; str += nr) {
18 		nr = chartorune(&r, str);
19 		if(r == 0)
20 			break;
21 		if(utfrune(set, r)) {
22 			if(narg >= max)
23 				break;
24 			*str = 0;
25 			intok = 0;
26 			args[narg] = str + nr;
27 			if(!mflag)
28 				narg++;
29 		} else {
30 			if(!intok && mflag)
31 				narg++;
32 			intok = 1;
33 		}
34 	}
35 	return narg;
36 }
37 
38 int
tokenize(char * str,char ** args,int max)39 tokenize(char *str, char **args, int max)
40 {
41 	return getfields(str, args, max, 1, " \t\n\r");
42 }
43