1 /* 2 * $Id: tailbox.c,v 1.56 2010/04/28 20:52:20 tom Exp $ 3 * 4 * tailbox.c -- implements the tail box 5 * 6 * Copyright 2000-2009,2010 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 * Pasquale De Marco (demarco_p@abramo.it) 25 */ 26 27 #include <dialog.h> 28 #include <dlg_keys.h> 29 30 typedef struct { 31 DIALOG_CALLBACK obj; 32 WINDOW *text; 33 const char **buttons; 34 int hscroll; 35 int old_hscroll; 36 char line[MAX_LEN + 1]; 37 } MY_OBJ; 38 39 /* 40 * Return current line of text. 41 */ 42 static char * 43 get_line(MY_OBJ * obj) 44 { 45 FILE *fp = obj->obj.input; 46 int col = -(obj->hscroll); 47 int j, tmpint, ch; 48 49 do { 50 if (((ch = getc(fp)) == EOF) && !feof(fp)) 51 dlg_exiterr("Error moving file pointer in get_line()."); 52 else if (!feof(fp) && (ch != '\n')) { 53 if ((ch == TAB) && (dialog_vars.tab_correct)) { 54 tmpint = dialog_state.tab_len 55 - ((col + obj->hscroll) % dialog_state.tab_len); 56 for (j = 0; j < tmpint; j++) { 57 if (col >= 0 && col < MAX_LEN) 58 obj->line[col] = ' '; 59 ++col; 60 } 61 } else { 62 if (col >= 0) 63 obj->line[col] = (char) ch; 64 ++col; 65 } 66 if (col >= MAX_LEN) 67 break; 68 } 69 } while (!feof(fp) && (ch != '\n')); 70 71 if (col < 0) 72 col = 0; 73 obj->line[col] = '\0'; 74 75 return obj->line; 76 } 77 78 /* 79 * Print a new line of text. 80 */ 81 static void 82 print_line(MY_OBJ * obj, WINDOW *win, int row, int width) 83 { 84 int i, y, x; 85 char *line = get_line(obj); 86 87 (void) wmove(win, row, 0); /* move cursor to correct line */ 88 (void) waddch(win, ' '); 89 #ifdef NCURSES_VERSION 90 (void) waddnstr(win, line, MIN((int) strlen(line), width - 2)); 91 #else 92 line[MIN((int) strlen(line), width - 2)] = '\0'; 93 waddstr(win, line); 94 #endif 95 96 getyx(win, y, x); 97 /* Clear 'residue' of previous line */ 98 for (i = 0; i < width - x; i++) 99 (void) waddch(win, ' '); 100 } 101 102 /* 103 * Go back 'target' lines in text file. BUFSIZ has to be in 'size_t' range. 104 */ 105 static void 106 last_lines(MY_OBJ * obj, int target) 107 { 108 FILE *fp = obj->obj.input; 109 long inx; 110 int count = 0; 111 char buf[BUFSIZ + 1]; 112 long size_to_read; 113 long offset = 0; 114 long fpos = 0; 115 116 if (fseek(fp, 0, SEEK_END) == -1 117 || (fpos = ftell(fp)) < 0) 118 dlg_exiterr("Error moving file pointer in last_lines()."); 119 120 if (fpos != 0) { 121 ++target; 122 for (;;) { 123 if (fpos >= BUFSIZ) { 124 size_to_read = BUFSIZ; 125 } else { 126 size_to_read = fpos; 127 } 128 fpos = fpos - size_to_read; 129 if (fseek(fp, fpos, SEEK_SET) == -1) 130 dlg_exiterr("Error moving file pointer in last_lines()."); 131 (void) fread(buf, (size_t) size_to_read, 1, fp); 132 if (ferror(fp)) 133 dlg_exiterr("Error reading file in last_lines()."); 134 135 offset += size_to_read; 136 for (inx = size_to_read - 1; inx >= 0; --inx) { 137 if (buf[inx] == '\n') { 138 if (++count > target) 139 break; 140 offset = inx + 1; 141 } 142 } 143 144 if (count > target) { 145 break; 146 } else if (fpos == 0) { 147 offset = 0; 148 break; 149 } 150 } 151 152 if (fseek(fp, fpos + offset, SEEK_SET) == -1) 153 dlg_exiterr("Error moving file pointer in last_lines()."); 154 } 155 } 156 157 /* 158 * Print a new page of text. 159 */ 160 static void 161 print_page(MY_OBJ * obj, int height, int width) 162 { 163 int i; 164 165 for (i = 0; i < height; i++) { 166 print_line(obj, obj->text, i, width); 167 } 168 (void) wnoutrefresh(obj->text); 169 } 170 171 static void 172 print_last_page(MY_OBJ * obj) 173 { 174 int high = getmaxy(obj->obj.win) - (2 * MARGIN + (obj->obj.bg_task ? 1 : 3)); 175 int wide = getmaxx(obj->text); 176 177 last_lines(obj, high); 178 print_page(obj, high, wide); 179 } 180 181 static void 182 repaint_text(MY_OBJ * obj) 183 { 184 int cur_y, cur_x; 185 186 getyx(obj->obj.win, cur_y, cur_x); 187 obj->old_hscroll = obj->hscroll; 188 print_last_page(obj); 189 (void) wmove(obj->obj.win, cur_y, cur_x); /* Restore cursor position */ 190 wrefresh(obj->obj.win); 191 } 192 193 static bool 194 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result) 195 { 196 MY_OBJ *obj = (MY_OBJ *) cb; 197 bool done = FALSE; 198 199 if (!fkey && dlg_char_to_button(ch, obj->buttons) == 0) { 200 ch = DLGK_ENTER; 201 fkey = TRUE; 202 } 203 204 if (fkey) { 205 switch (ch) { 206 case DLGK_ENTER: 207 *result = DLG_EXIT_OK; 208 done = TRUE; 209 break; 210 case DLGK_BEGIN: /* Beginning of line */ 211 obj->hscroll = 0; 212 break; 213 case DLGK_GRID_LEFT: /* Scroll left */ 214 if (obj->hscroll > 0) { 215 obj->hscroll -= 1; 216 } 217 break; 218 case DLGK_GRID_RIGHT: /* Scroll right */ 219 if (obj->hscroll < MAX_LEN) 220 obj->hscroll += 1; 221 break; 222 default: 223 beep(); 224 break; 225 } 226 if ((obj->hscroll != obj->old_hscroll)) 227 repaint_text(obj); 228 } else { 229 switch (ch) { 230 case ERR: 231 clearerr(cb->input); 232 ch = getc(cb->input); 233 (void) ungetc(ch, cb->input); 234 if ((ch != EOF) || (obj->hscroll != obj->old_hscroll)) { 235 repaint_text(obj); 236 } 237 break; 238 case ESC: 239 done = TRUE; 240 *result = DLG_EXIT_ESC; 241 break; 242 default: 243 beep(); 244 break; 245 } 246 } 247 248 return !done; 249 } 250 251 /* 252 * Display text from a file in a dialog box, like in a "tail -f". 253 */ 254 int 255 dialog_tailbox(const char *title, const char *file, int height, int width, int bg_task) 256 { 257 /* *INDENT-OFF* */ 258 static DLG_KEYS_BINDING binding[] = { 259 ENTERKEY_BINDINGS, 260 DLG_KEYS_DATA( DLGK_BEGIN, '0' ), 261 DLG_KEYS_DATA( DLGK_BEGIN, KEY_BEG ), 262 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'H' ), 263 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'h' ), 264 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ), 265 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ), 266 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ), 267 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ), 268 END_KEYS_BINDING 269 }; 270 /* *INDENT-ON* */ 271 272 #ifdef KEY_RESIZE 273 int old_height = height; 274 int old_width = width; 275 #endif 276 int fkey; 277 int x, y, result, thigh; 278 WINDOW *dialog, *text; 279 const char **buttons = 0; 280 MY_OBJ *obj; 281 FILE *fd; 282 int min_width = 12; 283 284 /* Open input file for reading */ 285 if ((fd = fopen(file, "rb")) == NULL) 286 dlg_exiterr("Can't open input file in dialog_tailbox()."); 287 288 #ifdef KEY_RESIZE 289 retry: 290 #endif 291 dlg_auto_sizefile(title, file, &height, &width, 2, min_width); 292 dlg_print_size(height, width); 293 dlg_ctl_size(height, width); 294 295 x = dlg_box_x_ordinate(width); 296 y = dlg_box_y_ordinate(height); 297 thigh = height - ((2 * MARGIN) + (bg_task ? 0 : 2)); 298 299 dialog = dlg_new_window(height, width, y, x); 300 301 dlg_mouse_setbase(x, y); 302 303 /* Create window for text region, used for scrolling text */ 304 text = dlg_sub_window(dialog, 305 thigh, 306 width - (2 * MARGIN), 307 y + MARGIN, 308 x + MARGIN); 309 310 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 311 dlg_draw_bottom_box(dialog); 312 dlg_draw_title(dialog, title); 313 314 if (!bg_task) { 315 buttons = dlg_exit_label(); 316 dlg_button_layout(buttons, &min_width); 317 dlg_draw_buttons(dialog, height - (2 * MARGIN), 0, buttons, FALSE, 318 FALSE, width); 319 } 320 321 (void) wmove(dialog, thigh, (MARGIN + 1)); 322 (void) wnoutrefresh(dialog); 323 324 obj = dlg_calloc(MY_OBJ, 1); 325 assert_ptr(obj, "dialog_tailbox"); 326 327 obj->obj.input = fd; 328 obj->obj.win = dialog; 329 obj->obj.handle_getc = handle_my_getc; 330 obj->obj.keep_bg = bg_task && dialog_vars.cant_kill; 331 obj->obj.bg_task = bg_task; 332 obj->text = text; 333 obj->buttons = buttons; 334 dlg_add_callback(&(obj->obj)); 335 336 dlg_register_window(dialog, "tailbox", binding); 337 dlg_register_buttons(dialog, "tailbox", buttons); 338 339 /* Print last page of text */ 340 dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr); 341 repaint_text(obj); 342 343 if (bg_task) { 344 result = DLG_EXIT_OK; 345 } else { 346 int ch; 347 do { 348 ch = dlg_getc(dialog, &fkey); 349 #ifdef KEY_RESIZE 350 if (fkey && ch == KEY_RESIZE) { 351 /* reset data */ 352 height = old_height; 353 width = old_width; 354 /* repaint */ 355 dlg_clear(); 356 dlg_del_window(dialog); 357 refresh(); 358 dlg_mouse_free_regions(); 359 dlg_button_layout(buttons, &min_width); 360 goto retry; 361 } 362 #endif 363 } 364 while (handle_my_getc(&(obj->obj), ch, fkey, &result)); 365 } 366 dlg_mouse_free_regions(); 367 return result; 368 } 369