home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / tput-1.0 / tparam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-10  |  6.7 KB  |  280 lines

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