diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-11-09 01:47:36 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-11-09 01:47:36 +0000 |
commit | 50d6360771be509737bb55b2cc5bc5e25f2a4fea (patch) | |
tree | 81d4cfe9ec9b5281924f678c28f61542616a3db7 /util-linux/more.c | |
parent | fbb39c83b69d6c4de943c0b7374000339635d13d (diff) | |
download | busybox-w32-50d6360771be509737bb55b2cc5bc5e25f2a4fea.tar.gz busybox-w32-50d6360771be509737bb55b2cc5bc5e25f2a4fea.tar.bz2 busybox-w32-50d6360771be509737bb55b2cc5bc5e25f2a4fea.zip |
Stuff
Diffstat (limited to 'util-linux/more.c')
-rw-r--r-- | util-linux/more.c | 73 |
1 files changed, 53 insertions, 20 deletions
diff --git a/util-linux/more.c b/util-linux/more.c index ea5e22562..469316128 100644 --- a/util-linux/more.c +++ b/util-linux/more.c | |||
@@ -25,7 +25,9 @@ | |||
25 | 25 | ||
26 | 26 | ||
27 | /* Turning this off makes things a bit smaller (and less pretty) */ | 27 | /* Turning this off makes things a bit smaller (and less pretty) */ |
28 | #define BB_MORE_TERM | 28 | #define BB_FEATURE_USE_TERMIOS |
29 | /* Turning this off makes things a bit smaller (and less pretty) */ | ||
30 | #define BB_FEATURE_AUTOWIDTH | ||
29 | 31 | ||
30 | 32 | ||
31 | 33 | ||
@@ -33,18 +35,16 @@ | |||
33 | #include <stdio.h> | 35 | #include <stdio.h> |
34 | #include <fcntl.h> | 36 | #include <fcntl.h> |
35 | #include <signal.h> | 37 | #include <signal.h> |
36 | 38 | #include <sys/ioctl.h> | |
37 | 39 | ||
38 | static const char more_usage[] = "[file ...]"; | 40 | static const char more_usage[] = "[file ...]"; |
39 | 41 | ||
40 | |||
41 | /* ED: sparc termios is broken: revert back to old termio handling. */ | 42 | /* ED: sparc termios is broken: revert back to old termio handling. */ |
42 | #ifdef BB_MORE_TERM | 43 | #ifdef BB_FEATURE_USE_TERMIOS |
43 | 44 | ||
44 | #if #cpu(sparc) | 45 | #if #cpu(sparc) |
45 | # define USE_OLD_TERMIO | 46 | # define USE_OLD_TERMIO |
46 | # include <termio.h> | 47 | # include <termio.h> |
47 | # include <sys/ioctl.h> | ||
48 | # define termios termio | 48 | # define termios termio |
49 | # define stty(fd,argp) ioctl(fd,TCSETAF,argp) | 49 | # define stty(fd,argp) ioctl(fd,TCSETAF,argp) |
50 | #else | 50 | #else |
@@ -57,16 +57,35 @@ static const char more_usage[] = "[file ...]"; | |||
57 | 57 | ||
58 | void gotsig(int sig) { | 58 | void gotsig(int sig) { |
59 | stty(fileno(cin), &initial_settings); | 59 | stty(fileno(cin), &initial_settings); |
60 | fprintf(stdout, "\n"); | ||
60 | exit( TRUE); | 61 | exit( TRUE); |
61 | } | 62 | } |
62 | #endif | 63 | #endif |
63 | 64 | ||
65 | |||
66 | |||
67 | #define TERMINAL_WIDTH 79 /* not 80 in case terminal has linefold bug */ | ||
68 | #define TERMINAL_HEIGHT 24 | ||
69 | |||
70 | |||
71 | #if defined BB_FEATURE_AUTOWIDTH && ! defined USE_OLD_TERMIO | ||
72 | static int terminal_width = 0, terminal_height = 0; | ||
73 | #else | ||
74 | #define terminal_width TERMINAL_WIDTH | ||
75 | #define terminal_height TERMINAL_HEIGHT | ||
76 | #endif | ||
77 | |||
78 | |||
79 | |||
64 | extern int more_main(int argc, char **argv) | 80 | extern int more_main(int argc, char **argv) |
65 | { | 81 | { |
66 | int c, lines=0, input=0; | 82 | int c, lines=0, input=0; |
67 | int next_page=0; | 83 | int next_page=0; |
68 | struct stat st; | 84 | struct stat st; |
69 | FILE *file; | 85 | FILE *file; |
86 | #ifdef BB_FEATURE_AUTOWIDTH | ||
87 | struct winsize win; | ||
88 | #endif | ||
70 | 89 | ||
71 | argc--; | 90 | argc--; |
72 | argv++; | 91 | argv++; |
@@ -87,7 +106,7 @@ extern int more_main(int argc, char **argv) | |||
87 | } | 106 | } |
88 | fstat(fileno(file), &st); | 107 | fstat(fileno(file), &st); |
89 | 108 | ||
90 | #ifdef BB_MORE_TERM | 109 | #ifdef BB_FEATURE_USE_TERMIOS |
91 | cin = fopen("/dev/tty", "r"); | 110 | cin = fopen("/dev/tty", "r"); |
92 | if (!cin) | 111 | if (!cin) |
93 | cin = fopen("/dev/console", "r"); | 112 | cin = fopen("/dev/console", "r"); |
@@ -100,12 +119,19 @@ extern int more_main(int argc, char **argv) | |||
100 | new_settings.c_lflag &= ~ICANON; | 119 | new_settings.c_lflag &= ~ICANON; |
101 | new_settings.c_lflag &= ~ECHO; | 120 | new_settings.c_lflag &= ~ECHO; |
102 | stty(fileno(cin), &new_settings); | 121 | stty(fileno(cin), &new_settings); |
103 | 122 | ||
123 | #ifdef BB_FEATURE_AUTOWIDTH | ||
124 | ioctl(STDOUT_FILENO, TIOCGWINSZ, &win); | ||
125 | if (win.ws_row > 4) | ||
126 | terminal_height = win.ws_row - 2; | ||
127 | if (win.ws_col > 0) | ||
128 | terminal_width = win.ws_col - 1; | ||
129 | #endif | ||
130 | |||
104 | (void) signal(SIGINT, gotsig); | 131 | (void) signal(SIGINT, gotsig); |
105 | (void) signal(SIGQUIT, gotsig); | 132 | (void) signal(SIGQUIT, gotsig); |
106 | (void) signal(SIGTERM, gotsig); | 133 | (void) signal(SIGTERM, gotsig); |
107 | 134 | ||
108 | |||
109 | #endif | 135 | #endif |
110 | while ((c = getc(file)) != EOF) { | 136 | while ((c = getc(file)) != EOF) { |
111 | if ( next_page ) { | 137 | if ( next_page ) { |
@@ -119,7 +145,7 @@ extern int more_main(int argc, char **argv) | |||
119 | st.st_size); | 145 | st.st_size); |
120 | } | 146 | } |
121 | len += fprintf(stdout, "%s", | 147 | len += fprintf(stdout, "%s", |
122 | #ifdef BB_MORE_TERM | 148 | #ifdef BB_FEATURE_USE_TERMIOS |
123 | "" | 149 | "" |
124 | #else | 150 | #else |
125 | "\n" | 151 | "\n" |
@@ -129,24 +155,31 @@ extern int more_main(int argc, char **argv) | |||
129 | fflush(stdout); | 155 | fflush(stdout); |
130 | input = getc( cin); | 156 | input = getc( cin); |
131 | 157 | ||
132 | #ifdef BB_MORE_TERM | 158 | #ifdef BB_FEATURE_USE_TERMIOS |
133 | /* Erase the "More" message */ | 159 | /* Erase the "More" message */ |
134 | while(len-- > 0) | 160 | while(--len >= 0) |
135 | putc('\b', stdout); | 161 | putc('\b', stdout); |
136 | while(len++ < 79) | 162 | while(++len <= terminal_width) |
137 | putc(' ', stdout); | 163 | putc(' ', stdout); |
138 | while(len-- > 0) | 164 | while(--len >= 0) |
139 | putc('\b', stdout); | 165 | putc('\b', stdout); |
140 | fflush(stdout); | 166 | fflush(stdout); |
141 | #endif | 167 | #endif |
142 | 168 | ||
143 | } | 169 | } |
144 | if (input=='q') | 170 | if (c == '\n' ) { |
145 | goto end; | 171 | switch(input) { |
146 | if (input=='\n' && c == '\n' ) | 172 | case 'q': |
147 | next_page = 1; | 173 | goto end; |
148 | if ( c == ' ' && ++lines == 24 ) | 174 | case '\n': |
149 | next_page = 1; | 175 | /* increment by just one line if we are at |
176 | * the end of this line*/ | ||
177 | next_page = 1; | ||
178 | break; | ||
179 | } | ||
180 | if ( ++lines == terminal_height ) | ||
181 | next_page = 1; | ||
182 | } | ||
150 | putc(c, stdout); | 183 | putc(c, stdout); |
151 | } | 184 | } |
152 | fclose(file); | 185 | fclose(file); |
@@ -155,7 +188,7 @@ extern int more_main(int argc, char **argv) | |||
155 | argv++; | 188 | argv++; |
156 | } while (--argc > 0); | 189 | } while (--argc > 0); |
157 | end: | 190 | end: |
158 | #ifdef BB_MORE_TERM | 191 | #ifdef BB_FEATURE_USE_TERMIOS |
159 | gotsig(0); | 192 | gotsig(0); |
160 | #endif | 193 | #endif |
161 | exit(TRUE); | 194 | exit(TRUE); |