The Go Programming Language

Text file src/lib9/tokenize.c

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

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.