github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/lib9/tokenize.c (about) 1 // +build !plan9 2 3 /* 4 Inferno lib9/tokenize.c 5 http://code.google.com/p/inferno-os/source/browse/lib9/tokenize.c 6 7 Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. 8 Revisions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com). All rights reserved. 9 10 Permission is hereby granted, free of charge, to any person obtaining a copy 11 of this software and associated documentation files (the "Software"), to deal 12 in the Software without restriction, including without limitation the rights 13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 copies of the Software, and to permit persons to whom the Software is 15 furnished to do so, subject to the following conditions: 16 17 The above copyright notice and this permission notice shall be included in 18 all copies or substantial portions of the Software. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 THE SOFTWARE. 27 */ 28 29 #include <u.h> 30 #include <libc.h> 31 32 static char qsep[] = " \t\r\n"; 33 34 static char* 35 qtoken(char *s, char *sep) 36 { 37 int quoting; 38 char *t; 39 40 quoting = 0; 41 t = s; /* s is output string, t is input string */ 42 while(*t!='\0' && (quoting || utfrune(sep, (Rune)*t)==nil)){ 43 if(*t != '\''){ 44 *s++ = *t++; 45 continue; 46 } 47 /* *t is a quote */ 48 if(!quoting){ 49 quoting = 1; 50 t++; 51 continue; 52 } 53 /* quoting and we're on a quote */ 54 if(t[1] != '\''){ 55 /* end of quoted section; absorb closing quote */ 56 t++; 57 quoting = 0; 58 continue; 59 } 60 /* doubled quote; fold one quote into two */ 61 t++; 62 *s++ = *t++; 63 } 64 if(*s != '\0'){ 65 *s = '\0'; 66 if(t == s) 67 t++; 68 } 69 return t; 70 } 71 72 static char* 73 etoken(char *t, char *sep) 74 { 75 int quoting; 76 77 /* move to end of next token */ 78 quoting = 0; 79 while(*t!='\0' && (quoting || utfrune(sep, (Rune)*t)==nil)){ 80 if(*t != '\''){ 81 t++; 82 continue; 83 } 84 /* *t is a quote */ 85 if(!quoting){ 86 quoting = 1; 87 t++; 88 continue; 89 } 90 /* quoting and we're on a quote */ 91 if(t[1] != '\''){ 92 /* end of quoted section; absorb closing quote */ 93 t++; 94 quoting = 0; 95 continue; 96 } 97 /* doubled quote; fold one quote into two */ 98 t += 2; 99 } 100 return t; 101 } 102 103 int 104 gettokens(char *s, char **args, int maxargs, char *sep) 105 { 106 int nargs; 107 108 for(nargs=0; nargs<maxargs; nargs++){ 109 while(*s!='\0' && utfrune(sep, (Rune)*s)!=nil) 110 *s++ = '\0'; 111 if(*s == '\0') 112 break; 113 args[nargs] = s; 114 s = etoken(s, sep); 115 } 116 117 return nargs; 118 } 119 120 int 121 tokenize(char *s, char **args, int maxargs) 122 { 123 int nargs; 124 125 for(nargs=0; nargs<maxargs; nargs++){ 126 while(*s!='\0' && utfrune(qsep, (Rune)*s)!=nil) 127 s++; 128 if(*s == '\0') 129 break; 130 args[nargs] = s; 131 s = qtoken(s, qsep); 132 } 133 134 return nargs; 135 }