Changeset d553f81 in mainline for uspace/app/bdsh/cmds/modules/cat/cat.c
- Timestamp:
- 2011-03-24T18:31:01Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- fb2ceeb
- Parents:
- 8f29507f
- git-author:
- Martin Sucha <> (2011-03-24 18:31:01)
- git-committer:
- Jakub Jermar <jakub@…> (2011-03-24 18:31:01)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cat/cat.c
r8f29507f rd553f81 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 bool paging_enabled = false; 60 static size_t chars_remaining = 0; 61 static size_t lines_remaining = 0; 62 static sysarg_t console_cols = 0; 63 static sysarg_t console_rows = 0; 50 64 51 65 static struct option const long_options[] = { … … 56 70 { "buffer", required_argument, 0, 'b' }, 57 71 { "more", no_argument, 0, 'm' }, 72 { "hex", no_argument, 0, 'x' }, 58 73 { 0, 0, 0, 0 } 59 74 }; … … 75 90 " -b, --buffer ## Set the read buffer size to ##\n" 76 91 " -m, --more Pause after each screen full\n" 92 " -x, --hex Print bytes as hex values\n" 77 93 "Currently, %s is under development, some options don't work.\n", 78 94 cmdname, cmdname); … … 82 98 } 83 99 84 static unsigned int cat_file(const char *fname, size_t blen) 100 static void waitprompt() 101 { 102 console_set_pos(fphone(stdout), 0, console_rows-1); 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 = console_cols; 128 lines_remaining = console_rows-1; 129 } 130 131 static void paged_char(wchar_t c) 132 { 133 putchar(c); 134 if (paging_enabled) { 135 chars_remaining--; 136 if (c == '\n' || chars_remaining == 0) { 137 chars_remaining = console_cols; 138 lines_remaining--; 139 } 140 if (lines_remaining == 0) { 141 fflush(stdout); 142 waitprompt(); 143 waitkey(); 144 newpage(); 145 } 146 } 147 } 148 149 static unsigned int cat_file(const char *fname, size_t blen, bool hex) 85 150 { 86 151 int fd, bytes = 0, count = 0, reads = 0; 87 152 off64_t total = 0; 88 153 char *buff = NULL; 154 int i; 155 size_t offset = 0; 89 156 90 157 fd = open(fname, O_RDONLY); … … 109 176 count += bytes; 110 177 buff[bytes] = '\0'; 111 printf("%s", buff); 178 offset = 0; 179 for (i = 0; i < bytes; i++) { 180 if (hex) { 181 paged_char(hexchars[((uint8_t)buff[i])/16]); 182 paged_char(hexchars[((uint8_t)buff[i])%16]); 183 } 184 else { 185 wchar_t c = str_decode(buff, &offset, bytes); 186 if (c == 0) { 187 // reached end of string 188 break; 189 } 190 paged_char(c); 191 } 192 193 } 112 194 reads++; 113 195 } … … 131 213 unsigned int argc, i, ret = 0, buffer = 0; 132 214 int c, opt_ind; 215 bool hex = false; 216 bool more = false; 217 sysarg_t rows, cols; 218 int rc; 219 220 // reset global state 221 // TODO: move to structure? 222 paging_enabled = false; 223 chars_remaining = 0; 224 lines_remaining = 0; 225 console_cols = 0; 226 console_rows = 0; 133 227 134 228 argc = cli_count_args(argv); 135 229 136 230 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 137 c = getopt_long(argc, argv, " hvmH:t:b:", long_options, &opt_ind);231 c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind); 138 232 switch (c) { 139 233 case 'h': … … 153 247 break; 154 248 case 'm': 155 printf("%s", cat_oops); 156 return CMD_FAILURE; 249 more = true; 250 break; 251 case 'x': 252 hex = true; 253 break; 157 254 } 158 255 } … … 168 265 if (buffer <= 0) 169 266 buffer = CAT_DEFAULT_BUFLEN; 267 268 if (more) { 269 rc = console_get_size(fphone(stdout), &cols, &rows); 270 if (rc != EOK) { 271 printf("%s - cannot get console size\n", cmdname); 272 return CMD_FAILURE; 273 } 274 console_cols = cols; 275 console_rows = rows; 276 paging_enabled = true; 277 newpage(); 278 } 170 279 171 280 for (i = optind; argv[i] != NULL; i++) 172 ret += cat_file(argv[i], buffer );281 ret += cat_file(argv[i], buffer, hex); 173 282 174 283 if (ret)
Note:
See TracChangeset
for help on using the changeset viewer.