Commit 2c7e46d6
2c7e46d6eb74Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #527 from ydah/fix/system-argument-list
fix(codegen,runtime): pass system argument lists
Details
- Commit
- 2c7e46d6eb74
- Parent
- cafb94bbddfa
- Merge parent
- 52e92fee3c39
lib/sp_runtime.hmodifiedView file
@@ -24,8 +24,10 @@
2424
#include <stdarg.h>2525
#include <time.h>2626
#include <setjmp.h>27+
#include <errno.h>2728
#ifdef _WIN322829
#include <windows.h>30+
#include <process.h>2931
/* POSIX compat shims for MinGW */3032
#define mmap(a,l,p,f,fd,off) VirtualAlloc(NULL,(l),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE)3133
#define munmap(a,l) (VirtualFree((a),0,MEM_RELEASE)?0:-1)@@ -39,6 +41,7 @@
3941
#include <ucontext.h>4042
#include <unistd.h>4143
#include <sys/mman.h>44+
#include <sys/wait.h>4245
#endif4346
#if !defined(__APPLE__) && !defined(_WIN32) && !defined(__FreeBSD__)4447
#include <malloc.h>@@ -1748,6 +1751,147 @@
17481751
17491752
/* System/backtick support */17501753
static int sp_last_status = 0;1754+
#ifdef _WIN321755+
static char *sp_win_quote_arg(const char *arg) {1756+
const char *p = arg;1757+
size_t len = 2;1758+
mrb_bool quote = (*p == '\0') ? TRUE : FALSE;1759+
while (*p) {1760+
if (isspace((unsigned char)*p) || *p == '"') quote = TRUE;1761+
if (*p == '"') {1762+
len += 2;1763+
}1764+
else {1765+
len += 1;1766+
}1767+
p++;1768+
}1769+
if (!quote) {1770+
char *copy = (char *)malloc(len - 1);1771+
if (copy) memcpy(copy, arg, len - 1);1772+
return copy;1773+
}1774+
1775+
p = arg;1776+
size_t bs = 0;1777+
len = 3;1778+
while (*p) {1779+
if (*p == '\\') {1780+
bs++;1781+
}1782+
else if (*p == '"') {1783+
len += bs * 2 + 2;1784+
bs = 0;1785+
}1786+
else {1787+
len += bs + 1;1788+
bs = 0;1789+
}1790+
p++;1791+
}1792+
len += bs * 2;1793+
1794+
char *out = (char *)malloc(len);1795+
if (!out) return NULL;1796+
char *q = out;1797+
*q++ = '"';1798+
p = arg;1799+
bs = 0;1800+
while (*p) {1801+
if (*p == '\\') {1802+
bs++;1803+
}1804+
else if (*p == '"') {1805+
while (bs > 0) {1806+
*q++ = '\\';1807+
*q++ = '\\';1808+
bs--;1809+
}1810+
*q++ = '\\';1811+
*q++ = '"';1812+
bs = 0;1813+
}1814+
else {1815+
while (bs > 0) {1816+
*q++ = '\\';1817+
bs--;1818+
}1819+
*q++ = *p;1820+
bs = 0;1821+
}1822+
p++;1823+
}1824+
while (bs > 0) {1825+
*q++ = '\\';1826+
*q++ = '\\';1827+
bs--;1828+
}1829+
*q++ = '"';1830+
*q = '\0';1831+
return out;1832+
}1833+
#endif1834+
1835+
static mrb_bool sp_system_args(int argc, const char *const *argv) {1836+
if (argc <= 0 || argv == NULL || argv[0] == NULL) {1837+
sp_last_status = -1;1838+
return FALSE;1839+
}1840+
fflush(NULL);1841+
#ifdef _WIN321842+
char **quoted_argv = (char **)malloc(sizeof(char *) * (size_t)(argc + 1));1843+
if (!quoted_argv) {1844+
sp_last_status = -1;1845+
return FALSE;1846+
}1847+
int i = 0;1848+
while (i < argc) {1849+
if (argv[i] == NULL) {1850+
while (i-- > 0) free(quoted_argv[i]);1851+
free(quoted_argv);1852+
sp_last_status = -1;1853+
return FALSE;1854+
}1855+
quoted_argv[i] = sp_win_quote_arg(argv[i]);1856+
if (!quoted_argv[i]) {1857+
while (i-- > 0) free(quoted_argv[i]);1858+
free(quoted_argv);1859+
sp_last_status = -1;1860+
return FALSE;1861+
}1862+
i++;1863+
}1864+
quoted_argv[argc] = NULL;1865+
1866+
intptr_t rc = _spawnvp(_P_WAIT, argv[0], (const char *const *)quoted_argv);1867+
for (i = 0; i < argc; i++) free(quoted_argv[i]);1868+
free(quoted_argv);1869+
if (rc < 0) {1870+
sp_last_status = -1;1871+
return FALSE;1872+
}1873+
sp_last_status = (int)rc << 8;1874+
return rc == 0 ? TRUE : FALSE;1875+
#else1876+
pid_t pid = fork();1877+
if (pid < 0) {1878+
sp_last_status = -1;1879+
return FALSE;1880+
}1881+
if (pid == 0) {1882+
execvp(argv[0], (char * const *)argv);1883+
_exit(127);1884+
}1885+
int status = 0;1886+
while (waitpid(pid, &status, 0) < 0) {1887+
if (errno == EINTR) continue;1888+
sp_last_status = -1;1889+
return FALSE;1890+
}1891+
sp_last_status = status;1892+
return (WIFEXITED(status) && WEXITSTATUS(status) == 0) ? TRUE : FALSE;1893+
#endif1894+
}17511895
17521896
/* Bigint (linked from libspinel_rt.a) */17531897
typedef struct sp_Bigint sp_Bigint;spinel_codegen.rbmodifiedView file
Loading diff…
test/system_argument_list.rbaddedView file
Loading diff…
test/system_argument_list.rb.expectedaddedView file
Loading diff…