OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [scripts/] [lxdialog/] [textbox.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *  textbox.c -- implements the text box
3
 *
4
 *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5
 *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
6
 *
7
 *  This program is free software; you can redistribute it and/or
8
 *  modify it under the terms of the GNU General Public License
9
 *  as published by the Free Software Foundation; either version 2
10
 *  of the License, or (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
 */
21
 
22
#include "dialog.h"
23
 
24
static void back_lines (int n);
25
static void print_page (WINDOW * win, int height, int width);
26
static void print_line (WINDOW * win, int row, int width);
27
static char *get_line (void);
28
static void print_position (WINDOW * win, int height, int width);
29
 
30
static int hscroll = 0, fd, file_size, bytes_read;
31
static int begin_reached = 1, end_reached = 0, page_length;
32
static char *buf, *page;
33
 
34
/*
35
 * Display text from a file in a dialog box.
36
 */
37
int
38
dialog_textbox (const char *title, const char *file, int height, int width)
39
{
40
    int i, x, y, cur_x, cur_y, fpos, key = 0;
41
    int passed_end;
42
    char search_term[MAX_LEN + 1];
43
    WINDOW *dialog, *text;
44
 
45
    search_term[0] = '\0';       /* no search term entered yet */
46
 
47
    /* Open input file for reading */
48
    if ((fd = open (file, O_RDONLY)) == -1) {
49
        endwin ();
50
        fprintf (stderr,
51
                 "\nCan't open input file in dialog_textbox().\n");
52
        exit (-1);
53
    }
54
    /* Get file size. Actually, 'file_size' is the real file size - 1,
55
       since it's only the last byte offset from the beginning */
56
    if ((file_size = lseek (fd, 0, SEEK_END)) == -1) {
57
        endwin ();
58
        fprintf (stderr, "\nError getting file size in dialog_textbox().\n");
59
        exit (-1);
60
    }
61
    /* Restore file pointer to beginning of file after getting file size */
62
    if (lseek (fd, 0, SEEK_SET) == -1) {
63
        endwin ();
64
        fprintf (stderr, "\nError moving file pointer in dialog_textbox().\n");
65
        exit (-1);
66
    }
67
    /* Allocate space for read buffer */
68
    if ((buf = malloc (BUF_SIZE + 1)) == NULL) {
69
        endwin ();
70
        fprintf (stderr, "\nCan't allocate memory in dialog_textbox().\n");
71
        exit (-1);
72
    }
73
    if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
74
        endwin ();
75
        fprintf (stderr, "\nError reading file in dialog_textbox().\n");
76
        exit (-1);
77
    }
78
    buf[bytes_read] = '\0';     /* mark end of valid data */
79
    page = buf;                 /* page is pointer to start of page to be displayed */
80
 
81
    /* center dialog box on screen */
82
    x = (COLS - width) / 2;
83
    y = (LINES - height) / 2;
84
 
85
 
86
    draw_shadow (stdscr, y, x, height, width);
87
 
88
    dialog = newwin (height, width, y, x);
89
    keypad (dialog, TRUE);
90
 
91
    /* Create window for text region, used for scrolling text */
92
    text = subwin (dialog, height - 4, width - 2, y + 1, x + 1);
93
    wattrset (text, dialog_attr);
94
    wbkgdset (text, dialog_attr & A_COLOR);
95
 
96
    keypad (text, TRUE);
97
 
98
    /* register the new window, along with its borders */
99
    draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
100
 
101
    wattrset (dialog, border_attr);
102
    mvwaddch (dialog, height-3, 0, ACS_LTEE);
103
    for (i = 0; i < width - 2; i++)
104
        waddch (dialog, ACS_HLINE);
105
    wattrset (dialog, dialog_attr);
106
    wbkgdset (dialog, dialog_attr & A_COLOR);
107
    waddch (dialog, ACS_RTEE);
108
 
109
    if (title != NULL) {
110
        wattrset (dialog, title_attr);
111
        mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
112
        waddstr (dialog, (char *)title);
113
        waddch (dialog, ' ');
114
    }
115
    print_button (dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
116
    wnoutrefresh (dialog);
117
    getyx (dialog, cur_y, cur_x);       /* Save cursor position */
118
 
119
    /* Print first page of text */
120
    attr_clear (text, height - 4, width - 2, dialog_attr);
121
    print_page (text, height - 4, width - 2);
122
    print_position (dialog, height, width);
123
    wmove (dialog, cur_y, cur_x);       /* Restore cursor position */
124
    wrefresh (dialog);
125
 
126
    while ((key != ESC) && (key != '\n')) {
127
        key = wgetch (dialog);
128
        switch (key) {
129
        case 'E':               /* Exit */
130
        case 'e':
131
        case 'X':
132
        case 'x':
133
            delwin (dialog);
134
            free (buf);
135
            close (fd);
136
            return 0;
137
        case 'g':               /* First page */
138
        case KEY_HOME:
139
            if (!begin_reached) {
140
                begin_reached = 1;
141
                /* First page not in buffer? */
142
                if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
143
                    endwin ();
144
                    fprintf (stderr,
145
                      "\nError moving file pointer in dialog_textbox().\n");
146
                    exit (-1);
147
                }
148
                if (fpos > bytes_read) {        /* Yes, we have to read it in */
149
                    if (lseek (fd, 0, SEEK_SET) == -1) {
150
                        endwin ();
151
                        fprintf (stderr, "\nError moving file pointer in "
152
                                 "dialog_textbox().\n");
153
                        exit (-1);
154
                    }
155
                    if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
156
                        endwin ();
157
                        fprintf (stderr,
158
                             "\nError reading file in dialog_textbox().\n");
159
                        exit (-1);
160
                    }
161
                    buf[bytes_read] = '\0';
162
                }
163
                page = buf;
164
                print_page (text, height - 4, width - 2);
165
                print_position (dialog, height, width);
166
                wmove (dialog, cur_y, cur_x);   /* Restore cursor position */
167
                wrefresh (dialog);
168
            }
169
            break;
170
        case 'G':               /* Last page */
171
        case KEY_END:
172
 
173
            end_reached = 1;
174
            /* Last page not in buffer? */
175
            if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
176
                endwin ();
177
                fprintf (stderr,
178
                      "\nError moving file pointer in dialog_textbox().\n");
179
                exit (-1);
180
            }
181
            if (fpos < file_size) {     /* Yes, we have to read it in */
182
                if (lseek (fd, -BUF_SIZE, SEEK_END) == -1) {
183
                    endwin ();
184
                    fprintf (stderr,
185
                      "\nError moving file pointer in dialog_textbox().\n");
186
                    exit (-1);
187
                }
188
                if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
189
                    endwin ();
190
                    fprintf (stderr,
191
                             "\nError reading file in dialog_textbox().\n");
192
                    exit (-1);
193
                }
194
                buf[bytes_read] = '\0';
195
            }
196
            page = buf + bytes_read;
197
            back_lines (height - 4);
198
            print_page (text, height - 4, width - 2);
199
            print_position (dialog, height, width);
200
            wmove (dialog, cur_y, cur_x);       /* Restore cursor position */
201
            wrefresh (dialog);
202
            break;
203
        case 'K':               /* Previous line */
204
        case 'k':
205
        case KEY_UP:
206
            if (!begin_reached) {
207
                back_lines (page_length + 1);
208
 
209
                /* We don't call print_page() here but use scrolling to ensure
210
                   faster screen update. However, 'end_reached' and
211
                   'page_length' should still be updated, and 'page' should
212
                   point to start of next page. This is done by calling
213
                   get_line() in the following 'for' loop. */
214
                scrollok (text, TRUE);
215
                wscrl (text, -1);       /* Scroll text region down one line */
216
                scrollok (text, FALSE);
217
                page_length = 0;
218
                passed_end = 0;
219
                for (i = 0; i < height - 4; i++) {
220
                    if (!i) {
221
                        /* print first line of page */
222
                        print_line (text, 0, width - 2);
223
                        wnoutrefresh (text);
224
                    } else
225
                        /* Called to update 'end_reached' and 'page' */
226
                        get_line ();
227
                    if (!passed_end)
228
                        page_length++;
229
                    if (end_reached && !passed_end)
230
                        passed_end = 1;
231
                }
232
 
233
                print_position (dialog, height, width);
234
                wmove (dialog, cur_y, cur_x);   /* Restore cursor position */
235
                wrefresh (dialog);
236
            }
237
            break;
238
        case 'B':               /* Previous page */
239
        case 'b':
240
        case KEY_PPAGE:
241
            if (begin_reached)
242
                break;
243
            back_lines (page_length + height - 4);
244
            print_page (text, height - 4, width - 2);
245
            print_position (dialog, height, width);
246
            wmove (dialog, cur_y, cur_x);
247
            wrefresh (dialog);
248
            break;
249
        case 'J':               /* Next line */
250
        case 'j':
251
        case KEY_DOWN:
252
            if (!end_reached) {
253
                begin_reached = 0;
254
                scrollok (text, TRUE);
255
                scroll (text);  /* Scroll text region up one line */
256
                scrollok (text, FALSE);
257
                print_line (text, height - 5, width - 2);
258
                wnoutrefresh (text);
259
                print_position (dialog, height, width);
260
                wmove (dialog, cur_y, cur_x);   /* Restore cursor position */
261
                wrefresh (dialog);
262
            }
263
            break;
264
        case KEY_NPAGE:         /* Next page */
265
        case ' ':
266
            if (end_reached)
267
                break;
268
 
269
            begin_reached = 0;
270
            print_page (text, height - 4, width - 2);
271
            print_position (dialog, height, width);
272
            wmove (dialog, cur_y, cur_x);
273
            wrefresh (dialog);
274
            break;
275
        case '0':               /* Beginning of line */
276
        case 'H':               /* Scroll left */
277
        case 'h':
278
        case KEY_LEFT:
279
            if (hscroll <= 0)
280
                break;
281
 
282
            if (key == '0')
283
                hscroll = 0;
284
            else
285
                hscroll--;
286
            /* Reprint current page to scroll horizontally */
287
            back_lines (page_length);
288
            print_page (text, height - 4, width - 2);
289
            wmove (dialog, cur_y, cur_x);
290
            wrefresh (dialog);
291
            break;
292
        case 'L':               /* Scroll right */
293
        case 'l':
294
        case KEY_RIGHT:
295
            if (hscroll >= MAX_LEN)
296
                break;
297
            hscroll++;
298
            /* Reprint current page to scroll horizontally */
299
            back_lines (page_length);
300
            print_page (text, height - 4, width - 2);
301
            wmove (dialog, cur_y, cur_x);
302
            wrefresh (dialog);
303
            break;
304
        case ESC:
305
            break;
306
        }
307
    }
308
 
309
    delwin (dialog);
310
    free (buf);
311
    close (fd);
312
    return -1;                  /* ESC pressed */
313
}
314
 
315
/*
316
 * Go back 'n' lines in text file. Called by dialog_textbox().
317
 * 'page' will be updated to point to the desired line in 'buf'.
318
 */
319
static void
320
back_lines (int n)
321
{
322
    int i, fpos;
323
 
324
    begin_reached = 0;
325
    /* We have to distinguish between end_reached and !end_reached
326
       since at end of file, the line is not ended by a '\n'.
327
       The code inside 'if' basically does a '--page' to move one
328
       character backward so as to skip '\n' of the previous line */
329
    if (!end_reached) {
330
        /* Either beginning of buffer or beginning of file reached? */
331
        if (page == buf) {
332
            if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
333
                endwin ();
334
                fprintf (stderr, "\nError moving file pointer in "
335
                         "back_lines().\n");
336
                exit (-1);
337
            }
338
            if (fpos > bytes_read) {    /* Not beginning of file yet */
339
                /* We've reached beginning of buffer, but not beginning of
340
                   file yet, so read previous part of file into buffer.
341
                   Note that we only move backward for BUF_SIZE/2 bytes,
342
                   but not BUF_SIZE bytes to avoid re-reading again in
343
                   print_page() later */
344
                /* Really possible to move backward BUF_SIZE/2 bytes? */
345
                if (fpos < BUF_SIZE / 2 + bytes_read) {
346
                    /* No, move less then */
347
                    if (lseek (fd, 0, SEEK_SET) == -1) {
348
                        endwin ();
349
                        fprintf (stderr, "\nError moving file pointer in "
350
                                 "back_lines().\n");
351
                        exit (-1);
352
                    }
353
                    page = buf + fpos - bytes_read;
354
                } else {        /* Move backward BUF_SIZE/2 bytes */
355
                    if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR)
356
                        == -1) {
357
                        endwin ();
358
                        fprintf (stderr, "\nError moving file pointer "
359
                                 "in back_lines().\n");
360
                        exit (-1);
361
                    }
362
                    page = buf + BUF_SIZE / 2;
363
                }
364
                if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
365
                    endwin ();
366
                    fprintf (stderr, "\nError reading file in back_lines().\n");
367
                    exit (-1);
368
                }
369
                buf[bytes_read] = '\0';
370
            } else {            /* Beginning of file reached */
371
                begin_reached = 1;
372
                return;
373
            }
374
        }
375
        if (*(--page) != '\n') {        /* '--page' here */
376
            /* Something's wrong... */
377
            endwin ();
378
            fprintf (stderr, "\nInternal error in back_lines().\n");
379
            exit (-1);
380
        }
381
    }
382
    /* Go back 'n' lines */
383
    for (i = 0; i < n; i++)
384
        do {
385
            if (page == buf) {
386
                if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
387
                    endwin ();
388
                    fprintf (stderr,
389
                          "\nError moving file pointer in back_lines().\n");
390
                    exit (-1);
391
                }
392
                if (fpos > bytes_read) {
393
                    /* Really possible to move backward BUF_SIZE/2 bytes? */
394
                    if (fpos < BUF_SIZE / 2 + bytes_read) {
395
                        /* No, move less then */
396
                        if (lseek (fd, 0, SEEK_SET) == -1) {
397
                            endwin ();
398
                            fprintf (stderr, "\nError moving file pointer "
399
                                     "in back_lines().\n");
400
                            exit (-1);
401
                        }
402
                        page = buf + fpos - bytes_read;
403
                    } else {    /* Move backward BUF_SIZE/2 bytes */
404
                        if (lseek (fd, -(BUF_SIZE / 2 + bytes_read),
405
                                   SEEK_CUR) == -1) {
406
                            endwin ();
407
                            fprintf (stderr, "\nError moving file pointer"
408
                                     " in back_lines().\n");
409
                            exit (-1);
410
                        }
411
                        page = buf + BUF_SIZE / 2;
412
                    }
413
                    if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
414
                        endwin ();
415
                        fprintf (stderr, "\nError reading file in "
416
                                 "back_lines().\n");
417
                        exit (-1);
418
                    }
419
                    buf[bytes_read] = '\0';
420
                } else {        /* Beginning of file reached */
421
                    begin_reached = 1;
422
                    return;
423
                }
424
            }
425
        } while (*(--page) != '\n');
426
    page++;
427
}
428
 
429
/*
430
 * Print a new page of text. Called by dialog_textbox().
431
 */
432
static void
433
print_page (WINDOW * win, int height, int width)
434
{
435
    int i, passed_end = 0;
436
 
437
    page_length = 0;
438
    for (i = 0; i < height; i++) {
439
        print_line (win, i, width);
440
        if (!passed_end)
441
            page_length++;
442
        if (end_reached && !passed_end)
443
            passed_end = 1;
444
    }
445
    wnoutrefresh (win);
446
}
447
 
448
/*
449
 * Print a new line of text. Called by dialog_textbox() and print_page().
450
 */
451
static void
452
print_line (WINDOW * win, int row, int width)
453
{
454
    int i, y, x;
455
    char *line;
456
 
457
    line = get_line ();
458
    line += MIN (strlen (line), hscroll);       /* Scroll horizontally */
459
    wmove (win, row, 0); /* move cursor to correct line */
460
    waddch (win, ' ');
461
    waddnstr (win, line, MIN (strlen (line), width - 2));
462
 
463
    getyx (win, y, x);
464
    /* Clear 'residue' of previous line */
465
#if OLD_NCURSES
466
    for (i = 0; i < width - x; i++)
467
        waddch (win, ' ');
468
#else
469
    wclrtoeol(win);
470
#endif
471
}
472
 
473
/*
474
 * Return current line of text. Called by dialog_textbox() and print_line().
475
 * 'page' should point to start of current line before calling, and will be
476
 * updated to point to start of next line.
477
 */
478
static char *
479
get_line (void)
480
{
481
    int i = 0, fpos;
482
    static char line[MAX_LEN + 1];
483
 
484
    end_reached = 0;
485
    while (*page != '\n') {
486
        if (*page == '\0') {
487
            /* Either end of file or end of buffer reached */
488
            if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
489
                endwin ();
490
                fprintf (stderr, "\nError moving file pointer in "
491
                         "get_line().\n");
492
                exit (-1);
493
            }
494
            if (fpos < file_size) {     /* Not end of file yet */
495
                /* We've reached end of buffer, but not end of file yet,
496
                   so read next part of file into buffer */
497
                if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
498
                    endwin ();
499
                    fprintf (stderr, "\nError reading file in get_line().\n");
500
                    exit (-1);
501
                }
502
                buf[bytes_read] = '\0';
503
                page = buf;
504
            } else {
505
                if (!end_reached)
506
                    end_reached = 1;
507
                break;
508
            }
509
        } else if (i < MAX_LEN)
510
            line[i++] = *(page++);
511
        else {
512
            /* Truncate lines longer than MAX_LEN characters */
513
            if (i == MAX_LEN)
514
                line[i++] = '\0';
515
            page++;
516
        }
517
    }
518
    if (i <= MAX_LEN)
519
        line[i] = '\0';
520
    if (!end_reached)
521
        page++;                 /* move pass '\n' */
522
 
523
    return line;
524
}
525
 
526
/*
527
 * Print current position
528
 */
529
static void
530
print_position (WINDOW * win, int height, int width)
531
{
532
    int fpos, percent;
533
 
534
    if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
535
        endwin ();
536
        fprintf (stderr, "\nError moving file pointer in print_position().\n");
537
        exit (-1);
538
    }
539
    wattrset (win, position_indicator_attr);
540
    wbkgdset (win, position_indicator_attr & A_COLOR);
541
    percent = !file_size ?
542
        100 : ((fpos - bytes_read + page - buf) * 100) / file_size;
543
    wmove (win, height - 3, width - 9);
544
    wprintw (win, "(%3d%%)", percent);
545
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.