Changeset e7ffdd3 in mainline
- Timestamp:
- 2011-03-22T20:02:28Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ac897e8
- Parents:
- 5e3eea10
- Location:
- uspace/app/bdsh/cmds/modules/cat
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cat/cat.c
r5e3eea10 re7ffdd3 1 1 /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> 2 * Copyright (c) 2011, Martin Sucha 2 3 * All rights reserved. 3 4 * … … 35 36 #include <str.h> 36 37 #include <fcntl.h> 38 #include <io/console.h> 39 #include <io/color.h> 40 #include <io/style.h> 41 #include <errno.h> 42 #include <vfs/vfs.h> 43 #include <assert.h> 37 44 38 45 #include "config.h" … … 48 55 49 56 static const char *cat_oops = "That option is not yet supported\n"; 57 static const char *hexchars = "0123456789abcdef"; 58 59 static size_t chars_per_screen = 0; 60 static size_t chars_remaining = 0; 50 61 51 62 static struct option const long_options[] = { … … 56 67 { "buffer", required_argument, 0, 'b' }, 57 68 { "more", no_argument, 0, 'm' }, 69 { "hex", no_argument, 0, 'x' }, 58 70 { 0, 0, 0, 0 } 59 71 }; … … 75 87 " -b, --buffer ## Set the read buffer size to ##\n" 76 88 " -m, --more Pause after each screen full\n" 89 " -x, --hex Print bytes as hex values\n" 77 90 "Currently, %s is under development, some options don't work.\n", 78 91 cmdname, cmdname); … … 82 95 } 83 96 84 static unsigned int cat_file(const char *fname, size_t blen) 97 static void waitprompt() 98 { 99 sysarg_t rows, cols; 100 if (console_get_size(fphone(stdout), &cols, &rows) == EOK) { 101 console_set_pos(fphone(stdout), 0, rows-1); 102 } 103 console_set_color(fphone(stdout), COLOR_BLUE, COLOR_WHITE, 0); 104 printf("Press any key to continue"); 105 fflush(stdout); 106 console_set_style(fphone(stdout), STYLE_NORMAL); 107 } 108 109 static void waitkey() 110 { 111 console_event_t ev; 112 113 while (true) { 114 if (!console_get_event(fphone(stdin), &ev)) { 115 return; 116 } 117 if (ev.type == KEY_PRESS) { 118 return; 119 } 120 } 121 assert(false); 122 } 123 124 static void newpage() 125 { 126 console_clear(fphone(stdout)); 127 chars_remaining = chars_per_screen; 128 } 129 130 static void paged_char(wchar_t c) 131 { 132 putchar(c); 133 if (chars_per_screen > 0) { 134 chars_remaining--; 135 if (chars_remaining == 0) { 136 fflush(stdout); 137 waitprompt(); 138 waitkey(); 139 newpage(); 140 } 141 } 142 } 143 144 static unsigned int cat_file(const char *fname, size_t blen, bool hex) 85 145 { 86 146 int fd, bytes = 0, count = 0, reads = 0; 87 147 off64_t total = 0; 88 148 char *buff = NULL; 149 int i; 150 size_t offset = 0; 89 151 90 152 fd = open(fname, O_RDONLY); … … 109 171 count += bytes; 110 172 buff[bytes] = '\0'; 111 printf("%s", buff); 173 offset = 0; 174 for (i = 0; i < bytes; i++) { 175 if (hex) { 176 paged_char(hexchars[((uint8_t)buff[i])/16]); 177 paged_char(hexchars[((uint8_t)buff[i])%16]); 178 } 179 else { 180 wchar_t c = str_decode(buff, &offset, bytes); 181 if (c == 0) { 182 // reached end of string 183 break; 184 } 185 paged_char(c); 186 } 187 188 } 112 189 reads++; 113 190 } … … 131 208 unsigned int argc, i, ret = 0, buffer = 0; 132 209 int c, opt_ind; 210 bool hex = false; 211 bool more = false; 212 sysarg_t rows, cols; 213 int rc; 133 214 134 215 argc = cli_count_args(argv); 135 216 136 217 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 137 c = getopt_long(argc, argv, " hvmH:t:b:", long_options, &opt_ind);218 c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind); 138 219 switch (c) { 139 220 case 'h': … … 153 234 break; 154 235 case 'm': 155 printf("%s", cat_oops); 156 return CMD_FAILURE; 236 more = true; 237 break; 238 case 'x': 239 hex = true; 240 break; 157 241 } 158 242 } … … 168 252 if (buffer <= 0) 169 253 buffer = CAT_DEFAULT_BUFLEN; 254 255 if (more) { 256 rc = console_get_size(fphone(stdout), &cols, &rows); 257 if (rc != EOK) { 258 printf("%s - cannot get console size\n", cmdname); 259 return CMD_FAILURE; 260 } 261 chars_per_screen = cols * (rows-1); 262 newpage(); 263 } 170 264 171 265 for (i = optind; argv[i] != NULL; i++) 172 ret += cat_file(argv[i], buffer );266 ret += cat_file(argv[i], buffer, hex); 173 267 174 268 if (ret) -
uspace/app/bdsh/cmds/modules/cat/cat.h
r5e3eea10 re7ffdd3 4 4 /* Prototypes for the cat command, excluding entry points */ 5 5 6 static unsigned int cat_file(const char *, size_t );6 static unsigned int cat_file(const char *, size_t, bool); 7 7 8 8 #endif /* CAT_H */
Note:
See TracChangeset
for help on using the changeset viewer.