-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmore_u.c
415 lines (384 loc) · 9.73 KB
/
more_u.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* more - toolbox
Copyright 2015-2016 Rivoreo
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifdef _WIN32
#include <windows.h>
#include <winioctl.h>
#ifdef _WIN32_WCE
#ifndef IOCTL_CONSOLE_SETMODE
#define IOCTL_CONSOLE_SETMODE CTL_CODE(FILE_DEVICE_CONSOLE, 1, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif
#ifndef IOCTL_CONSOLE_GETMODE
#define IOCTL_CONSOLE_GETMODE CTL_CODE(FILE_DEVICE_CONSOLE, 2, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif
#ifndef IOCTL_CONSOLE_GETSCREENROWS
#define IOCTL_CONSOLE_GETSCREENROWS CTL_CODE(FILE_DEVICE_CONSOLE, 7, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif
#ifndef IOCTL_CONSOLE_GETSCREENCOLS
#define IOCTL_CONSOLE_GETSCREENCOLS CTL_CODE(FILE_DEVICE_CONSOLE, 9, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif
#define CECONSOLE_MODE_ECHO_INPUT 0x0001
#define CECONSOLE_MODE_LINE_INPUT 0x0002
#define CECONSOLE_MODE_PROCESSED_OUTPUT 0x0004
#ifndef FILE_DEVICE_CONSOLE
#define FILE_DEVICE_CONSOLE 0x00000102
#endif
#else
#include <conio.h>
#endif /* _WIN32_WCE */
#else
#include <sys/ioctl.h>
#include <termios.h>
#endif
#if !defined _WIN32 || defined _WIN32_WCE || defined _WINDOWSNT_NATIVE
/* Initialize new terminal i/o settings */
#ifdef _WIN32_WCE
unsigned long int old, new;
static void set_terminal(int echo) {
unsigned long int rsize;
DeviceIoControl((void *)STDIN_FILENO, IOCTL_CONSOLE_GETMODE, NULL, 0, &old, sizeof old, &rsize, NULL);
new = old;
if(echo) new |= CECONSOLE_MODE_ECHO_INPUT;
else new &= ~CECONSOLE_MODE_ECHO_INPUT;
new &= ~CECONSOLE_MODE_LINE_INPUT;
DeviceIoControl((void *)STDIN_FILENO, IOCTL_CONSOLE_SETMODE, &new, sizeof new, NULL, 0, &rsize, NULL);
}
static void reset_terminal() {
unsigned long int rsize;
DeviceIoControl((void *)STDIN_FILENO, IOCTL_CONSOLE_SETMODE, &old, sizeof old, NULL, 0, &rsize, NULL);
}
#else
static struct winsize winsz;
static struct termios old, new;
static void set_terminal(int echo) {
tcgetattr(STDIN_FILENO, &old); /* grab old terminal i/o settings */
new = old; /* make new settings same as old settings */
new.c_iflag &= ~ICRNL; /* Translate carriage return to newline on input (unless IGNCR is set) */
new.c_lflag &= ~ICANON; /* disable buffered i/o */
if(echo) new.c_lflag |= ECHO; /* set echo mode */
else new.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &new); /* use these new terminal i/o settings now */
}
/* Restore old terminal i/o settings */
static void reset_terminal(void) {
tcsetattr(STDIN_FILENO, TCSANOW, &old);
}
#endif
/* Read 1 character - echo defines echo mode */
static char getch_(int echo) {
char ch;
set_terminal(echo);
ch = getchar();
reset_terminal();
return ch;
}
/* Read 1 character without echo */
static char getch(void) {
return getch_(0);
}
#if 0 /* Not used */
/* Read 1 character with echo */
static char getche(void) {
return getch_(1);
}
#endif
#endif
#define SKIP_MULTI_BLACK_LINES (1 << 0)
unsigned int term_row;
static int page_col, page_row;
static char filename[1024];
static int usage(char *name) {
fprintf(stdout, "Usage: %s [<options>] [<file>]\n\n"
"Options:\n"
" -s Squeeze multiple blank lines into a single line\n"
" -V Display version information and exit\n\n", name);
return 1;
}
static int read_more() {
switch(getch()) {
case 13:
page_row++;
#ifdef _WIN32
printf("\r \r");
#else
/* Clean the current line */
printf("\x1b[1K");
/* Set the cursor to the start of the line */
printf("\x1b[%u;0H", term_row);
#endif
break;
case 32:
page_row = term_row;
#ifdef _WIN32
printf("\r \r");
#else
/* Clean the current line */
printf("\x1b[1K");
/* Set the cursor to the start of the line */
printf("\x1b[%u;0H", term_row);
#endif
break;
case 'q':
putchar('\n');
exit(0);
default:
#ifdef _WIN32
printf("\r \r");
#else
/* Clean the current line */
printf("\x1b[1K");
/* Set the cursor to the start of the line */
printf("\x1b[%u;0H", term_row);
#endif
/* If none input, return 1 */
return 1;
}
return 0;
}
static int read_file(FILE *fp, int use_pipe, int flags) {
char line[page_col];
int seek;
off_t filesize = -1;
int black_line = 0;
if(!use_pipe) {
struct stat filestat;
stat(filename, &filestat);
filesize = filestat.st_size;
}
while(fgets(line, page_col, fp) != NULL) {
if((flags & SKIP_MULTI_BLACK_LINES)) {
if(*line == '\n') {
if(black_line) continue;
else black_line = 1;
} else black_line = 0;
}
if(page_row != 1) {
fprintf(stdout,"%s",line);
//fflush(stdout);
page_row--;
} else {
if(use_pipe) {
fprintf(stdout, "--More--");
} else {
seek = ftell(fp);
int percent = ((float)seek / (float)filesize) * 100;
fprintf(stdout, "--More-- (%%%d)", percent);
}
while(read_more() == 1) {
fprintf(stdout, "--More--");
}
}
}
#if 0
// There is no need to read all data from a pipe that just for calculate the length
} else {
char **buff;
int i = 1;
char line[page_col];
/* First alloc */
buff = (char **)malloc(sizeof(char *));
if(!buff) return -1;
while(fgets(line, page_col, fp) != NULL) {
/* Alloc buff[i-1] */
buff[i-1] = (char *)malloc(sizeof(char)*page_col+1);
strcpy(buff[i-1], line);
i++;
/* realloc */
buff = (char **)realloc(buff, sizeof(char *)*i);
if(!buff) return -1;
//printf("%s",buff[i-1]);
}
if(close(STDIN_FILENO)) {
perror("STDIN_FILENO close faild");
return errno;
}
if((fp = fopen("/dev/tty", "r")) == NULL) {
perror("Error open TTY");
return errno;
}
int line_num = i -1;
int l = 0;
for(i-- ; i > 0; i--) {
if(page_row != 1) {
fprintf(stdout,"%s",buff[l]);
//fflush(stdout);
page_row--;
} else {
int percent = ((float)l/(float)line_num) * 100;
fprintf(stdout, "--More-- (%%%d)",percent);
read_more();
}
l++;
}
/* Free memory */
for(i=0; i<=line_num; i++) {
free(buff[i]);
}
free(buff);
}
#endif
/* Should close in main
if(fclose(fp) != 0) {
perror("Error to close");
return errno;
}
*/
return 0;
}
#include "version.h"
int more_main(int argc, char *argv[]) {
int use_tty = isatty(STDOUT_FILENO);
int use_pipe = !isatty(STDIN_FILENO);
int opt;
if(use_tty) {
#ifdef _WINDOWSNT_NATIVE
// From Windows 2000
page_row = 31;
page_col = 78;
#else
#ifdef _WIN32
#ifdef _WIN32_WCE
unsigned long int row, col, rsize;
if(!DeviceIoControl((void *)STDOUT_FILENO, IOCTL_CONSOLE_GETSCREENROWS, NULL, 0, &row, sizeof row, &rsize, NULL)) {
return -errno;
}
if(!DeviceIoControl((void *)STDOUT_FILENO, IOCTL_CONSOLE_GETSCREENCOLS, NULL, 0, &col, sizeof col, &rsize, NULL)) {
return -errno;
}
page_row = row;
page_col = col;
#else
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(!GetConsoleScreenBufferInfo((void *)STDOUT_FILENO, &csbi)) {
return 1;
}
page_row = csbi.srWindow.Right - csbi.srWindow.Left + 1;
page_col = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
#endif
//fprintf(stderr, "%d, %d\n", page_col, page_row);
#else
/*
* Get terminal size
*
* struct winsize {
* unsigned short ws_row;
* unsigned short ws_col;
* unsigned short ws_xpixel;
* unsigned short ws_ypixel;
*};
*/
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsz) == -1) {
return -errno;
}
page_col = winsz.ws_col;
page_row = winsz.ws_row;
#endif
#endif
term_row = page_row;
} else {
//usage(argv[0]);
fprintf(stderr, "%s: Not a terminal\n", argv[0]);
return 1;
}
int flags = 0;
/* getopt */
while((opt = getopt(argc, argv, "sV")) != -1) {
switch(opt) {
case 's':
flags |= SKIP_MULTI_BLACK_LINES;
break;
case 'V':
fprintf(stdout,"%s, From ToolBox by Rivoreo\nToolBox version: " VERSION "\n", argv[0]);
return 0;
default:
usage(argv[0]);
return EXIT_FAILURE;
}
}
if(optind == argc && !use_pipe) {
usage(argv[0]);
return -1;
}
setvbuf(stdout, NULL, _IOLBF, 0);
int r = 0;
int fd = -1;
/*
* Now only support open a file or pipe
* So I dont use get opt now.
*/
FILE *fp;
if(argc != 1) {
strcpy(filename, argv[optind]);
fp = fopen(filename, "r");
if(!fp) {
perror("Error open file");
return 1;
}
} else if(argc == 1 && !use_pipe) {
usage(argv[0]);
return 1;
} else if(use_pipe) {
#if defined _WIN32 && !defined _WINDOWSNT_NATIVE
fprintf(stderr, "%s: Reading from stdin is not supported yet\n", argv[0]);
return 1;
#else
fd = dup(STDIN_FILENO);
if(fd == -1) {
perror("dup");
return 1;
}
//#if defined _WIN32 && !defined _WIN32_WCE
//fp = stdin;
//#else
fp = fdopen(fd, "r");
if(!fp) {
perror("Error open STDIN_FILENO");
return 1;
}
if(close(STDIN_FILENO) < 0) {
perror("close");
return 1;
}
//#endif
//#ifndef _WIN32
if(open("/dev/tty", O_RDONLY) != STDIN_FILENO) {
fprintf(stderr, "%s: Cannot open terminal for stdin\n", argv[0]);
return 1;
}
//#endif
#endif
} else {
fprintf(stderr, "%s: Cannot read from a terminal\n", argv[0]);
return 1;
}
if(read_file(fp, use_pipe, flags) < 0) {
perror(argv[0]);
r = 1;
}
#if defined _WIN32 && !defined _WIN32_WNT_NATIVE
if(use_pipe) {
// Since our ToolBox may be used as a library, restore the original file
if(close(STDIN_FILENO) < 0) {
perror("close");
return 1;
}
if(dup(fd) != STDIN_FILENO) {
fprintf(stderr, "%s: Cannot restore original stdin\n", argv[0]);
return 1;
}
close(fd);
}
#endif
fclose(fp);
return r;
}