2012-03-11 10:57:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2012.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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 3 of the License, or (at your option)
|
|
|
|
any later version.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
pager.cpp
|
|
|
|
Displays files one page at a time.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2012-01-22 17:46:41 -05:00
|
|
|
#include <sys/keycodes.h>
|
|
|
|
#include <sys/termmode.h>
|
2011-12-26 11:15:14 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <error.h>
|
2012-07-31 10:48:56 -04:00
|
|
|
#include <termios.h>
|
2011-12-26 11:15:14 -05:00
|
|
|
|
2012-03-07 15:06:29 -05:00
|
|
|
char* stdinargv[2];
|
|
|
|
|
2011-12-26 11:15:14 -05:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2012-03-07 15:06:29 -05:00
|
|
|
if ( isatty(0) && argc < 2 )
|
|
|
|
{
|
|
|
|
printf("usage: %s [FILE] ...\n", argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !isatty(0) && argc < 2 )
|
|
|
|
{
|
|
|
|
stdinargv[0] = argv[0];
|
|
|
|
stdinargv[1] = (char*) "-";
|
|
|
|
argv = stdinargv;
|
|
|
|
argc = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stdinfd = dup(0);
|
|
|
|
close(0);
|
|
|
|
int ttyfd = open("/dev/tty", O_RDONLY);
|
|
|
|
if ( ttyfd != 0 ) { perror("/dev/tty"); return 1; }
|
2012-07-31 10:48:56 -04:00
|
|
|
struct winsize ws;
|
|
|
|
if ( tcgetwinsize(0, &ws) )
|
|
|
|
error(1, errno, "tcgetwinsize");
|
|
|
|
const int HEIGHT = ws.ws_row;
|
|
|
|
const int WIDTH = ws.ws_col;
|
2011-12-26 11:15:14 -05:00
|
|
|
int linesleft = HEIGHT-1;
|
|
|
|
int result = 0;
|
|
|
|
size_t charleft = WIDTH;
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
2012-03-07 15:06:29 -05:00
|
|
|
int fd = ( strcmp(argv[i], "-") == 0 ) ? stdinfd : open(argv[i], O_RDONLY);
|
2011-12-26 11:15:14 -05:00
|
|
|
if ( fd < 0 ) { result = 1; perror(argv[i]); continue; }
|
|
|
|
const size_t BUFFER_SIZE = 4096;
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
ssize_t numbytes = read(fd, buffer, BUFFER_SIZE);
|
|
|
|
if ( !numbytes ) { break; }
|
|
|
|
if ( numbytes < 0 )
|
|
|
|
{
|
|
|
|
result = 1;
|
|
|
|
error(0, errno, "read: %s", argv[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for ( ssize_t i = 0; i < numbytes; i++ )
|
|
|
|
{
|
|
|
|
char c = buffer[i];
|
|
|
|
if ( c == '\n' ) { charleft = WIDTH; }
|
|
|
|
bool eol = (c == '\n') || !charleft;
|
|
|
|
if ( eol ) { charleft = WIDTH; }
|
2012-03-11 13:16:55 -04:00
|
|
|
if ( eol && linesleft <= 1 )
|
2011-12-26 11:15:14 -05:00
|
|
|
{
|
|
|
|
printf("\n--pager--");
|
|
|
|
fflush(stdout);
|
2012-01-22 17:46:41 -05:00
|
|
|
settermmode(0, TERMMODE_KBKEY | TERMMODE_SIGNAL);
|
2011-12-26 11:15:14 -05:00
|
|
|
bool doexit = false;
|
2012-01-22 17:46:41 -05:00
|
|
|
int kbkey;
|
2011-12-26 11:15:14 -05:00
|
|
|
do
|
|
|
|
{
|
2012-01-22 17:46:41 -05:00
|
|
|
uint32_t codepoint;
|
|
|
|
ssize_t numbytes = read(0, &codepoint, sizeof(codepoint));
|
|
|
|
if ( !numbytes ) { exit(0); }
|
|
|
|
if ( numbytes < 0 ) { error(1, errno, "read(stdin)"); }
|
2012-09-08 14:55:43 -04:00
|
|
|
if ( numbytes < (ssize_t) sizeof(codepoint) ) { error(1, errno, "bad stdin"); }
|
2012-01-22 17:46:41 -05:00
|
|
|
if ( !(kbkey = KBKEY_DECODE(codepoint)) ) { continue; }
|
|
|
|
if ( kbkey == KBKEY_DOWN ) { break; }
|
|
|
|
if ( kbkey == KBKEY_PGDOWN ) { linesleft = HEIGHT-1; break; }
|
2012-03-21 19:52:29 -04:00
|
|
|
if ( kbkey == -KBKEY_Q ) { doexit = true; break; }
|
2012-01-22 17:46:41 -05:00
|
|
|
} while ( kbkey != KBKEY_ENTER );
|
2011-12-26 11:15:14 -05:00
|
|
|
printf("\r\e[J");
|
|
|
|
if ( doexit ) { exit(result); }
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( eol && linesleft ) { linesleft--; }
|
|
|
|
printf("%c", c);
|
|
|
|
charleft--;
|
2012-09-08 09:06:10 -04:00
|
|
|
if ( c == '\t' ) { charleft &= ~(8-1); }
|
2011-12-26 11:15:14 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-07 15:06:29 -05:00
|
|
|
if ( fd != stdinfd ) { close(fd); }
|
2011-12-26 11:15:14 -05:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|