3 * $Id: rxglue.c,v 1.2 2002/01/30 09:22:48 mdw Exp $
5 * REXX glue for C core functionality
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Jog: Programming for a jogging machine.
14 * Jog is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Jog is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Jog; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 2002/01/30 09:22:48 mdw
33 * Use memory-allocation functions provided by the REXX interpreter.
34 * Now that configuration can be applied after initialization, allow
35 * @txconf@ to set parameters. Make @txsend@ add a newline to its output,
38 * Revision 1.1 2002/01/25 19:34:45 mdw
43 /*----- Header files ------------------------------------------------------*/
58 #include <sys/types.h>
63 #define RX_STRONGTYPING
66 #include <mLib/alloc.h>
68 #include <mLib/dstr.h>
74 /*----- Static variables --------------------------------------------------*/
76 static txport *tx = 0;
78 /*----- Memory allocation functions ---------------------------------------*/
80 static void *rx_alloc(size_t sz)
82 void *p = RexxAllocateMemory(sz);
88 static void rx_free(void *p)
93 /*----- Conversion functions ----------------------------------------------*/
95 /* --- @rxs_putm@ --- *
97 * Arguments: @RXSTRING *x@ = pointer to REXX string structure
99 * @const void *p@ = pointer to data block
100 * @size_t sz@ = size of data
102 * @const dstr *d@ = pointer to source string
103 * For @rxs_putf@ and @rxs_vputf@:
104 * @const char *m@ = message format string
108 * Use: Stashes some text in an @RXSTRING@, overwriting whatever was
109 * there before. We assume that the previous contents don't
113 #define RXS_PUTM(x, p, sz) do { \
114 RXSTRING *_x = (x); \
115 const void *_p = (p); \
117 if (!_x->strptr || _x->strlength < _sz) \
118 _x->strptr = rx_alloc(_sz); \
119 memcpy(_x->strptr, _p, _sz); \
120 _x->strlength = _sz; \
123 static void rxs_putm(RXSTRING *x, const void *p, size_t sz)
128 #define RXS_PUTD(x, d) do { \
130 RXS_PUTM((x), _d->buf, _d->len); \
133 static void rxs_putd(RXSTRING *x, dstr *d) { RXS_PUTD(x, d); }
135 static void rxs_vputf(RXSTRING *x, const char *m, va_list *ap)
138 dstr_vputf(&d, m, ap);
143 static void rxs_putf(RXSTRING *x, const char *m, ...)
148 dstr_vputf(&d, m, &ap);
154 /* --- @rxs_get@ --- *
156 * Arguments: @const RXSTRING *x@ = pointer to a REXX string
157 * @dstr *d@ = where to put it
161 * Use: Pulls a REXX string out and puts it in a dynamic string.
164 #define RXS_GET(x, d) do { \
165 const RXSTRING *_x = (x); \
167 DPUTM(_dd, _x->strptr, _x->strlength); \
171 static void rxs_get(const RXSTRING *x, dstr *d) { RXS_GET(x, d); }
173 /* --- @rxs_tol@ --- *
175 * Arguments: @const RXSTRING *x@ = pointer to a REXX string
176 * @long *ii@ = where to put the answer
178 * Returns: Zero on success, or nonzero on error.
180 * Use: Fetches an integer from a REXX string. This doesn't cope
181 * with multiprecision integers or similar silliness.
184 static int rxs_tol(const RXSTRING *x, long *ii)
187 const char *p = x->strptr, *l = p + x->strlength;
193 #define MINR (LONG_MIN/10)
194 #define MIND (LONG_MIN%10)
196 while (p < l && isspace((unsigned char)*p))
202 else if (*p == '-') {
206 while (p < l && isspace((unsigned char)*p))
208 while (p < l && isdigit((unsigned char)*p)) {
210 if (i < MINR || (i == MINR && -j < MIND))
215 while (p < l && isspace((unsigned char)*p))
217 if (p < l || !(f & f_ok))
234 /* --- @rxs_block@ --- *
236 * Arguments: @const RXSTRING *x@ = a REXX string
237 * @unsigned long *t@ = where to put the block spec
239 * Returns: Zero if OK, nonzero on error.
241 * Use: Picks out a blockingness spec.
244 static int rxs_block(const RXSTRING *x, unsigned long *t)
248 if (!x->strptr || x->strlength < 1)
250 switch (x->strptr[0]) {
256 if (rxs_tol(x, &i) || i < 0)
264 /*----- REXX functions ----------------------------------------------------*/
266 static APIRET APIENTRY rxfn_test(const char *fn, ULONG ac, RXSTRING *av,
267 const char *sn, RXSTRING *r)
271 printf("test entry\n"
273 for (i = 0; i < ac; i++) {
276 printf(" av[%lu] = `", i);
277 fwrite(av[i].strptr, 1, av[i].strlength, stdout);
278 if (rxs_tol(&av[i], &l))
281 printf("' (%ld)\n", l);
283 printf("tx = `%s'; f = `%s'; c = `%s'.\n", txname, txfile, txconf);
284 rxs_putf(r, "function `%s' completed ok", fn);
288 /* --- @txname()@ ---
292 * Returns: The currently-selected transport name.
295 static APIRET APIENTRY rxfn_txname(const char *fn, ULONG ac, RXSTRING *av,
296 const char *sn, RXSTRING *r)
300 rxs_putf(r, "%s", txname);
304 /* --- @txfile()@ ---
308 * Returns: The currently-selected transport filename.
311 static APIRET APIENTRY rxfn_txfile(const char *fn, ULONG ac, RXSTRING *av,
312 const char *sn, RXSTRING *r)
316 rxs_putf(r, "%s", txfile ? txfile : "");
320 /* --- @txconf([CONFIG])@ ---
322 * Arguments: @CONFIG@ = optional string to set
324 * Returns: The currently-selected transport configuration string.
327 static APIRET APIENTRY rxfn_txconf(const char *fn, ULONG ac, RXSTRING *av,
328 const char *sn, RXSTRING *r)
332 if (ac > 0 && av[0].strptr) {
338 rc = tx_configure(tx, d.buf);
343 rxs_putf(r, "%s", txconf ? txconf : "");
347 /* --- @txinit([NAME], [FILE], [CONFIG])@ ---
349 * Arguments: @NAME@ = transport name to select
350 * @FILE@ = transport filename
351 * @CONFIG@ = transport configuration string
355 * Use: Initializes a transport using the given settings. Omitted
356 * arguments are filled in from the command line, or internal
360 static APIRET APIENTRY rxfn_txinit(const char *fn, ULONG ac, RXSTRING *av,
361 const char *sn, RXSTRING *r)
363 const char *n = txname, *f = txfile, *c = txconf;
364 dstr dn = DSTR_INIT, df = DSTR_INIT, dc = DSTR_INIT;
370 if (ac >= 1 && av[0].strptr) {
371 rxs_get(&av[0], &dn);
374 if (ac >= 2 && av[1].strptr) {
375 rxs_get(&av[1], &df);
378 if (ac >= 3 && av[2].strptr) {
379 rxs_get(&av[2], &dc);
382 tx = tx_create(n, f, c);
391 /* --- @txsend(STRING, [OPTION])@ --- *
393 * Arguments: @STRING@ = string to send
394 * @OPTION@ = `l' or `n' (for `linebreak' or `nolinebreak')
398 * Use: Sends a string (exactly as written) to the transport.
401 static APIRET APIENTRY rxfn_txsend(const char *fn, ULONG ac, RXSTRING *av,
402 const char *sn, RXSTRING *r)
404 if ((ac != 1 && ac != 2) || !tx || !av[0].strptr)
406 tx_write(tx, av[0].strptr, av[0].strlength);
407 if (ac == 1 || !av[1].strptr || !av[1].strlength ||
408 av[1].strptr[0] == 'l' || av[1].strptr[0] == 'L')
413 /* --- @txrecv([MILLIS])@ --- *
415 * Arguments: @MILLIS@ = how long (in milliseconds) to wait, or `forever'
417 * Returns: The string read (may be null if nothing available -- sorry).
419 * Use: Reads the next line from the transport. If @MILLIS@ is an
420 * integer, then give up after that many milliseconds of
421 * waiting; if it is `forever' (or anything beginning with an
422 * `f') then don't give up. The default is to wait forever.
425 static APIRET APIENTRY rxfn_txrecv(const char *fn, ULONG ac, RXSTRING *av,
426 const char *sn, RXSTRING *r)
429 unsigned long t = FOREVER;
433 if (ac >= 1 && rxs_block(&av[0], &t))
440 rxs_putm(r, l->s, l->len);
446 /* --- @TXEOF()@ --- *
450 * Returns: True if end-of-file has been seen on the transport, otherwise
454 static APIRET APIENTRY rxfn_txeof(const char *fn, ULONG ac, RXSTRING *av,
455 const char *sn, RXSTRING *r)
459 rxs_putf(r, "%d", tx->s == TX_CLOSED && !tx->ll);
463 /* --- @txready([MILLIS])@ --- *
465 * Arguments: @MILLIS@ = how long (in milliseconds) to wait, or `forever'
467 * Returns: True if a line is ready, otherwise false.
469 * Use: Returns whether the transport is ready for reading. If
470 * @MILLIS@ is an integer, then wait for at most that many
471 * milliseconds before returning. If @MILLIS@ is `forever' (or
472 * anything beginning with `f') then wait forever for
473 * readiness. This isn't useless: it can trip the end-of-file
474 * detector. If @MILLIS@ is omitted, return immediately (as if
475 * 0 had been specified).
478 static APIRET APIENTRY rxfn_txready(const char *fn, ULONG ac, RXSTRING *av,
479 const char *sn, RXSTRING *r)
485 if (ac >= 1 && rxs_block(&av[0], &t))
487 rxs_putf(r, "%d", !!tx_read(tx, t));
491 /* --- @MILLIWAIT(MILLIS)@ --- *
493 * Arguments: @MILLIS@ = how long (in milliseconds) to wait
497 * Use: Waits for @MILLIS@ milliseconds. Always.
500 static APIRET APIENTRY rxfn_milliwait(const char *fn, ULONG ac, RXSTRING *av,
501 const char *sn, RXSTRING *r)
506 if (ac != 1 || !av[0].strptr)
508 if (rxs_tol(&av[0], &l) || l < 0)
510 tv.tv_sec = l / 1000;
511 tv.tv_usec = (l % 1000) * 1000;
512 select(0, 0, 0, 0, &tv);
516 /*----- Initialization ----------------------------------------------------*/
518 struct rxfntab { char *name; RexxFunctionHandler *fn; };
520 static const struct rxfntab rxfntab[] = {
521 { "test", rxfn_test },
522 { "txname", rxfn_txname },
523 { "txfile", rxfn_txfile },
524 { "txconf", rxfn_txconf },
525 { "txinit", rxfn_txinit },
526 { "txsend", rxfn_txsend },
527 { "txrecv", rxfn_txrecv },
528 { "txeof", rxfn_txeof },
529 { "txready", rxfn_txready },
530 { "milliwait", rxfn_milliwait },
534 /* --- @rx_init@ --- *
540 * Use: Initializes the REXX external functions.
545 const struct rxfntab *f;
548 for (f = rxfntab; f->fn; f++) {
549 if ((rc = RexxRegisterFunctionExe(f->name, f->fn)) != 0) {
550 err_report(ERR_RXGLUE, ERRRX_INIT, rc,
551 "couldn't register function `%s' (code %d)", f->name, rc);
557 /*----- Running REXX programs ---------------------------------------------*/
559 /* --- @rx_run@ --- *
561 * Arguments: @const char *name@ = pointer to filename (or null)
562 * @const void *p@ = pointer to program text
563 * @size_t sz@ = size of program text
564 * @int ac@ = number of arguments
565 * @const char *const *av@ = vector of command-line arguments
567 * Returns: Exit code from program.
569 * Use: Runs a REXX script from memory.
572 int rx_run(const char *name, const void *p, size_t sz,
573 int ac, const char *const *av)
583 /* --- Set things up --- */
587 MAKERXSTRING(prog[0], (void *)p, sz);
588 MAKERXSTRING(prog[1], 0, 0);
589 argv = rx_alloc(ac * sizeof(*argv));
590 for (i = 0; i < ac; i++)
591 MAKERXSTRING(argv[i], (char *)av[i], strlen(av[i]));
593 /* --- Run the script --- */
595 MAKERXSTRING(res, 0, 0);
596 rc = RexxStart(ac, argv, name, prog,
597 "SYSTEM", RXSUBROUTINE, 0, &badrc, &res);
599 rx_free(RXSTRPTR(res));
602 err_report(ERR_RXERR, 0, -rc, "rexx error from script `%s'", name);
604 err_report(ERR_RXGLUE, ERRRX_INTERP, rc, "intepreter internal error");
608 /* --- Pick apart the results --- */
610 dstr_putm(&d, RXSTRPTR(res), RXSTRLEN(res));
611 rx_free(RXSTRPTR(res));
619 /* --- @rx_runfile@ --- *
621 * Arguments: @const char *name@ = pointer to filename
622 * @int ac@ = number of command-line arguments
623 * @const char *const *av@ = vector of command-line arguments
625 * Returns: Exit code from program.
627 * Use: Runs a REXX script from a file, given its name.
630 int rx_runfile(const char *name, int ac, const char *const *av)
638 /* --- Read the file into memory --- *
640 * This way avoids any crapness in the REXX implementation and means we can
641 * report errors in a more sensible way.
644 if ((fp = fopen(name, "r")) == 0)
647 n = fread(buf, 1, sizeof(buf), fp);
649 } while (n == sizeof(buf));
654 /* --- Now do the from-memory thing --- */
656 rc = rx_run(name, d.buf, d.len, ac, av);
660 /* --- Tidy up on errors --- */
666 err_report(ERR_RXGLUE, ERRRX_SCRIPTREAD, errno,
667 "couldn't read script `%s': %s", name, strerror(errno));
671 /*----- That's all, folks -------------------------------------------------*/