[36ab7c7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Tim Post
|
---|
[d553f81] | 3 | * Copyright (c) 2011, Martin Sucha
|
---|
[c9f5e24f] | 4 | * All rights reserved.
|
---|
[216d6fc] | 5 | *
|
---|
[c9f5e24f] | 6 | * Redistribution and use in source and binary forms, with or without
|
---|
[36ab7c7] | 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
[c9f5e24f] | 9 | *
|
---|
[36ab7c7] | 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.
|
---|
[c9f5e24f] | 17 | *
|
---|
[36ab7c7] | 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.
|
---|
[c9f5e24f] | 28 | */
|
---|
[216d6fc] | 29 |
|
---|
| 30 | #include <stdio.h>
|
---|
| 31 | #include <stdlib.h>
|
---|
[c9f5e24f] | 32 | #include <unistd.h>
|
---|
[216d6fc] | 33 | #include <getopt.h>
|
---|
[19f857a] | 34 | #include <str.h>
|
---|
[c9f5e24f] | 35 | #include <fcntl.h>
|
---|
[d553f81] | 36 | #include <io/console.h>
|
---|
| 37 | #include <io/color.h>
|
---|
| 38 | #include <io/style.h>
|
---|
[3cefd70] | 39 | #include <io/keycode.h>
|
---|
[d553f81] | 40 | #include <errno.h>
|
---|
| 41 | #include <vfs/vfs.h>
|
---|
| 42 | #include <assert.h>
|
---|
[216d6fc] | 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 |
|
---|
[a000878c] | 51 | static const char *cmdname = "cat";
|
---|
[c9f5e24f] | 52 | #define CAT_VERSION "0.0.1"
|
---|
| 53 | #define CAT_DEFAULT_BUFLEN 1024
|
---|
[cc9f314] | 54 | #define CAT_FULL_FILE 0
|
---|
[c9f5e24f] | 55 |
|
---|
[d553f81] | 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;
|
---|
[3cefd70] | 63 | static bool should_quit = false;
|
---|
[a33706e] | 64 | static bool dash_represents_stdin = false;
|
---|
[216d6fc] | 65 |
|
---|
[79ae36dd] | 66 | static console_ctrl_t *console = NULL;
|
---|
| 67 |
|
---|
[3771a6e] | 68 | static struct option const long_options[] = {
|
---|
[c9f5e24f] | 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' },
|
---|
[d553f81] | 75 | { "hex", no_argument, 0, 'x' },
|
---|
[a33706e] | 76 | { "stdin", no_argument, 0, 's' },
|
---|
[216d6fc] | 77 | { 0, 0, 0, 0 }
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | /* Dispays help for cat in various levels */
|
---|
[809813d] | 81 | void help_cmd_cat(unsigned int level)
|
---|
[216d6fc] | 82 | {
|
---|
[c9f5e24f] | 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"
|
---|
[d553f81] | 96 | " -x, --hex Print bytes as hex values\n"
|
---|
[a33706e] | 97 | " -s --stdin Treat `-' in file list as standard input\n"
|
---|
[c9f5e24f] | 98 | "Currently, %s is under development, some options don't work.\n",
|
---|
| 99 | cmdname, cmdname);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[809813d] | 102 | return;
|
---|
[216d6fc] | 103 | }
|
---|
| 104 |
|
---|
[d553f81] | 105 | static void waitprompt()
|
---|
| 106 | {
|
---|
[79ae36dd] | 107 | console_set_pos(console, 0, console_rows-1);
|
---|
[7c014d1] | 108 | console_set_color(console, COLOR_WHITE, COLOR_BLUE, 0);
|
---|
[79ae36dd] | 109 |
|
---|
[3cefd70] | 110 | printf("ENTER/SPACE/PAGE DOWN - next page, "
|
---|
| 111 | "ESC/Q - quit, C - continue unpaged");
|
---|
[d553f81] | 112 | fflush(stdout);
|
---|
[79ae36dd] | 113 |
|
---|
| 114 | console_set_style(console, STYLE_NORMAL);
|
---|
[d553f81] | 115 | }
|
---|
| 116 |
|
---|
| 117 | static void waitkey()
|
---|
| 118 | {
|
---|
[07b7c48] | 119 | cons_event_t ev;
|
---|
| 120 | kbd_event_t *kev;
|
---|
[d553f81] | 121 |
|
---|
| 122 | while (true) {
|
---|
[07b7c48] | 123 | if (!console_get_event(console, &ev)) {
|
---|
[d553f81] | 124 | return;
|
---|
| 125 | }
|
---|
[07b7c48] | 126 | if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS) {
|
---|
| 127 | kev = &ev.ev.key;
|
---|
| 128 |
|
---|
| 129 | if (kev->key == KC_ESCAPE || kev->key == KC_Q) {
|
---|
[3cefd70] | 130 | should_quit = true;
|
---|
| 131 | return;
|
---|
| 132 | }
|
---|
[07b7c48] | 133 | if (kev->key == KC_C) {
|
---|
[3cefd70] | 134 | paging_enabled = false;
|
---|
| 135 | return;
|
---|
| 136 | }
|
---|
[07b7c48] | 137 | if (kev->key == KC_ENTER || kev->key == KC_SPACE ||
|
---|
| 138 | kev->key == KC_PAGE_DOWN) {
|
---|
[3cefd70] | 139 | return;
|
---|
| 140 | }
|
---|
[d553f81] | 141 | }
|
---|
| 142 | }
|
---|
| 143 | assert(false);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | static void newpage()
|
---|
| 147 | {
|
---|
[79ae36dd] | 148 | console_clear(console);
|
---|
[d553f81] | 149 | chars_remaining = console_cols;
|
---|
[79ae36dd] | 150 | lines_remaining = console_rows - 1;
|
---|
[d553f81] | 151 | }
|
---|
| 152 |
|
---|
| 153 | static void paged_char(wchar_t c)
|
---|
| 154 | {
|
---|
| 155 | putchar(c);
|
---|
| 156 | if (paging_enabled) {
|
---|
| 157 | chars_remaining--;
|
---|
| 158 | if (c == '\n' || chars_remaining == 0) {
|
---|
| 159 | chars_remaining = console_cols;
|
---|
| 160 | lines_remaining--;
|
---|
| 161 | }
|
---|
| 162 | if (lines_remaining == 0) {
|
---|
| 163 | fflush(stdout);
|
---|
| 164 | waitprompt();
|
---|
| 165 | waitkey();
|
---|
| 166 | newpage();
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[cc9f314] | 171 | static unsigned int cat_file(const char *fname, size_t blen, bool hex,
|
---|
| 172 | off64_t head, off64_t tail, bool tail_first)
|
---|
[c9f5e24f] | 173 | {
|
---|
| 174 | int fd, bytes = 0, count = 0, reads = 0;
|
---|
| 175 | char *buff = NULL;
|
---|
[d553f81] | 176 | int i;
|
---|
[cc9f314] | 177 | size_t offset = 0, copied_bytes = 0;
|
---|
| 178 | off64_t file_size = 0, length = 0;
|
---|
[c9f5e24f] | 179 |
|
---|
[a33706e] | 180 | bool reading_stdin = dash_represents_stdin && (str_cmp(fname, "-") == 0);
|
---|
[e435537] | 181 |
|
---|
[a33706e] | 182 | if (reading_stdin) {
|
---|
| 183 | fd = fileno(stdin);
|
---|
| 184 | /* Allow storing the whole UTF-8 character. */
|
---|
| 185 | blen = STR_BOUNDS(1);
|
---|
[e435537] | 186 | } else
|
---|
[a33706e] | 187 | fd = open(fname, O_RDONLY);
|
---|
[e435537] | 188 |
|
---|
[8bb129d] | 189 | if (fd < 0) {
|
---|
[c9f5e24f] | 190 | printf("Unable to open %s\n", fname);
|
---|
| 191 | return 1;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | if (NULL == (buff = (char *) malloc(blen + 1))) {
|
---|
| 195 | close(fd);
|
---|
| 196 | printf("Unable to allocate enough memory to read %s\n",
|
---|
| 197 | fname);
|
---|
| 198 | return 1;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[cc9f314] | 201 | if (tail != CAT_FULL_FILE) {
|
---|
| 202 | file_size = lseek(fd, 0, SEEK_END);
|
---|
| 203 | if (head == CAT_FULL_FILE) {
|
---|
| 204 | head = file_size;
|
---|
| 205 | length = tail;
|
---|
| 206 | } else if (tail_first) {
|
---|
| 207 | length = head;
|
---|
| 208 | } else {
|
---|
| 209 | if (tail > head)
|
---|
| 210 | tail = head;
|
---|
| 211 | length = tail;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | if (tail_first) {
|
---|
| 215 | lseek(fd, (tail >= file_size) ? 0 : (file_size - tail), SEEK_SET);
|
---|
| 216 | } else {
|
---|
| 217 | lseek(fd, ((head - tail) >= file_size) ? 0 : (head - tail), SEEK_SET);
|
---|
| 218 | }
|
---|
| 219 | } else
|
---|
| 220 | length = head;
|
---|
| 221 |
|
---|
[c9f5e24f] | 222 | do {
|
---|
[a33706e] | 223 | size_t bytes_to_read;
|
---|
| 224 | if (reading_stdin) {
|
---|
| 225 | bytes_to_read = 1;
|
---|
| 226 | } else {
|
---|
[e435537] | 227 | if ((length != CAT_FULL_FILE) &&
|
---|
| 228 | (length - (off64_t)count <= (off64_t)(blen - copied_bytes))) {
|
---|
[a33706e] | 229 | bytes_to_read = (size_t) (length - count);
|
---|
| 230 | } else {
|
---|
| 231 | bytes_to_read = blen - copied_bytes;
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
[e435537] | 234 |
|
---|
[a33706e] | 235 | bytes = read(fd, buff + copied_bytes, bytes_to_read);
|
---|
[cc9f314] | 236 | bytes += copied_bytes;
|
---|
| 237 | copied_bytes = 0;
|
---|
| 238 |
|
---|
[c9f5e24f] | 239 | if (bytes > 0) {
|
---|
[2953f9a] | 240 | buff[bytes] = '\0';
|
---|
[d553f81] | 241 | offset = 0;
|
---|
[3cefd70] | 242 | for (i = 0; i < bytes && !should_quit; i++) {
|
---|
[d553f81] | 243 | if (hex) {
|
---|
| 244 | paged_char(hexchars[((uint8_t)buff[i])/16]);
|
---|
| 245 | paged_char(hexchars[((uint8_t)buff[i])%16]);
|
---|
[cc9f314] | 246 | paged_char(((count+i+1) & 0xf) == 0 ? '\n' : ' ');
|
---|
[d553f81] | 247 | }
|
---|
| 248 | else {
|
---|
| 249 | wchar_t c = str_decode(buff, &offset, bytes);
|
---|
| 250 | if (c == 0) {
|
---|
[28a3e74] | 251 | /* Reached end of string */
|
---|
[d553f81] | 252 | break;
|
---|
[cc9f314] | 253 | } else if (c == U_SPECIAL && offset + 2 >= (size_t)bytes) {
|
---|
| 254 | /* If an extended character is cut off due to the size of the buffer,
|
---|
| 255 | we will copy it over to the next buffer so it can be read correctly. */
|
---|
| 256 | copied_bytes = bytes - offset + 1;
|
---|
| 257 | memcpy(buff, buff + offset - 1, copied_bytes);
|
---|
| 258 | break;
|
---|
[d553f81] | 259 | }
|
---|
| 260 | paged_char(c);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | }
|
---|
[cc9f314] | 264 | count += bytes;
|
---|
[c9f5e24f] | 265 | reads++;
|
---|
| 266 | }
|
---|
[e435537] | 267 |
|
---|
| 268 | if (reading_stdin)
|
---|
[a33706e] | 269 | fflush(stdout);
|
---|
[cc9f314] | 270 | } while (bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE));
|
---|
[c9f5e24f] | 271 |
|
---|
| 272 | close(fd);
|
---|
| 273 | if (bytes == -1) {
|
---|
| 274 | printf("Error reading %s\n", fname);
|
---|
| 275 | free(buff);
|
---|
| 276 | return 1;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | free(buff);
|
---|
| 280 |
|
---|
| 281 | return 0;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[216d6fc] | 284 | /* Main entry point for cat, accepts an array of arguments */
|
---|
[809813d] | 285 | int cmd_cat(char **argv)
|
---|
[216d6fc] | 286 | {
|
---|
[cc9f314] | 287 | unsigned int argc, i, ret = 0;
|
---|
| 288 | size_t buffer = 0;
|
---|
[216d6fc] | 289 | int c, opt_ind;
|
---|
[cc9f314] | 290 | aoff64_t head = CAT_FULL_FILE, tail = CAT_FULL_FILE;
|
---|
[d553f81] | 291 | bool hex = false;
|
---|
| 292 | bool more = false;
|
---|
[cc9f314] | 293 | bool tailFirst = false;
|
---|
[d553f81] | 294 | sysarg_t rows, cols;
|
---|
| 295 | int rc;
|
---|
| 296 |
|
---|
[28a3e74] | 297 | /*
|
---|
| 298 | * reset global state
|
---|
| 299 | * TODO: move to structure?
|
---|
| 300 | */
|
---|
[d553f81] | 301 | paging_enabled = false;
|
---|
| 302 | chars_remaining = 0;
|
---|
| 303 | lines_remaining = 0;
|
---|
| 304 | console_cols = 0;
|
---|
| 305 | console_rows = 0;
|
---|
[3cefd70] | 306 | should_quit = false;
|
---|
[79ae36dd] | 307 | console = console_init(stdin, stdout);
|
---|
[216d6fc] | 308 |
|
---|
[43e02a6] | 309 | argc = cli_count_args(argv);
|
---|
[216d6fc] | 310 |
|
---|
| 311 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
|
---|
[a33706e] | 312 | c = getopt_long(argc, argv, "xhvmH:t:b:s", long_options, &opt_ind);
|
---|
[216d6fc] | 313 | switch (c) {
|
---|
| 314 | case 'h':
|
---|
[c9f5e24f] | 315 | help_cmd_cat(HELP_LONG);
|
---|
| 316 | return CMD_SUCCESS;
|
---|
[216d6fc] | 317 | case 'v':
|
---|
[c9f5e24f] | 318 | printf("%s\n", CAT_VERSION);
|
---|
| 319 | return CMD_SUCCESS;
|
---|
| 320 | case 'H':
|
---|
[cc9f314] | 321 | if (!optarg || str_uint64_t(optarg, NULL, 10, false, &head) != EOK ) {
|
---|
| 322 | puts("Invalid head size\n");
|
---|
| 323 | return CMD_FAILURE;
|
---|
| 324 | }
|
---|
| 325 | break;
|
---|
[c9f5e24f] | 326 | case 't':
|
---|
[cc9f314] | 327 | if (!optarg || str_uint64_t(optarg, NULL, 10, false, &tail) != EOK ) {
|
---|
| 328 | puts("Invalid tail size\n");
|
---|
| 329 | return CMD_FAILURE;
|
---|
| 330 | }
|
---|
| 331 | if (head == CAT_FULL_FILE)
|
---|
| 332 | tailFirst = true;
|
---|
| 333 | break;
|
---|
[c9f5e24f] | 334 | case 'b':
|
---|
[cc9f314] | 335 | if (!optarg || str_size_t(optarg, NULL, 10, false, &buffer) != EOK ) {
|
---|
| 336 | puts("Invalid buffer size\n");
|
---|
| 337 | return CMD_FAILURE;
|
---|
| 338 | }
|
---|
[216d6fc] | 339 | break;
|
---|
[c9f5e24f] | 340 | case 'm':
|
---|
[d553f81] | 341 | more = true;
|
---|
| 342 | break;
|
---|
| 343 | case 'x':
|
---|
| 344 | hex = true;
|
---|
| 345 | break;
|
---|
[a33706e] | 346 | case 's':
|
---|
| 347 | dash_represents_stdin = true;
|
---|
| 348 | break;
|
---|
[216d6fc] | 349 | }
|
---|
| 350 | }
|
---|
| 351 |
|
---|
[c9f5e24f] | 352 | argc -= optind;
|
---|
| 353 |
|
---|
| 354 | if (argc < 1) {
|
---|
| 355 | printf("%s - incorrect number of arguments. Try `%s --help'\n",
|
---|
| 356 | cmdname, cmdname);
|
---|
| 357 | return CMD_FAILURE;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[cc9f314] | 360 | if (buffer < 4)
|
---|
[c9f5e24f] | 361 | buffer = CAT_DEFAULT_BUFLEN;
|
---|
[d553f81] | 362 |
|
---|
| 363 | if (more) {
|
---|
[79ae36dd] | 364 | rc = console_get_size(console, &cols, &rows);
|
---|
[d553f81] | 365 | if (rc != EOK) {
|
---|
| 366 | printf("%s - cannot get console size\n", cmdname);
|
---|
| 367 | return CMD_FAILURE;
|
---|
| 368 | }
|
---|
| 369 | console_cols = cols;
|
---|
| 370 | console_rows = rows;
|
---|
| 371 | paging_enabled = true;
|
---|
| 372 | newpage();
|
---|
| 373 | }
|
---|
[c9f5e24f] | 374 |
|
---|
[3cefd70] | 375 | for (i = optind; argv[i] != NULL && !should_quit; i++)
|
---|
[cc9f314] | 376 | ret += cat_file(argv[i], buffer, hex, head, tail, tailFirst);
|
---|
[216d6fc] | 377 |
|
---|
[c9f5e24f] | 378 | if (ret)
|
---|
| 379 | return CMD_FAILURE;
|
---|
| 380 | else
|
---|
| 381 | return CMD_SUCCESS;
|
---|
[216d6fc] | 382 | }
|
---|
| 383 |
|
---|