home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1778 < prev    next >
Internet Message Format  |  1990-12-28  |  23KB

  1. From: peter@sugar.hackercorp.com (Peter da Silva)
  2. Newsgroups: alt.sources,alt.sources.amiga
  3. Subject: Small termlib (suitable for use with Elvis)
  4. Message-ID: <6517@sugar.hackercorp.com>
  5. Date: 4 Sep 90 12:15:45 GMT
  6.  
  7. Archive-name: termlib
  8.  
  9. The following implementation of termlib is hereby placed in the public
  10. domain. It's not fancy, it doesn't inlcude terminfo, but it's small and
  11. clean and is useful for porting termlib-based programs like Elvis to
  12. other platforms. I originally wrote it for CP/M under the BDS C compiler
  13. so it makes few assumptions about the environment.
  14.  
  15. The included makefile is for the Amiga, but I think it unlikely anyone would
  16. have much trouble porting it to other machines. The implementation of tgoto
  17. is probably out of date, but if you really need more it'd be easy enough to
  18. expand.
  19.  
  20. The port of elvis itself to the Amiga is underway.
  21.  
  22. : This archive contains the following files...
  23. : 'termlib.h'
  24. : 'tinit.c'
  25. : 'tgetnum.c'
  26. : 'tutil.c'
  27. : 'tvars.c'
  28. : 'tgoto.c'
  29. : 'tgetent.c'
  30. : 'tputs.c'
  31. : 'cur.c'
  32. : 'termcap.c'
  33. : 'tgetflag.c'
  34. : 'tgetstr.c'
  35. : 'Makefile'
  36. : To extract them, run the following through /bin/sh
  37. echo x - termlib.h
  38. sed 's/^X//' > termlib.h << '//END'
  39. X/* TERMLIB: Terminal independant database.
  40. X *
  41. X * Module: termlib.h
  42. X *
  43. X * Purpose: declare global variables and functions.
  44. X */
  45. X
  46. X/* termlib.h
  47. X * Global variables for termlib
  48. X *
  49. X*/
  50. X#ifndef AMIGA
  51. X#define AMIGA 0
  52. X#endif
  53. X
  54. Xextern char *tent;             /* Pointer to terminal entry, set by tgetent */
  55. Xextern char PC;                              /* Pad character, default NULL */
  56. Xextern char *UP, *BC;        /* Pointers to UP and BC strings from database */
  57. Xextern short ospeed;       /* Baud rate (1-16, 1=300, 16=19200), as in stty */
  58. X
  59. Xint tgetnum();
  60. Xchar *tgoto();
  61. Xint tgetent();
  62. Xint tgetflag();
  63. Xchar *tgetstr();
  64. X
  65. Xchar *_find();
  66. Xchar *_addfmt();
  67. //END
  68. echo x - tinit.c
  69. sed 's/^X//' > tinit.c << '//END'
  70. X/* TERMLIB: Terminal independant database.
  71. X *
  72. X * Module: tinit
  73. X *
  74. X * Purpose: simplified terminal initialisation.
  75. X *
  76. X * Calling conventions: name is name of terminal.
  77. X *
  78. X * Returned values: none.
  79. X *
  80. X * Notes
  81. X *        tinit calls tgetent, then sets up the global
  82. X *    variables PC, UP, BC, ospeed appropriately.
  83. X *
  84. X */
  85. X#include "termlib.h"
  86. X#include <stdio.h>
  87. X#if !AMIGA
  88. X#include <sgtty.h>
  89. X#endif
  90. X
  91. X/* tinit.c (libtermlib.a)
  92. X *
  93. X */
  94. X
  95. Xchar tbuf[1024];                                /* Buffer for termcap entry */
  96. Xchar junkbuf[1024];                                  /* Big buffer for junk */
  97. Xchar *junkptr;
  98. X
  99. Xtinit(name)
  100. Xchar *name;
  101. X{
  102. X#if !AMIGA
  103. X    struct sgttyb sgbuf;
  104. X#endif
  105. X    char *ps;
  106. X
  107. X    junkptr = junkbuf;
  108. X
  109. X    tgetent(tbuf, name);
  110. X
  111. X    ps = tgetstr("pc", &junkptr);
  112. X    if(ps) PC = *ps;
  113. X    UP = tgetstr("up", &junkptr);
  114. X    BC = tgetstr("bc", &junkptr);
  115. X
  116. X#if AMIGA
  117. X    ospeed=0;
  118. X#else
  119. X    gtty(1, &sgbuf);
  120. X    ospeed=sgbuf.sg_ospeed;
  121. X#endif
  122. X}
  123. //END
  124. echo x - tgetnum.c
  125. sed 's/^X//' > tgetnum.c << '//END'
  126. X/* TERMLIB: Terminal independant database.
  127. X *
  128. X * Module: tgetnum
  129. X *
  130. X * Purpose: get numeric value such as 'li' or 'co' from termcap.
  131. X *
  132. X * Calling conventions: id = 2 character id.
  133. X *
  134. X * Returned values: -1 for failure, else numerical value.
  135. X */
  136. X#include <stdio.h>
  137. X#include <ctype.h>
  138. X#include "termlib.h"
  139. X
  140. X/* tgetnum.c (libtermlib.a)
  141. X *
  142. X */
  143. X
  144. Xtgetnum(id)
  145. Xchar *id;
  146. X{
  147. X    char *ptr, buf[256];
  148. X    ptr = buf;
  149. X
  150. X    if(tgetstr(id, &ptr))
  151. X        return atoi(buf);
  152. X    else
  153. X        return 0;
  154. X}
  155. //END
  156. echo x - tutil.c
  157. sed 's/^X//' > tutil.c << '//END'
  158. X/* TERMLIB: Terminal independant database.
  159. X *
  160. X * Module: tutil.c
  161. X *
  162. X * Purpose: Utility routines for TERMLIB functions.
  163. X *
  164. X */
  165. X
  166. X/* tutil.c (libtermlib.a)
  167. X * Utility routines for termlib
  168. X *
  169. X */
  170. X
  171. X_match(s1, s2)                 /* returns length of text common to s1 and s2 */
  172. Xchar *s1, *s2;
  173. X{
  174. X    int i = 0;
  175. X
  176. X    while(s1[i] && s1[i] == s2[i])
  177. X        i++;
  178. X
  179. X    return i;
  180. X}
  181. X
  182. Xchar *
  183. X_find(s, set)   /* finds next c in s that's a member of set, returns pointer */
  184. Xchar *s, *set;
  185. X{
  186. X    for(; *s; s++) {
  187. X        char    *ptr = set;
  188. X
  189. X        while(*ptr && *s != *ptr)
  190. X            ptr++;
  191. X
  192. X        if(*ptr)
  193. X            return s;
  194. X    }
  195. X
  196. X    return s;
  197. X}
  198. X
  199. Xchar *
  200. X_addfmt(buf, fmt, val)             /* add val to buf according to format fmt */
  201. Xchar *buf, *fmt;
  202. Xint val;
  203. X{
  204. X    sprintf(buf, fmt, val);
  205. X    while(*buf)
  206. X        buf++;
  207. X    return buf;
  208. X}
  209. //END
  210. echo x - tvars.c
  211. sed 's/^X//' > tvars.c << '//END'
  212. X/* TERMLIB: Terminal independant database.
  213. X *
  214. X * Module: tvars
  215. X *
  216. X * Purpose: supply actual global variables.
  217. X */
  218. X
  219. X/* tvars.c (libtermlib.a)
  220. X * Global variables for termlib
  221. X *
  222. X*/
  223. X
  224. Xchar *tent;                     /* Pointer to terminal entry, set by tgetent */
  225. Xchar PC = 0;                                  /* Pad character, default NULL */
  226. Xchar *UP = 0, *BC = 0;        /* Pointers to UP and BC strings from database */
  227. Xshort ospeed;               /* Baud rate (1-16, 1=300, 16=19200), as in stty */
  228. //END
  229. echo x - tgoto.c
  230. sed 's/^X//' > tgoto.c << '//END'
  231. X/* TERMLIB: Terminal independant database.
  232. X *
  233. X * Module: tgoto
  234. X *
  235. X * Purpose: decode cm cursor motion string.
  236. X *
  237. X * Calling conventions: cm is cursor motion string.
  238. X *            line, col, are the desired destination.
  239. X *
  240. X * Returned values: a string pointing to the decoded string, or
  241. X *            "OOPS" if it cannot be decoded.
  242. X *
  243. X * Notes
  244. X *        The accepted escapes are:
  245. X *            %d     as in printf, 0 origin.
  246. X *            %2, %3     like %02d, %03d in printf.
  247. X *            %.     like %c
  248. X *            %+x     adds <x> to value, then %.
  249. X *            %>xy     if value>x, adds y. No output.
  250. X *            %i     increments line& col, no output.
  251. X *            %r     reverses order of line&col. No output.
  252. X *            %%     prints as a single %.
  253. X *            %n     exclusive or row & col with 0140.
  254. X *            %B     BCD, no output.
  255. X *            %D     reverse coding (x-2*(x%16)), no output.
  256. X */
  257. X#include <stdio.h>
  258. X#include <ctype.h>
  259. X#include "termlib.h"
  260. X
  261. X/* tgoto.c (libtermlib.a)
  262. X *
  263. X */
  264. X
  265. Xchar *
  266. Xtgoto(cm, col, line)
  267. Xchar    *cm;                                      /* cm string, from termcap */
  268. Xint    col,                                           /* column, x position */
  269. X    line;                                            /* line, y position */
  270. X{
  271. X    char    *_addfmt(),
  272. X        gx, gy,                                           /*    x, y */
  273. X        *ptr,                                     /* pointer in 'cm' */
  274. X        reverse = 0,                                 /* reverse flag */
  275. X        *bufp,                         /* pointer in returned string */
  276. X        addup = 0,                                     /* add upline */
  277. X        addbak = 0,                                    /* add backup */
  278. X        c;
  279. X    static char buffer[32];
  280. X
  281. X    if(!cm)
  282. X        return "OOPS";                       /* Kludge, but standard */
  283. X
  284. X    bufp = buffer;
  285. X    ptr = cm;
  286. X
  287. X    while(*ptr) {
  288. X        if((c = *ptr++) != '%') {                     /* normal char */
  289. X            *bufp++ = c;
  290. X        } else {                                         /* % escape */
  291. X            switch(c = *ptr++) {
  292. X            case 'd':                                 /* decimal */
  293. X                bufp = _addfmt(bufp, "%d", line);
  294. X                line = col;
  295. X                break;
  296. X            case '2':                         /* 2 digit decimal */
  297. X                bufp = _addfmt(bufp, "%02d", line);
  298. X                line = col;
  299. X                break;
  300. X            case '3':                         /* 3 digit decimal */
  301. X                bufp = _addfmt(bufp, "%03d", line);
  302. X                line = col;
  303. X                break;
  304. X            case '>':                      /* %>xy: if >x, add y */
  305. X                gx = *ptr++;
  306. X                gy = *ptr++;
  307. X                if(col>gx) col += gy;
  308. X                if(line>gx) line += gy;
  309. X                break;
  310. X            case '+':                              /* %+c: add c */
  311. X                line += *ptr++;
  312. X            case '.':                               /* print x/y */
  313. X                if(line=='\t' ||                /* these are */
  314. X                   line == '\n' ||             /* chars that */
  315. X                   line=='\004' ||             /* UNIX hates */
  316. X                   line=='\0') {
  317. X                    line++;         /* so go to next pos */
  318. X                    if(reverse==(line==col))
  319. X                        addup=1;      /* and mark UP */
  320. X                    else
  321. X                        addbak=1;           /* or BC */
  322. X                }
  323. X                *bufp++=line;
  324. X                line = col;
  325. X                break;
  326. X            case 'r':                              /* r: reverse */
  327. X                gx = line; 
  328. X                line = col; 
  329. X                col = gx;
  330. X                reverse = 1;
  331. X                break;
  332. X            case 'i':             /* increment (1-origin screen) */
  333. X                col++;
  334. X                line++;
  335. X                break;
  336. X            case '%':                          /* %%=% literally */
  337. X                *bufp++='%';
  338. X                break;
  339. X            case 'n':                       /* magic DM2500 code */
  340. X                line ^= 0140;
  341. X                col ^= 0140;
  342. X                break;
  343. X            case 'B':                            /* bcd encoding */
  344. X                line = line/10<<4+line%10;
  345. X                col = col/10<<4+col%10;
  346. X                break;
  347. X            case 'D':                   /* magic Delta Data code */
  348. X                line = line-2*(line&15);
  349. X                col = col-2*(col&15);
  350. X                break;
  351. X            default:                           /* Unknown escape */
  352. X                return "OOPS";
  353. X            }
  354. X        }
  355. X    }
  356. X
  357. X    if(addup)                                              /* add upline */
  358. X        if(UP) {
  359. X            ptr=UP;
  360. X            while(isdigit(*ptr) || *ptr=='.')
  361. X                ptr++;
  362. X            if(*ptr=='*')
  363. X                ptr++;
  364. X            while(*ptr)
  365. X                *bufp++ = *ptr++;
  366. X        }
  367. X
  368. X    if(addbak)                                          /* add backspace */
  369. X        if(BC) {
  370. X            ptr=BC;
  371. X            while(isdigit(*ptr) || *ptr=='.')
  372. X                ptr++;
  373. X            if(*ptr=='*')
  374. X                ptr++;
  375. X            while(*ptr)
  376. X                *bufp++ = *ptr++;
  377. X        } 
  378. X        else
  379. X            *bufp++='\b';
  380. X
  381. X    *bufp = 0;
  382. X
  383. X    return(buffer);
  384. X}
  385. //END
  386. echo x - tgetent.c
  387. sed 's/^X//' > tgetent.c << '//END'
  388. X/* TERMLIB: Terminal independant database.
  389. X *
  390. X * Module: tgetent
  391. X *
  392. X * Purpose: Get termcap entry for <term> into buffer at <tbuf>.
  393. X *
  394. X * Calling conventions: char tbuf[1024+], term=canonical name for
  395. X *            terminal.
  396. X *
  397. X * Returned values: 1 = success, -1 = can't open file,
  398. X *            0 = can't find terminal.
  399. X *
  400. X * Notes
  401. X *        Should probably supply static buffer.
  402. X *
  403. X *        Uses environment variables "TERM" and
  404. X *    "TERMCAP". If TERM = term (that is, if the argument
  405. X *    matches the environment) then it looks at TERMCAP.
  406. X *        If TERMCAP begins with a slash, then it assumes
  407. X *    this is the file to search rather than /etc/termcap.
  408. X *        If TERMCAP does not begin with a slash, and it
  409. X *    matches TERM, then this is used as the entry.
  410. X *
  411. X *        This could be simplified considerably for non-UNIX
  412. X *    systems.
  413. X */
  414. X#include <stdio.h>
  415. X#include <ctype.h>
  416. X#include "termlib.h"
  417. X
  418. X/* tgetent.c (libtermlib.a)
  419. X *
  420. X */
  421. X#if AMIGA
  422. X#define TERMCAP "s:termcap"
  423. X#else
  424. X#define TERMCAP "/etc/termcap"
  425. X#endif
  426. X
  427. Xtgetent(tbuf, term)
  428. Xchar    *tbuf,               /* Buffer to hold termcap entry, 1024 bytes max */
  429. X    *term;                                           /* Name of terminal */
  430. X{
  431. X    char    tcbuf[32],                          /* Temp buffer to handle */
  432. X        *tc,                                     /* :tc=: entry for  */
  433. X        *tcptr = tcbuf;                          /* extended entries */
  434. X    char    *tcap = TERMCAP,              /* Default termcap file */
  435. X        *getenv();
  436. X    char    *tmp;
  437. X    FILE    *termcap;
  438. X
  439. X    if((tmp=getenv("TERMCAP")) != NULL) {
  440. X        if(*tmp == '/')           /* TERMCAP = name of termcap file */
  441. X            tcap = tmp ;
  442. X        else {                    /* TERMCAP = termcap entry itself */
  443. X            int tlen = strlen(term);
  444. X            while(*tmp && *tmp != ':') {/* Check if TERM matches */
  445. X                while(*tmp == '|')
  446. X                    tmp++;
  447. X                if(_match(tmp, term)==tlen) {
  448. X                    strcpy(tbuf, tmp);
  449. X                    tent=tbuf;
  450. X                    return 1;
  451. X                } 
  452. X                else
  453. X                    tmp = _find(tmp, ":|");
  454. X            }
  455. X        }
  456. X    }
  457. X    if(!(termcap=fopen(tcap, "r"))) {
  458. X        strcpy(tbuf, tcap);
  459. X        return -1;
  460. X    }
  461. X
  462. X    if(getent(tbuf, term, termcap)) {
  463. X        if(tc=tgetstr("tc", &tcptr)) {              /* extended entry */
  464. X            rewind(termcap);
  465. X            if(getent(tbuf+strlen(tbuf), tc, termcap)) { 
  466. X                fclose(termcap);               /* Completed */
  467. X                return 1; 
  468. X            }
  469. X            else { 
  470. X                fclose(termcap);              /* Incomplete */
  471. X                return 0; 
  472. X            }
  473. X        } else { 
  474. X            fclose(termcap);              /* non-extended entry */
  475. X            return 1; 
  476. X        }
  477. X    } else { 
  478. X        fclose(termcap);                                /* No entry */
  479. X        return 0; 
  480. X    }
  481. X}
  482. X
  483. Xgetent(tbuf, term, termcap)
  484. Xchar *tbuf, *term;
  485. XFILE *termcap;
  486. X{
  487. X    char    *tptr;
  488. X    int    tlen = strlen(term);
  489. X
  490. X    while(nextent(tbuf, termcap)) {           /* For each possible entry */
  491. X        tptr = tbuf;
  492. X        while(*tptr && *tptr != ':') {    /* : terminates name field */
  493. X            while(*tptr == '|')             /* | seperates names */
  494. X                tptr++;
  495. X            if(_match(tptr, term)==tlen) {             /* FOUND! */
  496. X                fclose(termcap);
  497. X                tent=tbuf;
  498. X                return 1;
  499. X            } 
  500. X            else                           /* Look for next name */
  501. X                tptr = _find(tptr, ":|");
  502. X        }
  503. X    }
  504. X
  505. X    return 0;
  506. X}
  507. X
  508. Xnextent(tbuf, termcap)                     /* Read 1 entry from TERMCAP file */
  509. Xchar    *tbuf;
  510. XFILE    *termcap;
  511. X{
  512. X    char *lbuf =                                     /* lbuf=line buffer */
  513. X         tbuf;                        /* read lines straight into buffer */
  514. X
  515. X    while(lbuf < tbuf+1024 &&                        /* There's room and */
  516. X          fgets(lbuf, tbuf+1024-lbuf, termcap)) {        /* another line */
  517. X        int llen = strlen(lbuf);
  518. X
  519. X        if(*lbuf=='#')                               /* eat comments */
  520. X            continue;
  521. X        if(lbuf[-1]==':' &&                        /* and whitespace */
  522. X           lbuf[0]=='\t' &&
  523. X           lbuf[1]==':') {
  524. X            strcpy(lbuf, lbuf+2);
  525. X            llen -= 2;
  526. X        }
  527. X        if(lbuf[llen-2]=='\\')                  /* and continuations */
  528. X            lbuf += llen-2;
  529. X        else {
  530. X            lbuf[llen-1]=0;           /* no continuation, return */
  531. X            return 1;
  532. X        }
  533. X    }
  534. X
  535. X    return 0;                                    /* ran into end of file */
  536. X}
  537. //END
  538. echo x - tputs.c
  539. sed 's/^X//' > tputs.c << '//END'
  540. X/* TERMLIB: Terminal independant database.
  541. X *
  542. X * Module: tputs
  543. X *
  544. X * Purpose: decode padding information
  545. X *
  546. X * Calling conventions: cp = string to be padded, affcnt = # of items
  547. X *            affected (lines, characters, whatever),
  548. X *            outc = routine to output 1 character.
  549. X *
  550. X * Returned values: none
  551. X *
  552. X * Notes
  553. X *        cp has padding information ahead of it, in the form
  554. X *    nnnTEXT or nnn*TEXT. nnn is the number of milliseconds to delay,
  555. X *    and may be a decimal (nnn.mmm). If the asterisk is given, then
  556. X *    the delay is multiplied by afcnt. The delay is produced by outputting
  557. X *    a number of nulls (or other padding char) after printing the
  558. X *    TEXT.
  559. X *
  560. X */
  561. X#include <stdio.h>
  562. X#include <ctype.h>
  563. X#include "termlib.h"
  564. X
  565. X/* tputs.c (libtermlib.a)
  566. X *
  567. X */
  568. X
  569. Xlong _bauds[16]={
  570. X    0,    50,    75,    110,
  571. X    134,    150,    200,    300,
  572. X    600,    1200,    1800,    2400,
  573. X    4800,    9600,    19200,    19200 };
  574. X
  575. Xtputs(cp, affcnt, outc)
  576. Xchar *cp;                                                 /* string to print */
  577. Xint affcnt;                                      /* Number of lines affected */
  578. Xint (*outc)();                              /* routine to output 1 character */
  579. X{
  580. X    long    frac,                    /* 10^(#digits after decimal point) */
  581. X        counter,                                           /* digits */
  582. X        atol();
  583. X
  584. X    if(isdigit(*cp)) {
  585. X        counter = 0;
  586. X        frac = 1000;
  587. X        while(isdigit(*cp))
  588. X            counter = counter * 10L + (long)(*cp++ - '0');
  589. X        if(*cp=='.')
  590. X            while(isdigit(*++cp)) {
  591. X                counter = counter * 10L + (long)(*cp++ - '0');
  592. X                frac = frac * 10;
  593. X            }
  594. X        if(*cp!='*') {                 /* multiply by affected lines */
  595. X            if(affcnt>1) affcnt = 1;
  596. X        } 
  597. X        else
  598. X            cp++;
  599. X
  600. X        /* Calculate number of characters for padding counter/frac ms delay */
  601. X        if(ospeed)
  602. X            counter = (counter * _bauds[ospeed] * (long)affcnt) / frac;
  603. X
  604. X        while(*cp)                                  /* output string */
  605. X            (*outc)(*cp++);
  606. X        if(ospeed)
  607. X            while(counter--)            /* followed by pad characters */
  608. X                (*outc)(PC);
  609. X    } 
  610. X    else
  611. X        while(*cp)
  612. X            (*outc)(*cp++);
  613. X}
  614. //END
  615. echo x - cur.c
  616. sed 's/^X//' > cur.c << '//END'
  617. X/* Cur: Provide cursor addressing for shell scripts.
  618. X *
  619. X * Cur performs the same functions as echo, with 2 differences:
  620. X *  1. 
  621. X *     a. Arguments of the form -xx result in the generation of the capability
  622. X *        string xx. The two special strings "cm" and "cs" are handled by
  623. X *        cur ... -cm x y ... and ... -cs lo hi ...
  624. X *     b. Arguments of the form #xx return the value of the numeric capability
  625. X *        xx.
  626. X *     c. Arguments of the form +terminal force cur to assume that as the
  627. X *        terminal type. Any number of these can be included, and will be
  628. X *        evaluated when encountered.
  629. X *  2. No newline is appended to the echoed string.
  630. X *
  631. X * Syntax: cur [string|-xx|#xx|+term]...
  632. X *
  633. X * Other notes: The code is obvious.
  634. X */
  635. X#include <stdio.h>
  636. X#include "termlib.h"
  637. X
  638. Xextern char *junkptr;
  639. X
  640. Xmain(ac, av)
  641. Xint ac; char **av;
  642. X{
  643. X    int line, col, outch();
  644. X    char *val;
  645. X
  646. X    tinit(getenv("TERM"));
  647. X
  648. X    while(--ac)
  649. X        if(**++av=='-') {
  650. X            if(val=tgetstr(*av+1, &junkptr)) {
  651. X                if(strcmp(*av+1, "cm") &&
  652. X                   strcmp(*av+1, "cs"))
  653. X                    tputs(val, 1, outch);
  654. X                else {
  655. X                    col = atoi(*++av); --ac;
  656. X                    line = atoi(*++av); --ac;
  657. X                    tputs(tgoto(val, col, line), 0, outch);
  658. X                }
  659. X            }
  660. X        } else if(**av=='#') {
  661. X            if(val = tgetstr(*av+1, &junkptr))
  662. X                fputs(val, stdout);
  663. X        } else if(**av=='+') {
  664. X            tinit(*av+1);
  665. X        } else
  666. X            fputs(*av, stdout);
  667. X}
  668. X
  669. Xoutch(c) char c; { putchar(c); }
  670. //END
  671. echo x - termcap.c
  672. sed 's/^X//' > termcap.c << '//END'
  673. X/* termcap... print current terminal capabilities.
  674. X *
  675. X * Termcap prints all the termcap capability strings for the terminal `term'.
  676. X * The output is in machine readable form, suitable for use in the construct:
  677. X *
  678. X *    TERMCAP="`termcap`"; export TERMCAP
  679. X *
  680. X * Syntax: termcap [term]
  681. X */
  682. X#include <stdio.h>
  683. X
  684. Xchar *tent;
  685. Xmain(ac, av)
  686. Xint ac;
  687. Xchar **av;
  688. X{
  689. X    char tbuf[1024];
  690. X    if(ac==1) if(tgetent(tbuf, getenv("TERM"))) {
  691. X        puts(tbuf);
  692. X        exit(0);
  693. X    } else exit(-1);
  694. X    if(tgetent(tbuf, av[1])) {
  695. X        puts(tbuf);
  696. X        exit(0);
  697. X    } else exit(-1);
  698. X}
  699. //END
  700. echo x - tgetflag.c
  701. sed 's/^X//' > tgetflag.c << '//END'
  702. X/* The following software is (C) 1984 Peter da Silva,
  703. X   the Mad Australian, in the public domain. It may
  704. X   be re-distributed for any purpose with the inclusion
  705. X   of this notice. */
  706. X/* TERMLIB: Terminal independant database.
  707. X *
  708. X * Module: tgetflag
  709. X *
  710. X * Purpose: returns flag true or false as to the existence of a given
  711. X *        entry. used with 'bs', 'am', etc...
  712. X *
  713. X * Calling conventions: id is the 2 character capability id.
  714. X *
  715. X * Returned values: 1 for success, 0 for failure.
  716. X */
  717. X#include <stdio.h>
  718. X#include <ctype.h>
  719. X#include "termlib.h"
  720. X
  721. X/* tgetflag.c (libtermlib.a)
  722. X *
  723. XBUILD
  724. XOBJS=tinit.o tutil.o tvars.o \
  725. X     tgoto.o tputs.o tgetent.o tgetflag.o tgetnum.o tgetstr.o
  726. XCFLAGS=-O
  727. X
  728. Xlibtermlib.a: $(OBJS) termlib.h
  729. X    ar cr libtermlib.a $(OBJS)
  730. X    ranlib libtermlib.a
  731. X
  732. X$(OBJS):: termlib.h
  733. XEND
  734. X*/
  735. X
  736. Xtgetflag(id)
  737. Xchar *id;
  738. X{
  739. X    char    buf[256], *ptr = buf;
  740. X
  741. X    return tgetstr(id, &ptr) ? 1 : 0;
  742. X}
  743. //END
  744. echo x - tgetstr.c
  745. sed 's/^X//' > tgetstr.c << '//END'
  746. X/* The following software is (C) 1984 Peter da Silva,
  747. X   the Mad Australian, in the public domain. It may
  748. X   be re-distributed for any purpose with the inclusion
  749. X   of this notice. */
  750. X/* TERMLIB: Terminal independant database.
  751. X *
  752. X * Module: tgetstr
  753. X *
  754. X * Purpose: get terminal capability string from database.
  755. X *
  756. X * Calling conventions: id is the two character capability id.
  757. X *            (*buf) points into a hold buffer for the
  758. X *            id. the capability is copied into the buffer
  759. X *            and (*buf) is advanced to point to the next
  760. X *            free byte in the buffer.
  761. X *
  762. X * Returned values: 0 = no such entry, otherwise returns original
  763. X *            (*buf) (now a pointer to the string).
  764. X *
  765. X * Notes
  766. X *        It also decodes certain escape sequences in the buffer.
  767. X *    they should be obvious from the code:
  768. X *        \E = escape.
  769. X *        \n, \r, \t, \f, \b match the 'c' escapes.
  770. X *        ^x matches control-x (^@...^_).
  771. X *        \nnn matches nnn octal.
  772. X *        \x, where x is anything else, matches x. I differ
  773. X *    from the standard library here, in that I allow ^: to match
  774. X *    :.
  775. X *
  776. X */
  777. X#include <stdio.h>
  778. X#include <ctype.h>
  779. X#include "termlib.h"
  780. X
  781. X/* tgetstr.c (libtermlib.a)
  782. X *
  783. XBUILD
  784. XOBJS=tinit.o tutil.o tvars.o \
  785. X     tgoto.o tputs.o tgetent.o tgetflag.o tgetnum.o tgetstr.o
  786. XCFLAGS=-O
  787. X
  788. Xlibtermlib.a: $(OBJS) termlib.h
  789. X    ar cr libtermlib.a $(OBJS)
  790. X    ranlib libtermlib.a
  791. X
  792. X$(OBJS):: termlib.h
  793. XEND
  794. X*/
  795. X
  796. Xchar *
  797. Xtgetstr(id, buf)
  798. Xchar    *id, **buf;
  799. X{
  800. X    int    len = strlen(id);
  801. X    char *tmp=tent;
  802. X    char *hold;
  803. X
  804. X    do {
  805. X        tmp = _find(tmp, ":");                     /* For each field */
  806. X        while(*tmp==':')                        /* skip empty fields */
  807. X            tmp++;
  808. X        if(!*tmp)
  809. X            break;
  810. X
  811. X        if(_match(id, tmp)==len) {
  812. X            tmp += len;                   /* find '=' '@' or '#' */
  813. X            if(*tmp=='@')                  /* :xx@: entry for tc */
  814. X                return 0;                   /* deleted entry */
  815. X            hold= *buf;
  816. X            while(*++tmp && *tmp != ':') {/* not at end of field */
  817. X                switch(*tmp) {
  818. X                case '\\':            /* Expand escapes here */
  819. X                    switch(*++tmp) {
  820. X                    case 0:        /* ignore backslashes */
  821. X                        tmp--;    /* at end of entry */
  822. X                        break;   /* shouldn't happen */
  823. X                    case 'e':
  824. X                    case 'E':                     /* ESC */
  825. X                        *(*buf)++ = '\033'; 
  826. X                        break;
  827. X                    case 'n':                      /* \n */
  828. X                        *(*buf)++ = '\n'; 
  829. X                        break;
  830. X                    case 'r':                      /* \r */
  831. X                        *(*buf)++ = '\r'; 
  832. X                        break;
  833. X                    case 't':                      /* \t */
  834. X                        *(*buf)++ = '\t'; 
  835. X                        break;
  836. X                    case 'b':                      /* \b */
  837. X                        *(*buf)++ = '\b'; 
  838. X                        break;
  839. X                    case 'f':                      /* \f */
  840. X                        *(*buf)++ = '\f'; 
  841. X                        break;
  842. X                    case '0':                    /* \nnn */
  843. X                    case '1': 
  844. X                    case '2': 
  845. X                    case '3': 
  846. X                    case '4':
  847. X                    case '5': 
  848. X                    case '6': 
  849. X                    case '7': 
  850. X                    case '8': 
  851. X                    case '9':
  852. X                        **buf = 0;
  853. X                        while(isdigit(*tmp))
  854. X                            **buf = **buf * 8 + *tmp++ - '0';
  855. X                        (*buf)++;
  856. X                        tmp--;
  857. X                        break;
  858. X                    default:      /* \x, for all other x */
  859. X                        *(*buf)++= *tmp;
  860. X                    }
  861. X                    break;
  862. X                case '^':              /* control characters */
  863. X                    *(*buf)++ = *++tmp - '@'; 
  864. X                    break;
  865. X                default: 
  866. X                    *(*buf)++ = *tmp;
  867. X                }
  868. X            }
  869. X            *(*buf)++ = 0;
  870. X            return hold;
  871. X        }
  872. X    } while(*tmp);
  873. X
  874. X    return 0;
  875. X}
  876. //END
  877. echo x - Makefile
  878. sed 's/^X//' > Makefile << '//END'
  879. X#
  880. X# The following module order is needed to create a properly sorted library
  881. X# for Manx
  882. X#
  883. X
  884. XOBJS=\
  885. X    tgetflag.o\
  886. X    tgetnum.o\
  887. X    tinit.o\
  888. X    tgoto.o\
  889. X    tputs.o\
  890. X    tvars.o\
  891. X    tgetent.o\
  892. X    tgetstr.o\
  893. X    tutil.o
  894. XSRC=\
  895. X    termlib.h\
  896. X    tinit.c\
  897. X    tgetnum.c\
  898. X    tutil.c\
  899. X    tvars.c\
  900. X    tgoto.c\
  901. X    tgetent.c\
  902. X    tputs.c\
  903. X    cur.c\
  904. X    termcap.c\
  905. X    tgetflag.c\
  906. X    tgetstr.c\
  907. X    Makefile
  908. X
  909. XCFLAGS= +P -B -DAMIGA=1
  910. XLIB=termlibl32.lib
  911. X
  912. XLIBDIR=manx:lib
  913. XBINDIR=work:c
  914. X
  915. Xall: cur termcap
  916. X
  917. X$(LIB): $(OBJS)
  918. X    lb $(LIB) $(OBJS)
  919. X
  920. Xtermcap: $(LIB) termcap.o
  921. X    ln -o termcap termcap.o $(LIB) -lcl32
  922. X
  923. Xcur: $(LIB) cur.o
  924. X    ln -o cur cur.o $(LIB) -lcl32
  925. X
  926. Xclean:
  927. X    delete #?.o #?.lib termcap cur #?.bak
  928. X
  929. Xinstall: all
  930. X    copy $(LIB) $(LIBDIR)
  931. X    copy termcap $(BINDIR)
  932. X    copy cur $(BINDIR)
  933. X
  934. Xtermlib.shar: $(SRC)
  935. X    shar >termlib.shar $(SRC)
  936. //END
  937. : end of archive.
  938. exit 0
  939. -- 
  940. Peter da Silva.   `-_-'
  941. <peter@sugar.hackercorp.com>.
  942.