home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 419b.lha / TERMLIB / tgetent.c < prev    next >
C/C++ Source or Header  |  1990-10-05  |  4KB  |  150 lines

  1. /* TERMLIB: Terminal independant database.
  2.  *
  3.  * Module: tgetent
  4.  *
  5.  * Purpose: Get termcap entry for <term> into buffer at <tbuf>.
  6.  *
  7.  * Calling conventions: char tbuf[1024+], term=canonical name for
  8.  *            terminal.
  9.  *
  10.  * Returned values: 1 = success, -1 = can't open file,
  11.  *            0 = can't find terminal.
  12.  *
  13.  * Notes
  14.  *        Should probably supply static buffer.
  15.  *
  16.  *        Uses environment variables "TERM" and
  17.  *    "TERMCAP". If TERM = term (that is, if the argument
  18.  *    matches the environment) then it looks at TERMCAP.
  19.  *        If TERMCAP begins with a slash, then it assumes
  20.  *    this is the file to search rather than /etc/termcap.
  21.  *        If TERMCAP does not begin with a slash, and it
  22.  *    matches TERM, then this is used as the entry.
  23.  *
  24.  *        This could be simplified considerably for non-UNIX
  25.  *    systems.
  26.  */
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #include "termlib.h"
  30.  
  31. /* tgetent.c (libtermlib.a)
  32.  *
  33.  */
  34. #if AMIGA
  35. #define TERMCAP "s:termcap"
  36. #else
  37. #define TERMCAP "/etc/termcap"
  38. #endif
  39.  
  40. tgetent(tbuf, term)
  41. char    *tbuf,               /* Buffer to hold termcap entry, 1024 bytes max */
  42.     *term;                                           /* Name of terminal */
  43. {
  44.     char    tcbuf[32],                          /* Temp buffer to handle */
  45.         *tc,                                     /* :tc=: entry for  */
  46.         *tcptr = tcbuf;                          /* extended entries */
  47.     char    *tcap = TERMCAP,              /* Default termcap file */
  48.         *getenv();
  49.     char    *tmp;
  50.     FILE    *termcap;
  51.  
  52.     if((tmp=getenv("TERMCAP")) != NULL) {
  53.         if(*tmp == '/')           /* TERMCAP = name of termcap file */
  54.             tcap = tmp ;
  55.         else {                    /* TERMCAP = termcap entry itself */
  56.             int tlen = strlen(term);
  57.             while(*tmp && *tmp != ':') {/* Check if TERM matches */
  58.                 while(*tmp == '|')
  59.                     tmp++;
  60.                 if(_match(tmp, term)==tlen) {
  61.                     strcpy(tbuf, tmp);
  62.                     tent=tbuf;
  63.                     return 1;
  64.                 } 
  65.                 else
  66.                     tmp = _find(tmp, ":|");
  67.             }
  68.         }
  69.     }
  70.     if(!(termcap=fopen(tcap, "r"))) {
  71.         strcpy(tbuf, tcap);
  72.         return -1;
  73.     }
  74.  
  75.     if(getent(tbuf, term, termcap)) {
  76.         if(tc=tgetstr("tc", &tcptr)) {              /* extended entry */
  77.             rewind(termcap);
  78.             if(getent(tbuf+strlen(tbuf), tc, termcap)) { 
  79.                 fclose(termcap);               /* Completed */
  80.                 return 1; 
  81.             }
  82.             else { 
  83.                 fclose(termcap);              /* Incomplete */
  84.                 return 0; 
  85.             }
  86.         } else { 
  87.             fclose(termcap);              /* non-extended entry */
  88.             return 1; 
  89.         }
  90.     } else { 
  91.         fclose(termcap);                                /* No entry */
  92.         return 0; 
  93.     }
  94. }
  95.  
  96. getent(tbuf, term, termcap)
  97. char *tbuf, *term;
  98. FILE *termcap;
  99. {
  100.     char    *tptr;
  101.     int    tlen = strlen(term);
  102.  
  103.     while(nextent(tbuf, termcap)) {           /* For each possible entry */
  104.         tptr = tbuf;
  105.         while(*tptr && *tptr != ':') {    /* : terminates name field */
  106.             while(*tptr == '|')             /* | seperates names */
  107.                 tptr++;
  108.             if(_match(tptr, term)==tlen) {             /* FOUND! */
  109.                 fclose(termcap);
  110.                 tent=tbuf;
  111.                 return 1;
  112.             } 
  113.             else                           /* Look for next name */
  114.                 tptr = _find(tptr, ":|");
  115.         }
  116.     }
  117.  
  118.     return 0;
  119. }
  120.  
  121. nextent(tbuf, termcap)                     /* Read 1 entry from TERMCAP file */
  122. char    *tbuf;
  123. FILE    *termcap;
  124. {
  125.     char *lbuf =                                     /* lbuf=line buffer */
  126.          tbuf;                        /* read lines straight into buffer */
  127.  
  128.     while(lbuf < tbuf+1024 &&                        /* There's room and */
  129.           fgets(lbuf, tbuf+1024-lbuf, termcap)) {        /* another line */
  130.         int llen = strlen(lbuf);
  131.  
  132.         if(*lbuf=='#')                               /* eat comments */
  133.             continue;
  134.         if(lbuf[-1]==':' &&                        /* and whitespace */
  135.            lbuf[0]=='\t' &&
  136.            lbuf[1]==':') {
  137.             strcpy(lbuf, lbuf+2);
  138.             llen -= 2;
  139.         }
  140.         if(lbuf[llen-2]=='\\')                  /* and continuations */
  141.             lbuf += llen-2;
  142.         else {
  143.             lbuf[llen-1]=0;           /* no continuation, return */
  144.             return 1;
  145.         }
  146.     }
  147.  
  148.     return 0;                                    /* ran into end of file */
  149. }
  150.