home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ucb_logoppc / source / coms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-20  |  10.9 KB  |  520 lines

  1. /*
  2.  *      coms.c   program execution control module  dvb
  3.  *
  4.  * Copyright (C) 1993 by the Regents of the University of California
  5.  *
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  */
  21.  
  22. #include "logo.h"
  23. #include "globals.h"
  24. #include <math.h>
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #ifdef ibm
  29. #include "process.h"
  30. #endif
  31. #ifdef mac
  32. #include <console.h>
  33. #endif
  34. #ifdef __ZTC__
  35. #include <time.h>
  36. #include <controlc.h>
  37. #include <dos.h>
  38. #endif
  39.  
  40. #if defined(__PPC__) && defined(AMIGA)
  41.  
  42. #define __USE_SYSBASE
  43. #include <proto/exec.h>
  44. #include <proto/dos.h>
  45. #include <powerup/ppclib/interface.h>
  46. #include <powerup/gcclib/powerup_protos.h>
  47.  
  48. #define AllocVec(n, f) PPCAllocVec(n, f)
  49. #define FreeVec(b)     PPCFreeVec(b)
  50.  
  51. #endif
  52.  
  53. #ifdef HAVE_TERMIO_H
  54. #include <termio.h>
  55. #else
  56. #ifdef HAVE_SGTTY_H
  57. #include <sgtty.h>
  58. #endif
  59. #endif
  60.  
  61. FIXNUM ift_iff_flag = -1;
  62.  
  63. NODE *make_cont(enum labels cont, NODE *val) {
  64. #ifdef __ZTC__
  65.     union { enum labels lll;
  66.        NODE *ppp;} cast;
  67. #endif
  68.     NODE *retval = cons(NIL, val);
  69. #ifdef __ZTC__
  70.     cast.lll = cont;
  71.     retval->n_car = cast.ppp;
  72. #else
  73.     retval->n_car = (NODE *)cont;
  74. #endif
  75.     settype(retval, CONT);
  76.     return retval;
  77. }
  78.  
  79. NODE *loutput(NODE *arg) {
  80.     if (NOT_THROWING) {
  81.    stopping_flag = OUTPUT;
  82.    output_node = car(arg);
  83.     }
  84.     return(UNBOUND);
  85. }
  86.  
  87. NODE *lstop(NODE *args) {
  88.     if (NOT_THROWING)
  89.    stopping_flag = STOP;
  90.     return(UNBOUND);
  91. }
  92.  
  93. NODE *lthrow(NODE *arg) {
  94.     if (NOT_THROWING) {
  95.    if (compare_node(car(arg),Error,TRUE) == 0) {
  96.        if (cdr(arg) != NIL)
  97.       err_logo(USER_ERR, cadr(arg));
  98.        else
  99.       err_logo(USER_ERR, UNBOUND);
  100.    } else {
  101.        stopping_flag = THROWING;
  102.        throw_node = car(arg);
  103.        if (cdr(arg) != NIL)
  104.       output_node = cadr(arg);
  105.        else
  106.       output_node = UNBOUND;
  107.    }
  108.     }
  109.     return(UNBOUND);
  110. }
  111.  
  112. NODE *lcatch(NODE *args) {
  113.     return make_cont(catch_continuation, cons(car(args), lrun(cdr(args))));
  114. }
  115.  
  116. int torf_arg(NODE *args) {
  117.     NODE *arg = car(args);
  118.  
  119.     while (NOT_THROWING) {
  120.    if (compare_node(arg, True, TRUE) == 0) return TRUE;
  121.    if (compare_node(arg, False, TRUE) == 0) return FALSE;
  122.    setcar(args, err_logo(BAD_DATA, arg));
  123.    arg = car(args);
  124.     }
  125.     return -1;
  126. }
  127.  
  128. NODE *lgoto(NODE *args) {
  129.     return make_cont(goto_continuation, car(args));
  130. }
  131.  
  132. NODE *ltag(NODE *args) {
  133.     return UNBOUND;
  134. }
  135.  
  136. NODE *lnot(NODE *args) {
  137.     int arg = torf_arg(args);
  138.  
  139.     if (NOT_THROWING) {
  140.    if (arg) return(False);
  141.    else return(True);
  142.     }
  143.     return(UNBOUND);
  144. }
  145.  
  146. NODE *land(NODE *args) {
  147.     int arg;
  148.  
  149.     if (args == NIL) return(True);
  150.     while (NOT_THROWING) {
  151.    arg = torf_arg(args);
  152.    if (arg == FALSE)
  153.        return(False);
  154.    args = cdr(args);
  155.    if (args == NIL) break;
  156.     }
  157.     if (NOT_THROWING) return(True);
  158.     else return(UNBOUND);
  159. }
  160.  
  161. NODE *lor(NODE *args) {
  162.     int arg;
  163.  
  164.     if (args == NIL) return(False);
  165.     while (NOT_THROWING) {
  166.    arg = torf_arg(args);
  167.    if (arg == TRUE)
  168.        return(True);
  169.    args = cdr(args);
  170.    if (args == NIL) break;
  171.     }
  172.     if (NOT_THROWING) return(False);
  173.     else return(UNBOUND);
  174. }
  175.  
  176. NODE *runnable_arg(NODE *args) {
  177.     NODE *arg = car(args);
  178.  
  179.     if (!aggregate(arg)) {
  180.    setcar(args, parser(arg, TRUE));
  181.    arg = car(args);
  182.     }
  183.     while (!is_list(arg) && NOT_THROWING) {
  184.    setcar(args, err_logo(BAD_DATA, arg));
  185.    arg = car(args);
  186.     }
  187.     return(arg);
  188. }
  189.  
  190. NODE *lif(NODE *args) { /* macroized */
  191.     NODE *yes;
  192.     int pred;
  193.  
  194.     if (cddr(args) != NIL) return(lifelse(args));
  195.  
  196.     pred = torf_arg(args);
  197.     yes = runnable_arg(cdr(args));
  198.     if (NOT_THROWING) {
  199.    if (pred) return(yes);
  200.    return(NIL);
  201.     }
  202.     return(UNBOUND);
  203. }
  204.  
  205. NODE *lifelse(NODE *args) {    /* macroized */
  206.     NODE *yes, *no;
  207.     int pred;
  208.  
  209.     pred = torf_arg(args);
  210.     yes = runnable_arg(cdr(args));
  211.     no = runnable_arg(cddr(args));
  212.     if (NOT_THROWING) {
  213.    if (pred) return(yes);
  214.    return(no);
  215.     }
  216.     return(UNBOUND);
  217. }
  218.  
  219. NODE *lrun(NODE *args) {    /* macroized */
  220.     NODE *arg = runnable_arg(args);
  221.  
  222.     if (NOT_THROWING) return(arg);
  223.     return(UNBOUND);
  224. }
  225.  
  226. NODE *lrunresult(NODE *args) {
  227.     return make_cont(runresult_continuation, lrun(args));
  228. }
  229.  
  230. NODE *pos_int_arg(NODE *args) {
  231.     NODE *arg = car(args), *val;
  232.     FIXNUM i;
  233.     FLONUM f;
  234.  
  235.     val = cnv_node_to_numnode(arg);
  236.     while ((nodetype(val) != INT || getint(val) < 0) && NOT_THROWING) {
  237.    if (nodetype(val) == FLOATT &&
  238.           fmod((f = getfloat(val)), 1.0) == 0.0 &&
  239.           f >= 0.0 && f < (FLONUM)MAXLOGOINT) {
  240. #if HAVE_IRINT
  241.        i = irint(f);
  242. #else
  243.        i = f;
  244. #endif
  245.        val = make_intnode(i);
  246.        break;
  247.    }
  248.    setcar(args, err_logo(BAD_DATA, arg));
  249.    arg = car(args);
  250.    val = cnv_node_to_numnode(arg);
  251.     }
  252.     setcar(args,val);
  253.     if (nodetype(val) == INT) return(val);
  254.     return UNBOUND;
  255. }
  256.  
  257. NODE *lrepeat(NODE *args) {
  258.     NODE *cnt, *torpt, *retval = NIL;
  259.  
  260.     cnt = pos_int_arg(args);
  261.     torpt = lrun(cdr(args));
  262.     if (NOT_THROWING) {
  263.    retval = make_cont(repeat_continuation, cons(cnt,torpt));
  264.     }
  265.     return(retval);
  266. }
  267.  
  268. NODE *lrepcount(NODE *args) {
  269.     return make_intnode(user_repcount);
  270. }
  271.  
  272. NODE *lforever(NODE *args) {
  273.     NODE *torpt = lrun(args);
  274.  
  275.     if (NOT_THROWING)
  276.     return make_cont(repeat_continuation, cons(make_intnode(-1), torpt));
  277.     return NIL;
  278. }
  279.  
  280. NODE *ltest(NODE *args) {
  281.     int arg = torf_arg(args);
  282.  
  283.     if (tailcall != 0) return UNBOUND;
  284.     if (NOT_THROWING) {
  285.    ift_iff_flag = arg;
  286.    dont_fix_ift = 1;
  287.     }
  288.     return(UNBOUND);
  289. }
  290.  
  291. NODE *liftrue(NODE *args) {
  292.     if (ift_iff_flag < 0)
  293.    return(err_logo(NO_TEST,NIL));
  294.     else if (ift_iff_flag > 0)
  295.    return(lrun(args));
  296.     else
  297.    return(NIL);
  298. }
  299.  
  300. NODE *liffalse(NODE *args) {
  301.     if (ift_iff_flag < 0)
  302.    return(err_logo(NO_TEST,NIL));
  303.     else if (ift_iff_flag == 0)
  304.    return(lrun(args));
  305.     else
  306.    return(NIL);
  307. }
  308.  
  309. void prepare_to_exit(BOOLEAN okay) {
  310. #ifdef AMIGA
  311.    amiga_deinit();
  312. #else
  313. #ifdef mac
  314.     if (okay) {
  315.    console_options.pause_atexit = 0;
  316.    exit(0);
  317.     }
  318. #endif
  319. #ifndef WIN32 /* sowings */
  320. #ifdef ibm
  321.     ltextscreen(NIL);
  322.     ibm_plain_mode();
  323. #ifdef __ZTC__
  324.     zflush();
  325.     controlc_close();
  326. #endif
  327. #endif
  328. #endif /* !WIN32 */
  329.  
  330. #ifdef unix
  331. #ifndef HAVE_UNISTD_H
  332.     extern int getpid();
  333. #endif
  334.     char ef[30];
  335.  
  336.     charmode_off();
  337.     sprintf(ef, "/tmp/logo%d", (int)getpid());
  338.     unlink(ef);
  339. #endif
  340. #endif /* AMIGA */
  341. }
  342.  
  343. NODE *lbye(NODE *args) {
  344.     prepare_to_exit(TRUE);
  345.     if (ufun != NIL || loadstream != stdin) exit(0);
  346. #ifndef WIN32
  347.     if (isatty(0) && isatty(1))
  348. #endif
  349. #ifndef AMIGA
  350.     lcleartext(NIL);
  351.     ndprintf(stdout, "Thank you for using Logo.\n");
  352.     ndprintf(stdout, "Have a nice day.\n");
  353. #endif
  354. #ifdef __ZTC__
  355.     printf("\n");
  356. #endif
  357.     exit(0);
  358.     return UNBOUND;  /* not reached, but makes compiler happy */
  359. }
  360.  
  361. NODE *lwait(NODE *args) {
  362.     NODE *num;
  363.     unsigned int n;
  364.  
  365.     num = pos_int_arg(args);
  366. #ifdef AMIGA
  367.     amiga_wait((unsigned int)getint(num));
  368. #else
  369.     if (NOT_THROWING) {
  370. #ifdef WIN32
  371.       win32_update_text();
  372. #else
  373.    fflush(stdout); /* csls v. 1 p. 7 */
  374. #endif
  375. #if defined(__ZTC__) && !defined(WIN32) /* sowings */
  376.    zflush();
  377. #endif
  378.    if (getint(num) > 0) {
  379. #ifdef unix
  380. #ifdef HAVE_USLEEP
  381.        n = (unsigned int)getint(num) * 16667;
  382.        usleep(n);
  383. #else
  384.        n = (unsigned int)getint(num) / 60;
  385.        sleep(n);
  386. #endif
  387. #else /* not unix */
  388. #if defined(__ZTC__) && !defined(_MSC_VER)
  389.        usleep(getint(num) * 16667L);
  390. #else /* not DOS */
  391.       if (!setjmp(iblk_buf)) {
  392.          input_blocking++;
  393. #ifndef _MSC_VER
  394.          n = ((unsigned int)getint(num)+30) / 60;
  395. #ifdef mac
  396.          while (n > 1) {
  397.             sleep(1);
  398.             n -= 1;
  399.             if (check_throwing) n = 0;
  400.          }
  401. #endif
  402.          if (n > 0) sleep(n);
  403. #else
  404.          n = (unsigned int)getint(num);
  405.          while (n > 60) {
  406.             _sleep(1000);
  407.             n -= 60;
  408.             if (check_throwing) n = 0;
  409.          }
  410.          if (n > 0) _sleep(n*1000/60);
  411. #endif
  412.       }
  413.       input_blocking = 0;
  414. #endif   /* DOS */
  415. #endif   /* Unix */
  416.    }
  417.     }
  418. #endif   /* AMIGA */
  419.     return(UNBOUND);
  420. }
  421.  
  422. NODE *lshell(NODE *args) {
  423. #ifdef AMIGA
  424.    char *foo = AllocVec(256L,MEMF_CLEAR);
  425.    if (foo) {
  426.       typestring(foo,args,255);
  427.       Execute(foo,NULL,console);
  428.       FreeVec(foo);
  429.    } else
  430.       ami_print("Shell failed: Unable to get memory.\n");
  431.    return(UNBOUND);
  432. #else
  433. #ifdef mac
  434.     printf("Sorry, no shell on the Mac.\n");
  435.     return(UNBOUND);
  436. #else
  437. #ifdef ibm
  438.     NODE *arg;
  439.     char doscmd[200];
  440. /*  union REGS r;     */
  441.  
  442.     arg = car(args);
  443.     while (!is_list(arg) && NOT_THROWING) {
  444.    setcar(args, err_logo(BAD_DATA, arg));
  445.    arg = car(args);
  446.     }
  447.     if (arg == NIL) {
  448.    ndprintf(stdout,"Type EXIT to return to Logo.\n");
  449.    if (spawnlp(P_WAIT, "command", "command", NULL))
  450.        err_logo(FILE_ERROR,
  451.          make_static_strnode
  452.        ("Could not open shell (probably due to low memory)"));
  453.     }
  454.     else {
  455.    print_stringlen = 199;
  456.    print_stringptr = doscmd;
  457.    ndprintf((FILE *)NULL,"%p",arg);
  458.    *print_stringptr = '\0';
  459.    if (system(doscmd))
  460.        err_logo(FILE_ERROR,
  461.          make_static_strnode
  462.        ("Could not open shell (probably due to low memory)"));
  463.     }
  464. /*
  465.     r.h.ah = 0x3;
  466.     r.h.al = 0;
  467.     r.h.dh = 0; r.h.dl = 0;
  468.     int86(0x21, &r, &r);
  469.     x_coord = x_margin;
  470.     y_coord = r.h.dh;
  471.  */
  472. #ifndef WIN32
  473.     x_coord = x_margin;
  474.     y_coord = y_max;
  475.     ibm_gotoxy(x_coord, y_coord);
  476. #else
  477.     win32_repaint_screen();
  478. #endif
  479.     return(UNBOUND);
  480. #else
  481.     extern FILE *popen();
  482.     char cmdbuf[300];
  483.     FILE *strm;
  484.     NODE *head = NIL, *tail = NIL, *this;
  485.     BOOLEAN wordmode = FALSE;
  486.     int len;
  487.  
  488.     if (cdr(args) != NIL) wordmode = TRUE;
  489.     print_stringptr = cmdbuf;
  490.     print_stringlen = 300;
  491.     ndprintf((FILE *)NULL,"%p\n",car(args));
  492.     *print_stringptr = '\0';
  493.     strm = popen(cmdbuf,"r");
  494.     fgets(cmdbuf,300,strm);
  495.     while (!feof(strm)) {
  496.    len = (int)strlen(cmdbuf);
  497.    if (cmdbuf[len-1] == '\n')
  498.        cmdbuf[--len] = '\0';
  499.    if (wordmode)
  500.        this = make_strnode(cmdbuf, (struct string_block *)NULL, len,
  501.          STRING, strnzcpy);
  502.    else
  503.        this = parser(make_static_strnode(cmdbuf), FALSE);
  504.    if (head == NIL) {
  505.        tail = head = cons(this,NIL);
  506.    } else {
  507.        setcdr(tail, cons(this,NIL));
  508.        tail = cdr(tail);
  509.    }
  510.    fgets(cmdbuf,300,strm);
  511.     }
  512.     pclose(strm);
  513.     return(head);
  514. #endif   /* ibm */
  515. #endif   /* mac */
  516. #endif   /* AMIGA */
  517. }
  518.  
  519. /* end of coms.c */
  520.