Changes in uspace/app/bdsh/cmds/modules/cat/cat.c [afe1d1e:3cefd70] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cat/cat.c
rafe1d1e r3cefd70 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 <io/keycode.h> 42 #include <errno.h> 43 #include <vfs/vfs.h> 44 #include <assert.h> 37 45 38 46 #include "config.h" … … 48 56 49 57 static const char *cat_oops = "That option is not yet supported\n"; 58 static const char *hexchars = "0123456789abcdef"; 59 60 static bool paging_enabled = false; 61 static size_t chars_remaining = 0; 62 static size_t lines_remaining = 0; 63 static sysarg_t console_cols = 0; 64 static sysarg_t console_rows = 0; 65 static bool should_quit = false; 50 66 51 67 static struct option const long_options[] = { … … 56 72 { "buffer", required_argument, 0, 'b' }, 57 73 { "more", no_argument, 0, 'm' }, 74 { "hex", no_argument, 0, 'x' }, 58 75 { 0, 0, 0, 0 } 59 76 }; … … 75 92 " -b, --buffer ## Set the read buffer size to ##\n" 76 93 " -m, --more Pause after each screen full\n" 94 " -x, --hex Print bytes as hex values\n" 77 95 "Currently, %s is under development, some options don't work.\n", 78 96 cmdname, cmdname); … … 82 100 } 83 101 84 static unsigned int cat_file(const char *fname, size_t blen) 102 static void waitprompt() 103 { 104 console_set_pos(fphone(stdout), 0, console_rows-1); 105 console_set_color(fphone(stdout), COLOR_BLUE, COLOR_WHITE, 0); 106 printf("ENTER/SPACE/PAGE DOWN - next page, " 107 "ESC/Q - quit, C - continue unpaged"); 108 fflush(stdout); 109 console_set_style(fphone(stdout), STYLE_NORMAL); 110 } 111 112 static void waitkey() 113 { 114 console_event_t ev; 115 116 while (true) { 117 if (!console_get_event(fphone(stdin), &ev)) { 118 return; 119 } 120 if (ev.type == KEY_PRESS) { 121 if (ev.key == KC_ESCAPE || ev.key == KC_Q) { 122 should_quit = true; 123 return; 124 } 125 if (ev.key == KC_C) { 126 paging_enabled = false; 127 return; 128 } 129 if (ev.key == KC_ENTER || ev.key == KC_SPACE || 130 ev.key == KC_PAGE_DOWN) { 131 return; 132 } 133 } 134 } 135 assert(false); 136 } 137 138 static void newpage() 139 { 140 console_clear(fphone(stdout)); 141 chars_remaining = console_cols; 142 lines_remaining = console_rows-1; 143 } 144 145 static void paged_char(wchar_t c) 146 { 147 putchar(c); 148 if (paging_enabled) { 149 chars_remaining--; 150 if (c == '\n' || chars_remaining == 0) { 151 chars_remaining = console_cols; 152 lines_remaining--; 153 } 154 if (lines_remaining == 0) { 155 fflush(stdout); 156 waitprompt(); 157 waitkey(); 158 newpage(); 159 } 160 } 161 } 162 163 static unsigned int cat_file(const char *fname, size_t blen, bool hex) 85 164 { 86 165 int fd, bytes = 0, count = 0, reads = 0; 87 166 off64_t total = 0; 88 167 char *buff = NULL; 168 int i; 169 size_t offset = 0; 89 170 90 171 fd = open(fname, O_RDONLY); … … 109 190 count += bytes; 110 191 buff[bytes] = '\0'; 111 printf("%s", buff); 192 offset = 0; 193 for (i = 0; i < bytes && !should_quit; i++) { 194 if (hex) { 195 paged_char(hexchars[((uint8_t)buff[i])/16]); 196 paged_char(hexchars[((uint8_t)buff[i])%16]); 197 } 198 else { 199 wchar_t c = str_decode(buff, &offset, bytes); 200 if (c == 0) { 201 // reached end of string 202 break; 203 } 204 paged_char(c); 205 } 206 207 } 112 208 reads++; 113 209 } 114 } while (bytes > 0 );210 } while (bytes > 0 && !should_quit); 115 211 116 212 close(fd); … … 131 227 unsigned int argc, i, ret = 0, buffer = 0; 132 228 int c, opt_ind; 229 bool hex = false; 230 bool more = false; 231 sysarg_t rows, cols; 232 int rc; 233 234 // reset global state 235 // TODO: move to structure? 236 paging_enabled = false; 237 chars_remaining = 0; 238 lines_remaining = 0; 239 console_cols = 0; 240 console_rows = 0; 241 should_quit = false; 133 242 134 243 argc = cli_count_args(argv); 135 244 136 245 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 137 c = getopt_long(argc, argv, " hvmH:t:b:", long_options, &opt_ind);246 c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind); 138 247 switch (c) { 139 248 case 'h': … … 153 262 break; 154 263 case 'm': 155 printf("%s", cat_oops); 156 return CMD_FAILURE; 264 more = true; 265 break; 266 case 'x': 267 hex = true; 268 break; 157 269 } 158 270 } … … 168 280 if (buffer <= 0) 169 281 buffer = CAT_DEFAULT_BUFLEN; 170 171 for (i = optind; argv[i] != NULL; i++) 172 ret += cat_file(argv[i], buffer); 282 283 if (more) { 284 rc = console_get_size(fphone(stdout), &cols, &rows); 285 if (rc != EOK) { 286 printf("%s - cannot get console size\n", cmdname); 287 return CMD_FAILURE; 288 } 289 console_cols = cols; 290 console_rows = rows; 291 paging_enabled = true; 292 newpage(); 293 } 294 295 for (i = optind; argv[i] != NULL && !should_quit; i++) 296 ret += cat_file(argv[i], buffer, hex); 173 297 174 298 if (ret)
Note:
See TracChangeset
for help on using the changeset viewer.