Add changes to lisp/url/ChangeLog omitted from 2011-04-02T23:41:03Z!cyd@stupidchicken...
[bpt/emacs.git] / src / tparam.c
index 2470289..6aae0b9 100644 (file)
@@ -1,5 +1,6 @@
 /* Merge parameters into a termcap entry string.
-   Copyright (C) 1985, 87, 93, 95 Free Software Foundation, Inc.
+   Copyright (C) 1985, 1987, 1993, 1995, 2000, 2001, 2002, 2003, 2004,
+                 2005, 2006, 2007, 2008  Free Software Foundation, Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -13,63 +14,19 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* Emacs config.h may rename various library functions such as malloc.  */
-#ifdef HAVE_CONFIG_H
 #include <config.h>
-#else /* not HAVE_CONFIG_H */
-
-#if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
-#define bcopy(s, d, n) memcpy ((d), (s), (n))
-#endif
-
-#ifdef STDC_HEADERS
-#include <stdlib.h>
-#include <string.h>
-#else
-char *malloc ();
-char *realloc ();
-#endif
-
-#endif /* not HAVE_CONFIG_H */
+#include <setjmp.h>
+#include "lisp.h"              /* for xmalloc */
+#include "tparam.h"
 
 #ifndef NULL
 #define NULL (char *) 0
 #endif
 \f
-#ifndef emacs
-static void
-memory_out ()
-{
-  write (2, "virtual memory exhausted\n", 25);
-  exit (1);
-}
-
-static char *
-xmalloc (size)
-     unsigned size;
-{
-  register char *tem = malloc (size);
-
-  if (!tem)
-    memory_out ();
-  return tem;
-}
-
-static char *
-xrealloc (ptr, size)
-     char *ptr;
-     unsigned size;
-{
-  register char *tem = realloc (ptr, size);
-
-  if (!tem)
-    memory_out ();
-  return tem;
-}
-#endif /* not emacs */
-\f
 /* Assuming STRING is the value of a termcap string entry
    containing `%' constructs to expand parameters,
    merge in parameter values and store result in block OUTSTRING points to.
@@ -82,15 +39,12 @@ xrealloc (ptr, size)
 
    The fourth and following args to tparam serve as the parameter values.  */
 
-static char *tparam1 ();
+static char *tparam1 (char const *string, char *outstring, int len,
+                     char *up, char *left, int *argp);
 
-/* VARARGS 2 */
 char *
-tparam (string, outstring, len, arg0, arg1, arg2, arg3)
-     char *string;
-     char *outstring;
-     int len;
-     int arg0, arg1, arg2, arg3;
+tparam (const char *string, char *outstring, int len,
+       int arg0, int arg1, int arg2, int arg3)
 {
   int arg[4];
 
@@ -107,9 +61,7 @@ char *UP;
 static char tgoto_buf[50];
 
 char *
-tgoto (cm, hpos, vpos)
-     char *cm;
-     int hpos, vpos;
+tgoto (const char *cm, int hpos, int vpos)
 {
   int args[2];
   if (!cm)
@@ -120,21 +72,19 @@ tgoto (cm, hpos, vpos)
 }
 
 static char *
-tparam1 (string, outstring, len, up, left, argp)
-     char *string;
-     char *outstring;
-     int len;
-     char *up, *left;
-     register int *argp;
+tparam1 (const char *string, char *outstring, int len,
+        char *up, char *left, register int *argp)
 {
   register int c;
-  register char *p = string;
+  register const char *p = string;
   register char *op = outstring;
   char *outend;
   int outlen = 0;
 
   register int tem;
-  int *old_argp = argp;
+  int *old_argp = argp;                 /* can move */
+  int *fixed_argp = argp;               /* never moves */
+  int explicit_param_p = 0;             /* set by %p */
   int doleft = 0;
   int doup = 0;
 
@@ -146,21 +96,22 @@ tparam1 (string, outstring, len, up, left, argp)
       if (op + 5 >= outend)
        {
          register char *new;
+         int offset = op - outstring;
+
          if (outlen == 0)
            {
              outlen = len + 40;
              new = (char *) xmalloc (outlen);
-             outend += 40;
-             bcopy (outstring, new, op - outstring);
+             memcpy (new, outstring, offset);
            }
          else
            {
-             outend += outlen;
              outlen *= 2;
              new = (char *) xrealloc (outstring, outlen);
            }
-         op += new - outstring;
-         outend += new - outstring;
+
+         op = new + offset;
+         outend = new + outlen;
          outstring = new;
        }
       c = *p++;
@@ -169,7 +120,10 @@ tparam1 (string, outstring, len, up, left, argp)
       if (c == '%')
        {
          c = *p++;
-         tem = *argp;
+         if (explicit_param_p)
+           explicit_param_p = 0;
+         else
+           tem = *argp;
          switch (c)
            {
            case 'd':           /* %d means output in decimal.  */
@@ -192,7 +146,10 @@ tparam1 (string, outstring, len, up, left, argp)
              *op++ = tem % 10 + '0';
              argp++;
              break;
-
+            case 'p':           /* %pN means use param N for next subst.  */
+             tem = fixed_argp[(*p++) - '1'];
+             explicit_param_p = 1;
+             break;
            case 'C':
              /* For c-100: print quotient of value by 96, if nonzero,
                 then do like %+.  */
@@ -288,6 +245,9 @@ tparam1 (string, outstring, len, up, left, argp)
            case 'D':           /* %D means weird Delta Data transformation.  */
              argp[0] -= 2 * (tem % 16);
              break;
+
+           default:
+             abort ();
            }
        }
       else