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

  1. /* The following software is (C) 1984 Peter da Silva,
  2.    the Mad Australian, in the public domain. It may
  3.    be re-distributed for any purpose with the inclusion
  4.    of this notice. */
  5. /* TERMLIB: Terminal independant database.
  6.  *
  7.  * Module: tgetstr
  8.  *
  9.  * Purpose: get terminal capability string from database.
  10.  *
  11.  * Calling conventions: id is the two character capability id.
  12.  *            (*buf) points into a hold buffer for the
  13.  *            id. the capability is copied into the buffer
  14.  *            and (*buf) is advanced to point to the next
  15.  *            free byte in the buffer.
  16.  *
  17.  * Returned values: 0 = no such entry, otherwise returns original
  18.  *            (*buf) (now a pointer to the string).
  19.  *
  20.  * Notes
  21.  *        It also decodes certain escape sequences in the buffer.
  22.  *    they should be obvious from the code:
  23.  *        \E = escape.
  24.  *        \n, \r, \t, \f, \b match the 'c' escapes.
  25.  *        ^x matches control-x (^@...^_).
  26.  *        \nnn matches nnn octal.
  27.  *        \x, where x is anything else, matches x. I differ
  28.  *    from the standard library here, in that I allow ^: to match
  29.  *    :.
  30.  *
  31.  */
  32. #include <stdio.h>
  33. #include <ctype.h>
  34. #include "termlib.h"
  35.  
  36. /* tgetstr.c (libtermlib.a)
  37.  *
  38. BUILD
  39. OBJS=tinit.o tutil.o tvars.o \
  40.      tgoto.o tputs.o tgetent.o tgetflag.o tgetnum.o tgetstr.o
  41. CFLAGS=-O
  42.  
  43. libtermlib.a: $(OBJS) termlib.h
  44.     ar cr libtermlib.a $(OBJS)
  45.     ranlib libtermlib.a
  46.  
  47. $(OBJS):: termlib.h
  48. END
  49. */
  50.  
  51. char *
  52. tgetstr(id, buf)
  53. char    *id, **buf;
  54. {
  55.     int    len = strlen(id);
  56.     char *tmp=tent;
  57.     char *hold;
  58.  
  59.     do {
  60.         tmp = _find(tmp, ":");                     /* For each field */
  61.         while(*tmp==':')                        /* skip empty fields */
  62.             tmp++;
  63.         if(!*tmp)
  64.             break;
  65.  
  66.         if(_match(id, tmp)==len) {
  67.             tmp += len;                   /* find '=' '@' or '#' */
  68.             if(*tmp=='@')                  /* :xx@: entry for tc */
  69.                 return 0;                   /* deleted entry */
  70.             hold= *buf;
  71.             while(*++tmp && *tmp != ':') {/* not at end of field */
  72.                 switch(*tmp) {
  73.                 case '\\':            /* Expand escapes here */
  74.                     switch(*++tmp) {
  75.                     case 0:        /* ignore backslashes */
  76.                         tmp--;    /* at end of entry */
  77.                         break;   /* shouldn't happen */
  78.                     case 'e':
  79.                     case 'E':                     /* ESC */
  80.                         *(*buf)++ = '\033'; 
  81.                         break;
  82.                     case 'n':                      /* \n */
  83.                         *(*buf)++ = '\n'; 
  84.                         break;
  85.                     case 'r':                      /* \r */
  86.                         *(*buf)++ = '\r'; 
  87.                         break;
  88.                     case 't':                      /* \t */
  89.                         *(*buf)++ = '\t'; 
  90.                         break;
  91.                     case 'b':                      /* \b */
  92.                         *(*buf)++ = '\b'; 
  93.                         break;
  94.                     case 'f':                      /* \f */
  95.                         *(*buf)++ = '\f'; 
  96.                         break;
  97.                     case '0':                    /* \nnn */
  98.                     case '1': 
  99.                     case '2': 
  100.                     case '3': 
  101.                     case '4':
  102.                     case '5': 
  103.                     case '6': 
  104.                     case '7': 
  105.                     case '8': 
  106.                     case '9':
  107.                         **buf = 0;
  108.                         while(isdigit(*tmp))
  109.                             **buf = **buf * 8 + *tmp++ - '0';
  110.                         (*buf)++;
  111.                         tmp--;
  112.                         break;
  113.                     default:      /* \x, for all other x */
  114.                         *(*buf)++= *tmp;
  115.                     }
  116.                     break;
  117.                 case '^':              /* control characters */
  118.                     *(*buf)++ = *++tmp - '@'; 
  119.                     break;
  120.                 default: 
  121.                     *(*buf)++ = *tmp;
  122.                 }
  123.             }
  124.             *(*buf)++ = 0;
  125.             return hold;
  126.         }
  127.     } while(*tmp);
  128.  
  129.     return 0;
  130. }
  131.