forked from Surachai-kent/util-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmore.c
More file actions
2135 lines (1986 loc) · 54.4 KB
/
more.c
File metadata and controls
2135 lines (1986 loc) · 54.4 KB
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 MERCHANTABILITY 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>
#include <stdarg.h>
#include <sys/param.h>
#include <ctype.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/ttydefaults.h>
#include <sys/wait.h>
#include <regex.h>
#include <assert.h>
#include <poll.h>
#include <sys/signalfd.h>
#include <paths.h>
#include <getopt.h>
#if defined(HAVE_NCURSESW_TERM_H)
# include <ncursesw/term.h>
#elif defined(HAVE_NCURSES_TERM_H)
# include <ncurses/term.h>
#elif defined(HAVE_TERM_H)
# include <term.h>
#endif
#ifdef HAVE_MAGIC
# include <magic.h>
#endif
#include "strutils.h"
#include "nls.h"
#include "xalloc.h"
#include "widechar.h"
#include "closestream.h"
#include "env.h"
#ifdef TEST_PROGRAM
# define NON_INTERACTIVE_MORE 1
#endif
#define BACKSPACE "\b"
#define CARAT "^"
#define ARROW_UP "\x1b\x5b\x41"
#define ARROW_DOWN "\x1b\x5b\x42"
#define PAGE_UP "\x1b\x5b\x35\x7e"
#define PAGE_DOWN "\x1b\x5b\x36\x7e"
#define MIN_LINE_SZ 256 /* minimal line_buf buffer size */
#define ESC '\033'
#define SCROLL_LEN 11
#define LINES_PER_PAGE 24
#define NUM_COLUMNS 80
#define TERMINAL_BUF 4096
#define INIT_BUF 80
#define COMMAND_BUF 200
#define REGERR_BUF NUM_COLUMNS
#define TERM_AUTO_RIGHT_MARGIN "am"
#define TERM_BACKSPACE "cub1"
#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_EXIT_STANDARD_MODE "rmso"
#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_STANDARD_MODE "smso"
#define TERM_STD_MODE_GLITCH "xmc"
/* Used in read_command() */
typedef enum {
more_kc_unknown_command,
more_kc_colon,
more_kc_repeat_previous,
more_kc_backwards,
more_kc_jump_lines_per_screen,
more_kc_set_lines_per_screen,
more_kc_set_scroll_len,
more_kc_quit,
more_kc_skip_forward_screen,
more_kc_skip_forward_line,
more_kc_next_line,
more_kc_clear_screen,
more_kc_previous_search_match,
more_kc_display_line,
more_kc_display_file_and_line,
more_kc_repeat_search,
more_kc_search,
more_kc_run_shell,
more_kc_help,
more_kc_next_file,
more_kc_previous_file,
more_kc_run_editor
} more_key_commands;
struct number_command {
unsigned int number;
more_key_commands key;
};
struct more_control {
struct termios output_tty; /* output terminal */
struct termios original_tty; /* original terminal settings */
FILE *current_file; /* currently open input file */
off_t file_position; /* file position */
off_t file_size; /* file size */
int argv_position; /* argv[] position */
int lines_per_screen; /* screen size in lines */
int d_scroll_len; /* number of lines scrolled by 'd' */
int prompt_len; /* message prompt length */
int current_line; /* line we are currently at */
int next_jump; /* number of lines to skip ahead */
char **file_names; /* The list of file names */
int num_files; /* Number of files left to process */
char *shell; /* name of the shell to use */
int sigfd; /* signalfd() file descriptor */
sigset_t sigset; /* signal operations */
char *line_buf; /* line buffer */
size_t line_sz; /* size of line_buf buffer */
int lines_per_page; /* lines per page */
char *clear; /* clear screen */
char *erase_line; /* erase line */
char *enter_std; /* enter standout mode */
char *exit_std; /* exit standout mode */
char *backspace_ch; /* backspace character */
char *go_home; /* go to home */
char *move_line_down; /* move line down */
char *clear_rest; /* clear rest of screen */
int num_columns; /* number of columns */
char *next_search; /* file beginning search string */
char *previous_search; /* previous search() buf[] item */
struct {
off_t row_num; /* row file position */
long line_num; /* line number */
} context,
screen_start;
unsigned int leading_number; /* number in front of key command */
struct number_command previous_command; /* previous key command */
char *shell_line; /* line to execute in subshell */
#ifdef HAVE_MAGIC
magic_t magic; /* libmagic database entries */
#endif
unsigned int
bad_stdout:1, /* true if overwriting does not turn off standout */
catch_suspend:1, /* we should catch the SIGTSTP signal */
clear_line_ends:1, /* do not scroll, paint each screen from the top */
clear_first:1, /* is first character in file \f */
dumb_tty:1, /* is terminal type known */
eat_newline:1, /* is newline ignored after 80 cols */
erase_input_ok:1, /* is erase input supported */
erase_previous_ok:1, /* is erase previous supported */
exit_on_eof:1, /* exit on EOF */
first_file:1, /* is the input file the first in list */
fold_long_lines:1, /* fold long lines */
hard_tabs:1, /* print spaces instead of '\t' */
hard_tty:1, /* is this hard copy terminal (a printer or such) */
leading_colon:1, /* key command has leading ':' character */
is_eof:1, /* EOF detected */
is_paused:1, /* is output paused */
no_quit_dialog:1, /* suppress quit dialog */
no_scroll:1, /* do not scroll, clear the screen and then display text */
no_tty_in:1, /* is input in interactive mode */
no_tty_out:1, /* is output in interactive mode */
no_tty_err:1, /* is stderr terminal */
print_banner:1, /* print file name banner */
reading_num:1, /* are we reading leading_number */
report_errors:1, /* is an error reported */
search_at_start:1, /* search pattern defined at start up */
search_called:1, /* previous more command was a search */
squeeze_spaces:1, /* suppress white space */
stdout_glitch:1, /* terminal has standout mode glitch */
stop_after_formfeed:1, /* stop after form feeds */
suppress_bell:1, /* suppress bell */
wrap_margin:1; /* set if automargins */
};
static void __attribute__((__noreturn__)) usage(void)
{
printf("%s", USAGE_HEADER);
printf(_(" %s [options] <file>...\n"), program_invocation_short_name);
printf("%s", USAGE_SEPARATOR);
printf("%s\n", _("Display the contents of a file in a terminal."));
printf("%s", USAGE_OPTIONS);
printf("%s\n", _(" -d, --silent display help instead of ringing bell"));
printf("%s\n", _(" -f, --logical count logical rather than screen lines"));
printf("%s\n", _(" -l, --no-pause suppress pause after form feed"));
printf("%s\n", _(" -c, --print-over do not scroll, display text and clean line ends"));
printf("%s\n", _(" -p, --clean-print do not scroll, clean screen and display text"));
printf("%s\n", _(" -e, --exit-on-eof exit on end-of-file"));
printf("%s\n", _(" -s, --squeeze squeeze multiple blank lines into one"));
printf("%s\n", _(" -u, --plain suppress underlining and bold"));
printf("%s\n", _(" -n, --lines <number> the number of lines per screenful"));
printf("%s\n", _(" -<number> same as --lines"));
printf("%s\n", _(" +<number> display file beginning from line number"));
printf("%s\n", _(" +/<pattern> display file beginning from pattern match"));
printf("%s", USAGE_SEPARATOR);
printf(USAGE_HELP_OPTIONS(23));
printf(USAGE_MAN_TAIL("more(1)"));
exit(EXIT_SUCCESS);
}
static void argscan(struct more_control *ctl, int as_argc, char **as_argv)
{
int c, opt;
static const struct option longopts[] = {
{ "silent", no_argument, NULL, 'd' },
{ "logical", no_argument, NULL, 'f' },
{ "no-pause", no_argument, NULL, 'l' },
{ "print-over", no_argument, NULL, 'c' },
{ "clean-print", no_argument, NULL, 'p' },
{ "exit-on-eof", no_argument, NULL, 'e' },
{ "squeeze", no_argument, NULL, 's' },
{ "plain", no_argument, NULL, 'u' },
{ "lines", required_argument, NULL, 'n' },
{ "version", no_argument, NULL, 'V' },
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
/* Take care of number option and +args. */
for (opt = 0; opt < as_argc; opt++) {
int move = 0;
if (as_argv[opt][0] == '-' && isdigit_string(as_argv[opt] + 1)) {
ctl->lines_per_screen =
strtos16_or_err(as_argv[opt], _("failed to parse number"));
ctl->lines_per_screen = abs(ctl->lines_per_screen);
move = 1;
} else if (as_argv[opt][0] == '+') {
if (isdigit_string(as_argv[opt] + 1)) {
ctl->next_jump = strtos32_or_err(as_argv[opt],
_("failed to parse number")) - 1;
move = 1;
} else if (as_argv[opt][1] == '/') {
free(ctl->next_search);
ctl->next_search = xstrdup(as_argv[opt] + 2);
ctl->search_at_start = 1;
move = 1;
}
}
if (move) {
as_argc = remote_entry(as_argv, opt, as_argc);
opt--;
}
}
while ((c = getopt_long(as_argc, as_argv, "dflcpsun:eVh", longopts, NULL)) != -1) {
switch (c) {
case 'd':
ctl->suppress_bell = 1;
break;
case 'l':
ctl->stop_after_formfeed = 0;
break;
case 'f':
ctl->fold_long_lines = 0;
break;
case 'p':
ctl->no_scroll = 1;
break;
case 'c':
ctl->clear_line_ends = 1;
break;
case 's':
ctl->squeeze_spaces = 1;
break;
case 'u':
break;
case 'n':
ctl->lines_per_screen = strtou16_or_err(optarg, _("argument error"));
break;
case 'e':
ctl->exit_on_eof = 1;
break;
case 'V':
print_version(EXIT_SUCCESS);
case 'h':
usage();
default:
errtryhelp(EXIT_FAILURE);
break;
}
}
ctl->num_files = as_argc - optind;
ctl->file_names = as_argv + optind;
}
static void env_argscan(struct more_control *ctl, const char *s)
{
char **env_argv;
int env_argc = 1;
int size = 8;
const char delim[] = { ' ', '\n', '\t', '\0' };
char *str = xstrdup(s);
char *key = NULL, *tok;
env_argv = xmalloc(sizeof(char *) * size);
env_argv[0] = _("MORE environment variable"); /* program name */
for (tok = strtok_r(str, delim, &key); tok; tok = strtok_r(NULL, delim, &key)) {
env_argv[env_argc++] = tok;
if (size < env_argc) {
size *= 2;
env_argv = xrealloc(env_argv, sizeof(char *) * size);
}
}
argscan(ctl, env_argc, env_argv);
/* Reset optind, command line parsing needs this. */
optind = 0;
free(str);
free(env_argv);
}
static void more_fseek(struct more_control *ctl, off_t pos)
{
ctl->file_position = pos;
fseeko(ctl->current_file, pos, SEEK_SET);
}
static int more_getc(struct more_control *ctl)
{
int ret = getc(ctl->current_file);
ctl->file_position = ftello(ctl->current_file);
return ret;
}
static int more_ungetc(struct more_control *ctl, int c)
{
int ret = ungetc(c, ctl->current_file);
ctl->file_position = ftello(ctl->current_file);
return ret;
}
static void print_separator(const int c, int n)
{
while (n--)
putchar(c);
putchar('\n');
}
/* check_magic -- check for file magic numbers. */
static int check_magic(struct more_control *ctl, char *fs)
{
#ifdef HAVE_MAGIC
const int fd = fileno(ctl->current_file);
const char *mime_encoding = magic_descriptor(ctl->magic, fd);
const char *magic_error_msg = magic_error(ctl->magic);
if (magic_error_msg) {
printf("%s: %s: %s\n", program_invocation_short_name,
_("magic failed"), magic_error_msg);
return 0;
}
if (!mime_encoding || !(strcmp("binary", mime_encoding))) {
printf(_("\n******** %s: Not a text file ********\n\n"), fs);
return 1;
}
#else
signed char twobytes[2];
/* don't try to look ahead if the input is unseekable */
if (fseek(ctl->current_file, 0L, SEEK_SET))
return 0;
if (fread(twobytes, 2, 1, ctl->current_file) == 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);
return 1;
}
}
fseek(ctl->current_file, 0L, SEEK_SET); /* rewind() not necessary */
#endif
return 0;
}
/* 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. */
static void checkf(struct more_control *ctl, char *fs)
{
struct stat st;
int c;
ctl->current_line = 0;
ctl->file_position = 0;
ctl->file_size = 0;
fflush(NULL);
ctl->current_file = fopen(fs, "r");
if (ctl->current_file == NULL) {
if (ctl->clear_line_ends)
putp(ctl->erase_line);
warn(_("cannot open %s"), fs);
return;
}
if (fstat(fileno(ctl->current_file), &st) != 0) {
warn(_("stat of %s failed"), fs);
return;
}
if ((st.st_mode & S_IFMT) == S_IFDIR) {
printf(_("\n*** %s: directory ***\n\n"), fs);
ctl->current_file = NULL;
return;
}
ctl->file_size = st.st_size;
if (0 < ctl->file_size && check_magic(ctl, fs)) {
fclose(ctl->current_file);
ctl->current_file = NULL;
return;
}
fcntl(fileno(ctl->current_file), F_SETFD, FD_CLOEXEC);
c = more_getc(ctl);
ctl->clear_first = (c == '\f');
more_ungetc(ctl, c);
}
static void prepare_line_buffer(struct more_control *ctl)
{
size_t sz = ctl->num_columns * 4;
if (ctl->line_sz >= sz)
return;
if (sz < MIN_LINE_SZ)
sz = MIN_LINE_SZ;
/* alloc sz and extra space for \n\0 */
ctl->line_buf = xrealloc(ctl->line_buf, sz + 2);
ctl->line_sz = sz;
}
/* Get a logical line */
static int get_line(struct more_control *ctl, int *length)
{
int c;
char *p;
int column;
static int column_wrap;
#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(). */
off_t file_position_bak = ctl->file_position;
memset(&state, '\0', sizeof(mbstate_t));
#endif
p = ctl->line_buf;
column = 0;
c = more_getc(ctl);
if (column_wrap && c == '\n') {
ctl->current_line++;
c = more_getc(ctl);
}
while (p < &ctl->line_buf[ctl->line_sz - 1]) {
#ifdef HAVE_WIDECHAR
if (ctl->fold_long_lines && 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_position_bak++;
if (column >= ctl->num_columns) {
more_fseek(ctl, file_position_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 > ctl->num_columns) {
more_fseek(ctl, file_position_bak);
break_flag = 1;
} else {
for (i = 0; p < &ctl->line_buf[ctl->line_sz - 1] &&
i < mbc_pos; i++)
*p++ = mbc[i];
if (wc_width > 0)
column += wc_width;
}
}
if (break_flag || column >= ctl->num_columns)
break;
c = more_getc(ctl);
continue;
}
#endif /* HAVE_WIDECHAR */
if (c == EOF) {
if (p > ctl->line_buf) {
*p = '\0';
*length = p - ctl->line_buf;
return column;
}
*length = p - ctl->line_buf;
return EOF;
}
if (c == '\n') {
ctl->current_line++;
break;
}
*p++ = c;
if (c == '\t') {
if (!ctl->hard_tabs || (column < ctl->prompt_len && !ctl->hard_tty)) {
if (ctl->hard_tabs && ctl->erase_line && !ctl->dumb_tty) {
column = 1 + (column | 7);
putp(ctl->erase_line);
ctl->prompt_len = 0;
} else {
for (--p; p < &ctl->line_buf[ctl->line_sz - 1];) {
*p++ = ' ';
if ((++column & 7) == 0)
break;
}
if (column >= ctl->prompt_len)
ctl->prompt_len = 0;
}
} else
column = 1 + (column | 7);
} else if (c == '\b' && column > 0) {
column--;
} else if (c == '\r') {
int next = more_getc(ctl);
if (next == '\n') {
p--;
ctl->current_line++;
break;
}
more_ungetc(ctl, c);
column = 0;
} else if (c == '\f' && ctl->stop_after_formfeed) {
p[-1] = '^';
*p++ = 'L';
column += 2;
ctl->is_paused = 1;
} else if (c == EOF) {
*length = p - ctl->line_buf;
return column;
} else {
#ifdef HAVE_WIDECHAR
if (ctl->fold_long_lines && MB_CUR_MAX > 1) {
memset(mbc, '\0', MB_LEN_MAX);
mbc_pos = 0;
mbc[mbc_pos++] = c;
state_bak = state;
mblength = mbrtowc(&wc, mbc, mbc_pos, &state);
/* The value of mblength is always less than 2 here. */
switch (mblength) {
case (size_t)-2:
p--;
file_position_bak = ctl->file_position - 1;
state = state_bak;
use_mbc_buffer_flag = 1;
break;
case (size_t)-1:
state = state_bak;
column++;
break;
default:
wc_width = wcwidth(wc);
if (wc_width > 0)
column += wc_width;
}
} else
#endif /* HAVE_WIDECHAR */
{
if (isprint(c))
column++;
}
}
if (column >= ctl->num_columns && ctl->fold_long_lines)
break;
#ifdef HAVE_WIDECHAR
if (use_mbc_buffer_flag == 0 && p >= &ctl->line_buf[ctl->line_sz - 1 - 4])
/* don't read another char if there is no space for
* whole multibyte sequence */
break;
#endif
c = more_getc(ctl);
}
if (column >= ctl->num_columns && ctl->num_columns > 0) {
if (!ctl->wrap_margin) {
*p++ = '\n';
}
}
column_wrap = column == ctl->num_columns && ctl->fold_long_lines;
if (column_wrap && ctl->eat_newline && ctl->wrap_margin) {
*p++ = '\n'; /* simulate normal wrap */
}
*length = p - ctl->line_buf;
*p = 0;
return column;
}
/* Erase the rest of the prompt, assuming we are starting at column col. */
static void erase_to_col(struct more_control *ctl, int col)
{
if (ctl->prompt_len == 0)
return;
if (col == 0 && ctl->clear_line_ends)
puts(ctl->erase_line);
else if (ctl->hard_tty)
putchar('\n');
else {
if (col == 0)
putchar('\r');
if (!ctl->dumb_tty && ctl->erase_line)
putp(ctl->erase_line);
else {
printf("%*s", ctl->prompt_len - col, "");
if (col == 0)
putchar('\r');
}
}
ctl->prompt_len = col;
}
static void output_prompt(struct more_control *ctl, char *filename)
{
if (ctl->clear_line_ends)
putp(ctl->erase_line);
else if (ctl->prompt_len > 0)
erase_to_col(ctl, 0);
if (!ctl->hard_tty) {
ctl->prompt_len = 0;
if (ctl->enter_std) {
putp(ctl->enter_std);
ctl->prompt_len += (2 * ctl->stdout_glitch);
}
if (ctl->clear_line_ends)
putp(ctl->erase_line);
ctl->prompt_len += printf(_("--More--"));
if (filename != NULL) {
ctl->prompt_len += printf(_("(Next file: %s)"), filename);
} else if (!ctl->no_tty_in && 0 < ctl->file_size) {
int position = ((ctl->file_position * 100) / ctl->file_size);
if (position == 100) {
erase_to_col(ctl, 0);
ctl->prompt_len += printf(_("(END)"));
} else {
ctl->prompt_len += printf("(%d%%)", position);
}
} else if (ctl->is_eof) {
erase_to_col(ctl, 0);
ctl->prompt_len += printf(_("(END)"));
}
if (ctl->suppress_bell) {
ctl->prompt_len +=
printf(_("[Press space to continue, 'q' to quit.]"));
}
if (ctl->exit_std)
putp(ctl->exit_std);
if (ctl->clear_line_ends)
putp(ctl->clear_rest);
} else
fprintf(stderr, "\a");
fflush(NULL);
}
static void reset_tty(struct more_control *ctl)
{
if (ctl->no_tty_out)
return;
fflush(NULL);
ctl->output_tty.c_lflag |= ICANON | ECHO;
ctl->output_tty.c_cc[VMIN] = ctl->original_tty.c_cc[VMIN];
ctl->output_tty.c_cc[VTIME] = ctl->original_tty.c_cc[VTIME];
tcsetattr(STDERR_FILENO, TCSANOW, &ctl->original_tty);
}
/* Clean up terminal state and exit. Also come here if interrupt signal received */
static void __attribute__((__noreturn__)) more_exit(struct more_control *ctl)
{
#ifdef HAVE_MAGIC
magic_close(ctl->magic);
#endif
reset_tty(ctl);
if (ctl->clear_line_ends) {
putchar('\r');
putp(ctl->erase_line);
} else if (!ctl->clear_line_ends && (ctl->prompt_len > 0))
erase_to_col(ctl, 0);
fflush(NULL);
free(ctl->previous_search);
free(ctl->shell_line);
free(ctl->line_buf);
free(ctl->go_home);
if (ctl->current_file)
fclose(ctl->current_file);
del_curterm(cur_term);
_exit(EXIT_SUCCESS);
}
static cc_t read_user_input(struct more_control *ctl)
{
cc_t c;
errno = 0;
/*
* Key commands can be read() from either stderr or stdin. If they
* are read from stdin such as 'cat file.txt | more' then the pipe
* input is understood as series key commands - and that is not
* wanted. Keep the read() reading from stderr.
*/
if (read(STDERR_FILENO, &c, 1) <= 0) {
if (errno != EINTR)
more_exit(ctl);
else
c = ctl->output_tty.c_cc[VKILL];
}
return c;
}
/* Read a number and command from the terminal. Set cmd to the non-digit
* which terminates the number. */
static struct number_command read_command(struct more_control *ctl)
{
cc_t input[8] = { 0 };
ssize_t i, ilen;
struct number_command cmd = { .key = more_kc_unknown_command };
/* See stderr note in read_user_input() */
if ((ilen = read(STDERR_FILENO, &input, sizeof(input))) <= 0)
return cmd;
if (2 < ilen) {
if (!memcmp(input, ARROW_UP, sizeof(ARROW_UP))) {
cmd.key = more_kc_backwards;
return cmd;
} else if (!memcmp(input, ARROW_DOWN, sizeof(ARROW_DOWN))) {
cmd.key = more_kc_jump_lines_per_screen;
return cmd;
} else if (!memcmp(input, PAGE_UP, sizeof(PAGE_UP))) {
cmd.key = more_kc_backwards;
return cmd;
} else if (!memcmp(input, PAGE_DOWN, sizeof(PAGE_DOWN))) {
cmd.key = more_kc_jump_lines_per_screen;
return cmd;
}
}
for (i = 0; i < ilen; i++) {
if (isdigit(input[i])) {
if (0 < ctl->reading_num) {
ctl->leading_number *= 10;
ctl->leading_number += input[i] - '0';
} else
ctl->leading_number = input[i] - '0';
ctl->reading_num = 1;
continue;
}
cmd.number = ctl->leading_number;
ctl->reading_num = 0;
ctl->leading_number = 0;
if (ctl->leading_colon) {
ctl->leading_colon = 0;
switch (input[i]) {
case 'f':
cmd.key = more_kc_display_file_and_line;
return cmd;
case 'n':
cmd.key = more_kc_next_file;
return cmd;
case 'p':
cmd.key = more_kc_previous_file;
return cmd;
default:
cmd.key = more_kc_unknown_command;
return cmd;
}
}
/* command is a single char */
switch (input[i]) {
case '.':
cmd.key = more_kc_repeat_previous;
break;
case ':':
ctl->leading_colon = 1;
break;
case 'b':
case CTRL('B'):
cmd.key = more_kc_backwards;
break;
case ' ':
cmd.key = more_kc_jump_lines_per_screen;
break;
case 'z':
cmd.key = more_kc_set_lines_per_screen;
break;
case 'd':
case CTRL('D'):
cmd.key = more_kc_set_scroll_len;
break;
case 'q':
case 'Q':
cmd.key = more_kc_quit;
return cmd;
break;
case 'f':
case CTRL('F'):
cmd.key = more_kc_skip_forward_screen;
break;
case 's':
cmd.key = more_kc_skip_forward_line;
break;
case '\n':
cmd.key = more_kc_next_line;
break;
case '\f':
cmd.key = more_kc_clear_screen;
break;
case '\'':
cmd.key = more_kc_previous_search_match;
break;
case '=':
cmd.key = more_kc_display_line;
break;
case 'n':
cmd.key = more_kc_repeat_search;
break;
case '/':
cmd.key = more_kc_search;
break;
case '!':
cmd.key = more_kc_run_shell;
break;
case '?':
case 'h':
cmd.key = more_kc_help;
break;
case 'v':
cmd.key = more_kc_run_editor;
break;
}
}
return cmd;
}
/* Change displayed file from command line list to next nskip, where nskip
* is relative position in argv and can be negative, that is a previous
* file. */
static void change_file(struct more_control *ctl, int nskip)
{
if (nskip == 0)
return;
if (nskip > 0) {
if (ctl->argv_position + nskip > ctl->num_files - 1)
nskip = ctl->num_files - ctl->argv_position - 1;
}
ctl->argv_position += nskip;
if (ctl->argv_position < 0)
ctl->argv_position = 0;
puts(_("\n...Skipping "));
if (ctl->clear_line_ends)
putp(ctl->erase_line);
if (nskip > 0)
fputs(_("...Skipping to file "), stdout);
else
fputs(_("...Skipping back to file "), stdout);
puts(ctl->file_names[ctl->argv_position]);
if (ctl->clear_line_ends)
putp(ctl->erase_line);
putchar('\n');
ctl->argv_position--;
}
static void show(struct more_control *ctl, char c)
{
if ((c < ' ' && c != '\n' && c != ESC) || c == CERASE) {
c += (c == CERASE) ? -0100 : 0100;
fputs(CARAT, stderr);
ctl->prompt_len++;
}
fputc(c, stderr);
ctl->prompt_len++;
}
static void more_error(struct more_control *ctl, char *mess)
{
if (ctl->clear_line_ends)
putp(ctl->erase_line);
else
erase_to_col(ctl, 0);
ctl->prompt_len += strlen(mess);
if (ctl->enter_std)
putp(ctl->enter_std);
fputs(mess, stdout);
if (ctl->exit_std)
putp(ctl->exit_std);
fflush(NULL);
ctl->report_errors++;
}
static void erase_one_column(struct more_control *ctl)
{
if (ctl->erase_previous_ok)
fprintf(stderr, "%s ", ctl->backspace_ch);
fputs(ctl->backspace_ch, stderr);
}
static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
{
char *sp;
cc_t c;