home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / crssrc16 / tgetnum.c < prev    next >
C/C++ Source or Header  |  1993-07-29  |  3KB  |  157 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.  *    tgetnum   extract numeric option from termcap entry
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    termcap
  28.  *    ce functions
  29.  *
  30.  *  SYNOPSIS
  31.  *
  32.  *    tgetnum(id)
  33.  *    char *id;
  34.  *
  35.  *  DESCRIPTION
  36.  *
  37.  *    Returns numeric value of capability <id>, or -1 if <id>
  38.  *    is not found.   Knows about octal numbers, which
  39.  *    begin with 0.
  40.  *
  41.  */
  42.  
  43. #ifndef _COMPILER_H
  44. #  include <compiler.h>
  45. #endif
  46.  
  47. #include <stdio.h>
  48. #include <ctype.h>
  49. #include <string.h>
  50. #include <termcap.h>
  51. #if __STDC__
  52. #  include <stdlib.h>
  53. #endif
  54.  
  55. # ifdef MSDOS
  56. # define index strchr
  57. # endif
  58.  
  59. extern char *_tcpbuf;        /* Termcap entry buffer pointer */
  60.  
  61. /*
  62.  *  PSEUDO CODE
  63.  *
  64.  *    Begin tgetnum
  65.  *        Initialize pointer to the termcap entry buffer.
  66.  *        While there is a field to process
  67.  *        Skip over the field separator character.
  68.  *        If this is the entry we want then
  69.  *            If the entry is not a numeric then
  70.  *            Return failure value.
  71.  *            Else
  72.  *            Initialize value to zero.
  73.  *            If number begins with zero then
  74.  *                Set accumulation base to 8.
  75.  *            Else
  76.  *                Set accumulation base to 10.
  77.  *            End if
  78.  *            While there is a numeric character
  79.  *                Accumulate the value.
  80.  *            End while
  81.  *            Return value.
  82.  *            End if
  83.  *        End if
  84.  *        End while
  85.  *        Return failure value.
  86.  *    End tgetnum
  87.  *
  88.  *  Atari ST specific change, when this fails, we chech to see if we are 
  89.  *     trying to get the entry for "li" = lines, ro "co" = columns, if 
  90.  *     this is the case try the environment variables "ROWS" "LINES" and 
  91.  *     "COLUMNS", before returning error. This is great for window 
  92.  *     managers like "Gemini" or "TOSwin". 
  93.  *
  94.  */
  95.  
  96. #if defined(GEMDOS)
  97. /* get line /col from the env */
  98. static int getlinecol(id)
  99. char *id;
  100. {
  101.     char *p;
  102.  
  103.     if (id[0] == 'l' && id[1] =='i' )
  104.     {
  105.         if(!(p = getenv("LINES")))
  106.             p = getenv("ROWS");
  107.         if(p)
  108.             return(atoi(p));
  109.     }
  110.     else if (id[0] == 'c' && id[1] =='o' )
  111.     {
  112.         if((p = getenv("COLUMNS")))
  113.             return(atoi(p));
  114.     }
  115.     return(-1);
  116. }
  117. #endif
  118.  
  119. int tgetnum(id)
  120. char *id;
  121. {
  122.     int value, base;
  123.     char *bp;
  124.  
  125.     bp = _tcpbuf;
  126.     while ((bp = index(bp,':')) != NULL) {
  127.     bp++;
  128.     if (*bp++ == id[0] && *bp != '\0' && *bp++ == id[1]) {
  129.         if (*bp != '\0' && *bp++ != '#') {
  130. #if !defined(GEMDOS)
  131.         return(-1);
  132. #else 
  133.                 return getlinecol(id);
  134. #endif 
  135.  
  136.         } else {
  137.         value = 0;
  138.         if (*bp == '0') {
  139.             base = 8;
  140.         } else {
  141.             base = 10;
  142.         }
  143.         while (isdigit(*bp)) {
  144.             value *= base;
  145.             value += (*bp++ - '0');
  146.         }
  147.         return(value);
  148.         }
  149.     }
  150.       }
  151. #if !defined(GEMDOS)
  152.       return(-1);
  153. #else 
  154.       return getlinecol(id);
  155. #endif 
  156. }
  157.