1.\" $NetBSD: menuc.1,v 1.30 2012/03/06 16:55:18 mbalmer Exp $ 2.\" 3.\" Copyright 1997 Piermont Information Systems Inc. 4.\" All rights reserved. 5.\" 6.\" Written by Philip A. Nelson for Piermont Information Systems Inc. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. The name of Piermont Information Systems Inc. may not be used to endorse 17.\" or promote products derived from this software without specific prior 18.\" written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS'' 21.\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE 24.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30.\" THE POSSIBILITY OF SUCH DAMAGE. 31.\" 32.Dd August 2, 2004 33.Dt MENUC 1 34.Os 35.Sh NAME 36.Nm menuc 37.Nd menu compiler 38.Sh SYNOPSIS 39.Nm 40.Op Fl o Ar name 41.Ar file 42.Sh DESCRIPTION 43This implements a curses based menu system. 44A source file that describes menus, their options, and how to process 45the options is given to 46.Nm 47and produces both a .c and a .h file that implement the menu system. 48The standard base name of the files is 49.Pa menu_defs . 50The 51.Fl o Ar name 52can be used to specify a different base name. 53.Sh ENVIRONMENT 54.Bl -tag -width MENUDEF 55.It Ev MENUDEF 56Can be set to point to a different set of definition files for 57.Nm . 58The current location defaults to 59.Pa /usr/share/misc . 60.El 61.Sh MENU DESCRIPTIONS 62The input 63.Ar file 64defines static menus and options for processing those menus. 65It also contains comments, initial C code that is required to provide 66for definitions and other code necessary for the menu system, and an 67option declaration if dynamic menus are requested. 68.Pp 69Comments may appear anywhere in the input 70.Ar file 71and are like a space in the input. 72They are like C comments starting with 73.Em /* 74and ending with 75.Em */ . 76They are unlike C comments in that they may be nested. 77A comment does not end until a matching end comment is found. 78.Pp 79In many places, C code is included in the definition 80.Ar file . 81All C code is passed verbatim to the C output file. 82.Nm 83comments do not start in C code and comments in the C code are 84passed verbatim to the output. 85The C comments are not recognized by 86.Nm . 87In all cases, C code starts with a left brace 88.Pq Em \&{ 89and ends with the matching right brace 90.Pq Em \&} . 91It is important to recognize that in code segments, any brace 92will be counted, even if it is in a C comment inside the code. 93.Pp 94The 95.Ar file 96contains an initial (and optional) code block followed by any 97number of menu definition elements in any order. 98The initial code block usually contains includes of header files used by 99code in the menu code blocks later in the 100.Ar file . 101If 102.Dv USER_MENU_INIT 103is #defined, then it will be evaluated before the 104rest of the menu is initialised, if it evaluates to a non-zero value 105then the initialisation will fail. 106The file is free format, so the actual formatting of the input 107.Ar file 108is to the taste of the programmer. 109.Pp 110All other C code that will appear in an 111.Em action . 112This will be specified as 113.Aq Em action 114in later text. 115Such an action will appear as: 116.Dl action \*[Lt]opt_endwin\*[Gt] \*[Lt]code\*[Gt] 117in the 118.Ar file . 119The 120.Aq Em opt_endwin , 121if present is: 122.Dl ( endwin ) 123and specifies that the curses 124.Fn endwin 125function should be called before executing the code and 126then reinstating the current curses window after the 127code has been run. 128The 129.Aq Em code 130is as described above. 131.Pp 132There are four kinds of menu definition elements. 133The first one just declares whether the programmer wants dynamic menus 134available. 135The default is static menus only. 136The static menus are the ones defined by the menu definitions and do not 137change at run time. 138The dynamic menus provide the programmer with a method to create and 139modify menus during the running of the program. 140To include dynamic menus, one needs only add the declaration: 141.Dl allow dynamic menus ; 142The semicolon is required to terminate this declaration. 143This declaration may appear anywhere in the 144.Ar file , 145but usually appears before any menus are defined. 146.Pp 147The next element is a code block to execute if the curses 148screen can not be successfully initialized. 149The declaration 150.Dl error code ; 151tells the menu system to execute the associated code block 152if the initialization fails. 153If no code is provided, a default code block is used that prints 154.Dl Could not initialize curses. 155and exits. 156This element may appear anywhere in the 157.Ar file 158but usually appears before any menus are defined. 159.Pp 160The next element defines default options for menus. 161Each menu is built from a list of options. 162These options include the location of the upper left corner of the menu, 163whether there is a "box" drawn around the menu, whether the menu is 164scrollable, the menu's title, whether shortcut letters are 165allowed, whether a standard exit option should be included 166in the menu and text associated with the standard exit option. 167The general format is: 168.Dl default \*[Lt]comma separated option list\*[Gt] ; 169.Pp 170The supported options are: 171.Bl -tag -width ".Ic exitstring Va text" 172.It Ic x = Va startx 173The column number of the upper left corner of the menu window. 174If 175.Va startx 176is -1 the menu will be centered horizontally. 177.It Ic y = Va starty 178The row number of the upper left corner of the menu window. 179If 180.Va starty 181is negative then the menu will be placed below any message text, but 182in at least row 183.Va -starty . 184.It Ic h = Va height 185Specifies the number of menu entries to be displayed. 186If zero, the height will be based on the number of entries. 187.It Ic h = Va width 188Specifies the width of the menu window. 189If zero, the width will be that of the longest menu text line. 190.It Ic title Va text 191The specified 192.Va text 193will be displayed at the top of the menu window (inside any box). 194.It Ic box 195If specified, draw a box around the menu. 196.It Ic clear 197If specified, clear the window before performing the 198.Va action . 199.It Ic exit 200If specified add an addition option to exit the menu. 201.It Ic exitstring Va text 202The menu label for the 203.Va exit 204option. 205If not specified defaults to "exit". 206.It Ic default exit 207If specified, place the cursor on the 208.Va exit 209line of the menu, instead of the top line. 210.It Ic shortcut 211If specified, add alphabetic tags to each menu line. 212.It Ic scrollable 213If specified, and the menu has more lines than will fit in its window, then 214only part of the menu will be displayed and the 215.Sq \*[Lt] 216and 217.Sq \*[Gt] 218keys will scroll the displayed menu lines. 219.It Ic always scroll 220If specified, allow for the scroll message line even if the menu doesn't 221appear to have too many lines. 222Useful for dynamic menus, when the number of entries isn't known when the 223menu window is created.. 224.It Ic sub menu 225If specified, the screen contents that the menu window overwrites are saved 226and restored when the menu exits. 227.El 228The 229.Ic box , clear , exit , default exit , shortcut , scrollable , always scroll , 230and 231.Ic sub menu 232options can be preceded by 233.Ic no 234in order to negate a default. 235.Pp 236The 237.Va text 238arguments can be either a quoted text string or a name #defined to something 239suitable for initialising a const char * field. 240.Pp 241The default declaration may appear multiple times. 242Each time, it sets the default values for menu definitions that follow 243in the 244.Ar file . 245In each menu definition, any or all of these default definitions 246may be overridden for that menu. 247.Pp 248The final element is the actual static menu definitions. 249The format and order for a menu definition is: 250.Bd -ragged -offset indent 251menu \*[Lt]name\*[Gt] \*[Lt]options\*[Gt] ; 252 \*[Lt]display action\*[Gt] 253 \*[Lt]menu items\*[Gt] 254 \*[Lt]exit action\*[Gt] 255 \*[Lt]help text\*[Gt] 256.Ed 257.Pp 258Names are unquoted strings of alpha-numeric and underscore 259characters. 260They must start with an alpha character. 261In C source, a menu named 262.Dq foo 263is appears as 264.Dq MENU_foo . 265(Capitalization is important.) 266This is important, because the menu is displayed and processed by 267calling the function 268.Dl process_menu (MENU_foo, arg) ; 269.Pp 270The options are a comma separated list of options as in the 271.Dq default 272declaration. 273These override the options from the most recent default declaration. 274.Pp 275The display action is optional and provides C code to 276execute at each and every time the menu is displayed for processing. 277If it is included, the format is: 278.Dl display \*[Lt]action\*[Gt] ; 279.Pp 280The bulk of the menu definition is the specification 281of the menu items. 282The general format of a menu item is: 283.Dl option \*[Lt]string\*[Gt], \*[Lt]element_list\*[Gt] ; 284The 285.Aq Em string 286is the text displayed for the menu item, this must be a quoted string 287or a name #defined to something that will initialise a const char * field. 288There may be an arbitrary number of these items. 289(If there are shortcuts in the menu, a practical limit 290of 51 should be recognized. 291It produces shortcuts a to w, y, z, and A to Z. 292x is the shortcut for the exit item.) 293.Pp 294The 295.Aq Em element_list 296is a comma separated list of what to do when the item is selected. 297They may appear in any order. 298.Pp 299The first element processed when a menu item 300is selected is the associated action. 301The next element to be processed is the sub or next menu option. 302They are declared as: 303.Dl next menu \*[Lt]name\*[Gt] 304.Dl sub menu \*[Lt]name\*[Gt] 305The difference between these two is that a sub 306menu will return to the current menu when exited. 307The next menu will just replace the current 308menu and when exited, will return to where the 309current menu would have gone. 310Only one of menu element may be used for each menu item. 311Finally, after processing both the action and a sub menu, 312the current menu will be exited if the element 313.Dl exit 314is specified. 315.Em Note : 316If 317.Em exit 318is specified, next menu will not work because 319the menu system will exit the 320.Em current 321menu, even if current has been set by 322.Em next menu . 323.Pp 324After all menu items, the final two menu definition 325elements may appear. 326The exit action is optional and provides C code to 327execute in the process of exiting a menu. 328If it is included, the format is: 329.Dl exit \*[Lt]action\*[Gt] ; 330.Pp 331The final part of the menu definition is the optional 332help string. 333The format is: 334.Dl help \*[Lt]text\*[Gt] ; 335This text is displayed in a full page 336help window if the question mark is typed. 337The actual help text starts with a left brace 338.Pq Em \&{ 339and ends with the matching right brace 340.Pq Em \&} . 341The braces are not included in the 342help string, but all other characters between 343them are included. 344Newlines in the code translate to newlines in the help text. 345Alternatively, the name of a const char * variable may be given. 346.Sh DYNAMIC MENUS 347If requested, 348.Nm 349supports dynamic menus by allowing the user to create new 350menus. 351The related definitions for using dynamic menus are: 352.Bd -literal 353struct menudesc; 354 355typedef 356struct menu_ent { 357 const char *opt_name; 358 int opt_menu; 359 int opt_flags; 360 int (*opt_action)(struct menudesc *, void *); 361} menu_ent ; 362 363/* For opt_menu */ 364#define OPT_NOMENU -1 365 366/* For opt_flags */ 367#define OPT_SUB 1 368#define OPT_ENDWIN 2 369#define OPT_EXIT 4 370 371typedef 372struct menudesc { 373 const char *title; 374 int y, x; 375 int h, w; 376 int mopt; 377 int numopts; 378 int cursel; 379 int topline; 380 menu_ent *opts; 381 WINDOW *mw; 382 WINDOW *sv_mw; 383 const char *helpstr; 384 const char *exitstr; 385 void (*post_act)(struct menudesc *, void *); 386 void (*exit_act)(struct menudesc *, void *); 387 void (*draw_line)(struct menudesc *, int, void *); 388} menudesc ; 389 390/* defines for mopt field. */ 391#define MC_NOEXITOPT 1 392#define MC_NOBOX 2 393#define MC_SCROLL 4 394#define MC_NOSHORTCUT 8 395#define MC_NOCLEAR 16 396#define MC_DFLTEXIT 32 397#define MC_ALWAYS_SCROLL 64 398#define MC_SUBMENU 128 399 400int new_menu(const char *title, menu_ent *opts, int numopts, 401 int x, int y, int h, int w, int mopt, 402 void (*post_act)(struct menudesc *, void *), 403 void (*draw_line)(struct menudesc *, int, void *), 404 void (*exit_act)(struct menudesc *, void *), 405 const char *help, const char *exitstr); 406 407void free_menu (int menu_no); 408.Ed 409.Pp 410The 411.Ar title 412is the title displayed at the top of the menu. 413The 414.Ar opts 415is an array of menu entry definitions that has 416.Ar numopts 417elements. 418The programmer must build this array and 419fill in all of the fields before processing calling 420.Fn process_menu 421for the new menu. 422The fields of the 423.Ar opts 424may change at any time. 425For example, 426.Em opt_name 427may change as a result of selecting that option. 428When the menu is redisplayed, the new text is printed. 429Arguments, 430.Ar x , y , h , 431and 432.Ar w 433are the same as the options in the menu description. 434.Ar mopt 435is the boolean options. 436Note, box, clear, exit and shortcuts are enabled by default. 437You need to add option flags to turn them off or turn on scrollable menus. 438The options 439.Ar post_act , 440and 441.Ar exit_act 442are function pointers to the display action and the exit action. 443If they are 444.Dv NULL , 445no call will be made. 446.Ar draw_line 447will be called to display the menu line if the corresponding opt_name 448field is 449.Dv NULL . 450.Ar help 451is the text to display in a help screen. 452And finally, 453.Ar exitstr 454is the text for the 'exit' line of the menu. 455If 456.Dv NULL , 457"Exit" is used. 458A 459.Dv NULL 460help pointer will disable the help feature for the menu. 461.Sh FILES 462.Bl -item 463.It 464.Pa /usr/share/misc/menu_sys.def 465.El 466.Sh EXAMPLES 467The following is a simple menu definition file. 468It is complete in that the output of 469.Nm 470may be compiled into a complete program. 471For example, if the following was in a file called 472.Pa example.mc , 473an executable program could be produced by the following commands. 474.Bd -literal -offset indent 475menuc -o example example.mc 476cc -o example example.c -lcurses 477.Ed 478A much more complete example is available with the source 479distribution in a subdirectory called 480.Em testm . 481.Bd -literal 482/* This is an example menu definition file for menuc. */ 483 484{ 485#include \*[Lt]stdio.h\*[Gt] 486#include \*[Lt]unistd.h\*[Gt] 487 488/* Main program! This is often in a different file. */ 489int 490main() 491 { 492 process_menu (MENU_main, NULL); 493 endwin(); 494 return 0; 495 } 496 497/* Example initialize function! */ 498void 499init_main() 500 { 501 } 502} 503 504default x=20, y=10, box, scrollable, exit; 505 506error action { 507 fprintf (stderr, "Example Menu: Could not initialize curses."); 508 exit(1); 509}; 510 511menu main, title "Main Menu", no exit, no shortcut; 512 display action { init_main(); }; 513 option "Option 1", 514 action (endwin) { 515 printf ("That was option 1!"); 516 sleep(3); 517 }; 518 option "Sub Menu", sub menu othermenu; 519 option "Next Menu", next menu othermenu; 520 option "Quit", exit; 521 help { 522This is a simple help screen for an example menu definition file. 523}; 524 525menu othermenu, title "Sub/Next Menu", x=5, y=5, no box; 526 option "Do Nothing!", action { }; 527.Ed 528.Sh SEE ALSO 529.Xr msgc 1 530.Sh AUTHORS 531.An Philip A. Nelson 532for Piermont Information Systems Inc. 533Initial ideas for this were developed and implemented in Pascal at the 534Leiden University, Netherlands, in the summer of 1980. 535.Sh BUGS 536Both 537.Nm 538and 539.Nm msgc 540are probably only used by 541.Nm sysinst . 542The features of both have been tailored for 543.Nm sysinst , 544and further changes are likely to occur. 545