1 /*
2  *  $Id: guage.c,v 1.64 2011/10/20 23:34:35 tom Exp $
3  *
4  *  guage.c -- implements the gauge dialog
5  *
6  *  Copyright 2000-2010,2011	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     const 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
valid(MY_OBJ * obj)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
delink(MY_OBJ * obj)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
read_data(char * buffer,FILE * fp)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 	dlg_trim_string(buffer);
98 	result = 1;
99     } else {
100 	result = -1;
101     }
102     return result;
103 }
104 
105 static int
decode_percent(char * buffer)106 decode_percent(char *buffer)
107 {
108     char *tmp = 0;
109     long value = strtol(buffer, &tmp, 10);
110 
111     if (tmp != 0 && (*tmp == 0 || isspace(UCH(*tmp))) && value >= 0) {
112 	return TRUE;
113     }
114     return FALSE;
115 }
116 
117 static void
repaint_text(MY_OBJ * obj)118 repaint_text(MY_OBJ * obj)
119 {
120     WINDOW *dialog = obj->obj.win;
121     int i, x;
122 
123     if (dialog != 0 && obj->obj.input != 0) {
124 	(void) werase(dialog);
125 	dlg_draw_box2(dialog, 0, 0, obj->height, obj->width, dialog_attr,
126 		      border_attr, border2_attr);
127 
128 	dlg_draw_title(dialog, obj->title);
129 
130 	(void) wattrset(dialog, dialog_attr);
131 	dlg_draw_helpline(dialog, FALSE);
132 	dlg_print_autowrap(dialog, obj->prompt, obj->height, obj->width);
133 
134 	dlg_draw_box2(dialog,
135 		      obj->height - 4, 2 + MARGIN,
136 		      2 + MARGIN, obj->width - 2 * (2 + MARGIN),
137 		      dialog_attr,
138 		      border_attr,
139 		      border2_attr);
140 
141 	/*
142 	 * Clear the area for the progress bar by filling it with spaces
143 	 * in the gauge-attribute, and write the percentage with that
144 	 * attribute.
145 	 */
146 	(void) wmove(dialog, obj->height - 3, 4);
147 	(void) wattrset(dialog, gauge_attr);
148 
149 	for (i = 0; i < (obj->width - 2 * (3 + MARGIN)); i++)
150 	    (void) waddch(dialog, ' ');
151 
152 	(void) wmove(dialog, obj->height - 3, (obj->width / 2) - 2);
153 	(void) wprintw(dialog, "%3d%%", obj->percent);
154 
155 	/*
156 	 * Now draw a bar in reverse, relative to the background.
157 	 * The window attribute was useful for painting the background,
158 	 * but requires some tweaks to reverse it.
159 	 */
160 	x = (obj->percent * (obj->width - 2 * (3 + MARGIN))) / 100;
161 	if ((gauge_attr & A_REVERSE) != 0) {
162 	    wattroff(dialog, A_REVERSE);
163 	} else {
164 	    (void) wattrset(dialog, A_REVERSE);
165 	}
166 	(void) wmove(dialog, obj->height - 3, 4);
167 	for (i = 0; i < x; i++) {
168 	    chtype ch2 = winch(dialog);
169 	    if (gauge_attr & A_REVERSE) {
170 		ch2 &= ~A_REVERSE;
171 	    }
172 	    (void) waddch(dialog, ch2);
173 	}
174 
175 	(void) wrefresh(dialog);
176     }
177 }
178 
179 static bool
handle_input(DIALOG_CALLBACK * cb)180 handle_input(DIALOG_CALLBACK * cb)
181 {
182     MY_OBJ *obj = (MY_OBJ *) cb;
183     bool result;
184     int status;
185     char buf[MY_LEN];
186 
187     if (dialog_state.pipe_input == 0) {
188 	status = -1;
189     } else if ((status = read_data(buf, dialog_state.pipe_input)) > 0) {
190 
191 	if (isMarker(buf)) {
192 	    /*
193 	     * Historically, next line should be percentage, but one of the
194 	     * worse-written clones of 'dialog' assumes the number is missing.
195 	     * (Gresham's Law applied to software).
196 	     */
197 	    if ((status = read_data(buf, dialog_state.pipe_input)) > 0) {
198 
199 		obj->prompt_buf[0] = '\0';
200 		if (decode_percent(buf))
201 		    obj->percent = atoi(buf);
202 		else
203 		    strcpy(obj->prompt_buf, buf);
204 
205 		/* Rest is message text */
206 		while ((status = read_data(buf, dialog_state.pipe_input)) > 0
207 		       && !isMarker(buf)) {
208 		    if (strlen(obj->prompt_buf) + strlen(buf) <
209 			sizeof(obj->prompt_buf) - 1) {
210 			strcat(obj->prompt_buf, buf);
211 		    }
212 		}
213 
214 		if (obj->prompt != obj->prompt_buf)
215 		    free(obj->prompt);
216 		obj->prompt = obj->prompt_buf;
217 	    }
218 	} else if (decode_percent(buf)) {
219 	    obj->percent = atoi(buf);
220 	}
221     } else {
222 	if (feof(dialog_state.pipe_input) ||
223 	    (ferror(dialog_state.pipe_input) && errno != EINTR)) {
224 	    delink(obj);
225 	    dlg_remove_callback(cb);
226 	}
227     }
228 
229     if (status > 0) {
230 	result = TRUE;
231 	repaint_text(obj);
232     } else {
233 	result = FALSE;
234     }
235 
236     return result;
237 }
238 
239 static bool
handle_my_getc(DIALOG_CALLBACK * cb,int ch,int fkey,int * result)240 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
241 {
242     int status = TRUE;
243 
244     *result = DLG_EXIT_OK;
245     if (cb != 0) {
246 	if (!fkey && (ch == ERR)) {
247 	    (void) handle_input(cb);
248 	    /* cb might be freed in handle_input */
249 	    status = (valid((MY_OBJ *) cb) && (cb->input != 0));
250 	}
251     } else {
252 	status = FALSE;
253     }
254     return status;
255 }
256 
257 static void
my_cleanup(DIALOG_CALLBACK * cb)258 my_cleanup(DIALOG_CALLBACK * cb)
259 {
260     MY_OBJ *obj = (MY_OBJ *) cb;
261 
262     if (valid(obj)) {
263 	if (obj->prompt != obj->prompt_buf) {
264 	    free(obj->prompt);
265 	    obj->prompt = obj->prompt_buf;
266 	}
267 	delink(obj);
268     }
269 }
270 
271 void
dlg_update_gauge(void * objptr,int percent)272 dlg_update_gauge(void *objptr, int percent)
273 {
274     MY_OBJ *obj = (MY_OBJ *) objptr;
275 
276     curs_set(0);
277     obj->percent = percent;
278     repaint_text(obj);
279 }
280 
281 /*
282  * Allocates a new object and fills it as per the arguments
283  */
284 void *
dlg_allocate_gauge(const char * title,const char * cprompt,int height,int width,int percent)285 dlg_allocate_gauge(const char *title,
286 		   const char *cprompt,
287 		   int height,
288 		   int width,
289 		   int percent)
290 {
291     int x, y;
292     char *prompt = dlg_strclone(cprompt);
293     WINDOW *dialog;
294     MY_OBJ *obj = 0;
295 
296     dlg_tab_correct_str(prompt);
297 
298     dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
299     dlg_print_size(height, width);
300     dlg_ctl_size(height, width);
301 
302     /* center dialog box on screen */
303     x = dlg_box_x_ordinate(width);
304     y = dlg_box_y_ordinate(height);
305 
306     dialog = dlg_new_window(height, width, y, x);
307 
308     obj = dlg_calloc(MY_OBJ, 1);
309     assert_ptr(obj, "dialog_gauge");
310 
311     obj->obj.input = dialog_state.pipe_input;
312     obj->obj.win = dialog;
313     obj->obj.keep_win = TRUE;
314     obj->obj.bg_task = TRUE;
315     obj->obj.handle_getc = handle_my_getc;
316     obj->obj.handle_input = handle_input;
317 
318     obj->title = title;
319     obj->prompt = prompt;
320     obj->percent = percent;
321     obj->height = height;
322     obj->width = width;
323 
324     obj->next = all_objects;
325     all_objects = obj;
326 
327     return (void *) obj;
328 }
329 
330 void
dlg_free_gauge(void * objptr)331 dlg_free_gauge(void *objptr)
332 {
333     MY_OBJ *obj = (MY_OBJ *) objptr;
334 
335     curs_set(1);
336     if (valid(obj)) {
337 	delink(obj);
338 	obj->obj.keep_win = FALSE;
339 	dlg_remove_callback(&(obj->obj));
340     }
341 }
342 
343 /*
344  * Display a gauge, or progress meter.  Starts at percent% and reads stdin.  If
345  * stdin is not XXX, then it is interpreted as a percentage, and the display is
346  * updated accordingly.  Otherwise the next line is the percentage, and
347  * subsequent lines up to another XXX are used for the new prompt.  Note that
348  * the size of the window never changes, so the prompt can not get any larger
349  * than the height and width specified.
350  */
351 int
dialog_gauge(const char * title,const char * cprompt,int height,int width,int percent)352 dialog_gauge(const char *title,
353 	     const char *cprompt,
354 	     int height,
355 	     int width,
356 	     int percent)
357 {
358     int fkey;
359     int ch, result;
360     void *objptr = dlg_allocate_gauge(title, cprompt, height, width, percent);
361     MY_OBJ *obj = (MY_OBJ *) objptr;
362 
363     dlg_add_callback_ref((DIALOG_CALLBACK **) & obj, my_cleanup);
364     dlg_update_gauge(obj, percent);
365 
366     dlg_trace_win(obj->obj.win);
367     do {
368 	ch = dlg_getc(obj->obj.win, &fkey);
369 #ifdef KEY_RESIZE
370 	if (fkey && ch == KEY_RESIZE) {
371 	    MY_OBJ *oldobj = obj;
372 
373 	    dlg_mouse_free_regions();
374 
375 	    obj = dlg_allocate_gauge(title,
376 				     cprompt,
377 				     height,
378 				     width,
379 				     oldobj->percent);
380 
381 	    /* avoid breaking new window in dlg_remove_callback */
382 	    oldobj->obj.caller = 0;
383 	    oldobj->obj.input = 0;
384 	    oldobj->obj.keep_win = FALSE;
385 
386 	    /* remove the old version of the gauge */
387 	    dlg_clear();
388 	    dlg_remove_callback(&(oldobj->obj));
389 	    refresh();
390 
391 	    dlg_add_callback_ref((DIALOG_CALLBACK **) & obj, my_cleanup);
392 	    dlg_update_gauge(obj, obj->percent);
393 	}
394 #endif
395     }
396     while (valid(obj) && handle_my_getc(&(obj->obj), ch, fkey, &result));
397 
398     dlg_free_gauge(obj);
399 
400     return (DLG_EXIT_OK);
401 }
402