C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
menu.c
Go to the documentation of this file.
1/** @file menu.c
2 @brief Command line start-up for C-Menu components
3 @author Bill Waller
4 Copyright (c) 2025
5 MIT License
6 billxwaller@gmail.com
7 @date 2026-02-09
8 */
9
10#include <common.h>
11#include <string.h>
12
13/** @brief This function is responsible for cleaning up the terminal state and
14 exiting the program.
15 @note This function is typically registered to be called when the program
16 exits, ensuring that the terminal is properly restored to its original state,
17 even if the program encounters an error or is terminated unexpectedly. The
18 end_pgm function performs the following actions:
19 1. It calls win_del() to delete any windows that may have been created
20 during the program's execution.
21 2. It calls destroy_curses() to clean up the ncurses library and restore the
22 terminal to its normal state.
23 3. It calls restore_shell_tioctl() to restore the terminal's input/output
24 settings to their original state.
25 4. Finally, it calls exit(EXIT_FAILURE) to terminate the program with a
26 failure status, indicating that the program has encountered an error or is
27 exiting due to an unexpected condition. By defining this function and
28 registering it to be called on program exit, you can help ensure that the
29 terminal is properly cleaned up and restored, preventing any issues with the
30 terminal state after the program has exited. */
31static void end_pgm(void) {
32 curs_set(1);
37 exit(EXIT_SUCCESS);
38}
39
40int mview(Init *);
41
42int main(int argc, char **argv) {
43 int rc;
44 rc = atexit(end_pgm);
45 if (rc != 0) {
46 fprintf(stderr, "\nCannot set exit function\n");
47 exit(EXIT_FAILURE);
48 }
49 char pgm_name[MAXLEN];
51 Init *init = new_init(argc, argv);
52 SIO *sio = init->sio;
53 mapp_initialization(init, argc, argv);
58
59 base_name(pgm_name, argv[0]);
60
61 if (!strcmp(pgm_name, "menu")) {
62 new_menu(init, init->argc, init->argv, LINES / 14, COLS / 14);
63 menu = init->menu;
64 menu_engine(init);
65 } else if (!strcmp(pgm_name, "form")) {
66 init_form(init, init->argc, init->argv, LINES / 14, COLS / 14);
67 } else if (!strcmp(pgm_name, "pick")) {
68 init_pick(init, init->argc, init->argv, 0, 0);
69 } else if (!strcmp(pgm_name, "view")) {
70 view = new_view(init);
71 if (init->lines > 0 || init->cols > 0)
72 mview(init);
73 else if (!init_view_full_screen(init))
74 view_file(init);
75 } else if (!strcmp(pgm_name, "ckeys")) {
77 }
78 curs_set(1);
80 // erase();
81 // refresh();
85 exit(EXIT_SUCCESS);
86}
87
88int mview(Init *init) {
89 view = init->view;
90 if (init->lines != 0 && view->lines == 0)
91 view->lines = init->lines;
92 if (view->lines == 0)
93 view->lines = LINES * 3 / 4;
94 if (view->lines > LINES - 3)
95 view->lines = LINES - 3;
96 if (init->cols != 0 && view->lines == 0)
97 view->cols = init->cols;
98 if (view->cols == 0)
99 view->cols = COLS * 3 / 4;
100 if (view->cols > COLS - 3)
101 view->cols = COLS - 3;
102 if (init->begy != 0 && view->begy == 0)
103 view->begy = init->begy;
104 if (view->begy == 0)
105 view->begy = (LINES - view->lines) / 5;
106 if (view->begy + view->lines > LINES - 2)
107 view->begy = LINES - view->lines - 2;
108 if (init->begx != 0 && view->begx == 0)
109 view->begx = init->begx;
110 if (view->begx == 0)
111 view->begx = (COLS - view->cols) / 5;
112 if (view->begx + view->cols > COLS - 1)
113 view->begx = COLS - view->cols - 1;
114 view->f_full_screen = false;
115 if (!init_view_boxwin(init, init->title)) {
116 view_file(init);
117 win_del();
118 }
119 destroy_view(init);
120 return 0;
121}
Menu * menu
Definition mem.c:45
View * view
Definition mem.c:48
int popup_ckeys()
Display Curses Keys Responds to curses keys and mouse events, displaying the key code and description...
Definition curskeys.c:23
#define MAXLEN
Definition curskeys.c:15
int mview(Init *)
Definition menu.c:88
int main(int argc, char **argv)
Capture the current terminal settings for later restoration.
Definition enterchr.c:38
bool open_curses(SIO *)
Initialize NCurses and color settings.
Definition dwin.c:423
void win_init_attrs()
Initialize window attributes.
Definition dwin.c:162
WINDOW * win_del()
Delete the current window and its associated box window.
Definition dwin.c:902
void destroy_curses()
Gracefully shut down NCurses and restore terminal settings.
Definition dwin.c:738
int init_form(Init *, int, char **, int, int)
Initialize form data structure and parse description file.
Definition form_engine.c:59
bool base_name(char *, char *)
Returns the base name of a file specification.
Definition futil.c:775
void mapp_initialization(Init *, int, char **)
Main initialization function for MAPP - Menu Application.
Definition init.c:324
int init_view_full_screen(Init *)
Initialize C-Menu View in full screen mode.
Definition init_view.c:40
int init_view_boxwin(Init *, char *)
Initialize the C-Menu View in box window mode.
Definition init_view.c:123
View * destroy_view(Init *init)
Destroy View structure.
Definition mem.c:346
Init * new_init(int, char **)
Create and initialize Init structure.
Definition mem.c:70
Menu * new_menu(Init *, int, char **, int, int)
Create and initialize Menu structure.
Definition mem.c:148
View * new_view(Init *)
Create and initialize View structure.
Definition mem.c:300
unsigned int menu_engine(Init *)
The main loop of the menu system.
Definition menu_engine.c:37
int init_pick(Init *, int, char **, int, int)
Initializes pick structure and opens pick input file or pipe.
Definition pick_engine.c:59
bool capture_shell_tioctl()
capture_shell_tioctl() - capture shell terminal settings
Definition scriou.c:43
bool capture_curses_tioctl()
capture_curses_tioctl() - capture curses terminal settings
Definition scriou.c:68
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
Definition scriou.c:56
void sig_dfl_mode()
Set signal handlers to default behavior.
Definition sig.c:42
void sig_prog_mode()
Set up signal handlers for interrupt signals.
Definition sig.c:62
int view_file(Init *)
Start view.
char title[MAXLEN]
Definition common.h:123
int begx
Definition common.h:112
SIO * sio
Definition common.h:108
int argc
Definition common.h:124
int cols
Definition common.h:110
int begy
Definition common.h:111
View * view
Definition common.h:178
Menu * menu
Definition common.h:172
char ** argv
Definition common.h:125
int lines
Definition common.h:109
int cols
Definition view.h:49
bool f_full_screen
Definition view.h:86
int lines
Definition view.h:48
int begy
Definition view.h:50
int begx
Definition view.h:51