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