home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3057 < prev    next >
Encoding:
Internet Message Format  |  1991-03-15  |  21.9 KB

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