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 | static bool dash_represents_stdin = false;
|
---|
65 |
|
---|
66 | static console_ctrl_t *console = NULL;
|
---|
67 |
|
---|
68 | static struct option const long_options[] = {
|
---|
69 | { "help", no_argument, 0, 'h' },
|
---|
70 | { "version", no_argument, 0, 'v' },
|
---|
71 | { "head", required_argument, 0, 'H' },
|
---|
72 | { "tail", required_argument, 0, 't' },
|
---|
73 | { "buffer", required_argument, 0, 'b' },
|
---|
74 | { "more", no_argument, 0, 'm' },
|
---|
75 | { "hex", no_argument, 0, 'x' },
|
---|
76 | { "stdin", no_argument, 0, 's' },
|
---|
77 | { 0, 0, 0, 0 }
|
---|
78 | };
|
---|
79 |
|
---|
80 | /* Dispays help for cat in various levels */
|
---|
81 | void help_cmd_cat(unsigned int level)
|
---|
82 | {
|
---|
83 | if (level == HELP_SHORT) {
|
---|
84 | printf("`%s' shows the contents of files\n", cmdname);
|
---|
85 | } else {
|
---|
86 | help_cmd_cat(HELP_SHORT);
|
---|
87 | printf(
|
---|
88 | "Usage: %s [options] <file1> [file2] [...]\n"
|
---|
89 | "Options:\n"
|
---|
90 | " -h, --help A short option summary\n"
|
---|
91 | " -v, --version Print version information and exit\n"
|
---|
92 | " -H, --head ## Print only the first ## bytes\n"
|
---|
93 | " -t, --tail ## Print only the last ## bytes\n"
|
---|
94 | " -b, --buffer ## Set the read buffer size to ##\n"
|
---|
95 | " -m, --more Pause after each screen full\n"
|
---|
96 | " -x, --hex Print bytes as hex values\n"
|
---|
97 | " -s --stdin Treat `-' in file list as standard input\n"
|
---|
98 | "Currently, %s is under development, some options don't work.\n",
|
---|
99 | cmdname, cmdname);
|
---|
100 | }
|
---|
101 |
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | static void waitprompt()
|
---|
106 | {
|
---|
107 | console_set_pos(console, 0, console_rows-1);
|
---|
108 | console_set_color(console, COLOR_WHITE, COLOR_BLUE, 0);
|
---|
109 |
|
---|
110 | printf("ENTER/SPACE/PAGE DOWN - next page, "
|
---|
111 | "ESC/Q - quit, C - continue unpaged");
|
---|
112 | fflush(stdout);
|
---|
113 |
|
---|
114 | console_set_style(console, STYLE_NORMAL);
|
---|
115 | }
|
---|
116 |
|
---|
117 | static void waitkey()
|
---|
118 | {
|
---|
119 | kbd_event_t ev;
|
---|
120 |
|
---|
121 | while (true) {
|
---|
122 | if (!console_get_kbd_event(console, &ev)) {
|
---|
123 | return;
|
---|
124 | }
|
---|
125 | if (ev.type == KEY_PRESS) {
|
---|
126 | if (ev.key == KC_ESCAPE || ev.key == KC_Q) {
|
---|
127 | should_quit = true;
|
---|
128 | return;
|
---|
129 | }
|
---|
130 | if (ev.key == KC_C) {
|
---|
131 | paging_enabled = false;
|
---|
132 | return;
|
---|
133 | }
|
---|
134 | if (ev.key == KC_ENTER || ev.key == KC_SPACE ||
|
---|
135 | ev.key == KC_PAGE_DOWN) {
|
---|
136 | return;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | assert(false);
|
---|
141 | }
|
---|
142 |
|
---|
143 | static void newpage()
|
---|
144 | {
|
---|
145 | console_clear(console);
|
---|
146 | chars_remaining = console_cols;
|
---|
147 | lines_remaining = console_rows - 1;
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void paged_char(wchar_t c)
|
---|
151 | {
|
---|
152 | putchar(c);
|
---|
153 | if (paging_enabled) {
|
---|
154 | chars_remaining--;
|
---|
155 | if (c == '\n' || chars_remaining == 0) {
|
---|
156 | chars_remaining = console_cols;
|
---|
157 | lines_remaining--;
|
---|
158 | }
|
---|
159 | if (lines_remaining == 0) {
|
---|
160 | fflush(stdout);
|
---|
161 | waitprompt();
|
---|
162 | waitkey();
|
---|
163 | newpage();
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | static unsigned int cat_file(const char *fname, size_t blen, bool hex,
|
---|
169 | off64_t head, off64_t tail, bool tail_first)
|
---|
170 | {
|
---|
171 | int fd, bytes = 0, count = 0, reads = 0;
|
---|
172 | char *buff = NULL;
|
---|
173 | int i;
|
---|
174 | size_t offset = 0, copied_bytes = 0;
|
---|
175 | off64_t file_size = 0, length = 0;
|
---|
176 |
|
---|
177 | bool reading_stdin = dash_represents_stdin && (str_cmp(fname, "-") == 0);
|
---|
178 |
|
---|
179 | if (reading_stdin) {
|
---|
180 | fd = fileno(stdin);
|
---|
181 | /* Allow storing the whole UTF-8 character. */
|
---|
182 | blen = STR_BOUNDS(1);
|
---|
183 | } else
|
---|
184 | fd = open(fname, O_RDONLY);
|
---|
185 |
|
---|
186 | if (fd < 0) {
|
---|
187 | printf("Unable to open %s\n", fname);
|
---|
188 | return 1;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (NULL == (buff = (char *) malloc(blen + 1))) {
|
---|
192 | close(fd);
|
---|
193 | printf("Unable to allocate enough memory to read %s\n",
|
---|
194 | fname);
|
---|
195 | return 1;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (tail != CAT_FULL_FILE) {
|
---|
199 | file_size = lseek(fd, 0, SEEK_END);
|
---|
200 | if (head == CAT_FULL_FILE) {
|
---|
201 | head = file_size;
|
---|
202 | length = tail;
|
---|
203 | } else if (tail_first) {
|
---|
204 | length = head;
|
---|
205 | } else {
|
---|
206 | if (tail > head)
|
---|
207 | tail = head;
|
---|
208 | length = tail;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (tail_first) {
|
---|
212 | lseek(fd, (tail >= file_size) ? 0 : (file_size - tail), SEEK_SET);
|
---|
213 | } else {
|
---|
214 | lseek(fd, ((head - tail) >= file_size) ? 0 : (head - tail), SEEK_SET);
|
---|
215 | }
|
---|
216 | } else
|
---|
217 | length = head;
|
---|
218 |
|
---|
219 | do {
|
---|
220 | size_t bytes_to_read;
|
---|
221 | if (reading_stdin) {
|
---|
222 | bytes_to_read = 1;
|
---|
223 | } else {
|
---|
224 | if ((length != CAT_FULL_FILE) &&
|
---|
225 | (length - (off64_t)count <= (off64_t)(blen - copied_bytes))) {
|
---|
226 | bytes_to_read = (size_t) (length - count);
|
---|
227 | } else {
|
---|
228 | bytes_to_read = blen - copied_bytes;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | bytes = read(fd, buff + copied_bytes, bytes_to_read);
|
---|
233 | bytes += copied_bytes;
|
---|
234 | copied_bytes = 0;
|
---|
235 |
|
---|
236 | if (bytes > 0) {
|
---|
237 | buff[bytes] = '\0';
|
---|
238 | offset = 0;
|
---|
239 | for (i = 0; i < bytes && !should_quit; i++) {
|
---|
240 | if (hex) {
|
---|
241 | paged_char(hexchars[((uint8_t)buff[i])/16]);
|
---|
242 | paged_char(hexchars[((uint8_t)buff[i])%16]);
|
---|
243 | paged_char(((count+i+1) & 0xf) == 0 ? '\n' : ' ');
|
---|
244 | }
|
---|
245 | else {
|
---|
246 | wchar_t c = str_decode(buff, &offset, bytes);
|
---|
247 | if (c == 0) {
|
---|
248 | /* Reached end of string */
|
---|
249 | break;
|
---|
250 | } else if (c == U_SPECIAL && offset + 2 >= (size_t)bytes) {
|
---|
251 | /* If an extended character is cut off due to the size of the buffer,
|
---|
252 | we will copy it over to the next buffer so it can be read correctly. */
|
---|
253 | copied_bytes = bytes - offset + 1;
|
---|
254 | memcpy(buff, buff + offset - 1, copied_bytes);
|
---|
255 | break;
|
---|
256 | }
|
---|
257 | paged_char(c);
|
---|
258 | }
|
---|
259 |
|
---|
260 | }
|
---|
261 | count += bytes;
|
---|
262 | reads++;
|
---|
263 | }
|
---|
264 |
|
---|
265 | if (reading_stdin)
|
---|
266 | fflush(stdout);
|
---|
267 | } while (bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE));
|
---|
268 |
|
---|
269 | close(fd);
|
---|
270 | if (bytes == -1) {
|
---|
271 | printf("Error reading %s\n", fname);
|
---|
272 | free(buff);
|
---|
273 | return 1;
|
---|
274 | }
|
---|
275 |
|
---|
276 | free(buff);
|
---|
277 |
|
---|
278 | return 0;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /* Main entry point for cat, accepts an array of arguments */
|
---|
282 | int cmd_cat(char **argv)
|
---|
283 | {
|
---|
284 | unsigned int argc, i, ret = 0;
|
---|
285 | size_t buffer = 0;
|
---|
286 | int c, opt_ind;
|
---|
287 | aoff64_t head = CAT_FULL_FILE, tail = CAT_FULL_FILE;
|
---|
288 | bool hex = false;
|
---|
289 | bool more = false;
|
---|
290 | bool tailFirst = false;
|
---|
291 | sysarg_t rows, cols;
|
---|
292 | int rc;
|
---|
293 |
|
---|
294 | /*
|
---|
295 | * reset global state
|
---|
296 | * TODO: move to structure?
|
---|
297 | */
|
---|
298 | paging_enabled = false;
|
---|
299 | chars_remaining = 0;
|
---|
300 | lines_remaining = 0;
|
---|
301 | console_cols = 0;
|
---|
302 | console_rows = 0;
|
---|
303 | should_quit = false;
|
---|
304 | console = console_init(stdin, stdout);
|
---|
305 |
|
---|
306 | argc = cli_count_args(argv);
|
---|
307 |
|
---|
308 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
|
---|
309 | c = getopt_long(argc, argv, "xhvmH:t:b:s", long_options, &opt_ind);
|
---|
310 | switch (c) {
|
---|
311 | case 'h':
|
---|
312 | help_cmd_cat(HELP_LONG);
|
---|
313 | return CMD_SUCCESS;
|
---|
314 | case 'v':
|
---|
315 | printf("%s\n", CAT_VERSION);
|
---|
316 | return CMD_SUCCESS;
|
---|
317 | case 'H':
|
---|
318 | if (!optarg || str_uint64_t(optarg, NULL, 10, false, &head) != EOK ) {
|
---|
319 | puts("Invalid head size\n");
|
---|
320 | return CMD_FAILURE;
|
---|
321 | }
|
---|
322 | break;
|
---|
323 | case 't':
|
---|
324 | if (!optarg || str_uint64_t(optarg, NULL, 10, false, &tail) != EOK ) {
|
---|
325 | puts("Invalid tail size\n");
|
---|
326 | return CMD_FAILURE;
|
---|
327 | }
|
---|
328 | if (head == CAT_FULL_FILE)
|
---|
329 | tailFirst = true;
|
---|
330 | break;
|
---|
331 | case 'b':
|
---|
332 | if (!optarg || str_size_t(optarg, NULL, 10, false, &buffer) != EOK ) {
|
---|
333 | puts("Invalid buffer size\n");
|
---|
334 | return CMD_FAILURE;
|
---|
335 | }
|
---|
336 | break;
|
---|
337 | case 'm':
|
---|
338 | more = true;
|
---|
339 | break;
|
---|
340 | case 'x':
|
---|
341 | hex = true;
|
---|
342 | break;
|
---|
343 | case 's':
|
---|
344 | dash_represents_stdin = true;
|
---|
345 | break;
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | argc -= optind;
|
---|
350 |
|
---|
351 | if (argc < 1) {
|
---|
352 | printf("%s - incorrect number of arguments. Try `%s --help'\n",
|
---|
353 | cmdname, cmdname);
|
---|
354 | return CMD_FAILURE;
|
---|
355 | }
|
---|
356 |
|
---|
357 | if (buffer < 4)
|
---|
358 | buffer = CAT_DEFAULT_BUFLEN;
|
---|
359 |
|
---|
360 | if (more) {
|
---|
361 | rc = console_get_size(console, &cols, &rows);
|
---|
362 | if (rc != EOK) {
|
---|
363 | printf("%s - cannot get console size\n", cmdname);
|
---|
364 | return CMD_FAILURE;
|
---|
365 | }
|
---|
366 | console_cols = cols;
|
---|
367 | console_rows = rows;
|
---|
368 | paging_enabled = true;
|
---|
369 | newpage();
|
---|
370 | }
|
---|
371 |
|
---|
372 | for (i = optind; argv[i] != NULL && !should_quit; i++)
|
---|
373 | ret += cat_file(argv[i], buffer, hex, head, tail, tailFirst);
|
---|
374 |
|
---|
375 | if (ret)
|
---|
376 | return CMD_FAILURE;
|
---|
377 | else
|
---|
378 | return CMD_SUCCESS;
|
---|
379 | }
|
---|
380 |
|
---|