summaryrefslogtreecommitdiffstats
path: root/gnu/lib/libdialog/textbox.c
blob: a26247b6aac845d365ef1d6f9213a8ce5220162f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
 *  textbox.c -- implements the text box
 *
 *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <dialog.h>
#include "dialog.priv.h"


static void back_lines(int n);
static void print_page(WINDOW *win, int height, int width);
static void print_line(WINDOW *win, int row, int width);
static unsigned char *get_line(void);
static int get_search_term(WINDOW *win, unsigned char *search_term, int height, int width);
static void print_position(WINDOW *win, int height, int width);


static int hscroll = 0, fd, file_size, bytes_read, begin_reached = 1,
           end_reached = 0, page_length;
static unsigned char *buf, *page;


/*
 * Display text from a file in a dialog box.
 */
int dialog_textbox(unsigned char *title, unsigned char *file, int height, int width)
{
  int i, x, y, cur_x, cur_y, fpos, key = 0, dir, temp, temp1;
#ifdef HAVE_NCURSES
  int passed_end;
#endif
  unsigned char search_term[MAX_LEN+1], *tempptr, *found;
  WINDOW *dialog, *text;

  if (height < 0 || width < 0) {
    fprintf(stderr, "\nAutosizing is impossible in dialog_textbox().\n");
    return(-1);
  }

  search_term[0] = '\0';    /* no search term entered yet */

  /* Open input file for reading */
  if ((fd = open(file, O_RDONLY)) == -1) {
    fprintf(stderr, "\nCan't open input file <%s>in dialog_textbox().\n", file);
    return(-1);
  }
  /* Get file size. Actually, 'file_size' is the real file size - 1,
     since it's only the last byte offset from the beginning */
  if ((file_size = lseek(fd, 0, SEEK_END)) == -1) {
    fprintf(stderr, "\nError getting file size in dialog_textbox().\n");
    return(-1);
  }
  /* Restore file pointer to beginning of file after getting file size */
  if (lseek(fd, 0, SEEK_SET) == -1) {
    fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
    return(-1);
  }
  /* Allocate space for read buffer */
  if ((buf = malloc(BUF_SIZE+1)) == NULL) {
    endwin();
    fprintf(stderr, "\nCan't allocate memory in dialog_textbox().\n");
    exit(-1);
  }
  if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
    fprintf(stderr, "\nError reading file in dialog_textbox().\n");
    return(-1);
  }
  buf[bytes_read] = '\0';    /* mark end of valid data */
  page = buf;    /* page is pointer to start of page to be displayed */

  if (width > COLS)
	width = COLS;
  if (height > LINES)
	height = LINES;
  /* center dialog box on screen */
  x = DialogX ? DialogX : (COLS - width)/2;
  y = DialogY ? DialogY : (LINES - height)/2;

#ifdef HAVE_NCURSES
  if (use_shadow)
    draw_shadow(stdscr, y, x, height, width);
#endif
  dialog = newwin(height, width, y, x);
  if (dialog == NULL) {
    endwin();
    fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
    exit(1);
  }
  keypad(dialog, TRUE);

  /* Create window for text region, used for scrolling text */
/*  text = newwin(height-4, width-2, y+1, x+1); */
  text = subwin(dialog, height-4, width-2, y+1, x+1);
  if (text == NULL) {
    endwin();
    fprintf(stderr, "\nsubwin(dialog,%d,%d,%d,%d) failed, maybe wrong dims\n", height-4,width-2,y+1,x+1);
    exit(1);
  }
  keypad(text, TRUE);

  draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);

  wattrset(dialog, border_attr);
  wmove(dialog, height-3, 0);
  waddch(dialog, ACS_LTEE);
  for (i = 0; i < width-2; i++)
    waddch(dialog, ACS_HLINE);
  wattrset(dialog, dialog_attr);
  waddch(dialog, ACS_RTEE);
  wmove(dialog, height-2, 1);
  for (i = 0; i < width-2; i++)
    waddch(dialog, ' ');

  if (title != NULL) {
    wattrset(dialog, title_attr);
    wmove(dialog, 0, (width - strlen(title))/2 - 1);
    waddch(dialog, ' ');
    waddstr(dialog, title);
    waddch(dialog, ' ');
  }
  display_helpline(dialog, height-1, width);

  print_button(dialog, " OK ", height-2, width/2-2, TRUE);
  wnoutrefresh(dialog);
  getyx(dialog, cur_y, cur_x);    /* Save cursor position */

  /* Print first page of text */
  attr_clear(text, height-4, width-2, dialog_attr);
  print_page(text, height-4, width-2);
  print_position(dialog, height, width);
  wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
  wrefresh(dialog);

  while ((key != ESC) && (key != '\n') && (key != '\r')) {
    key = wgetch(dialog);
    switch (key) {
      case 'E':    /* Exit */
      case 'e':
        delwin(dialog);
        free(buf);
        close(fd);
        return 0;
      case 'g':    /* First page */
      case KEY_HOME:
        if (!begin_reached) {
          begin_reached = 1;
          /* First page not in buffer? */
          if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
            endwin();
            fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
            exit(-1);
          }
          if (fpos > bytes_read) {    /* Yes, we have to read it in */
            if (lseek(fd, 0, SEEK_SET) == -1) {
              endwin();
              fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
              exit(-1);
            }
            if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
              endwin();
              fprintf(stderr, "\nError reading file in dialog_textbox().\n");
              exit(-1);
            }
            buf[bytes_read] = '\0';
          }
          page = buf;
          print_page(text, height-4, width-2);
          print_position(dialog, height, width);
          wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
          wrefresh(dialog);
        }
        break;
      case 'G':    /* Last page */
#ifdef HAVE_NCURSES
      case KEY_END:
#endif
        end_reached = 1;
        /* Last page not in buffer? */
        if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
          endwin();
          fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
          exit(-1);
        }
        if (fpos < file_size) {    /* Yes, we have to read it in */
          if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) {
            endwin();
            fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
            exit(-1);
          }
          if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
            endwin();
            fprintf(stderr, "\nError reading file in dialog_textbox().\n");
            exit(-1);
          }
          buf[bytes_read] = '\0';
        }
        page = buf + bytes_read;
        back_lines(height-4);
        print_page(text, height-4, width-2);
        print_position(dialog, height, width);
        wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
        wrefresh(dialog);
        break;
      case 'K':    /* Previous line */
      case 'k':
      case KEY_UP:
        if (!begin_reached) {
          back_lines(page_length+1);
#ifdef HAVE_NCURSES
          /* We don't call print_page() here but use scrolling to ensure
             faster screen update. However, 'end_reached' and 'page_length'
             should still be updated, and 'page' should point to start of
             next page. This is done by calling get_line() in the following
             'for' loop. */
          scrollok(text, TRUE);
          wscrl(text, -1);    /* Scroll text region down one line */
          scrollok(text, FALSE);
          page_length = 0;
          passed_end = 0;
          for (i = 0; i < height-4; i++) {
            if (!i) {
              print_line(text, 0, width-2);    /* print first line of page */
              wnoutrefresh(text);
            }
            else
              get_line();    /* Called to update 'end_reached' and 'page' */
            if (!passed_end)
              page_length++;
            if (end_reached && !passed_end)
              passed_end = 1;
          }
#else
          print_page(text, height-4, width-2);
#endif
          print_position(dialog, height, width);
          wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
          wrefresh(dialog);
        }
        break;
      case 'B':    /* Previous page */
      case 'b':
      case KEY_PPAGE:
        if (!begin_reached) {
          back_lines(page_length + height-4);
          print_page(text, height-4, width-2);
          print_position(dialog, height, width);
          wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
          wrefresh(dialog);
        }
        break;
      case 'J':    /* Next line */
      case 'j':
      case KEY_DOWN:
        if (!end_reached) {
          begin_reached = 0;
          scrollok(text, TRUE);
          scroll(text);    /* Scroll text region up one line */
          scrollok(text, FALSE);
          print_line(text, height-5, width-2);
#ifndef HAVE_NCURSES
          wmove(text, height-5, 0);
          waddch(text, ' ');
          wmove(text, height-5, width-3);
          waddch(text, ' ');
#endif
          wnoutrefresh(text);
          print_position(dialog, height, width);
          wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
          wrefresh(dialog);
        }
        break;
      case ' ':    /* Next page */
      case KEY_NPAGE:
        if (!end_reached) {
          begin_reached = 0;
          print_page(text, height-4, width-2);
          print_position(dialog, height, width);
          wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
          wrefresh(dialog);
        }
        break;
      case '0':    /* Beginning of line */
      case 'H':    /* Scroll left */
      case 'h':
      case KEY_LEFT:
        if (hscroll > 0) {
          if (key == '0')
            hscroll = 0;
          else
            hscroll--;
          /* Reprint current page to scroll horizontally */
          back_lines(page_length);
          print_page(text, height-4, width-2);
          wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
          wrefresh(dialog);
        }
        break;
      case 'L':    /* Scroll right */
      case 'l':
      case KEY_RIGHT:
        if (hscroll < MAX_LEN) {
          hscroll++;
          /* Reprint current page to scroll horizontally */
          back_lines(page_length);
          print_page(text, height-4, width-2);
          wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
          wrefresh(dialog);
        }
        break;
      case '/':    /* Forward search */
      case 'n':    /* Repeat forward search */
      case '?':    /* Backward search */
      case 'N':    /* Repeat backward search */
        /* set search direction */
        dir = (key == '/' || key == 'n') ? 1 : 0;
        if (dir ? !end_reached : !begin_reached) {
          if (key == 'n' || key == 'N') {
            if (search_term[0] == '\0') {    /* No search term yet */
              fprintf(stderr, "\a");    /* beep */
              break;
            }
	  }
          else    /* Get search term from user */
            if (get_search_term(text, search_term, height-4, width-2) == -1) {
              /* ESC pressed in get_search_term(). Reprint page to clear box */
              wattrset(text, dialog_attr);
              back_lines(page_length);
              print_page(text, height-4, width-2);
              wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
              wrefresh(dialog);
              break;
            }
          /* Save variables for restoring in case search term can't be found */
          tempptr = page;
          temp = begin_reached;
          temp1 = end_reached;
          if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
            endwin();
            fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
            exit(-1);
          }
          fpos -= bytes_read;
          /* update 'page' to point to next (previous) line before
             forward (backward) searching */
          back_lines(dir ? page_length-1 : page_length+1);
          found = NULL;
          if (dir)    /* Forward search */
            while((found = strstr(get_line(), search_term)) == NULL) {
              if (end_reached)
                break;
	    }
          else    /* Backward search */
            while((found = strstr(get_line(), search_term)) == NULL) {
              if (begin_reached)
                break;
              back_lines(2);
            }
          if (found == NULL) {    /* not found */
            fprintf(stderr, "\a");    /* beep */
            /* Restore program state to that before searching */
            if (lseek(fd, fpos, SEEK_SET) == -1) {
              endwin();
              fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
              exit(-1);
            }
            if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
              endwin();
              fprintf(stderr, "\nError reading file in dialog_textbox().\n");
              exit(-1);
            }
            buf[bytes_read] = '\0';
            page = tempptr;
            begin_reached = temp;
            end_reached = temp1;
            /* move 'page' to point to start of current page in order to
               re-print current page. Note that 'page' always points to
               start of next page, so this is necessary */
            back_lines(page_length);
          }
          else    /* Search term found */
            back_lines(1);
          /* Reprint page */
          wattrset(text, dialog_attr);
          print_page(text, height-4, width-2);
          if (found != NULL)
            print_position(dialog, height, width);
          wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
          wrefresh(dialog);
        }
        else    /* no need to find */
          fprintf(stderr, "\a");    /* beep */
        break;
      case ESC:
        break;
    case KEY_F(1):
	display_helpfile();
	break;
    }
  }

  delwin(dialog);
  free(buf);
  close(fd);
  return -1;    /* ESC pressed */
}
/* End of dialog_textbox() */


/*
 * Go back 'n' lines in text file. Called by dialog_textbox().
 * 'page' will be updated to point to the desired line in 'buf'.
 */
static void back_lines(int n)
{
  int i, fpos;

  begin_reached = 0;
  /* We have to distinguish between end_reached and !end_reached since at end
     of file, the line is not ended by a '\n'. The code inside 'if' basically
     does a '--page' to move one character backward so as to skip '\n' of the
     previous line */
  if (!end_reached) {
    /* Either beginning of buffer or beginning of file reached? */
    if (page == buf) {
      if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
        endwin();
        fprintf(stderr, "\nError moving file pointer in back_lines().\n");
        exit(-1);
      }
      if (fpos > bytes_read) {    /* Not beginning of file yet */
        /* We've reached beginning of buffer, but not beginning of file yet,
           so read previous part of file into buffer. Note that we only
           move backward for BUF_SIZE/2 bytes, but not BUF_SIZE bytes to
           avoid re-reading again in print_page() later */
        /* Really possible to move backward BUF_SIZE/2 bytes? */
        if (fpos < BUF_SIZE/2 + bytes_read) {
          /* No, move less then */
          if (lseek(fd, 0, SEEK_SET) == -1) {
            endwin();
            fprintf(stderr, "\nError moving file pointer in back_lines().\n");
            exit(-1);
          }
          page = buf + fpos - bytes_read;
        }
        else {    /* Move backward BUF_SIZE/2 bytes */
          if (lseek(fd, -(BUF_SIZE/2 + bytes_read), SEEK_CUR) == -1) {
            endwin();
            fprintf(stderr, "\nError moving file pointer in back_lines().\n");
            exit(-1);
          }
          page = buf + BUF_SIZE/2;
        }
        if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
          endwin();
          fprintf(stderr, "\nError reading file in back_lines().\n");
          exit(-1);
        }
        buf[bytes_read] = '\0';
      }
      else {    /* Beginning of file reached */
        begin_reached = 1;
        return;
      }
    }
    if (*(--page) != '\n') {    /* '--page' here */
      /* Something's wrong... */
      endwin();
      fprintf(stderr, "\nInternal error in back_lines().\n");
      exit(-1);
    }
  }

  /* Go back 'n' lines */
  for (i = 0; i < n; i++)
    do {
      if (page == buf) {
        if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
          endwin();
          fprintf(stderr, "\nError moving file pointer in back_lines().\n");
          exit(-1);
        }
        if (fpos > bytes_read) {
          /* Really possible to move backward BUF_SIZE/2 bytes? */
          if (fpos < BUF_SIZE/2 + bytes_read) {
            /* No, move less then */
            if (lseek(fd, 0, SEEK_SET) == -1) {
              endwin();
              fprintf(stderr, "\nError moving file pointer in back_lines().\n");
              exit(-1);
            }
            page = buf + fpos - bytes_read;
          }
          else {    /* Move backward BUF_SIZE/2 bytes */
            if (lseek(fd, -(BUF_SIZE/2 + bytes_read), SEEK_CUR) == -1) {
              endwin();
              fprintf(stderr, "\nError moving file pointer in back_lines().\n");
              exit(-1);
            }
            page = buf + BUF_SIZE/2;
          }
          if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
            endwin();
            fprintf(stderr, "\nError reading file in back_lines().\n");
            exit(-1);
          }
          buf[bytes_read] = '\0';
        }
        else {    /* Beginning of file reached */
          begin_reached = 1;
          return;
        }
      }
    } while (*(--page) != '\n');
  page++;
}
/* End of back_lines() */


/*
 * Print a new page of text. Called by dialog_textbox().
 */
static void print_page(WINDOW *win, int height, int width)
{
  int i, passed_end = 0;

  page_length = 0;
  for (i = 0; i < height; i++) {
    print_line(win, i, width);
    if (!passed_end)
      page_length++;
    if (end_reached && !passed_end)
      passed_end = 1;
  }
  wnoutrefresh(win);
}
/* End of print_page() */


/*
 * Print a new line of text. Called by dialog_textbox() and print_page().
 */
static void print_line(WINDOW *win, int row, int width)
{
  int i, y, x;
  unsigned char *line;

  line = get_line();
  line += MIN(strlen(line),hscroll);    /* Scroll horizontally */
  wmove(win, row, 0);    /* move cursor to correct line */
  waddch(win,' ');
#ifdef HAVE_NCURSES
  waddnstr(win, line, MIN(strlen(line),width-2));
#else
  line[MIN(strlen(line),width-2)] = '\0';
  waddstr(win, line);
#endif

  getyx(win, y, x);
  /* Clear 'residue' of previous line */
  for (i = 0; i < width-x; i++)
    waddch(win, ' ');
}
/* End of print_line() */


/*
 * Return current line of text. Called by dialog_textbox() and print_line().
 * 'page' should point to start of current line before calling, and will be
 * updated to point to start of next line.
 */
static unsigned char *get_line(void)
{
  int i = 0, fpos;
  static unsigned char line[MAX_LEN+1];

  end_reached = 0;
  while (*page != '\n') {
    if (*page == '\0') {    /* Either end of file or end of buffer reached */
      if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
        endwin();
        fprintf(stderr, "\nError moving file pointer in get_line().\n");
        exit(-1);
      }
      if (fpos < file_size) {    /* Not end of file yet */
        /* We've reached end of buffer, but not end of file yet, so read next
           part of file into buffer */
        if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
          endwin();
          fprintf(stderr, "\nError reading file in get_line().\n");
          exit(-1);
        }
        buf[bytes_read] = '\0';
        page = buf;
      }
      else {
        if (!end_reached)
          end_reached = 1;
        break;
      }
    }
    else
      if (i < MAX_LEN)
        line[i++] = *(page++);
      else {
        if (i == MAX_LEN)  /* Truncate lines longer than MAX_LEN characters */
          line[i++] = '\0';
        page++;
      }
  }
  if (i <= MAX_LEN)
    line[i] = '\0';
  if (!end_reached)
    page++;    /* move pass '\n' */

  return line;
}
/* End of get_line() */


/*
 * Display a dialog box and get the search term from user
 */
static int get_search_term(WINDOW *win, unsigned char *search_term, int height, int width)
{
  int x, y, key = 0, first,
      box_height = 3, box_width = 30;

  x = (width - box_width)/2;
  y = (height - box_height)/2;
#ifdef HAVE_NCURSES
  if (use_shadow)
    draw_shadow(win, y, x, box_height, box_width);
#endif
  draw_box(win, y, x, box_height, box_width, dialog_attr, searchbox_border_attr);
  wattrset(win, searchbox_title_attr);
  wmove(win, y, x+box_width/2-4);
  waddstr(win, " Search ");
  wattrset(win, dialog_attr);

  search_term[0] = '\0';

  first = 1;
  while (key != ESC) {
    key = line_edit(win, y+1, x+1, -1, box_width-2, searchbox_attr, first, search_term);
    first = 0;
    switch (key) {
      case '\n':
        if (search_term[0] != '\0')
          return 0;
        break;
      case ESC:
	break;
    }
  }

  return -1;    /* ESC pressed */
}
/* End of get_search_term() */


/*
 * Print current position
 */
static void print_position(WINDOW *win, int height, int width)
{
  int fpos, percent;

  if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
    endwin();
    fprintf(stderr, "\nError moving file pointer in print_position().\n");
    exit(-1);
  }
  wattrset(win, position_indicator_attr);
  percent = !file_size ? 100 : ((fpos-bytes_read+page-buf)*100)/file_size;
  wmove(win, height-3, width-9);
  wprintw(win, "(%3d%%)", percent);
}
/* End of print_position() */
OpenPOWER on IntegriCloud