Skip to content

Instantly share code, notes, and snippets.

@mattn
Created June 5, 2012 05:33
Show Gist options
  • Save mattn/2872887 to your computer and use it in GitHub Desktop.
Save mattn/2872887 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int
main(int argc, char* argv[]) {
int n;
for (n = 0; n < argc; n++) {
printf("%s\n", argv[n]);
}
return 0;
}
function! s:system(...)
let cmd = ''
for a in a:000
if len(cmd) | let cmd .= ' ' | endif
if a =~ '\s'
if len(split(a, '"', 1)) % 2 == 0
let a = '^"'.substitute(a, '"', '"""', 'g').'"'
else
let a = '"'.substitute(a, '"', '"""', 'g').'"'
endif
else
let a = substitute(a, '"', '""""', 'g')
endif
let a = substitute(a, "'", "''", 'g')
let cmd .= a
endfor
return system(cmd)
endfunction
function! s:test(...)
let ret = call('s:system', a:000)
let arr = split(ret, "\n", 1)[:-2]
echo (string(arr[1:]) ==# string(a:000[1:]) ? "OK" : "NG") . ": " . string(arr)
endfunction
call s:test('arglist', '-"foo"', "bar")
call s:test('arglist', '-foo', "bar")
call s:test('arglist', '-f oo', "bar")
call s:test('arglist', '-f "oo', "bar")
call s:test('arglist', '-f ""oo', "bar")
call s:test('arglist', '-f """oo', "bar")
call s:test('arglist', '"-f """oo', "bar")
call s:test('arglist', '"-f """oo"', "bar")
call s:test('arglist', 'bar', '-"foo"', "bar")
call s:test('arglist', 'b ar', '-foo', "bar")
call s:test('arglist', 'ba r', '-f oo', "bar")
call s:test('arglist', 'b a r', '-f "oo', "bar")
call s:test('arglist', 'bar', '-f ""oo', "bar")
call s:test('arglist', ' bar', '-f """oo', "bar")
call s:test('arglist', 'bar ', '"-f """oo', "bar")
call s:test('arglist', ' b a r ', '"-f """oo"', "bar")
call s:test('arglist', ' "b a r" ', '"-f """oo"', "bar")
call s:test('arglist', ' "b "a" r" ', '"-f """oo"', "bar")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment