3 * $Id: rxglue.c,v 1.4 2002/02/02 22:43:50 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 /*----- Header files ------------------------------------------------------*/
44 #include <sys/types.h>
49 #define RX_STRONGTYPING
52 #include <mLib/alloc.h>
54 #include <mLib/dstr.h>
62 /*----- Static variables --------------------------------------------------*/
64 static txport *tx = 0;
66 /*----- Memory allocation functions ---------------------------------------*/
68 static void *rx_alloc(size_t sz)
70 void *p = RexxAllocateMemory(sz);
76 static void rx_free(void *p)
81 /*----- Conversion functions ----------------------------------------------*/
83 /* --- @rxs_putm@ --- *
85 * Arguments: @RXSTRING *x@ = pointer to REXX string structure
87 * @const void *p@ = pointer to data block
88 * @size_t sz@ = size of data
90 * @const dstr *d@ = pointer to source string
91 * For @rxs_putf@ and @rxs_vputf@:
92 * @const char *m@ = message format string
96 * Use: Stashes some text in an @RXSTRING@, overwriting whatever was
97 * there before. We assume that the previous contents don't
101 #define RXS_PUTM(x, p, sz) do { \
102 RXSTRING *_x = (x); \
103 const void *_p = (p); \
105 if (!_x->strptr || _x->strlength < _sz) \
106 _x->strptr = rx_alloc(_sz); \
107 memcpy(_x->strptr, _p, _sz); \
108 _x->strlength = _sz; \
111 static void rxs_putm(RXSTRING *x, const void *p, size_t sz)
116 #define RXS_PUTD(x, d) do { \
118 RXS_PUTM((x), _d->buf, _d->len); \
121 static void rxs_putd(RXSTRING *x, dstr *d) { RXS_PUTD(x, d); }
123 static void rxs_vputf(RXSTRING *x, const char *m, va_list *ap)
126 dstr_vputf(&d, m, ap);
131 static void rxs_putf(RXSTRING *x, const char *m, ...)
136 dstr_vputf(&d, m, &ap);
142 /* --- @rxs_get@ --- *
144 * Arguments: @const RXSTRING *x@ = pointer to a REXX string
145 * @dstr *d@ = where to put it
149 * Use: Pulls a REXX string out and puts it in a dynamic string.
152 #define RXS_GET(x, d) do { \
153 const RXSTRING *_x = (x); \
155 DPUTM(_dd, _x->strptr, _x->strlength); \
159 static void rxs_get(const RXSTRING *x, dstr *d) { RXS_GET(x, d); }
161 /* --- @rxs_tol@ --- *
163 * Arguments: @const RXSTRING *x@ = pointer to a REXX string
164 * @long *ii@ = where to put the answer
166 * Returns: Zero on success, or nonzero on error.
168 * Use: Fetches an integer from a REXX string. This doesn't cope
169 * with multiprecision integers or similar silliness.
172 static int rxs_tol(const RXSTRING *x, long *ii)
175 const char *p = x->strptr, *l = p + x->strlength;
181 #define MINR (LONG_MIN/10)
182 #define MIND (LONG_MIN%10)
184 while (p < l && isspace((unsigned char)*p))
190 else if (*p == '-') {
194 while (p < l && isspace((unsigned char)*p))
196 while (p < l && isdigit((unsigned char)*p)) {
198 if (i < MINR || (i == MINR && -j < MIND))
203 while (p < l && isspace((unsigned char)*p))
205 if (p < l || !(f & f_ok))
222 /* --- @rxs_block@ --- *
224 * Arguments: @const RXSTRING *x@ = a REXX string
225 * @unsigned long *t@ = where to put the block spec
227 * Returns: Zero if OK, nonzero on error.
229 * Use: Picks out a blockingness spec.
232 static int rxs_block(const RXSTRING *x, unsigned long *t)
236 if (!x->strptr || x->strlength < 1)
238 switch (x->strptr[0]) {
244 if (rxs_tol(x, &i) || i < 0)
252 /*----- REXX functions ----------------------------------------------------*/
254 static APIRET APIENTRY rxfn_test(const char *fn, ULONG ac, RXSTRING *av,
255 const char *sn, RXSTRING *r)
259 printf("test entry\n"
261 for (i = 0; i < ac; i++) {
264 printf(" av[%lu] = `", i);
265 fwrite(av[i].strptr, 1, av[i].strlength, stdout);
266 if (rxs_tol(&av[i], &l))
269 printf("' (%ld)\n", l);
271 printf("tx = `%s'; f = `%s'; c = `%s'.\n", txname, txfile, txconf);
272 rxs_putf(r, "function `%s' completed ok", fn);
276 /* --- @txname()@ ---
280 * Returns: The currently-selected transport name.
283 static APIRET APIENTRY rxfn_txname(const char *fn, ULONG ac, RXSTRING *av,
284 const char *sn, RXSTRING *r)
288 rxs_putf(r, "%s", txname);
292 /* --- @txfile()@ ---
296 * Returns: The currently-selected transport filename.
299 static APIRET APIENTRY rxfn_txfile(const char *fn, ULONG ac, RXSTRING *av,
300 const char *sn, RXSTRING *r)
304 rxs_putf(r, "%s", txfile ? txfile : "");
308 /* --- @txconf([CONFIG])@ ---
310 * Arguments: @CONFIG@ = optional string to set
312 * Returns: The currently-selected transport configuration string.
315 static APIRET APIENTRY rxfn_txconf(const char *fn, ULONG ac, RXSTRING *av,
316 const char *sn, RXSTRING *r)
320 if (ac > 0 && av[0].strptr) {
326 rc = tx_configure(tx, d.buf);
331 rxs_putf(r, "%s", txconf ? txconf : "");
335 /* --- @txinit([NAME], [FILE], [CONFIG])@ ---
337 * Arguments: @NAME@ = transport name to select
338 * @FILE@ = transport filename
339 * @CONFIG@ = transport configuration string
343 * Use: Initializes a transport using the given settings. Omitted
344 * arguments are filled in from the command line, or internal
348 static APIRET APIENTRY rxfn_txinit(const char *fn, ULONG ac, RXSTRING *av,
349 const char *sn, RXSTRING *r)
351 const char *n = txname, *f = txfile, *c = txconf;
352 dstr dn = DSTR_INIT, df = DSTR_INIT, dc = DSTR_INIT;
358 if (ac >= 1 && av[0].strptr) {
359 rxs_get(&av[0], &dn);
362 if (ac >= 2 && av[1].strptr) {
363 rxs_get(&av[1], &df);
366 if (ac >= 3 && av[2].strptr) {
367 rxs_get(&av[2], &dc);
370 tx = tx_create(n, f, c);
379 /* --- @txsend(STRING, [OPTION])@ --- *
381 * Arguments: @STRING@ = string to send
382 * @OPTION@ = `l' or `n' (for `linebreak' or `nolinebreak')
386 * Use: Sends a string (exactly as written) to the transport.
389 static APIRET APIENTRY rxfn_txsend(const char *fn, ULONG ac, RXSTRING *av,
390 const char *sn, RXSTRING *r)
392 if ((ac != 1 && ac != 2) || !tx || !av[0].strptr)
394 tx_write(tx, av[0].strptr, av[0].strlength);
395 if (ac == 1 || !av[1].strptr || !av[1].strlength ||
396 av[1].strptr[0] == 'l' || av[1].strptr[0] == 'L')
401 /* --- @txrecv([MILLIS])@ --- *
403 * Arguments: @MILLIS@ = how long (in milliseconds) to wait, or `forever'
405 * Returns: The string read (may be null if nothing available -- sorry).
407 * Use: Reads the next line from the transport. If @MILLIS@ is an
408 * integer, then give up after that many milliseconds of
409 * waiting; if it is `forever' (or anything beginning with an
410 * `f') then don't give up. The default is to wait forever.
413 static APIRET APIENTRY rxfn_txrecv(const char *fn, ULONG ac, RXSTRING *av,
414 const char *sn, RXSTRING *r)
417 unsigned long t = FOREVER;
421 if (ac >= 1 && rxs_block(&av[0], &t))
428 rxs_putm(r, l->s, l->len);
434 /* --- @TXEOF()@ --- *
438 * Returns: True if end-of-file has been seen on the transport, otherwise
442 static APIRET APIENTRY rxfn_txeof(const char *fn, ULONG ac, RXSTRING *av,
443 const char *sn, RXSTRING *r)
447 rxs_putf(r, "%d", tx->s == TX_CLOSED && !tx->ll);
451 /* --- @txready([MILLIS])@ --- *
453 * Arguments: @MILLIS@ = how long (in milliseconds) to wait, or `forever'
455 * Returns: True if a line is ready, otherwise false.
457 * Use: Returns whether the transport is ready for reading. If
458 * @MILLIS@ is an integer, then wait for at most that many
459 * milliseconds before returning. If @MILLIS@ is `forever' (or
460 * anything beginning with `f') then wait forever for
461 * readiness. This isn't useless: it can trip the end-of-file
462 * detector. If @MILLIS@ is omitted, return immediately (as if
463 * 0 had been specified).
466 static APIRET APIENTRY rxfn_txready(const char *fn, ULONG ac, RXSTRING *av,
467 const char *sn, RXSTRING *r)
473 if (ac >= 1 && rxs_block(&av[0], &t))
475 rxs_putf(r, "%d", !!tx_read(tx, t));
479 /* --- @AUPLAY(TAG, [FLAG])@ --- *
481 * Arguments: @TAG@ = audio sample tag to play
482 * @FLAG@ = a string to explain what to do more clearly.
484 * Returns: True if it succeeded.
486 * Use: Plays a sample. If @FLAG@ begins with `t', don't report
487 * errors if the sample can't be found.
490 static APIRET APIENTRY rxfn_auplay(const char *fn, ULONG ac, RXSTRING *av,
491 const char *sn, RXSTRING *r)
496 if (ac < 1 || !av[0].strlength || ac > 2)
499 if (ac > 1 && av[1].strlength >= 1 &&
500 (av[1].strptr[0] == 't' || av[1].strptr[0] == 'T'))
501 rc = au_tryplay(d.buf);
505 rxs_putf(r, "%d", rc);
509 /* --- @AUFETCH(TAG)@ --- *
511 * Arguments: @TAG@ = audio sample tag to play
513 * Returns: True if it succeeded.
515 * Use: Prefetches a sample into the cache.
518 static APIRET APIENTRY rxfn_aufetch(const char *fn, ULONG ac, RXSTRING *av,
519 const char *sn, RXSTRING *r)
526 if (ac < 1 || !av[0].strlength || ac > 1)
529 if ((s = au_find(d.buf)) != 0 &&
530 (a = au_fetch(s)) != 0) {
535 rxs_putf(r, "%d", rc);
539 /* --- @AUNUM(TAG)@ --- *
541 * Arguments: @NUM@ = a number to be read
545 * Use: Reads a number aloud to the audio device.
548 static APIRET APIENTRY rxfn_aunum(const char *fn, ULONG ac, RXSTRING *av,
549 const char *sn, RXSTRING *r)
553 if (ac < 1 || !av[0].strlength || ac > 1)
561 /* --- @AUCACHE([FLAG], [VALUE, ...]@ --- *
563 * Arguments: @FLAG@ = operation to perform
565 * Returns: Dependent on operation.
567 * Use: If @FLAG@ is omitted or `Info', returns audio cache usage
568 * information as words in the following order:
570 * sz_max Maximum allowed cache size
571 * sz_total Total size used by samples
572 * sz_spare Size used by `spare' samples
573 * sz_queue Size used by queued samples
574 * n_total Total number of cached samples
575 * n_spare Number of `spare' samples
576 * n_queue Number of queued samples
577 * hits Number of cache hits
578 * misses Number of cache misses
580 * If @FLAG@ is `Max', sets the maximum cache size to the first
581 * @VALUE@ (if set), and returns the old maximum on its own.
583 * If @FLAG@ is `Usage', returns the `sz_*' items, as a list of
586 * If @FLAGS@ is `Numbers', returns the `n_*' items, as a list
589 * If @FLAGS@ is `Hits', returns `hits' and `misses' as a pair
593 static APIRET APIENTRY rxfn_aucache(const char *fn, ULONG ac, RXSTRING *av,
594 const char *sn, RXSTRING *r)
600 if (ac < 1 || !av[0].strlength)
602 switch (av[0].strptr[0]) {
603 case 'i': case 'I': info:
604 rxs_putf(r, "%lu %lu %lu %lu %u %u %u %lu %lu",
605 (unsigned long)c.sz_max, (unsigned long)c.sz_total,
606 (unsigned long)c.sz_spare, (unsigned long)c.sz_queue,
607 c.n_total, c.n_spare, c.n_total, c.hits, c.misses);
612 if (rxs_tol(&av[i], &max))
614 au_setcachelimit(max);
617 rxs_putf(r, "%lu", (unsigned long)c.sz_max);
620 rxs_putf(r, "%lu %lu %lu %lu",
621 (unsigned long)c.sz_max, (unsigned long)c.sz_total,
622 (unsigned long)c.sz_spare, (unsigned long)c.sz_queue);
625 rxs_putf(r, "%u %u %u", c.n_total, c.n_spare, c.n_total);
628 rxs_putf(r, "%lu %lu", c.hits, c.misses);
638 /* --- @MILLIWAIT(MILLIS)@ --- *
640 * Arguments: @MILLIS@ = how long (in milliseconds) to wait
644 * Use: Waits for @MILLIS@ milliseconds. Always.
647 static APIRET APIENTRY rxfn_milliwait(const char *fn, ULONG ac, RXSTRING *av,
648 const char *sn, RXSTRING *r)
653 if (ac != 1 || !av[0].strptr)
655 if (rxs_tol(&av[0], &l) || l < 0)
657 tv.tv_sec = l / 1000;
658 tv.tv_usec = (l % 1000) * 1000;
659 select(0, 0, 0, 0, &tv);
663 /*----- Initialization ----------------------------------------------------*/
665 struct rxfntab { char *name; RexxFunctionHandler *fn; };
667 static const struct rxfntab rxfntab[] = {
668 { "test", rxfn_test },
669 { "txname", rxfn_txname },
670 { "txfile", rxfn_txfile },
671 { "txconf", rxfn_txconf },
672 { "txinit", rxfn_txinit },
673 { "txsend", rxfn_txsend },
674 { "txrecv", rxfn_txrecv },
675 { "txeof", rxfn_txeof },
676 { "txready", rxfn_txready },
677 { "auplay", rxfn_auplay },
678 { "aufetch", rxfn_aufetch },
679 { "aucache", rxfn_aucache },
680 { "aunum", rxfn_aunum },
681 { "milliwait", rxfn_milliwait },
685 /* --- @rx_init@ --- *
691 * Use: Initializes the REXX external functions.
696 const struct rxfntab *f;
699 for (f = rxfntab; f->fn; f++) {
700 if ((rc = RexxRegisterFunctionExe(f->name, f->fn)) != 0) {
701 err_report(ERR_RXGLUE, ERRRX_INIT, rc,
702 "couldn't register function `%s' (code %d)", f->name, rc);
708 /*----- Running REXX programs ---------------------------------------------*/
710 /* --- @rx_run@ --- *
712 * Arguments: @const char *name@ = pointer to filename (or null)
713 * @const void *p@ = pointer to program text
714 * @size_t sz@ = size of program text
715 * @int ac@ = number of arguments
716 * @const char *const *av@ = vector of command-line arguments
718 * Returns: Exit code from program.
720 * Use: Runs a REXX script from memory.
723 int rx_run(const char *name, const void *p, size_t sz,
724 int ac, const char *const *av)
734 /* --- Set things up --- */
738 MAKERXSTRING(prog[0], (void *)p, sz);
739 MAKERXSTRING(prog[1], 0, 0);
740 argv = rx_alloc(ac * sizeof(*argv));
741 for (i = 0; i < ac; i++)
742 MAKERXSTRING(argv[i], (char *)av[i], strlen(av[i]));
744 /* --- Run the script --- */
746 MAKERXSTRING(res, 0, 0);
747 rc = RexxStart(ac, argv, name, prog,
748 "SYSTEM", RXSUBROUTINE, 0, &badrc, &res);
750 rx_free(RXSTRPTR(res));
753 err_report(ERR_RXERR, 0, -rc, "rexx error from script `%s'", name);
755 err_report(ERR_RXGLUE, ERRRX_INTERP, rc, "intepreter internal error");
759 /* --- Pick apart the results --- */
761 dstr_putm(&d, RXSTRPTR(res), RXSTRLEN(res));
762 rx_free(RXSTRPTR(res));
770 /* --- @rx_runfile@ --- *
772 * Arguments: @const char *name@ = pointer to filename
773 * @int ac@ = number of command-line arguments
774 * @const char *const *av@ = vector of command-line arguments
776 * Returns: Exit code from program.
778 * Use: Runs a REXX script from a file, given its name.
781 int rx_runfile(const char *name, int ac, const char *const *av)
789 /* --- Read the file into memory --- *
791 * This way avoids any crapness in the REXX implementation and means we can
792 * report errors in a more sensible way.
795 if ((fp = fopen(name, "r")) == 0)
798 n = fread(buf, 1, sizeof(buf), fp);
800 } while (n == sizeof(buf));
805 /* --- Now do the from-memory thing --- */
807 rc = rx_run(name, d.buf, d.len, ac, av);
811 /* --- Tidy up on errors --- */
817 err_report(ERR_RXGLUE, ERRRX_SCRIPTREAD, errno,
818 "couldn't read script `%s': %s", name, strerror(errno));
822 /*----- That's all, folks -------------------------------------------------*/