lua.c 3.2 KB

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