Просмотр исходного кода

remove: 清理io和os模块中在嵌入式环境不适用的方法

Wendal Chen 5 лет назад
Родитель
Сommit
3b33b29ca6
2 измененных файлов с 162 добавлено и 162 удалено
  1. 116 116
      lua/src/liolib.c
  2. 46 46
      lua/src/loslib.c

+ 116 - 116
lua/src/liolib.c

@@ -212,11 +212,11 @@ static int f_close (lua_State *L) {
 }
 
 
-static int io_close (lua_State *L) {
-  if (lua_isnone(L, 1))  /* no argument? */
-    lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT);  /* use standard output */
-  return f_close(L);
-}
+// static int io_close (lua_State *L) {
+//   if (lua_isnone(L, 1))  /* no argument? */
+//     lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT);  /* use standard output */
+//   return f_close(L);
+// }
 
 
 static int f_gc (lua_State *L) {
@@ -245,12 +245,12 @@ static LStream *newfile (lua_State *L) {
 }
 
 
-static void opencheck (lua_State *L, const char *fname, const char *mode) {
-  LStream *p = newfile(L);
-  p->f = fopen(fname, mode);
-  if (p->f == NULL)
-    luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
-}
+// static void opencheck (lua_State *L, const char *fname, const char *mode) {
+//   LStream *p = newfile(L);
+//   p->f = fopen(fname, mode);
+//   if (p->f == NULL)
+//     luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
+// }
 
 
 static int io_open (lua_State *L) {
@@ -264,67 +264,67 @@ static int io_open (lua_State *L) {
 }
 
 
-/*
-** 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));
-}
+// /*
+// ** 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;
+// }
 
 
-static int io_tmpfile (lua_State *L) {
-  LStream *p = newfile(L);
-  p->f = tmpfile();
-  return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
-}
+// static int io_tmpfile (lua_State *L) {
+//   LStream *p = newfile(L);
+//   p->f = tmpfile();
+//   return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
+// }
 
 
-static FILE *getiofile (lua_State *L, const char *findex) {
-  LStream *p;
-  lua_getfield(L, LUA_REGISTRYINDEX, findex);
-  p = (LStream *)lua_touserdata(L, -1);
-  if (isclosed(p))
-    luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN);
-  return p->f;
-}
+// static FILE *getiofile (lua_State *L, const char *findex) {
+//   LStream *p;
+//   lua_getfield(L, LUA_REGISTRYINDEX, findex);
+//   p = (LStream *)lua_touserdata(L, -1);
+//   if (isclosed(p))
+//     luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN);
+//   return p->f;
+// }
 
 
-static int g_iofile (lua_State *L, const char *f, const char *mode) {
-  if (!lua_isnoneornil(L, 1)) {
-    const char *filename = lua_tostring(L, 1);
-    if (filename)
-      opencheck(L, filename, mode);
-    else {
-      tofile(L);  /* check that it's a valid file handle */
-      lua_pushvalue(L, 1);
-    }
-    lua_setfield(L, LUA_REGISTRYINDEX, f);
-  }
-  /* return current value */
-  lua_getfield(L, LUA_REGISTRYINDEX, f);
-  return 1;
-}
+// static int g_iofile (lua_State *L, const char *f, const char *mode) {
+//   if (!lua_isnoneornil(L, 1)) {
+//     const char *filename = lua_tostring(L, 1);
+//     if (filename)
+//       opencheck(L, filename, mode);
+//     else {
+//       tofile(L);  /* check that it's a valid file handle */
+//       lua_pushvalue(L, 1);
+//     }
+//     lua_setfield(L, LUA_REGISTRYINDEX, f);
+//   }
+//   /* return current value */
+//   lua_getfield(L, LUA_REGISTRYINDEX, f);
+//   return 1;
+// }
 
 
-static int io_input (lua_State *L) {
-  return g_iofile(L, IO_INPUT, "r");
-}
+// static int io_input (lua_State *L) {
+//   return g_iofile(L, IO_INPUT, "r");
+// }
 
 
-static int io_output (lua_State *L) {
-  return g_iofile(L, IO_OUTPUT, "w");
-}
+// static int io_output (lua_State *L) {
+//   return g_iofile(L, IO_OUTPUT, "w");
+// }
 
 
 static int io_readline (lua_State *L);
@@ -353,24 +353,24 @@ static int f_lines (lua_State *L) {
 }
 
 
-static int io_lines (lua_State *L) {
-  int toclose;
-  if (lua_isnone(L, 1)) lua_pushnil(L);  /* at least one argument */
-  if (lua_isnil(L, 1)) {  /* no file name? */
-    lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT);  /* get default input */
-    lua_replace(L, 1);  /* put it at index 1 */
-    tofile(L);  /* check that it's a valid file handle */
-    toclose = 0;  /* do not close it after iteration */
-  }
-  else {  /* open a new file */
-    const char *filename = luaL_checkstring(L, 1);
-    opencheck(L, filename, "r");
-    lua_replace(L, 1);  /* put file at index 1 */
-    toclose = 1;  /* close it after iteration */
-  }
-  aux_lines(L, toclose);
-  return 1;
-}
+// static int io_lines (lua_State *L) {
+//   int toclose;
+//   if (lua_isnone(L, 1)) lua_pushnil(L);  /* at least one argument */
+//   if (lua_isnil(L, 1)) {  /* no file name? */
+//     lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT);  /* get default input */
+//     lua_replace(L, 1);  /* put it at index 1 */
+//     tofile(L);  /* check that it's a valid file handle */
+//     toclose = 0;  /* do not close it after iteration */
+//   }
+//   else {  /* open a new file */
+//     const char *filename = luaL_checkstring(L, 1);
+//     opencheck(L, filename, "r");
+//     lua_replace(L, 1);  /* put file at index 1 */
+//     toclose = 1;  /* close it after iteration */
+//   }
+//   aux_lines(L, toclose);
+//   return 1;
+// }
 
 
 /*
@@ -576,9 +576,9 @@ static int g_read (lua_State *L, FILE *f, int first) {
 }
 
 
-static int io_read (lua_State *L) {
-  return g_read(L, getiofile(L, IO_INPUT), 1);
-}
+// static int io_read (lua_State *L) {
+//   return g_read(L, getiofile(L, IO_INPUT), 1);
+// }
 
 
 static int f_read (lua_State *L) {
@@ -641,9 +641,9 @@ static int g_write (lua_State *L, FILE *f, int arg) {
 }
 
 
-static int io_write (lua_State *L) {
-  return g_write(L, getiofile(L, IO_OUTPUT), 1);
-}
+// static int io_write (lua_State *L) {
+//   return g_write(L, getiofile(L, IO_OUTPUT), 1);
+// }
 
 
 static int f_write (lua_State *L) {
@@ -684,9 +684,9 @@ static int f_setvbuf (lua_State *L) {
 
 
 
-static int io_flush (lua_State *L) {
-  return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
-}
+// static int io_flush (lua_State *L) {
+//   return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
+// }
 
 
 static int f_flush (lua_State *L) {
@@ -699,17 +699,17 @@ static int f_flush (lua_State *L) {
 */
 #include "rotable.h"
 static const rotable_Reg iolib[] = {
-  {"close", io_close,  0},
-  {"flush", io_flush,  0},
-  {"input", io_input,  0},
-  {"lines", io_lines,  0},
+  // {"close", io_close,  0},
+  // {"flush", io_flush,  0},
+  // {"input", io_input,  0},
+  // {"lines", io_lines,  0},
   {"open", io_open,    0},
-  {"output", io_output,0},
-  {"popen", io_popen,  0},
-  {"read", io_read,    0},
-  {"tmpfile", io_tmpfile, 0},
+  // {"output", io_output,0},
+  // {"popen", io_popen,  0},
+  // {"read", io_read,    0},
+  // {"tmpfile", io_tmpfile, 0},
   {"type", io_type,    0},
-  {"write", io_write,  0},
+  // {"write", io_write,  0},
   {NULL, NULL,         0}
 };
 
@@ -749,26 +749,26 @@ static void createmeta (lua_State *L) {
 /*
 ** function to (not) close the standard files stdin, stdout, and stderr
 */
-static int io_noclose (lua_State *L) {
-  LStream *p = tolstream(L);
-  p->closef = &io_noclose;  /* keep file opened */
-  lua_pushnil(L);
-  lua_pushliteral(L, "cannot close standard file");
-  return 2;
-}
-
-
-static void createstdfile (lua_State *L, FILE *f, const char *k,
-                           const char *fname) {
-  LStream *p = newprefile(L);
-  p->f = f;
-  p->closef = &io_noclose;
-  if (k != NULL) {
-    lua_pushvalue(L, -1);
-    lua_setfield(L, LUA_REGISTRYINDEX, k);  /* add file to registry */
-  }
-  lua_setfield(L, -2, fname);  /* add file to module */
-}
+// static int io_noclose (lua_State *L) {
+//   LStream *p = tolstream(L);
+//   p->closef = &io_noclose;  /* keep file opened */
+//   lua_pushnil(L);
+//   lua_pushliteral(L, "cannot close standard file");
+//   return 2;
+// }
+
+
+// static void createstdfile (lua_State *L, FILE *f, const char *k,
+//                            const char *fname) {
+//   LStream *p = newprefile(L);
+//   p->f = f;
+//   p->closef = &io_noclose;
+//   if (k != NULL) {
+//     lua_pushvalue(L, -1);
+//     lua_setfield(L, LUA_REGISTRYINDEX, k);  /* add file to registry */
+//   }
+//   lua_setfield(L, -2, fname);  /* add file to module */
+// }
 
 
 LUAMOD_API int luaopen_io (lua_State *L) {

+ 46 - 46
lua/src/loslib.c

@@ -138,16 +138,16 @@ 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;
-  }
-}
+// 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;
+//   }
+// }
 
 
 static int os_remove (lua_State *L) {
@@ -163,21 +163,21 @@ static int os_rename (lua_State *L) {
 }
 
 
-static int os_tmpname (lua_State *L) {
-  char buff[LUA_TMPNAMBUFSIZE];
-  int err;
-  lua_tmpnam(buff, err);
-  if (err)
-    return luaL_error(L, "unable to generate a unique filename");
-  lua_pushstring(L, buff);
-  return 1;
-}
+// static int os_tmpname (lua_State *L) {
+//   char buff[LUA_TMPNAMBUFSIZE];
+//   int err;
+//   lua_tmpnam(buff, err);
+//   if (err)
+//     return luaL_error(L, "unable to generate a unique filename");
+//   lua_pushstring(L, buff);
+//   return 1;
+// }
 
 
-static int os_getenv (lua_State *L) {
-  lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
-  return 1;
-}
+// static int os_getenv (lua_State *L) {
+//   lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
+//   return 1;
+// }
 
 
 static int os_clock (lua_State *L) {
@@ -358,29 +358,29 @@ static int os_difftime (lua_State *L) {
 /* }====================================================== */
 
 
-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_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;
+// }
 
 #include "rotable.h"
 static const rotable_Reg syslib[] = {