mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Rewrite pager(1).
This commit is contained in:
parent
a03546be01
commit
d238518c35
1 changed files with 366 additions and 78 deletions
444
utils/pager.cpp
444
utils/pager.cpp
|
@ -1,6 +1,6 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2012.
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2014.
|
||||
|
||||
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
|
||||
|
@ -22,97 +22,385 @@
|
|||
|
||||
#include <sys/keycodes.h>
|
||||
#include <sys/termmode.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <fcntl.h>
|
||||
#include <ioleast.h>
|
||||
#include <locale.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <wchar.h>
|
||||
|
||||
char* stdinargv[2];
|
||||
#if !defined(VERSIONSTR)
|
||||
#define VERSIONSTR "unknown version"
|
||||
#endif
|
||||
|
||||
struct display_entry
|
||||
{
|
||||
wchar_t c;
|
||||
};
|
||||
|
||||
struct pager
|
||||
{
|
||||
int tty_fd;
|
||||
unsigned int tty_fd_old_termmode;
|
||||
int out_fd;
|
||||
bool out_fd_tty;
|
||||
struct winsize out_fd_winsize;
|
||||
struct display_entry* display;
|
||||
struct wincurpos begun_wcp;
|
||||
struct wincurpos current_wcp;
|
||||
mbstate_t in_ps;
|
||||
mbstate_t out_ps;
|
||||
const char* input_prompt_name;
|
||||
size_t allowed_lines;
|
||||
bool quiting;
|
||||
};
|
||||
|
||||
void pager_init(struct pager* pager)
|
||||
{
|
||||
if ( !isatty(pager->tty_fd = 0) )
|
||||
{
|
||||
if ( (pager->tty_fd = open("/dev/tty", O_RDONLY)) < 0 )
|
||||
error(1, errno, "/dev/tty");
|
||||
if ( !isatty(pager->tty_fd) )
|
||||
error(1, errno, "/dev/tty");
|
||||
}
|
||||
gettermmode(pager->tty_fd, &pager->tty_fd_old_termmode);
|
||||
settermmode(pager->tty_fd, TERMMODE_KBKEY | TERMMODE_UNICODE | TERMMODE_SIGNAL);
|
||||
|
||||
if ( (pager->out_fd_tty = isatty(pager->out_fd = 1)) )
|
||||
{
|
||||
tcgetwinsize(pager->out_fd, &pager->out_fd_winsize);
|
||||
size_t display_length = (size_t) pager->out_fd_winsize.ws_col *
|
||||
(size_t) pager->out_fd_winsize.ws_row;
|
||||
pager->display = (struct display_entry*)
|
||||
calloc(display_length, sizeof(struct display_entry));
|
||||
if ( !pager->display )
|
||||
error(1, errno, "malloc");
|
||||
tcgetwincurpos(pager->out_fd, &pager->begun_wcp);
|
||||
memcpy(&pager->current_wcp, &pager->begun_wcp, sizeof(struct wincurpos));
|
||||
pager->allowed_lines = pager->out_fd_winsize.ws_row - 1;
|
||||
}
|
||||
|
||||
memset(&pager->in_ps, 0, sizeof(pager->in_ps));
|
||||
memset(&pager->out_ps, 0, sizeof(pager->out_ps));
|
||||
}
|
||||
|
||||
void pager_end(struct pager* pager)
|
||||
{
|
||||
settermmode(pager->tty_fd, pager->tty_fd_old_termmode);
|
||||
if ( pager->tty_fd != 0 )
|
||||
close(pager->tty_fd);
|
||||
}
|
||||
|
||||
void pager_prompt(struct pager* pager)
|
||||
{
|
||||
if ( pager->input_prompt_name[0] )
|
||||
dprintf(pager->out_fd, "\e[47;30m%s\e[m\e[J", pager->input_prompt_name);
|
||||
else
|
||||
dprintf(pager->out_fd, ":");
|
||||
uint32_t codepoint;
|
||||
while ( read(pager->tty_fd, &codepoint, sizeof(codepoint)) == sizeof(codepoint) )
|
||||
{
|
||||
if ( codepoint == L'\n' || KBKEY_DECODE(codepoint) == KBKEY_DOWN )
|
||||
{
|
||||
dprintf(pager->out_fd, "\r\e[J");
|
||||
pager->allowed_lines++;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( KBKEY_DECODE(codepoint) == KBKEY_PGDOWN )
|
||||
{
|
||||
dprintf(pager->out_fd, "\r\e[J");
|
||||
pager->allowed_lines = pager->out_fd_winsize.ws_row - 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( KBKEY_DECODE(codepoint) == KBKEY_END )
|
||||
{
|
||||
dprintf(pager->out_fd, "\r\e[J");
|
||||
pager->allowed_lines = SIZE_MAX;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( codepoint == L'q' || codepoint == L'Q' )
|
||||
{
|
||||
dprintf(pager->out_fd, "\r\e[J");
|
||||
pager->quiting = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
error(1, errno, "/dev/tty");
|
||||
}
|
||||
|
||||
bool pager_would_push_newline_scroll(struct pager* pager)
|
||||
{
|
||||
return pager->out_fd_winsize.ws_row <= pager->current_wcp.wcp_row + 1;
|
||||
}
|
||||
|
||||
bool pager_would_push_char_overflow(struct pager* pager, wchar_t wc)
|
||||
{
|
||||
if ( wc == L'\r' )
|
||||
return false;
|
||||
return pager->out_fd_winsize.ws_col <= pager->current_wcp.wcp_col;
|
||||
}
|
||||
|
||||
bool pager_would_push_char_scroll(struct pager* pager, wchar_t wc)
|
||||
{
|
||||
if ( wc == L'\n' )
|
||||
return pager_would_push_newline_scroll(pager);
|
||||
if ( pager_would_push_char_overflow(pager, wc) )
|
||||
return pager_would_push_newline_scroll(pager);
|
||||
return false;
|
||||
}
|
||||
|
||||
void pager_push_newline(struct pager* pager)
|
||||
{
|
||||
if ( 0 < pager->allowed_lines && pager->allowed_lines != SIZE_MAX )
|
||||
pager->allowed_lines--;
|
||||
if ( pager->current_wcp.wcp_row + 1 < pager->out_fd_winsize.ws_row )
|
||||
{
|
||||
pager->current_wcp.wcp_row++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( 0 < pager->begun_wcp.wcp_row )
|
||||
pager->begun_wcp.wcp_row--;
|
||||
}
|
||||
pager->current_wcp.wcp_col = 0;
|
||||
}
|
||||
|
||||
bool pager_is_out_of_allowed_lines(struct pager* pager, wchar_t wc)
|
||||
{
|
||||
if ( pager->allowed_lines == 1 && wc != L'\n' &&
|
||||
pager_would_push_char_overflow(pager, wc) )
|
||||
return true;
|
||||
return pager->allowed_lines == 0;
|
||||
}
|
||||
|
||||
void pager_push_final_char(struct pager* pager, wchar_t wc)
|
||||
{
|
||||
if ( !wc )
|
||||
return;
|
||||
|
||||
while ( pager_is_out_of_allowed_lines(pager, wc) )
|
||||
{
|
||||
pager_prompt(pager);
|
||||
if ( pager->quiting )
|
||||
return;
|
||||
}
|
||||
|
||||
mbstate_t ps;
|
||||
memcpy(&ps, &pager->out_ps, sizeof(mbstate_t));
|
||||
char mb[MB_CUR_MAX];
|
||||
size_t mb_size = wcrtomb(mb, wc, &ps);
|
||||
if ( mb_size == (size_t) -1 )
|
||||
{
|
||||
memset(&pager->out_ps, 0, sizeof(pager->out_ps));
|
||||
return;
|
||||
}
|
||||
|
||||
if ( writeall(pager->out_fd, mb, mb_size) < mb_size )
|
||||
error(1, errno, "write: <stdout>");
|
||||
memcpy(&pager->out_ps, &ps, sizeof(mbstate_t));
|
||||
|
||||
if ( wc == L'\n' )
|
||||
{
|
||||
pager_push_newline(pager);
|
||||
}
|
||||
else if ( wc == L'\t' )
|
||||
{
|
||||
if ( pager_would_push_char_overflow(pager, ' ') )
|
||||
pager_push_newline(pager);
|
||||
do
|
||||
{
|
||||
if ( pager_would_push_char_overflow(pager, ' ') )
|
||||
break;
|
||||
pager->current_wcp.wcp_col++;
|
||||
} while ( pager->current_wcp.wcp_col % 8 != 0 );
|
||||
}
|
||||
else if ( wc == L'\r' )
|
||||
{
|
||||
pager->current_wcp.wcp_col = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( pager_would_push_char_overflow(pager, wc) )
|
||||
pager_push_newline(pager);
|
||||
pager->current_wcp.wcp_col++;
|
||||
}
|
||||
}
|
||||
|
||||
void pager_push_char(struct pager* pager, wchar_t wc)
|
||||
{
|
||||
if ( wc < 32 && wc != L'\t' && wc != L'\n' )
|
||||
{
|
||||
dprintf(pager->out_fd, "\e[97m");
|
||||
pager_push_char(pager, L'^');
|
||||
pager_push_char(pager, L'@' + wc);
|
||||
dprintf(pager->out_fd, "\e[m");
|
||||
}
|
||||
else
|
||||
{
|
||||
pager_push_final_char(pager, wc);
|
||||
}
|
||||
}
|
||||
|
||||
void pager_push_byte(struct pager* pager, unsigned char byte)
|
||||
{
|
||||
if ( pager->quiting )
|
||||
return;
|
||||
|
||||
wchar_t wc;
|
||||
size_t amount = mbrtowc(&wc, (const char*) &byte, 1, &pager->in_ps);
|
||||
if ( amount == (size_t) -2 )
|
||||
return;
|
||||
if ( amount == (size_t) -1 )
|
||||
{
|
||||
wc = 0xFFFD /* REPLACEMENT CHARACTER */;
|
||||
memset(&pager->in_ps, 0, sizeof(pager->in_ps));
|
||||
}
|
||||
pager_push_char(pager, wc);
|
||||
}
|
||||
|
||||
void pager_simple_output_fd(struct pager* pager, int fd, const char* fdpath)
|
||||
{
|
||||
unsigned char buffer[4096];
|
||||
ssize_t in_amount;
|
||||
while ( 0 < (in_amount = read(fd, buffer, sizeof(buffer))) )
|
||||
{
|
||||
if ( writeall(pager->out_fd, buffer, in_amount) < (size_t) in_amount )
|
||||
error(1, errno, "<stdout>");
|
||||
}
|
||||
if ( in_amount < 0 )
|
||||
error(1, errno, "%s", fdpath);
|
||||
}
|
||||
|
||||
void pager_push_fd(struct pager* pager, int fd, const char* fdpath)
|
||||
{
|
||||
if ( pager->quiting )
|
||||
return;
|
||||
|
||||
if ( !strcmp(fdpath, "<stdin>") )
|
||||
pager->input_prompt_name = "";
|
||||
else
|
||||
pager->input_prompt_name = fdpath;
|
||||
// TODO: In this case, we should disable echoing and read from the terminal
|
||||
// anyways.
|
||||
if ( isatty(fd) )
|
||||
error(1, errno, "/dev/tty");
|
||||
if ( !pager->out_fd_tty )
|
||||
return pager_simple_output_fd(pager, fd, fdpath);
|
||||
unsigned char buffer[4096];
|
||||
ssize_t in_amount;
|
||||
while ( !pager->quiting &&
|
||||
0 < (in_amount = read(fd, buffer, sizeof(buffer))) )
|
||||
{
|
||||
for ( ssize_t i = 0; i < in_amount; i++ )
|
||||
pager_push_byte(pager, buffer[i]);
|
||||
}
|
||||
if ( in_amount < 0 )
|
||||
error(1, errno, "%s", fdpath);
|
||||
}
|
||||
|
||||
void pager_push_path(struct pager* pager, const char* path)
|
||||
{
|
||||
if ( pager->quiting )
|
||||
return;
|
||||
|
||||
if ( !strcmp(path, "-") )
|
||||
return pager_push_fd(pager, 0, "<stdin>");
|
||||
int fd = open(path, O_RDONLY);
|
||||
if ( fd < 0 )
|
||||
error(1, errno, "%s", path);
|
||||
pager_push_fd(pager, fd, path);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void compact_arguments(int* argc, char*** argv)
|
||||
{
|
||||
for ( int i = 0; i < *argc; i++ )
|
||||
{
|
||||
while ( i < *argc && !(*argv)[i] )
|
||||
{
|
||||
for ( int n = i; n < *argc; n++ )
|
||||
(*argv)[n] = (*argv)[n+1];
|
||||
(*argc)--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void help(FILE* fp, const char* argv0)
|
||||
{
|
||||
fprintf(fp, "Usage: %s [OPTION]... [FILES]...\n", argv0);
|
||||
fprintf(fp, "Displays files one page at a time.\n");
|
||||
}
|
||||
|
||||
void version(FILE* fp, const char* argv0)
|
||||
{
|
||||
fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR);
|
||||
fprintf(fp, "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n");
|
||||
fprintf(fp, "This is free software: you are free to change and redistribute it.\n");
|
||||
fprintf(fp, "There is NO WARRANTY, to the extent permitted by law.\n");
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if ( isatty(0) && argc < 2 )
|
||||
{
|
||||
printf("usage: %s [FILE] ...\n", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
setlocale(LC_ALL, "");
|
||||
|
||||
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; }
|
||||
struct winsize ws;
|
||||
if ( tcgetwinsize(0, &ws) )
|
||||
error(1, errno, "tcgetwinsize");
|
||||
const int HEIGHT = ws.ws_row;
|
||||
const int WIDTH = ws.ws_col;
|
||||
int linesleft = HEIGHT-1;
|
||||
int result = 0;
|
||||
size_t charleft = WIDTH;
|
||||
const char* argv0 = argv[0];
|
||||
for ( int i = 1; i < argc; i++ )
|
||||
{
|
||||
int fd = ( strcmp(argv[i], "-") == 0 ) ? stdinfd : open(argv[i], O_RDONLY);
|
||||
if ( fd < 0 ) { result = 1; perror(argv[i]); continue; }
|
||||
const size_t BUFFER_SIZE = 4096;
|
||||
char buffer[BUFFER_SIZE];
|
||||
while ( true )
|
||||
const char* arg = argv[i];
|
||||
if ( arg[0] != '-' )
|
||||
continue;
|
||||
argv[i] = NULL;
|
||||
if ( !strcmp(arg, "--") )
|
||||
break;
|
||||
if ( arg[1] != '-' )
|
||||
{
|
||||
ssize_t numbytes = read(fd, buffer, BUFFER_SIZE);
|
||||
if ( !numbytes ) { break; }
|
||||
if ( numbytes < 0 )
|
||||
while ( char c = *++arg ) switch ( c )
|
||||
{
|
||||
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; }
|
||||
if ( eol && linesleft <= 1 )
|
||||
{
|
||||
printf("\n--pager--");
|
||||
fflush(stdout);
|
||||
settermmode(0, TERMMODE_KBKEY | TERMMODE_SIGNAL);
|
||||
bool doexit = false;
|
||||
int kbkey;
|
||||
do
|
||||
{
|
||||
uint32_t codepoint;
|
||||
ssize_t numbytes = read(0, &codepoint, sizeof(codepoint));
|
||||
if ( !numbytes ) { exit(0); }
|
||||
if ( numbytes < 0 ) { error(1, errno, "read(stdin)"); }
|
||||
if ( numbytes < (ssize_t) sizeof(codepoint) ) { error(1, errno, "bad stdin"); }
|
||||
if ( !(kbkey = KBKEY_DECODE(codepoint)) ) { continue; }
|
||||
if ( kbkey == KBKEY_DOWN ) { break; }
|
||||
if ( kbkey == KBKEY_PGDOWN ) { linesleft = HEIGHT-1; break; }
|
||||
if ( kbkey == -KBKEY_Q ) { doexit = true; break; }
|
||||
} while ( kbkey != KBKEY_ENTER );
|
||||
printf("\r\e[J");
|
||||
if ( doexit ) { exit(result); }
|
||||
continue;
|
||||
}
|
||||
if ( eol && linesleft ) { linesleft--; }
|
||||
printf("%c", c);
|
||||
charleft--;
|
||||
if ( c == '\t' ) { charleft &= ~(8-1); }
|
||||
default:
|
||||
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
|
||||
help(stderr, argv0);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if ( fd != stdinfd ) { close(fd); }
|
||||
else if ( !strcmp(arg, "--help") )
|
||||
help(stdout, argv0), exit(0);
|
||||
else if ( !strcmp(arg, "--version") )
|
||||
version(stdout, argv0), exit(0);
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "%s: unknown option: %s\n", argv0, arg);
|
||||
help(stderr, argv0);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
compact_arguments(&argc, &argv);
|
||||
|
||||
struct pager pager;
|
||||
memset(&pager, 0, sizeof(pager));
|
||||
pager_init(&pager);
|
||||
|
||||
if ( argc == 1 )
|
||||
{
|
||||
if ( pager.tty_fd == 0 )
|
||||
error(1, 0, "missing file operand");
|
||||
pager_push_fd(&pager, 0, "<stdin>");
|
||||
}
|
||||
else for ( int i = 1; i < argc; i++ )
|
||||
{
|
||||
pager_push_path(&pager, argv[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue