Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/sushi Major cleanup:



details:   https://anonhg.NetBSD.org/src/rev/4f43e68562bb
branches:  trunk
changeset: 503093:4f43e68562bb
user:      garbled <garbled%NetBSD.org@localhost>
date:      Wed Jan 31 09:35:42 2001 +0000

description:
Major cleanup:

Add new form keyword, iscript, which allows script generated integer fields.
This uncovered a whole host of bugs, which uncovered a whole host of memory
leaks, which hopefully I've fixed most of now (in this file at least).

diffstat:

 usr.sbin/sushi/formtree.h |    4 +-
 usr.sbin/sushi/scanform.c |  322 +++++++++++++++++++++++++++++++++++++--------
 2 files changed, 267 insertions(+), 59 deletions(-)

diffs (truncated from 656 to 300 lines):

diff -r 1066019758b9 -r 4f43e68562bb usr.sbin/sushi/formtree.h
--- a/usr.sbin/sushi/formtree.h Wed Jan 31 07:44:51 2001 +0000
+++ b/usr.sbin/sushi/formtree.h Wed Jan 31 09:35:42 2001 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: formtree.h,v 1.2 2001/01/10 03:05:48 garbled Exp $       */
+/*      $NetBSD: formtree.h,v 1.3 2001/01/31 09:35:42 garbled Exp $       */
 
 /*
  * Copyright (c) 2000 The NetBSD Foundation, Inc.
@@ -51,6 +51,7 @@
 #define DATAT_MSCRIPT  11      /* multiple selection scriptgen list */
 #define DATAT_ESCRIPT  12      /* script generated entry */
 #define DATAT_NESCRIPT 13      /* script generated uneditable field */
+#define DATAT_ISCRIPT  14      /* script generated integer field */
 
 CIRCLEQ_HEAD(cqForm, formentry);
 
@@ -63,6 +64,7 @@
        int     required;
        int     elen;
        char    *data;
+       char    *origdata; /* to allow free */
        char    **list; /* optional list entry */
 } FTREE_ENTRY;
 
diff -r 1066019758b9 -r 4f43e68562bb usr.sbin/sushi/scanform.c
--- a/usr.sbin/sushi/scanform.c Wed Jan 31 07:44:51 2001 +0000
+++ b/usr.sbin/sushi/scanform.c Wed Jan 31 09:35:42 2001 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: scanform.c,v 1.10 2001/01/24 09:30:30 garbled Exp $       */
+/*      $NetBSD: scanform.c,v 1.11 2001/01/31 09:35:42 garbled Exp $       */
 
 /*
  * Copyright (c) 2000 The NetBSD Foundation, Inc.
@@ -83,7 +83,7 @@
            "PGUP/PGDN to change page, UP/DOWN switch field, ENTER=Do."));
        wstandend(stdscr);
        sprintf(buf, "%s (%d/%d)", catgets(catalog, 4, 8, "Form Page:"),
-           form_page(form)+1, form->max_page);
+           form_page(form)+1, form->max_page); /* XXX */
        mvwaddstr(stdscr, ws.ws_row-3, 60, buf);
        wrefresh(stdscr);
 }
@@ -136,11 +136,11 @@
         if ((fte = malloc(sizeof(FTREE_ENTRY))) == NULL ||
                 ((fte->desc = strdup(desc)) == NULL) ||
                 ((fte->type = type) == NULL) ||
-               ((fte->data = strdup(data)) == NULL) ||
-               ((fte->list = malloc(sizeof(char *)*2)) == NULL))
+               ((fte->data = strdup(data)) == NULL))
                         bailout("malloc: %s", strerror(errno));
        fte->required = req;
        fte->elen = 0;
+       fte->origdata = fte->data;
 
        CIRCLEQ_INIT(&fte->cqSubFormHead);
        CIRCLEQ_INSERT_TAIL(cqf, fte, cqFormEntries);
@@ -202,6 +202,11 @@
        else if (strcmp(x, "req-integer") == 0) {
                type = DATAT_INTEGER;
                req = 1;
+       } else if (strcmp(x, "iscript") == 0)
+               type = DATAT_ISCRIPT;
+       else if (strcmp(x, "req-iscript") == 0) {
+               type = DATAT_ISCRIPT;
+               req = 1;
        } else
                bailout("%s: %s",
                    catgets(catalog, 1, 11, "invalid data type"), x);
@@ -387,7 +392,7 @@
        FIELD *curfield;
        char **list;
        int i, j, y, n, dcols, drows, dmax;
-       char *tmp, *p;
+       char *tmp, *otmp, *p;
        char *choices[] = {" ", "+"};
        char buf[1024];
 
@@ -471,14 +476,15 @@
                if (field_buffer(curfield, 1) == NULL || 
                    field_buffer(curfield, 0) == NULL)
                        return(FALSE);
-               tmp = strdup(field_buffer(curfield, 1));
+               otmp = tmp = strdup(field_buffer(curfield, 1));
                stripWhiteSpace(vBOTH, tmp);
                if (*tmp == 'm') {
                        slist = newCDKSelection(cdkscreen, RIGHT, CENTER,
                            RIGHT, 10, y+2,
                            catgets(catalog, 4, 1, "Select choices"),
                            list, i, choices, 2, A_REVERSE ,TRUE, FALSE);
-                       tmp = strdup(field_buffer(curfield, 0));
+                       free(otmp);
+                       otmp = tmp = strdup(field_buffer(curfield, 0));
                        stripWhiteSpace(vBOTH, tmp);
                        if (*tmp != '\0')
                                for (p = tmp; p != NULL; p = strsep(&tmp, ","))
@@ -498,6 +504,7 @@
                                                sprintf(buf, "%s,", buf);
                                        sprintf(buf, "%s%s", buf, list[y]);
                                }
+                       free(otmp);
                        tmp = buf;
                        set_field_buffer(curfield, 0, tmp);
                        destroyCDKSelection(slist);
@@ -509,6 +516,7 @@
                        if (i != -1)
                                set_field_buffer(curfield, 0, list[i]);
                        destroyCDKScroll(plist);
+                       free(otmp);
                }
                return(FALSE);
                /* NOTREACHED */
@@ -524,7 +532,7 @@
                /* NOTREACHED */
                break;
        case BAIL:
-               return(TRUE);
+               return(3); /* TRUE, so handle_preform can free F */
                /* NOTREACHED */
                break;
        case FASTBAIL:
@@ -543,14 +551,19 @@
        FIELD *f = new_field(1, (int)(strlen(x->v)+2), x->frow, x->fcol, 0, 0);
 
        if (f) {
-               tmp = malloc(sizeof(char *) * (strlen(x->v)+2));
-               *tmp = '\0';
+               x->v = realloc(x->v, sizeof(char *) * (strlen(x->v)+2));
+               tmp = malloc(sizeof(char *) * strlen(x->v));
+               if (x->v == NULL || tmp == NULL)
+                       bailout("malloc: %s", strerror(errno));
+
                if (x->required == 1)
-                       tmp = strcat(tmp, "* ");
+                       (void)strcpy(tmp, "* ");
                else
-                       tmp = strcat(tmp, "  ");
-               tmp = strcat(tmp, x->v);
-               set_field_buffer(f, 0, tmp);
+                       (void)strcpy(tmp, "  ");
+               (void)strcat(tmp, x->v);
+               (void)strcpy(x->v, tmp);
+               set_field_buffer(f, 0, x->v);
+               free(tmp);
                field_opts_off(f, O_ACTIVE);
                if (x->newpage == 1)
                        set_new_page(f, TRUE);
@@ -600,14 +613,20 @@
 {                              /* create an INTEGER field */
        FIELD *f = new_field(x->rows, x->cols, x->frow, x->fcol, 0, 0);
        int pre, min, max;
-       char *p, *q;
+       char *p, *q, *n;
 
-       p = strdup(x->v);
+       p = x->v;
        q = strsep(&p, ",");
        pre = atoi(q);
        q = strsep(&p, ",");
        min = atoi(q);
-       max = atoi(p);
+       n = strdup(p);
+       q = strsep(&p, ",");
+       if (p == NULL)
+               max = atoi(n);
+       else
+               max = atoi(q);
+
        if (f) {
                set_field_back(f, A_UNDERLINE);
                set_field_type(f, TYPE_INTEGER, pre, min, max);
@@ -617,6 +636,8 @@
                        field_opts_off(f, O_NULLOK);
                if (x->bigfield)
                        field_opts_off(f, O_STATIC);
+               if (p != NULL)
+                       set_field_buffer(f, 0, p);
        }
        return f;
 }
@@ -673,7 +694,6 @@
        return f;
 }
 
-static FIELD   *fields[MAX_FIELD + 1];
 FIELD_RECORD *F;
 
 static int
@@ -682,7 +702,7 @@
        char file[PATH_MAX];
        struct stat sb;
        char *p;
-       int lcnt, i;
+       int lcnt, i, j;
        FIELD **f;
        char **args;
 
@@ -720,8 +740,20 @@
                }
        args[i] = NULL;
 
+       for (i=0; F[i].type != NULL; i++) {
+               free(F[i].v);
+               if (F[i].list != NULL) {
+                       for (j=0; F[i].list[j] != NULL; j++)
+                               free(F[i].list[j]);
+                       free(F[i].list);
+               }
+       }
+       free(F);
+
        i = handle_form(path, file, args);
 
+       for (j=0; args[j] != NULL; j++)
+               free(args[j]);
        free(args);
        return(i);
 }
@@ -734,7 +766,7 @@
        struct stat sb;
        char *exec, *t, *p;
        size_t len;
-       int lcnt, i;
+       int lcnt, i, j;
        FIELD **f;
        char **args;
 
@@ -816,16 +848,24 @@
 
        i = run_prog(1, args);
 
+       for (j=0; args[j] != NULL; j++)
+               free(args[j]);
        free(args);
+
        return(i);
 }
 
 static FIELD  **
 make_fields(void)
 {                              /* create the fields */
-       FIELD **f = fields;
+       FIELD **f, **fields;
        int i;
 
+       f = malloc(sizeof(FIELD *) * (MAX_FIELD+1));
+       if (f == NULL)
+               bailout("malloc: %s", strerror(errno));
+       fields = f;
+
        for (i = 0; i < MAX_FIELD && F[i].type; ++i, ++f)
                *f = (F[i].type)(&F[i]);
 
@@ -840,15 +880,13 @@
 tstring(int max, char *string)
 {
        char hold[10];
-       char *str;
        int cur;
 
        if (max == 0)
                return(0);
        for (cur=0; cur <= max; cur++) {
                sprintf(hold, "@@@%d@@@", cur);
-               str = strdup(hold);
-               if (strcmp(str, string) == 0)
+               if (strcmp(hold, string) == 0)
                        return(cur);
        }
        return(0);
@@ -858,7 +896,7 @@
 strlen_data(FTREE_ENTRY *ftp)
 {
        int i, j;
-       char *p, *q;
+       char *p, *q, *o;
 
        i = 0;
        switch(ftp->type) {
@@ -890,9 +928,12 @@
                /* NOTREACHED */
                break;
        case DATAT_INTEGER:
-               p = strdup(ftp->data);
+       case DATAT_ISCRIPT:
+               o = p = strdup(ftp->data);
                q = strsep(&p, ",");
-               return(atoi(q));
+               i = atoi(q);
+               free(o);
+               return(i);
                /* NOTREACHED */
                break;
        default:
@@ -954,40 +995,47 @@
 static void
 gen_script(FTREE_ENTRY *ftp, char *dir, int max, char **args)
 {
-       char *p, *q, *comm, *test;
+       char *p, *q, *qo, *po, *comm, *test;



Home | Main Index | Thread Index | Old Index