This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnostt.c
179 lines (151 loc) · 3.33 KB
/
nostt.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
/* nostt.c - Copyright (c) 2018, Sijmen J. Mulder (see LICENSE.md) */
#define USAGE "usage: nostt [page]"
#define _WITH_GETLINE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
#include "api.h"
#include "compat.h"
#ifdef _WIN32
# include <windows.h>
#endif
#ifndef COMPAT_ERR
# include <err.h>
#endif
static void
enablecolor()
{
#ifdef _WIN32
HANDLE hout;
DWORD mode = 0;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(hout, &mode);
/* ENABLE_VIRTUAL_TERMINAL_PROCESSING */
SetConsoleMode(hout, mode|4);
#endif
}
static int
enveq(const char *name, const char *val)
{
char *actual;
return (actual = getenv(name)) && strcmp(actual, val) == 0;
}
static void
putcell_color(struct ttpage *page, int line, int col)
{
struct ttattrs *attrs;
struct ttattrs *prevattrs;
int attrsflag;
attrs = &page->attrs[line][col];
if (!col)
attrsflag = 1;
else {
prevattrs = &page->attrs[line][col-1];
attrsflag = memcmp(attrs, prevattrs, sizeof(*attrs));
}
if (attrsflag)
printf("\033[%d;%dm", 30 + attrs->fg, 40 + attrs->bg);
putwchar(page->chars[line][col]);
}
static const char *
prompt(const char *suggestion)
{
static char *input = NULL;
static ssize_t len;
static size_t cap;
while (1) {
if (suggestion)
printf("page [%s]? ", suggestion);
else
printf("page? ");
fflush(stdout);
if ((len = getline(&input, &cap, stdin)) == -1)
return NULL;
if (len && input[len-1] == '\n')
input[--len] = '\0';
if (!strcmp("q", input))
return NULL;
else if (len)
return input;
else
return suggestion;
}
}
int
main(int argc, char **argv)
{
const char *id;
struct ttpage page;
enum tterr ret;
int withcolor = 0;
int interactive = 0;
int line, col;
#ifdef __OpenBSD__
if (unveil("/etc/ssl/cert.pem", "r") == -1)
err(1, "unveil: /etc/ssl/cert.pem");
if (unveil("/etc/ssl/openssl.cnf", "r") == -1)
err(1, "unveil: /etc/ssl/openssl.cnf");
if (unveil("/usr/share/locale", "r") == -1)
err(1, "unveil: /usr/share/locale");
if (pledge("stdio rpath inet dns", NULL) == -1)
err(1, "pledge");
#endif
(void) argc;
argv0 = *argv;
setlocale(LC_ALL, "");
if (argv[1]) {
if (argv[2] || *argv[1] == '-')
errx(1, USAGE);
id = argv[1];
} else {
id = "100";
interactive = 1;
}
withcolor =
!enveq("NO_COLOR", "1") &&
!enveq("CLICOLOR", "0") &&
(enveq("CLICOLOR_FORCE", "1") || isatty(STDOUT_FILENO));
if (withcolor)
enablecolor();
while (1) {
ret = tt_get(id, &page);
if (ret != TT_OK) {
if (!interactive)
errx(1, "%s", tt_errstr(ret));
warnx("%s", tt_errstr(ret));
puts("(q or ^C to exit)\n");
if (!(id = prompt(NULL)))
return 0;
continue;
}
if (withcolor) {
for (line = 0; line < TT_NLINES; line++) {
for (col = 0; col < TT_NCOLS; col++)
putcell_color(&page, line, col);
puts("\033[0m");
}
} else {
for (line = 0; line < TT_NLINES; line++)
printf("%.*ls\n", TT_NCOLS,
page.chars[line]);
}
if (interactive) {
putchar('\n');
id = prompt(
*page.nextsub ? page.nextsub :
*page.nextpage ? page.nextpage : NULL);
if (!id)
return 0;
} else if (id == argv[0] && strchr(id, '-')) {
/* Only print requested subpage */
return 0;
} else if (*page.nextsub) {
putchar('\n');
id = page.nextsub;
} else
return 0;
}
}