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

  1. /*
  2.  *      print.c         logo printing 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. #include "logo.h"
  22. #include "globals.h"
  23. #include <stdarg.h>
  24.  
  25. #if defined(__PPC__) && defined(AMIGA)
  26.  
  27. #define __USE_SYSBASE
  28. #include <proto/exec.h>
  29. #include <proto/dos.h>
  30. #include <powerup/ppclib/interface.h>
  31. #include <powerup/gcclib/powerup_protos.h>
  32.  
  33. #define AllocVec(n, f) PPCAllocVec(n, f)
  34. #define FreeVec(b)     PPCFreeVec(b)
  35.  
  36. #endif
  37.  
  38. int print_stringlen;
  39. char *print_stringptr;
  40. int x_margin=0, y_margin=0;
  41. void real_print_node(FILE *, NODE *, int, int);
  42.  
  43. BOOLEAN print_backslashes = FALSE;
  44.  
  45. #ifdef mac
  46. BOOLEAN boldmode = 0;
  47. #endif
  48.  
  49. void update_coords(char ch) {
  50.     int i;
  51.  
  52. #ifdef ibm
  53. #if !defined(__ZTC__) && !defined(_MSC_VER) && !defined(WIN32)
  54.     check_scroll();
  55. #endif
  56. #endif
  57.     if (ch == '\n') {
  58.    x_coord = 0;
  59.    y_coord++;
  60.    for (i = 0; i < x_margin; i++) print_char(stdout,' ');
  61.     } else if (ch == '\b') {
  62.    if (x_coord > 0) --x_coord;
  63.     } else if (ch == '\t') {
  64.    x_coord &= ~07;
  65.    x_coord += 8;
  66. #ifdef WIN32
  67.     } else if (ch == '\1' || ch == '\2') {
  68. #endif
  69.     } else if (ch != '\007')
  70.    x_coord++;
  71.     if (x_coord > x_max) {
  72.    y_coord += x_coord/x_max;
  73.    x_coord %= x_max;
  74.     }
  75.     if (y_coord > y_max) y_coord = y_max;
  76. }
  77.  
  78. void print_char(FILE *strm, char ch) {
  79.     if (strm) {
  80.    if (interactive && strm==stdout) {
  81. #ifdef AMIGA
  82.       if (console)
  83.          FPutC(console,ch);
  84.       else
  85. #endif
  86. #ifdef mac
  87.        if (boldmode) {
  88.       if (ch == '\2')
  89.           boldmode = 0;
  90.       else
  91.           ch = ch | 0200;     /* Not so good in Europe */
  92.        } else if (ch == '\1')
  93.       boldmode = 1;
  94. #endif
  95. #ifdef ibm
  96.        if (ch == '\1')
  97.       ibm_bold_mode();
  98.        if (ch == '\2')
  99.       ibm_plain_mode();
  100. #if defined(__ZTC__) && !defined(WIN32) /* sowings */
  101.        ztc_put_char(ch);
  102. #elif defined(TURBO_C)
  103.        if (in_graphics_mode && ibm_screen_top == 0)
  104.       lsplitscreen();
  105.        if (ch == '\n' || in_graphics_mode)
  106.       rd_putc(ch, strm);
  107.        else if (ch != '\1' && ch != '\2')
  108.       rd_putc(ch, stdout); /* takes advantage of bold attribute */
  109. #else /* WIN32 */
  110.        if (ch != '\1' && ch != '\2')
  111.          rd_putc(ch, strm);
  112. #endif /* ibm */
  113. #else /* Unix and Amiga */
  114.        rd_putc(ch, strm);
  115. #endif
  116.    } else       /* printing to stream but not screen */
  117.        rd_putc(ch, strm);
  118.    if (strm == stdout) {
  119.        if (dribblestream != NULL)
  120.       rd_putc(ch, dribblestream);
  121.        update_coords(ch);
  122.    }
  123.     } else {       /* printing to string */
  124.    if (--print_stringlen > 0)
  125.        *print_stringptr++ = ch;
  126.     }
  127. }
  128.  
  129. void print_space(FILE *strm) {
  130.     print_char(strm,' ');
  131. }
  132.  
  133. /*VARARGS2*/
  134. void ndprintf(FILE *strm, char *fmt, ...) {
  135.     va_list ap;
  136.     NODE *nd;
  137.     char *cp;
  138.     char ch;
  139.  
  140.     va_start(ap,fmt);
  141. #ifdef AMIGA
  142.    if (strm==stdin)
  143.       SetMode(console,0);  /* turn buffering on */
  144. #endif
  145.     while ((ch = *fmt++) != '\0') {
  146.    if (ch == '%') {
  147.        ch = *fmt++;
  148.        if (ch == 's')    /* show */
  149.       print_node(strm,va_arg(ap,NODE *));
  150.        else if (ch == 'p') { /* print */
  151.       nd = va_arg(ap,NODE *);
  152.       if (is_list(nd))
  153.           print_help(strm,nd);
  154.       else
  155.           print_node(strm,nd);
  156.        } else if (ch == 't') { /* text */
  157.       cp = va_arg(ap,char *);
  158.       while ((ch = *cp++) != '\0') print_char(strm,ch);
  159.        } else {
  160.       print_char(strm,'%');
  161.       print_char(strm,ch);
  162.        }
  163.    } else print_char(strm,ch);
  164.     }
  165.     if (!strm) print_char(strm,'\0');
  166. #ifdef AMIGA
  167.    if (strm==stdin)
  168.       SetMode(console,1);  /* turn buffering off */
  169. #endif
  170.     va_end(ap);
  171. }
  172.  
  173. void real_print_help(FILE *strm, NODE *ndlist, int depth, int width) {
  174.     NODE *arg = NIL;
  175.     int wid = width;
  176.  
  177.     while (ndlist != NIL) {
  178.       if (!is_list(ndlist)) return;
  179.    arg = car(ndlist);
  180.    ndlist = cdr(ndlist);
  181.    if (check_throwing) break;
  182.    real_print_node(strm, arg, depth, width);
  183.    if (ndlist != NIL) {
  184.        print_space(strm);
  185.        if (--wid == 0) {
  186.       ndprintf(strm, "...");
  187.       break;
  188.        }
  189.    }
  190.     }
  191. }
  192.  
  193. void real_print_node(FILE *strm, NODE *nd, int depth, int width) {
  194.     int i;
  195.     char *cp;
  196.     NODETYPES ndty;
  197.  
  198.     if (depth == 0) {
  199.    ndprintf(strm, "...");
  200.    return;
  201.     }
  202.     if (nd == NIL) {
  203.    print_char(strm,'[');
  204.    print_char(strm,']');
  205.     } else if (nd == UNBOUND) {
  206.    ndprintf(strm, "UNBOUND");
  207.     } else if ((ndty = nodetype(nd)) & NT_PRIM) {
  208.    ndprintf(strm, "PRIM");
  209.     } else if (ndty & NT_LIST) {
  210.    print_char(strm,'[');
  211.    real_print_help(strm, nd, depth-1, width);
  212.    print_char(strm,']');
  213.     } else if (ndty == ARRAY) {
  214.    int i = 0, dim = getarrdim(nd), wid;
  215.    NODE **pp = getarrptr(nd);
  216.  
  217.    if (width < 0) wid = dim;
  218.    else wid = (dim > width ? width : dim);
  219.    print_char(strm,'{');
  220.    while (i < wid) {
  221.        real_print_node(strm,*pp++,depth-1,width);
  222.        if (++i < dim) print_space(strm);
  223.    }
  224.    if (wid < dim) ndprintf(strm, "...");
  225.    print_char(strm,'}');
  226.    if (print_backslashes && (getarrorg(nd) != 1)) {
  227.        char org[] = "@  ";
  228.  
  229.        sprintf(&org[1],"%d",getarrorg(nd));
  230.        ndprintf(strm,org);
  231.    }
  232.     } else if (ndty == QUOTE) {
  233.    print_char(strm, '\"');
  234.    print_node(strm, car(nd));
  235.     } else if (ndty == COLON) {
  236.    print_char(strm, ':');
  237.    print_node(strm, car(nd));
  238.     } else {
  239.    int wid, dots=0;
  240.  
  241.    nd = cnv_node_to_strnode(nd);
  242.    cp = getstrptr(nd);
  243.    if (width < 0) wid = getstrlen(nd);
  244.    else {
  245.        wid = (width < 10 ? 10 : width);
  246.        wid = (wid < getstrlen(nd) ? wid : getstrlen(nd));
  247.    }
  248.    if (wid < getstrlen(nd)) dots++;
  249.  
  250.    if (!backslashed(nd))
  251.        for (i = 0; i < wid; i++) {
  252.       print_char(strm,*cp++);
  253.        }
  254.    else if (print_backslashes == FALSE) {
  255.        for (i = 0; i < wid; i++) {
  256.       print_char(strm,clearparity(*cp++));
  257.        }
  258.    } else {
  259.        for (i = 0; i < wid; i++) {
  260.       if (getparity(cp[i])) break;
  261.        }
  262.        if (i < wid) {   /* word was in vbars */
  263.          if (strchr("\":", *cp)) {
  264.             print_char(strm, *cp++);
  265.             wid--;
  266.          }
  267.       print_char(strm,'|');
  268.       for (i = 0; i < wid; i++) {
  269.           print_char(strm,clearparity(*cp++));
  270.       }
  271.       print_char(strm,'|');
  272.        } else for (i = 0; i < wid; i++) {
  273.       if (strchr(special_chars,(int)*cp)) {
  274.           print_char(strm,'\\');
  275.       }
  276.       print_char(strm,*cp++);
  277.        }
  278.    };
  279.    if (dots) ndprintf(strm, "...");
  280.     }
  281. }
  282.  
  283. int find_limit(NODE *nd) {
  284.     int val = -1;
  285.  
  286.     if (nd == NIL) return(-1);
  287.     nd = cnv_node_to_numnode(valnode__caseobj(nd));
  288.     if (nodetype(nd) == INT) val = getint(nd);
  289.     return(val);
  290. }
  291.  
  292. void print_help(FILE *strm, NODE *nd) {
  293.     real_print_help(strm, nd, find_limit(Printdepthlimit),
  294.           find_limit(Printwidthlimit));
  295. }
  296.  
  297. void print_node(FILE *strm, NODE *nd) {
  298.     real_print_node(strm, nd, find_limit(Printdepthlimit),
  299.           find_limit(Printwidthlimit));
  300. }
  301.  
  302. void print_nobrak(FILE *strm, NODE *nd) {
  303.     if (is_list(nd)) print_help(strm, nd);
  304.     else print_node(strm, nd);
  305. }
  306.  
  307. void new_line(FILE *strm) {
  308.   /*
  309. #ifdef WIN32
  310.   if (strm == stdout)
  311.   {
  312.     win32_advance_line();
  313.     if (dribblestream != NULL)
  314.       rd_putc('\n', dribblestream);
  315.     update_coords('\n');
  316.   }
  317.   else
  318. #endif
  319. */
  320.     print_char(strm,'\n');
  321. }
  322.  
  323. NODE *lshow(NODE *args) {
  324.     print_help(writestream, args);
  325.     new_line(writestream);
  326.     return(UNBOUND);
  327. }
  328.  
  329. void type_help(NODE *args, int sp) {
  330.     NODE *arg = NIL;
  331.  
  332.     while (args != NIL) {
  333.    arg = car(args);
  334.    args = cdr(args);
  335.    if (is_list(arg))
  336.        print_help(writestream, arg);
  337.    else
  338.        print_node(writestream, arg);
  339.    if (sp && (args != NIL)) {
  340.        print_space(writestream);
  341.    }
  342.     }
  343. }
  344.  
  345. void typestring(buffer,args,bufsiz)
  346. char *buffer;
  347. NODE *args;
  348. int bufsiz;
  349. {
  350.    NODE *arg = NIL;
  351.  
  352.    print_stringptr = buffer;
  353.    print_stringlen = bufsiz;
  354.  
  355.    while (args != NIL) {
  356.       arg = car(args);
  357.       args = cdr(args);
  358.       if (is_list(arg))
  359.          print_help(NULL, arg);
  360.       else
  361.          print_node(NULL, arg);
  362.    }
  363. }
  364.  
  365. NODE *ltype(NODE *args) {
  366.     type_help(args,0);
  367.     return(UNBOUND);
  368. }
  369.  
  370. NODE *lprint(NODE *args) {
  371.     type_help(args,1);
  372.     new_line(writestream);
  373.     return(UNBOUND);
  374. }
  375.