]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/dialog/guage.c
Import device-tree files from Linux 5.19
[FreeBSD/FreeBSD.git] / contrib / dialog / guage.c
1 /*
2  *  $Id: guage.c,v 1.83 2020/03/27 20:54:43 tom Exp $
3  *
4  *  guage.c -- implements the gauge dialog
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  *      Marc Ewing, Red Hat Software
25  */
26
27 #include <dialog.h>
28
29 #include <errno.h>
30
31 #define MY_LEN (MAX_LEN)/2
32
33 #define MIN_HIGH (4)
34 #define MIN_WIDE (10 + 2 * (2 + MARGIN))
35
36 #define isMarker(buf) !strncmp(buf, "XXX", (size_t) 3)
37
38 typedef struct _my_obj {
39     DIALOG_CALLBACK obj;        /* has to be first in struct */
40     struct _my_obj *next;
41     WINDOW *text;
42     char *title;
43     char *prompt;
44     char prompt_buf[MY_LEN];
45     int percent;
46     int height;
47     int width;
48     char line[MAX_LEN + 1];
49 } MY_OBJ;
50
51 static MY_OBJ *all_objects;
52
53 static int
54 valid(MY_OBJ * obj)
55 {
56     MY_OBJ *list = all_objects;
57     int result = 0;
58
59     while (list != 0) {
60         if (list == obj) {
61             result = 1;
62             break;
63         }
64         list = list->next;
65     }
66     return result;
67 }
68
69 static void
70 delink(MY_OBJ * obj)
71 {
72     MY_OBJ *p = all_objects;
73     MY_OBJ *q = 0;
74     while (p != 0) {
75         if (p == obj) {
76             if (q != 0) {
77                 q->next = p->next;
78             } else {
79                 all_objects = p->next;
80             }
81             break;
82         }
83         q = p;
84         p = p->next;
85     }
86 }
87
88 static int
89 read_data(char *buffer, FILE *fp)
90 {
91     int result;
92
93     if (feof(fp)) {
94         result = 0;
95     } else if (fgets(buffer, MY_LEN, fp) != 0) {
96         DLG_TRACE(("read_data:%s", buffer));
97         buffer[MY_LEN] = '\0';
98         dlg_trim_string(buffer);
99         result = 1;
100     } else {
101         result = -1;
102     }
103     return result;
104 }
105
106 static int
107 decode_percent(char *buffer)
108 {
109     char *tmp = 0;
110     long value = strtol(buffer, &tmp, 10);
111
112     if (tmp != 0 && (*tmp == 0 || isspace(UCH(*tmp))) && value >= 0) {
113         return TRUE;
114     }
115     return FALSE;
116 }
117
118 static void
119 repaint_text(MY_OBJ * obj)
120 {
121     WINDOW *dialog = obj->obj.win;
122
123     if (dialog != 0) {
124         int i, x;
125
126         (void) werase(dialog);
127         dlg_draw_box2(dialog, 0, 0, obj->height, obj->width, dialog_attr,
128                       border_attr, border2_attr);
129
130         dlg_draw_title(dialog, obj->title);
131
132         dlg_attrset(dialog, dialog_attr);
133         dlg_draw_helpline(dialog, FALSE);
134         dlg_print_autowrap(dialog, obj->prompt, obj->height, obj->width);
135
136         dlg_draw_box2(dialog,
137                       obj->height - 4, 2 + MARGIN,
138                       2 + MARGIN, obj->width - 2 * (2 + MARGIN),
139                       dialog_attr,
140                       border_attr,
141                       border2_attr);
142
143         /*
144          * Clear the area for the progress bar by filling it with spaces
145          * in the gauge-attribute, and write the percentage with that
146          * attribute.
147          */
148         (void) wmove(dialog, obj->height - 3, 4);
149         dlg_attrset(dialog, gauge_attr);
150
151         for (i = 0; i < (obj->width - 2 * (3 + MARGIN)); i++)
152             (void) waddch(dialog, ' ');
153
154         (void) wmove(dialog, obj->height - 3, (obj->width / 2) - 2);
155         (void) wprintw(dialog, "%3d%%", obj->percent);
156
157         /*
158          * Now draw a bar in reverse, relative to the background.
159          * The window attribute was useful for painting the background,
160          * but requires some tweaks to reverse it.
161          */
162         x = (obj->percent * (obj->width - 2 * (3 + MARGIN))) / 100;
163         if ((gauge_attr & A_REVERSE) != 0) {
164             dlg_attroff(dialog, A_REVERSE);
165         } else {
166             dlg_attrset(dialog, A_REVERSE);
167         }
168         (void) wmove(dialog, obj->height - 3, 4);
169         for (i = 0; i < x; i++) {
170             chtype ch2 = winch(dialog);
171             if (gauge_attr & A_REVERSE) {
172                 ch2 &= ~A_REVERSE;
173             }
174             (void) waddch(dialog, ch2);
175         }
176
177         (void) wrefresh(dialog);
178     }
179 }
180
181 static bool
182 handle_input(DIALOG_CALLBACK * cb)
183 {
184     MY_OBJ *obj = (MY_OBJ *) cb;
185     bool result;
186     bool cleanup = FALSE;
187     int status;
188     char buf[MY_LEN + 1];
189
190     if (dialog_state.pipe_input == 0) {
191         status = -1;
192         cleanup = TRUE;
193     } else if ((status = read_data(buf, dialog_state.pipe_input)) > 0) {
194
195         if (isMarker(buf)) {
196             /*
197              * Historically, next line should be percentage, but one of the
198              * worse-written clones of 'dialog' assumes the number is missing.
199              * (Gresham's Law applied to software).
200              */
201             if ((status = read_data(buf, dialog_state.pipe_input)) > 0) {
202
203                 obj->prompt_buf[0] = '\0';
204                 if (decode_percent(buf))
205                     obj->percent = atoi(buf);
206                 else
207                     strcpy(obj->prompt_buf, buf);
208
209                 /* Rest is message text */
210                 while ((status = read_data(buf, dialog_state.pipe_input)) > 0
211                        && !isMarker(buf)) {
212                     if (strlen(obj->prompt_buf) + strlen(buf) <
213                         sizeof(obj->prompt_buf) - 1) {
214                         strcat(obj->prompt_buf, buf);
215                     }
216                 }
217
218                 if (obj->prompt != obj->prompt_buf)
219                     free(obj->prompt);
220                 obj->prompt = obj->prompt_buf;
221             }
222         } else if (decode_percent(buf)) {
223             obj->percent = atoi(buf);
224         }
225     } else {
226         if (feof(dialog_state.pipe_input) ||
227             (ferror(dialog_state.pipe_input) && errno != EINTR)) {
228             cleanup = TRUE;
229         }
230     }
231
232     repaint_text(obj);
233     if (status > 0) {
234         result = TRUE;
235     } else {
236         result = FALSE;
237         if (cleanup) {
238             dlg_remove_callback(cb);
239             delink(obj);
240         }
241     }
242
243     return result;
244 }
245
246 static bool
247 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
248 {
249     bool status = TRUE;
250
251     *result = DLG_EXIT_OK;
252     if (cb != 0) {
253         if (!fkey && (ch == ERR)) {
254             (void) handle_input(cb);
255             /* cb might be freed in handle_input */
256             status = (valid((MY_OBJ *) cb) && (cb->input != 0));
257         }
258     } else {
259         status = FALSE;
260     }
261     return status;
262 }
263
264 static void
265 my_cleanup(DIALOG_CALLBACK * cb)
266 {
267     MY_OBJ *obj = (MY_OBJ *) cb;
268
269     if (valid(obj)) {
270         if (obj->prompt != obj->prompt_buf) {
271             free(obj->prompt);
272             obj->prompt = obj->prompt_buf;
273         }
274         free(obj->title);
275         dlg_del_window(obj->obj.win);
276         delink(obj);
277     }
278 }
279
280 void
281 dlg_update_gauge(void *objptr, int percent)
282 {
283     MY_OBJ *obj = (MY_OBJ *) objptr;
284     bool save_finish_string = dialog_state.finish_string;
285
286     dialog_state.finish_string = TRUE;
287     curs_set(0);
288     obj->percent = percent;
289     repaint_text(obj);
290     dialog_state.finish_string = save_finish_string;
291 }
292
293 /*
294  * (Re)Allocates an object and fills it as per the arguments
295  */
296 void *
297 dlg_reallocate_gauge(void *objptr,
298                      const char *title,
299                      const char *cprompt,
300                      int height,
301                      int width,
302                      int percent)
303 {
304     char *prompt = dlg_strclone(cprompt);
305     MY_OBJ *obj;
306     bool save_finish_string = dialog_state.finish_string;
307
308     dialog_state.finish_string = TRUE;
309     dlg_tab_correct_str(prompt);
310
311     if (objptr == 0) {
312         /* create a new object */
313         obj = dlg_calloc(MY_OBJ, 1);
314         assert_ptr(obj, "dialog_gauge");
315
316         dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
317         dlg_print_size(height, width);
318         dlg_ctl_size(height, width);
319
320     } else {
321         /* reuse an existing object */
322         obj = objptr;
323         height = obj->height;
324         width = obj->width;
325     }
326
327     if (obj->obj.win == 0) {
328         /* center dialog box on screen */
329         int x = dlg_box_x_ordinate(width);
330         int y = dlg_box_y_ordinate(height);
331         WINDOW *dialog = dlg_new_window(height, width, y, x);
332         obj->obj.win = dialog;
333     }
334
335     obj->obj.input = dialog_state.pipe_input;
336     obj->obj.keep_win = TRUE;
337     obj->obj.bg_task = TRUE;
338     obj->obj.handle_getc = handle_my_getc;
339     obj->obj.handle_input = handle_input;
340
341     if (obj->title == 0 || strcmp(obj->title, title)) {
342         dlg_finish_string(obj->title);
343         free(obj->title);
344         obj->title = dlg_strclone(title);
345     }
346
347     dlg_finish_string(obj->prompt);
348     free(obj->prompt);
349
350     obj->prompt = prompt;
351     obj->percent = percent;
352     obj->height = height;
353     obj->width = width;
354
355     /* if this was a new object, link it into the list */
356     if (objptr == 0) {
357         obj->next = all_objects;
358         all_objects = obj;
359     }
360
361     dialog_state.finish_string = save_finish_string;
362     return (void *) obj;
363 }
364
365 void *
366 dlg_allocate_gauge(const char *title,
367                    const char *cprompt,
368                    int height,
369                    int width,
370                    int percent)
371 {
372     return dlg_reallocate_gauge(NULL, title, cprompt, height, width, percent);
373 }
374
375 void
376 dlg_free_gauge(void *objptr)
377 {
378     MY_OBJ *obj = (MY_OBJ *) objptr;
379
380     if (valid(obj)) {
381         if (obj->title)
382             free(obj->title);
383         if (obj->prompt)
384             free(obj->prompt);
385         obj->obj.keep_win = FALSE;
386         dlg_remove_callback(&(obj->obj));
387         delink(obj);
388     }
389     curs_set(1);
390 }
391
392 /*
393  * Display a gauge, or progress meter.  Starts at percent% and reads stdin.  If
394  * stdin is not XXX, then it is interpreted as a percentage, and the display is
395  * updated accordingly.  Otherwise the next line is the percentage, and
396  * subsequent lines up to another XXX are used for the new prompt.  Note that
397  * the size of the window never changes, so the prompt can not get any larger
398  * than the height and width specified.
399  */
400 int
401 dialog_gauge(const char *title,
402              const char *cprompt,
403              int height,
404              int width,
405              int percent)
406 {
407     int fkey;
408     int ch, result;
409     void *objptr = dlg_allocate_gauge(title, cprompt, height, width, percent);
410     MY_OBJ *obj = (MY_OBJ *) objptr;
411
412     DLG_TRACE(("# gauge args:\n"));
413     DLG_TRACE2S("title", title);
414     DLG_TRACE2S("message", cprompt);
415     DLG_TRACE2N("height", height);
416     DLG_TRACE2N("width", width);
417     DLG_TRACE2N("percent", percent);
418
419     dlg_add_callback_ref((DIALOG_CALLBACK **) & obj, my_cleanup);
420     dlg_update_gauge(obj, percent);
421
422     dlg_trace_win(obj->obj.win);
423     do {
424         ch = dlg_getc(obj->obj.win, &fkey);
425 #ifdef KEY_RESIZE
426         if (fkey && ch == KEY_RESIZE) {
427             MY_OBJ *oldobj = obj;
428
429             dlg_will_resize(obj->obj.win);
430
431             obj = dlg_allocate_gauge(title,
432                                      cprompt,
433                                      height,
434                                      width,
435                                      oldobj->percent);
436
437             /* avoid breaking new window in dlg_remove_callback */
438             oldobj->obj.caller = 0;
439             oldobj->obj.input = 0;
440             oldobj->obj.keep_win = FALSE;
441
442             /* remove the old version of the gauge */
443             _dlg_resize_cleanup(oldobj->obj.win);
444             dlg_remove_callback(&(oldobj->obj));
445
446             dlg_add_callback_ref((DIALOG_CALLBACK **) & obj, my_cleanup);
447             dlg_update_gauge(obj, obj->percent);
448         }
449 #endif
450     }
451     while (valid(obj) && handle_my_getc(&(obj->obj), ch, fkey, &result));
452
453     dlg_free_gauge(obj);
454
455     return (DLG_EXIT_OK);
456 }