tgetnum.c (2407B)
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 #include <stdio.h> 44 # ifdef MSDOS 45 # define index strchr 46 # endif 47 48 extern char *_tcpbuf; /* Termcap entry buffer pointer */ 49 50 /* 51 * PSEUDO CODE 52 * 53 * Begin tgetnum 54 * Initialize pointer to the termcap entry buffer. 55 * While there is a field to process 56 * Skip over the field separator character. 57 * If this is the entry we want then 58 * If the entry is not a numeric then 59 * Return failure value. 60 * Else 61 * Initialize value to zero. 62 * If number begins with zero then 63 * Set accumulation base to 8. 64 * Else 65 * Set accumulation base to 10. 66 * End if 67 * While there is a numeric character 68 * Accumulate the value. 69 * End while 70 * Return value. 71 * End if 72 * End if 73 * End while 74 * Return failure value. 75 * End tgetnum 76 * 77 */ 78 79 tgetnum(id) 80 char *id; 81 { 82 int value, base; 83 char *bp; 84 extern char *index(); 85 86 bp = _tcpbuf; 87 while ((bp = index(bp,':')) != NULL) { 88 bp++; 89 if (*bp++ == id[0] && *bp != NULL && *bp++ == id[1]) { 90 if (*bp != NULL && *bp++ != '#') { 91 return(-1); 92 } else { 93 value = 0; 94 if (*bp == '0') { 95 base = 8; 96 } else { 97 base = 10; 98 } 99 while (isdigit(*bp)) { 100 value *= base; 101 value += (*bp++ - '0'); 102 } 103 return(value); 104 } 105 } 106 } 107 return(-1); 108 }