16185db85Sdougm /*
26185db85Sdougm  * CDDL HEADER START
36185db85Sdougm  *
46185db85Sdougm  * The contents of this file are subject to the terms of the
56185db85Sdougm  * Common Development and Distribution License (the "License").
66185db85Sdougm  * You may not use this file except in compliance with the License.
76185db85Sdougm  *
86185db85Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96185db85Sdougm  * or http://www.opensolaris.org/os/licensing.
106185db85Sdougm  * See the License for the specific language governing permissions
116185db85Sdougm  * and limitations under the License.
126185db85Sdougm  *
136185db85Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
146185db85Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156185db85Sdougm  * If applicable, add the following below this CDDL HEADER, with the
166185db85Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
176185db85Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
186185db85Sdougm  *
196185db85Sdougm  * CDDL HEADER END
206185db85Sdougm  */
216185db85Sdougm 
226185db85Sdougm /*
238d7e4166Sjose borrego  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
246185db85Sdougm  * Use is subject to license terms.
2533f5ff17SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
266185db85Sdougm  */
276185db85Sdougm 
286185db85Sdougm #include <stdio.h>
296185db85Sdougm #include <ctype.h>
306185db85Sdougm 
316185db85Sdougm #define	TK_INIT		0
326185db85Sdougm #define	TK_TOKEN	1
336185db85Sdougm #define	TK_SKIPWHITE	2
346185db85Sdougm #define	TK_QUOTED	3
356185db85Sdougm 
366185db85Sdougm /*
376185db85Sdougm  * assumes quoted strings are delimited by white space (i.e sp
386185db85Sdougm  * "string" sp). Backslash can be used to quote a quote mark.
396185db85Sdougm  * quoted strings will have the quotes stripped.
406185db85Sdougm  */
416185db85Sdougm 
426185db85Sdougm char *
_sa_get_token(char * string)438d7e4166Sjose borrego _sa_get_token(char *string)
446185db85Sdougm {
456185db85Sdougm 	static char *orig = NULL;
466185db85Sdougm 	static char *curp;
476185db85Sdougm 	char *ret;
486185db85Sdougm 	int state = TK_INIT;
496185db85Sdougm 	int c;
50*2bc647a2SToomas Soome 	int quotechar = 0;
516185db85Sdougm 
526185db85Sdougm 	if (string != orig || string == NULL) {
536185db85Sdougm 		orig = string;
546185db85Sdougm 		curp = string;
556185db85Sdougm 		if (string == NULL) {
566185db85Sdougm 			return (NULL);
576185db85Sdougm 		}
586185db85Sdougm 	}
596185db85Sdougm 	ret = curp;
606185db85Sdougm 	while ((c = *curp) != '\0') {
616185db85Sdougm 		switch (state) {
626185db85Sdougm 		case TK_SKIPWHITE:
636185db85Sdougm 		case TK_INIT:
646185db85Sdougm 			if (isspace(c)) {
656185db85Sdougm 				while (*curp && isspace(*curp))
666185db85Sdougm 					curp++;
676185db85Sdougm 				ret = curp;
686185db85Sdougm 			}
696185db85Sdougm 			if (c == '"' || c == '\'') {
706185db85Sdougm 				state = TK_QUOTED;
716185db85Sdougm 				curp++;
726185db85Sdougm 				ret = curp;
736185db85Sdougm 				quotechar = c; /* want to match for close */
746185db85Sdougm 			} else {
756185db85Sdougm 				state = TK_TOKEN;
766185db85Sdougm 			}
776185db85Sdougm 			break;
786185db85Sdougm 		case TK_TOKEN:
796185db85Sdougm 			switch (c) {
806185db85Sdougm 			case '\\':
816185db85Sdougm 				curp++;
826185db85Sdougm 				if (*curp) {
836185db85Sdougm 					curp++;
846185db85Sdougm 					break;
856185db85Sdougm 				}
8633f5ff17SMilan Jurik 				return (ret);
876185db85Sdougm 			default:
886185db85Sdougm 				if (*curp == '\0' || isspace(c)) {
896185db85Sdougm 					*curp++ = '\0';
906185db85Sdougm 					return (ret);
916185db85Sdougm 				}
926185db85Sdougm 				curp++;
936185db85Sdougm 				break;
946185db85Sdougm 			}
956185db85Sdougm 			break;
966185db85Sdougm 		case TK_QUOTED:
976185db85Sdougm 			switch (c) {
986185db85Sdougm 			case '\\':
996185db85Sdougm 				curp++;
1006185db85Sdougm 				if (*curp) {
1016185db85Sdougm 					curp++;
1026185db85Sdougm 					break;
1036185db85Sdougm 				}
1046185db85Sdougm 				curp++;
1056185db85Sdougm 				break;
1066185db85Sdougm 			default:
1076185db85Sdougm 				if (c == '\0' || c == quotechar) {
1086185db85Sdougm 					*curp++ = '\0';
1096185db85Sdougm 					return (ret);
1106185db85Sdougm 				}
1116185db85Sdougm 				curp++;
1126185db85Sdougm 				break;
1136185db85Sdougm 			}
1146185db85Sdougm 			break;
1156185db85Sdougm 		}
1166185db85Sdougm 	}
1176185db85Sdougm 	return (NULL);
1186185db85Sdougm }
119