ソースを参照

add: 为bsp/win32开放io.popen接口

Wendal Chen 4 年 前
コミット
86126a4f99
3 ファイル変更70 行追加63 行削除
  1. 3 1
      .gitignore
  2. 20 18
      lua/src/liolib.c
  3. 47 44
      lua/src/loslib.c

+ 3 - 1
.gitignore

@@ -84,4 +84,6 @@ bsp/air302/air302_sdk/**
 
 debug.log
 
-量产文件
+量产文件
+
+CMakefiles

+ 20 - 18
lua/src/liolib.c

@@ -282,25 +282,25 @@ static int io_open (lua_State *L) {
   return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
 }
 
-
-// /*
-// ** function to close 'popen' files
-// */
-// static int io_pclose (lua_State *L) {
-//   LStream *p = tolstream(L);
-//   return luaL_execresult(L, l_pclose(L, p->f));
-// }
+#ifdef LUA_USE_WINDOWS
+/*
+** function to close 'popen' files
+*/
+static int io_pclose (lua_State *L) {
+  LStream *p = tolstream(L);
+  return luaL_execresult(L, l_pclose(L, p->f));
+}
 
 
-// static int io_popen (lua_State *L) {
-//   const char *filename = luaL_checkstring(L, 1);
-//   const char *mode = luaL_optstring(L, 2, "r");
-//   LStream *p = newprefile(L);
-//   p->f = l_popen(L, filename, mode);
-//   p->closef = &io_pclose;
-//   return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
-// }
-
+static int io_popen (lua_State *L) {
+  const char *filename = luaL_checkstring(L, 1);
+  const char *mode = luaL_optstring(L, 2, "r");
+  LStream *p = newprefile(L);
+  p->f = l_popen(L, filename, mode);
+  p->closef = &io_pclose;
+  return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
+}
+#endif
 
 // static int io_tmpfile (lua_State *L) {
 //   LStream *p = newfile(L);
@@ -779,7 +779,9 @@ static const rotable_Reg iolib[] = {
   // {"lines", io_lines,  0},
   {"open", io_open,    0},
   // {"output", io_output,0},
-  // {"popen", io_popen,  0},
+#ifdef LUA_USE_WINDOWS
+  {"popen", io_popen,  0},
+#endif
   // {"read", io_read,    0},
   // {"tmpfile", io_tmpfile, 0},
   {"type", io_type,    0},

+ 47 - 44
lua/src/loslib.c

@@ -139,18 +139,18 @@ static time_t l_checktime (lua_State *L, int arg) {
 
 
 
-
-// static int os_execute (lua_State *L) {
-//   const char *cmd = luaL_optstring(L, 1, NULL);
-//   int stat = system(cmd);
-//   if (cmd != NULL)
-//     return luaL_execresult(L, stat);
-//   else {
-//     lua_pushboolean(L, stat);  /* true if there is a shell */
-//     return 1;
-//   }
-// }
-
+#ifdef LUA_USE_WINDOWS
+static int os_execute (lua_State *L) {
+  const char *cmd = luaL_optstring(L, 1, NULL);
+  int stat = system(cmd);
+  if (cmd != NULL)
+    return luaL_execresult(L, stat);
+  else {
+    lua_pushboolean(L, stat);  /* true if there is a shell */
+    return 1;
+  }
+}
+#endif
 
 static int os_remove (lua_State *L) {
   const char *filename = luaL_checkstring(L, 1);
@@ -175,11 +175,12 @@ static int os_rename (lua_State *L) {
 //   return 1;
 // }
 
-
-// static int os_getenv (lua_State *L) {
-//   lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
-//   return 1;
-// }
+#ifdef LUA_USE_WINDOWS
+static int os_getenv (lua_State *L) {
+  lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
+  return 1;
+}
+#endif
 
 
 static int os_clock (lua_State *L) {
@@ -359,42 +360,44 @@ static int os_difftime (lua_State *L) {
 
 /* }====================================================== */
 
+#ifdef LUA_USE_WINDOWS
+static int os_setlocale (lua_State *L) {
+  static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
+                      LC_NUMERIC, LC_TIME};
+  static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
+     "numeric", "time", NULL};
+  const char *l = luaL_optstring(L, 1, NULL);
+  int op = luaL_checkoption(L, 2, "all", catnames);
+  lua_pushstring(L, setlocale(cat[op], l));
+  return 1;
+}
 
-// static int os_setlocale (lua_State *L) {
-//   static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
-//                       LC_NUMERIC, LC_TIME};
-//   static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
-//      "numeric", "time", NULL};
-//   const char *l = luaL_optstring(L, 1, NULL);
-//   int op = luaL_checkoption(L, 2, "all", catnames);
-//   lua_pushstring(L, setlocale(cat[op], l));
-//   return 1;
-// }
-
-
-// static int os_exit (lua_State *L) {
-//   int status;
-//   if (lua_isboolean(L, 1))
-//     status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
-//   else
-//     status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
-//   if (lua_toboolean(L, 2))
-//     lua_close(L);
-//   if (L) exit(status);  /* 'if' to avoid warnings for unreachable 'return' */
-//   return 0;
-// }
+static int os_exit (lua_State *L) {
+  int status;
+  if (lua_isboolean(L, 1))
+    status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
+  else
+    status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
+  if (lua_toboolean(L, 2))
+    lua_close(L);
+  if (L) exit(status);  /* 'if' to avoid warnings for unreachable 'return' */
+  return 0;
+}
+#endif
 
 #include "rotable.h"
 static const rotable_Reg syslib[] = {
   {"clock",     os_clock, 0},
   {"date",      os_date, 0},
   {"difftime",  os_difftime, 0},
-//  {"execute",   os_execute, 0},
-//  {"exit",      os_exit, 0},
-//  {"getenv",    os_getenv, 0},
+#ifdef LUA_USE_WINDOWS
+ {"execute",   os_execute, 0},
+ {"exit",      os_exit, 0},
+ {"getenv",    os_getenv, 0},
+ {"setlocale", os_setlocale, 0},
+#endif
   {"remove",    os_remove, 0},
   {"rename",    os_rename, 0},
-//  {"setlocale", os_setlocale, 0},
   {"time",      os_time, 0},
 //  {"tmpname",   os_tmpname, 0},
   {NULL, NULL}