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