Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/menuc Add (optional) support for expanded static men...



details:   https://anonhg.NetBSD.org/src/rev/44cb65e0206c
branches:  trunk
changeset: 837163:44cb65e0206c
user:      martin <martin%NetBSD.org@localhost>
date:      Wed Nov 21 20:04:48 2018 +0000

description:
Add (optional) support for expanded static menu texts - that is: whatever
the application programmer defines as expansion, e.g. to implement
parameter substitution.
While here add rudimentary documentation of the dynamic messages
feature (so at least the parser and the syntax documented here
are in sync).
The man page could use some typesetting help...

diffstat:

 usr.bin/menuc/defs.h       |   3 +-
 usr.bin/menuc/mdb.c        |  38 +++++++++++++++++--
 usr.bin/menuc/mdb.h        |   3 +-
 usr.bin/menuc/menu_sys.def |  46 +++++++++++++++++++++++-
 usr.bin/menuc/menuc.1      |  86 ++++++++++++++++++++++++++++++++++++++++++---
 usr.bin/menuc/parse.y      |  24 +++++++++---
 usr.bin/menuc/scan.l       |   4 +-
 7 files changed, 180 insertions(+), 24 deletions(-)

diffs (truncated from 460 to 300 lines):

diff -r 268e8fd2a2db -r 44cb65e0206c usr.bin/menuc/defs.h
--- a/usr.bin/menuc/defs.h      Wed Nov 21 19:03:18 2018 +0000
+++ b/usr.bin/menuc/defs.h      Wed Nov 21 20:04:48 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: defs.h,v 1.10 2013/10/18 20:19:36 christos Exp $        */
+/*     $NetBSD: defs.h,v 1.11 2018/11/21 20:04:48 martin Exp $  */
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -65,6 +65,7 @@
 
 EXTERN int do_dynamic INIT(0);
 EXTERN int do_msgxlat INIT(0);
+EXTERN int do_expands INIT(0);
 EXTERN int line_no INIT(1);
 EXTERN int had_errors INIT(FALSE);
 EXTERN int max_strlen INIT(1);
diff -r 268e8fd2a2db -r 44cb65e0206c usr.bin/menuc/mdb.c
--- a/usr.bin/menuc/mdb.c       Wed Nov 21 19:03:18 2018 +0000
+++ b/usr.bin/menuc/mdb.c       Wed Nov 21 20:04:48 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mdb.c,v 1.46 2012/03/06 16:55:18 mbalmer Exp $ */
+/*     $NetBSD: mdb.c,v 1.47 2018/11/21 20:04:48 martin Exp $  */
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -41,7 +41,7 @@
 #include <sys/cdefs.h>
 
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: mdb.c,v 1.46 2012/03/06 16:55:18 mbalmer Exp $");
+__RCSID("$NetBSD: mdb.c,v 1.47 2018/11/21 20:04:48 martin Exp $");
 #endif
 
 
@@ -163,6 +163,8 @@
                (void)fprintf(out_file, "#define MSG_XLAT(x) (x)\n");
        if (do_dynamic)
                (void)fprintf(out_file, "#define DYNAMIC_MENUS\n");
+       if (do_expands)
+               (void)fprintf(out_file, "#define MENU_EXPANDS\n");
        if (do_dynamic || do_msgxlat)
                (void)fprintf(out_file, "\n");
 
@@ -171,6 +173,9 @@
                "typedef struct menu_ent menu_ent;\n"   
                "struct menu_ent {\n"
                "       const char      *opt_name;\n"
+               "#ifdef MENU_EXPANDS\n"
+               "       const char      *opt_exp_name;\n"
+               "#endif\n"
                "       int             opt_menu;\n"
                "       int             opt_flags;\n"
                "       int             (*opt_action)(menudesc *, void *);\n"
@@ -192,7 +197,12 @@
                "       WINDOW          *mw;\n"
                "       WINDOW          *sv_mw;\n"
                "       const char      *helpstr;\n"
-               "       const char      *exitstr;\n"
+               "       const char      *exitstr;\n");
+       if (do_expands)
+               (void)fprintf(out_file,
+                       "       void            (*expand_act)(menudesc *, "
+                           "void *);\n");
+       (void)fprintf(out_file,
                "       void            (*post_act)(menudesc *, void *);\n"
                "       void            (*exit_act)(menudesc *, void *);\n"
                "       void            (*draw_line)(menudesc *, int, void *);\n"
@@ -263,6 +273,14 @@
 
        /* func defs */
        for (i = 0; i < menu_no; i++) {
+               if (do_expands && strlen(menus[i]->info->expact.code)) {
+                       (void)fprintf(out_file, "/*ARGSUSED*/\n"
+                           "static void menu_%d_expact(menudesc *menu, void *arg)\n{\n", i);
+                       if (menus[i]->info->expact.endwin)
+                               (void)fprintf(out_file, "\tendwin();\n");
+                       (void)fprintf(out_file, "\t%s\n}\n\n",
+                           menus[i]->info->expact.code);
+               }
                if (strlen(menus[i]->info->postact.code)) {
                        (void)fprintf(out_file, "/*ARGSUSED*/\n"
                            "static void menu_%d_postact(menudesc *menu, void *arg)\n{\n", i);
@@ -307,7 +325,8 @@
                for (j = 0, toptn = menus[i]->info->optns; toptn;
                    toptn = toptn->next, j++) {
                        name_is_code += toptn->name_is_code;
-                       (void)fprintf(out_file, "\t{%s,%d,%d,",
+                       (void)fprintf(out_file, "\t{ .opt_name = %s, "
+                               ".opt_menu=%d, .opt_flags=%d, .opt_action=",
                                toptn->name_is_code ? "0" : toptn->name,
                                toptn->menu,
                                (toptn->issub ? OPT_SUB : 0)
@@ -380,6 +399,12 @@
                        (void)fprintf(out_file, "%s", menus[i]->info->exitstr);
                else
                        (void)fprintf(out_file, "\"Exit\"");
+               if (do_expands) {
+                       if (strlen(menus[i]->info->expact.code))
+                               (void)fprintf(out_file, ",menu_%d_expact", i);
+                       else
+                               (void)fprintf(out_file, ",NULL");
+               }
                if (strlen(menus[i]->info->postact.code))
                        (void)fprintf(out_file, ",menu_%d_postact", i);
                else
@@ -397,7 +422,10 @@
 
        }
        (void)fprintf(out_file, "{NULL, 0, 0, 0, 0, 0, 0, 0, 0, "
-               "NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}};\n\n");
+               "NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL");
+       if (do_expands)
+               (void)fprintf(out_file, ", NULL");
+       (void)fprintf(out_file, "}};\n\n");
 
        /* __menu_initerror: initscr failed. */
        (void)fprintf(out_file, 
diff -r 268e8fd2a2db -r 44cb65e0206c usr.bin/menuc/mdb.h
--- a/usr.bin/menuc/mdb.h       Wed Nov 21 19:03:18 2018 +0000
+++ b/usr.bin/menuc/mdb.h       Wed Nov 21 20:04:48 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mdb.h,v 1.9 2012/03/06 16:55:18 mbalmer Exp $  */
+/*     $NetBSD: mdb.h,v 1.10 2018/11/21 20:04:48 martin Exp $  */
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -81,6 +81,7 @@
        int numopt;
        int name_is_code;
        optn_info *optns;
+       action expact;
        action postact;
        action exitact;
 };
diff -r 268e8fd2a2db -r 44cb65e0206c usr.bin/menuc/menu_sys.def
--- a/usr.bin/menuc/menu_sys.def        Wed Nov 21 19:03:18 2018 +0000
+++ b/usr.bin/menuc/menu_sys.def        Wed Nov 21 20:04:48 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: menu_sys.def,v 1.59 2012/03/06 16:55:18 mbalmer Exp $  */
+/*     $NetBSD: menu_sys.def,v 1.60 2018/11/21 20:04:48 martin Exp $   */
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -213,7 +213,13 @@
        if (w == 0) {
                int l;
                for (i = 0; i < m->numopts; i++) {
+#ifdef MENU_EXPANDS
+                       tp = m->opts[i].opt_exp_name ?
+                           m->opts[i].opt_exp_name :
+                           MSG_XLAT(m->opts[i].opt_name);
+#else
                        tp = MSG_XLAT(m->opts[i].opt_name);
+#endif
                        if (tp == NULL)
                                continue;
                        l = strlen(tp);
@@ -359,7 +365,14 @@
        for (opt = m->topline; opt < m->numopts; opt++) {
                if (cury >= maxy)
                        break;
-               draw_menu_line(m, opt, cury++, arg, m->opts[opt].opt_name);
+               draw_menu_line(m, opt, cury++, arg,
+#ifdef MENU_EXPANDS
+                   m->opts[opt].opt_exp_name ?
+                       m->opts[opt].opt_exp_name : m->opts[opt].opt_name
+#else
+                   m->opts[opt].opt_name
+#endif
+                   );
        }
 
        /* Add the exit option. */
@@ -482,6 +495,21 @@
        wclear(stdscr);
 }
 
+#ifdef MENU_EXPANDS
+static void
+free_exp_menu_items(menudesc *m)
+{
+       int i;
+
+       for (i = 0; i < m->numopts; i++) {
+               if (m->opts[i].opt_exp_name != NULL) {
+                       free(__UNCONST(m->opts[i].opt_exp_name));
+                       m->opts[i].opt_exp_name = NULL;
+               }
+       }
+}
+#endif
+
 static void
 process_req(menudesc *m, void *arg, int req)
 {
@@ -667,6 +695,12 @@
                if (isendwin())
                        /* I'm not sure this is needed with netbsd's curses */
                        doupdate();
+#ifdef MENU_EXPANDS
+               /* Expand the menu items, if needed */
+               if (m->expand_act && m->mw == NULL)
+                       (*m->expand_act)(m, arg);
+#endif
+
                /* Process the display action */
                if (m->post_act)
                        (*m->post_act)(m, arg);
@@ -726,6 +760,11 @@
                wnoutrefresh(m->mw);
        }
 
+#ifdef MENU_EXPANDS
+       /* Free expanded menu items, if any */
+       free_exp_menu_items(m);
+#endif
+
        /* Process the exit action */
        if (m->exit_act)
                (*m->exit_act)(m, arg);
@@ -801,6 +840,9 @@
        m->h = h;
        m->w = w;
        m->mopt = mopt | MC_VALID;
+#ifdef MENU_EXPANDS
+       m->expand_act = NULL;
+#endif
        m->post_act = post_act;
        m->draw_line = draw_line;
        m->exit_act = exit_act;
diff -r 268e8fd2a2db -r 44cb65e0206c usr.bin/menuc/menuc.1
--- a/usr.bin/menuc/menuc.1     Wed Nov 21 19:03:18 2018 +0000
+++ b/usr.bin/menuc/menuc.1     Wed Nov 21 20:04:48 2018 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: menuc.1,v 1.31 2017/07/03 21:34:20 wiz Exp $
+.\"    $NetBSD: menuc.1,v 1.32 2018/11/21 20:04:48 martin Exp $
 .\"
 .\" Copyright 1997 Piermont Information Systems Inc.
 .\" All rights reserved.
@@ -29,7 +29,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 .\" THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd August 2, 2004
+.Dd November 20, 2018
 .Dt MENUC 1
 .Os
 .Sh NAME
@@ -130,12 +130,13 @@
 is as described above.
 .Pp
 There are four kinds of menu definition elements.
-The first one just declares whether the programmer wants dynamic menus
-available.
-The default is static menus only.
-The static menus are the ones defined by the menu definitions and do not
+The first one just declares whether the programmer wants dynamic menus,
+dynamic messages and argument expansion in menus available.
+All these option default to off (or static only).
+.Pp
+Static menus are the ones defined by the menu definitions and do not
 change at run time.
-The dynamic menus provide the programmer with a method to create and
+Dynamic menus provide the programmer with a method to create and
 modify menus during the running of the program.
 To include dynamic menus, one needs only add the declaration:
 .Dl allow dynamic menus ;
@@ -143,6 +144,18 @@
 This declaration may appear anywhere in the
 .Ar file ,
 but usually appears before any menus are defined.
+See below for a detailed explanation of dynamic menus.
+.Pp
+To allow dynamic messages, one needs to add the declaration:
+.Dl allow dynamic messages ;
+This enables internationalization by loading message files at
+run time.
+.Pp
+To allow argument expansion on static menu strings, one needs to add
+the declaration:
+.Dl allow expand ;
+This enables the expand action.
+See below for a detailed explanation.
 .Pp
 The next element is a code block to execute if the curses
 screen can not be successfully initialized.
@@ -249,6 +262,7 @@
 The format and order for a menu definition is:
 .Bd -ragged -offset indent
 menu <name> <options> ;
+  <expand action>
   <display action>
   <menu items>
   <exit action>
@@ -272,6 +286,11 @@
 declaration.
 These override the options from the most recent default declaration.
 .Pp
+The expand action is optional and only available if the global option



Home | Main Index | Thread Index | Old Index