bisondynlib-linux.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Linux-specific dynamic library manipulation routines
  3. */
  4. #include "bisondynlib.h"
  5. #include <stdio.h>
  6. #include <dlfcn.h>
  7. void *bisondynlib_open(char *filename)
  8. {
  9. void *handle;
  10. dlerror();
  11. handle = dlopen(filename, (RTLD_NOW|RTLD_GLOBAL));
  12. return handle;
  13. }
  14. int bisondynlib_close(void *handle)
  15. {
  16. return dlclose(handle);
  17. }
  18. char *bisondynlib_err()
  19. {
  20. return dlerror();
  21. }
  22. char *bisondynlib_lookup_hash(void *handle)
  23. {
  24. char **hash;
  25. dlerror();
  26. hash = dlsym(handle, "rules_hash");
  27. /*
  28. printf("bisondynlib_lookup_hash: hash=%s\n", *hash);
  29. */
  30. return *hash;
  31. }
  32. PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, int debug)
  33. {
  34. PyObject *(*pparser)(PyObject *, void *, void *, int);
  35. pparser = bisondynlib_lookup_parser(handle);
  36. if (!pparser) {
  37. PyErr_SetString(PyExc_RuntimeError,
  38. "bisondynlib_lookup_parser() returned NULL");
  39. return NULL;
  40. }
  41. (*pparser)(parser, cb, in, debug);
  42. // Do not ignore a raised exception, but pass the exception through.
  43. if (PyErr_Occurred())
  44. return NULL;
  45. Py_INCREF(Py_None);
  46. return Py_None;
  47. }
  48. /*
  49. * function(void *) returns a pointer to a function(PyObject *, char *) returning PyObject*
  50. */
  51. PyObject *(*bisondynlib_lookup_parser(void *handle))(PyObject *, void *, void *, int)
  52. {
  53. dlerror();
  54. return dlsym(handle, "do_parse");
  55. }
  56. /*
  57. * Runs the compiler commands which build the parser/lexer into a shared lib
  58. */
  59. /*
  60. int bisondynlib_build(char *libName, char *pyincdir)
  61. {
  62. char buf[1024];
  63. sprintf(buf, "gcc -fPIC -shared -I%s tmp.bison.c tmp.lex.c -o %s", pyincdir, libName);
  64. printf("Running linux build command: %s\n", buf);
  65. system(buf);
  66. return 0;
  67. }
  68. */