home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ucb_logoppc / source / term.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-25  |  6.2 KB  |  314 lines

  1. /*
  2.  * term.c      terminal cursor control       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.  
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28.  
  29. #ifdef AMIGA
  30. void ami_term_init( void);
  31. void ami_clear_text( void);
  32. void ami_gotoxy( int x, int y);
  33. #endif
  34.  
  35. #ifdef mac
  36. #include <console.h>
  37. #endif
  38.  
  39. #ifdef HAVE_TERMIO_H
  40. #include <termio.h>
  41. #else
  42. #ifdef HAVE_SGTTY_H
  43. #include <sgtty.h>
  44. #endif
  45. #endif
  46.  
  47. #undef TRUE
  48. #undef FALSE
  49.  
  50. #ifdef HAVE_TERMCAP_H
  51. #include <termcap.h>
  52. #else
  53. #ifdef HAVE_TERMLIB_H
  54. #include <termlib.h>
  55. #else
  56. #ifdef HAVE_CURSES_H
  57. #include <curses.h>
  58. #endif
  59. #endif
  60. #endif
  61.  
  62. #undef TRUE
  63. #undef FALSE
  64.  
  65. #define FALSE  0
  66. #define TRUE   1
  67.  
  68. int x_coord, y_coord, x_max, y_max;
  69.  
  70. char PC;
  71. char *BC;
  72. char *UP;
  73. short ospeed;
  74. char bp[1024];
  75. char cl_arr[40];
  76. char cm_arr[40];
  77. char so_arr[40];
  78. char se_arr[40];
  79.  
  80. #ifdef HAVE_TERMIO_H
  81. struct termio tty_cooked, tty_cbreak;
  82. #else
  83. #ifdef HAVE_SGTTY_H
  84. struct sgttyb tty_cooked, tty_cbreak;
  85. #endif
  86. #endif
  87.  
  88. int interactive, tty_charmode;
  89.  
  90. extern char **environ, *tgoto(), *tgetstr();
  91.  
  92. char *termcap_ptr;
  93.  
  94. int termcap_putter(char ch) {
  95.     *termcap_ptr++ = ch;
  96.     return 0;
  97. }
  98.  
  99. #ifdef unix
  100. void termcap_getter(char *cap, char *buf) {
  101.     char temp[40];
  102.     char *temp_ptr = temp;
  103.  
  104.     termcap_ptr = buf;
  105.     tgetstr(cap,&temp_ptr);
  106.     tputs(temp,1,termcap_putter);
  107. }
  108. #endif
  109.  
  110. void term_init(void) {
  111. #ifdef unix
  112.     int term_sg;
  113. #endif
  114.  
  115. #ifdef AMIGA
  116.     interactive = isatty(0);
  117.     ami_term_init();
  118.     x_max = 80;
  119.     y_max = 24;
  120.     return;
  121. #else
  122.  
  123. #ifdef WIN32
  124.     interactive = 1;
  125. #else
  126.     interactive = isatty(0);
  127. #endif
  128.  
  129. #ifdef mac
  130.     term_init_mac();
  131.     return;
  132. #else
  133. #ifdef ibm
  134. #ifndef WIN32
  135.     term_init_ibm();
  136. #endif /* WIN32 */
  137. #else
  138.     if (interactive) {
  139. #ifdef HAVE_TERMIO_H
  140.    ioctl(0,TCGETA,(char *)(&tty_cooked));
  141.    tty_cbreak = tty_cooked;
  142.    tty_cbreak.c_cc[VMIN] = '\01';
  143.    tty_cbreak.c_cc[VTIME] = '\0';
  144.    tty_cbreak.c_lflag &= ~(ECHO|ICANON);
  145. #else
  146.    ioctl(0,TIOCGETP,(char *)(&tty_cooked));
  147.    tty_cbreak = tty_cooked;
  148.    tty_cbreak.sg_flags |= CBREAK;
  149.    tty_cbreak.sg_flags &= ~ECHO;
  150. #endif
  151.     }
  152.     tty_charmode = 0;
  153.     tgetent(bp, getenv("TERM"));
  154.     x_max = tgetnum("co");
  155.     if (x_max <= 0) x_max = 80;
  156.     y_max = tgetnum("li");
  157.     if (y_max <= 0) y_max = 24;
  158.     term_sg = tgetnum("sg");
  159.  
  160.     x_coord = y_coord = 0;
  161.     termcap_getter("cm", cm_arr);
  162.     termcap_getter("cl", cl_arr);
  163.  
  164.     if (term_sg <= 0) {
  165.    termcap_getter("so", so_arr);
  166.    termcap_getter("se", se_arr);
  167.     } else  /* don't mess with stupid standout modes */
  168.    so_arr[0] = se_arr[0] = '\0';
  169. #endif
  170. #endif
  171. #endif
  172. }
  173.  
  174. void charmode_on() {
  175. #ifdef AMIGA
  176.    ;  /* not used by Amiga */
  177. #else
  178. #ifdef unix
  179.     if ((readstream == stdin) && interactive && !tty_charmode) {
  180. #ifdef HAVE_TERMIO_H
  181.    ioctl(0,TCSETA,(char *)(&tty_cbreak));
  182. #else /* !HAVE_TERMIO_H */
  183.    ioctl(0,TIOCSETP,(char *)(&tty_cbreak));
  184. #endif /* HAVE_TERMIO_H */
  185.    tty_charmode++;
  186.     }
  187. #endif /* unix */
  188. #ifdef WIN32
  189.     win32_charmode_on();
  190. #endif
  191. #endif
  192. }
  193.  
  194. void charmode_off() {
  195. #ifdef AMIGA
  196.    ; /* not used by Amiga */
  197. #else
  198. #ifdef unix
  199.     if (tty_charmode) {
  200. #ifdef HAVE_TERMIO_H
  201.    ioctl(0,TCSETA,(char *)(&tty_cooked));
  202. #else /* !HAVE_TERMIO_H */
  203.    ioctl(0,TIOCSETP,(char *)(&tty_cooked));
  204. #endif /* HAVE_TERMIO_H */
  205.    tty_charmode = 0;
  206.     }
  207. #endif /* unix */
  208. #ifdef WIN32
  209.     win32_charmode_off();
  210. #endif
  211. #endif
  212. }
  213.  
  214. NODE *lcleartext(NODE *args) {
  215. #ifdef AMIGA
  216.    ami_clear_text();
  217.    ami_gotoxy(x_margin, y_margin);
  218. #else
  219. #ifdef mac
  220.     cgotoxy(x_margin + 1, y_margin + 1, stdout);
  221.     ccleos(stdout);
  222. #else
  223. #ifdef ibm
  224. #ifndef WIN32 /* sowings */
  225.     ibm_clear_text();
  226.     ibm_gotoxy(x_margin, y_margin);
  227. #else /* WIN32 */
  228.     win32_clear_text();
  229. #endif /* WIN32 || !Win32 */
  230. #else
  231.     printf("%s", cl_arr);
  232.     printf("%s", tgoto(cm_arr, x_margin, y_margin));
  233. #endif
  234. #endif
  235. #endif
  236.     x_coord = x_margin;
  237.     y_coord = y_margin;
  238.     return(UNBOUND);
  239. }
  240.  
  241. NODE *lcursor(NODE *args) {
  242. #ifdef AMIGA
  243.     get_con_position();
  244. #endif
  245.     return(cons(make_intnode((FIXNUM)(x_coord-x_margin)),
  246.       cons(make_intnode((FIXNUM)(y_coord-y_margin)), NIL)));
  247. }
  248.  
  249. NODE *lsetcursor(NODE *args) {
  250. #ifdef WIN32
  251.     return (win32_lsetcursor(args));
  252. #else /* !win32 */
  253.     NODE *arg;
  254.  
  255.     arg = pos_int_vector_arg(args);
  256.     if (NOT_THROWING) {
  257.    x_coord = x_margin + getint(car(arg));
  258.    y_coord = y_margin + getint(cadr(arg));
  259.    while ((x_coord >= x_max || y_coord >= y_max) && NOT_THROWING) {
  260.        setcar(args, err_logo(BAD_DATA, arg));
  261.        if (NOT_THROWING) {
  262.       arg = pos_int_vector_arg(args);
  263.       x_coord = x_margin + getint(car(arg));
  264.       y_coord = y_margin + getint(cadr(arg));
  265.        }
  266.    }
  267.     }
  268.     if (NOT_THROWING) {
  269. #ifdef AMIGA
  270.    ami_gotoxy(x_coord,y_coord);
  271. #else
  272. #ifdef mac
  273.    mac_gotoxy(x_coord, y_coord);
  274. #else
  275. #ifdef ibm
  276.    ibm_gotoxy(x_coord, y_coord);
  277. #else
  278.    printf("%s", tgoto(cm_arr, x_coord, y_coord));
  279. #endif
  280. #endif
  281. #endif
  282.    fflush(stdout);
  283. #ifdef __ZTC__
  284.    zflush();
  285. #endif
  286.     }
  287.     return(UNBOUND);
  288. #endif /* !win32 (for non-windows version of this code) */
  289. }
  290.  
  291. NODE *lsetmargins(NODE *args) {
  292.     NODE *arg;
  293.  
  294.     arg = pos_int_vector_arg(args);
  295.     if (NOT_THROWING) {
  296.    x_margin = getint(car(arg));
  297.    y_margin = getint(cadr(arg));
  298.    lcleartext(NIL);
  299.     }
  300.     return(UNBOUND);
  301. }
  302.  
  303. NODE *lstandout(NODE *args) {
  304.     char textbuf[300];
  305.     char fmtbuf[100];
  306.  
  307.     sprintf(fmtbuf,"%s%%p%s",so_arr,se_arr);
  308.     print_stringptr = textbuf;
  309.     print_stringlen = 300;
  310.     ndprintf((FILE *)NULL,fmtbuf,car(args));
  311.     *print_stringptr = '\0';
  312.     return(make_strnode(textbuf,NULL,(int)strlen(textbuf),STRING,strnzcpy));
  313. }
  314.