summaryrefslogtreecommitdiffstats
path: root/text-utils/more.c
blob: 3855d8549cfb256fb8669e0b223a24859baa751c (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
/*
 * 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 "rpmatch.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,
	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 */
		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_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 */
		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", _("A file perusal filter for CRT viewing."));

	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", _(" -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' },
		{ "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':	/* ignored silently to be posix compliant */
			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);
	struct stat st;

	if (magic_error_msg) {
		printf(_("magic failed: %s\n"), magic_error_msg);
		return 0;
	}
	if (fstat(fd, &st)) {
		warn(_("cannot stat %s"), fs);
		return 1;
	}
	if (st.st_size == 0) {
		/* libmagic tells an empty file has binary encoding */
		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;
	fflush(NULL);
	if (((ctl->current_file = fopen(fs, "r")) == NULL) ||
	    (fstat(fileno(ctl->current_file), &st) != 0)) {
		if (ctl->clear_line_ends)
			putp(ctl->erase_line);
		warn(_("cannot open %s"), fs);
		return;
	}
#ifndef HAVE_MAGIC
	if ((st.st_mode & S_IFMT) == S_IFDIR) {
		printf(_("\n*** %s: directory ***\n\n"), fs);
		ctl->current_file = NULL;
		return;
	}
#endif
	if (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);
	if ((ctl->file_size = st.st_size) == 0)
		ctl->file_size = ~((off_t)0);
}

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) {
			ctl->prompt_len +=
			    printf("(%d%%)",
				   (int)((ctl->file_position * 100) / ctl->file_size));
		}
		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_skip_forward;
			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_skip_forward;
			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;
			break;
		case 'f':
		case 's':
		case CTRL('F'):
			cmd.key = more_kc_skip_forward;
			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;
	int slash = 0;
	int maxlen;

	sp = buf;
	maxlen = 0;
	while (sp - buf < nmax) {
		if (ctl->prompt_len > maxlen)
			maxlen = ctl->prompt_len;
		c = read_user_input(ctl);
		if (c == '\\') {
			slash++;
		} else if (c == ctl->output_tty.c_cc[VERASE] && !slash) {
			if (sp > buf) {
#ifdef HAVE_WIDECHAR
				if (MB_CUR_MAX > 1) {
					wchar_t wc;
					size_t pos = 0, mblength;
					mbstate_t state, state_bak;

					memset(&state, '\0', sizeof(mbstate_t));

					while (1) {
						state_bak = state;
						mblength =
						    mbrtowc(&wc, buf + pos,
							    sp - buf, &state);

						switch (mblength) {
						case (size_t)-2:
						case (size_t)-1:
							state = state_bak;
							/* fallthrough */
						case 0:
							mblength = 1;
						}
						if (buf + pos + mblength >= sp)
							break;

						pos += mblength;
					}

					if (mblength == 1) {
						erase_one_column(ctl);
					} else {
						int wc_width;
						wc_width = wcwidth(wc);
						wc_width =
						    (wc_width <
						     1) ? 1 : wc_width;
						while (wc_width--) {
							erase_one_column(ctl);
						}
					}

					while (mblength--) {
						--ctl->prompt_len;
						--sp;
					}
				} else
#endif	/* HAVE_WIDECHAR */
				{
					--ctl->prompt_len;
					erase_one_column(ctl);
					--sp;
				}

				if ((*sp < ' ' && *sp != '\n') || *sp == CERASE) {
					--ctl->prompt_len;
					erase_one_column(ctl);
				}
				continue;
			}
			if (!ctl->erase_line)
				ctl->prompt_len = maxlen;
		} else if (c == ctl->output_tty.c_cc[VKILL] && !slash) {
			if (ctl->hard_tty) {
				show(ctl, c);
				putchar('\n');
				putchar(pchar);
			} else {
				putchar('\r');
				putchar(pchar);
				if (ctl->erase_line)
					erase_to_col(ctl, 1);
				else if (ctl->erase_input_ok)
					while (ctl->prompt_len-- > 1)
						fprintf(stderr, "%s %s", ctl->backspace_ch, ctl->backspace_ch);
				ctl->prompt_len = 1;
			}
			sp = buf;
			fflush(NULL);
			continue;
		}
		if (slash && (c == ctl->output_tty.c_cc[VKILL] ||
			      c == ctl->output_tty.c_cc[VERASE])) {
			erase_one_column(ctl);
			--sp;
		}
		if (c != '\\')
			slash = 0;
		*sp++ = c;
		if ((c < ' ' && c != '\n' && c != ESC) || c == CERASE) {
			c += (c == CERASE) ? -0100 : 0100;
			fputs(CARAT, stderr);
			ctl->prompt_len++;
		}
		if (c != '\n' && c != ESC) {
			fputc(c, stderr);
			ctl->prompt_len++;
		} else
			break;
	}
	*--sp = '\0';
	if (!ctl->erase_line)
		ctl->prompt_len = maxlen;
	if (sp - buf >= nmax - 1)
		more_error(ctl, _("Line too long"));
}

/* Expand shell command line. */
static void expand(struct more_control *ctl, char *inbuf)
{
	char *inpstr;
	char *outstr;
	char c;
	char *temp;
	int tempsz, xtra, offset;

	xtra = strlen(ctl->file_names[ctl->argv_position]) + strlen(ctl->shell_line) + 1;
	tempsz = COMMAND_BUF + xtra;
	temp = xmalloc(tempsz);
	inpstr = inbuf;
	outstr = temp;
	while ((c = *inpstr++) != '\0') {
		offset = outstr - temp;
		if (tempsz - offset - 1 < xtra) {
			tempsz += COMMAND_BUF + xtra;
			temp = xrealloc(temp, tempsz);
			outstr = temp + offset;
		}
		switch (c) {
		case '%':
			if (!ctl->no_tty_in) {
				strcpy(outstr, ctl->file_names[ctl->argv_position]);
				outstr += strlen(ctl->file_names[ctl->argv_position]);
			} else
				*outstr++ = c;
			break;
		case '!':
			if (ctl->shell_line) {
				strcpy(outstr, ctl->shell_line);
				outstr += strlen(ctl->shell_line);
			} else
				more_error(ctl, _
					   ("No previous command to substitute for"));
			break;
		case '\\':
			if (*inpstr == '%' || *inpstr == '!') {
				*outstr++ = *inpstr++;
				break;
			}
			/* fallthrough */
		default:
			*outstr++ = c;
		}
	}
	*outstr++ = '\0';
	free(ctl->shell_line);
	ctl->shell_line = temp;
}

static void set_tty(struct more_control *ctl)
{
	ctl->output_tty.c_lflag &= ~(ICANON | ECHO);
	ctl->output_tty.c_cc[VMIN] = 1;	/* read at least 1 char */
	ctl->output_tty.c_cc[VTIME] = 0;	/* no timeout */
	tcsetattr(STDERR_FILENO, TCSANOW, &ctl->output_tty);
}

/* Come here if a quit signal is received */
static void sigquit_handler(struct more_control *ctl)
{
	if (!ctl->dumb_tty && ctl->no_quit_dialog) {
		ctl->prompt_len += fprintf(stderr, _("[Use q or Q to quit]"));
		ctl->no_quit_dialog = 0;
	} else
		more_exit(ctl);
}

/* Come here when we get a suspend signal from the terminal */
static void sigtstp_handler(struct more_control *ctl)
{
	reset_tty(ctl);
	fflush(NULL);
	kill(getpid(), SIGSTOP);
}

/* Come here when we get a continue signal from the terminal */
static void sigcont_handler(struct more_control *ctl)
{
	set_tty(ctl);
}

/* Come here if a signal for a window size change is received */
static void sigwinch_handler(struct more_control *ctl)
{
	struct winsize win;

	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1) {
		if (win.ws_row != 0) {
			ctl->lines_per_page = win.ws_row;
			ctl->d_scroll_len = ctl->lines_per_page / 2 - 1;
			if (ctl->d_scroll_len < 1)
				ctl->d_scroll_len = 1;
			ctl->lines_per_screen = ctl->lines_per_page - 1;
		}
		if (win.ws_col != 0)
			ctl->num_columns = win.ws_col;
	}
	prepare_line_buffer(ctl);
}

static void execute(struct more_control *ctl, char *filename, char *cmd, ...)
{
	pid_t id;
	va_list argp;
	char *arg;
	char **args;
	int argcount;

	fflush(NULL);
	id = fork();
	if (id == 0) {
		int errsv;
		if (!isatty(STDIN_FILENO)) {
			close(STDIN_FILENO);
			open("/dev/tty", 0);
		}
		reset_tty(ctl);

		va_start(argp, cmd);
		arg = va_arg(argp, char *);
		argcount = 0;
		while (arg) {
			argcount++;
			arg = va_arg(argp, char *);
		}
		va_end(argp);

		args = alloca(sizeof(char *) * (argcount + 1));
		args[argcount] = NULL;

		va_start(argp, cmd);
		arg = va_arg(argp, char *);
		argcount = 0;
		while (arg) {
			args[argcount] = arg;
			argcount++;
			arg = va_arg(argp, char *);
		}
		va_end(argp);

		if (geteuid() != getuid() || getegid() != getgid()) {
			if (setuid(getuid()) < 0)
				err(EXIT_FAILURE, _("setuid failed"));
			if (setgid(getgid()) < 0)
				err(EXIT_FAILURE, _("setgid failed"));
		}

		execvp(cmd, args);
		errsv = errno;
		fputs(_("exec failed\n"), stderr);
		exit(errsv == ENOENT ? EX_EXEC_ENOENT : EX_EXEC_FAILED);
	}
	if (id > 0) {
		errno = 0;
		while (wait(NULL) > 0) {
			if (errno == EINTR)
				continue;
		}
	} else
		fputs(_("can't fork\n"), stderr);
	set_tty(ctl);
	print_separator('-', 24);
	output_prompt(ctl, filename);
}

static void run_shell(struct more_control *ctl, char *filename)
{
	char cmdbuf[COMMAND_BUF];

	erase_to_col(ctl, 0);
	putchar('!');
	fflush(NULL);
	if (ctl->previous_command.key == more_kc_run_shell && ctl->shell_line)
		fputs(ctl->shell_line, stdout);
	else {
		ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '!');
		if (strpbrk(cmdbuf, "%!\\"))
			expand(ctl, cmdbuf);
		else {
			free(ctl->shell_line);
			ctl->shell_line = xstrdup(cmdbuf);
		}
	}
	fputc('\n', stderr);
	fflush(NULL);
	ctl->prompt_len = 0;
	execute(ctl, filename, ctl->shell, ctl->shell, "-c", ctl->shell_line, 0);
}

/* Skip n lines in the file f */
static void skip_lines(struct more_control *ctl)
{
	int c;

	while (ctl->next_jump > 0) {
		while ((c = more_getc(ctl)) != '\n')
			if (c == EOF)
				return;
		ctl->next_jump--;
		ctl->current_line++;
	}
}

/*  Clear the screen */
static void more_clear_screen(struct more_control *ctl)
{
	if (ctl->clear && !ctl->hard_tty) {
		putp(ctl->clear);
		/* Put out carriage return so that system doesn't get
		 * confused by escape sequences when expanding tabs */
		putchar('\r');
		ctl->prompt_len = 0;
	}
}

static void read_line(struct more_control *ctl)
{
	int c;
	char *p;

	p = ctl->line_buf;
	while ((c = more_getc(ctl)) != '\n' && c != EOF
	       && (ptrdiff_t)p != (ptrdiff_t)(ctl->line_buf + ctl->line_sz - 1))
		*p++ = c;
	if (c == '\n')
		ctl->current_line++;
	*p = '\0';
}

static int more_poll(struct more_control *ctl, int timeout)
{
	struct pollfd pfd[2];

	pfd[0].fd = ctl->sigfd;
	pfd[0].events = POLLIN | POLLERR | POLLHUP;
	pfd[1].fd = STDIN_FILENO;
	pfd[1].events = POLLIN;

	if (poll(pfd, 2, timeout) < 0) {
		if (errno == EAGAIN)
			return 1;
		more_error(ctl, _("poll failed"));
		return 1;
	}
	if (pfd[0].revents != 0) {
		struct signalfd_siginfo info;
		ssize_t sz;

		sz = read(pfd[0].fd, &info, sizeof(info));
		assert(sz == sizeof(info));
		switch (info.ssi_signo) {
		case SIGINT:
			more_exit(ctl);
			break;
		case SIGQUIT:
			sigquit_handler(ctl);
			break;
		case SIGTSTP:
			sigtstp_handler(ctl);
			break;
		case SIGCONT:
			sigcont_handler(ctl);
			break;
		case SIGWINCH:
			sigwinch_handler(ctl);
			break;
		default:
			abort();
		}
	}
	if (pfd[1].revents == 0)
		return 1;
	return 0;
}

/* Search for nth occurrence of regular expression contained in buf in
 * the file */
static void search(struct more_control *ctl, char buf[], int n)
{
	off_t startline = ctl->file_position;
	off_t line1 = startline;
	off_t line2 = startline;
	off_t line3;
	int lncount;
	int saveln, rc;
	regex_t re;

	if (buf != ctl->previous_search) {
		free(ctl->previous_search);
		ctl->previous_search = buf;
	}

	ctl->search_called = 1;
	ctl->context.line_num = saveln = ctl->current_line;
	ctl->context.row_num = startline;
	lncount = 0;
	if (!buf)
		goto notfound;
	if ((rc = regcomp(&re, buf, REG_NOSUB)) != 0) {
		char s[REGERR_BUF];
		regerror(rc, &re, s, sizeof s);
		more_error(ctl, s);
		return;
	}
	while (!feof(ctl->current_file)) {
		line3 = line2;
		line2 = line1;
		line1 = ctl->file_position;
		read_line(ctl);
		lncount++;
		if (regexec(&re, ctl->line_buf, 0, NULL, 0) == 0 && --n == 0) {
			if ((1 < lncount && ctl->no_tty_in) || 3 < lncount) {
				putchar('\n');
				if (ctl->clear_line_ends)
					putp(ctl->erase_line);
				fputs(_("...skipping\n"), stdout);
			}
			if (!ctl->no_tty_in) {
				ctl->current_line -= (lncount < 3 ? lncount : 3);
				more_fseek(ctl, line3);
				if (ctl->no_scroll) {
					if (ctl->clear_line_ends) {
						putp(ctl->go_home);
						putp(ctl->erase_line);
					} else
						more_clear_screen(ctl);
				}
			} else {
				erase_to_col(ctl, 0);
				if (ctl->no_scroll) {
					if (ctl->clear_line_ends) {
						putp(ctl->go_home);
						putp(ctl->erase_line);
					} else
						more_clear_screen(ctl);
				}
				puts(ctl->line_buf);
			}
			break;
		}
		more_poll(ctl, 1);
	}
	/* Move ctrl+c signal handling back to more_key_command(). */
	signal(SIGINT, SIG_DFL);
	sigaddset(&ctl->sigset, SIGINT);
	sigprocmask(SIG_BLOCK, &ctl->sigset, NULL);
	regfree(&re);
	if (feof(ctl->current_file)) {
		if (!ctl->no_tty_in) {
			ctl->current_line = saveln;
			more_fseek(ctl, startline);
		} else {
			fputs(_("\nPattern not found\n"), stdout);
			more_exit(ctl);
		}
notfound:
		more_error(ctl, _("Pattern not found"));
	}
}

static char *find_editor(void)
{
	static char *editor;

	editor = getenv("VISUAL");
	if (editor == NULL || *editor == '\0')
		editor = getenv("EDITOR");
	if (editor == NULL || *editor == '\0')
		editor = _PATH_VI;
	return editor;
}

static void runtime_usage(void)
{
	fputs(_("Most commands optionally preceded by integer argument k.  "
		"Defaults in brackets.\n"
		"Star (*) indicates argument becomes new default.\n"), stdout);
	print_separator('-', 79);
	fprintf(stdout,
		_
		("<space>                 Display next k lines of text [current screen size]\n"
		 "z                       Display next k lines of text [current screen size]*\n"
		 "<return>                Display next k lines of text [1]*\n"
		 "d or ctrl-D             Scroll k lines [current scroll size, initially 11]*\n"
		 "q or Q or <interrupt>   Exit from more\n"
		 "s                       Skip forward k lines of text [1]\n"
		 "f                       Skip forward k screenfuls of text [1]\n"
		 "b or ctrl-B             Skip backwards k screenfuls of text [1]\n"
		 "'                       Go to place where previous search started\n"
		 "=                       Display current line number\n"
		 "/<regular expression>   Search for kth occurrence of regular expression [1]\n"
		 "n                       Search for kth occurrence of last r.e [1]\n"
		 "!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"
		 "v                       Start up '%s' at current line\n"
		 "ctrl-L                  Redraw screen\n"
		 ":n                      Go to kth next file [1]\n"
		 ":p                      Go to kth previous file [1]\n"
		 ":f                      Display current file name and line number\n"
		 ".                       Repeat previous command\n"),
		find_editor());
	print_separator('-', 79);
}

static void execute_editor(struct more_control *ctl, char *cmdbuf, char *filename)
{
	char *editor, *p;
	int split = 0;
	int n;

	if ((ctl->current_line - ctl->lines_per_screen) < 1)
		n = 1;
	else
		n = ctl->current_line - (ctl->lines_per_screen + 1) / 2;
	editor = find_editor();
	p = strrchr(editor, '/');
	if (p)
		p++;
	else
		p = editor;
	/*
	 * Earlier: call vi +n file. This also works for emacs.
	 * POSIX: call vi -c n file (when editor is vi or ex).
	 */
	if (!strcmp(p, "vi") || !strcmp(p, "ex")) {
		sprintf(cmdbuf, "-c %d", n);
		split = 1;
	} else
		sprintf(cmdbuf, "+%d", n);

	erase_to_col(ctl, 0);
	printf("%s %s %s", editor, cmdbuf, ctl->file_names[ctl->argv_position]);
	if (split) {
		cmdbuf[2] = 0;
		execute(ctl, filename, editor, editor,
			cmdbuf, cmdbuf + 3,
			ctl->file_names[ctl->argv_position], (char *)0);
	} else
		execute(ctl, filename, editor, editor,
			cmdbuf, ctl->file_names[ctl->argv_position], (char *)0);
}

static int skip_backwards(struct more_control *ctl, int nlines)
{
	if (nlines == 0)
		nlines++;
	erase_to_col(ctl, 0);
	printf(P_("...back %d page", "...back %d pages", nlines), nlines);
	putchar('\n');
	ctl->next_jump = ctl->current_line - (ctl->lines_per_screen * (nlines + 1)) - 1;
	if (ctl->next_jump < 0)
		ctl->next_jump = 0;
	more_fseek(ctl, 0);
	ctl->current_line = 0;
	skip_lines(ctl);
	return ctl->lines_per_screen;
}

static int skip_forwards(struct more_control *ctl, int nlines, cc_t comchar)
{
	int c;

	if (nlines == 0)
		nlines++;
	if (comchar == 'f')
		nlines *= ctl->lines_per_screen;
	putchar('\r');
	erase_to_col(ctl, 0);
	putchar('\n');
	if (ctl->clear_line_ends)
		putp(ctl->erase_line);
	printf(P_("...skipping %d line",
		  "...skipping %d lines", nlines), nlines);

	if (ctl->clear_line_ends)
		putp(ctl->erase_line);
	putchar('\n');

	while (nlines > 0) {
		while ((c = more_getc(ctl)) != '\n')
			if (c == EOF)
				return 0;
		ctl->current_line++;
		nlines--;
	}
	return 1;
}

/* Read a command and do it.  A command consists of an optional integer
 * argument followed by the command character.  Return the number of
 * lines to display in the next screenful.  If there is nothing more to
 * display in the current file, zero is returned. */
static int more_key_command(struct more_control *ctl, char *filename)
{
	int retval = 0;
	int done = 0, search_again = 0;
	char cmdbuf[INIT_BUF];
	struct number_command cmd;

	if (!ctl->report_errors)
		output_prompt(ctl, filename);
	else
		ctl->report_errors = 0;
	ctl->search_called = 0;
	for (;;) {
		if (more_poll(ctl, -1) != 0)
			continue;
		cmd = read_command(ctl);
		if (cmd.key == more_kc_unknown_command)
			continue;
		if (cmd.key == more_kc_repeat_previous)
			cmd = ctl->previous_command;
		switch (cmd.key) {
		case more_kc_backwards:
			if (ctl->no_tty_in) {
				fprintf(stderr, "\a");
				return -1;
			}
			retval = skip_backwards(ctl, cmd.number);
			done = 1;
			break;
		case more_kc_jump_lines_per_screen:
		case more_kc_set_lines_per_screen:
			if (cmd.number == 0)
				cmd.number = ctl->lines_per_screen;
			else if (cmd.key == more_kc_set_lines_per_screen)
				ctl->lines_per_screen = cmd.number;
			retval = cmd.number;
			done = 1;
			break;
		case more_kc_set_scroll_len:
			if (cmd.number != 0)
				ctl->d_scroll_len = cmd.number;
			retval = ctl->d_scroll_len;
			done = 1;
			break;
		case more_kc_quit:
			more_exit(ctl);
		case more_kc_skip_forward:
			if (skip_forwards(ctl, cmd.number, cmd.number))
				retval = ctl->lines_per_screen;
			done = 1;
			break;
		case more_kc_next_line:
			if (cmd.number != 0)
				ctl->lines_per_screen = cmd.number;
			else
				cmd.number = 1;
			retval = cmd.number;
			done = 1;
			break;
		case more_kc_clear_screen:
			if (!ctl->no_tty_in) {
				more_clear_screen(ctl);
				more_fseek(ctl, ctl->screen_start.row_num);
				ctl->current_line = ctl->screen_start.line_num;
				retval = ctl->lines_per_screen;
				done = 1;
				break;
			} else {
				fprintf(stderr, "\a");
				break;
			}
		case more_kc_previous_search_match:
			if (!ctl->no_tty_in) {
				erase_to_col(ctl, 0);
				fputs(_("\n***Back***\n\n"), stdout);
				more_fseek(ctl, ctl->context.row_num);
				ctl->current_line = ctl->context.line_num;
				retval = ctl->lines_per_screen;
				done = 1;
				break;
			} else {
				fprintf(stderr, "\a");
				break;
			}
		case more_kc_display_line:
			erase_to_col(ctl, 0);
			ctl->prompt_len = printf("%d", ctl->current_line);
			fflush(NULL);
			break;
		case more_kc_display_file_and_line:
			erase_to_col(ctl, 0);
			if (!ctl->no_tty_in)
				ctl->prompt_len =
				    printf(_("\"%s\" line %d"),
				           ctl->file_names[ctl->argv_position], ctl->current_line);
			else
				ctl->prompt_len = printf(_("[Not a file] line %d"),
							 ctl->current_line);
			fflush(NULL);
			break;
		case more_kc_repeat_search:
			if (!ctl->previous_search) {
				more_error(ctl, _("No previous regular expression"));
				break;
			}
			search_again = 1;
			/* fallthrough */
		case more_kc_search:
			if (cmd.number == 0)
				cmd.number++;
			erase_to_col(ctl, 0);
			putchar('/');
			ctl->prompt_len = 1;
			fflush(NULL);
			if (search_again) {
				fputc('\r', stderr);
				search(ctl, ctl->previous_search, cmd.number);
				search_again = 0;
			} else {
				ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '/');
				fputc('\r', stderr);
				ctl->next_search = xstrdup(cmdbuf);
				search(ctl, ctl->next_search, cmd.number);
			}
			retval = ctl->lines_per_screen - 1;
			done = 1;
			break;
		case more_kc_run_shell:
			run_shell(ctl, filename);
			break;
		case more_kc_help:
			if (ctl->no_scroll)
				more_clear_screen(ctl);
			erase_to_col(ctl, 0);
			runtime_usage();
			output_prompt(ctl, filename);
			break;
		case more_kc_next_file:
			putchar('\r');
			erase_to_col(ctl, 0);
			if (cmd.number == 0)
				cmd.number = 1;
			if (ctl->argv_position + cmd.number >= (unsigned int)ctl->num_files)
				more_exit(ctl);
			change_file(ctl, cmd.number);
			done = 1;
			break;
		case more_kc_previous_file:
			if (ctl->no_tty_in) {
				fprintf(stderr, "\a");
				break;
			}
			putchar('\r');
			erase_to_col(ctl, 0);
			if (cmd.number == 0)
				cmd.number = 1;
			change_file(ctl, -cmd.number);
			done = 1;
			break;
		case more_kc_run_editor:	/* This case should go right before default */
			if (!ctl->no_tty_in) {
				execute_editor(ctl, cmdbuf, filename);
				break;
			}
			/* fallthrough */
		default:
			if (ctl->suppress_bell) {
				erase_to_col(ctl, 0);
				if (ctl->enter_std)
					putp(ctl->enter_std);
				ctl->prompt_len =
				    printf(_("[Press 'h' for instructions.]"))
					    + 2 * ctl->stdout_glitch;
				if (ctl->exit_std)
					putp(ctl->exit_std);
			} else
				fprintf(stderr, "\a");
			fflush(NULL);
			break;
		}
		ctl->previous_command = cmd;
		if (done) {
			cmd.key = more_kc_unknown_command;
			break;
		}
	}
	putchar('\r');
	ctl->no_quit_dialog = 1;
	return retval;
}

/* Print out the contents of the file f, one screenful at a time. */
static void screen(struct more_control *ctl, int num_lines)
{
	int c;
	int nchars;
	int length;			/* length of current line */
	static int prev_len = 1;	/* length of previous line */

	for (;;) {
		while (num_lines > 0 && !ctl->is_paused) {
			if ((nchars = get_line(ctl, &length)) == EOF) {
				if (ctl->clear_line_ends)
					putp(ctl->clear_rest);
				return;
			}
			if (ctl->squeeze_spaces && length == 0 && prev_len == 0)
				continue;
			prev_len = length;
			if (ctl->bad_stdout
			    || ((ctl->enter_std && *ctl->enter_std == ' ') && (ctl->prompt_len > 0)))
				erase_to_col(ctl, 0);
			/* must clear before drawing line since tabs on
			 * some terminals do not erase what they tab
			 * over. */
			if (ctl->clear_line_ends)
				putp(ctl->erase_line);
			fwrite(ctl->line_buf, length, 1, stdout);
			if (nchars < ctl->prompt_len)
				erase_to_col(ctl, nchars);
			ctl->prompt_len = 0;
			if (nchars < ctl->num_columns || !ctl->fold_long_lines)
				putchar('\n');
			num_lines--;
		}
		fflush(NULL);
		if ((c = more_getc(ctl)) == EOF) {
			if (ctl->clear_line_ends)
				putp(ctl->clear_rest);
			return;
		}

		if (ctl->is_paused && ctl->clear_line_ends)
			putp(ctl->clear_rest);
		more_ungetc(ctl, c);
		ctl->is_paused = 0;
		do {
			if ((num_lines = more_key_command(ctl, NULL)) == 0)
				return;
		} while (ctl->search_called && !ctl->previous_search);
		if (ctl->hard_tty && ctl->prompt_len > 0)
			erase_to_col(ctl, 0);
		if (ctl->no_scroll && num_lines >= ctl->lines_per_screen) {
			if (ctl->clear_line_ends)
				putp(ctl->go_home);
			else
				more_clear_screen(ctl);
		}
		ctl->screen_start.line_num = ctl->current_line;
		ctl->screen_start.row_num = ctl->file_position;
	}
}

static void copy_file(FILE *f)
{
	char buf[BUFSIZ];
	size_t sz;

	while ((sz = fread(&buf, sizeof(char), sizeof(buf), f)) > 0)
		fwrite(&buf, sizeof(char), sz, stdout);
}


static void display_file(struct more_control *ctl, int left)
{
	if (!ctl->current_file)
		return;
	ctl->context.line_num = ctl->context.row_num = 0;
	ctl->current_line = 0;
	if (ctl->first_file) {
		ctl->first_file = 0;
		if (ctl->next_jump)
			skip_lines(ctl);
		if (ctl->search_at_start) {
			search(ctl, ctl->next_search, 1);
			if (ctl->no_scroll)
				left--;
		}
	} else if (ctl->argv_position < ctl->num_files && !ctl->no_tty_out)
		left =
		    more_key_command(ctl, ctl->file_names[ctl->argv_position]);
	if (left != 0) {
		if ((ctl->no_scroll || ctl->clear_first)
		    && ctl->file_size != ~((off_t)0)) {
			if (ctl->clear_line_ends)
				putp(ctl->go_home);
			else
				more_clear_screen(ctl);
		}
		if (ctl->print_banner) {
			if (ctl->bad_stdout)
				erase_to_col(ctl, 0);
			if (ctl->clear_line_ends)
				putp(ctl->erase_line);
			if (ctl->prompt_len > 14)
				erase_to_col(ctl, 14);
			if (ctl->clear_line_ends)
				putp(ctl->erase_line);
			print_separator(':', 14);
			puts(ctl->file_names[ctl->argv_position]);
			if (ctl->clear_line_ends)
				putp(ctl->erase_line);
			print_separator(':', 14);
			if (left > ctl->lines_per_page - 4)
				left = ctl->lines_per_page - 4;
		}
		if (ctl->no_tty_out)
			copy_file(ctl->current_file);
		else
			screen(ctl, left);
	}
	fflush(NULL);
	fclose(ctl->current_file);
	ctl->current_file = NULL;
	ctl->screen_start.line_num = ctl->screen_start.row_num = 0;
	ctl->context.line_num = ctl->context.row_num = 0L;
}

static void initterm(struct more_control *ctl)
{
	int ret;
	char *term;
	struct winsize win;
	char *cursor_addr;

#ifndef NON_INTERACTIVE_MORE
	ctl->no_tty_out = tcgetattr(STDOUT_FILENO, &ctl->output_tty);
#endif
	ctl->no_tty_in = tcgetattr(STDIN_FILENO, &ctl->output_tty);
	tcgetattr(STDERR_FILENO, &ctl->output_tty);
	ctl->original_tty = ctl->output_tty;
	ctl->hard_tabs = (ctl->output_tty.c_oflag & TABDLY) != TAB3;
	if (ctl->no_tty_out)
		return;

	ctl->output_tty.c_lflag &= ~(ICANON | ECHO);
	ctl->output_tty.c_cc[VMIN] = 1;
	ctl->output_tty.c_cc[VTIME] = 0;
	ctl->erase_previous_ok = (ctl->output_tty.c_cc[VERASE] != 255);
	ctl->erase_input_ok = (ctl->output_tty.c_cc[VKILL] != 255);
	if ((term = getenv("TERM")) == NULL) {
		ctl->dumb_tty = 1;
	}
	setupterm(term, 1, &ret);
	if (ret <= 0) {
		ctl->dumb_tty = 1;
		return;
	}
	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) < 0) {
		ctl->lines_per_page = tigetnum(TERM_LINES);
		ctl->num_columns = tigetnum(TERM_COLS);
	} else {
		if ((ctl->lines_per_page = win.ws_row) == 0)
			ctl->lines_per_page = tigetnum(TERM_LINES);
		if ((ctl->num_columns = win.ws_col) == 0)
			ctl->num_columns = tigetnum(TERM_COLS);
	}
	if ((ctl->lines_per_page <= 0) || tigetflag(TERM_HARD_COPY)) {
		ctl->hard_tty = 1;
		ctl->lines_per_page = LINES_PER_PAGE;
	}

	if (tigetflag(TERM_EAT_NEW_LINE))
		/* Eat newline at last column + 1; dec, concept */
		ctl->eat_newline++;
	if (ctl->num_columns <= 0)
		ctl->num_columns = NUM_COLUMNS;

	ctl->wrap_margin = tigetflag(TERM_AUTO_RIGHT_MARGIN);
	ctl->bad_stdout = tigetflag(TERM_CEOL);
	ctl->erase_line = tigetstr(TERM_CLEAR_TO_LINE_END);
	ctl->clear = tigetstr(TERM_CLEAR);
	if ((ctl->enter_std = tigetstr(TERM_STANDARD_MODE)) != NULL) {
		ctl->exit_std = tigetstr(TERM_EXIT_STANDARD_MODE);
		if (0 < tigetnum(TERM_STD_MODE_GLITCH))
			ctl->stdout_glitch = 1;
	}

	cursor_addr = tigetstr(TERM_HOME);
	if (cursor_addr == NULL || *cursor_addr == '\0') {
		cursor_addr = tigetstr(TERM_CURSOR_ADDRESS);
		if (cursor_addr)
			cursor_addr = tparm(cursor_addr, 0, 0);
	}
	if (cursor_addr)
		ctl->go_home = xstrdup(cursor_addr);

	if ((ctl->move_line_down = tigetstr(TERM_LINE_DOWN)) == NULL)
		ctl->move_line_down = BACKSPACE;
	ctl->clear_rest = tigetstr(TERM_CLEAR_TO_SCREEN_END);
	if ((ctl->backspace_ch = tigetstr(TERM_BACKSPACE)) == NULL)
		ctl->backspace_ch = BACKSPACE;

	if ((ctl->shell = getenv("SHELL")) == NULL)
		ctl->shell = _PATH_BSHELL;
}

int main(int argc, char **argv)
{
	char *s;
	int left;
	struct more_control ctl = {
		.first_file = 1,
		.fold_long_lines = 1,
		.no_quit_dialog = 1,
		.stop_after_formfeed = 1,
		.wrap_margin = 1,
		.lines_per_page = LINES_PER_PAGE,
		.num_columns = NUM_COLUMNS,
		.d_scroll_len = SCROLL_LEN,
		0
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	close_stdout_atexit();
	setlocale(LC_ALL, "");

	/* Auto set no scroll on when binary is called page */
	if (!(strcmp(program_invocation_short_name, "page")))
		ctl.no_scroll++;

	if ((s = getenv("MORE")) != NULL)
		env_argscan(&ctl, s);
	argscan(&ctl, argc, argv);

	initterm(&ctl);

#ifdef HAVE_MAGIC
	ctl.magic = magic_open(MAGIC_MIME_ENCODING | MAGIC_SYMLINK);
	magic_load(ctl.magic, NULL);
#endif
	prepare_line_buffer(&ctl);

	ctl.d_scroll_len = ctl.lines_per_page / 2 - 1;
	if (ctl.d_scroll_len <= 0)
		ctl.d_scroll_len = 1;

	/* allow clear_line_ends only if go_home and erase_line and clear_rest strings are
	 * defined, and in that case, make sure we are in no_scroll mode */
	if (ctl.clear_line_ends) {
		if ((ctl.go_home == NULL) || (*ctl.go_home == '\0') ||
		    (ctl.erase_line == NULL) || (*ctl.erase_line == '\0') ||
		    (ctl.clear_rest == NULL) || (*ctl.clear_rest == '\0'))
			ctl.clear_line_ends = 0;
		else
			ctl.no_scroll = 1;
	}
	if (ctl.lines_per_screen == 0)
		ctl.lines_per_screen = ctl.lines_per_page - 1;
	left = ctl.lines_per_screen;
	if (ctl.num_files > 1)
		ctl.print_banner = 1;
	if (!ctl.no_tty_in && ctl.num_files == 0) {
		warnx(_("bad usage"));
		errtryhelp(EXIT_FAILURE);
	} else
		ctl.current_file = stdin;
	if (!ctl.no_tty_out) {
		if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
			ctl.catch_suspend++;
		}
		tcsetattr(STDERR_FILENO, TCSANOW, &ctl.output_tty);
	}
	sigemptyset(&ctl.sigset);
	sigaddset(&ctl.sigset, SIGINT);
	sigaddset(&ctl.sigset, SIGQUIT);
	sigaddset(&ctl.sigset, SIGTSTP);
	sigaddset(&ctl.sigset, SIGCONT);
	sigaddset(&ctl.sigset, SIGWINCH);
	sigprocmask(SIG_BLOCK, &ctl.sigset, NULL);
	ctl.sigfd = signalfd(-1, &ctl.sigset, SFD_CLOEXEC);
	if (ctl.no_tty_in) {
		if (ctl.no_tty_out)
			copy_file(stdin);
		else {
			ctl.current_file = stdin;
			display_file(&ctl, left);
		}
		ctl.no_tty_in = 0;
		ctl.print_banner = 1;
		ctl.first_file = 0;
	}

	while (ctl.argv_position < ctl.num_files) {
		checkf(&ctl, ctl.file_names[ctl.argv_position]);
		display_file(&ctl, left);
		ctl.first_file = 0;
		ctl.argv_position++;
	}
	ctl.clear_line_ends = 0;
	ctl.prompt_len = 0;
	more_exit(&ctl);
}