1 | /*
|
---|
2 | * Copyright (c) 2008 Tim Post
|
---|
3 | * Copyright (c) 2011, Martin Sucha
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <unistd.h>
|
---|
33 | #include <getopt.h>
|
---|
34 | #include <str.h>
|
---|
35 | #include <fcntl.h>
|
---|
36 | #include <io/console.h>
|
---|
37 | #include <io/color.h>
|
---|
38 | #include <io/style.h>
|
---|
39 | #include <io/keycode.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <vfs/vfs.h>
|
---|
42 | #include <assert.h>
|
---|
43 |
|
---|
44 | #include "config.h"
|
---|
45 | #include "util.h"
|
---|
46 | #include "errors.h"
|
---|
47 | #include "entry.h"
|
---|
48 | #include "cat.h"
|
---|
49 | #include "cmds.h"
|
---|
50 |
|
---|
51 | static const char *cmdname = "cat";
|
---|
52 | #define CAT_VERSION "0.0.1"
|
---|
53 | #define CAT_DEFAULT_BUFLEN 1024
|
---|
54 | #define CAT_FULL_FILE 0
|
---|
55 |
|
---|
56 | static const char *hexchars = "0123456789abcdef";
|
---|
57 |
|
---|
58 | static bool paging_enabled = false;
|
---|
59 | static size_t chars_remaining = 0;
|
---|
60 | static size_t lines_remaining = 0;
|
---|
61 | static sysarg_t console_cols = 0;
|
---|
62 | static sysarg_t console_rows = 0;
|
---|
63 | static bool should_quit = false;
|
---|
64 |
|
---|
65 | static console_ctrl_t *console = NULL;
|
---|
66 |
|
---|
67 | static struct option const long_options[] = {
|
---|
68 | { "help", no_argument, 0, 'h' },
|
---|
69 | { "version", no_argument, 0, 'v' },
|
---|
70 | { "head", required_argument, 0, 'H' },
|
---|
71 | { "tail", required_argument, 0, 't' },
|
---|
72 | { "buffer", required_argument, 0, 'b' },
|
---|
73 | { "more", no_argument, 0, 'm' },
|
---|
74 | { "hex", no_argument, 0, 'x' },
|
---|
75 | { 0, 0, 0, 0 }
|
---|
76 | };
|
---|
77 |
|
---|
78 | /* Dispays help for cat in various levels */
|
---|
79 | void help_cmd_cat(unsigned int level)
|
---|
80 | {
|
---|
81 | if (level == HELP_SHORT) {
|
---|
82 | printf("`%s' shows the contents of files\n", cmdname);
|
---|
83 | } else {
|
---|
84 | help_cmd_cat(HELP_SHORT);
|
---|
85 | printf(
|
---|
86 | "Usage: %s [options] <file1> [file2] [...]\n"
|
---|
87 | "Options:\n"
|
---|
88 | " -h, --help A short option summary\n"
|
---|
89 | " -v, --version Print version information and exit\n"
|
---|
90 | " -H, --head ## Print only the first ## bytes\n"
|
---|
91 | " -t, --tail ## Print only the last ## bytes\n"
|
---|
92 | " -b, --buffer ## Set the read buffer size to ##\n"
|
---|
93 | " -m, --more Pause after each screen full\n"
|
---|
94 | " -x, --hex Print bytes as hex values\n"
|
---|
95 | "Currently, %s is under development, some options don't work.\n",
|
---|
96 | cmdname, cmdname);
|
---|
97 | }
|
---|
98 |
|
---|
99 | return;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static void waitprompt()
|
---|
103 | {
|
---|
104 | console_set_pos(console, 0, console_rows-1);
|
---|
105 | console_set_color(console, COLOR_WHITE, COLOR_BLUE, 0);
|
---|
106 |
|
---|
107 | printf("ENTER/SPACE/PAGE DOWN - next page, "
|
---|
108 | "ESC/Q - quit, C - continue unpaged");
|
---|
109 | fflush(stdout);
|
---|
110 |
|
---|
111 | console_set_style(console, STYLE_NORMAL);
|
---|
112 | }
|
---|
113 |
|
---|
114 | static void waitkey()
|
---|
115 | {
|
---|
116 | kbd_event_t ev;
|
---|
117 |
|
---|
118 | while (true) {
|
---|
119 | if (!console_get_kbd_event(console, &ev)) {
|
---|
120 | return;
|
---|
121 | }
|
---|
122 | if (ev.type == KEY_PRESS) {
|
---|
123 | if (ev.key == KC_ESCAPE || ev.key == KC_Q) {
|
---|
124 | should_quit = true;
|
---|
125 | return;
|
---|
126 | }
|
---|
127 | if (ev.key == KC_C) {
|
---|
128 | paging_enabled = false;
|
---|
129 | return;
|
---|
130 | }
|
---|
131 | if (ev.key == KC_ENTER || ev.key == KC_SPACE ||
|
---|
132 | ev.key == KC_PAGE_DOWN) {
|
---|
133 | return;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | assert(false);
|
---|
138 | }
|
---|
139 |
|
---|
140 | static void newpage()
|
---|
141 | {
|
---|
142 | console_clear(console);
|
---|
143 | chars_remaining = console_cols;
|
---|
144 | lines_remaining = console_rows - 1;
|
---|
145 | }
|
---|
146 |
|
---|
147 | static void paged_char(wchar_t c)
|
---|
148 | {
|
---|
149 | putchar(c);
|
---|
150 | if (paging_enabled) {
|
---|
151 | chars_remaining--;
|
---|
152 | if (c == '\n' || chars_remaining == 0) {
|
---|
153 | chars_remaining = console_cols;
|
---|
154 | lines_remaining--;
|
---|
155 | }
|
---|
156 | if (lines_remaining == 0) {
|
---|
157 | fflush(stdout);
|
---|
158 | waitprompt();
|
---|
159 | waitkey();
|
---|
160 | newpage();
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | static unsigned int cat_file(const char *fname, size_t blen, bool hex,
|
---|
166 | off64_t head, off64_t tail, bool tail_first)
|
---|
167 | {
|
---|
168 | int fd, bytes = 0, count = 0, reads = 0;
|
---|
169 | char *buff = NULL;
|
---|
170 | int i;
|
---|
171 | size_t offset = 0, copied_bytes = 0;
|
---|
172 | off64_t file_size = 0, length = 0;
|
---|
173 |
|
---|
174 | fd = open(fname, O_RDONLY);
|
---|
175 | if (fd < 0) {
|
---|
176 | printf("Unable to open %s\n", fname);
|
---|
177 | return 1;
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (NULL == (buff = (char *) malloc(blen + 1))) {
|
---|
181 | close(fd);
|
---|
182 | printf("Unable to allocate enough memory to read %s\n",
|
---|
183 | fname);
|
---|
184 | return 1;
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (tail != CAT_FULL_FILE) {
|
---|
188 | file_size = lseek(fd, 0, SEEK_END);
|
---|
189 | if (head == CAT_FULL_FILE) {
|
---|
190 | head = file_size;
|
---|
191 | length = tail;
|
---|
192 | } else if (tail_first) {
|
---|
193 | length = head;
|
---|
194 | } else {
|
---|
195 | if (tail > head)
|
---|
196 | tail = head;
|
---|
197 | length = tail;
|
---|
198 | }
|
---|
199 |
|
---|
200 | if (tail_first) {
|
---|
201 | lseek(fd, (tail >= file_size) ? 0 : (file_size - tail), SEEK_SET);
|
---|
202 | } else {
|
---|
203 | lseek(fd, ((head - tail) >= file_size) ? 0 : (head - tail), SEEK_SET);
|
---|
204 | }
|
---|
205 | } else
|
---|
206 | length = head;
|
---|
207 |
|
---|
208 | do {
|
---|
209 | bytes = read(fd, buff + copied_bytes, (
|
---|
210 | (length != CAT_FULL_FILE && length - (off64_t)count <= (off64_t)(blen - copied_bytes)) ?
|
---|
211 | (size_t)(length - count) :
|
---|
212 | (blen - copied_bytes) ) );
|
---|
213 | bytes += copied_bytes;
|
---|
214 | copied_bytes = 0;
|
---|
215 |
|
---|
216 | if (bytes > 0) {
|
---|
217 | buff[bytes] = '\0';
|
---|
218 | offset = 0;
|
---|
219 | for (i = 0; i < bytes && !should_quit; i++) {
|
---|
220 | if (hex) {
|
---|
221 | paged_char(hexchars[((uint8_t)buff[i])/16]);
|
---|
222 | paged_char(hexchars[((uint8_t)buff[i])%16]);
|
---|
223 | paged_char(((count+i+1) & 0xf) == 0 ? '\n' : ' ');
|
---|
224 | }
|
---|
225 | else {
|
---|
226 | wchar_t c = str_decode(buff, &offset, bytes);
|
---|
227 | if (c == 0) {
|
---|
228 | /* Reached end of string */
|
---|
229 | break;
|
---|
230 | } else if (c == U_SPECIAL && offset + 2 >= (size_t)bytes) {
|
---|
231 | /* If an extended character is cut off due to the size of the buffer,
|
---|
232 | we will copy it over to the next buffer so it can be read correctly. */
|
---|
233 | copied_bytes = bytes - offset + 1;
|
---|
234 | memcpy(buff, buff + offset - 1, copied_bytes);
|
---|
235 | break;
|
---|
236 | }
|
---|
237 | paged_char(c);
|
---|
238 | }
|
---|
239 |
|
---|
240 | }
|
---|
241 | count += bytes;
|
---|
242 | reads++;
|
---|
243 | }
|
---|
244 | } while (bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE));
|
---|
245 |
|
---|
246 | close(fd);
|
---|
247 | if (bytes == -1) {
|
---|
248 | printf("Error reading %s\n", fname);
|
---|
249 | free(buff);
|
---|
250 | return 1;
|
---|
251 | }
|
---|
252 |
|
---|
253 | free(buff);
|
---|
254 |
|
---|
255 | return 0;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /* Main entry point for cat, accepts an array of arguments */
|
---|
259 | int cmd_cat(char **argv)
|
---|
260 | {
|
---|
261 | unsigned int argc, i, ret = 0;
|
---|
262 | size_t buffer = 0;
|
---|
263 | int c, opt_ind;
|
---|
264 | aoff64_t head = CAT_FULL_FILE, tail = CAT_FULL_FILE;
|
---|
265 | bool hex = false;
|
---|
266 | bool more = false;
|
---|
267 | bool tailFirst = false;
|
---|
268 | sysarg_t rows, cols;
|
---|
269 | int rc;
|
---|
270 |
|
---|
271 | /*
|
---|
272 | * reset global state
|
---|
273 | * TODO: move to structure?
|
---|
274 | */
|
---|
275 | paging_enabled = false;
|
---|
276 | chars_remaining = 0;
|
---|
277 | lines_remaining = 0;
|
---|
278 | console_cols = 0;
|
---|
279 | console_rows = 0;
|
---|
280 | should_quit = false;
|
---|
281 | console = console_init(stdin, stdout);
|
---|
282 |
|
---|
283 | argc = cli_count_args(argv);
|
---|
284 |
|
---|
285 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
|
---|
286 | c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind);
|
---|
287 | switch (c) {
|
---|
288 | case 'h':
|
---|
289 | help_cmd_cat(HELP_LONG);
|
---|
290 | return CMD_SUCCESS;
|
---|
291 | case 'v':
|
---|
292 | printf("%s\n", CAT_VERSION);
|
---|
293 | return CMD_SUCCESS;
|
---|
294 | case 'H':
|
---|
295 | if (!optarg || str_uint64_t(optarg, NULL, 10, false, &head) != EOK ) {
|
---|
296 | puts("Invalid head size\n");
|
---|
297 | return CMD_FAILURE;
|
---|
298 | }
|
---|
299 | break;
|
---|
300 | case 't':
|
---|
301 | if (!optarg || str_uint64_t(optarg, NULL, 10, false, &tail) != EOK ) {
|
---|
302 | puts("Invalid tail size\n");
|
---|
303 | return CMD_FAILURE;
|
---|
304 | }
|
---|
305 | if (head == CAT_FULL_FILE)
|
---|
306 | tailFirst = true;
|
---|
307 | break;
|
---|
308 | case 'b':
|
---|
309 | if (!optarg || str_size_t(optarg, NULL, 10, false, &buffer) != EOK ) {
|
---|
310 | puts("Invalid buffer size\n");
|
---|
311 | return CMD_FAILURE;
|
---|
312 | }
|
---|
313 | break;
|
---|
314 | case 'm':
|
---|
315 | more = true;
|
---|
316 | break;
|
---|
317 | case 'x':
|
---|
318 | hex = true;
|
---|
319 | break;
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | argc -= optind;
|
---|
324 |
|
---|
325 | if (argc < 1) {
|
---|
326 | printf("%s - incorrect number of arguments. Try `%s --help'\n",
|
---|
327 | cmdname, cmdname);
|
---|
328 | return CMD_FAILURE;
|
---|
329 | }
|
---|
330 |
|
---|
331 | if (buffer < 4)
|
---|
332 | buffer = CAT_DEFAULT_BUFLEN;
|
---|
333 |
|
---|
334 | if (more) {
|
---|
335 | rc = console_get_size(console, &cols, &rows);
|
---|
336 | if (rc != EOK) {
|
---|
337 | printf("%s - cannot get console size\n", cmdname);
|
---|
338 | return CMD_FAILURE;
|
---|
339 | }
|
---|
340 | console_cols = cols;
|
---|
341 | console_rows = rows;
|
---|
342 | paging_enabled = true;
|
---|
343 | newpage();
|
---|
344 | }
|
---|
345 |
|
---|
346 | for (i = optind; argv[i] != NULL && !should_quit; i++)
|
---|
347 | ret += cat_file(argv[i], buffer, hex, head, tail, tailFirst);
|
---|
348 |
|
---|
349 | if (ret)
|
---|
350 | return CMD_FAILURE;
|
---|
351 | else
|
---|
352 | return CMD_SUCCESS;
|
---|
353 | }
|
---|
354 |
|
---|