-
Notifications
You must be signed in to change notification settings - Fork 16
/
hx.c
192 lines (164 loc) · 4.19 KB
/
hx.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
/*
* This file is part of hx - a hex editor for the terminal.
*
* Copyright (c) 2016 Kevin Pors. See LICENSE for details.
*/
// Needed for Apple clang to recognize SIGWINCH
#ifdef __APPLE__
#define _DARWIN_C_SOURCE
#endif
#include "editor.h"
#include "util.h"
#include "undo.h"
// C99 includes
#include <stdio.h>
#include <string.h>
// POSIX and Linux cruft
#include <getopt.h>
#include <signal.h>
// hx defines. Not declared as const because we may want to adjust
// this by using a tool or whatever.
#ifndef HX_GIT_HASH
#define HX_GIT_HASH "unknown"
#endif
#ifndef HX_VERSION
#define HX_VERSION "1.0.0"
#endif
// Comp unit wide editor config.
static struct editor* g_ec;
volatile sig_atomic_t resizeflag; // flag indicating a SIGWINCH signal was received.
/*
* Exits the editor, frees some stuff and resets the terminal setting.
*/
static void editor_exit() {
editor_free(g_ec);
clear_screen();
disable_raw_mode();
term_state_restore();
}
/*
* Prints help to the stderr when invoked with -h or with unknown arguments.
* Explanation can be given for some extra information.
*/
static void print_help(const char* explanation) {
fprintf(stderr,
"%s"\
"usage: hx [-hv] [-o octets_per_line] [-g grouping_bytes] filename\n"\
"\n"
"Command options:\n"
" -h Print this cruft and exits\n"
" -v Version information\n"
" -o Amount of octets per line\n"
" -g Grouping of bytes in one line\n"
"\n"
"Currently, both these values are advised to be a multiple of 2\n"
"to prevent garbled display :)\n"
"\n"
"Report bugs to <krpors at gmail.com> or see <http://github.com/krpors/hx>\n"
, explanation);
}
/*
* Prints some version information back to the stdout.
*/
static void print_version() {
printf("hx version %s (git: %s)\n", HX_VERSION, HX_GIT_HASH);
}
#if 0
#include <unistd.h>
#include <ctype.h>
void debug_keypress() {
char c;
ssize_t nread;
// check == 0 to see if EOF.
while ((nread = read(STDIN_FILENO, &c, 1))) {
if (c == 'q') { exit(1); };
if (c == '\r' || c == '\n') { printf("\r\n"); continue; }
if (isprint(c)) {
printf("%c = %02x, ", c, c);
} else {
printf(". = %02x, ", c);
}
fflush(stdout);
}
}
#endif
/*
* Handles the SIGWINCH signal upon terminal resizing.
*/
static void handle_term_resize(int sig) {
(void)(sig); // suppress compiler warning about unused param.
// When the signal SIGWINCH is received, we set an sig_atomic_t variable
// called 'resizeflag' to 1. The read() in read_key() will be interrupted,
// and in the main loop we will detect that the flag is set.
// See https://www.securecoding.cert.org/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers
// for more information.
resizeflag = 1;
}
static void resize_term() {
clear_screen();
get_window_size(&(g_ec->screen_rows), &(g_ec->screen_cols));
}
int main(int argc, char* argv[]) {
char* file = NULL;
int octets_per_line = 16;
int grouping = 4;
int ch = 0;
while ((ch = getopt(argc, argv, "vhg:o:")) != -1) {
switch (ch) {
case 'v':
print_version();
return 0;
case 'h':
print_help("");
exit(0);
break;
case 'g':
// parse grouping
grouping = str2int(optarg, 2, 16, 4);
break;
case 'o':
// parse octets per line
octets_per_line = str2int(optarg, 16, 64, 16);
break;
default:
print_help("");
exit(1);
break;
}
}
// After all options are parsed, we expect a filename to open.
if (optind >= argc) {
print_help("error: expected filename\n");
exit(1);
}
file = argv[optind];
// Signal handler to react on screen resizing.
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = handle_term_resize;
if (sigaction(SIGWINCH, &act, NULL) == -1) {
perror("A sigaction() call failed");
abort();
}
resizeflag = 0;
// Editor configuration passed around.
g_ec = editor_init();
g_ec->octets_per_line = octets_per_line;
g_ec->grouping = grouping;
editor_openfile(g_ec, file);
enable_raw_mode();
term_state_save();
atexit(editor_exit);
clear_screen();
while (true) {
editor_refresh_screen(g_ec);
editor_process_keypress(g_ec);
if (resizeflag == 1) {
resize_term();
resizeflag = 0;
}
//debug_keypress();
}
editor_free(g_ec);
return EXIT_SUCCESS;
}