1 /*
2  *  dialog.h -- common declarations for all dialog modules
3  *
4  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #include <sys/types.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <ctype.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdbool.h>
16 
17 #ifndef KBUILD_NO_NLS
18 # include <libintl.h>
19 #else
20 # define gettext(Msgid) ((const char *) (Msgid))
21 #endif
22 
23 #ifdef __sun__
24 #define CURS_MACROS
25 #endif
26 #include CURSES_LOC
27 
28 /*
29  * Colors in ncurses 1.9.9e do not work properly since foreground and
30  * background colors are OR'd rather than separately masked.  This version
31  * of dialog was hacked to work with ncurses 1.9.9e, making it incompatible
32  * with standard curses.  The simplest fix (to make this work with standard
33  * curses) uses the wbkgdset() function, not used in the original hack.
34  * Turn it off if we're building with 1.9.9e, since it just confuses things.
35  */
36 #if defined(NCURSES_VERSION) && defined(_NEED_WRAP) && !defined(GCC_PRINTFLIKE)
37 #define OLD_NCURSES 1
38 #undef  wbkgdset
39 #define wbkgdset(w,p)		/*nothing */
40 #else
41 #define OLD_NCURSES 0
42 #endif
43 
44 #define TR(params) _tracef params
45 
46 #define KEY_ESC 27
47 #define TAB 9
48 #define MAX_LEN 2048
49 #define BUF_SIZE (10*1024)
50 #define MIN(x,y) (x < y ? x : y)
51 #define MAX(x,y) (x > y ? x : y)
52 
53 #ifndef ACS_ULCORNER
54 #define ACS_ULCORNER '+'
55 #endif
56 #ifndef ACS_LLCORNER
57 #define ACS_LLCORNER '+'
58 #endif
59 #ifndef ACS_URCORNER
60 #define ACS_URCORNER '+'
61 #endif
62 #ifndef ACS_LRCORNER
63 #define ACS_LRCORNER '+'
64 #endif
65 #ifndef ACS_HLINE
66 #define ACS_HLINE '-'
67 #endif
68 #ifndef ACS_VLINE
69 #define ACS_VLINE '|'
70 #endif
71 #ifndef ACS_LTEE
72 #define ACS_LTEE '+'
73 #endif
74 #ifndef ACS_RTEE
75 #define ACS_RTEE '+'
76 #endif
77 #ifndef ACS_UARROW
78 #define ACS_UARROW '^'
79 #endif
80 #ifndef ACS_DARROW
81 #define ACS_DARROW 'v'
82 #endif
83 
84 /* error return codes */
85 #define ERRDISPLAYTOOSMALL (KEY_MAX + 1)
86 
87 /*
88  *   Color definitions
89  */
90 struct dialog_color {
91 	chtype atr;	/* Color attribute */
92 	int fg;		/* foreground */
93 	int bg;		/* background */
94 	int hl;		/* highlight this item */
95 };
96 
97 struct subtitle_list {
98 	struct subtitle_list *next;
99 	const char *text;
100 };
101 
102 struct dialog_info {
103 	const char *backtitle;
104 	struct subtitle_list *subtitles;
105 	struct dialog_color screen;
106 	struct dialog_color shadow;
107 	struct dialog_color dialog;
108 	struct dialog_color title;
109 	struct dialog_color border;
110 	struct dialog_color button_active;
111 	struct dialog_color button_inactive;
112 	struct dialog_color button_key_active;
113 	struct dialog_color button_key_inactive;
114 	struct dialog_color button_label_active;
115 	struct dialog_color button_label_inactive;
116 	struct dialog_color inputbox;
117 	struct dialog_color inputbox_border;
118 	struct dialog_color searchbox;
119 	struct dialog_color searchbox_title;
120 	struct dialog_color searchbox_border;
121 	struct dialog_color position_indicator;
122 	struct dialog_color menubox;
123 	struct dialog_color menubox_border;
124 	struct dialog_color item;
125 	struct dialog_color item_selected;
126 	struct dialog_color tag;
127 	struct dialog_color tag_selected;
128 	struct dialog_color tag_key;
129 	struct dialog_color tag_key_selected;
130 	struct dialog_color check;
131 	struct dialog_color check_selected;
132 	struct dialog_color uarrow;
133 	struct dialog_color darrow;
134 };
135 
136 /*
137  * Global variables
138  */
139 extern struct dialog_info dlg;
140 extern char dialog_input_result[];
141 extern int saved_x, saved_y;		/* Needed in signal handler in mconf.c */
142 
143 /*
144  * Function prototypes
145  */
146 
147 /* item list as used by checklist and menubox */
148 void item_reset(void);
149 void item_make(const char *fmt, ...);
150 void item_add_str(const char *fmt, ...);
151 void item_set_tag(char tag);
152 void item_set_data(void *p);
153 void item_set_selected(int val);
154 int item_activate_selected(void);
155 void *item_data(void);
156 char item_tag(void);
157 
158 /* item list manipulation for lxdialog use */
159 #define MAXITEMSTR 200
160 struct dialog_item {
161 	char str[MAXITEMSTR];	/* prompt displayed */
162 	char tag;
163 	void *data;	/* pointer to menu item - used by menubox+checklist */
164 	int selected;	/* Set to 1 by dialog_*() function if selected. */
165 };
166 
167 /* list of lialog_items */
168 struct dialog_list {
169 	struct dialog_item node;
170 	struct dialog_list *next;
171 };
172 
173 extern struct dialog_list *item_cur;
174 extern struct dialog_list item_nil;
175 extern struct dialog_list *item_head;
176 
177 int item_count(void);
178 void item_set(int n);
179 int item_n(void);
180 const char *item_str(void);
181 int item_is_selected(void);
182 int item_is_tag(char tag);
183 #define item_foreach() \
184 	for (item_cur = item_head ? item_head: item_cur; \
185 	     item_cur && (item_cur != &item_nil); item_cur = item_cur->next)
186 
187 /* generic key handlers */
188 int on_key_esc(WINDOW *win);
189 int on_key_resize(void);
190 
191 /* minimum (re)size values */
192 #define CHECKLIST_HEIGTH_MIN 6	/* For dialog_checklist() */
193 #define CHECKLIST_WIDTH_MIN 6
194 #define INPUTBOX_HEIGTH_MIN 2	/* For dialog_inputbox() */
195 #define INPUTBOX_WIDTH_MIN 2
196 #define MENUBOX_HEIGTH_MIN 15	/* For dialog_menu() */
197 #define MENUBOX_WIDTH_MIN 65
198 #define TEXTBOX_HEIGTH_MIN 8	/* For dialog_textbox() */
199 #define TEXTBOX_WIDTH_MIN 8
200 #define YESNO_HEIGTH_MIN 4	/* For dialog_yesno() */
201 #define YESNO_WIDTH_MIN 4
202 #define WINDOW_HEIGTH_MIN 19	/* For init_dialog() */
203 #define WINDOW_WIDTH_MIN 80
204 
205 int init_dialog(const char *backtitle);
206 void set_dialog_backtitle(const char *backtitle);
207 void set_dialog_subtitles(struct subtitle_list *subtitles);
208 void end_dialog(int x, int y);
209 void attr_clear(WINDOW * win, int height, int width, chtype attr);
210 void dialog_clear(void);
211 void print_autowrap(WINDOW * win, const char *prompt, int width, int y, int x);
212 void print_button(WINDOW * win, const char *label, int y, int x, int selected);
213 void print_title(WINDOW *dialog, const char *title, int width);
214 void draw_box(WINDOW * win, int y, int x, int height, int width, chtype box,
215 	      chtype border);
216 void draw_shadow(WINDOW * win, int y, int x, int height, int width);
217 
218 int first_alpha(const char *string, const char *exempt);
219 int dialog_yesno(const char *title, const char *prompt, int height, int width);
220 int dialog_msgbox(const char *title, const char *prompt, int height,
221 		  int width, int pause);
222 
223 
224 typedef void (*update_text_fn)(char *buf, size_t start, size_t end, void
225 			       *_data);
226 int dialog_textbox(const char *title, char *tbuf, int initial_height,
227 		   int initial_width, int *keys, int *_vscroll, int *_hscroll,
228 		   update_text_fn update_text, void *data);
229 int dialog_menu(const char *title, const char *prompt,
230 		const void *selected, int *s_scroll);
231 int dialog_checklist(const char *title, const char *prompt, int height,
232 		     int width, int list_height);
233 int dialog_inputbox(const char *title, const char *prompt, int height,
234 		    int width, const char *init);
235 
236 /*
237  * This is the base for fictitious keys, which activate
238  * the buttons.
239  *
240  * Mouse-generated keys are the following:
241  *   -- the first 32 are used as numbers, in addition to '0'-'9'
242  *   -- the lowercase are used to signal mouse-enter events (M_EVENT + 'o')
243  *   -- uppercase chars are used to invoke the button (M_EVENT + 'O')
244  */
245 #define M_EVENT (KEY_MAX+1)
246