The branch main has been updated by bapt:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=24b05d7a6190292f62488aa4d7de86246a09467c

commit 24b05d7a6190292f62488aa4d7de86246a09467c
Author:     Baptiste Daroussin <b...@freebsd.org>
AuthorDate: 2021-11-24 10:55:26 +0000
Commit:     Baptiste Daroussin <b...@freebsd.org>
CommitDate: 2021-11-24 10:55:26 +0000

    kbdmap: use libbsddialog instead of calling dialog(1) via system(3)
---
 usr.sbin/kbdmap/Makefile |   2 +
 usr.sbin/kbdmap/kbdmap.c | 108 +++++++++++++++++++----------------------------
 2 files changed, 46 insertions(+), 64 deletions(-)

diff --git a/usr.sbin/kbdmap/Makefile b/usr.sbin/kbdmap/Makefile
index ec493416a573..22747e70720c 100644
--- a/usr.sbin/kbdmap/Makefile
+++ b/usr.sbin/kbdmap/Makefile
@@ -1,6 +1,8 @@
 # $FreeBSD$
 
 PROG=  kbdmap
+CFLAGS+=       -I${SRCTOP}/contrib/bsddialog/lib
+LIBADD=        bsddialog
 LINKS= ${BINDIR}/kbdmap ${BINDIR}/vidfont
 MLINKS=        kbdmap.1 vidfont.1
 
diff --git a/usr.sbin/kbdmap/kbdmap.c b/usr.sbin/kbdmap/kbdmap.c
index 4f99ba03c2c8..2783d2368e0e 100644
--- a/usr.sbin/kbdmap/kbdmap.c
+++ b/usr.sbin/kbdmap/kbdmap.c
@@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/sysctl.h>
 
 #include <assert.h>
+#include <bsddialog.h>
 #include <ctype.h>
 #include <dirent.h>
 #include <limits.h>
@@ -350,19 +351,23 @@ do_vidfont(struct keymap *km)
 static void
 show_dialog(struct keymap **km_sorted, int num_keymaps)
 {
-       FILE *fp;
-       char *cmd, *dialog;
-       char tmp_name[] = "/tmp/_kbd_lang.XXXX";
-       int fd, i, size;
-
-       fd = mkstemp(tmp_name);
-       if (fd == -1) {
-               fprintf(stderr, "Could not open temporary file \"%s\"\n",
-                   tmp_name);
+       struct bsddialog_conf conf;
+       struct bsddialog_menuitem *listitems;
+       int i, result;
+
+       bsddialog_initconf(&conf);
+       conf.clear = true;
+       if (bsddialog_init() < 0) {
+               fprintf(stderr, "Failed to initialize bsddialog");
+               exit(1);
+       }
+       conf.title = __DECONST(char *, title);
+
+       listitems = calloc(num_keymaps + 1, sizeof(struct bsddialog_menuitem));
+       if (listitems == NULL) {
+               fprintf(stderr, "Failed to allocate memory in show_dialog");
                exit(1);
        }
-       asprintf(&dialog, "/usr/bin/dialog --clear --title \"%s\" "
-                         "--menu \"%s\" 0 0 0", title, menu);
 
        /* start right font, assume that current font is equal
         * to default font in /etc/rc.conf
@@ -374,63 +379,38 @@ show_dialog(struct keymap **km_sorted, int num_keymaps)
        if (font && strcmp(font, font_current))
                vidcontrol(font);
 
-       /* Build up the command */
-       size = 0;
-       for (i=0; i<num_keymaps; i++) {
-               /*
-                * Each 'font' is passed as ' "font" ""', so allow the
-                * extra space
-                */
-               size += strlen(km_sorted[i]->desc) + 6;
-       }
-
-       /* Allow the space for '2> tmpfilename' redirection */
-       size += strlen(tmp_name) + 3;
-
-       cmd = (char *) malloc(strlen(dialog) + size + 1);
-       strcpy(cmd, dialog);
-
+       /* Build up the menu */
        for (i=0; i<num_keymaps; i++) {
-               strcat(cmd, " \"");
-               strcat(cmd, km_sorted[i]->desc);
-               strcat(cmd, "\"");
-               strcat(cmd, " \"\"");
+               listitems[i].prefix = __DECONST(char *, "");
+               listitems[i].depth = 0;
+               listitems[i].bottomdesc = __DECONST(char *, "");
+               listitems[i].on = false;
+               listitems[i].name = km_sorted[i]->desc;
+               listitems[i].desc = __DECONST(char *, "");
        }
-
-       strcat(cmd, " 2>");
-       strcat(cmd, tmp_name);
-
-       /* Show the dialog.. */
-       system(cmd);
-
-       fp = fopen(tmp_name, "r");
-       if (fp) {
-               char choice[64];
-               if (fgets(choice, sizeof(choice), fp) != NULL) {
-                       /* Find key for desc */
-                       for (i=0; i<num_keymaps; i++) {
-                               if (!strcmp(choice, km_sorted[i]->desc)) {
-                                       if (!strcmp(program, "kbdmap"))
-                                               do_kbdcontrol(km_sorted[i]);
-                                       else
-                                               do_vidfont(km_sorted[i]);
-                                       break;
-                               }
+       result = bsddialog_menu(conf, __DECONST(char *, menu), 0, 0, 0,
+           num_keymaps, listitems, NULL);
+       bsddialog_end();
+       switch (result) {
+       case BSDDIALOG_YESOK:
+               for (i = 0; i < num_keymaps; i++) {
+                       if (listitems[i].on) {
+                               printf("ici\n");
+                               if (!strcmp(program, "kdbmap"))
+                                       do_kbdcontrol(km_sorted[i]);
+                               else
+                                       do_vidfont(km_sorted[i]);
+                               break;
                        }
-               } else {
-                       if (font != NULL && strcmp(font, font_current))
-                               /* Cancelled, restore old font */
-                               vidcontrol(font_current);
                }
-               fclose(fp);
-       } else
-               fprintf(stderr, "Failed to open temporary file");
-
-       /* Tidy up */
-       remove(tmp_name);
-       free(cmd);
-       free(dialog);
-       close(fd);
+               break;
+       default:
+               printf("la\n");
+               if (font != NULL && strcmp(font, font_current))
+                       /* Cancelled, restore old font */
+                       vidcontrol(font_current);
+               break;
+       }
 }
 
 /*

Reply via email to