summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2017-03-10 09:55:42 +0800
committerruki <[email protected]>2017-03-10 09:55:42 +0800
commitf99ff317a4b67d7508b40842f83aaea74ab7e20e (patch)
treecec35f7c9938f78ad06731a41300e097b6d4b894 /core/src
parent952b7eb1d18223a664c0c9b20094201cad9a53c8 (diff)
fix compile error for rust android
Diffstat (limited to 'core/src')
-rwxr-xr-xcore/src/xmake/os/argv.c83
1 files changed, 29 insertions, 54 deletions
diff --git a/core/src/xmake/os/argv.c b/core/src/xmake/os/argv.c
index 233b55d0b..359c94331 100755
--- a/core/src/xmake/os/argv.c
+++ b/core/src/xmake/os/argv.c
@@ -47,48 +47,21 @@ tb_int_t xm_os_argv(lua_State* lua)
tb_check_return_val(args, 0);
// done
- tb_char_t* buffer = tb_null;
+ tb_string_t arg;
do
{
- // make buffer
- tb_size_t maxn = TB_PATH_MAXN;
- buffer = (tb_char_t*)tb_malloc(maxn);
- tb_assert_and_check_break(buffer);
-
- // copy and translate command
- tb_char_t ch;
- tb_size_t i = 0;
- tb_size_t j = 0;
- for (i = 0; j <= maxn && (ch = args[i]); i++)
- {
- // not enough? grow it
- if (j == maxn)
- {
- // grow it
- maxn += TB_PATH_MAXN;
- buffer = (tb_char_t*)tb_ralloc(buffer, maxn);
- tb_assert_and_check_break(buffer);
- }
-
- // translate "\"", "\'", "\\"
- tb_char_t next = args[i + 1];
- if (ch == '\\' && (next == '\"' || next == '\'' || next == '\\')) /* skip it */ ;
- // copy it
- else buffer[j++] = ch;
- }
- tb_assert_and_check_break(j < maxn);
- buffer[j] = '\0';
-
- // reset index
- i = 1;
-
// init table
lua_newtable(lua);
+ // init arg
+ if (!tb_string_init(&arg)) break;
+
// parse command to the arguments
- tb_bool_t s = 0;
- tb_char_t* p = buffer;
- tb_char_t* b = tb_null;
+ tb_int_t i = 1;
+ tb_int_t s = 0;
+ tb_int_t escape = 0;
+ tb_char_t ch = 0;
+ tb_char_t const* p = args;
while ((ch = *p))
{
// enter double quote?
@@ -97,47 +70,49 @@ tb_int_t xm_os_argv(lua_State* lua)
else if (!s && ch == '\'') s = 1;
// leave quote?
else if ((s == 2 && ch == '\"') || (s == 1 && ch == '\'')) s = 0;
+ // escape charactor?
+ else if (!escape && ch == '\\') escape = 1;
// is argument end with ' '?
else if (!s && tb_isspace(ch))
{
- // fill zero
- *p = '\0';
-
// save this argument
- if (b)
+ tb_string_ltrim(&arg);
+ if (tb_string_size(&arg))
{
// save argument
- lua_pushstring(lua, b);
+ lua_pushstring(lua, tb_string_cstr(&arg));
lua_rawseti(lua, -2, i++);
-
- // clear it
- b = tb_null;
}
+
+ // clear arg
+ tb_string_clear(&arg);
}
- // get the argument pointer
- if ((s || !tb_isspace(ch)) && !b) b = p;
+ // save this charactor to arg if not escape charactor: '\\'
+ if (escape == 2 || (!escape && ch != '\"' && ch != '\''))
+ tb_string_chrcat(&arg, ch);
+
+ // step and cancel escape
+ if (escape == 1) escape++;
+ else if (escape == 2) escape = 0;
// next
p++;
}
// save this argument
- if (b)
+ tb_string_ltrim(&arg);
+ if (tb_string_size(&arg))
{
// save argument
- lua_pushstring(lua, b);
+ lua_pushstring(lua, tb_string_cstr(&arg));
lua_rawseti(lua, -2, i++);
-
- // clear it
- b = tb_null;
}
} while (0);
- // exit buffer
- if (buffer) tb_free(buffer);
- buffer = tb_null;
+ // exit arg
+ tb_string_exit(&arg);
// ok
return 1;