home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / beav1402.zip / termcap.c < prev    next >
C/C++ Source or Header  |  1993-11-07  |  8KB  |  421 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. /* In order to reduce the size of this file drastically, the large
  20.  * comments and pseudo code was removed as well as the padding stuff
  21.  * which is not needed for OS/2. Size went from 28k down to 8k.
  22.  */
  23.  
  24. #include <stdlib.h>
  25. #include <ctype.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29. static char *fgetlr(char *bp, int bpsize, FILE *fp);
  30. static FILE *find_file(char *bp);
  31. static gotcha(char *bp, char *name);
  32. static char *decode(char *bp, char **area);
  33. static char *do_esc(char *out, char *in);
  34. static void process(void);
  35.  
  36. #define TRUE 1
  37. #define FALSE 0
  38. #define BUFSIZE 1024            /* Assumed size of external buffer */
  39.  
  40. #define NO_FILE     -1            /* Returned if can't open file */
  41. #define NO_ENTRY  0            /* Returned if can't find entry */
  42. #define SUCCESS   1            /* Returned if entry found ok */
  43. #define TRUNCATED 2            /* Returned if entry found but trunc */
  44.  
  45. # ifdef DGK
  46. # define DEFAULT_ROOT "termcap.cnf"        /* name without path component */
  47.   FILE *fopenp();
  48. # endif
  49.  
  50. # define DEFAULT_FILE "termcap.dat"
  51.  
  52. char *_tcpbuf;                          /* Place to remember buffer pointer */
  53.  
  54. # define index strchr
  55.  
  56.  
  57. static char *fgetlr(bp,bpsize,fp)
  58. char *bp;
  59. int bpsize;
  60. FILE *fp;
  61. {
  62.     int numch;
  63.     char *cp;
  64.  
  65.     if (fgets(bp,bpsize,fp) == NULL) {
  66.     return(NULL);
  67.     } else {
  68.     numch = strlen(bp);
  69.     cp = &bp[numch];
  70.     if (*--cp == '\n') {
  71.         if (numch > 1 && *--cp == '\\') {
  72.         *cp++ = '\n';
  73.         *cp = 0;
  74.         fgetlr(cp,bpsize-numch+1,fp);
  75.         }
  76.     }
  77.     return(bp);
  78.     }
  79. }
  80.  
  81.  
  82. int tgetent(bp,name)
  83. char *bp;                /* Pointer to buffer (1024 char min) */
  84. char *name;                /* Pointer to terminal entry to find */
  85. {
  86.     FILE *fp;
  87.  
  88.     *bp = 0;
  89.     _tcpbuf = bp;
  90.     if ((fp = find_file(bp)) == NULL) {
  91.     if (*bp != 0) {
  92.         return(SUCCESS);
  93.     } else {
  94.         return(NO_FILE);
  95.     }
  96.     } else {
  97.     while (fgetlr(bp,BUFSIZE,fp)) {
  98.         if (gotcha(bp,name)) {
  99.         fclose(fp);
  100.         if (bp[strlen(bp)-1] != '\n') {
  101.             return(TRUNCATED);
  102.         } else {
  103.             return(SUCCESS);
  104.         }
  105.         }
  106.     }
  107.     return(NO_ENTRY);
  108.     }
  109. }
  110.  
  111.  
  112. static FILE *find_file(bp)
  113. char *bp;
  114. {
  115.     FILE *fp;
  116.     char *cp, *ncp;
  117.  
  118.     if ((cp = getenv("TERMCAP")) != NULL) {
  119.     if (*cp != 0) {
  120.         if (*cp == '/' || *cp == '\\' || 
  121.         (isalpha(*cp) && *(cp + 1) == ':' &&
  122.           (*(cp + 2) == '/' || *(cp + 2) == '\\'))) {
  123.         if ((fp = fopen(cp,"r")) != NULL) {
  124.             return(fp);
  125.         }
  126.         } else {
  127.         if ((ncp = getenv("TERM")) != NULL) {
  128.             if (strcmp(cp,ncp) == 0) {
  129.             strcpy(bp,cp);
  130.             return((FILE *)NULL);
  131.             }
  132.         }
  133.         }
  134.     }
  135.     }
  136.  
  137.   {
  138.     char path[128];
  139.  
  140.     _searchenv(DEFAULT_FILE, "INIT", path);
  141.     if ( path[0] == 0 )
  142.       _searchenv(DEFAULT_FILE, "PATH", path);
  143.     if ( path[0] == 0 )
  144.       _searchenv(DEFAULT_FILE, "DPATH", path);
  145.  
  146.     return(fopen(path,"r"));
  147.   }
  148. }
  149.  
  150.  
  151. static int gotcha(bp,name)
  152. char *bp;
  153. char *name;
  154. {
  155.     char *np;
  156.  
  157.     if (*bp == '#') {
  158.     return(FALSE);
  159.     } else {
  160.     np = name;
  161.     while (*np == *bp && *np != 0) {np++; bp++;}
  162.     if (*np == 0 && (*bp == 0 || *bp == '|' || *bp == ':')) {
  163.         return(TRUE);
  164.     } else {
  165.         while (*bp != 0 && *bp != ':' && *bp != '|') {bp++;}
  166.         if (*bp != '|') {
  167.         return(FALSE);
  168.         } else {
  169.         return(gotcha(++bp,name));
  170.         }
  171.     }
  172.     }
  173. }
  174.  
  175.  
  176. tgetflag(id)
  177. char *id;
  178. {
  179.     char *bp;
  180.  
  181.     bp = _tcpbuf;
  182.     while ((bp = index(bp,':')) != NULL) {
  183.     bp++;
  184.     if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
  185.         if (*bp == 0 || *bp++ == ':') {
  186.         return(TRUE);
  187.         } else {
  188.         return(FALSE);
  189.         }
  190.     }
  191.     }
  192.     return(FALSE);
  193. }
  194.  
  195.  
  196. tgetnum(id)
  197. char *id;
  198. {
  199.     int value, base;
  200.     char *bp;
  201.  
  202.     bp = _tcpbuf;
  203.     while ((bp = index(bp,':')) != NULL) {
  204.     bp++;
  205.     if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
  206.         if (*bp != 0 && *bp++ != '#') {
  207.         return(-1);
  208.         } else {
  209.         value = 0;
  210.         if (*bp == '0') {
  211.             base = 8;
  212.         } else {
  213.             base = 10;
  214.         }
  215.         while (isdigit(*bp)) {
  216.             value *= base;
  217.             value += (*bp++ - '0');
  218.         }
  219.         return(value);
  220.         }
  221.     }
  222.     }
  223.     return(-1);
  224. }
  225.  
  226.  
  227. char *tgetstr(id,area)
  228. char *id;
  229. char **area;
  230. {
  231.     char *bp;
  232.     char *decode();
  233.  
  234.     bp = _tcpbuf;
  235.     while ((bp = index(bp,':')) != NULL) {
  236.     bp++;
  237.     if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
  238.         if (*bp != 0 && *bp++ != '=') {
  239.         return(NULL);
  240.         } else {
  241.         return(decode(bp,area));
  242.         }
  243.     }
  244.     }
  245.     return(NULL);
  246. }
  247.  
  248.  
  249. static char *decode(bp,area)
  250. char *bp;
  251. char **area;
  252. {
  253.     char *cp, *bgn;
  254.     char *do_esc();
  255.  
  256.     cp = *area;
  257.     while (*bp != 0 && *bp != ':') {
  258.     switch(*bp) {
  259.     case '\\':
  260.         bp = do_esc(cp++,++bp);
  261.         break;
  262.     case '^':
  263.         *cp++ = (char) (*++bp & 037);
  264.         bp++;
  265.         break;
  266.     default:
  267.         *cp++ = *bp++;
  268.         break;
  269.     }
  270.     }
  271.     *cp++ = 0;
  272.     bgn = *area;
  273.     *area = cp;
  274.     return(bgn);
  275. }
  276.  
  277.  
  278. static char *maplist = {
  279.     "E\033b\bf\fn\nr\rt\t"
  280. };
  281.  
  282. char *do_esc(out,in)
  283. char *out;
  284. char *in;
  285. {
  286.     int count;
  287.     char ch;
  288.     char *cp;
  289.  
  290.     if (*in != 0) {
  291.     if (isdigit(*in)) {
  292.         ch = 0;
  293.         for (count = 0; count < 3 && isdigit(*in); in++) {
  294.          ch <<= 3;
  295.          ch |= (*in - '0');
  296.         }
  297.         *out++ = ch;
  298.     } else if ((cp = index(maplist,*in)) != NULL) {
  299.         *out++ = *++cp;
  300.         in++;
  301.     } else {
  302.         *out++ = *in++;
  303.     }
  304.     }
  305.     return(in);
  306. }
  307.  
  308.  
  309. #define MAXARGS 2
  310.  
  311. static char *in;        /* Internal copy of input string pointer */
  312. static char *out;        /* Pointer to output array */
  313. static int args[MAXARGS];    /* Maximum number of args to convert */
  314. static int pcount;        /* Count of args processed */
  315. static char output[64];        /* Converted string */
  316.  
  317. char *tgoto(cm,destcol,destline)
  318. char *cm;
  319. int destcol;
  320. int destline;
  321. {
  322.     if (cm == NULL) {
  323.     return("OOPS");
  324.     } else {
  325.     in = cm;
  326.     out = output;
  327.     args[0] = destline;
  328.     args[1] = destcol;
  329.     pcount = 0;
  330.     while (*in != 0) {
  331.         if (*in != '%') {
  332.         *out++ = *in++;
  333.         } else {
  334.         process();
  335.         }
  336.     }
  337.         *out++ = 0;
  338.     return(output);
  339.     }
  340. }
  341.  
  342.  
  343. static void process()
  344. {
  345.     int temp;
  346.  
  347.     in++;
  348.     switch(*in++) {
  349.     case 'd':
  350.     sprintf(out,"%d",args[pcount++]);
  351.     out = &output[strlen(output)];    
  352.     break;
  353.     case '2':
  354.     sprintf(out,"%02d",args[pcount++]);
  355.     out = &output[strlen(output)];
  356.     break;
  357.     case '3':
  358.     sprintf(out,"%03d",args[pcount++]);
  359.     out = &output[strlen(output)];
  360.     break;
  361.     case '.':
  362.     *out++ = (char) args[pcount++];
  363.     break;
  364.     case '+':
  365.     *out++ = (char) args[pcount++] + *in++;
  366.     break;
  367.     case '>':
  368.     if (args[pcount] > (int) *in++) {
  369.         args[pcount] += *in++;
  370.     } else {
  371.         in++;
  372.     }
  373.     break;
  374.     case 'r':
  375.     temp = args[pcount];
  376.     args[pcount] = args[pcount+1];
  377.     args[pcount+1] = temp;
  378.     break;
  379.     case 'i':
  380.     args[pcount]++;
  381.     args[pcount+1]++;
  382.     break;
  383.     case '%':
  384.     *out++ = '%';
  385.     break;
  386.     }
  387. }
  388.  
  389.  
  390. void tputs(cp,affcnt,outc)
  391. char *cp;
  392. int affcnt;
  393. int (*outc)(int);
  394. {
  395.     int ptime;            /* Pad time in tenths of milliseconds */
  396.  
  397.     if (cp == NULL || *cp == 0) {
  398.     return;
  399.     } else {
  400.     for (ptime = 0; isdigit(*cp); cp++) {
  401.         ptime *= 10;
  402.         ptime += (*cp - '0');
  403.     }
  404.     ptime *= 10;
  405.     if (*cp == '.') {
  406.         cp++;
  407.         if (isdigit(*cp)) {
  408.         ptime += (*cp++ - '0');
  409.         }
  410.         while (isdigit(*cp)) {cp++;}
  411.     }
  412.     if (*cp == '*') {
  413.         ptime *= affcnt;
  414.         cp++;
  415.     }
  416.     while (*cp != 0) {
  417.         (*outc)(*cp++);
  418.     }
  419.     }
  420. }
  421.