home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Games / larn12s.arc / TERMCAP.ARC / TGETENT.C < prev    next >
C/C++ Source or Header  |  1987-10-28  |  9KB  |  368 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *    tgetent   load buffer with entry for specified terminal
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    termcap functions
  28.  *    utility routines
  29.  *
  30.  *  SYNOPSIS
  31.  *
  32.  *    int tgetent(bp,name)
  33.  *    char *bp;
  34.  *    char *name;
  35.  *
  36.  *  DESCRIPTION
  37.  *
  38.  *    Extracts the entry for terminal <name> from the termcap file
  39.  *    and places it in the character buffer <bp>.   It is currently
  40.  *    assumed that bp is at least 1024 characters.  If the entry in
  41.  *    the termcap file is larger than 1023 characters the excess
  42.  *    characters will be discarded and appropriate status will
  43.  *    be returned.
  44.  *
  45.  *    Also note that since bp is used by other termcap
  46.  *    routines, the storage associated with the termcap entry
  47.  *    cannot be freed until all termcap calls are completed.
  48.  *
  49.  *    Tgetent can be directed to look in a file other than
  50.  *    the default (/etc/termcap) by defining an environment
  51.  *    variable called TERMCAP to be the pathname of the desired
  52.  *    termcap file.  This is useful for debugging new entries.
  53.  *    NOTE: the pathname MUST begin with a '/' character.
  54.  *
  55.  *    Also, if the string assigned to TERMCAP does not begin with
  56.  *    a '/' and if the environment variable TERM matches <name> then
  57.  *    the string assigned to TERMCAP is copied to buffer <bp> 
  58.  *    instead of reading a termcap file.
  59.  *    
  60.  *  RETURNS
  61.  *
  62.  *    -1  if the termcap file cannot be opened
  63.  *     0  if no entry in termcap file matches <name>
  64.  *     1  if extraction is successful with no errors
  65.  *     2  if extraction is successful but entry truncated
  66.  *
  67.  *  SEE ALSO
  68.  *
  69.  *    tgetnum   extract numeric type capability
  70.  *    tgetflag  test boolean type capability
  71.  *    tgetstr   get string value of capability
  72.  *
  73.  *  AUTHOR
  74.  *
  75.  *    Fred Fish
  76.  *
  77.  */
  78.  
  79. #include <stdio.h>
  80.  
  81. #define TRUE 1
  82. #define FALSE 0
  83. #define BUFSIZE 1024            /* Assumed size of external buffer */
  84.  
  85. #define NO_FILE     -1            /* Returned if can't open file */
  86. #define NO_ENTRY  0            /* Returned if can't find entry */
  87. #define SUCCESS   1            /* Returned if entry found ok */
  88. #define TRUNCATED 2            /* Returned if entry found but trunc */
  89.  
  90. # ifdef DGK
  91. # define DEFAULT_ROOT "termcap"        /* name without path component */
  92.   FILE *fopenp();
  93. # define DEFAULT_FILE "\\etc\\termcap"
  94. # else
  95. # define DEFAULT_FILE "/etc/termcap"    /* default termcap filename */
  96. # endif
  97.  
  98. char *_tcpbuf;                /* Place to remember buffer pointer */
  99.  
  100. /*
  101.  *  PSEUDO CODE
  102.  *
  103.  *    Begin tgetent
  104.  *        Erase any previous buffer contents.
  105.  *        Remember the buffer pointer.
  106.  *        If termcap file is not found then
  107.  *        If buffer was filled anyway then
  108.  *            Return SUCCESS.
  109.  *        Else
  110.  *            Return NO_FILE.
  111.  *        End if
  112.  *        Else
  113.  *        While records left to process
  114.  *            If this is entry is what we want then
  115.  *            Close the termcap file.
  116.  *            If entry was truncated then
  117.  *                Return TRUNCATED status
  118.  *            Else
  119.  *                Return SUCCESS status.
  120.  *            End if
  121.  *            End if
  122.  *        End while
  123.  *        Return NO_ENTRY status.
  124.  *        End if
  125.  *    End tgetent
  126.  *            
  127.  */
  128.  
  129. int tgetent(bp,name)
  130. char *bp;                /* Pointer to buffer (1024 char min) */
  131. char *name;                /* Pointer to terminal entry to find */
  132. {
  133.     FILE *fp, *find_file();
  134.  
  135.     *bp = '\0';
  136.     _tcpbuf = bp;
  137.     if ((fp = find_file(bp)) == NULL) {
  138.     if (*bp != '\0') {
  139.         return(SUCCESS);
  140.     } else {
  141.         return(NO_FILE);
  142.     }
  143.     } else {
  144.     while (fgetlr(bp,BUFSIZE,fp)) {
  145.         if (gotcha(bp,name)) {
  146.         fclose(fp);
  147.         if (bp[strlen(bp)-1] != '\n') {
  148.             return(TRUNCATED);
  149.         } else {
  150.             return(SUCCESS);
  151.         }
  152.         }
  153.     }
  154.     return(NO_ENTRY);
  155.     }
  156. }
  157.  
  158. /*
  159.  *  INTERNAL FUNCTION
  160.  *
  161.  *    find_file    find the termcap file and open it if possible
  162.  *
  163.  *  KEY WORDS
  164.  *
  165.  *    internal functions
  166.  *    find_file
  167.  *
  168.  *  SYNOPSIS
  169.  *
  170.  *    static FILE *find_file(bp)
  171.  *    char *bp;
  172.  *
  173.  *  DESCRIPTION
  174.  *
  175.  *    Attempts to locate and open the termcap file.  Also handles
  176.  *    using the environment TERMCAP string as the actual buffer
  177.  *    (that's why bp has to be an input parameter).
  178.  *
  179.  *    If TERMCAP is defined an begins with a '/' character then
  180.  *    it is taken to be the pathname of the termcap file and
  181.  *    an attempt is made to open it.  If this fails then
  182.  *    the default termcap file is used instead.
  183.  *
  184.  *    If TERMCAP is defined but does not begin with a '/' then
  185.  *    it is assumed to be the actual buffer contents provided
  186.  *    that <name> matches the environment variable TERM.
  187.  *
  188.  *  BUGS
  189.  *
  190.  *    There is currently no way to be sure which termcap
  191.  *    file was opened since the default will always be
  192.  *    tried.
  193.  *
  194.  */
  195.  
  196. /*
  197.  *  PSEUDO CODE
  198.  *
  199.  *    Begin find_file
  200.  *        If there is a TERMCAP environment string then
  201.  *        If the string is not null then
  202.  *            If the string is a pathname then
  203.  *            If that file is opened successfully then
  204.  *                Return its pointer.
  205.  *            End if
  206.  *            Else
  207.  *            If there is a TERM environment string then
  208.  *                If TERM matches <name> then
  209.  *                Copy TERMCAP string to buffer.
  210.  *                Return NULL for no file.
  211.  *                End if
  212.  *            End if
  213.  *            End if
  214.  *        End if
  215.  *        End if
  216.  *        Open default termcap file and return results.
  217.  *    End find_file
  218.  *
  219.  */
  220.  
  221. static FILE *find_file(bp)
  222. char *bp;
  223. {
  224.     FILE *fp, *fopen();
  225.     char *cp, *ncp, *getenv();
  226.  
  227.     if ((cp = getenv("TERMCAP")) != NULL) {
  228.     if (*cp != '\0') {
  229.         if (*cp == '/' || *cp == '\\') {
  230.         if ((fp = fopen(cp,"r")) != NULL) {
  231.             return(fp);
  232.         }
  233.         } else {
  234.         if ((ncp = getenv("TERM")) != NULL) {
  235.             if (strcmp(cp,ncp) == 0) {
  236.             strcpy(bp,cp);
  237.             return((FILE *)NULL);
  238.             }
  239.         }
  240.         }
  241.     }
  242.     }
  243. # ifdef DGK
  244.     /* Try current directory, then /etc/termcap, then along the path
  245.      */
  246.     if (fp = fopen(DEFAULT_ROOT, "r"))
  247.         return fp;
  248.     else if (fp = fopen(DEFAULT_FILE, "r"))
  249.         return fp;
  250.     else
  251.         return fopenp(DEFAULT_ROOT, "r", NULL);
  252. # else
  253.     return(fopen(DEFAULT_FILE,"r"));
  254. # endif
  255. }
  256.  
  257. /*
  258.  *  INTERNAL FUNCTION
  259.  *
  260.  *    gotcha   test to see if entry is for specified terminal
  261.  *
  262.  *  SYNOPSIS
  263.  *
  264.  *    gotcha(bp,name)
  265.  *    char *bp;
  266.  *    char *name;
  267.  *
  268.  *  DESCRIPTION
  269.  *
  270.  *    Tests to see if the entry in buffer bp matches the terminal
  271.  *    specified by name.  Returns TRUE if match is detected, FALSE
  272.  *    otherwise.
  273.  *
  274.  */
  275.  
  276. /*
  277.  *  PSEUDO CODE
  278.  *
  279.  *    Begin gotcha
  280.  *        If buffer character is comment character then
  281.  *        Return FALSE since remainder is comment
  282.  *        Else
  283.  *        Initialize name scan pointer.
  284.  *        Compare name and buffer until end or mismatch.
  285.  *        If valid terminators for both name and buffer strings
  286.  *            Return TRUE since a match was found.
  287.  *        Else
  288.  *            Find next non-name character in buffer.
  289.  *            If not an alternate name separater character
  290.  *            Return FALSE since no more names to check.
  291.  *            Else
  292.  *            Test next name and return results.
  293.  *            End if
  294.  *        End if
  295.  *        End if
  296.  *    End gotcha
  297.  *
  298.  */
  299.  
  300. gotcha(bp,name)
  301. char *bp;
  302. char *name;
  303. {
  304.     char *np;
  305.  
  306.     if (*bp == '#') {
  307.     return(FALSE);
  308.     } else {
  309.     np = name;
  310.     while (*np == *bp && *np != '\0') {np++; bp++;}
  311.     if (*np == '\0' && (*bp == '\0' || *bp == '|' || *bp == ':')) {
  312.         return(TRUE);
  313.     } else {
  314.         while (*bp != '\0' && *bp != ':' && *bp != '|') {bp++;}
  315.         if (*bp != '|') {
  316.         return(FALSE);
  317.         } else {
  318.         return(gotcha(++bp,name));
  319.         }
  320.     }
  321.     }
  322. }
  323.  
  324. #ifdef DGK
  325. # ifdef MSDOS
  326. # define PATHSEP ';'
  327. # endif
  328. # ifdef GEMDOS
  329. # define PATHSEP ','
  330. # endif
  331.  
  332. /* Follow the PATH, trying to fopen the file.  Takes one additional
  333.  * argument which can be NULL.  Otherwise this argument gets filled
  334.  * in the full path to the file.  Returns as does fopen().
  335.  */
  336. FILE *
  337. fopenp(name, mode, pathname)
  338. char *name, *mode, *pathname;
  339. {
  340.     char buffer[BUFSIZ], *buf, *bufp, *pathp, *getenv(), lastch;
  341.     FILE *fp;
  342.  
  343.     /* If pathname is given, use it instead of buf so the calling
  344.      * process knows the path we found name under
  345.      */
  346.     if (pathname)
  347.         buf = pathname;
  348.     else
  349.         buf = buffer;
  350.  
  351.     strcpy(buf, name);
  352.     pathp = getenv("PATH");
  353.     while (pathp && *pathp) {
  354.         bufp = buf;
  355.         while (*pathp && *pathp != PATHSEP)
  356.             lastch = *bufp++ = *pathp++;
  357.         if (lastch != '\\')
  358.             *bufp++ = '\\';
  359.         strcpy(bufp, name);
  360.         if (fp = fopen(buf, mode))
  361.             return fp;
  362.         if (*pathp)
  363.             pathp++;
  364.     }
  365.     return NULL;
  366. }
  367. #endif
  368.