Assume C89 or later.
[bpt/emacs.git] / src / tparam.c
index d8a8f02..4d26ef5 100644 (file)
@@ -21,10 +21,7 @@ Boston, MA 02110-1301, USA.  */
 #include <config.h>
 #include <setjmp.h>
 #include "lisp.h"              /* for xmalloc */
-
-#ifndef NULL
-#define NULL (char *) 0
-#endif
+#include "tparam.h"
 \f
 /* Assuming STRING is the value of a termcap string entry
    containing `%' constructs to expand parameters,
@@ -38,11 +35,12 @@ Boston, MA 02110-1301, USA.  */
 
    The fourth and following args to tparam serve as the parameter values.  */
 
-static char *tparam1 (char *string, char *outstring, int len, char *up, char *left, register int *argp);
+static char *tparam1 (char const *string, char *outstring, int len,
+                     char *up, char *left, int *argp);
 
-/* VARARGS 2 */
 char *
-tparam (char *string, char *outstring, int len, int arg0, int arg1, int arg2, int arg3)
+tparam (const char *string, char *outstring, int len,
+       int arg0, int arg1, int arg2, int arg3)
 {
   int arg[4];
 
@@ -59,7 +57,7 @@ char *UP;
 static char tgoto_buf[50];
 
 char *
-tgoto (char *cm, int hpos, int vpos)
+tgoto (const char *cm, int hpos, int vpos)
 {
   int args[2];
   if (!cm)
@@ -70,30 +68,32 @@ tgoto (char *cm, int hpos, int vpos)
 }
 
 static char *
-tparam1 (char *string, char *outstring, int len, char *up, char *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;
+  char *new = 0;
+  ptrdiff_t outlen = 0;
 
   register int tem;
   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;
+  ptrdiff_t doleft = 0;
+  ptrdiff_t doup = 0;
+  ptrdiff_t append_len = 0;
 
   outend = outstring + len;
 
   while (1)
     {
       /* If the buffer might be too short, make it bigger.  */
-      if (op + 5 >= outend)
+      while (outend - op - append_len <= 5)
        {
-         register char *new;
-         int offset = op - outstring;
+         ptrdiff_t offset = op - outstring;
 
          if (outlen == 0)
            {
@@ -103,8 +103,7 @@ tparam1 (char *string, char *outstring, int len, char *up, char *left, register
            }
          else
            {
-             outlen *= 2;
-             new = (char *) xrealloc (outstring, outlen);
+             new = xpalloc (outstring, &outlen, 1, -1, 1);
            }
 
          op = new + offset;
@@ -164,11 +163,15 @@ tparam1 (char *string, char *outstring, int len, char *up, char *left, register
                     and this is one of them, increment it.  */
                  while (tem == 0 || tem == '\n' || tem == '\t')
                    {
+                     ptrdiff_t append_len_incr;
                      tem++;
                      if (argp == old_argp)
-                       doup++, outend -= strlen (up);
+                       doup++, append_len_incr = strlen (up);
                      else
-                       doleft++, outend -= strlen (left);
+                       doleft++, append_len_incr = strlen (left);
+                     if (INT_ADD_OVERFLOW (append_len, append_len_incr))
+                       memory_full (SIZE_MAX);
+                     append_len += append_len_incr;
                    }
                }
              *op++ = tem ? tem : 0200;
@@ -262,21 +265,17 @@ tparam1 (char *string, char *outstring, int len, char *up, char *left, register
 \f
 #ifdef DEBUG
 
-main (argc, argv)
-     int argc;
-     char **argv;
+int
+main (int argc, char **argv)
 {
   char buf[50];
   int args[3];
   args[0] = atoi (argv[2]);
   args[1] = atoi (argv[3]);
   args[2] = atoi (argv[4]);
-  tparam1 (argv[1], buf, "LEFT", "UP", args);
+  tparam1 (argv[1], buf, 50, "LEFT", "UP", args);
   printf ("%s\n", buf);
   return 0;
 }
 
 #endif /* DEBUG */
-
-/* arch-tag: 83f7b5ac-a808-4f75-b87a-123de009b402
-   (do not change this comment) */