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