Changes in uspace/app/bdsh/cmds/modules/cat/cat.c [afe1d1e:36ab7c7] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cat/cat.c
rafe1d1e r36ab7c7 1 /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> 1 /* 2 * Copyright (c) 2008 Tim Post 3 * Copyright (c) 2011, Martin Sucha 2 4 * All rights reserved. 3 5 * 4 6 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions 8 * are met: 6 9 * 7 * Redistributions of source code must retain the above copyright notice, this 8 * list of conditions and the following disclaimer. 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. 9 17 * 10 * Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * Neither the name of the original program's authors nor the names of its 15 * contributors may be used to endorse or promote products derived from this 16 * software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 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. 29 28 */ 30 29 … … 35 34 #include <str.h> 36 35 #include <fcntl.h> 36 #include <io/console.h> 37 #include <io/color.h> 38 #include <io/style.h> 39 #include <io/keycode.h> 40 #include <errno.h> 41 #include <vfs/vfs.h> 42 #include <assert.h> 37 43 38 44 #include "config.h" … … 48 54 49 55 static const char *cat_oops = "That option is not yet supported\n"; 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; 63 static bool should_quit = false; 64 65 static console_ctrl_t *console = NULL; 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(console, 0, console_rows-1); 105 console_set_color(console, COLOR_BLUE, COLOR_WHITE, 0); 106 107 printf("ENTER/SPACE/PAGE DOWN - next page, " 108 "ESC/Q - quit, C - continue unpaged"); 109 fflush(stdout); 110 111 console_set_style(console, STYLE_NORMAL); 112 } 113 114 static void waitkey() 115 { 116 kbd_event_t ev; 117 118 while (true) { 119 if (!console_get_kbd_event(console, &ev)) { 120 return; 121 } 122 if (ev.type == KEY_PRESS) { 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 } 135 } 136 } 137 assert(false); 138 } 139 140 static void newpage() 141 { 142 console_clear(console); 143 chars_remaining = console_cols; 144 lines_remaining = console_rows - 1; 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) 85 166 { 86 167 int fd, bytes = 0, count = 0, reads = 0; 87 off64_t total = 0;88 168 char *buff = NULL; 169 int i; 170 size_t offset = 0; 89 171 90 172 fd = open(fname, O_RDONLY); … … 93 175 return 1; 94 176 } 95 96 total = lseek(fd, 0, SEEK_END);97 lseek(fd, 0, SEEK_SET);98 177 99 178 if (NULL == (buff = (char *) malloc(blen + 1))) { … … 109 188 count += bytes; 110 189 buff[bytes] = '\0'; 111 printf("%s", buff); 190 offset = 0; 191 for (i = 0; i < bytes && !should_quit; i++) { 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) { 199 /* Reached end of string */ 200 break; 201 } 202 paged_char(c); 203 } 204 205 } 112 206 reads++; 113 207 } 114 } while (bytes > 0 );208 } while (bytes > 0 && !should_quit); 115 209 116 210 close(fd); … … 131 225 unsigned int argc, i, ret = 0, buffer = 0; 132 226 int c, opt_ind; 227 bool hex = false; 228 bool more = false; 229 sysarg_t rows, cols; 230 int rc; 231 232 /* 233 * reset global state 234 * TODO: move to structure? 235 */ 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 console = console_init(stdin, stdout); 133 243 134 244 argc = cli_count_args(argv); 135 245 136 246 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 137 c = getopt_long(argc, argv, " hvmH:t:b:", long_options, &opt_ind);247 c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind); 138 248 switch (c) { 139 249 case 'h': … … 153 263 break; 154 264 case 'm': 155 printf("%s", cat_oops); 156 return CMD_FAILURE; 265 more = true; 266 break; 267 case 'x': 268 hex = true; 269 break; 157 270 } 158 271 } … … 168 281 if (buffer <= 0) 169 282 buffer = CAT_DEFAULT_BUFLEN; 170 171 for (i = optind; argv[i] != NULL; i++) 172 ret += cat_file(argv[i], buffer); 283 284 if (more) { 285 rc = console_get_size(console, &cols, &rows); 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 } 295 296 for (i = optind; argv[i] != NULL && !should_quit; i++) 297 ret += cat_file(argv[i], buffer, hex); 173 298 174 299 if (ret)
Note:
See TracChangeset
for help on using the changeset viewer.