tailbox.c revision 224014
1178525Sjb/* 2178525Sjb * $Id: tailbox.c,v 1.63 2011/06/27 08:19:43 tom Exp $ 3178525Sjb * 4178525Sjb * tailbox.c -- implements the tail box 5178525Sjb * 6178525Sjb * Copyright 2000-2010,2011 Thomas E. Dickey 7178525Sjb * 8178525Sjb * This program is free software; you can redistribute it and/or modify 9178525Sjb * it under the terms of the GNU Lesser General Public License, version 2.1 10178525Sjb * as published by the Free Software Foundation. 11178525Sjb * 12178525Sjb * This program is distributed in the hope that it will be useful, but 13178525Sjb * WITHOUT ANY WARRANTY; without even the implied warranty of 14178525Sjb * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15178525Sjb * Lesser General Public License for more details. 16178525Sjb * 17178525Sjb * You should have received a copy of the GNU Lesser General Public 18178525Sjb * License along with this program; if not, write to 19178525Sjb * Free Software Foundation, Inc. 20178525Sjb * 51 Franklin St., Fifth Floor 21178525Sjb * Boston, MA 02110, USA. 22210767Srpaulo * 23178525Sjb * An earlier version of this program lists as authors 24178525Sjb * Pasquale De Marco (demarco_p@abramo.it) 25178525Sjb */ 26178525Sjb 27269845Sdelphij#include <dialog.h> 28269845Sdelphij#include <dlg_keys.h> 29269845Sdelphij#include <sys/stat.h> 30269845Sdelphij 31178525Sjbtypedef struct { 32178525Sjb DIALOG_CALLBACK obj; 33178525Sjb WINDOW *text; 34178525Sjb const char **buttons; 35178525Sjb int hscroll; 36178525Sjb int old_hscroll; 37178525Sjb char line[MAX_LEN + 1]; 38178525Sjb off_t last_pos; 39178525Sjb} MY_OBJ; 40178525Sjb 41178525Sjb/* 42178525Sjb * Return current line of text. 43178525Sjb */ 44269845Sdelphijstatic char * 45178525Sjbget_line(MY_OBJ * obj) 46178525Sjb{ 47178525Sjb FILE *fp = obj->obj.input; 48178525Sjb int col = -(obj->hscroll); 49178525Sjb int j, tmpint, ch; 50178525Sjb 51178525Sjb do { 52269845Sdelphij if (((ch = getc(fp)) == EOF) && !feof(fp)) 53178525Sjb dlg_exiterr("Error moving file pointer in get_line()."); 54178525Sjb else if (!feof(fp) && (ch != '\n')) { 55178525Sjb if ((ch == TAB) && (dialog_vars.tab_correct)) { 56178525Sjb tmpint = dialog_state.tab_len 57178525Sjb - ((col + obj->hscroll) % dialog_state.tab_len); 58178525Sjb for (j = 0; j < tmpint; j++) { 59178525Sjb if (col >= 0 && col < MAX_LEN) 60178525Sjb obj->line[col] = ' '; 61178525Sjb ++col; 62178525Sjb } 63178525Sjb } else { 64178525Sjb if (col >= 0) 65178525Sjb obj->line[col] = (char) ch; 66178525Sjb ++col; 67178525Sjb } 68178525Sjb if (col >= MAX_LEN) 69178525Sjb break; 70178525Sjb } 71178525Sjb } while (!feof(fp) && (ch != '\n')); 72178525Sjb 73178525Sjb if (col < 0) 74178525Sjb col = 0; 75178525Sjb obj->line[col] = '\0'; 76178525Sjb 77178525Sjb return obj->line; 78178525Sjb} 79178525Sjb 80178525Sjb/* 81178525Sjb * Print a new line of text. 82178525Sjb */ 83178525Sjbstatic void 84178525Sjbprint_line(MY_OBJ * obj, WINDOW *win, int row, int width) 85178525Sjb{ 86178525Sjb int i, y, x; 87178525Sjb char *line = get_line(obj); 88178525Sjb 89178525Sjb (void) wmove(win, row, 0); /* move cursor to correct line */ 90178525Sjb (void) waddch(win, ' '); 91178525Sjb#ifdef NCURSES_VERSION 92269845Sdelphij (void) waddnstr(win, line, MIN((int) strlen(line), width - 2)); 93269845Sdelphij#else 94269845Sdelphij line[MIN((int) strlen(line), width - 2)] = '\0'; 95269845Sdelphij waddstr(win, line); 96269845Sdelphij#endif 97269845Sdelphij 98178525Sjb getyx(win, y, x); 99178525Sjb /* Clear 'residue' of previous line */ 100178525Sjb for (i = 0; i < width - x; i++) 101178525Sjb (void) waddch(win, ' '); 102178525Sjb} 103178525Sjb 104178525Sjb/* 105178525Sjb * Go back 'target' lines in text file. BUFSIZ has to be in 'size_t' range. 106178525Sjb */ 107269845Sdelphijstatic void 108178525Sjblast_lines(MY_OBJ * obj, int target) 109178525Sjb{ 110178525Sjb FILE *fp = obj->obj.input; 111178525Sjb size_t inx; 112178525Sjb int count = 0; 113178525Sjb char buf[BUFSIZ + 1]; 114178525Sjb size_t size_to_read; 115178525Sjb size_t size_as_read; 116178525Sjb long offset = 0; 117178525Sjb long fpos = 0; 118178525Sjb 119178525Sjb if (fseek(fp, 0L, SEEK_END) == -1 120178525Sjb || (fpos = ftell(fp)) < 0) 121178525Sjb dlg_exiterr("Error moving file pointer in last_lines()."); 122178525Sjb 123178525Sjb if (fpos != 0) { 124178525Sjb ++target; 125178525Sjb for (;;) { 126178525Sjb if (fpos >= BUFSIZ) { 127269845Sdelphij size_to_read = BUFSIZ; 128269845Sdelphij } else { 129178525Sjb size_to_read = (size_t) fpos; 130178525Sjb } 131178525Sjb fpos = fpos - (long) size_to_read; 132178525Sjb if (fseek(fp, fpos, SEEK_SET) == -1) 133178525Sjb dlg_exiterr("Error moving file pointer in last_lines()."); 134178525Sjb size_as_read = fread(buf, sizeof(char), size_to_read, fp); 135178525Sjb if (ferror(fp)) 136178525Sjb dlg_exiterr("Error reading file in last_lines()."); 137178525Sjb 138178525Sjb if (size_as_read == 0) { 139178525Sjb fpos = 0; 140178525Sjb offset = 0; 141178525Sjb break; 142178525Sjb } 143178525Sjb 144178525Sjb offset += (long) size_as_read; 145178525Sjb for (inx = size_as_read - 1; inx != 0; --inx) { 146178525Sjb if (buf[inx] == '\n') { 147178525Sjb if (++count > target) 148178525Sjb break; 149178525Sjb offset = (long) (inx + 1); 150178525Sjb } 151178525Sjb } 152178525Sjb 153178525Sjb if (count > target) { 154178525Sjb break; 155178525Sjb } else if (fpos == 0) { 156178525Sjb offset = 0; 157178525Sjb break; 158178525Sjb } 159178525Sjb } 160178525Sjb 161178525Sjb if (fseek(fp, fpos + offset, SEEK_SET) == -1) 162178525Sjb dlg_exiterr("Error moving file pointer in last_lines()."); 163178525Sjb } 164178525Sjb} 165178525Sjb 166178525Sjb/* 167178525Sjb * Print a new page of text. 168178525Sjb */ 169178525Sjbstatic void 170178525Sjbprint_page(MY_OBJ * obj, int height, int width) 171178525Sjb{ 172178525Sjb int i; 173178525Sjb 174178525Sjb for (i = 0; i < height; i++) { 175178525Sjb print_line(obj, obj->text, i, width); 176178525Sjb } 177178525Sjb (void) wnoutrefresh(obj->text); 178178525Sjb} 179178525Sjb 180178525Sjbstatic void 181178525Sjbprint_last_page(MY_OBJ * obj) 182178525Sjb{ 183178525Sjb int high = getmaxy(obj->obj.win) - (2 * MARGIN + (obj->obj.bg_task ? 1 : 3)); 184178525Sjb int wide = getmaxx(obj->text); 185178525Sjb 186178525Sjb last_lines(obj, high); 187178525Sjb print_page(obj, high, wide); 188178525Sjb} 189178525Sjb 190178525Sjbstatic void 191178525Sjbrepaint_text(MY_OBJ * obj) 192178525Sjb{ 193178525Sjb FILE *fp = obj->obj.input; 194178525Sjb int cur_y, cur_x; 195178525Sjb 196178525Sjb getyx(obj->obj.win, cur_y, cur_x); 197178525Sjb obj->old_hscroll = obj->hscroll; 198178525Sjb 199178525Sjb print_last_page(obj); 200178525Sjb obj->last_pos = ftell(fp); 201178525Sjb 202178525Sjb (void) wmove(obj->obj.win, cur_y, cur_x); /* Restore cursor position */ 203178525Sjb wrefresh(obj->obj.win); 204178525Sjb} 205178525Sjb 206178525Sjbstatic bool 207178525Sjbhandle_input(DIALOG_CALLBACK * cb) 208178525Sjb{ 209178525Sjb MY_OBJ *obj = (MY_OBJ *) cb; 210178525Sjb FILE *fp = obj->obj.input; 211178525Sjb struct stat sb; 212178525Sjb 213178525Sjb if (fstat(fileno(fp), &sb) == 0 214178525Sjb && sb.st_size != obj->last_pos) { 215178525Sjb repaint_text(obj); 216178525Sjb } 217178525Sjb 218178525Sjb return TRUE; 219178525Sjb} 220178525Sjb 221178525Sjbstatic bool 222178525Sjbhandle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result) 223178525Sjb{ 224178525Sjb MY_OBJ *obj = (MY_OBJ *) cb; 225178525Sjb bool done = FALSE; 226178525Sjb 227178525Sjb if (!fkey && dlg_char_to_button(ch, obj->buttons) == 0) { 228178525Sjb ch = DLGK_ENTER; 229178525Sjb fkey = TRUE; 230178525Sjb } 231178525Sjb 232178525Sjb if (fkey) { 233178525Sjb switch (ch) { 234178525Sjb case DLGK_ENTER: 235178525Sjb *result = DLG_EXIT_OK; 236178525Sjb done = TRUE; 237178525Sjb break; 238178525Sjb case DLGK_BEGIN: /* Beginning of line */ 239178525Sjb obj->hscroll = 0; 240178525Sjb break; 241178525Sjb case DLGK_GRID_LEFT: /* Scroll left */ 242178525Sjb if (obj->hscroll > 0) { 243178525Sjb obj->hscroll -= 1; 244178525Sjb } 245178525Sjb break; 246178525Sjb case DLGK_GRID_RIGHT: /* Scroll right */ 247178525Sjb if (obj->hscroll < MAX_LEN) 248178525Sjb obj->hscroll += 1; 249178525Sjb break; 250178525Sjb default: 251178525Sjb beep(); 252178525Sjb break; 253178525Sjb } 254210767Srpaulo if ((obj->hscroll != obj->old_hscroll)) 255178525Sjb repaint_text(obj); 256178525Sjb } else { 257178525Sjb switch (ch) { 258178525Sjb case ERR: 259178525Sjb clearerr(cb->input); 260178525Sjb ch = getc(cb->input); 261178525Sjb (void) ungetc(ch, cb->input); 262178525Sjb if (ch != EOF) { 263178525Sjb handle_input(cb); 264178525Sjb } 265178525Sjb break; 266178525Sjb case ESC: 267178525Sjb done = TRUE; 268178525Sjb *result = DLG_EXIT_ESC; 269178525Sjb break; 270178525Sjb default: 271178525Sjb beep(); 272178525Sjb break; 273178525Sjb } 274178525Sjb } 275178525Sjb 276178525Sjb return !done; 277178525Sjb} 278178525Sjb 279178525Sjb/* 280178525Sjb * Display text from a file in a dialog box, like in a "tail -f". 281178525Sjb */ 282178525Sjbint 283178525Sjbdialog_tailbox(const char *title, const char *file, int height, int width, int bg_task) 284178525Sjb{ 285178525Sjb /* *INDENT-OFF* */ 286178525Sjb static DLG_KEYS_BINDING binding[] = { 287178525Sjb HELPKEY_BINDINGS, 288178525Sjb ENTERKEY_BINDINGS, 289178525Sjb DLG_KEYS_DATA( DLGK_BEGIN, '0' ), 290178525Sjb DLG_KEYS_DATA( DLGK_BEGIN, KEY_BEG ), 291178525Sjb DLG_KEYS_DATA( DLGK_GRID_LEFT, 'H' ), 292178525Sjb DLG_KEYS_DATA( DLGK_GRID_LEFT, 'h' ), 293178525Sjb DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ), 294178525Sjb DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ), 295178525Sjb DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ), 296178525Sjb DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ), 297178525Sjb END_KEYS_BINDING 298178525Sjb }; 299178525Sjb /* *INDENT-ON* */ 300178525Sjb 301178525Sjb#ifdef KEY_RESIZE 302178525Sjb int old_height = height; 303178525Sjb int old_width = width; 304178525Sjb#endif 305178525Sjb int fkey; 306178525Sjb int x, y, result, thigh; 307178525Sjb WINDOW *dialog, *text; 308178525Sjb const char **buttons = 0; 309178525Sjb MY_OBJ *obj; 310178525Sjb FILE *fd; 311178525Sjb int min_width = 12; 312178525Sjb 313178525Sjb /* Open input file for reading */ 314178525Sjb if ((fd = fopen(file, "rb")) == NULL) 315178525Sjb dlg_exiterr("Can't open input file in dialog_tailbox()."); 316178525Sjb 317178525Sjb#ifdef KEY_RESIZE 318178525Sjb retry: 319178525Sjb#endif 320178525Sjb dlg_auto_sizefile(title, file, &height, &width, 2, min_width); 321178525Sjb dlg_print_size(height, width); 322178525Sjb dlg_ctl_size(height, width); 323178525Sjb 324178525Sjb x = dlg_box_x_ordinate(width); 325178525Sjb y = dlg_box_y_ordinate(height); 326178525Sjb thigh = height - ((2 * MARGIN) + (bg_task ? 0 : 2)); 327178525Sjb 328178525Sjb dialog = dlg_new_window(height, width, y, x); 329178525Sjb 330178525Sjb dlg_mouse_setbase(x, y); 331178525Sjb 332178525Sjb /* Create window for text region, used for scrolling text */ 333178525Sjb text = dlg_sub_window(dialog, 334178525Sjb thigh, 335178525Sjb width - (2 * MARGIN), 336178525Sjb y + MARGIN, 337178525Sjb x + MARGIN); 338178525Sjb 339178525Sjb dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 340178525Sjb dlg_draw_bottom_box(dialog); 341178525Sjb dlg_draw_title(dialog, title); 342178525Sjb dlg_draw_helpline(dialog, FALSE); 343178525Sjb 344178525Sjb if (!bg_task) { 345178525Sjb buttons = dlg_exit_label(); 346178525Sjb dlg_button_layout(buttons, &min_width); 347178525Sjb dlg_draw_buttons(dialog, height - (2 * MARGIN), 0, buttons, FALSE, 348178525Sjb FALSE, width); 349178525Sjb } 350178525Sjb 351178525Sjb (void) wmove(dialog, thigh, (MARGIN + 1)); 352178525Sjb (void) wnoutrefresh(dialog); 353178525Sjb 354178525Sjb obj = dlg_calloc(MY_OBJ, 1); 355178525Sjb assert_ptr(obj, "dialog_tailbox"); 356178525Sjb 357178525Sjb obj->obj.input = fd; 358178525Sjb obj->obj.win = dialog; 359178525Sjb obj->obj.handle_getc = handle_my_getc; 360178525Sjb obj->obj.handle_input = bg_task ? handle_input : 0; 361178525Sjb obj->obj.keep_bg = bg_task && dialog_vars.cant_kill; 362178525Sjb obj->obj.bg_task = bg_task; 363178525Sjb obj->text = text; 364178525Sjb obj->buttons = buttons; 365178525Sjb dlg_add_callback(&(obj->obj)); 366178525Sjb 367178525Sjb dlg_register_window(dialog, "tailbox", binding); 368178525Sjb dlg_register_buttons(dialog, "tailbox", buttons); 369178525Sjb 370178525Sjb /* Print last page of text */ 371178525Sjb dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr); 372178525Sjb repaint_text(obj); 373178525Sjb 374178525Sjb if (bg_task) { 375178525Sjb result = DLG_EXIT_OK; 376178525Sjb } else { 377178525Sjb int ch; 378178525Sjb do { 379178525Sjb ch = dlg_getc(dialog, &fkey); 380178525Sjb#ifdef KEY_RESIZE 381178525Sjb if (fkey && ch == KEY_RESIZE) { 382178525Sjb /* reset data */ 383178525Sjb height = old_height; 384178525Sjb width = old_width; 385178525Sjb /* repaint */ 386178525Sjb dlg_clear(); 387178525Sjb dlg_del_window(dialog); 388178525Sjb refresh(); 389178525Sjb dlg_mouse_free_regions(); 390178525Sjb dlg_button_layout(buttons, &min_width); 391178525Sjb goto retry; 392178525Sjb } 393178525Sjb#endif 394178525Sjb } 395178525Sjb while (handle_my_getc(&(obj->obj), ch, fkey, &result)); 396178525Sjb } 397178525Sjb dlg_mouse_free_regions(); 398178525Sjb return result; 399178525Sjb} 400178525Sjb