home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March / PCWK3A99.iso / Linux / DDD331 / DDD-3_1_.000 / DDD-3_1_ / ddd-3.1.1 / termcap / tparam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-30  |  7.3 KB  |  322 lines

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