Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libedit change allocation policy in el_push to allocate ...



details:   https://anonhg.NetBSD.org/src/rev/5dfe303b79b8
branches:  trunk
changeset: 553580:5dfe303b79b8
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Oct 18 23:48:42 2003 +0000

description:
change allocation policy in el_push to allocate the string itself.
fix issues with strdup.

diffstat:

 lib/libedit/chared.c    |   6 +++---
 lib/libedit/chared.h    |   4 ++--
 lib/libedit/el.c        |  11 +++++++----
 lib/libedit/el.h        |   3 ++-
 lib/libedit/history.c   |   7 ++++---
 lib/libedit/key.c       |   7 ++++---
 lib/libedit/read.c      |  34 ++++++++++++++++------------------
 lib/libedit/tokenizer.c |   7 ++++---
 8 files changed, 42 insertions(+), 37 deletions(-)

diffs (280 lines):

diff -r 1f7a185b3ce3 -r 5dfe303b79b8 lib/libedit/chared.c
--- a/lib/libedit/chared.c      Sat Oct 18 23:46:26 2003 +0000
+++ b/lib/libedit/chared.c      Sat Oct 18 23:48:42 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chared.c,v 1.19 2003/08/07 16:44:30 agc Exp $  */
+/*     $NetBSD: chared.c,v 1.20 2003/10/18 23:48:42 christos Exp $     */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)chared.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: chared.c,v 1.19 2003/08/07 16:44:30 agc Exp $");
+__RCSID("$NetBSD: chared.c,v 1.20 2003/10/18 23:48:42 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -456,8 +456,8 @@
        el->el_state.argument           = 1;
        el->el_state.lastcmd            = ED_UNASSIGNED;
 
-       el->el_chared.c_macro.nline     = NULL;
        el->el_chared.c_macro.level     = -1;
+       el->el_chared.c_macro.offset    = 0;
        el->el_chared.c_macro.macro     = (char **) el_malloc(EL_MAXMACRO *
            sizeof(char *));
        if (el->el_chared.c_macro.macro == NULL)
diff -r 1f7a185b3ce3 -r 5dfe303b79b8 lib/libedit/chared.h
--- a/lib/libedit/chared.h      Sat Oct 18 23:46:26 2003 +0000
+++ b/lib/libedit/chared.h      Sat Oct 18 23:48:42 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chared.h,v 1.12 2003/08/07 16:44:30 agc Exp $  */
+/*     $NetBSD: chared.h,v 1.13 2003/10/18 23:48:42 christos Exp $     */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -62,8 +62,8 @@
 
 typedef struct c_macro_t {
        int       level;
+       int       offset;
        char    **macro;
-       char     *nline;
 } c_macro_t;
 
 /*
diff -r 1f7a185b3ce3 -r 5dfe303b79b8 lib/libedit/el.c
--- a/lib/libedit/el.c  Sat Oct 18 23:46:26 2003 +0000
+++ b/lib/libedit/el.c  Sat Oct 18 23:48:42 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: el.c,v 1.35 2003/10/16 22:26:32 christos Exp $ */
+/*     $NetBSD: el.c,v 1.36 2003/10/18 23:48:42 christos Exp $ */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)el.c       8.2 (Berkeley) 1/3/94";
 #else
-__RCSID("$NetBSD: el.c,v 1.35 2003/10/16 22:26:32 christos Exp $");
+__RCSID("$NetBSD: el.c,v 1.36 2003/10/18 23:48:42 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -68,7 +68,10 @@
        el->el_infd = fileno(fin);
        el->el_outfile = fout;
        el->el_errfile = ferr;
-       el->el_prog = strdup(prog);
+       if ((el->el_prog = el_strdup(prog)) == NULL) {
+               el_free(el);
+               return NULL;
+       }
 
        /*
          * Initialize all the modules. Order is important!!!
@@ -76,7 +79,7 @@
        el->el_flags = 0;
 
        if (term_init(el) == -1) {
-               free(el->el_prog);
+               el_free(el->el_prog);
                el_free(el);
                return NULL;
        }
diff -r 1f7a185b3ce3 -r 5dfe303b79b8 lib/libedit/el.h
--- a/lib/libedit/el.h  Sat Oct 18 23:46:26 2003 +0000
+++ b/lib/libedit/el.h  Sat Oct 18 23:48:42 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: el.h,v 1.15 2003/09/26 17:44:51 christos Exp $ */
+/*     $NetBSD: el.h,v 1.16 2003/10/18 23:48:42 christos Exp $ */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -88,6 +88,7 @@
 /*
  * Until we come up with something better...
  */
+#define        el_strdup(a)    strdup(a)
 #define        el_malloc(a)    malloc(a)
 #define        el_realloc(a,b) realloc(a, b)
 #define        el_free(a)      free(a)
diff -r 1f7a185b3ce3 -r 5dfe303b79b8 lib/libedit/history.c
--- a/lib/libedit/history.c     Sat Oct 18 23:46:26 2003 +0000
+++ b/lib/libedit/history.c     Sat Oct 18 23:48:42 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: history.c,v 1.24 2003/08/07 16:44:31 agc Exp $ */
+/*     $NetBSD: history.c,v 1.25 2003/10/18 23:48:42 christos Exp $    */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)history.c  8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: history.c,v 1.24 2003/08/07 16:44:31 agc Exp $");
+__RCSID("$NetBSD: history.c,v 1.25 2003/10/18 23:48:42 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -87,6 +87,7 @@
 #define        HENTER(h, ev, str)      (*(h)->h_enter)((h)->h_ref, ev, str)
 #define        HADD(h, ev, str)        (*(h)->h_add)((h)->h_ref, ev, str)
 
+#define        h_strdup(a)     strdup(a)
 #define        h_malloc(a)     malloc(a)
 #define        h_realloc(a, b) realloc((a), (b))
 #define        h_free(a)       free(a)
@@ -394,7 +395,7 @@
        h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t));
        if (h->cursor == NULL)
                goto oomem;
-       if ((h->cursor->ev.str = strdup(str)) == NULL) {
+       if ((h->cursor->ev.str = h_strdup(str)) == NULL) {
                h_free((ptr_t)h->cursor);
                goto oomem;
        }
diff -r 1f7a185b3ce3 -r 5dfe303b79b8 lib/libedit/key.c
--- a/lib/libedit/key.c Sat Oct 18 23:46:26 2003 +0000
+++ b/lib/libedit/key.c Sat Oct 18 23:48:42 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: key.c,v 1.14 2003/08/07 16:44:31 agc Exp $     */
+/*     $NetBSD: key.c,v 1.15 2003/10/18 23:48:42 christos Exp $        */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)key.c      8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: key.c,v 1.14 2003/08/07 16:44:31 agc Exp $");
+__RCSID("$NetBSD: key.c,v 1.15 2003/10/18 23:48:42 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -352,7 +352,8 @@
                        break;
                case XK_STR:
                case XK_EXE:
-                       ptr->val.str = strdup(val->str);
+                       if ((ptr->val.str = el_strdup(val->str)) == NULL)
+                               return -1;
                        break;
                default:
                        EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype));
diff -r 1f7a185b3ce3 -r 5dfe303b79b8 lib/libedit/read.c
--- a/lib/libedit/read.c        Sat Oct 18 23:46:26 2003 +0000
+++ b/lib/libedit/read.c        Sat Oct 18 23:48:42 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: read.c,v 1.29 2003/10/09 00:42:28 christos Exp $       */
+/*     $NetBSD: read.c,v 1.30 2003/10/18 23:48:42 christos Exp $       */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)read.c     8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: read.c,v 1.29 2003/10/09 00:42:28 christos Exp $");
+__RCSID("$NetBSD: read.c,v 1.30 2003/10/18 23:48:42 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -187,10 +187,6 @@
 {
        int chrs = 0;
 
-       if (el->el_chared.c_macro.nline) {
-               el_free((ptr_t) el->el_chared.c_macro.nline);
-               el->el_chared.c_macro.nline = NULL;
-       }
        if (el->el_tty.t_mode == ED_IO)
                return (0);
 
@@ -203,8 +199,7 @@
                    (size_t) MIN(chrs, EL_BUFSIZ - 1));
                if (chrs > 0) {
                        buf[chrs] = '\0';
-                       el->el_chared.c_macro.nline = strdup(buf);
-                       el_push(el, el->el_chared.c_macro.nline);
+                       el_push(el, buf);
                }
        }
 #endif /* FIONREAD */
@@ -223,11 +218,12 @@
 
        if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
                ma->level++;
-               ma->macro[ma->level] = str;
-       } else {
-               term_beep(el);
-               term__flush();
+               if ((ma->macro[ma->level] = el_strdup(str)) != NULL)
+                       return;
+               ma->level--;
        }
+       term_beep(el);
+       term__flush();
 }
 
 
@@ -324,14 +320,16 @@
                if (ma->level < 0)
                        break;
 
-               if (*ma->macro[ma->level] == 0) {
-                       ma->level--;
+               if (ma->macro[ma->level][ma->offset] == '\0') {
+                       el_free(ma->macro[ma->level--]);
+                       ma->offset = 0;
                        continue;
                }
-               *cp = *ma->macro[ma->level]++ & 0377;
-               if (*ma->macro[ma->level] == 0) {       /* Needed for QuoteMode
-                                                        * On */
-                       ma->level--;
+               *cp = ma->macro[ma->level][ma->offset++] & 0377;
+               if (ma->macro[ma->level][ma->offset] == '\0') {
+                       /* Needed for QuoteMode On */
+                       el_free(ma->macro[ma->level--]);
+                       ma->offset = 0;
                }
                return (1);
        }
diff -r 1f7a185b3ce3 -r 5dfe303b79b8 lib/libedit/tokenizer.c
--- a/lib/libedit/tokenizer.c   Sat Oct 18 23:46:26 2003 +0000
+++ b/lib/libedit/tokenizer.c   Sat Oct 18 23:48:42 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tokenizer.c,v 1.12 2003/08/07 16:44:34 agc Exp $       */
+/*     $NetBSD: tokenizer.c,v 1.13 2003/10/18 23:48:42 christos Exp $  */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)tokenizer.c        8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: tokenizer.c,v 1.12 2003/08/07 16:44:34 agc Exp $");
+__RCSID("$NetBSD: tokenizer.c,v 1.13 2003/10/18 23:48:42 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -60,6 +60,7 @@
 #define        WINCR           20
 #define        AINCR           10
 
+#define        tok_strdup(a)           strdup(a)
 #define        tok_malloc(a)           malloc(a)
 #define        tok_free(a)             free(a)
 #define        tok_realloc(a, b)       realloc(a, b)
@@ -107,7 +108,7 @@
 
        if (tok == NULL)
                return NULL;
-       tok->ifs = strdup(ifs ? ifs : IFS);
+       tok->ifs = tok_strdup(ifs ? ifs : IFS);
        if (tok->ifs == NULL) {
                tok_free((ptr_t)tok);
                return NULL;



Home | Main Index | Thread Index | Old Index