aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/lxdialog/textbox.c
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lxdialog/textbox.c')
-rw-r--r--scripts/lxdialog/textbox.c93
1 files changed, 32 insertions, 61 deletions
diff --git a/scripts/lxdialog/textbox.c b/scripts/lxdialog/textbox.c
index d6e7f2afe31a..fa8d92ea02b6 100644
--- a/scripts/lxdialog/textbox.c
+++ b/scripts/lxdialog/textbox.c
@@ -46,30 +46,26 @@ int dialog_textbox(const char *title, const char *file, int height, int width)
46 /* Open input file for reading */ 46 /* Open input file for reading */
47 if ((fd = open(file, O_RDONLY)) == -1) { 47 if ((fd = open(file, O_RDONLY)) == -1) {
48 endwin(); 48 endwin();
49 fprintf(stderr, 49 fprintf(stderr, "\nCan't open input file in dialog_textbox().\n");
50 "\nCan't open input file in dialog_textbox().\n");
51 exit(-1); 50 exit(-1);
52 } 51 }
53 /* Get file size. Actually, 'file_size' is the real file size - 1, 52 /* Get file size. Actually, 'file_size' is the real file size - 1,
54 since it's only the last byte offset from the beginning */ 53 since it's only the last byte offset from the beginning */
55 if ((file_size = lseek(fd, 0, SEEK_END)) == -1) { 54 if ((file_size = lseek(fd, 0, SEEK_END)) == -1) {
56 endwin(); 55 endwin();
57 fprintf(stderr, 56 fprintf(stderr, "\nError getting file size in dialog_textbox().\n");
58 "\nError getting file size in dialog_textbox().\n");
59 exit(-1); 57 exit(-1);
60 } 58 }
61 /* Restore file pointer to beginning of file after getting file size */ 59 /* Restore file pointer to beginning of file after getting file size */
62 if (lseek(fd, 0, SEEK_SET) == -1) { 60 if (lseek(fd, 0, SEEK_SET) == -1) {
63 endwin(); 61 endwin();
64 fprintf(stderr, 62 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
65 "\nError moving file pointer in dialog_textbox().\n");
66 exit(-1); 63 exit(-1);
67 } 64 }
68 /* Allocate space for read buffer */ 65 /* Allocate space for read buffer */
69 if ((buf = malloc(BUF_SIZE + 1)) == NULL) { 66 if ((buf = malloc(BUF_SIZE + 1)) == NULL) {
70 endwin(); 67 endwin();
71 fprintf(stderr, 68 fprintf(stderr, "\nCan't allocate memory in dialog_textbox().\n");
72 "\nCan't allocate memory in dialog_textbox().\n");
73 exit(-1); 69 exit(-1);
74 } 70 }
75 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) { 71 if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) {
@@ -150,23 +146,20 @@ int dialog_textbox(const char *title, const char *file, int height, int width)
150 /* First page not in buffer? */ 146 /* First page not in buffer? */
151 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { 147 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
152 endwin(); 148 endwin();
153 fprintf(stderr, 149 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
154 "\nError moving file pointer in dialog_textbox().\n");
155 exit(-1); 150 exit(-1);
156 } 151 }
157 if (fpos > bytes_read) { /* Yes, we have to read it in */ 152 if (fpos > bytes_read) { /* Yes, we have to read it in */
158 if (lseek(fd, 0, SEEK_SET) == -1) { 153 if (lseek(fd, 0, SEEK_SET) == -1) {
159 endwin(); 154 endwin();
160 fprintf(stderr, 155 fprintf(stderr, "\nError moving file pointer in "
161 "\nError moving file pointer in " 156 "dialog_textbox().\n");
162 "dialog_textbox().\n");
163 exit(-1); 157 exit(-1);
164 } 158 }
165 if ((bytes_read = 159 if ((bytes_read =
166 read(fd, buf, BUF_SIZE)) == -1) { 160 read(fd, buf, BUF_SIZE)) == -1) {
167 endwin(); 161 endwin();
168 fprintf(stderr, 162 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
169 "\nError reading file in dialog_textbox().\n");
170 exit(-1); 163 exit(-1);
171 } 164 }
172 buf[bytes_read] = '\0'; 165 buf[bytes_read] = '\0';
@@ -185,22 +178,19 @@ int dialog_textbox(const char *title, const char *file, int height, int width)
185 /* Last page not in buffer? */ 178 /* Last page not in buffer? */
186 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { 179 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
187 endwin(); 180 endwin();
188 fprintf(stderr, 181 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
189 "\nError moving file pointer in dialog_textbox().\n");
190 exit(-1); 182 exit(-1);
191 } 183 }
192 if (fpos < file_size) { /* Yes, we have to read it in */ 184 if (fpos < file_size) { /* Yes, we have to read it in */
193 if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) { 185 if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) {
194 endwin(); 186 endwin();
195 fprintf(stderr, 187 fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
196 "\nError moving file pointer in dialog_textbox().\n");
197 exit(-1); 188 exit(-1);
198 } 189 }
199 if ((bytes_read = 190 if ((bytes_read =
200 read(fd, buf, BUF_SIZE)) == -1) { 191 read(fd, buf, BUF_SIZE)) == -1) {
201 endwin(); 192 endwin();
202 fprintf(stderr, 193 fprintf(stderr, "\nError reading file in dialog_textbox().\n");
203 "\nError reading file in dialog_textbox().\n");
204 exit(-1); 194 exit(-1);
205 } 195 }
206 buf[bytes_read] = '\0'; 196 buf[bytes_read] = '\0';
@@ -342,9 +332,8 @@ static void back_lines(int n)
342 if (page == buf) { 332 if (page == buf) {
343 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { 333 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
344 endwin(); 334 endwin();
345 fprintf(stderr, 335 fprintf(stderr, "\nError moving file pointer in "
346 "\nError moving file pointer in " 336 "back_lines().\n");
347 "back_lines().\n");
348 exit(-1); 337 exit(-1);
349 } 338 }
350 if (fpos > bytes_read) { /* Not beginning of file yet */ 339 if (fpos > bytes_read) { /* Not beginning of file yet */
@@ -358,21 +347,16 @@ static void back_lines(int n)
358 /* No, move less then */ 347 /* No, move less then */
359 if (lseek(fd, 0, SEEK_SET) == -1) { 348 if (lseek(fd, 0, SEEK_SET) == -1) {
360 endwin(); 349 endwin();
361 fprintf(stderr, 350 fprintf(stderr, "\nError moving file pointer in "
362 "\nError moving file pointer in " 351 "back_lines().\n");
363 "back_lines().\n");
364 exit(-1); 352 exit(-1);
365 } 353 }
366 page = buf + fpos - bytes_read; 354 page = buf + fpos - bytes_read;
367 } else { /* Move backward BUF_SIZE/2 bytes */ 355 } else { /* Move backward BUF_SIZE/2 bytes */
368 if (lseek 356 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) {
369 (fd, -(BUF_SIZE / 2 + bytes_read),
370 SEEK_CUR)
371 == -1) {
372 endwin(); 357 endwin();
373 fprintf(stderr, 358 fprintf(stderr, "\nError moving file pointer "
374 "\nError moving file pointer " 359 "in back_lines().\n");
375 "in back_lines().\n");
376 exit(-1); 360 exit(-1);
377 } 361 }
378 page = buf + BUF_SIZE / 2; 362 page = buf + BUF_SIZE / 2;
@@ -380,8 +364,7 @@ static void back_lines(int n)
380 if ((bytes_read = 364 if ((bytes_read =
381 read(fd, buf, BUF_SIZE)) == -1) { 365 read(fd, buf, BUF_SIZE)) == -1) {
382 endwin(); 366 endwin();
383 fprintf(stderr, 367 fprintf(stderr, "\nError reading file in back_lines().\n");
384 "\nError reading file in back_lines().\n");
385 exit(-1); 368 exit(-1);
386 } 369 }
387 buf[bytes_read] = '\0'; 370 buf[bytes_read] = '\0';
@@ -403,33 +386,25 @@ static void back_lines(int n)
403 if (page == buf) { 386 if (page == buf) {
404 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { 387 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
405 endwin(); 388 endwin();
406 fprintf(stderr, 389 fprintf(stderr, "\nError moving file pointer in back_lines().\n");
407 "\nError moving file pointer in back_lines().\n");
408 exit(-1); 390 exit(-1);
409 } 391 }
410 if (fpos > bytes_read) { 392 if (fpos > bytes_read) {
411 /* Really possible to move backward BUF_SIZE/2 bytes? */ 393 /* Really possible to move backward BUF_SIZE/2 bytes? */
412 if (fpos < BUF_SIZE / 2 + bytes_read) { 394 if (fpos < BUF_SIZE / 2 + bytes_read) {
413 /* No, move less then */ 395 /* No, move less then */
414 if (lseek(fd, 0, SEEK_SET) == 396 if (lseek(fd, 0, SEEK_SET) == -1) {
415 -1) {
416 endwin(); 397 endwin();
417 fprintf(stderr, 398 fprintf(stderr, "\nError moving file pointer "
418 "\nError moving file pointer " 399 "in back_lines().\n");
419 "in back_lines().\n");
420 exit(-1); 400 exit(-1);
421 } 401 }
422 page = buf + fpos - bytes_read; 402 page = buf + fpos - bytes_read;
423 } else { /* Move backward BUF_SIZE/2 bytes */ 403 } else { /* Move backward BUF_SIZE/2 bytes */
424 if (lseek 404 if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) {
425 (fd,
426 -(BUF_SIZE / 2 +
427 bytes_read),
428 SEEK_CUR) == -1) {
429 endwin(); 405 endwin();
430 fprintf(stderr, 406 fprintf(stderr, "\nError moving file pointer"
431 "\nError moving file pointer" 407 " in back_lines().\n");
432 " in back_lines().\n");
433 exit(-1); 408 exit(-1);
434 } 409 }
435 page = buf + BUF_SIZE / 2; 410 page = buf + BUF_SIZE / 2;
@@ -437,9 +412,8 @@ static void back_lines(int n)
437 if ((bytes_read = 412 if ((bytes_read =
438 read(fd, buf, BUF_SIZE)) == -1) { 413 read(fd, buf, BUF_SIZE)) == -1) {
439 endwin(); 414 endwin();
440 fprintf(stderr, 415 fprintf(stderr, "\nError reading file in "
441 "\nError reading file in " 416 "back_lines().\n");
442 "back_lines().\n");
443 exit(-1); 417 exit(-1);
444 } 418 }
445 buf[bytes_read] = '\0'; 419 buf[bytes_read] = '\0';
@@ -513,9 +487,8 @@ static char *get_line(void)
513 /* Either end of file or end of buffer reached */ 487 /* Either end of file or end of buffer reached */
514 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { 488 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
515 endwin(); 489 endwin();
516 fprintf(stderr, 490 fprintf(stderr, "\nError moving file pointer in "
517 "\nError moving file pointer in " 491 "get_line().\n");
518 "get_line().\n");
519 exit(-1); 492 exit(-1);
520 } 493 }
521 if (fpos < file_size) { /* Not end of file yet */ 494 if (fpos < file_size) { /* Not end of file yet */
@@ -524,8 +497,7 @@ static char *get_line(void)
524 if ((bytes_read = 497 if ((bytes_read =
525 read(fd, buf, BUF_SIZE)) == -1) { 498 read(fd, buf, BUF_SIZE)) == -1) {
526 endwin(); 499 endwin();
527 fprintf(stderr, 500 fprintf(stderr, "\nError reading file in get_line().\n");
528 "\nError reading file in get_line().\n");
529 exit(-1); 501 exit(-1);
530 } 502 }
531 buf[bytes_read] = '\0'; 503 buf[bytes_read] = '\0';
@@ -561,8 +533,7 @@ static void print_position(WINDOW * win, int height, int width)
561 533
562 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { 534 if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) {
563 endwin(); 535 endwin();
564 fprintf(stderr, 536 fprintf(stderr, "\nError moving file pointer in print_position().\n");
565 "\nError moving file pointer in print_position().\n");
566 exit(-1); 537 exit(-1);
567 } 538 }
568 wattrset(win, position_indicator_attr); 539 wattrset(win, position_indicator_attr);