home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / curses-2.10.lha / curses / src / tgetent.c < prev    next >
C/C++ Source or Header  |  1994-02-21  |  4KB  |  145 lines

  1. /* -*-C-*-
  2.  *
  3.  *
  4.  * Filename : Workbench2.0:src/curses/tgetent.c
  5.  *
  6.  * Author   : Simon J Raybould.    (sie@fulcrum.co.uk).
  7.  *
  8.  * Date     : Sat Feb 06 20:03:26 1993
  9.  *
  10.  * Desc     : Get a terminal entry from the termcap file.
  11.  *
  12.  *
  13.  * THIS CODE IS NOT PUBLIC DOMAIN
  14.  * ==============================
  15.  * 
  16.  * This code is copyright Simon J Raybould 1992, all rights are reserved.
  17.  * All code, ideas, data structures and algorithms remain the property of the
  18.  * author. Neither the whole nor sections of this code may be used as part
  19.  * of other code without the authors consent. If you wish to use some of this
  20.  * code then please email me at (sie@fulcrum.bt.co.uk).
  21.  *
  22.  * This source is not public domain, so you do not have any right to alter it
  23.  * or sell it for personal gain. The source is provided purely for reference
  24.  * purposes. You may re-compile the source with any compiler you choose.
  25.  * You must not distribute altered copies without the authors consent. My
  26.  * intention is that the source will help people isolate any bugs much more
  27.  * effectively.
  28.  *
  29.  * Disclaimer
  30.  * ==========
  31.  *
  32.  * No implication is made as to this code being fit for any purpose at all.
  33.  * I (the author) shall not be held responsible for any loss of data or damage 
  34.  * to property that may result from its use or misuse.
  35.  *
  36.  *
  37.  * Revision History
  38.  * ================
  39.  *
  40.  * $Log: tgetent.c,v $
  41.  * Revision 1.2  1994/02/21  22:19:54  sie
  42.  * Added some conditional debug code.
  43.  *
  44.  * Revision 1.1  1993/05/17  23:33:10  sie
  45.  * Initial revision
  46.  *
  47.  *
  48.  */
  49.  
  50. #ifndef lint
  51. static char *rcsid = "$Header: /SRC/lib/curses/src/RCS/tgetent.c,v 1.2 1994/02/21 22:19:54 sie Exp $";
  52. #endif /* lint */
  53.  
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <ctype.h>
  58. #include "acurses.h"
  59.  
  60. static unsigned char
  61. __CapEmpty(char *str)
  62. {
  63.   while(*str) {
  64.     switch(*str++) {
  65.     case ' ':
  66.     case '\t':
  67.       break;
  68.     default:
  69.       return FALSE;             /* This char isn't whitespace */
  70.     }
  71.   }
  72.   return TRUE;                  /* Yep, It's all whitespace */
  73. }
  74.  
  75. tgetent(char *buffer, char *terminal)
  76. {
  77.   char found = FALSE;
  78.   char *termcap_file, *ptr;
  79.   char line[LINE_LEN];
  80.   int i, len, tnamelen;
  81.   FILE *fp;
  82.   
  83.   __area = buffer;              /* store a pointer to the users buffer */
  84.   
  85.   if(!(termcap_file = getenv("TERMCAP")))
  86.     termcap_file = DFLT_TERMCAP;
  87.   
  88.   if(!(fp = fopen(termcap_file, "r")))
  89.     return -1;
  90.   
  91.   while(fgets(line, LINE_LEN, fp)) {
  92.     if(line[0] == '#') continue; /* skip comments */
  93.     len = strlen(line) - 1;     /* -1 to remove \n */
  94.     line[len] = '\0';
  95.     if(found) {
  96.       if(line[len-1] == '\\') {
  97.         len--;                  /* throw the \ away */
  98.         memcpy(buffer, line, len);
  99.         buffer += len;
  100.       } else {
  101.         memcpy(buffer, line, len);
  102.         buffer += len;
  103.         *buffer = '\0';        
  104.         /* get pointers to the capabilities */
  105.         __no_caps = 0;
  106.         ptr = __area;
  107.         while(__capary[__no_caps] = strtok(ptr, ":")) {
  108.           if(__CapEmpty(__capary[__no_caps]))
  109.              continue;
  110.           /* convert the cap name to lower case */
  111.           for(i=0; i<CAP_LEN; i++)
  112.             __capary[__no_caps][i] = tolower(__capary[__no_caps][i]);
  113.           __no_caps++;
  114.           if(ptr)
  115.             ptr = (char *)0;
  116.         }          
  117.         fclose(fp);
  118.         return 1;               /* SUCCESS */
  119.       }
  120.     } else {                    /* Not found yet */
  121.       if(line[len-1] == '\\') len--;
  122.       memcpy(buffer, line, len);
  123.       tnamelen = strlen(terminal);
  124.       strtok(line, ":");
  125.       ptr = strtok(line, "|");
  126.       do {
  127.         if(!strcmp(terminal, ptr)) {
  128.           found = TRUE;
  129.           buffer += len;
  130.           break;                /* stop looking now */
  131.         }
  132.       } while(ptr = strtok((char *)0, "|"));
  133.     }
  134.   }                             /* end of while fgets */
  135.   fclose(fp);
  136.   *__area = '\0';
  137. #ifdef DEBUG
  138.   printf("%d capabilities found\n", __no_caps);
  139.   for(i=0; i< __no_caps; i++)
  140.     printf("cap %d = %s\n", i,  __capary[i]);
  141. #endif /* DEBUG */
  142.   
  143.   return 0;                     /* not found */
  144. }
  145.