procl.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. I have just manufactured a version of CSL that is (perhaps) easy to call
  2. from other C code. What follows is the main chunk of code that shows
  3. what the interface is. What this means wrt REDUCE is that I will want to
  4. manufacture an alternative to (BEGIN) that reads-simplifies-prints just
  5. one REDUCE expression/command at a time rather than the current single
  6. function that gobbles things for ever....
  7. /* Signature: 38b248eb 26-Apr-1996 */
  8. /*
  9. * The next fragment of code is to help with the use of CSL (and hence
  10. * packages written in Lisp and supported under CSL) as OEM products
  11. * embedded within larger C-coded packages. There is (of course) a
  12. * significant issue about clashes between the names of external symbols
  13. * if CSL is to be linked with anything else, but I will not worry about that
  14. * just yet.
  15. * The protocol for calling Lisp code from C is as follows:
  16. *
  17. * cslstart(argc, argv, writer);allocate memory and Lisp heap etc. Args
  18. * should be "as if" CSL was being called
  19. * directly and this was the main entrypoint.
  20. * The extra arg accepts output from this
  21. * stage. Use NULL to get standard I/O.
  22. * execute_lisp_function(fname, reader, writer);
  23. * fname is a (C) string that names a Lisp
  24. * function of 0 args. This is called with
  25. * stdin/stdout access redirected to use the
  26. * two character-at-a-time functions passed
  27. * down. [Value returned indicates if
  28. * the evaluation succeeded?]
  29. * cslfinish(writer); Tidies up ready to stop.
  30. */
  31. int execute_lisp_function(char *fname,
  32. character_reader *r, character_writer *w)
  33. {
  34. Lisp_Object nil;
  35. Lisp_Object ff = make_undefined_symbol(fname);
  36. nil = C_nil;
  37. if (exception_pending()) return 1; /* Failed to make the symbol */
  38. procedural_input = r;
  39. procedural_output = w;
  40. Lapply0(nil, ff);
  41. procedural_input = NULL;
  42. procedural_output = NULL;
  43. nil = C_nil;
  44. if (exception_pending()) return 2; /* Failure during evaluation */
  45. return 0;
  46. }
  47. #ifdef SAMPLE_OF_PROCEDURAL_INTERFACE
  48. static char ibuff[100], obuff[100];
  49. static int ibufp = 0, obufp = 0;
  50. static int iget()
  51. {
  52. int c = ibuff[ibufp++];
  53. if (c == 0) return EOF;
  54. else return c;
  55. }
  56. static void iput(int c)
  57. {
  58. if (obufp < sizeof(obuff)-1)
  59. { obuff[obufp++] = c;
  60. obuff[obufp] = 0;
  61. }
  62. }
  63. #endif
  64. int MS_CDECL main(int argc, char *argv[])
  65. {
  66. cslstart(argc, argv, NULL);
  67. #ifdef SAMPLE_OF_PROCEDURAL_INTERFACE
  68. strcpy(ibuff, "(print '(a b c d))");
  69. execute_lisp_function("oem-supervisor", iget, iput);
  70. printf("Buffered output is <%s>\n", obuff);
  71. #else
  72. cslaction();
  73. #endif
  74. my_exit(cslfinish(NULL));
  75. return 0;
  76. }
  77. ...............................end