]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
restore automatic menu/list-height in whiptail
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 24 Nov 2022 13:36:39 +0000 (14:36 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 24 Nov 2022 14:40:05 +0000 (15:40 +0100)
Commit 3f5d1d27f09b ("don't modify popt args in whiptail") removed
replacement of the menu/list-height popt argument, which caused the
height to not be automatically calculated together with the window
height and width, when they are specified as zero.

Rework the code to provide the menu/list-height as an additional
integer argument to listBox and checkList instead of replacing the popt
argument.

dialogboxes.c
dialogboxes.h
whiptail.c
whiptcl.c

index cfd37f366244fd3cbccea90104ce5a584a45face..e37cf579d0a9557217c13247f6b1955f89aa333a 100644 (file)
@@ -254,13 +254,12 @@ static int mystrncpyw(char *dest, const char *src, int n, int *maxwidth)
     return i;
 }
 
-int listBox(const char * text, int height, int width, poptContext optCon,
+int listBox(const char * text, int height, int width, int listHeight, poptContext optCon,
                int flags, const char *default_item, char ** result) {
     newtComponent form = NULL, okay, tb, answer, listBox;
     newtComponent cancel = NULL;
     const char * arg;
     char * end;
-    int listHeight;
     int numItems = 0;
     int allocedItems = 5;
     int i, top;
@@ -279,11 +278,6 @@ int listBox(const char * text, int height, int width, poptContext optCon,
 
     if (itemInfo == NULL)
        goto error;
-    if (!(arg = poptGetArg(optCon)))
-       goto error;
-    listHeight = strtoul(arg, &end, 10);
-    if (*end)
-       goto error;
 
     while ((arg = poptGetArg(optCon))) {
        if (allocedItems == numItems) {
@@ -405,13 +399,12 @@ error:
     return rc;
 }
 
-int checkList(const char * text, int height, int width, poptContext optCon,
-               int useRadio, int flags, char *** selections) {
+int checkList(const char * text, int height, int width, int listHeight,
+             poptContext optCon, int useRadio, int flags, char *** selections) {
     newtComponent form = NULL, okay, tb, subform, answer;
     newtComponent sb = NULL, cancel = NULL;
     const char * arg;
     char * end;
-    int listHeight;
     int numBoxes = 0;
     int allocedBoxes = 5;
     int i;
@@ -430,11 +423,6 @@ int checkList(const char * text, int height, int width, poptContext optCon,
 
     if (cbInfo == NULL || cbStates == NULL)
        goto error;
-    if (!(arg = poptGetArg(optCon)))
-       goto error;
-    listHeight = strtoul(arg, &end, 10);
-    if (*end)
-       goto error;
 
     while ((arg = poptGetArg(optCon))) {
        if (allocedBoxes == numBoxes) {
index ca51090aab370f6933392f6d26036fb9518f7e18..19389c6e09baedbed184498bd213261a9252bf15 100644 (file)
@@ -28,9 +28,9 @@ int min(int a, int b);
 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 checkList(const char * text, int height, int width, int list_height, poptContext optCon,
                int useRadio, int flags, char *** selections);
-int listBox(const char * text, int height, int width, poptContext optCon,
+int listBox(const char * text, int height, int width, int list_height, poptContext optCon,
                int flags, const char *default_item, char ** result);
 int inputBox(const char * text, int height, int width, poptContext optCon, 
                int flags, char ** result);
index 8d3d2560b13c6dcbda76b1fd68ba21381b7f231b..773793a795412b16eff1692f4d828f796180832b 100644 (file)
@@ -199,8 +199,8 @@ static void spaceForButtons(int * height, int * width, int count, int full) {
     }
 }
 
-static int menuSize(int * height, int * width, enum mode mode,
-                   poptContext options) {
+static int menuSize(int * height, int * width, int * listHeight,
+                    enum mode mode, poptContext options) {
     const char ** argv = poptGetArgs(options);
     int         h = 0;
     int         tagWidth = 0;
@@ -210,7 +210,6 @@ static int menuSize(int * height, int * width, enum mode mode,
     if ( argv == 0 || *argv == 0 )
        return 0;
 
-    argv++;
     if ( mode == MODE_MENU )
        overhead = 5;
 
@@ -230,14 +229,15 @@ static int menuSize(int * height, int * width, enum mode mode,
 
     h = min(h, SLtt_Screen_Rows - *height - 4);
     *height = *height + h + 1;
+    *listHeight = h;
     return 0;
 }
 
 /*
  * Guess the size of a window, given what will be displayed within it.
  */
-static void guessSize(int * height, int * width, enum mode mode,
-                     int * flags, int fullButtons,
+static void guessSize(int * height, int * width, int * listHeight,
+                     enum mode mode, int * flags, int fullButtons,
                      const char * title, const char * text,
                      poptContext options) {
 
@@ -255,7 +255,7 @@ static void guessSize(int * height, int * width, enum mode mode,
        case MODE_MENU:
            spaceForButtons(&h, &w, *flags & FLAG_NOCANCEL ? 1 : 2,
             fullButtons);
-           menuSize(&h, &w, mode, options);
+           menuSize(&h, &w, listHeight, mode, options);
                break;
        case MODE_YESNO:
        case MODE_MSGBOX:
@@ -328,6 +328,7 @@ int main(int argc, const char ** argv) {
     char * end;
     int height;
     int width;
+    int listHeight = 0;
     int fd = -1;
     int needSpace = 0;
     int noCancel = 0;
@@ -495,7 +496,16 @@ int main(int argc, const char ** argv) {
     width = strtoul(nextArg, &end, 10);
     if (*end) usage(WAS_ERROR);
 
-    if (mode == MODE_GAUGE) {
+    switch (mode) {
+      case MODE_MENU:
+      case MODE_RADIOLIST:
+      case MODE_CHECKLIST:
+       if (!(nextArg = poptGetArg(optCon))) usage(WAS_ERROR);
+       listHeight = strtoul(nextArg, &end, 10);
+       if (*end) usage(WAS_ERROR);
+       break;
+
+      case MODE_GAUGE:
        fd = dup(0);
        if (fd < 0 || close(0) < 0) {
            perror("dup/close stdin");
@@ -505,6 +515,7 @@ int main(int argc, const char ** argv) {
            perror("open /dev/tty");
            exit(DLG_ERROR);
        }
+       break;
     }
 
     newtInit();
@@ -513,8 +524,8 @@ int main(int argc, const char ** argv) {
     cleanNewlines(text);
 
     if ( height <= 0 || width <= 0 )
-       guessSize(&height, &width, mode, &flags, fullButtons, title, text,
-                 optCon);
+       guessSize(&height, &width, &listHeight, mode, &flags, fullButtons,
+                title, text, optCon);
 
     width -= 2;
     height -= 2;
@@ -571,7 +582,7 @@ int main(int argc, const char ** argv) {
        break;
 
       case MODE_MENU:
-       rc = listBox(text, height, width, optCon, flags, default_item, &result);
+       rc = listBox(text, height, width, listHeight, optCon, flags, default_item, &result);
        if (rc == DLG_OKAY) {
            fprintf(output, "%s", result);
            free(result);
@@ -579,7 +590,7 @@ int main(int argc, const char ** argv) {
        break;
 
       case MODE_RADIOLIST:
-       rc = checkList(text, height, width, optCon, 1, flags, &selections);
+       rc = checkList(text, height, width, listHeight, optCon, 1, flags, &selections);
        if (rc == DLG_OKAY && selections[0]) {
            fprintf(output, "%s", selections[0]);
            free(selections[0]);
@@ -588,7 +599,7 @@ int main(int argc, const char ** argv) {
        break;
 
       case MODE_CHECKLIST:
-       rc = checkList(text, height, width, optCon, 0, flags, &selections);
+       rc = checkList(text, height, width, listHeight, optCon, 0, flags, &selections);
 
        if (!rc) {
            for (next = selections; *next; next++) {
index 8688780123754db9bbb683da9787b23165d16af6..c5762d9d22e988878d4e8274dc5400af7fa6cf1b 100644 (file)
--- a/whiptcl.c
+++ b/whiptcl.c
@@ -65,6 +65,7 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc,
     char * end;
     int height;
     int width;
+    int listHeight;
     int noCancel = 0;
     int noItem = 0;
     int scrollText = 0;
@@ -179,6 +180,22 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc,
        return TCL_ERROR;
     }
 
+    switch (mode) {
+      case MODE_MENU:
+      case MODE_RADIOLIST:
+      case MODE_CHECKLIST:
+       if (!(nextArg = poptGetArg(optCon))) {
+           interp->result = "list-height missing";
+           return TCL_ERROR;
+       }
+       listHeight = strtoul(nextArg, &end, 10);
+       if (*end) {
+           interp->result = "list-height is not a number";
+           return TCL_ERROR;
+       }
+       break;
+    }
+
     width -= 2;
     height -= 2;
     newtOpenWindow((80 - width) / 2, (24 - height) / 2, width, height, title);
@@ -211,7 +228,8 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc,
        break;
 
       case MODE_MENU:
-       rc = listBox(text, height, width, optCon, flags, default_item, &result);
+       rc = listBox(text, height, width, listHeight, optCon, flags, default_item,
+                    &result);
        if (rc==DLG_OKAY) {
            interp->result = result;
            interp->freeProc = TCL_DYNAMIC;
@@ -219,7 +237,8 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc,
        break;
 
       case MODE_RADIOLIST:
-       rc = checkList(text, height, width, optCon, 1, flags, &selections);
+       rc = checkList(text, height, width, listHeight, optCon, 1, flags,
+                      &selections);
        if (rc==DLG_OKAY) {
            interp->result = selections[0];
            interp->freeProc = TCL_DYNAMIC;
@@ -229,7 +248,8 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc,
        break;
 
       case MODE_CHECKLIST:
-       rc = checkList(text, height, width, optCon, 0, flags, &selections);
+       rc = checkList(text, height, width, listHeight, optCon, 0, flags,
+                      &selections);
 
        if (rc==DLG_OKAY) {
            for (next = selections; *next; next++)