1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/utils/pager.c

736 lines
16 KiB
C
Raw Normal View History

/*
2016-11-20 10:50:40 -05:00
* Copyright (c) 2014, 2015, 2016 Jonas 'Sortie' Termansen.
*
* 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.
*
* pager.c
* Displays files one page at a time.
*/
2016-11-20 10:50:40 -05:00
#include <sys/ioctl.h>
2014-12-27 17:43:39 -05:00
#include <assert.h>
2016-10-04 02:27:04 -04:00
#include <err.h>
2014-12-27 17:43:39 -05:00
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
2016-02-28 18:40:20 -05:00
#include <stdbool.h>
2016-11-20 10:50:40 -05:00
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
2014-12-27 17:43:39 -05:00
#include <unistd.h>
#include <wchar.h>
2015-08-14 09:16:27 -04:00
#define CONTROL_SEQUENCE_MAX 128
enum control_state
{
CONTROL_STATE_NONE = 0,
CONTROL_STATE_CSI,
CONTROL_STATE_COMMAND,
};
2015-12-27 15:43:41 -05:00
struct line
2014-12-27 17:43:39 -05:00
{
2015-12-27 15:43:41 -05:00
char* content;
size_t content_used;
size_t content_length;
2014-12-27 17:43:39 -05:00
};
2016-11-20 10:50:40 -05:00
static struct termios restore_tio;
static bool restore_scrollback;
static int tty_fd;
static bool stdout_is_tty;
static struct winsize winsize;
static mbstate_t in_ps;
static mbstate_t out_ps;
static const char* input_prompt_name;
static size_t possible_lines;
static size_t allowed_lines;
static bool quiting;
static bool flag_raw_control_chars;
static bool flag_color_sequences;
static enum control_state control_state;
static wchar_t control_sequence[CONTROL_SEQUENCE_MAX];
static size_t control_sequence_length;
static bool input_set_color;
static struct line* lines;
static size_t lines_used;
static size_t lines_length;
static enum control_state incoming_control_state;
static struct line* incoming_line;
static size_t incoming_line_width;
static size_t current_line;
static size_t current_line_offset;
static bool allowance_ever_exhausted;
static bool skipping_to_end;
static bool next_bold;
static bool next_underline;
static void exit_restore_tio(void)
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
if ( restore_scrollback )
dprintf(1, "\e[?1049l");
if ( tcsetattr(tty_fd, TCSADRAIN, &restore_tio) < 0 )
warn("tcsetattr");
}
2014-12-27 17:43:39 -05:00
2016-11-20 10:50:40 -05:00
static void init(void)
{
2016-11-20 10:50:40 -05:00
tty_fd = 0;
if ( !isatty(tty_fd) )
{
if ( (tty_fd = open("/dev/tty", O_RDONLY)) < 0 )
err(1, "/dev/tty");
if ( !isatty(tty_fd) )
err(1, "/dev/tty");
}
if ( tcgetattr(tty_fd, &restore_tio) < 0 )
err(1, "tcgetattr");
if ( atexit(exit_restore_tio) < 0 )
err(1, "atexit");
struct termios tio = restore_tio;
tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
if ( tcsetattr(tty_fd, TCSADRAIN, &tio) < 0 )
err(1, "tcsetattr");
if ( (stdout_is_tty = isatty(1)) )
{
if ( ioctl(1, TIOCGWINSZ, &winsize) < 0 )
err(1, "ioctl: TIOCGWINSZ");
possible_lines = winsize.ws_row - 1;
allowed_lines = possible_lines;
const char* term = getenv("TERM");
if ( term &&
strcmp(term, "sortix") != 0 &&
strncmp(term, "sortix-", strlen("sortix-")) != 0 )
{
dprintf(1, "\e[?1049h\e[H");
restore_scrollback = true;
}
}
2016-11-20 10:50:40 -05:00
memset(&in_ps, 0, sizeof(in_ps));
memset(&out_ps, 0, sizeof(out_ps));
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
static char next_char(void)
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
char c;
if ( read(tty_fd, &c, 1) < 1 )
err(1, "/dev/tty");
return c;
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
static void prompt(bool at_end)
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
const char* pre = input_set_color ? "" : "\e[47;30m";
const char* post = input_set_color ? "" : "\e[m";
2015-12-27 15:43:41 -05:00
if ( at_end )
2016-11-20 10:50:40 -05:00
dprintf(1, "%s(END)%s\e[J", pre, post);
else if ( input_prompt_name[0] )
dprintf(1, "%s%s%s\e[J", pre, input_prompt_name, post);
2014-12-27 17:43:39 -05:00
else
2016-11-20 10:50:40 -05:00
dprintf(1, ":");
while ( true )
{
2016-11-20 10:50:40 -05:00
char c;
char buffer[CONTROL_SEQUENCE_MAX + 1];
size_t buffer_used = 0;
buffer[buffer_used++] = c = next_char();
if ( c == '\e' &&
(buffer[buffer_used++] = c = next_char()) == '[' )
{
2016-11-20 10:50:40 -05:00
while ( buffer_used < CONTROL_SEQUENCE_MAX )
{
char c = next_char();
buffer[buffer_used++] = c;
if ( '@' <= c && c <= '~' )
break;
}
}
buffer[buffer_used] = '\0';
if ( !strcmp(buffer, "\n") || !strcmp(buffer, "\e[B") )
{
dprintf(1, "\r\e[J");
allowed_lines++;
2014-12-27 17:43:39 -05:00
return;
}
2016-11-20 10:50:40 -05:00
if ( !strcmp(buffer, "\e[A") )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
if ( current_line <= possible_lines )
2015-12-27 15:43:41 -05:00
continue;
2016-11-20 10:50:40 -05:00
dprintf(1, "\e[2J\e[H");
current_line -= possible_lines + 1;
current_line_offset = 0;
allowed_lines = possible_lines;
2015-12-27 15:43:41 -05:00
return;
}
2016-11-20 10:50:40 -05:00
if ( !strcmp(buffer, " ") || !strcmp(buffer, "\e[6~") )
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
dprintf(1, "\r\e[J");
allowed_lines = possible_lines;
2015-12-27 15:43:41 -05:00
return;
}
2016-11-20 10:50:40 -05:00
if ( !strcmp(buffer, "\e[5~") )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
if ( current_line <= possible_lines )
2015-12-27 15:43:41 -05:00
continue;
2016-11-20 10:50:40 -05:00
size_t distance = possible_lines;
if ( current_line - possible_lines < distance )
distance = current_line - possible_lines;
dprintf(1, "\e[2J\e[H");
current_line -= possible_lines + distance;
current_line_offset = 0;
allowed_lines = possible_lines;
2014-12-27 17:43:39 -05:00
return;
}
2016-11-20 10:50:40 -05:00
if ( !strcmp(buffer, "\e[F") || !strcmp(buffer, "\e[4~") )
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
dprintf(1, "\r\e[J");
skipping_to_end = true;
allowed_lines = SIZE_MAX;
2014-12-27 17:43:39 -05:00
return;
}
2016-11-20 10:50:40 -05:00
if ( !strcmp(buffer, "\e[H") || !strcmp(buffer, "\e[1~") )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
if ( current_line <= possible_lines )
2015-12-27 15:43:41 -05:00
continue;
2016-11-20 10:50:40 -05:00
dprintf(1, "\e[2J\e[H");
current_line = 0;
current_line_offset = 0;
allowed_lines = possible_lines;
2015-12-27 15:43:41 -05:00
return;
}
2016-11-20 10:50:40 -05:00
if ( !strcmp(buffer, "q") || !strcmp(buffer, "Q") )
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
dprintf(1, "\r\e[J");
quiting = true;
2014-12-27 17:43:39 -05:00
return;
}
}
2016-11-20 10:50:40 -05:00
err(1, "/dev/tty");
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
static void line_push_char(struct line* line, char c)
2014-12-27 17:43:39 -05:00
{
2015-12-27 15:43:41 -05:00
if ( line->content_used == line->content_length )
{
size_t new_length = 2 * line->content_length;
if ( new_length == 0 )
new_length = 128;
char* new_content = (char*) realloc(line->content, new_length);
if ( !new_content )
2016-11-20 10:50:40 -05:00
err(1, "malloc");
2015-12-27 15:43:41 -05:00
line->content = new_content;
line->content_length = new_length;
}
line->content[line->content_used++] = c;
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
static void line_push_string(struct line* line, const char* str)
{
for ( size_t i = 0; str[i]; i++ )
line_push_char(line, str[i]);
}
static struct line* continue_line(void)
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
if ( incoming_line )
return incoming_line;
if ( lines_used == lines_length )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
size_t new_length = 2 * lines_length;
2015-12-27 15:43:41 -05:00
if ( new_length == 0 )
new_length = 128;
struct line* new_lines = (struct line*)
2016-11-20 10:50:40 -05:00
reallocarray(lines, new_length, sizeof(struct line));
2015-12-27 15:43:41 -05:00
if ( !new_lines )
2016-11-20 10:50:40 -05:00
err(1, "malloc");
lines = new_lines;
lines_length = new_length;
}
incoming_line = &lines[lines_used++];
memset(incoming_line, 0, sizeof(*incoming_line));
incoming_line_width = 0;
return incoming_line;
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
static void finish_line(void)
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
struct line* line = incoming_line;
2015-12-27 15:43:41 -05:00
assert(line);
char* final_content = (char*) realloc(line->content, line->content_used);
if ( final_content )
line->content = final_content;
2016-11-20 10:50:40 -05:00
incoming_line = NULL;
incoming_line_width = 0;
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
static struct line* next_line(void)
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
finish_line();
return continue_line();
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
static void push_wchar(wchar_t wc)
2014-12-27 17:43:39 -05:00
{
2015-12-27 15:43:41 -05:00
bool newline = false;
int width;
2016-11-20 10:50:40 -05:00
struct line* line = continue_line();
2014-12-27 17:43:39 -05:00
2016-11-20 10:50:40 -05:00
if ( incoming_control_state == CONTROL_STATE_CSI )
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
incoming_control_state = CONTROL_STATE_NONE;
2015-12-27 15:43:41 -05:00
if ( wc == '[' )
2016-11-20 10:50:40 -05:00
incoming_control_state = CONTROL_STATE_COMMAND;
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
else if ( incoming_control_state == CONTROL_STATE_COMMAND )
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
incoming_control_state = CONTROL_STATE_NONE;
2015-12-27 15:43:41 -05:00
if ( ('0' <= wc && wc <= '9') ||
wc == L';' || wc == L':' || wc == L'?' )
2016-11-20 10:50:40 -05:00
incoming_control_state = CONTROL_STATE_COMMAND;
2014-12-27 17:43:39 -05:00
}
2015-12-27 15:43:41 -05:00
else if ( wc == L'\b' )
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
if ( incoming_line_width )
incoming_line_width--;
while ( line->content_used &&
(line->content[line->content_used-1] & 0xC0) == 0x80 )
line->content_used--;
if ( line->content_used )
{
char c = line->content[--line->content_used];
if ( c == '_' )
next_underline = true;
else if ( c == ' ' )
next_bold = false;
else
next_bold = true;
}
return;
2015-08-14 09:16:27 -04:00
}
2015-12-27 15:43:41 -05:00
else if ( wc == L'\e' )
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
incoming_control_state = CONTROL_STATE_CSI;
2015-12-27 15:43:41 -05:00
}
else if ( wc == L'\n' )
{
newline = true;
2014-12-27 17:43:39 -05:00
}
else if ( wc == L'\t' )
{
2016-11-20 10:50:40 -05:00
if ( winsize.ws_col == incoming_line_width )
line = next_line();
2014-12-27 17:43:39 -05:00
do
{
2016-11-20 10:50:40 -05:00
if ( winsize.ws_col == incoming_line_width )
break;
2016-11-20 10:50:40 -05:00
incoming_line_width++;
} while ( incoming_line_width % 8 != 0 );
2014-12-27 17:43:39 -05:00
}
else if ( wc == L'\r' )
{
2016-11-20 10:50:40 -05:00
incoming_line_width = 0;
2015-12-27 15:43:41 -05:00
}
else if ( wc == 127 )
{
}
else if ( 0 <= (width = wcwidth(wc)) )
{
2016-11-20 10:50:40 -05:00
size_t left = winsize.ws_col - incoming_line_width;
2015-12-27 15:43:41 -05:00
if ( left < (size_t) width )
2016-11-20 10:50:40 -05:00
line = next_line();
incoming_line_width += width;
2014-12-27 17:43:39 -05:00
}
else
{
2015-12-27 15:43:41 -05:00
// TODO: What can cause this and how to handle it?
2014-12-27 17:43:39 -05:00
}
2015-12-27 15:43:41 -05:00
char mb[MB_CUR_MAX];
2016-11-20 10:50:40 -05:00
size_t amount = wcrtomb(mb, wc, &out_ps);
2015-12-27 15:43:41 -05:00
if ( amount != (size_t) -1 )
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
if ( next_bold && next_underline )
line_push_string(line, "\e[1;4m");
else if ( next_bold )
line_push_string(line, "\e[1m");
else if ( next_underline )
line_push_string(line, "\e[4m");
2015-12-27 15:43:41 -05:00
for ( size_t i = 0; i < amount; i++ )
2016-11-20 10:50:40 -05:00
line_push_char(line, mb[i]);
if ( next_bold && next_underline )
line_push_string(line, "\e[22;24m");
else if ( next_bold )
line_push_string(line, "\e[22m");
else if ( next_underline )
line_push_string(line, "\e[24m");
next_bold = false;
next_underline = false;
2015-08-14 09:16:27 -04:00
}
2015-12-27 15:43:41 -05:00
if ( newline )
2016-11-20 10:50:40 -05:00
finish_line();
2015-12-27 15:43:41 -05:00
}
2016-11-20 10:50:40 -05:00
static bool push_wchar_is_escaped(wchar_t wc)
2015-12-27 15:43:41 -05:00
{
if ( wc == '\b' &&
2016-11-20 10:50:40 -05:00
(flag_raw_control_chars || flag_color_sequences) )
2015-12-27 15:43:41 -05:00
return false;
return wc < 32 && wc != L'\t' && wc != L'\n';
}
2016-11-20 10:50:40 -05:00
static void push_wchar_escape(wchar_t wc)
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
if ( push_wchar_is_escaped(wc) )
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
push_wchar(L'^');
//push_wchar(L'\b');
//push_wchar(L'^');
push_wchar(L'@' + wc);
//push_wchar(L'\b');
//push_wchar(L'@' + wc);
2014-12-27 17:43:39 -05:00
}
else
{
2016-11-20 10:50:40 -05:00
push_wchar(wc);
2014-12-27 17:43:39 -05:00
}
}
2016-11-20 10:50:40 -05:00
static void control_sequence_begin(void)
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
control_sequence_length = 0;
2015-08-14 09:16:27 -04:00
}
2016-11-20 10:50:40 -05:00
static void control_sequence_accept(void)
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
for ( size_t i = 0; i < control_sequence_length; i++ )
push_wchar(control_sequence[i]);
control_sequence_length = 0;
control_state = CONTROL_STATE_NONE;
2015-08-14 09:16:27 -04:00
}
2016-11-20 10:50:40 -05:00
static void control_sequence_reject(void)
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
for ( size_t i = 0; i < control_sequence_length; i++ )
push_wchar_escape(control_sequence[i]);
control_sequence_length = 0;
control_state = CONTROL_STATE_NONE;
2015-08-14 09:16:27 -04:00
}
2016-11-20 10:50:40 -05:00
static void control_sequence_push(wchar_t wc)
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
if ( flag_raw_control_chars )
return push_wchar(wc);
if ( CONTROL_SEQUENCE_MAX <= control_sequence_length )
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
control_sequence_reject();
push_wchar_escape(wc);
2015-12-27 15:43:41 -05:00
return;
2015-08-14 09:16:27 -04:00
}
2016-11-20 10:50:40 -05:00
control_sequence[control_sequence_length++] = wc;
2015-08-14 09:16:27 -04:00
}
2016-11-20 10:50:40 -05:00
static void control_sequence_finish(wchar_t wc)
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
control_sequence_push(wc);
if ( control_state == CONTROL_STATE_NONE )
2015-08-14 09:16:27 -04:00
return;
2016-02-24 16:57:41 -05:00
if ( wc == L'm' )
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
input_set_color = true;
return control_sequence_accept();
2015-08-14 09:16:27 -04:00
}
2016-11-20 10:50:40 -05:00
if ( !flag_raw_control_chars )
return control_sequence_reject();
control_sequence_accept();
2015-08-14 09:16:27 -04:00
}
2016-11-20 10:50:40 -05:00
static void push_wchar_filter(wchar_t wc)
2014-12-27 17:43:39 -05:00
{
2015-12-27 15:43:41 -05:00
if ( wc == L'\e' &&
2016-11-20 10:50:40 -05:00
(flag_raw_control_chars || flag_color_sequences) &&
control_state == CONTROL_STATE_NONE )
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
control_sequence_begin();
control_sequence_push(wc);
control_state = CONTROL_STATE_CSI;
2015-08-14 09:16:27 -04:00
return;
}
2016-11-20 10:50:40 -05:00
else if ( control_state == CONTROL_STATE_CSI )
2015-08-14 09:16:27 -04:00
{
2015-12-27 15:43:41 -05:00
if ( wc == L'[' )
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
control_sequence_push(wc);
control_state = CONTROL_STATE_COMMAND;
2015-08-14 09:16:27 -04:00
return;
}
2016-11-20 10:50:40 -05:00
control_sequence_reject();
2015-08-14 09:16:27 -04:00
}
2016-11-20 10:50:40 -05:00
else if ( control_state == CONTROL_STATE_COMMAND )
2015-08-14 09:16:27 -04:00
{
2015-12-27 15:43:41 -05:00
if ( ('0' <= wc && wc <= '9') ||
wc == L';' || wc == L':' || wc == L'?' )
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
control_sequence_push(wc);
2015-08-14 09:16:27 -04:00
return;
}
2016-11-20 10:50:40 -05:00
control_sequence_finish(wc);
2015-08-14 09:16:27 -04:00
return;
}
2016-11-20 10:50:40 -05:00
push_wchar_escape(wc);
2015-12-27 15:43:41 -05:00
}
2016-11-20 10:50:40 -05:00
static void push_byte(unsigned char byte)
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
if ( quiting )
2015-12-27 15:43:41 -05:00
return;
2015-08-14 09:16:27 -04:00
2014-12-27 17:43:39 -05:00
wchar_t wc;
2016-11-20 10:50:40 -05:00
size_t amount = mbrtowc(&wc, (const char*) &byte, 1, &in_ps);
2014-12-27 17:43:39 -05:00
if ( amount == (size_t) -2 )
return;
if ( amount == (size_t) -1 )
{
wc = 0xFFFD /* REPLACEMENT CHARACTER */;
2016-11-20 10:50:40 -05:00
memset(&in_ps, 0, sizeof(in_ps));
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
push_wchar_filter(wc);
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
static bool read_fd(int fd, const char* fdpath)
2014-12-27 17:43:39 -05:00
{
unsigned char buffer[4096];
2015-12-27 15:43:41 -05:00
ssize_t amount = read(fd, buffer, sizeof(buffer));
if ( amount < 0 )
2016-11-20 10:50:40 -05:00
err(1, "%s", fdpath);
2015-12-27 15:43:41 -05:00
for ( ssize_t i = 0; i < amount; i++ )
2016-11-20 10:50:40 -05:00
push_byte(buffer[i]);
2015-12-27 15:43:41 -05:00
return amount != 0;
}
2016-11-20 10:50:40 -05:00
static void simple_fd(int fd, const char* fdpath)
2015-12-27 15:43:41 -05:00
{
unsigned char buffer[4096];
ssize_t amount = 0;
while ( 0 < (amount = read(fd, buffer, sizeof(buffer))) )
{
2016-11-20 10:50:40 -05:00
ssize_t sofar = 0;
while ( sofar < amount )
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
ssize_t done = write(1, buffer + sofar, amount - sofar);
if ( done < 0 )
err(1, "<stdout>");
sofar += done;
2015-08-14 09:16:27 -04:00
}
2014-12-27 17:43:39 -05:00
}
2015-12-27 15:43:41 -05:00
if ( amount < 0 )
2016-11-20 10:50:40 -05:00
err(1, "%s", fdpath);
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
static bool can_page(void)
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
if ( current_line + 1 == lines_used )
2016-02-24 16:57:41 -05:00
{
2016-11-20 10:50:40 -05:00
struct line* line = &lines[current_line];
return current_line_offset < line->content_used;
2016-02-24 16:57:41 -05:00
}
2016-11-20 10:50:40 -05:00
return current_line + 1 < lines_used;
2015-12-27 15:43:41 -05:00
}
2016-11-20 10:50:40 -05:00
static void page(void)
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
struct line* line = &lines[current_line];
if ( current_line_offset < line->content_used )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
const char* buffer = line->content + current_line_offset;
size_t amount = line->content_used - current_line_offset;
size_t sofar = 0;
while ( sofar < amount )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
ssize_t done = write(1, buffer + sofar, amount - sofar);
if ( done < 0 )
err(1, "<stdout>");
sofar += done;
2015-12-27 15:43:41 -05:00
}
2016-11-20 10:50:40 -05:00
current_line_offset = line->content_used;
2015-12-27 15:43:41 -05:00
}
2016-11-20 10:50:40 -05:00
if ( current_line + 1 < lines_used )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
if ( allowed_lines != SIZE_MAX )
allowed_lines--;
current_line++;
current_line_offset = 0;
2015-12-27 15:43:41 -05:00
}
}
2016-11-20 10:50:40 -05:00
static void push_fd(int fd, const char* fdpath)
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
if ( quiting )
2014-12-27 17:43:39 -05:00
return;
if ( !strcmp(fdpath, "<stdin>") )
2016-11-20 10:50:40 -05:00
input_prompt_name = "";
2014-12-27 17:43:39 -05:00
else
2016-11-20 10:50:40 -05:00
input_prompt_name = fdpath;
2014-12-27 17:43:39 -05:00
// TODO: In this case, we should disable echoing and read from the terminal
2015-12-27 15:43:41 -05:00
// anyways. Remember to enable it again.
2014-12-27 17:43:39 -05:00
if ( isatty(fd) )
2016-11-20 10:50:40 -05:00
errx(1, "/dev/tty: Is a terminal");
if ( !stdout_is_tty )
return simple_fd(fd, fdpath);
2015-12-27 15:43:41 -05:00
bool eof = false;
2016-11-20 10:50:40 -05:00
while ( !quiting )
2015-08-14 09:16:27 -04:00
{
2016-11-20 10:50:40 -05:00
if ( !skipping_to_end )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
if ( allowed_lines == 0 )
2016-02-24 16:57:41 -05:00
{
2016-11-20 10:50:40 -05:00
allowance_ever_exhausted = true;
prompt(false);
2016-02-24 16:57:41 -05:00
continue;
}
2016-11-20 10:50:40 -05:00
if ( can_page() )
2016-02-24 16:57:41 -05:00
{
2016-11-20 10:50:40 -05:00
page();
2016-02-24 16:57:41 -05:00
continue;
}
2015-12-27 15:43:41 -05:00
}
if ( eof )
break;
2016-11-20 10:50:40 -05:00
if ( !read_fd(fd, fdpath) )
2015-12-27 15:43:41 -05:00
eof = true;
2015-08-14 09:16:27 -04:00
}
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
static void push_path(const char* path)
2014-12-27 17:43:39 -05:00
{
2016-11-20 10:50:40 -05:00
if ( quiting )
2014-12-27 17:43:39 -05:00
return;
if ( !strcmp(path, "-") )
2016-11-20 10:50:40 -05:00
return push_fd(0, "<stdin>");
2014-12-27 17:43:39 -05:00
int fd = open(path, O_RDONLY);
if ( fd < 0 )
2016-11-20 10:50:40 -05:00
err(1, "%s", path);
push_fd(fd, path);
2014-12-27 17:43:39 -05:00
close(fd);
}
static void compact_arguments(int* argc, char*** argv)
2014-12-27 17:43:39 -05:00
{
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)--;
}
}
}
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "");
2015-08-14 09:16:27 -04:00
if ( getenv("LESS") )
{
const char* options = getenv("LESS");
char c;
while ( (c = *options++) )
{
switch ( c )
{
case '-': break;
2016-11-20 10:50:40 -05:00
case 'r': flag_raw_control_chars = true; break;
case 'R': flag_color_sequences = true; break;
2015-08-14 09:16:27 -04:00
default: break;
}
}
}
2014-12-27 17:43:39 -05:00
for ( int i = 1; i < argc; i++ )
{
const char* arg = argv[i];
if ( arg[0] != '-' || !arg[1] )
2014-12-27 17:43:39 -05:00
continue;
argv[i] = NULL;
if ( !strcmp(arg, "--") )
break;
if ( arg[1] != '-' )
{
2016-02-28 18:40:20 -05:00
char c;
while ( (c = *++arg) ) switch ( c )
{
2016-11-20 10:50:40 -05:00
case 'r': flag_raw_control_chars = true; break;
case 'R': flag_color_sequences = true; break;
2014-12-27 17:43:39 -05:00
default:
2016-10-04 02:27:04 -04:00
errx(1, "unknown option -- '%c'", c);
}
}
2014-12-27 17:43:39 -05:00
else
2016-10-04 02:27:04 -04:00
errx(1, "unknown option: %s", arg);
}
2014-12-27 17:43:39 -05:00
compact_arguments(&argc, &argv);
2016-11-20 10:50:40 -05:00
init();
2014-12-27 17:43:39 -05:00
if ( argc == 1 )
{
2016-11-20 10:50:40 -05:00
if ( tty_fd == 0 )
errx(1, "missing file operand");
push_fd(0, "<stdin>");
2014-12-27 17:43:39 -05:00
}
else for ( int i = 1; i < argc; i++ )
{
2016-11-20 10:50:40 -05:00
push_path(argv[i]);
2014-12-27 17:43:39 -05:00
}
2016-11-20 10:50:40 -05:00
while ( stdout_is_tty &&
(allowance_ever_exhausted || restore_scrollback) &&
!quiting )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
if ( skipping_to_end )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
dprintf(1, "\e[2J\e[H");
2015-12-27 15:43:41 -05:00
size_t line = 0;
2016-11-20 10:50:40 -05:00
if ( possible_lines <= lines_used )
line = lines_used - possible_lines;
current_line = line;
current_line_offset = 0;
allowed_lines = possible_lines;
skipping_to_end = false;
2015-12-27 15:43:41 -05:00
}
2016-11-20 10:50:40 -05:00
bool cant_page = !can_page();
if ( cant_page || allowed_lines == 0 )
2015-12-27 15:43:41 -05:00
{
2016-11-20 10:50:40 -05:00
prompt(cant_page);
2015-12-27 15:43:41 -05:00
continue;
}
2016-11-20 10:50:40 -05:00
page();
2015-12-27 15:43:41 -05:00
}
2014-12-27 17:43:39 -05:00
return 0;
}