1234567891011121314151617181920212223242526272829303132333435363738394041424344454647(*
* Copyright (c) mirage-bootvar AUTHORS
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)(* Split string into whitespace-separated substrings,
taking into account quoting *)typestate=Quoted_escaped|Quoted|Escaped|Normalletchars_to_str=function|[]->[]|chars->letchars=List.revcharsin[String.init(List.lengthchars)(List.nthchars)]letparses=letl=String.lengthsinletrecloopacccurrstateidx=ifidx=lthenifstate=NormalthenOk(List.rev(chars_to_strcurr@acc))elseError"bad input line - either escaped or quoted or both"elsematch(state,String.unsafe_getsidx)with|Normal,' '->loop(chars_to_strcurr@acc)[]state(idx+1)|Escaped,c->loopacc(c::curr)Normal(idx+1)|Quoted_escaped,c->loopacc(c::curr)Quoted(idx+1)|Quoted,'\\'->loopacccurrQuoted_escaped(idx+1)|Quoted,'"'->loopacccurrNormal(idx+1)|Quoted,c->loopacc(c::curr)Quoted(idx+1)|Normal,'\\'->loopacccurrEscaped(idx+1)|Normal,'"'->loopacccurrQuoted(idx+1)|Normal,c->loopacc(c::curr)Normal(idx+1)inloop[][]Normal0