[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
|
---|
| 54 |
|
---|
[a000878c] | 55 | static const char *cat_oops = "That option is not yet supported\n";
|
---|
[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;
|
---|
[216d6fc] | 64 |
|
---|
[79ae36dd] | 65 | static console_ctrl_t *console = NULL;
|
---|
| 66 |
|
---|
[3771a6e] | 67 | static struct option const long_options[] = {
|
---|
[c9f5e24f] | 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' },
|
---|
[d553f81] | 74 | { "hex", no_argument, 0, 'x' },
|
---|
[216d6fc] | 75 | { 0, 0, 0, 0 }
|
---|
| 76 | };
|
---|
| 77 |
|
---|
| 78 | /* Dispays help for cat in various levels */
|
---|
[809813d] | 79 | void help_cmd_cat(unsigned int level)
|
---|
[216d6fc] | 80 | {
|
---|
[c9f5e24f] | 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"
|
---|
[d553f81] | 94 | " -x, --hex Print bytes as hex values\n"
|
---|
[c9f5e24f] | 95 | "Currently, %s is under development, some options don't work.\n",
|
---|
| 96 | cmdname, cmdname);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[809813d] | 99 | return;
|
---|
[216d6fc] | 100 | }
|
---|
| 101 |
|
---|
[d553f81] | 102 | static void waitprompt()
|
---|
| 103 | {
|
---|
[79ae36dd] | 104 | console_set_pos(console, 0, console_rows-1);
|
---|
[7c014d1] | 105 | console_set_color(console, COLOR_WHITE, COLOR_BLUE, 0);
|
---|
[79ae36dd] | 106 |
|
---|
[3cefd70] | 107 | printf("ENTER/SPACE/PAGE DOWN - next page, "
|
---|
| 108 | "ESC/Q - quit, C - continue unpaged");
|
---|
[d553f81] | 109 | fflush(stdout);
|
---|
[79ae36dd] | 110 |
|
---|
| 111 | console_set_style(console, STYLE_NORMAL);
|
---|
[d553f81] | 112 | }
|
---|
| 113 |
|
---|
| 114 | static void waitkey()
|
---|
| 115 | {
|
---|
[79ae36dd] | 116 | kbd_event_t ev;
|
---|
[d553f81] | 117 |
|
---|
| 118 | while (true) {
|
---|
[79ae36dd] | 119 | if (!console_get_kbd_event(console, &ev)) {
|
---|
[d553f81] | 120 | return;
|
---|
| 121 | }
|
---|
| 122 | if (ev.type == KEY_PRESS) {
|
---|
[3cefd70] | 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 | }
|
---|
[d553f81] | 135 | }
|
---|
| 136 | }
|
---|
| 137 | assert(false);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | static void newpage()
|
---|
| 141 | {
|
---|
[79ae36dd] | 142 | console_clear(console);
|
---|
[d553f81] | 143 | chars_remaining = console_cols;
|
---|
[79ae36dd] | 144 | lines_remaining = console_rows - 1;
|
---|
[d553f81] | 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)
|
---|
[c9f5e24f] | 166 | {
|
---|
| 167 | int fd, bytes = 0, count = 0, reads = 0;
|
---|
| 168 | char *buff = NULL;
|
---|
[d553f81] | 169 | int i;
|
---|
| 170 | size_t offset = 0;
|
---|
[c9f5e24f] | 171 |
|
---|
[8bb129d] | 172 | fd = open(fname, O_RDONLY);
|
---|
| 173 | if (fd < 0) {
|
---|
[c9f5e24f] | 174 | printf("Unable to open %s\n", fname);
|
---|
| 175 | return 1;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | if (NULL == (buff = (char *) malloc(blen + 1))) {
|
---|
| 179 | close(fd);
|
---|
| 180 | printf("Unable to allocate enough memory to read %s\n",
|
---|
| 181 | fname);
|
---|
| 182 | return 1;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | do {
|
---|
| 186 | bytes = read(fd, buff, blen);
|
---|
| 187 | if (bytes > 0) {
|
---|
| 188 | count += bytes;
|
---|
[2953f9a] | 189 | buff[bytes] = '\0';
|
---|
[d553f81] | 190 | offset = 0;
|
---|
[3cefd70] | 191 | for (i = 0; i < bytes && !should_quit; i++) {
|
---|
[d553f81] | 192 | if (hex) {
|
---|
| 193 | paged_char(hexchars[((uint8_t)buff[i])/16]);
|
---|
| 194 | paged_char(hexchars[((uint8_t)buff[i])%16]);
|
---|
| 195 | }
|
---|
| 196 | else {
|
---|
| 197 | wchar_t c = str_decode(buff, &offset, bytes);
|
---|
| 198 | if (c == 0) {
|
---|
[28a3e74] | 199 | /* Reached end of string */
|
---|
[d553f81] | 200 | break;
|
---|
| 201 | }
|
---|
| 202 | paged_char(c);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | }
|
---|
[c9f5e24f] | 206 | reads++;
|
---|
| 207 | }
|
---|
[3cefd70] | 208 | } while (bytes > 0 && !should_quit);
|
---|
[c9f5e24f] | 209 |
|
---|
| 210 | close(fd);
|
---|
| 211 | if (bytes == -1) {
|
---|
| 212 | printf("Error reading %s\n", fname);
|
---|
| 213 | free(buff);
|
---|
| 214 | return 1;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | free(buff);
|
---|
| 218 |
|
---|
| 219 | return 0;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[216d6fc] | 222 | /* Main entry point for cat, accepts an array of arguments */
|
---|
[809813d] | 223 | int cmd_cat(char **argv)
|
---|
[216d6fc] | 224 | {
|
---|
[c9f5e24f] | 225 | unsigned int argc, i, ret = 0, buffer = 0;
|
---|
[216d6fc] | 226 | int c, opt_ind;
|
---|
[d553f81] | 227 | bool hex = false;
|
---|
| 228 | bool more = false;
|
---|
| 229 | sysarg_t rows, cols;
|
---|
| 230 | int rc;
|
---|
| 231 |
|
---|
[28a3e74] | 232 | /*
|
---|
| 233 | * reset global state
|
---|
| 234 | * TODO: move to structure?
|
---|
| 235 | */
|
---|
[d553f81] | 236 | paging_enabled = false;
|
---|
| 237 | chars_remaining = 0;
|
---|
| 238 | lines_remaining = 0;
|
---|
| 239 | console_cols = 0;
|
---|
| 240 | console_rows = 0;
|
---|
[3cefd70] | 241 | should_quit = false;
|
---|
[79ae36dd] | 242 | console = console_init(stdin, stdout);
|
---|
[216d6fc] | 243 |
|
---|
[43e02a6] | 244 | argc = cli_count_args(argv);
|
---|
[216d6fc] | 245 |
|
---|
| 246 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
|
---|
[d553f81] | 247 | c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind);
|
---|
[216d6fc] | 248 | switch (c) {
|
---|
| 249 | case 'h':
|
---|
[c9f5e24f] | 250 | help_cmd_cat(HELP_LONG);
|
---|
| 251 | return CMD_SUCCESS;
|
---|
[216d6fc] | 252 | case 'v':
|
---|
[c9f5e24f] | 253 | printf("%s\n", CAT_VERSION);
|
---|
| 254 | return CMD_SUCCESS;
|
---|
| 255 | case 'H':
|
---|
[afe1d1e] | 256 | printf("%s", cat_oops);
|
---|
[c9f5e24f] | 257 | return CMD_FAILURE;
|
---|
| 258 | case 't':
|
---|
[afe1d1e] | 259 | printf("%s", cat_oops);
|
---|
[c9f5e24f] | 260 | return CMD_FAILURE;
|
---|
| 261 | case 'b':
|
---|
[afe1d1e] | 262 | printf("%s", cat_oops);
|
---|
[216d6fc] | 263 | break;
|
---|
[c9f5e24f] | 264 | case 'm':
|
---|
[d553f81] | 265 | more = true;
|
---|
| 266 | break;
|
---|
| 267 | case 'x':
|
---|
| 268 | hex = true;
|
---|
| 269 | break;
|
---|
[216d6fc] | 270 | }
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[c9f5e24f] | 273 | argc -= optind;
|
---|
| 274 |
|
---|
| 275 | if (argc < 1) {
|
---|
| 276 | printf("%s - incorrect number of arguments. Try `%s --help'\n",
|
---|
| 277 | cmdname, cmdname);
|
---|
| 278 | return CMD_FAILURE;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | if (buffer <= 0)
|
---|
| 282 | buffer = CAT_DEFAULT_BUFLEN;
|
---|
[d553f81] | 283 |
|
---|
| 284 | if (more) {
|
---|
[79ae36dd] | 285 | rc = console_get_size(console, &cols, &rows);
|
---|
[d553f81] | 286 | if (rc != EOK) {
|
---|
| 287 | printf("%s - cannot get console size\n", cmdname);
|
---|
| 288 | return CMD_FAILURE;
|
---|
| 289 | }
|
---|
| 290 | console_cols = cols;
|
---|
| 291 | console_rows = rows;
|
---|
| 292 | paging_enabled = true;
|
---|
| 293 | newpage();
|
---|
| 294 | }
|
---|
[c9f5e24f] | 295 |
|
---|
[3cefd70] | 296 | for (i = optind; argv[i] != NULL && !should_quit; i++)
|
---|
[d553f81] | 297 | ret += cat_file(argv[i], buffer, hex);
|
---|
[216d6fc] | 298 |
|
---|
[c9f5e24f] | 299 | if (ret)
|
---|
| 300 | return CMD_FAILURE;
|
---|
| 301 | else
|
---|
| 302 | return CMD_SUCCESS;
|
---|
[216d6fc] | 303 | }
|
---|
| 304 |
|
---|