home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / plbin.zip / pl / src / pl-term.c < prev    next >
C/C++ Source or Header  |  1993-02-23  |  5KB  |  232 lines

  1. /*  pl-term.c,v 1.3 1993/02/23 13:16:48 jan Exp
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     See ../LICENCE to find out about your rights.
  5.     jan@swi.psy.uva.nl
  6.  
  7.     Purpose: Simple terminal handling
  8. */
  9.  
  10. #include "pl-incl.h"
  11.  
  12. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13. This module defines some hacks to get to the unix  termcap  library.   I
  14. realise this is not a proper answer to terminal control from Prolog, but
  15. I  needed  it  some day and at least it is better than doing things like
  16. shell(clear), coding terminal sequences hard, etc.   One  day  I  should
  17. write a decent interface to handle the terminal.  Maybe this will be too
  18. late;  character  terminals  disappear quickly now.  Use PCE if you want
  19. windowing!
  20. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  21.  
  22. #if unix || EMX
  23.  
  24. extern int  tgetent();
  25. extern int  tgetnum();
  26. extern int  tgetflag();
  27. extern char *tgetstr();
  28. extern char *tgoto();
  29. extern int  tputs();
  30.  
  31. #define MAX_TERMBUF    1024        /* Confirming manual */
  32. #define STAT_START    0
  33. #define STAT_OK        1
  34. #define STAT_ERROR    2
  35.  
  36. extern int Output;            /* Current output stream */
  37. char    PC;                /* Term lib variables */
  38. char   *BC;
  39. char   *UP;
  40. short    ospeed;
  41.  
  42. static int    term_initialised;    /* Extracted term info? */
  43. static char     *string_area_pointer;    /* Current location */
  44. static Table    capabilities;        /* Terminal capabilities */
  45. static word    tty_stream;        /* stream on which to do tty */
  46.  
  47. typedef struct
  48. { Atom    type;                /* type of the entry */
  49.   Atom  name;                /* Name of the value */
  50.   word  value;                /* Value of the entry */
  51. } entry, *Entry;
  52.  
  53. forwards bool    initTerm P((void));
  54. forwards Entry    lookupEntry P((Atom, Atom));
  55.  
  56. void
  57. resetTerm()
  58. { if ( capabilities == NULL )
  59.   { capabilities = newHTable(32);
  60.   } else
  61.   { Symbol s;
  62.  
  63.     term_initialised = STAT_START;
  64.     for_table(s, capabilities)
  65.       freeHeap(s->value, sizeof(entry));
  66.     clearHTable(capabilities);
  67.   }
  68.  
  69.   tty_stream = (word) ATOM_user_output;
  70. }
  71.  
  72. static bool
  73. initTerm()
  74. { static char *buf = NULL;
  75.   static char *string_area = NULL;
  76.  
  77.   if ( term_initialised == STAT_START )
  78.   { char *term;
  79.  
  80.     term_initialised = STAT_ERROR;
  81.     if ( (term = getenv("TERM")) == NULL )
  82.       return warning("No variable TERM");
  83.  
  84.     if ( buf == NULL )         buf         = allocHeap(MAX_TERMBUF);
  85.     if ( string_area == NULL ) string_area = allocHeap(MAX_TERMBUF);
  86.     string_area_pointer = string_area;
  87.  
  88.     switch( tgetent(buf, term) )
  89.     { case -1:    return warning("Cannot open termcap file");
  90.       case  1:    break;
  91.       default:
  92.       case  0:    return warning("Unknown terminal: %s", term);
  93.     }
  94.  
  95.     term_initialised = STAT_OK;
  96.   }
  97.  
  98.   return term_initialised == STAT_OK;
  99. }
  100.  
  101. static Entry
  102. lookupEntry(name, type)
  103. Atom name, type;
  104. { Symbol s;
  105.   Entry e;
  106.  
  107.   if ( (s = lookupHTable(capabilities, name)) == NULL )
  108.   { if ( initTerm() == FALSE )
  109.       return NULL;
  110.  
  111.     e = (Entry) allocHeap(sizeof(entry));
  112.     e->name = name;
  113.     e->type = type;
  114.     e->value = 0L;
  115.  
  116.     if ( type == ATOM_number )
  117.     { int n;
  118.  
  119.       if ( (n = tgetnum(stringAtom(name))) != -1 )
  120.         e->value  = consNum(n);
  121.     } else if ( type == ATOM_bool )
  122.     { bool b;
  123.     
  124.       if ( (b = tgetflag(stringAtom(name))) != -1 )
  125.         e->value = (word) (b ? ATOM_on : ATOM_off);
  126.     } else if ( type == ATOM_string )
  127.     { char *s;
  128.     
  129.       if ( (s = tgetstr(stringAtom(name), &string_area_pointer)) != NULL )
  130.         e->value  = (word) lookupAtom(s);
  131.     } else
  132.     { warning("tgetent/3: Illegal type");
  133.       freeHeap(e, sizeof(entry));
  134.       return NULL;
  135.     }
  136.  
  137.     addHTable(capabilities, name, e);
  138.     return e;
  139.   } else
  140.     return (Entry) s->value;
  141. }
  142.       
  143. word
  144. pl_tty_get_capability(name, type, value)
  145. Word name, type, value;
  146. { Entry e;
  147.  
  148.   if ( !isAtom(*name) || !isAtom(*type) )
  149.     return warning("tgetent/3: instantiation fault");
  150.   if ( (e = lookupEntry((Atom) *name, (Atom) *type)) == NULL )
  151.     fail;
  152.  
  153.   if ( e->value != 0L )
  154.     return unifyAtomic(value, e->value);
  155.  
  156.   fail;
  157. }
  158.   
  159. word
  160. pl_tty_goto(x, y)
  161. Word x, y;
  162. { Entry e;
  163.   char *s;
  164.  
  165.   if ( !isInteger(*x) || !isInteger(*y) )
  166.     return warning("tty_goto: instantiation fault");
  167.  
  168.   if ( (e = lookupEntry(ATOM_cm, ATOM_string)) == NULL ||
  169.         e->value == 0L )
  170.     fail;
  171.  
  172.   s = tgoto(stringAtom(e->value), (int)valNum(*x), (int)valNum(*y));
  173.   if ( streq(s, "OOPS") )
  174.     fail;
  175.   streamOutput(&tty_stream, (tputs(s, 1, put_character), TRUE));
  176. }
  177.  
  178. word
  179. pl_tty_put(a, affcnt)
  180. Word a;
  181. Word affcnt;
  182. { char *s = primitiveToString(*a, FALSE);
  183.  
  184.   if ( s == NULL || !isInteger(*affcnt) )
  185.     return warning("tty_put: instantiation fault");
  186.   streamOutput(&tty_stream, (tputs(s, (int)valNum(*affcnt), put_character), TRUE));
  187. }
  188.  
  189. word
  190. pl_set_tty(old, new)
  191. Word old, new;
  192. { TRY( unifyAtomic(old, tty_stream) );
  193.   if ( streamNo(new, F_WRITE) < 0 )
  194.     fail;
  195.  
  196.   tty_stream = *new;
  197.   succeed;
  198. }
  199.  
  200. #else /* ~unix */
  201.  
  202. void resetTerm()
  203. {
  204. }
  205.  
  206. word
  207. pl_tty_get_capability(name, type, value)
  208. Word name, type, value;
  209. { return notImplemented("tty_get_capability", 3);
  210. }
  211.  
  212. word
  213. pl_tty_goto(x, y)
  214. Word x, y;
  215. { return notImplemented("tty_goto", 2);
  216. }
  217.  
  218. word
  219. pl_tty_put(a, affcnt)
  220. Word a;
  221. Word affcnt;
  222. { return notImplemented("tty_put", 2);
  223. }
  224.  
  225. word
  226. pl_set_tty(old, new)
  227. Word old, new;
  228. { return notImplemented("set_tty", 2);
  229. }
  230.  
  231. #endif /* unix */
  232.