Changes in uspace/app/bdsh/cmds/modules/cat/cat.c [3cefd70:afe1d1e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cat/cat.c
r3cefd70 rafe1d1e 1 1 /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> 2 * Copyright (c) 2011, Martin Sucha3 2 * All rights reserved. 4 3 * … … 36 35 #include <str.h> 37 36 #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>45 37 46 38 #include "config.h" … … 56 48 57 49 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;66 50 67 51 static struct option const long_options[] = { … … 72 56 { "buffer", required_argument, 0, 'b' }, 73 57 { "more", no_argument, 0, 'm' }, 74 { "hex", no_argument, 0, 'x' },75 58 { 0, 0, 0, 0 } 76 59 }; … … 92 75 " -b, --buffer ## Set the read buffer size to ##\n" 93 76 " -m, --more Pause after each screen full\n" 94 " -x, --hex Print bytes as hex values\n"95 77 "Currently, %s is under development, some options don't work.\n", 96 78 cmdname, cmdname); … … 100 82 } 101 83 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) 84 static unsigned int cat_file(const char *fname, size_t blen) 164 85 { 165 86 int fd, bytes = 0, count = 0, reads = 0; 166 87 off64_t total = 0; 167 88 char *buff = NULL; 168 int i;169 size_t offset = 0;170 89 171 90 fd = open(fname, O_RDONLY); … … 190 109 count += bytes; 191 110 buff[bytes] = '\0'; 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 } 111 printf("%s", buff); 208 112 reads++; 209 113 } 210 } while (bytes > 0 && !should_quit);114 } while (bytes > 0); 211 115 212 116 close(fd); … … 227 131 unsigned int argc, i, ret = 0, buffer = 0; 228 132 int c, opt_ind; 229 bool hex = false;230 bool more = false;231 sysarg_t rows, cols;232 int rc;233 234 // reset global state235 // 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;242 133 243 134 argc = cli_count_args(argv); 244 135 245 136 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 246 c = getopt_long(argc, argv, " xhvmH:t:b:", long_options, &opt_ind);137 c = getopt_long(argc, argv, "hvmH:t:b:", long_options, &opt_ind); 247 138 switch (c) { 248 139 case 'h': … … 262 153 break; 263 154 case 'm': 264 more = true; 265 break; 266 case 'x': 267 hex = true; 268 break; 155 printf("%s", cat_oops); 156 return CMD_FAILURE; 269 157 } 270 158 } … … 280 168 if (buffer <= 0) 281 169 buffer = CAT_DEFAULT_BUFLEN; 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 170 295 for (i = optind; argv[i] != NULL && !should_quit; i++)296 ret += cat_file(argv[i], buffer , hex);171 for (i = optind; argv[i] != NULL; i++) 172 ret += cat_file(argv[i], buffer); 297 173 298 174 if (ret)
Note:
See TracChangeset
for help on using the changeset viewer.