home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / beav.1.40.lzh / BEAV140 / termcap.c < prev    next >
Text File  |  1995-06-14  |  8KB  |  418 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.         if ((fp = fopen(cp,"r")) != NULL) {
  122.             return(fp);
  123.         }
  124.         } else {
  125.         if ((ncp = getenv("TERM")) != NULL) {
  126.             if (strcmp(cp,ncp) == 0) {
  127.             strcpy(bp,cp);
  128.             return((FILE *)NULL);
  129.             }
  130.         }
  131.         }
  132.     }
  133.     }
  134.   {
  135.     char path[128];
  136.  
  137.     _searchenv(DEFAULT_FILE, "INIT", path);
  138.     if ( path[0] == 0 )
  139.       _searchenv(DEFAULT_FILE, "PATH", path);
  140.     if ( path[0] == 0 )
  141.       _searchenv(DEFAULT_FILE, "DPATH", path);
  142.  
  143.     return(fopen(path,"r"));
  144.   }
  145. }
  146.  
  147.  
  148. static int gotcha(bp,name)
  149. char *bp;
  150. char *name;
  151. {
  152.     char *np;
  153.  
  154.     if (*bp == '#') {
  155.     return(FALSE);
  156.     } else {
  157.     np = name;
  158.     while (*np == *bp && *np != 0) {np++; bp++;}
  159.     if (*np == 0 && (*bp == 0 || *bp == '|' || *bp == ':')) {
  160.         return(TRUE);
  161.     } else {
  162.         while (*bp != 0 && *bp != ':' && *bp != '|') {bp++;}
  163.         if (*bp != '|') {
  164.         return(FALSE);
  165.         } else {
  166.         return(gotcha(++bp,name));
  167.         }
  168.     }
  169.     }
  170. }
  171.  
  172.  
  173. tgetflag(id)
  174. char *id;
  175. {
  176.     char *bp;
  177.  
  178.     bp = _tcpbuf;
  179.     while ((bp = index(bp,':')) != NULL) {
  180.     bp++;
  181.     if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
  182.         if (*bp == 0 || *bp++ == ':') {
  183.         return(TRUE);
  184.         } else {
  185.         return(FALSE);
  186.         }
  187.     }
  188.     }
  189.     return(FALSE);
  190. }
  191.  
  192.  
  193. tgetnum(id)
  194. char *id;
  195. {
  196.     int value, base;
  197.     char *bp;
  198.  
  199.     bp = _tcpbuf;
  200.     while ((bp = index(bp,':')) != NULL) {
  201.     bp++;
  202.     if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
  203.         if (*bp != 0 && *bp++ != '#') {
  204.         return(-1);
  205.         } else {
  206.         value = 0;
  207.         if (*bp == '0') {
  208.             base = 8;
  209.         } else {
  210.             base = 10;
  211.         }
  212.         while (isdigit(*bp)) {
  213.             value *= base;
  214.             value += (*bp++ - '0');
  215.         }
  216.         return(value);
  217.         }
  218.     }
  219.     }
  220.     return(-1);
  221. }
  222.  
  223.  
  224. char *tgetstr(id,area)
  225. char *id;
  226. char **area;
  227. {
  228.     char *bp;
  229.     char *decode();
  230.  
  231.     bp = _tcpbuf;
  232.     while ((bp = index(bp,':')) != NULL) {
  233.     bp++;
  234.     if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
  235.         if (*bp != 0 && *bp++ != '=') {
  236.         return(NULL);
  237.         } else {
  238.         return(decode(bp,area));
  239.         }
  240.     }
  241.     }
  242.     return(NULL);
  243. }
  244.  
  245.  
  246. static char *decode(bp,area)
  247. char *bp;
  248. char **area;
  249. {
  250.     char *cp, *bgn;
  251.     char *do_esc();
  252.  
  253.     cp = *area;
  254.     while (*bp != 0 && *bp != ':') {
  255.     switch(*bp) {
  256.     case '\\':
  257.         bp = do_esc(cp++,++bp);
  258.         break;
  259.     case '^':
  260.         *cp++ = (char) (*++bp & 037);
  261.         bp++;
  262.         break;
  263.     default:
  264.         *cp++ = *bp++;
  265.         break;
  266.     }
  267.     }
  268.     *cp++ = 0;
  269.     bgn = *area;
  270.     *area = cp;
  271.     return(bgn);
  272. }
  273.  
  274.  
  275. static char *maplist = {
  276.     "E\033b\bf\fn\nr\rt\t"
  277. };
  278.  
  279. char *do_esc(out,in)
  280. char *out;
  281. char *in;
  282. {
  283.     int count;
  284.     char ch;
  285.     char *cp;
  286.  
  287.     if (*in != 0) {
  288.     if (isdigit(*in)) {
  289.         ch = 0;
  290.         for (count = 0; count < 3 && isdigit(*in); in++) {
  291.          ch <<= 3;
  292.          ch |= (*in - '0');
  293.         }
  294.         *out++ = ch;
  295.     } else if ((cp = index(maplist,*in)) != NULL) {
  296.         *out++ = *++cp;
  297.         in++;
  298.     } else {
  299.         *out++ = *in++;
  300.     }
  301.     }
  302.     return(in);
  303. }
  304.  
  305.  
  306. #define MAXARGS 2
  307.  
  308. static char *in;        /* Internal copy of input string pointer */
  309. static char *out;        /* Pointer to output array */
  310. static int args[MAXARGS];    /* Maximum number of args to convert */
  311. static int pcount;        /* Count of args processed */
  312. static char output[64];        /* Converted string */
  313.  
  314. char *tgoto(cm,destcol,destline)
  315. char *cm;
  316. int destcol;
  317. int destline;
  318. {
  319.     if (cm == NULL) {
  320.     return("OOPS");
  321.     } else {
  322.     in = cm;
  323.     out = output;
  324.     args[0] = destline;
  325.     args[1] = destcol;
  326.     pcount = 0;
  327.     while (*in != 0) {
  328.         if (*in != '%') {
  329.         *out++ = *in++;
  330.         } else {
  331.         process();
  332.         }
  333.     }
  334.         *out++ = 0;
  335.     return(output);
  336.     }
  337. }
  338.  
  339.  
  340. static void process()
  341. {
  342.     int temp;
  343.  
  344.     in++;
  345.     switch(*in++) {
  346.     case 'd':
  347.     sprintf(out,"%d",args[pcount++]);
  348.     out = &output[strlen(output)];    
  349.     break;
  350.     case '2':
  351.     sprintf(out,"%02d",args[pcount++]);
  352.     out = &output[strlen(output)];
  353.     break;
  354.     case '3':
  355.     sprintf(out,"%03d",args[pcount++]);
  356.     out = &output[strlen(output)];
  357.     break;
  358.     case '.':
  359.     *out++ = (char) args[pcount++];
  360.     break;
  361.     case '+':
  362.     *out++ = (char) args[pcount++] + *in++;
  363.     break;
  364.     case '>':
  365.     if (args[pcount] > (int) *in++) {
  366.         args[pcount] += *in++;
  367.     } else {
  368.         in++;
  369.     }
  370.     break;
  371.     case 'r':
  372.     temp = args[pcount];
  373.     args[pcount] = args[pcount+1];
  374.     args[pcount+1] = temp;
  375.     break;
  376.     case 'i':
  377.     args[pcount]++;
  378.     args[pcount+1]++;
  379.     break;
  380.     case '%':
  381.     *out++ = '%';
  382.     break;
  383.     }
  384. }
  385.  
  386.  
  387. void tputs(cp,affcnt,outc)
  388. char *cp;
  389. int affcnt;
  390. int (*outc)(int);
  391. {
  392.     int ptime;            /* Pad time in tenths of milliseconds */
  393.  
  394.     if (cp == NULL || *cp == 0) {
  395.     return;
  396.     } else {
  397.     for (ptime = 0; isdigit(*cp); cp++) {
  398.         ptime *= 10;
  399.         ptime += (*cp - '0');
  400.     }
  401.     ptime *= 10;
  402.     if (*cp == '.') {
  403.         cp++;
  404.         if (isdigit(*cp)) {
  405.         ptime += (*cp++ - '0');
  406.         }
  407.         while (isdigit(*cp)) {cp++;}
  408.     }
  409.     if (*cp == '*') {
  410.         ptime *= affcnt;
  411.         cp++;
  412.     }
  413.     while (*cp != 0) {
  414.         (*outc)(*cp++);
  415.     }
  416.     }
  417. }
  418.