3 * $Id: rxglue.c,v 1.3 2002/02/02 19:17:41 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.3 2002/02/02 19:17:41 mdw
33 * New audio subsystem.
35 * Revision 1.2 2002/01/30 09:22:48 mdw
36 * Use memory-allocation functions provided by the REXX interpreter.
37 * Now that configuration can be applied after initialization, allow
38 * @txconf@ to set parameters. Make @txsend@ add a newline to its output,
41 * Revision 1.1 2002/01/25 19:34:45 mdw
46 /*----- Header files ------------------------------------------------------*/
61 #include <sys/types.h>
66 #define RX_STRONGTYPING
69 #include <mLib/alloc.h>
71 #include <mLib/dstr.h>
79 /*----- Static variables --------------------------------------------------*/
81 static txport *tx = 0;
83 /*----- Memory allocation functions ---------------------------------------*/
85 static void *rx_alloc(size_t sz)
87 void *p = RexxAllocateMemory(sz);
93 static void rx_free(void *p)
98 /*----- Conversion functions ----------------------------------------------*/
100 /* --- @rxs_putm@ --- *
102 * Arguments: @RXSTRING *x@ = pointer to REXX string structure
104 * @const void *p@ = pointer to data block
105 * @size_t sz@ = size of data
107 * @const dstr *d@ = pointer to source string
108 * For @rxs_putf@ and @rxs_vputf@:
109 * @const char *m@ = message format string
113 * Use: Stashes some text in an @RXSTRING@, overwriting whatever was
114 * there before. We assume that the previous contents don't
118 #define RXS_PUTM(x, p, sz) do { \
119 RXSTRING *_x = (x); \
120 const void *_p = (p); \
122 if (!_x->strptr || _x->strlength < _sz) \
123 _x->strptr = rx_alloc(_sz); \
124 memcpy(_x->strptr, _p, _sz); \
125 _x->strlength = _sz; \
128 static void rxs_putm(RXSTRING *x, const void *p, size_t sz)
133 #define RXS_PUTD(x, d) do { \
135 RXS_PUTM((x), _d->buf, _d->len); \
138 static void rxs_putd(RXSTRING *x, dstr *d) { RXS_PUTD(x, d); }
140 static void rxs_vputf(RXSTRING *x, const char *m, va_list *ap)
143 dstr_vputf(&d, m, ap);
148 static void rxs_putf(RXSTRING *x, const char *m, ...)
153 dstr_vputf(&d, m, &ap);
159 /* --- @rxs_get@ --- *
161 * Arguments: @const RXSTRING *x@ = pointer to a REXX string
162 * @dstr *d@ = where to put it
166 * Use: Pulls a REXX string out and puts it in a dynamic string.
169 #define RXS_GET(x, d) do { \
170 const RXSTRING *_x = (x); \
172 DPUTM(_dd, _x->strptr, _x->strlength); \
176 static void rxs_get(const RXSTRING *x, dstr *d) { RXS_GET(x, d); }
178 /* --- @rxs_tol@ --- *
180 * Arguments: @const RXSTRING *x@ = pointer to a REXX string
181 * @long *ii@ = where to put the answer
183 * Returns: Zero on success, or nonzero on error.
185 * Use: Fetches an integer from a REXX string. This doesn't cope
186 * with multiprecision integers or similar silliness.
189 static int rxs_tol(const RXSTRING *x, long *ii)
192 const char *p = x->strptr, *l = p + x->strlength;
198 #define MINR (LONG_MIN/10)
199 #define MIND (LONG_MIN%10)
201 while (p < l && isspace((unsigned char)*p))
207 else if (*p == '-') {
211 while (p < l && isspace((unsigned char)*p))
213 while (p < l && isdigit((unsigned char)*p)) {
215 if (i < MINR || (i == MINR && -j < MIND))
220 while (p < l && isspace((unsigned char)*p))
222 if (p < l || !(f & f_ok))
239 /* --- @rxs_block@ --- *
241 * Arguments: @const RXSTRING *x@ = a REXX string
242 * @unsigned long *t@ = where to put the block spec
244 * Returns: Zero if OK, nonzero on error.
246 * Use: Picks out a blockingness spec.
249 static int rxs_block(const RXSTRING *x, unsigned long *t)
253 if (!x->strptr || x->strlength < 1)
255 switch (x->strptr[0]) {
261 if (rxs_tol(x, &i) || i < 0)
269 /*----- REXX functions ----------------------------------------------------*/
271 static APIRET APIENTRY rxfn_test(const char *fn, ULONG ac, RXSTRING *av,
272 const char *sn, RXSTRING *r)
276 printf("test entry\n"
278 for (i = 0; i < ac; i++) {
281 printf(" av[%lu] = `", i);
282 fwrite(av[i].strptr, 1, av[i].strlength, stdout);
283 if (rxs_tol(&av[i], &l))
286 printf("' (%ld)\n", l);
288 printf("tx = `%s'; f = `%s'; c = `%s'.\n", txname, txfile, txconf);
289 rxs_putf(r, "function `%s' completed ok", fn);
293 /* --- @txname()@ ---
297 * Returns: The currently-selected transport name.
300 static APIRET APIENTRY rxfn_txname(const char *fn, ULONG ac, RXSTRING *av,
301 const char *sn, RXSTRING *r)
305 rxs_putf(r, "%s", txname);
309 /* --- @txfile()@ ---
313 * Returns: The currently-selected transport filename.
316 static APIRET APIENTRY rxfn_txfile(const char *fn, ULONG ac, RXSTRING *av,
317 const char *sn, RXSTRING *r)
321 rxs_putf(r, "%s", txfile ? txfile : "");
325 /* --- @txconf([CONFIG])@ ---
327 * Arguments: @CONFIG@ = optional string to set
329 * Returns: The currently-selected transport configuration string.
332 static APIRET APIENTRY rxfn_txconf(const char *fn, ULONG ac, RXSTRING *av,
333 const char *sn, RXSTRING *r)
337 if (ac > 0 && av[0].strptr) {
343 rc = tx_configure(tx, d.buf);
348 rxs_putf(r, "%s", txconf ? txconf : "");
352 /* --- @txinit([NAME], [FILE], [CONFIG])@ ---
354 * Arguments: @NAME@ = transport name to select
355 * @FILE@ = transport filename
356 * @CONFIG@ = transport configuration string
360 * Use: Initializes a transport using the given settings. Omitted
361 * arguments are filled in from the command line, or internal
365 static APIRET APIENTRY rxfn_txinit(const char *fn, ULONG ac, RXSTRING *av,
366 const char *sn, RXSTRING *r)
368 const char *n = txname, *f = txfile, *c = txconf;
369 dstr dn = DSTR_INIT, df = DSTR_INIT, dc = DSTR_INIT;
375 if (ac >= 1 && av[0].strptr) {
376 rxs_get(&av[0], &dn);
379 if (ac >= 2 && av[1].strptr) {
380 rxs_get(&av[1], &df);
383 if (ac >= 3 && av[2].strptr) {
384 rxs_get(&av[2], &dc);
387 tx = tx_create(n, f, c);
396 /* --- @txsend(STRING, [OPTION])@ --- *
398 * Arguments: @STRING@ = string to send
399 * @OPTION@ = `l' or `n' (for `linebreak' or `nolinebreak')
403 * Use: Sends a string (exactly as written) to the transport.
406 static APIRET APIENTRY rxfn_txsend(const char *fn, ULONG ac, RXSTRING *av,
407 const char *sn, RXSTRING *r)
409 if ((ac != 1 && ac != 2) || !tx || !av[0].strptr)
411 tx_write(tx, av[0].strptr, av[0].strlength);
412 if (ac == 1 || !av[1].strptr || !av[1].strlength ||
413 av[1].strptr[0] == 'l' || av[1].strptr[0] == 'L')
418 /* --- @txrecv([MILLIS])@ --- *
420 * Arguments: @MILLIS@ = how long (in milliseconds) to wait, or `forever'
422 * Returns: The string read (may be null if nothing available -- sorry).
424 * Use: Reads the next line from the transport. If @MILLIS@ is an
425 * integer, then give up after that many milliseconds of
426 * waiting; if it is `forever' (or anything beginning with an
427 * `f') then don't give up. The default is to wait forever.
430 static APIRET APIENTRY rxfn_txrecv(const char *fn, ULONG ac, RXSTRING *av,
431 const char *sn, RXSTRING *r)
434 unsigned long t = FOREVER;
438 if (ac >= 1 && rxs_block(&av[0], &t))
445 rxs_putm(r, l->s, l->len);
451 /* --- @TXEOF()@ --- *
455 * Returns: True if end-of-file has been seen on the transport, otherwise
459 static APIRET APIENTRY rxfn_txeof(const char *fn, ULONG ac, RXSTRING *av,
460 const char *sn, RXSTRING *r)
464 rxs_putf(r, "%d", tx->s == TX_CLOSED && !tx->ll);
468 /* --- @txready([MILLIS])@ --- *
470 * Arguments: @MILLIS@ = how long (in milliseconds) to wait, or `forever'
472 * Returns: True if a line is ready, otherwise false.
474 * Use: Returns whether the transport is ready for reading. If
475 * @MILLIS@ is an integer, then wait for at most that many
476 * milliseconds before returning. If @MILLIS@ is `forever' (or
477 * anything beginning with `f') then wait forever for
478 * readiness. This isn't useless: it can trip the end-of-file
479 * detector. If @MILLIS@ is omitted, return immediately (as if
480 * 0 had been specified).
483 static APIRET APIENTRY rxfn_txready(const char *fn, ULONG ac, RXSTRING *av,
484 const char *sn, RXSTRING *r)
490 if (ac >= 1 && rxs_block(&av[0], &t))
492 rxs_putf(r, "%d", !!tx_read(tx, t));
496 /* --- @AUPLAY(TAG, [FLAG])@ --- *
498 * Arguments: @TAG@ = audio sample tag to play
499 * @FLAG@ = a string to explain what to do more clearly.
501 * Returns: True if it succeeded.
503 * Use: Plays a sample. If @FLAG@ begins with `t', don't report
504 * errors if the sample can't be found.
507 static APIRET APIENTRY rxfn_auplay(const char *fn, ULONG ac, RXSTRING *av,
508 const char *sn, RXSTRING *r)
513 if (ac < 1 || !av[0].strlength || ac > 2)
516 if (ac > 1 && av[1].strlength >= 1 &&
517 (av[1].strptr[0] == 't' || av[1].strptr[0] == 'T'))
518 rc = au_tryplay(d.buf);
522 rxs_putf(r, "%d", rc);
526 /* --- @AUFETCH(TAG)@ --- *
528 * Arguments: @TAG@ = audio sample tag to play
530 * Returns: True if it succeeded.
532 * Use: Prefetches a sample into the cache.
535 static APIRET APIENTRY rxfn_aufetch(const char *fn, ULONG ac, RXSTRING *av,
536 const char *sn, RXSTRING *r)
543 if (ac < 1 || !av[0].strlength || ac > 1)
546 if ((s = au_find(d.buf)) != 0 &&
547 (a = au_fetch(s)) != 0) {
552 rxs_putf(r, "%d", rc);
556 /* --- @AUNUM(TAG)@ --- *
558 * Arguments: @NUM@ = a number to be read
562 * Use: Reads a number aloud to the audio device.
565 static APIRET APIENTRY rxfn_aunum(const char *fn, ULONG ac, RXSTRING *av,
566 const char *sn, RXSTRING *r)
570 if (ac < 1 || !av[0].strlength || ac > 1)
578 /* --- @MILLIWAIT(MILLIS)@ --- *
580 * Arguments: @MILLIS@ = how long (in milliseconds) to wait
584 * Use: Waits for @MILLIS@ milliseconds. Always.
587 static APIRET APIENTRY rxfn_milliwait(const char *fn, ULONG ac, RXSTRING *av,
588 const char *sn, RXSTRING *r)
593 if (ac != 1 || !av[0].strptr)
595 if (rxs_tol(&av[0], &l) || l < 0)
597 tv.tv_sec = l / 1000;
598 tv.tv_usec = (l % 1000) * 1000;
599 select(0, 0, 0, 0, &tv);
603 /*----- Initialization ----------------------------------------------------*/
605 struct rxfntab { char *name; RexxFunctionHandler *fn; };
607 static const struct rxfntab rxfntab[] = {
608 { "test", rxfn_test },
609 { "txname", rxfn_txname },
610 { "txfile", rxfn_txfile },
611 { "txconf", rxfn_txconf },
612 { "txinit", rxfn_txinit },
613 { "txsend", rxfn_txsend },
614 { "txrecv", rxfn_txrecv },
615 { "txeof", rxfn_txeof },
616 { "txready", rxfn_txready },
617 { "auplay", rxfn_auplay },
618 { "aufetch", rxfn_aufetch },
619 { "aunum", rxfn_aunum },
620 { "milliwait", rxfn_milliwait },
624 /* --- @rx_init@ --- *
630 * Use: Initializes the REXX external functions.
635 const struct rxfntab *f;
638 for (f = rxfntab; f->fn; f++) {
639 if ((rc = RexxRegisterFunctionExe(f->name, f->fn)) != 0) {
640 err_report(ERR_RXGLUE, ERRRX_INIT, rc,
641 "couldn't register function `%s' (code %d)", f->name, rc);
647 /*----- Running REXX programs ---------------------------------------------*/
649 /* --- @rx_run@ --- *
651 * Arguments: @const char *name@ = pointer to filename (or null)
652 * @const void *p@ = pointer to program text
653 * @size_t sz@ = size of program text
654 * @int ac@ = number of arguments
655 * @const char *const *av@ = vector of command-line arguments
657 * Returns: Exit code from program.
659 * Use: Runs a REXX script from memory.
662 int rx_run(const char *name, const void *p, size_t sz,
663 int ac, const char *const *av)
673 /* --- Set things up --- */
677 MAKERXSTRING(prog[0], (void *)p, sz);
678 MAKERXSTRING(prog[1], 0, 0);
679 argv = rx_alloc(ac * sizeof(*argv));
680 for (i = 0; i < ac; i++)
681 MAKERXSTRING(argv[i], (char *)av[i], strlen(av[i]));
683 /* --- Run the script --- */
685 MAKERXSTRING(res, 0, 0);
686 rc = RexxStart(ac, argv, name, prog,
687 "SYSTEM", RXSUBROUTINE, 0, &badrc, &res);
689 rx_free(RXSTRPTR(res));
692 err_report(ERR_RXERR, 0, -rc, "rexx error from script `%s'", name);
694 err_report(ERR_RXGLUE, ERRRX_INTERP, rc, "intepreter internal error");
698 /* --- Pick apart the results --- */
700 dstr_putm(&d, RXSTRPTR(res), RXSTRLEN(res));
701 rx_free(RXSTRPTR(res));
709 /* --- @rx_runfile@ --- *
711 * Arguments: @const char *name@ = pointer to filename
712 * @int ac@ = number of command-line arguments
713 * @const char *const *av@ = vector of command-line arguments
715 * Returns: Exit code from program.
717 * Use: Runs a REXX script from a file, given its name.
720 int rx_runfile(const char *name, int ac, const char *const *av)
728 /* --- Read the file into memory --- *
730 * This way avoids any crapness in the REXX implementation and means we can
731 * report errors in a more sensible way.
734 if ((fp = fopen(name, "r")) == 0)
737 n = fread(buf, 1, sizeof(buf), fp);
739 } while (n == sizeof(buf));
744 /* --- Now do the from-memory thing --- */
746 rc = rx_run(name, d.buf, d.len, ac, av);
750 /* --- Tidy up on errors --- */
756 err_report(ERR_RXGLUE, ERRRX_SCRIPTREAD, errno,
757 "couldn't read script `%s': %s", name, strerror(errno));
761 /*----- That's all, folks -------------------------------------------------*/