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#define _GNU_SOURCE
10#include "common.h"
11#include <string.h>
12#ifdef NCURSES_UI
13#include "../ui/ui_ncurses_internal.h"
14#include "ui_backend.h"
15#include <ncursesw/ncurses.h>
16#include <ncursesw/panel.h>
17#endif
18
19/** @brief This function is responsible for cleaning up the terminal state and
20 exiting the program.
21 @details This function is typically registered to be called when the program
22 exits, ensuring that the terminal is properly restored to its original state,
23 even if the program encounters an error or is terminated unexpectedly. The
24 end_pgm function performs the following actions:
25 1. It calls win_del() to delete any windows that may have been created
26 during the program's execution.
27 2. It calls destroy_curses() to clean up the ncurses library and restore the
28 terminal to its normal state.
29 3. It calls restore_shell_tioctl() to restore the terminal's input/output
30 settings to their original state.
31 4. Finally, it calls exit(EXIT_FAILURE) to terminate the program with a
32 failure status, indicating that the program has encountered an error or is
33 exiting due to an unexpected condition. By defining this function and
34 registering it to be called on program exit, you can help ensure that the
35 terminal is properly cleaned up and restored, preventing any issues with the
36 terminal state after the program has exited. */
37static void end_pgm(void) {
38 exit(EXIT_SUCCESS);
39}
40
41int main(int argc, char **argv) {
42 int rc;
43 char pgm_name[MAXLEN];
45 Init *init = new_init(argc, argv);
46 mapp_initialization(init, argc, argv);
47 UiConfig ui_config = {
48 .border_style = UI_BORDER_ROUNDED,
49 .log_file = "/tmp/mylog.log",
50 .log_level = INFO};
51 ui_init(&ui_config, init->sio);
52 rc = atexit(end_pgm);
53 if (rc != 0) {
54 fprintf(stderr, "\n");
55 fprintf(stderr, _("Cannot set exit function"));
56 fprintf(stderr, "\n");
57 exit(EXIT_FAILURE);
58 }
61 base_name(pgm_name, argv[0]);
62 if (!strcmp(pgm_name, "menu")) {
63 new_menu(init, init->argc, init->argv, LINES / 14, COLS / 14);
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 *view = new_view(init);
71 if (init->lines > 0 || init->cols > 0) {
72 if (init->h_shift == 0)
73 init->h_shift = view->cols / 3;
74 if (!init_view_boxwin(init)) {
75 view_file(init);
76 }
77 } else if (!init_view_full_screen(init)) {
78 view_file(init);
79 }
80 } else if (!strcmp(pgm_name, "ckeys"))
85 exit(EXIT_SUCCESS);
86}
87
88void cmenu_shutdown(Init *init) {
92}
93
94void cmenu_abend(Init *init, int ec, char *s) {
98 fprintf(stderr, _("\n\nABEND: %s (code: %d)\n"), s, ec);
99 exit(EXIT_FAILURE);
100}
UiRuntime * ui_init(const UiConfig *config, SIO *sio)
Initialize the UI runtime with the given configuration and SIO.
Definition ui_ncurses.c:129
void ui_shutdown()
Shutdown the UI runtime, cleaning up resources and freeing memory.
Definition ui_ncurses.c:418
int popup_ckeys()
Definition curskeys.c:31
#define MAXLEN
Definition curskeys.c:15
void cmenu_shutdown(Init *init)
Definition menu.c:88
void cmenu_abend(Init *init, int ec, char *s)
Definition menu.c:94
#define _(String)
Definition amort.c:23
int main(int argc, char **argv)
Capture the current terminal settings for later restoration.
Definition amort.c:41
int init_form(Init *, int, char **, uint, uint)
Initialize form data structure and parse description file.
Definition form_engine.c:61
bool base_name(char *, char *)
Returns the base name of a file specification.
Definition futil.c:1132
void mapp_initialization(Init *, int, char **)
Main initialization function for MAPP - Menu Application.
Definition init.c:498
int init_view_full_screen(Init *init)
Initialize C-Menu View in full screen mode.
Definition init_view.c:47
int init_view_boxwin(Init *init)
Initialize the C-Menu View in box window mode.
Definition init_view.c:162
View * destroy_view(Init *init)
Destroy View structure.
Definition mem.c:337
Menu * new_menu(Init *init, int argc, char **argv, uint begy, uint begx)
Create and initialize Menu structure.
Definition mem.c:126
Init * new_init(int argc, char **argv)
Create and initialize Init structure.
Definition mem.c:60
View * new_view(Init *init)
Create and initialize View structure.
Definition mem.c:291
Init * destroy_init(Init *init)
Destroy Init structure.
Definition mem.c:91
unsigned int menu_engine(Init *)
The main loop of the menu system.
Definition menu_engine.c:36
int init_pick(Init *, int, char **, uint, uint)
Initializes pick structure and opens pick input file or pipe.
Definition pick_engine.c:68
bool capture_shell_tioctl()
capture_shell_tioctl() - capture shell terminal settings
Definition scriou.c:43
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
Definition scriou.c:57
bool capture_program_tioctl()
capture_program_tioctl() - capture program terminal settings
Definition scriou.c:70
void sig_dfl_mode()
Set signal handlers to default behavior.
Definition sig.c:41
void sig_prog_mode()
Set up signal handlers for interrupt signals.
Definition sig.c:61
int view_file(Init *)
Start view.
UiBorderStyle border_style
Definition ui_backend.h:445
char * log_file
Definition ui_backend.h:446
UiLogLevel log_level
Definition ui_backend.h:447
SIO * sio
Definition common.h:117
int argc
Definition common.h:133
int cols
Definition common.h:119
int h_shift
Definition common.h:186
char ** argv
Definition common.h:134
int lines
Definition common.h:118
uint cols
Definition view.h:71