]> code.delx.au - gnu-emacs/blobdiff - src/tparam.c
Doc tweak
[gnu-emacs] / src / tparam.c
index 6aae0b97db982cc4ad4159119ae774874e2b7b3b..d3ae4910b565c75fca7d3a36a9ac82e575c7e069 100644 (file)
@@ -1,6 +1,6 @@
 /* Merge parameters into a termcap entry string.
-   Copyright (C) 1985, 1987, 1993, 1995, 2000, 2001, 2002, 2003, 2004,
-                 2005, 2006, 2007, 2008  Free Software Foundation, Inc.
+   Copyright (C) 1985, 1987, 1993, 1995, 2000-2008, 2013-2016 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,19 +13,13 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 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, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* Emacs config.h may rename various library functions such as malloc.  */
 #include <config.h>
-#include <setjmp.h>
+
 #include "lisp.h"              /* for xmalloc */
 #include "tparam.h"
-
-#ifndef NULL
-#define NULL (char *) 0
-#endif
 \f
 /* Assuming STRING is the value of a termcap string entry
    containing `%' constructs to expand parameters,
@@ -79,35 +73,35 @@ tparam1 (const char *string, char *outstring, int len,
   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;
+  bool explicit_param_p = false;        /* set by %p */
+  ptrdiff_t doleft = 0;
+  ptrdiff_t doup = 0;
+  ptrdiff_t append_len = 0;
 
   outend = outstring + len;
 
-  while (1)
+  while (true)
     {
       /* 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)
            {
              outlen = len + 40;
-             new = (char *) xmalloc (outlen);
+             new = xmalloc (outlen);
              memcpy (new, outstring, offset);
            }
          else
            {
-             outlen *= 2;
-             new = (char *) xrealloc (outstring, outlen);
+             new = xpalloc (outstring, &outlen, 1, -1, 1);
            }
 
          op = new + offset;
@@ -121,7 +115,7 @@ tparam1 (const char *string, char *outstring, int len,
        {
          c = *p++;
          if (explicit_param_p)
-           explicit_param_p = 0;
+           explicit_param_p = false;
          else
            tem = *argp;
          switch (c)
@@ -148,7 +142,7 @@ tparam1 (const char *string, char *outstring, int len,
              break;
             case 'p':           /* %pN means use param N for next subst.  */
              tem = fixed_argp[(*p++) - '1'];
-             explicit_param_p = 1;
+             explicit_param_p = true;
              break;
            case 'C':
              /* For c-100: print quotient of value by 96, if nonzero,
@@ -167,11 +161,15 @@ tparam1 (const char *string, char *outstring, int len,
                     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_WRAPV (append_len_incr,
+                                        append_len, &append_len))
+                       memory_full (SIZE_MAX);
                    }
                }
              *op++ = tem ? tem : 0200;
@@ -247,7 +245,7 @@ tparam1 (const char *string, char *outstring, int len,
              break;
 
            default:
-             abort ();
+             emacs_abort ();
            }
        }
       else
@@ -257,24 +255,23 @@ tparam1 (const char *string, char *outstring, int len,
     }
   *op = 0;
   while (doup-- > 0)
-    strcat (op, up);
+    op = stpcpy (op, up);
   while (doleft-- > 0)
-    strcat (op, left);
+    op = stpcpy (op, left);
   return outstring;
 }
 \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;
 }