]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
fix returning strings in whiptail and whiptcl (#752818)
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 10 Nov 2011 15:04:45 +0000 (16:04 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 10 Nov 2011 15:04:45 +0000 (16:04 +0100)
dialogboxes.c
dialogboxes.h
whiptail.c
whiptcl.c

index 4850e5566b00e64edf7430dacb64ba662eafe4b8..c518f1628e2fb8e3a966b98d1bbc69421c73fccb 100644 (file)
@@ -192,7 +192,7 @@ int gauge(const char * text, int height, int width, poptContext optCon, int fd,
 }
 
 int inputBox(const char * text, int height, int width, poptContext optCon, 
-               int flags, const char ** result) {
+               int flags, char ** result) {
     newtComponent form, entry, okay, cancel, answer, tb;
     const char * val;
     int pFlag = (flags & FLAG_PASSWORD) ? NEWT_FLAG_PASSWORD : 0;
@@ -212,12 +212,13 @@ int inputBox(const char * text, int height, int width, poptContext optCon,
     addButtons(height, width, form, &okay, &cancel, flags);
 
     answer = newtRunForm(form);
+    *result = NULL;
     if (answer == cancel)
        rc = DLG_CANCEL;
     else if (answer == NULL)
        rc = DLG_ESCAPE;
-
-    *result = val;
+    else
+       *result = strdup(val);
 
     newtFormDestroy(form);
 
@@ -254,7 +255,7 @@ static int mystrncpyw(char *dest, const char *src, int n, int *maxwidth)
 }
 
 int listBox(const char * text, int height, int width, poptContext optCon,
-               int flags, const char *default_item, const char ** result) {
+               int flags, const char *default_item, char ** result) {
     newtComponent form, okay, tb, answer, listBox;
     newtComponent cancel = NULL;
     const char * arg;
@@ -372,13 +373,15 @@ int listBox(const char * text, int height, int width, poptContext optCon,
     addButtons(height, width, form, &okay, &cancel, flags);
 
     answer = newtRunForm(form);
+    *result = NULL;
     if (answer == cancel)
        rc = DLG_CANCEL;
     if (answer == NULL)
        rc = DLG_ESCAPE;
-
-    i = (long) newtListboxGetCurrent(listBox);
-    *result = itemInfo[i].tag;
+    else {
+       i = (long) newtListboxGetCurrent(listBox);
+       *result = strdup(itemInfo[i].tag);
+    }
 
     newtFormDestroy(form);
     free(itemInfo);
@@ -387,7 +390,7 @@ int listBox(const char * text, int height, int width, poptContext optCon,
 }
 
 int checkList(const char * text, int height, int width, poptContext optCon,
-               int useRadio, int flags, const char *** selections) {
+               int useRadio, int flags, char *** selections) {
     newtComponent form, okay, tb, subform, answer;
     newtComponent sb = NULL, cancel = NULL;
     const char * arg;
@@ -481,38 +484,41 @@ int checkList(const char * text, int height, int width, poptContext optCon,
     addButtons(height, width, form, &okay, &cancel, flags);
 
     answer = newtRunForm(form);
+    *selections = NULL;
     if (answer == cancel)
        rc = DLG_CANCEL;
     if (answer == NULL)
        rc = DLG_ESCAPE;
-
-    if (useRadio) {
-       answer = newtRadioGetCurrent(cbInfo[0].comp);
-       *selections = malloc(sizeof(char *) * 2);
-       if (*selections == NULL)
-           return DLG_ERROR;
-       (*selections)[0] = (*selections)[1] = NULL;
-       for (i = 0; i < numBoxes; i++) 
-           if (cbInfo[i].comp == answer) {
-               (*selections)[0] = cbInfo[i].tag;
-               break;
+    else {
+       if (useRadio) {
+           answer = newtRadioGetCurrent(cbInfo[0].comp);
+           *selections = malloc(sizeof(char *) * 2);
+           if (*selections == NULL)
+               return DLG_ERROR;
+           (*selections)[0] = (*selections)[1] = NULL;
+           for (i = 0; i < numBoxes; i++)
+               if (cbInfo[i].comp == answer) {
+                   (*selections)[0] = strdup(cbInfo[i].tag);
+                   break;
+               }
+       } else {
+           numSelected = 0;
+           for (i = 0; i < numBoxes; i++) {
+               if (cbStates[i] != ' ') numSelected++;
            }
-    } else {
-       numSelected = 0;
-       for (i = 0; i < numBoxes; i++) {
-           if (cbStates[i] != ' ') numSelected++;
-       }
 
-       *selections = malloc(sizeof(char *) * (numSelected + 1));
-       if (*selections == NULL) return DLG_ERROR;
+           *selections = malloc(sizeof(char *) * (numSelected + 1));
+           if (*selections == NULL)
+               return DLG_ERROR;
 
-       numSelected = 0;
-       for (i = 0; i < numBoxes; i++) {
-           if (cbStates[i] != ' ') 
-               (*selections)[numSelected++] = cbInfo[i].tag;
-       }
+           numSelected = 0;
+           for (i = 0; i < numBoxes; i++) {
+               if (cbStates[i] != ' ')
+                   (*selections)[numSelected++] = strdup(cbInfo[i].tag);
+           }
 
-       (*selections)[numSelected] = NULL;
+           (*selections)[numSelected] = NULL;
+       }
     }
 
     newtFormDestroy(form);
index 6d133e161544405734c2fc5c7ed759bedae355db..ca51090aab370f6933392f6d26036fb9518f7e18 100644 (file)
@@ -29,11 +29,11 @@ int max(int a, int b);
 
 int messageBox(const char * text, int height, int width, int type, int flags);
 int checkList(const char * text, int height, int width, poptContext optCon,
-               int useRadio, int flags, const char *** selections);
+               int useRadio, int flags, char *** selections);
 int listBox(const char * text, int height, int width, poptContext optCon,
-               int flags, const char *default_item, const char ** result);
+               int flags, const char *default_item, char ** result);
 int inputBox(const char * text, int height, int width, poptContext optCon, 
-               int flags, const char ** result);
+               int flags, char ** result);
 int gauge(const char * text, int height, int width, poptContext optCon, int fd, 
                int flags);
 void useFullButtons(int state);
index 7aa811ac945e019b9b41204d8670020567929e7a..a631bc479f9646ce75f4bebda19528230b16b801 100644 (file)
@@ -344,8 +344,8 @@ int main(int argc, const char ** argv) {
     int outputfd = 2;
     int topLeft = 0;
     FILE *output = stderr;
-    const char * result;
-    const char ** selections, ** next;
+    char * result;
+    char ** selections, ** next;
     char * title = NULL;
     char *default_item = NULL;
     char * backtitle = NULL;
@@ -556,25 +556,34 @@ int main(int argc, const char ** argv) {
 
       case MODE_INPUTBOX:
        rc = inputBox(text, height, width, optCon, flags, &result);
-       if (rc == DLG_OKAY) fprintf(output, "%s", result);
+       if (rc == DLG_OKAY) {
+           fprintf(output, "%s", result);
+           free(result);
+       }
        break;
 
       case MODE_PASSWORDBOX:
        rc = inputBox(text, height, width, optCon, flags | FLAG_PASSWORD,
              &result);
-       if (rc == DLG_OKAY) fprintf (output, "%s", result);
+       if (rc == DLG_OKAY) {
+           fprintf (output, "%s", result);
+           free(result);
+       }
        break;
 
       case MODE_MENU:
        rc = listBox(text, height, width, optCon, flags, default_item, &result);
-       if (rc == DLG_OKAY) fprintf(output, "%s", result);
+       if (rc == DLG_OKAY) {
+           fprintf(output, "%s", result);
+           free(result);
+       }
        break;
 
       case MODE_RADIOLIST:
        rc = checkList(text, height, width, optCon, 1, flags, &selections);
-       if (rc == DLG_OKAY) {
-           if (selections[0])
-               fprintf(output, "%s", selections[0]);
+       if (rc == DLG_OKAY && selections[0]) {
+           fprintf(output, "%s", selections[0]);
+           free(selections[0]);
            free(selections);
        }
        break;
@@ -591,6 +600,7 @@ int main(int argc, const char ** argv) {
                } else {
                    fprintf(output, "%s\n", *next);
                }
+               free(*next);
            }
 
            free(selections);
index 82c12ab55c5b68d165b2cdbde35f0ecf13065f31..8688780123754db9bbb683da9787b23165d16af6 100644 (file)
--- a/whiptcl.c
+++ b/whiptcl.c
@@ -71,8 +71,8 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc,
     int rc = 0;
     int flags = 0;
     int defaultNo = 0;
-    const char * result;
-    const char ** selections, ** next;
+    char * result;
+    char ** selections, ** next;
     char * title = NULL;
     char *default_item = NULL;
     struct poptOption optionsTable[] = {
@@ -205,7 +205,7 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc,
       case MODE_INPUTBOX:
        rc = inputBox(text, height, width, optCon, flags, &result);
        if (rc ==DLG_OKAY) {
-           interp->result = strdup(result);
+           interp->result = result;
            interp->freeProc = TCL_DYNAMIC;
        }
        break;
@@ -213,7 +213,7 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc,
       case MODE_MENU:
        rc = listBox(text, height, width, optCon, flags, default_item, &result);
        if (rc==DLG_OKAY) {
-           interp->result = strdup(result);
+           interp->result = result;
            interp->freeProc = TCL_DYNAMIC;
        }
        break;
@@ -221,8 +221,10 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc,
       case MODE_RADIOLIST:
        rc = checkList(text, height, width, optCon, 1, flags, &selections);
        if (rc==DLG_OKAY) {
-           interp->result = strdup(selections[0]);
+           interp->result = selections[0];
            interp->freeProc = TCL_DYNAMIC;
+
+           free(selections);
        }
        break;