lua.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. ** lua.c
  3. ** Linguagem para Usuarios de Aplicacao
  4. */
  5. char *rcs_lua="$Id: lua.c,v 1.18 1997/06/19 18:55:40 roberto Exp roberto $";
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "lualoc.h"
  9. #include "lua.h"
  10. #include "auxlib.h"
  11. #include "lualib.h"
  12. #ifdef _POSIX_SOURCE
  13. #include <unistd.h>
  14. #else
  15. #define isatty(x) (x==0) /* assume stdin is a tty */
  16. #endif
  17. #define DEBUG 0
  18. static void testC (void)
  19. {
  20. #if DEBUG
  21. #define getnum(s) ((*s++) - '0')
  22. #define getname(s) (nome[0] = *s++, nome)
  23. static int locks[10];
  24. lua_Object reg[10];
  25. char nome[2];
  26. char *s = luaL_check_string(1);
  27. nome[1] = 0;
  28. while (1) {
  29. switch (*s++) {
  30. case '0': case '1': case '2': case '3': case '4':
  31. case '5': case '6': case '7': case '8': case '9':
  32. lua_pushnumber(*(s-1) - '0');
  33. break;
  34. case 'c': reg[getnum(s)] = lua_createtable(); break;
  35. case 'P': reg[getnum(s)] = lua_pop(); break;
  36. case 'g': { int n = getnum(s); reg[n] = lua_getglobal(getname(s)); break; }
  37. case 'G': { int n = getnum(s);
  38. reg[n] = lua_rawgetglobal(getname(s));
  39. break;
  40. }
  41. case 'l': locks[getnum(s)] = lua_ref(1); break;
  42. case 'L': locks[getnum(s)] = lua_ref(0); break;
  43. case 'r': { int n = getnum(s); reg[n] = lua_getref(locks[getnum(s)]); break; }
  44. case 'u': lua_unref(locks[getnum(s)]); break;
  45. case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
  46. case '=': lua_setglobal(getname(s)); break;
  47. case 's': lua_pushstring(getname(s)); break;
  48. case 'o': lua_pushobject(reg[getnum(s)]); break;
  49. case 'f': lua_call(getname(s)); break;
  50. case 'i': reg[getnum(s)] = lua_gettable(); break;
  51. case 'I': reg[getnum(s)] = lua_rawgettable(); break;
  52. case 't': lua_settable(); break;
  53. case 'T': lua_rawsettable(); break;
  54. default: luaL_verror("unknown command in `testC': %c", *(s-1));
  55. }
  56. if (*s == 0) return;
  57. if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
  58. }
  59. #else
  60. lua_error("`testC' not active");
  61. #endif
  62. }
  63. static void manual_input (void)
  64. {
  65. if (isatty(0)) {
  66. char buffer[250];
  67. while (fgets(buffer, sizeof(buffer), stdin) != 0) {
  68. lua_beginblock();
  69. lua_dostring(buffer);
  70. lua_endblock();
  71. }
  72. }
  73. else
  74. lua_dofile(NULL); /* executes stdin as a file */
  75. }
  76. int main (int argc, char *argv[])
  77. {
  78. int i;
  79. int result = 0;
  80. setlocale(LC_ALL, "");
  81. iolib_open ();
  82. strlib_open ();
  83. mathlib_open ();
  84. lua_register("testC", testC);
  85. if (argc < 2)
  86. manual_input();
  87. else for (i=1; i<argc; i++) {
  88. if (strcmp(argv[i], "-") == 0)
  89. manual_input();
  90. else if (strcmp(argv[i], "-v") == 0)
  91. printf("%s %s\n(written by %s)\n\n",
  92. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  93. else if ((strcmp(argv[i], "-e") == 0 && i++) || strchr(argv[i], '=')) {
  94. if (lua_dostring(argv[i]) != 0) {
  95. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  96. return 1;
  97. }
  98. }
  99. else {
  100. result = lua_dofile (argv[i]);
  101. if (result) {
  102. if (result == 2) {
  103. fprintf(stderr, "lua: cannot execute file ");
  104. perror(argv[i]);
  105. }
  106. return 1;
  107. }
  108. }
  109. }
  110. return result;
  111. }