[BACK]Return to term.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libterminfo

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/lib/libterminfo/term.c between version 1.7 and 1.13.2.1

version 1.7, 2010/02/11 13:11:47 version 1.13.2.1, 2012/10/30 18:59:18
Line 1 
Line 1 
 /* $NetBSD$ */  /* $NetBSD$ */
   
 /*  /*
  * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.   * Copyright (c) 2009, 2010, 2011 The NetBSD Foundation, Inc.
  *   *
  * This code is derived from software contributed to The NetBSD Foundation   * This code is derived from software contributed to The NetBSD Foundation
  * by Roy Marples.   * by Roy Marples.
Line 33  __RCSID("$NetBSD$");
Line 33  __RCSID("$NetBSD$");
 #include <sys/stat.h>  #include <sys/stat.h>
   
 #include <assert.h>  #include <assert.h>
   #include <cdbr.h>
 #include <ctype.h>  #include <ctype.h>
 #include <errno.h>  #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #include <limits.h>  #include <limits.h>
 #include <ndbm.h>  
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
 #include <term_private.h>  #include <term_private.h>
 #include <term.h>  #include <term.h>
   
 #define TERMINFO_DIRS           "/usr/share/misc/terminfo"  #define _PATH_TERMINFO          "/usr/share/misc/terminfo"
   
 static char database[PATH_MAX];  static char database[PATH_MAX];
 static char pathbuf[PATH_MAX];  static char pathbuf[PATH_MAX];
 const char *_ti_database;  const char *_ti_database;
   
 /* rescue.c is generated from /usr/src/share/terminfo/terminfo  /* Include a generated list of pre-compiled terminfo descriptions. */
  * at build time as an array of strings.  #include "compiled_terms.c"
  * static const char *rescue_terms[] = {  
  *      "ansi",  
  *      "\002\005\000\141\156\163\151\000\000\000\043\000\141\156\163\151\057",  
  *      NULL,  
  *      NULL  
  * };  
  */  
   
 #include "rescue.c"  
   
 static int  static int
 _ti_readterm(TERMINAL *term, const char *cap, size_t caplen, int flags)  _ti_readterm(TERMINAL *term, const char *cap, size_t caplen, int flags)
Line 71  _ti_readterm(TERMINAL *term, const char 
Line 62  _ti_readterm(TERMINAL *term, const char 
         TERMUSERDEF *ud;          TERMUSERDEF *ud;
   
         ver = *cap++;          ver = *cap++;
         /* Only read version 1 and 2 structures */          /* Only read version 1 structures */
         if (ver != 1 && ver != 2) {          if (ver != 1) {
                 errno = EINVAL;                  errno = EINVAL;
                 return -1;                  return -1;
         }          }
   
         term->flags = calloc(TIFLAGMAX + 1, sizeof(char));          term->flags = calloc(TIFLAGMAX + 1, sizeof(char));
         if (term->flags == NULL)          if (term->flags == NULL)
                 goto err;                  return -1;
         term->nums = malloc((TINUMMAX + 1) * sizeof(short));          term->nums = malloc((TINUMMAX + 1) * sizeof(short));
         if (term->nums == NULL)          if (term->nums == NULL)
                 goto err;                  return -1;
         memset(term->nums, (short)-1, (TINUMMAX + 1) * sizeof(short));          memset(term->nums, (short)-1, (TINUMMAX + 1) * sizeof(short));
         term->strs = calloc(TISTRMAX + 1, sizeof(char *));          term->strs = calloc(TISTRMAX + 1, sizeof(char *));
         if (term->strs == NULL)          if (term->strs == NULL)
                 goto err;                  return -1;
         term->_arealen = caplen;          term->_arealen = caplen;
         term->_area = malloc(term->_arealen);          term->_area = malloc(term->_arealen);
         if (term->_area == NULL)          if (term->_area == NULL)
                 goto err;                  return -1;
         memcpy(term->_area, cap, term->_arealen);          memcpy(term->_area, cap, term->_arealen);
   
         cap = term->_area;          cap = term->_area;
Line 98  _ti_readterm(TERMINAL *term, const char 
Line 89  _ti_readterm(TERMINAL *term, const char 
         cap += sizeof(uint16_t);          cap += sizeof(uint16_t);
         term->name = cap;          term->name = cap;
         cap += len;          cap += len;
         if (ver == 1)          len = le16dec(cap);
           cap += sizeof(uint16_t);
           if (len == 0)
                 term->_alias = NULL;                  term->_alias = NULL;
         else {          else {
                 len = le16dec(cap);                  term->_alias = cap;
                 cap += sizeof(uint16_t);                  cap += len;
                 if (len == 0)  
                         term->_alias = NULL;  
                 else {  
                         term->_alias = cap;  
                         cap += len;  
                 }  
         }          }
         len = le16dec(cap);          len = le16dec(cap);
         cap += sizeof(uint16_t);          cap += sizeof(uint16_t);
Line 214  _ti_readterm(TERMINAL *term, const char 
Line 201  _ti_readterm(TERMINAL *term, const char 
                                 break;                                  break;
                         default:                          default:
                                 errno = EINVAL;                                  errno = EINVAL;
                                 goto err;                                  return -1;
                         }                          }
                 }                  }
         }          }
         return 1;          return 1;
   
 err:  
         _ti_freeterm(term);  
         return -1;  
 }  }
   
 static int  static int
 _ti_dbgetterm(TERMINAL *term, const char *path, const char *name, int flags)  _ti_dbgetterm(TERMINAL *term, const char *path, const char *name, int flags)
 {  {
         DBM *db;          struct cdbr *db;
         datum dt;          const void *data;
         char *p;          char *db_name;
           const uint8_t *data8;
           size_t len, klen;
         int r;          int r;
   
         db = dbm_open(path, O_RDONLY, 0644);          if (asprintf(&db_name, "%s.cdb", path) < 0)
                   return -1;
   
           db = cdbr_open(db_name, CDBR_DEFAULT);
           free(db_name);
         if (db == NULL)          if (db == NULL)
                 return -1;                  return -1;
   
           klen = strlen(name) + 1;
           if (cdbr_find(db, name, klen, &data, &len) == -1)
                   goto fail;
           data8 = data;
           if (len == 0)
                   goto fail;
           /* Check for alias first, fall through to processing normal entries. */
           if (data8[0] == 2) {
                   if (klen + 7 > len || le16dec(data8 + 5) != klen)
                           goto fail;
                   if (memcmp(data8 + 7, name, klen))
                           goto fail;
                   if (cdbr_get(db, le32dec(data8 + 1), &data, &len))
                           goto fail;
                   data8 = data;
                   if (data8[0] != 1)
                           goto fail;
           } else  if (data8[0] != 1)
                   goto fail;
           else if (klen + 3 >= len || le16dec(data8 + 1) != klen)
                   goto fail;
           else if (memcmp(data8 + 3, name, klen))
                   goto fail;
   
         strlcpy(database, path, sizeof(database));          strlcpy(database, path, sizeof(database));
         _ti_database = database;          _ti_database = database;
         dt.dptr = (void *)__UNCONST(name);  
         dt.dsize = strlen(name);  
         dt = dbm_fetch(db, dt);  
         if (dt.dptr == NULL) {  
                 dbm_close(db);  
                 return 0;  
         }  
   
         for (;;) {          r = _ti_readterm(term, data, len, flags);
                 p = (char *)dt.dptr;          cdbr_close(db);
                 if (*p++ != 0) /* not alias */  
                         break;  
                 dt.dsize = le16dec(p) - 1;  
                 p += sizeof(uint16_t);  
                 dt.dptr = p;  
                 dt = dbm_fetch(db, dt);  
                 if (dt.dptr == NULL) {  
                         dbm_close(db);  
                         return 0;  
                 }  
         }  
   
         r = _ti_readterm(term, (char *)dt.dptr, dt.dsize, flags);  
         dbm_close(db);  
         return r;          return r;
   
    fail:
           cdbr_close(db);
           return 0;
 }  }
   
 static int  static int
Line 291  _ti_dbgettermp(TERMINAL *term, const cha
Line 288  _ti_dbgettermp(TERMINAL *term, const cha
         return e;          return e;
 }  }
   
 int  static int
 _ti_getterm(TERMINAL *term, const char *name, int flags)  ticcmp(const TIC *tic, const char *name)
   {
           char *alias, *s;
           size_t len, l;
   
           if (strcmp(tic->name, name) == 0)
                   return 0;
           if (tic->alias == NULL)
                   return -1;
   
           len = strlen(name);
           alias = tic->alias;
           while (*alias != '\0') {
                   s = strchr(alias, '|');
                   if (s == NULL)
                           l = strlen(alias);
                   else
                           l = s - alias;
                   if (len == l && memcmp(alias, name, l) == 0)
                           return 0;
                   if (s == NULL)
                           break;
                   alias = s + 1;
           }
           return 1;
   }
   
   static int
   _ti_findterm(TERMINAL *term, const char *name, int flags)
 {  {
         int r;          int r;
         char *e, h[PATH_MAX];          char *c, *e, h[PATH_MAX];
         size_t i;          TIC *tic;
         const struct compiled_term *t;          uint8_t *f;
           ssize_t len;
   
         _DIAGASSERT(term != NULL);          _DIAGASSERT(term != NULL);
         _DIAGASSERT(name != NULL);          _DIAGASSERT(name != NULL);
   
         database[0] = '\0';          database[0] = '\0';
         _ti_database = NULL;          _ti_database = NULL;
           r = 0;
   
         e = getenv("TERMINFO");          if ((e = getenv("TERMINFO")) != NULL && *e != '\0')
         if (e != NULL)                  if (e[0] == '/')
                 return _ti_dbgetterm(term, e, name, flags);                          return _ti_dbgetterm(term, e, name, flags);
   
           c = NULL;
           if (e == NULL && (c = getenv("TERMCAP")) != NULL) {
                   if (*c != '\0' && *c != '/') {
                           c = strdup(c);
                           if (c != NULL) {
                                   e = captoinfo(c);
                                   free(c);
                           }
                   }
           }
   
         e = getenv("HOME");  
         if (e != NULL) {          if (e != NULL) {
                   if (c == NULL)
                           e = strdup(e); /* So we don't destroy env */
                   if (e  == NULL)
                           tic = NULL;
                   else
                           tic = _ti_compile(e, TIC_WARNING |
                               TIC_ALIAS | TIC_DESCRIPTION | TIC_EXTRA);
                   if (c == NULL && e != NULL)
                           free(e);
                   if (tic != NULL && ticcmp(tic, name) == 0) {
                           len = _ti_flatten(&f, tic);
                           if (len != -1) {
                                   r = _ti_readterm(term, (char *)f, (size_t)len,
                                       flags);
                                   free(f);
                           }
                   }
                   _ti_freetic(tic);
                   if (r == 1) {
                           if (c == NULL)
                                   _ti_database = "$TERMINFO";
                           else
                                   _ti_database = "$TERMCAP";
                           return r;
                   }
           }
   
           if ((e = getenv("TERMINFO_DIRS")) != NULL)
                   return _ti_dbgettermp(term, e, name, flags);
   
           if ((e = getenv("HOME")) != NULL) {
                 snprintf(h, sizeof(h), "%s/.terminfo", e);                  snprintf(h, sizeof(h), "%s/.terminfo", e);
                 r = _ti_dbgetterm(term, h, name, flags);                  r = _ti_dbgetterm(term, h, name, flags);
                 if (r == 1)  
                         return 1;  
         }          }
           if (r != 1)
                   r = _ti_dbgettermp(term, _PATH_TERMINFO, name, flags);
   
         r = _ti_dbgettermp(term, TERMINFO_DIRS, name, flags);          return r;
   
   }
   
   int
   _ti_getterm(TERMINAL *term, const char *name, int flags)
   {
           int r;
           size_t i;
           const struct compiled_term *t;
   
           r = _ti_findterm(term, name, flags);
         if (r == 1)          if (r == 1)
                 return 1;                  return r;
   
         for (i = 0; i < __arraycount(compiled_terms); i++) {          for (i = 0; i < __arraycount(compiled_terms); i++) {
                 t = &compiled_terms[i];                  t = &compiled_terms[i];
Line 331  _ti_getterm(TERMINAL *term, const char *
Line 410  _ti_getterm(TERMINAL *term, const char *
   
         return r;          return r;
 }  }
   
 void  
 _ti_freeterm(TERMINAL *term)  
 {  
   
         _DIAGASSERT(term != NULL);  
   
         free(term->_area);  
         term->_area = NULL;  
         free(term->strs);  
         term->strs = NULL;  
         free(term->nums);  
         term->nums = NULL;  
         free(term->flags);  
         term->flags = NULL;  
         free(term->_userdefs);  
         term->_userdefs = NULL;  
 }  

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.13.2.1

CVSweb <webmaster@jp.NetBSD.org>