home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / trmcap11.zip / termcap-1.1 / tparam.c < prev    next >
C/C++ Source or Header  |  1992-10-01  |  7KB  |  289 lines

  1. /* Merge parameters into a termcap entry string.
  2.    Copyright (C) 1985, 1987 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* config.h may rename various library functions such as malloc.  */
  19. #ifdef emacs
  20. #include "config.h"
  21. #else /* not emacs */
  22. #if defined(USG) || defined(STDC_HEADERS)
  23. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  24. #endif
  25.  
  26. #ifdef STDC_HEADERS
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #else
  30. char *malloc ();
  31. char *realloc ();
  32. #endif
  33.  
  34. #endif /* not emacs */
  35.  
  36. /* Assuming STRING is the value of a termcap string entry
  37.    containing `%' constructs to expand parameters,
  38.    merge in parameter values and store result in block OUTSTRING points to.
  39.    LEN is the length of OUTSTRING.  If more space is needed,
  40.    a block is allocated with `malloc'.
  41.  
  42.    The value returned is the address of the resulting string.
  43.    This may be OUTSTRING or may be the address of a block got with `malloc'.
  44.    In the latter case, the caller must free the block.
  45.  
  46.    The fourth and following args to tparam serve as the parameter values.  */
  47.  
  48. static char *tparam1 ();
  49.  
  50. /* VARARGS 2 */
  51. char *
  52. tparam (string, outstring, len, arg0, arg1, arg2, arg3)
  53.      char *string;
  54.      char *outstring;
  55.      int len;
  56.      int arg0, arg1, arg2, arg3;
  57. {
  58. #ifdef NO_ARG_ARRAY
  59.   int arg[4];
  60.   arg[0] = arg0;
  61.   arg[1] = arg1;
  62.   arg[2] = arg2;
  63.   arg[3] = arg3;
  64.   return tparam1 (string, outstring, len, 0, 0, arg);
  65. #else
  66.   return tparam1 (string, outstring, len, 0, 0, &arg0);
  67. #endif
  68. }
  69.  
  70. char *BC;
  71. char *UP;
  72.  
  73. static char tgoto_buf[50];
  74.  
  75. char *
  76. tgoto (cm, hpos, vpos)
  77.      char *cm;
  78.      int hpos, vpos;
  79. {
  80.   int args[2];
  81.   if (!cm)
  82.     return 0;
  83.   args[0] = vpos;
  84.   args[1] = hpos;
  85.   return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
  86. }
  87.  
  88. static char *
  89. tparam1 (string, outstring, len, up, left, argp)
  90.      char *string;
  91.      char *outstring;
  92.      int len;
  93.      char *up, *left;
  94.      register int *argp;
  95. {
  96.   register int c;
  97.   register char *p = string;
  98.   register char *op = outstring;
  99.   char *outend;
  100.   int outlen = 0;
  101.  
  102.   register int tem;
  103.   int *old_argp = argp;
  104.   int doleft = 0;
  105.   int doup = 0;
  106.  
  107.   outend = outstring + len;
  108.  
  109.   while (1)
  110.     {
  111.       /* If the buffer might be too short, make it bigger.  */
  112.       if (op + 5 >= outend)
  113.     {
  114.       register char *new;
  115.       if (outlen == 0)
  116.         {
  117.           outlen = len + 40;
  118.           new = (char *) malloc (outlen);
  119.           outend += 40;
  120.           bcopy (outstring, new, op - outstring);
  121.         }
  122.       else
  123.         {
  124.           outend += outlen;
  125.           outlen *= 2;
  126.           new = (char *) realloc (outstring, outlen);
  127.         }
  128.       op += new - outstring;
  129.       outend += new - outstring;
  130.       outstring = new;
  131.     }
  132.       c = *p++;
  133.       if (!c)
  134.     break;
  135.       if (c == '%')
  136.     {
  137.       c = *p++;
  138.       tem = *argp;
  139.       switch (c)
  140.         {
  141.         case 'd':        /* %d means output in decimal.  */
  142.           if (tem < 10)
  143.         goto onedigit;
  144.           if (tem < 100)
  145.         goto twodigit;
  146.         case '3':        /* %3 means output in decimal, 3 digits.  */
  147.           if (tem > 999)
  148.         {
  149.           *op++ = tem / 1000 + '0';
  150.           tem %= 1000;
  151.         }
  152.           *op++ = tem / 100 + '0';
  153.         case '2':        /* %2 means output in decimal, 2 digits.  */
  154.         twodigit:
  155.           tem %= 100;
  156.           *op++ = tem / 10 + '0';
  157.         onedigit:
  158.           *op++ = tem % 10 + '0';
  159.           argp++;
  160.           break;
  161.  
  162.         case 'C':
  163.           /* For c-100: print quotient of value by 96, if nonzero,
  164.          then do like %+.  */
  165.           if (tem >= 96)
  166.         {
  167.           *op++ = tem / 96;
  168.           tem %= 96;
  169.         }
  170.         case '+':        /* %+x means add character code of char x.  */
  171.           tem += *p++;
  172.         case '.':        /* %. means output as character.  */
  173.           if (left)
  174.         {
  175.           /* If want to forbid output of 0 and \n and \t,
  176.              and this is one of them, increment it.  */
  177.           while (tem == 0 || tem == '\n' || tem == '\t')
  178.             {
  179.               tem++;
  180.               if (argp == old_argp)
  181.             doup++, outend -= strlen (up);
  182.               else
  183.             doleft++, outend -= strlen (left);
  184.             }
  185.         }
  186.           *op++ = tem | 0200;
  187.         case 'f':        /* %f means discard next arg.  */
  188.           argp++;
  189.           break;
  190.  
  191.         case 'b':        /* %b means back up one arg (and re-use it).  */
  192.           argp--;
  193.           break;
  194.  
  195.         case 'r':        /* %r means interchange following two args.  */
  196.           argp[0] = argp[1];
  197.           argp[1] = tem;
  198.           old_argp++;
  199.           break;
  200.  
  201.         case '>':        /* %>xy means if arg is > char code of x, */
  202.           if (argp[0] > *p++) /* then add char code of y to the arg, */
  203.         argp[0] += *p;    /* and in any case don't output.  */
  204.           p++;        /* Leave the arg to be output later.  */
  205.           break;
  206.  
  207.         case 'a':        /* %a means arithmetic.  */
  208.           /* Next character says what operation.
  209.          Add or subtract either a constant or some other arg.  */
  210.           /* First following character is + to add or - to subtract
  211.          or = to assign.  */
  212.           /* Next following char is 'p' and an arg spec
  213.          (0100 plus position of that arg relative to this one)
  214.          or 'c' and a constant stored in a character.  */
  215.           tem = p[2] & 0177;
  216.           if (p[1] == 'p')
  217.         tem = argp[tem - 0100];
  218.           if (p[0] == '-')
  219.         argp[0] -= tem;
  220.           else if (p[0] == '+')
  221.         argp[0] += tem;
  222.           else if (p[0] == '*')
  223.         argp[0] *= tem;
  224.           else if (p[0] == '/')
  225.         argp[0] /= tem;
  226.           else
  227.         argp[0] = tem;
  228.  
  229.           p += 3;
  230.           break;
  231.  
  232.         case 'i':        /* %i means add one to arg, */
  233.           argp[0] ++;    /* and leave it to be output later.  */
  234.           argp[1] ++;    /* Increment the following arg, too!  */
  235.           break;
  236.  
  237.         case '%':        /* %% means output %; no arg.  */
  238.           goto ordinary;
  239.  
  240.         case 'n':        /* %n means xor each of next two args with 140.  */
  241.           argp[0] ^= 0140;
  242.           argp[1] ^= 0140;
  243.           break;
  244.  
  245.         case 'm':        /* %m means xor each of next two args with 177.  */
  246.           argp[0] ^= 0177;
  247.           argp[1] ^= 0177;
  248.           break;
  249.  
  250.         case 'B':        /* %B means express arg as BCD char code.  */
  251.           argp[0] += 6 * (tem / 10);
  252.           break;
  253.  
  254.         case 'D':        /* %D means weird Delta Data transformation.  */
  255.           argp[0] -= 2 * (tem % 16);
  256.           break;
  257.         }
  258.     }
  259.       else
  260.     /* Ordinary character in the argument string.  */
  261.       ordinary:
  262.     *op++ = c;
  263.     }
  264.   *op = 0;
  265.   while (doup-- > 0)
  266.     strcat (op, up);
  267.   while (doleft-- > 0)
  268.     strcat (op, left);
  269.   return outstring;
  270. }
  271.  
  272. #ifdef DEBUG
  273.  
  274. main (argc, argv)
  275.      int argc;
  276.      char **argv;
  277. {
  278.   char buf[50];
  279.   int args[3];
  280.   args[0] = atoi (argv[2]);
  281.   args[1] = atoi (argv[3]);
  282.   args[2] = atoi (argv[4]);
  283.   tparam1 (argv[1], buf, "LEFT", "UP", args);
  284.   printf ("%s\n", buf);
  285.   return 0;
  286. }
  287.  
  288. #endif /* DEBUG */
  289.