-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmore.c
160 lines (136 loc) · 3.46 KB
/
more.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) 2008 Chris Cappuccio <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ttycom.h>
#include <sys/ioctl.h>
#include <wchar.h>
#include "externs.h"
#define PAGERPROMPT " --More-- "
#define BACKOVERPROMPT "\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b"
int nsh_cbreak(void);
void nsh_nocbreak(void);
static struct termios oldtty;
struct winsize winsize;
/*
* Display file
*/
int
more(char *fname)
{
FILE *f;
char *input, c;
size_t s, wlen;
int i, nopager = 0;
wchar_t *ws = NULL;
if ((f = fopen(fname, "r")) == NULL) {
if (errno == ENOENT)
printf ("%% File %s not found\n", fname);
else
printf ("%% more: fopen(%s): %s\n", fname,
strerror(errno));
return(0);
}
if (!interactive_mode || nsh_cbreak() < 0)
nopager = 1;
for (i = 0; (input = fgetln(f, &s)) != NULL; i++) {
int extra_rows = 0;
/*
* We replace newline (or whatever was at the end of
* the line) with NUL termination
*/
input[s - 1] = '\0';
/* Account for lines overflowing the terminal's width. */
if (mbs2ws(&ws, &wlen, input) == 0) {
int width = wcswidth(ws, wcslen(ws));
if (width == -1) { /* unprintable wide character */
free(ws);
ws = NULL;
} else
extra_rows = width / winsize.ws_col;
} else
ws = NULL;
if (!nopager && i + extra_rows >= (winsize.ws_row - 1)) {
for (;;) {
printf(PAGERPROMPT);
fflush(0);
c = getchar();
printf(BACKOVERPROMPT);
if (c == 'q')
goto quit;
if (c == '\r' || c == '\n' || c == 'j' ||
c == CTRL('n')) {
i--; /* skip one line */
break;
}
if (c == ' ' || c == 'f' || c == CTRL('f')) {
i = 0; /* skip one page */
break;
}
}
}
if (ws) {
printf("%ls\n", ws);
free(ws);
ws = NULL;
} else
printf("%s\n", input);
i += extra_rows;
}
quit:
if (!nopager)
nsh_nocbreak();
fclose(f);
free(ws);
return(1);
}
int
nsh_cbreak(void)
{
struct termios newtty;
if (tcgetattr(fileno(stdout), &oldtty) < 0)
return(-1);
(void)memcpy(&newtty, &oldtty, sizeof(newtty));
newtty.c_lflag &= ~(ECHO | ICANON); /* no echo, canonical */
newtty.c_cc[VMIN] = 1; /* one char at a time */
newtty.c_cc[VTIME] = 0; /* no timeout */
if (tcsetattr(fileno(stdout), TCSAFLUSH, &newtty) < 0)
return(-1);
return(0);
}
void
nsh_nocbreak(void)
{
tcsetattr(0, TCSAFLUSH, &oldtty);
}
void
setwinsize(int signo)
{
int save_errno = errno;
if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) {
winsize.ws_col = winsize.ws_col ? winsize.ws_col : 80;
winsize.ws_row = winsize.ws_row ? winsize.ws_row : 24;
} else {
winsize.ws_col = 80;
winsize.ws_row = 24;
}
errno = save_errno;
}