1 .\" $NetBSD: menuc.1,v 1.28 2009/03/10 20:28:51 joerg Exp $
3 .\" Copyright 1997 Piermont Information Systems Inc.
4 .\" All rights reserved.
6 .\" Written by Philip A. Nelson for Piermont Information Systems Inc.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
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.
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.
47 This implements a curses based menu system.
48 A source file that describes menus, their options, and how to process
49 the options is given to
51 and produces both a .c and a .h file that implement the menu system.
52 The standard base name of the files is
56 can be used to specify a different base name.
58 .Bl -tag -width MENUDEF
60 Can be set to point to a different set of definition files for
62 The current location defaults to
68 defines static menus and options for processing those menus.
69 It also contains comments, initial C code that is required to provide
70 for definitions and other code necessary for the menu system, and an
71 option declaration if dynamic menus are requested.
73 Comments may appear anywhere in the input
75 and are like a space in the input.
76 They are like C comments starting with
80 They are unlike C comments in that they may be nested.
81 A comment does not end until a matching end comment is found.
83 In many places, C code is included in the definition
85 All C code is passed verbatim to the C output file.
87 comments do not start in C code and comments in the C code are
88 passed verbatim to the output.
89 The C comments are not recognized by
91 In all cases, C code starts with a left brace
93 and ends with the matching right brace
95 It is important to recognize that in code segments, any brace
96 will be counted, even if it is in a C comment inside the code.
100 contains an initial (and optional) code block followed by any
101 number of menu definition elements in any order.
102 The initial code block usually contains includes of header files used by
103 code in the menu code blocks later in the
107 is #defined, then it will be evaluated before the
108 rest of the menu is initialised, if it evaluates to a non-zero value
109 then the initialisation will fail.
110 The file is free format, so the actual formatting of the input
112 is to the taste of the programmer.
114 All other C code that will appear in an
116 This will be specified as
119 Such an action will appear as:
120 .Dl action \*[Lt]opt_endwin\*[Gt] \*[Lt]code\*[Gt]
127 and specifies that the curses
129 function should be called before executing the code and
130 then reinstating the current curses window after the
134 is as described above.
136 There are four kinds of menu definition elements.
137 The first one just declares whether the programmer wants dynamic menus
139 The default is static menus only.
140 The static menus are the ones defined by the menu definitions and do not
142 The dynamic menus provide the programmer with a method to create and
143 modify menus during the running of the program.
144 To include dynamic menus, one needs only add the declaration:
145 .Dl allow dynamic menus ;
146 The semicolon is required to terminate this declaration.
147 This declaration may appear anywhere in the
149 but usually appears before any menus are defined.
151 The next element is a code block to execute if the curses
152 screen can not be successfully initialized.
155 tells the menu system to execute the associated code block
156 if the initialization fails.
157 If no code is provided, a default code block is used that prints
158 .Dl Could not initialize curses.
160 This element may appear anywhere in the
162 but usually appears before any menus are defined.
164 The next element defines default options for menus.
165 Each menu is built from a list of options.
166 These options include the location of the upper left corner of the menu,
167 whether there is a "box" drawn around the menu, whether the menu is
168 scrollable, the menu's title, whether shortcut letters are
169 allowed, whether a standard exit option should be included
170 in the menu and text associated with the standard exit option.
171 The general format is:
172 .Dl default \*[Lt]comma separated option list\*[Gt] ;
174 The supported options are:
175 .Bl -tag -width ".Ic exitstring Va text"
177 The column number of the upper left corner of the menu window.
180 is -1 the menu will be centered horizontally.
182 The row number of the upper left corner of the menu window.
185 is negative then the menu will be placed below any message text, but
189 Specifies the number of menu entries to be displayed.
190 If zero, the height will be based on the number of entries.
192 Specifies the width of the menu window.
193 If zero, the width will be that of the longest menu text line.
197 will be displayed at the top of the menu window (inside any box).
199 If specified, draw a box around the menu.
201 If specified, clear the window before performing the
204 If specified add an addition option to exit the menu.
205 .It Ic exitstring Va text
206 The menu label for the
209 If not specified defaults to "exit".
211 If specified, place the cursor on the
213 line of the menu, instead of the top line.
215 If specified, add alphabetic tags to each menu line.
217 If specified, and the menu has more lines than will fit in its window, then
218 only part of the menu will be displayed and the
222 keys will scroll the displayed menu lines.
224 If specified, allow for the scroll message line even if the menu doesn't
225 appear to have too many lines.
226 Useful for dynamic menus, when the number of entries isn't known when the
227 menu window is created..
229 If specified, the screen contents that the menu window overwrites are saved
230 and restored when the menu exits.
233 .Ic box , clear , exit , default exit , shortcut , scrollable , always scroll ,
236 options can be preceded by
238 in order to negate a default.
242 arguments can be either a quoted text string or a name #defined to something
243 suitable for initialising a const char * field.
245 The default declaration may appear multiple times.
246 Each time, it sets the default values for menu definitions that follow
249 In each menu definition, any or all of these default definitions
250 may be overridden for that menu.
252 The final element is the actual static menu definitions.
253 The format and order for a menu definition is:
254 .Bd -ragged -offset indent
255 menu \*[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]
262 Names are unquoted strings of alpha-numeric and underscore
264 They must start with an alpha character.
265 In C source, a menu named
269 (Capitalization is important.)
270 This is important, because the menu is displayed and processed by
272 .Dl process_menu (MENU_foo, arg) ;
274 The options are a comma separated list of options as in the
277 These override the options from the most recent default declaration.
279 The display action is optional and provides C code to
280 execute at each and every time the menu is displayed for processing.
281 If it is included, the format is:
282 .Dl display \*[Lt]action\*[Gt] ;
284 The bulk of the menu definition is the specification
286 The general format of a menu item is:
287 .Dl option \*[Lt]string\*[Gt], \*[Lt]element_list\*[Gt] ;
290 is the text displayed for the menu item, this must be a quoted string
291 or a name #defined to something that will initialise a const char * field.
292 There may be an arbitrary number of these items.
293 (If there are shortcuts in the menu, a practical limit
294 of 51 should be recognized.
295 It produces shortcuts a to w, y, z, and A to Z.
296 x is the shortcut for the exit item.)
300 is a comma separated list of what to do when the item is selected.
301 They may appear in any order.
303 The first element processed when a menu item
304 is selected is the associated action.
305 The next element to be processed is the sub or next menu option.
306 They are declared as:
307 .Dl next menu \*[Lt]name\*[Gt]
308 .Dl sub menu \*[Lt]name\*[Gt]
309 The difference between these two is that a sub
310 menu will return to the current menu when exited.
311 The next menu will just replace the current
312 menu and when exited, will return to where the
313 current menu would have gone.
314 Only one of menu element may be used for each menu item.
315 Finally, after processing both the action and a sub menu,
316 the current menu will be exited if the element
322 is specified, next menu will not work because
323 the menu system will exit the
325 menu, even if current has been set by
328 After all menu items, the final two menu definition
330 The exit action is optional and provides C code to
331 execute in the process of exiting a menu.
332 If it is included, the format is:
333 .Dl exit \*[Lt]action\*[Gt] ;
335 The final part of the menu definition is the optional
338 .Dl help \*[Lt]text\*[Gt] ;
339 This text is displayed in a full page
340 help window if the question mark is typed.
341 The actual help text starts with a left brace
343 and ends with the matching right brace
345 The braces are not included in the
346 help string, but all other characters between
348 Newlines in the code translate to newlines in the help text.
349 Alternatively, the name of a const char * variable may be given.
353 supports dynamic menus by allowing the user to create new
355 The related definitions for using dynamic menus are:
361 const char *opt_name;
364 int (*opt_action)(struct menudesc *, void *);
368 #define OPT_NOMENU -1
389 void (*post_act)(struct menudesc *, void *);
390 void (*exit_act)(struct menudesc *, void *);
391 void (*draw_line)(struct menudesc *, int, void *);
394 /* defines for mopt field. */
395 #define MC_NOEXITOPT 1
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
404 int 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);
411 void free_menu (int menu_no);
416 is the title displayed at the top of the menu.
419 is an array of menu entry definitions that has
422 The programmer must build this array and
423 fill in all of the fields before processing calling
428 may change at any time.
431 may change as a result of selecting that option.
432 When the menu is redisplayed, the new text is printed.
437 are the same as the options in the menu description.
439 is the boolean options.
440 Note, box, clear, exit and shortcuts are enabled by default.
441 You need to add option flags to turn them off or turn on scrollable menus.
446 are function pointers to the display action and the exit action.
449 no call will be made.
451 will be called to display the menu line if the corresponding opt_name
455 is the text to display in a help screen.
458 is the text for the 'exit' line of the menu.
464 help pointer will disable the help feature for the menu.
468 .Pa /usr/share/misc/menu_sys.def
471 The following is a simple menu definition file.
472 It is complete in that the output of
474 may be compiled into a complete program.
475 For example, if the following was in a file called
477 an executable program could be produced by the following commands.
478 .Bd -literal -offset indent
479 menuc -o example example.mc
480 cc -o example example.c -lcurses
482 A much more complete example is available with the source
483 distribution in a subdirectory called
486 /* This is an example menu definition file for menuc. */
489 #include \*[Lt]stdio.h\*[Gt]
490 #include \*[Lt]unistd.h\*[Gt]
492 /* Main program! This is often in a different file. */
496 process_menu (MENU_main, NULL);
501 /* Example initialize function! */
508 default x=20, y=10, box, scrollable, exit;
511 fprintf (stderr, "Example Menu: Could not initialize curses.");
515 menu main, title "Main Menu", no exit, no shortcut;
516 display action { init_main(); };
519 printf ("That was option 1!");
522 option "Sub Menu", sub menu othermenu;
523 option "Next Menu", next menu othermenu;
526 This is a simple help screen for an example menu definition file.
529 menu othermenu, title "Sub/Next Menu", x=5, y=5, no box;
530 option "Do Nothing!", action { };
536 for Piermont Information Systems Inc.
537 Initial ideas for this were developed and implemented in Pascal at the
538 Leiden University, Netherlands, in the summer of 1980.
544 are probably only used by
546 The features of both have been tailored for
548 and further changes are likely to occur.