/script_binding/lua/lua_runner.c

http://ftk.googlecode.com/ · C · 73 lines · 34 code · 10 blank · 29 comment · 7 complexity · 7329258c4fd0f194de59c9b07e96fe16 MD5 · raw file

  1. /*
  2. * File: lua_runner.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: lua runner
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2009-11-22 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "lualib.h"
  31. #include "ftk_lua.h"
  32. #include "ftk_mmap.h"
  33. static void l_message (const char *pname, const char *msg) {
  34. if (pname) fprintf(stderr, "%s: ", pname);
  35. fprintf(stderr, "%s\n", msg);
  36. fflush(stderr);
  37. }
  38. static int report (lua_State *L, int status) {
  39. if (status && !lua_isnil(L, -1)) {
  40. const char *msg = lua_tostring(L, -1);
  41. if (msg == NULL) msg = "(error object is not a string)";
  42. l_message("ftk_run", msg);
  43. lua_pop(L, 1);
  44. }
  45. return status;
  46. }
  47. int FTK_MAIN(int argc, char* argv[])
  48. {
  49. int ret = 0;
  50. lua_State *L = NULL;
  51. if(argc != 2)
  52. {
  53. printf("Usage: %s lua\n", argv[0]);
  54. return 0;
  55. }
  56. L = lua_open();
  57. luaL_openlibs(L);
  58. ftk_lua_init(L);
  59. ret = luaL_dofile(L, argv[1]);
  60. report(L, ret);
  61. lua_close(L);
  62. return 0;
  63. }