home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / elvis184.zip / src / tinytcap.c < prev    next >
C/C++ Source or Header  |  1994-01-13  |  11KB  |  413 lines

  1. /* tinytcap.c */
  2.  
  3. /* This file contains functions which simulate the termcap functions.
  4.  *
  5.  * It doesn't access a "termcap" file.  Instead, it uses an initialized array
  6.  * of strings to store the entries.  Any string that doesn't start with a ':'
  7.  * is taken to be the name of a type of terminal.  Any string that does start
  8.  * with a ':' is interpretted as the list of fields describing all of the
  9.  * terminal types that precede it.
  10.  *
  11.  * Note: since these are C strings, you can't use special sequences like
  12.  * ^M or \E in the fields; your C compiler won't understand them.  Also,
  13.  * at run time there is no way to tell the difference between ':' and '\072'
  14.  * so I sure hope your terminal definition doesn't require a ':' character.
  15.  *
  16.  * getenv(TERM) on VMS checks the SET TERM device setting.  To implement
  17.  * non-standard terminals set the logical ELVIS_TERM in VMS. (jdc)
  18.  *
  19.  * Other possible terminal types are...
  20.  *     TERM_WYSE925    - "wyse925", a Wyse 50 terminal emulating Televideo 925
  21.  * ... or you could set $TERMCAP to the terminal's description string, which
  22.  * $TERM set up to match it.
  23.  *
  24.  * Note that you can include several terminal types at the same time.  Elvis
  25.  * chooses which entry to use at runtime, based primarily on the value of $TERM.
  26.  */
  27.  
  28.  
  29. #include "config.h"
  30. #if HAS_STDLIB
  31. # include <stdlib.h>
  32. #else
  33. extern char *getenv();
  34. #endif
  35. #include <stdio.h>    /* for 'sprintf()' */
  36.  
  37. /* decide which terminal descriptions should *always* be included. */
  38. #if MSDOS || OS2
  39. # define    TERM_NANSI
  40. # define    TERM_DOSANSI
  41. # ifdef RAINBOW
  42. #  define    TERM_RAINBOW
  43. # endif
  44. #endif
  45.  
  46. #if VMS
  47. # define    TERM_VT100
  48. # define    TERM_VT100W
  49. # define    TERM_VT52
  50. #endif
  51.  
  52. #if AMIGA
  53. # define    TERM_AMIGA    /* Internal Amiga termcap entry */
  54. /* # define    TERM_VT52    /* The rest of these are here for those */
  55. # define    TERM_VT100    /* people who want to use elvis over an */
  56. /* # define    TERM_NANSI    /* AUX: port (serial.device). */
  57. /* # define    TERM_DOSANSI    /* Take out all but AMIGA to save memory. */
  58. /* # define    TERM_MINIX    /* Vanilla ANSI? */
  59. /* # define    TERM_925    /* Hang a terminal off your Amiga */
  60. #endif
  61.  
  62. #if MINIX || UNIXV
  63. # define    TERM_MINIX
  64. #endif
  65.  
  66. #if COHERENT
  67. # define    TERM_COHERENT
  68. #endif
  69.  
  70. #if TOS
  71. # define    TERM_ATARI
  72. #endif
  73.  
  74. static char *termcap[] =
  75. {
  76. #ifdef TERM_AMIGA
  77. "AA",
  78. "amiga",
  79. "Amiga ANSI",
  80. /* Amiga termcap modified from version 1.3 by Kent Polk */
  81. ":co#80:li#24:am:bs:bw:xn:\
  82. :AL=\233%dL:DC=\233%dP:DL=\233%dM:DO=\233%dB:\
  83. :LE=\233%dD:RI=\233%dC:SF=\233%dS:SR=\233%dT:UP=\233%dA:IC=\233%d@:\
  84. :ae=\2330m:al=\233L:as=\2333m:bl=\007:bt=\233Z:cd=\233J:\
  85. :ce=\233K:cl=\013:cm=\233%i%d;%dH:dc=\233P:dl=\233M:do=\233B:\
  86. :kb=^H:ho=\233H:ic=\233@:is=\23320l:\
  87. :mb=\2337;2m:md=\2331m:me=\2330m:mh=\2332m:mk=\2338m:mr=\2337m:nd=\233C:\
  88. :rs=\033c:se=\2330m:sf=\233S:so=\2337m:sb=\233T:sr=\233T:ue=\23323m:\
  89. :up=\233A:us=\2334m:vb=\007:ve=\233\040p:vi=\2330\040p:\
  90. :k1=\2330~:k2=\2331~:k3=\2332~:k4=\2333~:k5=\2334~:\
  91. :k6=\2335~:k7=\2336~:k8=\2337~:k9=\2338~:k0=\2339~:\
  92. :s1=\23310~:s2=\23311~:s3=\23312~:s4=\23313~:s5=\23314~:\
  93. :s6=\23315~:s7=\23316~:s8=\23317~:s9=\23318~:s0=\23319~:\
  94. :kd=\233B:kl=\233D:kn#10:kr=\233C:ku=\233A:le=\233D:\
  95. :kP=\233T:kN=\233S:kh=\233\040A:kH=\233\040@:",
  96. #endif
  97.  
  98. #ifdef TERM_NANSI
  99. "fansi",
  100. "nnansi",
  101. "nansi",
  102. "pcbios",
  103. ":al=\033[L:dl=\033[M:am:bs:ce=\033[K:cl=\033[2J:\
  104. :cm=\033[%i%d;%dH:co#80:do=\033[B:\
  105. :k1=#;:k2=#<:k3=#=:k4=#>:k5=#?:k6=#@:k7=#A:k8=#B:k9=#C:k0=#D:\
  106. :s1=#T:s2=#U:s3=#V:s4=#W:s5=#X:s6=#Y:s7=#Z:s8=#[:s9=#\\:s0=#]:\
  107. :c1=#^:c2=#_:c3=#`:c4=#a:c5=#b:c6=#c:c7=#d:c8=#e:c9=#f:c0=#g:\
  108. :a1=#h:a2=#i:a3=#j:a4=#k:a5=#l:a6=#m:a7=#n:a8=#o:a9=#p:a0=#q:\
  109. :kd=#P:kh=#G:kH=#O:kI=#R:kD=#S:kl=#K:kN=#Q:kP=#I:kr=#M:ku=#H:\
  110. :li#25:md=\033[1m:me=\033[m:nd=\033[C:se=\033[m:so=\033[7m:\
  111. :ue=\033[m:up=\033[A:us=\033[4m:",
  112. #endif
  113.  
  114. #ifdef TERM_DOSANSI
  115. #if !ANY_UNIX
  116. "ansi",
  117. #endif
  118. "dosansi",
  119. ":am:bs:ce=\033[K:cl=\033[2J:\
  120. :cm=\033[%i%d;%dH:co#80:do=\033[B:\
  121. :k1=#;:k2=#<:k3=#=:k4=#>:k5=#?:k6=#@:k7=#A:k8=#B:k9=#C:k0=#D:\
  122. :s1=#T:s2=#U:s3=#V:s4=#W:s5=#X:s6=#Y:s7=#Z:s8=#[:s9=#\\:s0=#]:\
  123. :c1=#^:c2=#_:c3=#`:c4=#a:c5=#b:c6=#c:c7=#d:c8=#e:c9=#f:c0=#g:\
  124. :a1=#h:a2=#i:a3=#j:a4=#k:a5=#l:a6=#m:a7=#n:a8=#o:a9=#p:a0=#q:\
  125. :kd=#P:kh=#G:kH=#O:kI=#R:kD=#S:kl=#K:kN=#Q:kP=#I:kr=#M:ku=#H:\
  126. :li#25:md=\033[1m:me=\033[m:nd=\033[C:se=\033[m:so=\033[7m:\
  127. :ue=\033[m:up=\033[A:us=\033[4m:",
  128. #endif
  129.  
  130. #ifdef TERM_RAINBOW
  131. "vt220",
  132. "rainbow",
  133. ":al=\033[L:dl=\033[M:am:bs:ce=\033[K:cl=\033[2J:\
  134. :cm=\033[%i%d;%dH:co#80:do=\033[B:kd=\033[B:kl=\033[D:\
  135. :kr=\033[C:ku=\033[A:kP=\033[5~:kN=\033[6~:kI=\033[2~:\
  136. :li#24:md=\033[1m:me=\033[m:nd=\033[C:se=\033[m:so=\033[7m:\
  137. :ue=\033[m:up=\033[A:us=\033[4m:xn:",
  138. #endif
  139.  
  140. #ifdef TERM_VT100
  141. "vt100-80",
  142. "vt101-80",
  143. ":am:bs:ce=\033[K:cl=\033[2J:cm=\033[%i%d;%dH:\
  144. :co#80:do=\033[B:k0=\033[20~:k1=\033[1~:\
  145. :k2=\033[2~:k3=\033[3~:k4=\033[4~:k5=\033[5~:k6=\033[6~:\
  146. :k7=\033[17~:k8=\033[18~:k9=\033[19~:kd=\033[B:kh=\033[H:\
  147. :kH=\033[Y:kI=\033[I:kl=\033[D:kN=\033[U:kP=\033[V:\
  148. :kr=\033[C:ku=\033[A:li#24:md=\033[1m:me=\033[m:nd=\033[C:\
  149. :se=\033[m:so=\033[7m:\
  150. :ue=\033[m:up=\033[A:us=\033[4m:xn:",
  151. "vt200-80",
  152. "vt300-80",
  153. "vt102-80",
  154. ":al=\033[L:am:bs:ce=\033[K:cl=\033[2J:cm=\033[%i%d;%dH:\
  155. :co#80:dl=\033[M:do=\033[B:k0=\033[20~:k1=\033[1~:\
  156. :k2=\033[2~:k3=\033[3~:k4=\033[4~:k5=\033[5~:k6=\033[6~:\
  157. :k7=\033[17~:k8=\033[18~:k9=\033[19~:kd=\033[B:kh=\033[H:\
  158. :kH=\033[Y:kI=\033[I:kl=\033[D:kN=\033[U:kP=\033[V:\
  159. :kr=\033[C:ku=\033[A:li#24:md=\033[1m:me=\033[m:nd=\033[C:\
  160. :se=\033[m:so=\033[7m:\
  161. :ue=\033[m:up=\033[A:us=\033[4m:xn:",
  162. #endif
  163.  
  164. #ifdef TERM_VT100W
  165. "vt100-w",
  166. "vt100-132",
  167. "vt101-w",
  168. "vt101-132",
  169. ":am:bs:ce=\033[K:cl=\033[2J:cm=\033[%i%d;%dH:\
  170. :co#132:do=\033[B:k0=\033[20~:k1=\033[1~:\
  171. :k2=\033[2~:k3=\033[3~:k4=\033[4~:k5=\033[5~:k6=\033[6~:\
  172. :k7=\033[17~:k8=\033[18~:k9=\033[19~:kd=\033[B:kh=\033[H:\
  173. :kH=\033[Y:kI=\033[I:kl=\033[D:kN=\033[U:kP=\033[V:\
  174. :kr=\033[C:ku=\033[A:li#24:md=\033[1m:me=\033[m:nd=\033[C:\
  175. :se=\033[m:so=\033[7m:\
  176. :ue=\033[m:up=\033[A:us=\033[4m:xn:",
  177. "vt200-w",
  178. "vt300-w",
  179. "vt102-w",
  180. "vt200-132",
  181. "vt300-132",
  182. "vt102-132",
  183. ":al=\033[L:am:bs:ce=\033[K:cl=\033[2J:cm=\033[%i%d;%dH:\
  184. :co#132:dl=\033[M:do=\033[B:k0=\033[20~:k1=\033[1~:\
  185. :k2=\033[2~:k3=\033[3~:k4=\033[4~:k5=\033[5~:k6=\033[6~:\
  186. :k7=\033[17~:k8=\033[18~:k9=\033[19~:kd=\033[B:kh=\033[H:\
  187. :kH=\033[Y:kI=\033[I:kl=\033[D:kN=\033[U:kP=\033[V:\
  188. :kr=\033[C:ku=\033[A:li#24:md=\033[1m:me=\033[m:nd=\033[C:\
  189. :se=\033[m:so=\033[7m:\
  190. :ue=\033[m:up=\033[A:us=\033[4m:xn:",
  191. #endif
  192.  
  193. #ifdef TERM_VT52
  194. "vt52",
  195. ":do=\n:le=\b:up=\033A:nd=\033C:cm=\033Y%+ %+ :ti=\033e\033v:\
  196. :sr=\033I:cd=\033J:ce=\033K:cl=\033H\033J:co#80:li#24:\
  197. :ku=\033A:kd=\033B:kr=\033C:kl=\033D:kb=\b:pt:am:xn:bs:",
  198. #endif
  199.  
  200. #ifdef TERM_MINIX
  201. "minix",
  202. "ansi",
  203. "AT386",
  204. ":al=\033[L:am:bs:ce=\033[K:cl=\033[2J:cm=\033[%i%d;%dH:\
  205. :co#80:dl=\033[M:do=\033[B:k0=\033[20~:k1=\033[1~:\
  206. :k2=\033[2~:k3=\033[3~:k4=\033[4~:k5=\033[5~:k6=\033[6~:\
  207. :k7=\033[17~:k8=\033[18~:k9=\033[19~:kd=\033[B:kh=\033[H:\
  208. :kH=\033[Y:kI=\033[I:kl=\033[D:kN=\033[U:kP=\033[V:\
  209. :kr=\033[C:ku=\033[A:li#25:md=\033[1m:me=\033[m:nd=\033[C:\
  210. :se=\033[m:so=\033[7m:ue=\033[m:up=\033[A:us=\033[4m:",
  211. #endif /* MINIX */
  212.  
  213. #ifdef TERM_COHERENT
  214. "coherent",
  215. "ansipc",
  216. ":al=\033[L:am:bs:ce=\033[K:cl=\033[2J:cm=\033[%i%d;%dH:\
  217. :co#80:dl=\033[M:do=\033[B:k0=\033[0x:k1=\033[1x:k2=\033[2x:\
  218. :k3=\033[3x:k4=\033[4x:k5=\033[5x:k6=\033[6x:\
  219. :k7=\033[7x:k8=\033[8x:k9=\033[9x:kd=\033[B:kh=\033[H:\
  220. :kH=\033[24H:kI=\033[@:kD=\033[P:kl=\033[D:kN=\033[U:kP=\033[V:\
  221. :kr=\033[C:ku=\033[A:li#24:md=\033[1m:me=\033[m:\
  222. :nd=\033[C:se=\033[m:so=\033[7m:ue=\033[m:up=\033[A:\
  223. :us=\033[4m:",
  224. #endif /* COHERENT */
  225.  
  226. #ifdef TERM_ATARI
  227. "atari-st",
  228. "vt52",
  229. ":al=\033L:am:bs:ce=\033K:cl=\033E:cm=\033Y%i%+ %+ :\
  230. :co#80:dl=\033M:do=\033B:\
  231. :k1=#;:k2=#<:k3=#=:k4=#>:k5=#?:k6=#@:k7=#A:k8=#B:k9=#C:k0=#D:\
  232. :s1=#T:s2=#U:s3=#V:s4=#W:s5=#X:s6=#Y:s7=#Z:s8=#[:s9=#\\:s0=#]:\
  233. :c1=#^:c2=#_:c3=#`:c4=#a:c5=#b:c6=#c:c7=#d:c8=#e:c9=#f:c0=#g:\
  234. :a1=#h:a2=#i:a3=#j:a4=#k:a5=#l:a6=#m:a7=#n:a8=#o:a9=#p:a0=#q:\
  235. kd=#P:kh=#G:kI=#R:kl=#K:kr=#M:ku=#H:li#25:nd=\033C:se=\033q:\
  236. :so=\033p:te=:ti=\033e\033v:up=\033A:",
  237. #endif
  238.  
  239. #ifdef TERM_925
  240. "wyse925",
  241. ":xn@:\
  242. :hs:am:bs:co#80:li#24:cm=\033=%+ %+ :cl=\033*:cd=\033y:\
  243. :ce=\033t:is=\033l\033\":\
  244. :al=\033E:dl=\033R:im=:ei=:ic=\033Q:dc=\033W:\
  245. :ho=\036:nd=\014:bt=\033I:pt:so=\033G4:se=\033G0:sg#1:us=\033G8:ue=\033G0:ug#1:\
  246. :up=\013:do=\026:kb=\010:ku=\013:kd=\026:kl=\010:kr=\014:\
  247. :kh=\036:ma=\026\012\014 :\
  248. :k1=\001@\r:k2=\001A\r:k3=\001B\r:k4=\001C\r:k5=\001D\r:k6=\001E\r:k7=\001F\r:\
  249. :k8=\001G\r:k9=\001H\r:k0=\001I\r:ko=ic,dc,al,dl,cl,ce,cd,bt:\
  250. :ts=\033f:fs=\033g:ds=\033h:sr=\033j:",  /* was :xn: for tvi925 alone*/
  251. #endif
  252.  
  253. "unknown",
  254. ":",
  255.  
  256. (char *)0
  257. };
  258.  
  259.  
  260. static char *fields;
  261.  
  262.  
  263. /*ARGSUSED*/
  264. int tgetent(bp, name)
  265.     char    *bp;    /* buffer for storing the entry -- ignored */
  266.     char    *name;    /* name of the entry */
  267. {
  268.     int    i;
  269.  
  270.     /* if TERMCAP is defined, and seems to match, then use it */
  271.     fields = getenv("TERMCAP");
  272.     if (fields)
  273.     {
  274.         for (i = 0; fields[i] && fields[i] != ':'; i++)
  275.         {
  276.             if (!strncmp(fields + i, name, strlen(name)))
  277.             {
  278.                 return 1;
  279.             }
  280.         }
  281.     }
  282.  
  283.     /* locate the entry in termcap[] */
  284.     for (i = 0; termcap[i] && strcmp(termcap[i], name); i++)
  285.     {
  286.     }
  287.     if (!termcap[i])
  288.     {
  289.         return 0;
  290.     }
  291.  
  292.     /* search forward for fields */
  293.     while (termcap[i][0] != ':')
  294.     {
  295.         i++;
  296.     }
  297.     fields = termcap[i];
  298.     return 1;
  299. }
  300.  
  301.  
  302. static char *find(id, vtype)
  303.     char    *id;    /* name of a value to locate */
  304.     int    vtype;    /* '=' for strings, '#' for numbers, or 0 for bools */
  305. {
  306.     int    i;
  307.  
  308.     /* search for a ':' followed by the two-letter id */
  309.     for (i = 0; fields[i]; i++)
  310.     {
  311.         if (fields[i] == ':'
  312.          && fields[i + 1] == id[0]
  313.          && fields[i + 2] == id[1])
  314.         {
  315.             /* if correct type, then return its value */
  316.             if (fields[i + 3] == vtype)
  317.                 return &fields[i + 4];
  318.             else
  319.                 return (char *)0;
  320.         }
  321.     }
  322.     return (char *)0;
  323. }
  324.  
  325. int tgetnum(id)
  326.     char    *id;
  327. {
  328.     id = find(id, '#');
  329.     if (id)
  330.     {
  331.         return atoi(id);
  332.     }
  333.     return -1;
  334. }
  335.  
  336. int tgetflag(id)
  337.     char    *id;
  338. {
  339.     if (find(id, ':'))
  340.     {
  341.         return 1;
  342.     }
  343.     return 0;
  344. }
  345.  
  346. /*ARGSUSED*/
  347. char *tgetstr(id, bp)
  348.     char    *id;
  349.     char    **bp;    /* pointer to pointer to buffer - ignored */
  350. {
  351.     char    *cpy;
  352.  
  353.     /* find the string */
  354.     id = find(id, '=');
  355.     if (!id)
  356.     {
  357.         return (char *)0;
  358.     }
  359.  
  360.     /* copy it into the buffer, and terminate it with NUL */
  361.     for (cpy = *bp; *id != ':'; )
  362.     {
  363.         if (id[0] == '\\' && id[1] == 'E')
  364.             *cpy++ = '\033', id += 2;
  365.         else
  366.             *cpy++ = *id++;
  367.     }
  368.     *cpy++ = '\0';
  369.  
  370.     /* update the bp pointer */
  371.     id = *bp;
  372.     *bp = cpy;
  373.  
  374.     /* return a pointer to the copy of the string */
  375.     return id;
  376. }
  377.  
  378. /*ARGSUSED*/
  379. char *tgoto(cm, destcol, destrow)
  380.     char    *cm;    /* cursor movement string -- ignored */
  381.     int    destcol;/* destination column, 0 - 79 */
  382.     int    destrow;/* destination row, 0 - 24 */
  383. {
  384.     static char buf[30];
  385.  
  386. #ifdef CRUNCH
  387. # if TOS
  388.     sprintf(buf, "\033Y%c%c", ' ' + destrow, ' ' + destcol);
  389. # else
  390.     sprintf(buf, "\033[%d;%dH", destrow + 1, destcol + 1);
  391. # endif
  392. #else
  393.     if (cm[1] == 'Y' || cm[1] == '=')
  394.         sprintf(buf, "\033%c%c%c", cm[1], ' ' + destrow, ' ' + destcol);
  395.     else
  396.         sprintf(buf, "\033[%d;%dH", destrow + 1, destcol + 1);
  397. #endif
  398.     return buf;
  399. }
  400.  
  401. /*ARGSUSED*/
  402. void tputs(cp, affcnt, outfn)
  403.     char    *cp;        /* the string to output */
  404.     int    affcnt;        /* number of affected lines -- ignored */
  405.     int    (*outfn) P_((int));    /* the output function */
  406. {
  407.     while (*cp)
  408.     {
  409.         (*outfn)(*cp);
  410.         cp++;
  411.     }
  412. }
  413.