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