forked from maandree/util-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmore.c
2198 lines (2042 loc) · 47.7 KB
/
more.c
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 1980 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/* more.c - General purpose tty output filter and file perusal program
*
* by Eric Shienbrood, UC Berkeley
*
* modified by Geoff Peck
* UCB to add underlining, single spacing
* modified by John Foderaro
* UCB to add -c and MORE environment variable
* modified by Erik Troan <ewt@redhat.com>
* to be more posix and so compile on linux/axp.
* modified by Kars de Jong <jongk@cs.utwente.nl>
* to use terminfo instead of termcap.
* 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
* added Native Language Support
* 1999-03-19 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
* more nls translatable strings
* 1999-05-09 aeb
* applied a RedHat patch (setjmp->sigsetjmp); without it a second
* ^Z would fail.
* 1999-05-09 aeb
* undone Kars' work, so that more works without libcurses (and
* hence can be in /bin with libcurses being in
* /usr/lib which may not be mounted). However, when termcap is not
* present curses can still be used.
* 2010-10-21 Davidlohr Bueso <dave@gnu.org>
* modified mem allocation handling for util-linux
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h> /* for alloca() */
#include <stdarg.h> /* for va_start() etc */
#include <sys/param.h>
#include <ctype.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <setjmp.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/wait.h>
#include "strutils.h"
#include "nls.h"
#include "xalloc.h"
#include "widechar.h"
#include "closestream.h"
#include <regex.h>
#ifdef TEST_PROGRAM
# define NON_INTERACTIVE_MORE 1
#endif
#ifndef XTABS
# define XTABS TAB3
#endif
#define VI "vi" /* found on the user's path */
#define Fopen(s,m) (Currline = 0,file_pos=0,fopen(s,m))
#define Ftell(f) file_pos
#define Fseek(f,off) (file_pos=off,fseek(f,off,0))
#define Getc(f) (++file_pos, getc(f))
#define Ungetc(c,f) (--file_pos, ungetc(c,f))
#define putcerr(c) fputc(c, stderr)
#define putserr(s) fputs(s, stderr)
#define putsout(s) fputs(s, stdout)
#define stty(fd,argp) tcsetattr(fd,TCSANOW,argp)
/* some function declarations */
void initterm(void);
void kill_line(void);
void doclear(void);
void cleareol(void);
void clreos(void);
void home(void);
void more_error(char *mess);
void do_shell(char *filename);
int colon(char *filename, int cmd, int nlines);
int expand(char **outbuf, char *inbuf);
void argscan(char *s);
void rdline(register FILE *f);
void copy_file(register FILE *f);
void search(char buf[], FILE *file, register int n);
void skipf(register int nskip);
void skiplns(register int n, register FILE *f);
void screen(register FILE *f, register int num_lines);
int command(char *filename, register FILE *f);
void erasep(register int col);
void show(register char ch);
void set_tty(void);
void reset_tty(void);
void ttyin(char buf[], register int nmax, char pchar);
int number(char *cmd);
int readch(void);
int get_line(register FILE *f, int *length);
void prbuf(register char *s, register int n);
void execute(char *filename, char *cmd, ...);
FILE *checkf(char *, int *);
void prepare_line_buffer(void);
#define TBUFSIZ 1024
#define LINSIZ 256 /* minimal Line buffer size */
#define ctrl(letter) (letter & 077)
#define RUBOUT '\177'
#define ESC '\033'
#define QUIT '\034'
#define SCROLL_LEN 11
#define LINES_PER_PAGE 24
#define NUM_COLUMNS 80
#define TERMINAL_BUF 4096
#define INIT_BUF 80
#define SHELL_LINE 1000
#define COMMAND_BUF 200
#define REGERR_BUF NUM_COLUMNS
struct termios otty, savetty0;
long file_pos, file_size;
int fnum, no_intty, no_tty, slow_tty;
int dum_opt, dlines;
void onquit(int), onsusp(int), chgwinsz(int), end_it(int);
int nscroll = SCROLL_LEN; /* Number of lines scrolled by 'd' */
int fold_opt = 1; /* Fold long lines */
int stop_opt = 1; /* Stop after form feeds */
int ssp_opt = 0; /* Suppress white space */
int ul_opt = 1; /* Underline as best we can */
int promptlen;
int Currline; /* Line we are currently at */
int startup = 1;
int firstf = 1;
int notell = 1;
int docrterase = 0;
int docrtkill = 0;
int bad_so; /* True if overwriting does not turn
off standout */
int inwait, Pause, errors;
int within; /* true if we are within a file,
false if we are between files */
int hard, dumb, noscroll, hardtabs, clreol, eatnl;
int catch_susp; /* We should catch the SIGTSTP signal */
char **fnames; /* The list of file names */
int nfiles; /* Number of files left to process */
char *shell; /* The name of the shell to use */
int shellp; /* A previous shell command exists */
sigjmp_buf restore;
char *Line; /* Line buffer */
size_t LineLen; /* size of Line buffer */
int Lpp = LINES_PER_PAGE; /* lines per page */
char *Clear; /* clear screen */
char *eraseln; /* erase line */
char *Senter, *Sexit; /* enter and exit standout mode */
char *ULenter, *ULexit; /* enter and exit underline mode */
char *chUL; /* underline character */
char *chBS; /* backspace character */
char *Home; /* go to home */
char *cursorm; /* cursor movement */
char cursorhome[40]; /* contains cursor movement to home */
char *EodClr; /* clear rest of screen */
int Mcol = NUM_COLUMNS; /* number of columns */
int Wrap = 1; /* set if automargins */
int soglitch; /* terminal has standout mode glitch */
int ulglitch; /* terminal has underline mode glitch */
int pstate = 0; /* current UL state */
static int magic(FILE *, char *);
char *previousre; /* previous search() buf[] item */
struct {
long chrctr, line;
} context, screen_start;
extern char PC; /* pad character */
#ifdef HAVE_NCURSES_H
# include <ncurses.h>
#elif defined(HAVE_NCURSES_NCURSES_H)
# include <ncurses/ncurses.h>
#endif
#if defined(HAVE_NCURSES_H) || defined(HAVE_NCURSES_NCURSES_H)
# include <term.h> /* include after <curses.h> */
# define TERM_AUTO_RIGHT_MARGIN "am"
# define TERM_CEOL "xhp"
# define TERM_CLEAR "clear"
# define TERM_CLEAR_TO_LINE_END "el"
# define TERM_CLEAR_TO_SCREEN_END "ed"
# define TERM_COLS "cols"
# define TERM_CURSOR_ADDRESS "cup"
# define TERM_EAT_NEW_LINE "xenl"
# define TERM_ENTER_UNDERLINE "smul"
# define TERM_EXIT_STANDARD_MODE "rmso"
# define TERM_EXIT_UNDERLINE "rmul"
# define TERM_HARD_COPY "hc"
# define TERM_HOME "home"
# define TERM_LINE_DOWN "cud1"
# define TERM_LINES "lines"
# define TERM_OVER_STRIKE "os"
# define TERM_PAD_CHAR "pad"
# define TERM_STANDARD_MODE "smso"
# define TERM_STD_MODE_GLITCH "xmc"
# define TERM_UNDERLINE_CHAR "uc"
# define TERM_UNDERLINE "ul"
static void my_putstring(char *s)
{
tputs(s, fileno(stdout), putchar); /* putp(s); */
}
static void my_setupterm(char *term, int fildes, int *errret)
{
setupterm(term, fildes, errret);
}
static int my_tgetnum(char *s)
{
return tigetnum(s);
}
static int my_tgetflag(char *s)
{
return tigetflag(s);
}
static char *my_tgetstr(char *s)
{
return tigetstr(s);
}
static char *my_tgoto(char *cap, int col, int row)
{
return tparm(cap, col, row);
}
#elif defined(HAVE_LIBTERMCAP) /* ncurses not found */
# include <termcap.h>
# define TERM_AUTO_RIGHT_MARGIN "am"
# define TERM_CEOL "xs"
# define TERM_CLEAR "cl"
# define TERM_CLEAR_TO_LINE_END "ce"
# define TERM_CLEAR_TO_SCREEN_END "cd"
# define TERM_COLS "co"
# define TERM_CURSOR_ADDRESS "cm"
# define TERM_EAT_NEW_LINE "xn"
# define TERM_ENTER_UNDERLINE "us"
# define TERM_EXIT_STANDARD_MODE "se"
# define TERM_EXIT_UNDERLINE "ue"
# define TERM_HARD_COPY "hc"
# define TERM_HOME "ho"
# define TERM_LINE_DOWN "le"
# define TERM_LINES "li"
# define TERM_OVER_STRIKE "os"
# define TERM_PAD_CHAR "pc"
# define TERM_STANDARD_MODE "so"
# define TERM_STD_MODE_GLITCH "sg"
# define TERM_UNDERLINE_CHAR "uc"
# define TERM_UNDERLINE "ul"
char termbuffer[TERMINAL_BUF];
char tcbuffer[TERMINAL_BUF];
char *strbuf = termbuffer;
static void my_putstring(char *s)
{
tputs(s, fileno(stdout), putchar);
}
static void my_setupterm(char *term, int fildes __attribute__((__unused__)), int *errret)
{
*errret = tgetent(tcbuffer, term);
}
static int my_tgetnum(char *s)
{
return tgetnum(s);
}
static int my_tgetflag(char *s)
{
return tgetflag(s);
}
static char *my_tgetstr(char *s)
{
return tgetstr(s, &strbuf);
}
static char *my_tgoto(char *cap, int col, int row)
{
return tgoto(cap, col, row);
}
#endif /* HAVE_LIBTERMCAP */
static void __attribute__((__noreturn__)) usage(FILE *out)
{
fprintf(out,
_("Usage: %s [options] file...\n\n"),
program_invocation_short_name);
fprintf(out,
_("Options:\n"
" -d display help instead of ring bell\n"
" -f count logical, rather than screen lines\n"
" -l suppress pause after form feed\n"
" -p do not scroll, clean screen and display text\n"
" -c do not scroll, display text and clean line ends\n"
" -u suppress underlining\n"
" -s squeeze multiple blank lines into one\n"
" -NUM specify the number of lines per screenful\n"
" +NUM display file beginning from line number NUM\n"
" +/STRING display file beginning from search string match\n"
" -V output version information and exit\n"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
int main(int argc, char **argv)
{
FILE *f;
char *s;
int ch;
int left;
int prnames = 0;
int initopt = 0;
int srchopt = 0;
int clearit = 0;
int initline = 0;
char *initbuf = NULL;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
atexit(close_stdout);
nfiles = argc;
fnames = argv;
setlocale(LC_ALL, "");
initterm();
/* Auto set no scroll on when binary is called page */
if (!(strcmp(program_invocation_short_name, "page")))
noscroll++;
prepare_line_buffer();
nscroll = Lpp / 2 - 1;
if (nscroll <= 0)
nscroll = 1;
if ((s = getenv("MORE")) != NULL)
argscan(s);
while (--nfiles > 0) {
if ((ch = (*++fnames)[0]) == '-') {
argscan(*fnames + 1);
} else if (ch == '+') {
s = *fnames;
if (*++s == '/') {
srchopt++;
initbuf = xstrdup(s + 1);
} else {
initopt++;
for (initline = 0; *s != '\0'; s++)
if (isdigit(*s))
initline =
initline * 10 + *s - '0';
--initline;
}
} else
break;
}
/* allow clreol only if Home and eraseln and EodClr strings are
* defined, and in that case, make sure we are in noscroll mode */
if (clreol) {
if ((Home == NULL) || (*Home == '\0') ||
(eraseln == NULL) || (*eraseln == '\0') ||
(EodClr == NULL) || (*EodClr == '\0'))
clreol = 0;
else
noscroll = 1;
}
if (dlines == 0)
dlines = Lpp - 1; /* was: Lpp - (noscroll ? 1 : 2) */
left = dlines;
if (nfiles > 1)
prnames++;
if (!no_intty && nfiles == 0)
usage(stderr);
else
f = stdin;
if (!no_tty) {
signal(SIGQUIT, onquit);
signal(SIGINT, end_it);
#ifdef SIGWINCH
signal(SIGWINCH, chgwinsz);
#endif
if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
signal(SIGTSTP, onsusp);
catch_susp++;
}
stty(fileno(stderr), &otty);
}
if (no_intty) {
if (no_tty)
copy_file(stdin);
else {
if ((ch = Getc(f)) == '\f')
doclear();
else {
Ungetc(ch, f);
if (noscroll && (ch != EOF)) {
if (clreol)
home();
else
doclear();
}
}
if (srchopt) {
free(previousre);
previousre = xstrdup(initbuf);
search(initbuf, stdin, 1);
if (noscroll)
left--;
} else if (initopt)
skiplns(initline, stdin);
screen(stdin, left);
}
no_intty = 0;
prnames++;
firstf = 0;
}
while (fnum < nfiles) {
if ((f = checkf(fnames[fnum], &clearit)) != NULL) {
context.line = context.chrctr = 0;
Currline = 0;
if (firstf)
sigsetjmp(restore, 1);
if (firstf) {
firstf = 0;
if (srchopt) {
free(previousre);
previousre = xstrdup(initbuf);
search(initbuf, f, 1);
if (noscroll)
left--;
} else if (initopt)
skiplns(initline, f);
} else if (fnum < nfiles && !no_tty) {
sigsetjmp(restore, 1);
left = command(fnames[fnum], f);
}
if (left != 0) {
if ((noscroll || clearit)
&& (file_size != LONG_MAX)) {
if (clreol)
home();
else
doclear();
}
if (prnames) {
if (bad_so)
erasep(0);
if (clreol)
cleareol();
putsout("::::::::::::::");
if (promptlen > 14)
erasep(14);
putchar('\n');
if (clreol)
cleareol();
puts(fnames[fnum]);
if (clreol)
cleareol();
puts("::::::::::::::");
if (left > Lpp - 4)
left = Lpp - 4;
}
if (no_tty)
copy_file(f);
else {
within++;
screen(f, left);
within = 0;
}
}
sigsetjmp(restore, 1);
fflush(stdout);
fclose(f);
screen_start.line = screen_start.chrctr = 0L;
context.line = context.chrctr = 0L;
}
fnum++;
firstf = 0;
}
free(previousre);
free(initbuf);
free(Line);
reset_tty();
exit(EXIT_SUCCESS);
}
void argscan(char *s)
{
int seen_num = 0;
while (*s != '\0') {
switch (*s) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (!seen_num) {
dlines = 0;
seen_num = 1;
}
dlines = dlines * 10 + *s - '0';
break;
case 'd':
dum_opt = 1;
break;
case 'l':
stop_opt = 0;
break;
case 'f':
fold_opt = 0;
break;
case 'p':
noscroll++;
break;
case 'c':
clreol++;
break;
case 's':
ssp_opt = 1;
break;
case 'u':
ul_opt = 0;
break;
case '-':
case ' ':
case '\t':
break;
case 'V':
printf(UTIL_LINUX_VERSION);
exit(EXIT_SUCCESS);
break;
default:
warnx(_("unknown option -%s"), s);
usage(stderr);
break;
}
s++;
}
}
/* Check whether the file named by fs is an ASCII file which the user may
* access. If it is, return the opened file. Otherwise return NULL. */
FILE *checkf(register char *fs, int *clearfirst)
{
struct stat stbuf;
register FILE *f;
int c;
if (stat(fs, &stbuf) == -1) {
fflush(stdout);
if (clreol)
cleareol();
perror(fs);
return ((FILE *)NULL);
}
if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
printf(_("\n*** %s: directory ***\n\n"), fs);
return ((FILE *)NULL);
}
if ((f = Fopen(fs, "r")) == NULL) {
fflush(stdout);
perror(fs);
return ((FILE *)NULL);
}
if (magic(f, fs))
return ((FILE *)NULL);
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
c = Getc(f);
*clearfirst = (c == '\f');
Ungetc(c, f);
if ((file_size = stbuf.st_size) == 0)
file_size = LONG_MAX;
return (f);
}
/* magic --
* check for file magic numbers. This code would best be shared
* with the file(1) program or, perhaps, more should not try to be
* so smart. */
static int magic(FILE *f, char *fs)
{
signed char twobytes[2];
/* don't try to look ahead if the input is unseekable */
if (fseek(f, 0L, SEEK_SET))
return 0;
if (fread(twobytes, 2, 1, f) == 1) {
switch (twobytes[0] + (twobytes[1] << 8)) {
case 0407: /* a.out obj */
case 0410: /* a.out exec */
case 0413: /* a.out demand exec */
case 0405:
case 0411:
case 0177545:
case 0x457f: /* simple ELF detection */
printf(_("\n******** %s: Not a text file ********\n\n"),
fs);
fclose(f);
return 1;
}
}
fseek(f, 0L, SEEK_SET); /* rewind() not necessary */
return 0;
}
/* Print out the contents of the file f, one screenful at a time. */
#define STOP -10
void screen(register FILE *f, register int num_lines)
{
register int c;
register int nchars;
int length; /* length of current line */
static int prev_len = 1; /* length of previous line */
for (;;) {
while (num_lines > 0 && !Pause) {
if ((nchars = get_line(f, &length)) == EOF) {
if (clreol)
clreos();
return;
}
if (ssp_opt && length == 0 && prev_len == 0)
continue;
prev_len = length;
if (bad_so
|| ((Senter && *Senter == ' ') && (promptlen > 0)))
erasep(0);
/* must clear before drawing line since tabs on
* some terminals do not erase what they tab
* over. */
if (clreol)
cleareol();
prbuf(Line, length);
if (nchars < promptlen)
erasep(nchars); /* erasep () sets promptlen to 0 */
else
promptlen = 0;
/* is this needed?
* if (clreol)
* cleareol(); * must clear again in case we wrapped *
*/
if (nchars < Mcol || !fold_opt)
prbuf("\n", 1); /* will turn off UL if necessary */
if (nchars == STOP)
break;
num_lines--;
}
if (pstate) {
my_putstring(ULexit);
pstate = 0;
}
fflush(stdout);
if ((c = Getc(f)) == EOF) {
if (clreol)
clreos();
return;
}
if (Pause && clreol)
clreos();
Ungetc(c, f);
sigsetjmp(restore, 1);
Pause = 0;
startup = 0;
if ((num_lines = command(NULL, f)) == 0)
return;
if (hard && promptlen > 0)
erasep(0);
if (noscroll && num_lines >= dlines) {
if (clreol)
home();
else
doclear();
}
screen_start.line = Currline;
screen_start.chrctr = Ftell(f);
}
}
/* Come here if a quit signal is received */
void onquit(int dummy __attribute__((__unused__)))
{
signal(SIGQUIT, SIG_IGN);
if (!inwait) {
putchar('\n');
if (!startup) {
signal(SIGQUIT, onquit);
siglongjmp(restore, 1);
} else
Pause++;
} else if (!dum_opt && notell) {
promptlen += fprintf(stderr, _("[Use q or Q to quit]"));
notell = 0;
}
signal(SIGQUIT, onquit);
}
/* Come here if a signal for a window size change is received */
#ifdef SIGWINCH
void chgwinsz(int dummy __attribute__((__unused__)))
{
struct winsize win;
signal(SIGWINCH, SIG_IGN);
if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
if (win.ws_row != 0) {
Lpp = win.ws_row;
nscroll = Lpp / 2 - 1;
if (nscroll <= 0)
nscroll = 1;
dlines = Lpp - 1; /* was: Lpp - (noscroll ? 1 : 2) */
}
if (win.ws_col != 0)
Mcol = win.ws_col;
}
signal(SIGWINCH, chgwinsz);
}
#endif /* SIGWINCH */
/* Clean up terminal state and exit. Also come here if interrupt signal received */
void __attribute__((__noreturn__)) end_it(int dummy __attribute__((__unused__)))
{
reset_tty();
if (clreol) {
putchar('\r');
clreos();
fflush(stdout);
} else if (!clreol && (promptlen > 0)) {
kill_line();
fflush(stdout);
} else
putcerr('\n');
free(previousre);
free(Line);
_exit(EXIT_SUCCESS);
}
void copy_file(register FILE *f)
{
char buf[BUFSIZ];
size_t sz;
while ((sz = fread(&buf, sizeof(char), sizeof(buf), f)) > 0)
fwrite(&buf, sizeof(char), sz, stdout);
}
#define ringbell() putcerr('\007')
static void prompt(char *filename)
{
if (clreol)
cleareol();
else if (promptlen > 0)
kill_line();
if (!hard) {
promptlen = 0;
if (Senter && Sexit) {
my_putstring(Senter);
promptlen += (2 * soglitch);
}
if (clreol)
cleareol();
promptlen += printf(_("--More--"));
if (filename != NULL) {
promptlen += printf(_("(Next file: %s)"), filename);
} else if (!no_intty) {
promptlen +=
printf("(%d%%)",
(int)((file_pos * 100) / file_size));
}
if (dum_opt) {
promptlen +=
printf(_("[Press space to continue, 'q' to quit.]"));
}
if (Senter && Sexit)
my_putstring(Sexit);
if (clreol)
clreos();
fflush(stdout);
} else
ringbell();
inwait++;
}
void prepare_line_buffer(void)
{
char *nline;
size_t nsz = Mcol * 4;
if (LineLen >= nsz)
return;
if (nsz < LINSIZ)
nsz = LINSIZ;
/* alloc nsz and extra space for \n\0 */
nline = xrealloc(Line, nsz + 2);
Line = nline;
LineLen = nsz;
}
/* Get a logical line */
int get_line(register FILE *f, int *length)
{
int c;
char *p;
int column;
static int colflg;
#ifdef HAVE_WIDECHAR
size_t i;
wchar_t wc;
int wc_width;
mbstate_t state, state_bak; /* Current status of the stream. */
char mbc[MB_LEN_MAX]; /* Buffer for one multibyte char. */
size_t mblength; /* Byte length of multibyte char. */
size_t mbc_pos = 0; /* Position of the MBC. */
int use_mbc_buffer_flag = 0; /* If 1, mbc has data. */
int break_flag = 0; /* If 1, exit while(). */
long file_pos_bak = Ftell(f);
memset(&state, '\0', sizeof(mbstate_t));
#endif
prepare_line_buffer();
p = Line;
column = 0;
c = Getc(f);
if (colflg && c == '\n') {
Currline++;
c = Getc(f);
}
while (p < &Line[LineLen - 1]) {
#ifdef HAVE_WIDECHAR
if (fold_opt && use_mbc_buffer_flag && MB_CUR_MAX > 1) {
use_mbc_buffer_flag = 0;
state_bak = state;
mbc[mbc_pos++] = c;
process_mbc:
mblength = mbrtowc(&wc, mbc, mbc_pos, &state);
switch (mblength) {
case (size_t)-2: /* Incomplete multibyte character. */
use_mbc_buffer_flag = 1;
state = state_bak;
break;
case (size_t)-1: /* Invalid as a multibyte character. */
*p++ = mbc[0];
state = state_bak;
column++;
file_pos_bak++;
if (column >= Mcol) {
Fseek(f, file_pos_bak);
} else {
memmove(mbc, mbc + 1, --mbc_pos);
if (mbc_pos > 0) {
mbc[mbc_pos] = '\0';
goto process_mbc;
}
}
break;
default:
wc_width = wcwidth(wc);
if (column + wc_width > Mcol) {
Fseek(f, file_pos_bak);
break_flag = 1;
} else {
for (i = 0; p < &Line[LineLen - 1] &&
i < mbc_pos; i++)
*p++ = mbc[i];
if (wc_width > 0)
column += wc_width;
}
}
if (break_flag || column >= Mcol)
break;
c = Getc(f);
continue;
}
#endif /* HAVE_WIDECHAR */
if (c == EOF) {
if (p > Line) {
*p = '\0';
*length = p - Line;
return (column);
}
*length = p - Line;
return (EOF);
}
if (c == '\n') {
Currline++;
break;
}
*p++ = c;
#if 0
if (c == '\033') { /* ESC */
c = Getc(f);
while (c > ' ' && c < '0' && p < &Line[LineLen - 1]) {
*p++ = c;
c = Getc(f);
}
if (c >= '0' && c < '\177' && p < &Line[LineLen - 1]) {
*p++ = c;
c = Getc(f);
continue;
}
}
#endif /* 0 */
if (c == '\t') {
if (!hardtabs || (column < promptlen && !hard)) {
if (hardtabs && eraseln && !dumb) {
column = 1 + (column | 7);
my_putstring(eraseln);
promptlen = 0;
} else {
for (--p; p < &Line[LineLen - 1];) {
*p++ = ' ';
if ((++column & 7) == 0)
break;
}
if (column >= promptlen)
promptlen = 0;
}
} else
column = 1 + (column | 7);
} else if (c == '\b' && column > 0) {
column--;
} else if (c == '\r') {
int next = Getc(f);
if (next == '\n') {
p--;
Currline++;
break;
}
Ungetc(next, f);
column = 0;
} else if (c == '\f' && stop_opt) {
p[-1] = '^';
*p++ = 'L';
column += 2;
Pause++;
} else if (c == EOF) {
*length = p - Line;
return (column);
} else {
#ifdef HAVE_WIDECHAR
if (fold_opt && MB_CUR_MAX > 1) {
memset(mbc, '\0', MB_LEN_MAX);
mbc_pos = 0;