libressl: Fix validation errors in certificate chains with expired certificates
[dragonfly.git] / contrib / dialog / inputbox.c
1 /*
2  *  $Id: inputbox.c,v 1.91 2020/03/27 20:25:40 tom Exp $
3  *
4  *  inputbox.c -- implements the input box
5  *
6  *  Copyright 2000-2019,2020 Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License, version 2.1
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this program; if not, write to
19  *      Free Software Foundation, Inc.
20  *      51 Franklin St., Fifth Floor
21  *      Boston, MA 02110, USA.
22  *
23  *  An earlier version of this program lists as authors:
24  *      Savio Lam (lam836@cs.cuhk.hk)
25  */
26
27 #include <dlg_internals.h>
28 #include <dlg_keys.h>
29
30 #define sTEXT -1
31
32 #define NAVIGATE_BINDINGS \
33         DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_DOWN ), \
34         DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), \
35         DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
36         DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
37         DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ), \
38         DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_UP )
39
40 /*
41  * Display a dialog box for entering a string
42  */
43 int
44 dialog_inputbox(const char *title, const char *cprompt, int height, int width,
45                 const char *init, const int password)
46 {
47     /* *INDENT-OFF* */
48     static DLG_KEYS_BINDING binding[] = {
49         HELPKEY_BINDINGS,
50         ENTERKEY_BINDINGS,
51         NAVIGATE_BINDINGS,
52         TOGGLEKEY_BINDINGS,
53         END_KEYS_BINDING
54     };
55     static DLG_KEYS_BINDING binding2[] = {
56         INPUTSTR_BINDINGS,
57         HELPKEY_BINDINGS,
58         ENTERKEY_BINDINGS,
59         NAVIGATE_BINDINGS,
60         /* no TOGGLEKEY_BINDINGS, since that includes space... */
61         END_KEYS_BINDING
62     };
63     /* *INDENT-ON* */
64
65 #ifdef KEY_RESIZE
66     int old_height = height;
67     int old_width = width;
68 #endif
69     int xorg, yorg;
70     int x, y, box_y, box_x, box_width;
71     int show_buttons;
72     int col_offset = 0;
73     int chr_offset = 0;
74     int key, fkey, code;
75     int result = DLG_EXIT_UNKNOWN;
76     int state;
77     bool first;
78     bool edited;
79     char *input;
80     WINDOW *dialog;
81     WINDOW *editor;
82     char *prompt = dlg_strclone(cprompt);
83     const char **buttons = dlg_ok_labels();
84
85     dlg_does_output();
86
87     DLG_TRACE(("# inputbox args:\n"));
88     DLG_TRACE2S("title", title);
89     DLG_TRACE2S("message", cprompt);
90     DLG_TRACE2N("height", height);
91     DLG_TRACE2N("width", width);
92     DLG_TRACE2S("init", init);
93     DLG_TRACE2N("password", password);
94
95     dlg_tab_correct_str(prompt);
96
97     /* Set up the initial value */
98     input = dlg_set_result(init);
99     edited = FALSE;
100
101 #ifdef KEY_RESIZE
102   retry:
103 #endif
104     show_buttons = TRUE;
105     state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
106     first = (state == sTEXT);
107     key = fkey = 0;
108
109     if (init != NULL) {
110         dlg_auto_size(title, prompt, &height, &width, 5,
111                       MIN(MAX(dlg_count_columns(init) + 7, 26),
112                           SCOLS - (dialog_vars.begin_set ?
113                                    dialog_vars.begin_x : 0)));
114         chr_offset = (int) strlen(init);
115     } else {
116         dlg_auto_size(title, prompt, &height, &width, 5, 26);
117     }
118     dlg_button_layout(buttons, &width);
119     dlg_print_size(height, width);
120     dlg_ctl_size(height, width);
121
122     xorg = dlg_box_x_ordinate(width);
123     yorg = dlg_box_y_ordinate(height);
124
125     dialog = dlg_new_window(height, width, yorg, xorg);
126     dlg_register_window(dialog, "inputbox", binding);
127     dlg_register_buttons(dialog, "inputbox", buttons);
128
129     dlg_mouse_setbase(xorg, yorg);
130
131     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
132     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
133     dlg_draw_title(dialog, title);
134
135     dlg_attrset(dialog, dialog_attr);
136     dlg_draw_helpline(dialog, FALSE);
137     dlg_print_autowrap(dialog, prompt, height, width);
138
139     /* Draw the input field box */
140     box_width = width - 6;
141     getyx(dialog, y, x);
142     (void) x;
143     box_y = y + 2;
144     box_x = (width - box_width) / 2;
145     dlg_mouse_mkregion(y + 1, box_x - 1, 3, box_width + 2, 'i');
146     dlg_draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
147                  border_attr, border2_attr);
148
149     /* Make a window for the input-field, to associate bindings */
150     editor = dlg_sub_window(dialog, 1, box_width, yorg + box_y, xorg + box_x);
151     dlg_register_window(editor, "inputbox2", binding2);
152
153     if (*input != '\0') {
154         dlg_show_string(editor, input, chr_offset, inputbox_attr,
155                         0, 0, box_width, (bool) (password != 0), first);
156         wsyncup(editor);
157         wcursyncup(editor);
158     }
159
160     while (result == DLG_EXIT_UNKNOWN) {
161         /*
162          * The last field drawn determines where the cursor is shown:
163          */
164         if (show_buttons) {
165             show_buttons = FALSE;
166             col_offset = dlg_edit_offset(input, chr_offset, box_width);
167             (void) wmove(dialog, box_y, box_x + col_offset);
168             dlg_draw_buttons(dialog, height - 2, 0, buttons, state, FALSE, width);
169         }
170
171         if (!first) {
172             if (*input != '\0' && !edited) {
173                 dlg_show_string(editor, input, chr_offset, inputbox_attr,
174                                 0, 0, box_width, (bool) (password != 0), first);
175                 wmove(editor, 0, chr_offset);
176                 wsyncup(editor);
177                 wcursyncup(editor);
178             }
179             key = dlg_mouse_wgetch((state == sTEXT) ? editor : dialog, &fkey);
180             if (dlg_result_key(key, fkey, &result)) {
181                 if (!dlg_button_key(result, &code, &key, &fkey))
182                     break;
183             }
184         }
185
186         /*
187          * Handle mouse clicks first, since we want to know if this is a button,
188          * or something that dlg_edit_string() should handle.
189          */
190         if (fkey
191             && is_DLGK_MOUSE(key)
192             && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
193             result = code;
194             continue;
195         }
196
197         if (state == sTEXT) {   /* Input box selected */
198             int edit = dlg_edit_string(input, &chr_offset, key, fkey, first);
199
200             if (edit) {
201                 dlg_show_string(editor, input, chr_offset, inputbox_attr,
202                                 0, 0, box_width, (bool) (password != 0), first);
203                 wsyncup(editor);
204                 wcursyncup(editor);
205                 first = FALSE;
206                 edited = TRUE;
207                 continue;
208             } else if (first) {
209                 first = FALSE;
210                 continue;
211             }
212         }
213
214         /* handle non-functionkeys */
215         if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
216             dlg_del_window(dialog);
217             result = dlg_ok_buttoncode(code);
218             continue;
219         }
220
221         /* handle functionkeys */
222         if (fkey) {
223             switch (key) {
224             case DLGK_MOUSE('i'):       /* mouse enter events */
225                 state = 0;
226                 /* FALLTHRU */
227             case DLGK_FIELD_PREV:
228                 show_buttons = TRUE;
229                 state = dlg_prev_ok_buttonindex(state, sTEXT);
230                 break;
231             case DLGK_FIELD_NEXT:
232                 show_buttons = TRUE;
233                 state = dlg_next_ok_buttonindex(state, sTEXT);
234                 break;
235             case DLGK_TOGGLE:
236             case DLGK_ENTER:
237                 dlg_del_window(dialog);
238                 result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
239                 break;
240 #ifdef KEY_RESIZE
241             case KEY_RESIZE:
242                 dlg_will_resize(dialog);
243                 /* reset data */
244                 height = old_height;
245                 width = old_width;
246                 /* repaint */
247                 _dlg_resize_cleanup(dialog);
248                 goto retry;
249 #endif
250             default:
251                 beep();
252                 break;
253             }
254         } else if (key > 0) {
255             beep();
256         }
257     }
258     AddLastKey();
259
260     dlg_unregister_window(editor);
261     dlg_del_window(dialog);
262     dlg_mouse_free_regions();
263     free(prompt);
264     return result;
265 }