Changeset ad7a6c9 in mainline for uspace/app/bdsh/cmds/modules
- Timestamp:
- 2011-03-30T13:10:24Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4ae90f9
- Parents:
- 6e50466 (diff), d6b81941 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- uspace/app/bdsh/cmds/modules
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/bdd/bdd.c
r6e50466 rad7a6c9 70 70 unsigned int i, j; 71 71 devmap_handle_t handle; 72 aoff64_t offset; 72 73 uint8_t *blk; 73 74 size_t size, bytes, rows; … … 120 121 } 121 122 123 offset = ba * block_size; 124 122 125 while (size > 0) { 123 126 rc = block_read_direct(handle, ba, 1, blk); … … 133 136 134 137 for (j = 0; j < rows; j++) { 138 printf("[%06" PRIxOFF64 "] ", offset); 135 139 for (i = 0; i < BPR; i++) { 136 140 if (j * BPR + i < bytes) … … 152 156 } 153 157 } 158 offset += BPR; 154 159 putchar('\n'); 155 160 } -
uspace/app/bdsh/cmds/modules/cat/cat.c
r6e50466 rad7a6c9 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 off64_t total = 0;88 166 char *buff = NULL; 167 int i; 168 size_t offset = 0; 89 169 90 170 fd = open(fname, O_RDONLY); … … 93 173 return 1; 94 174 } 95 96 total = lseek(fd, 0, SEEK_END);97 lseek(fd, 0, SEEK_SET);98 175 99 176 if (NULL == (buff = (char *) malloc(blen + 1))) { … … 109 186 count += bytes; 110 187 buff[bytes] = '\0'; 111 printf("%s", buff); 188 offset = 0; 189 for (i = 0; i < bytes && !should_quit; i++) { 190 if (hex) { 191 paged_char(hexchars[((uint8_t)buff[i])/16]); 192 paged_char(hexchars[((uint8_t)buff[i])%16]); 193 } 194 else { 195 wchar_t c = str_decode(buff, &offset, bytes); 196 if (c == 0) { 197 // reached end of string 198 break; 199 } 200 paged_char(c); 201 } 202 203 } 112 204 reads++; 113 205 } 114 } while (bytes > 0 );206 } while (bytes > 0 && !should_quit); 115 207 116 208 close(fd); … … 131 223 unsigned int argc, i, ret = 0, buffer = 0; 132 224 int c, opt_ind; 225 bool hex = false; 226 bool more = false; 227 sysarg_t rows, cols; 228 int rc; 229 230 // reset global state 231 // TODO: move to structure? 232 paging_enabled = false; 233 chars_remaining = 0; 234 lines_remaining = 0; 235 console_cols = 0; 236 console_rows = 0; 237 should_quit = false; 133 238 134 239 argc = cli_count_args(argv); 135 240 136 241 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 137 c = getopt_long(argc, argv, " hvmH:t:b:", long_options, &opt_ind);242 c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind); 138 243 switch (c) { 139 244 case 'h': … … 144 249 return CMD_SUCCESS; 145 250 case 'H': 146 printf( cat_oops);251 printf("%s", cat_oops); 147 252 return CMD_FAILURE; 148 253 case 't': 149 printf( cat_oops);254 printf("%s", cat_oops); 150 255 return CMD_FAILURE; 151 256 case 'b': 152 printf( cat_oops);257 printf("%s", cat_oops); 153 258 break; 154 259 case 'm': 155 printf(cat_oops); 156 return CMD_FAILURE; 260 more = true; 261 break; 262 case 'x': 263 hex = true; 264 break; 157 265 } 158 266 } … … 168 276 if (buffer <= 0) 169 277 buffer = CAT_DEFAULT_BUFLEN; 170 171 for (i = optind; argv[i] != NULL; i++) 172 ret += cat_file(argv[i], buffer); 278 279 if (more) { 280 rc = console_get_size(fphone(stdout), &cols, &rows); 281 if (rc != EOK) { 282 printf("%s - cannot get console size\n", cmdname); 283 return CMD_FAILURE; 284 } 285 console_cols = cols; 286 console_rows = rows; 287 paging_enabled = true; 288 newpage(); 289 } 290 291 for (i = optind; argv[i] != NULL && !should_quit; i++) 292 ret += cat_file(argv[i], buffer, hex); 173 293 174 294 if (ret) -
uspace/app/bdsh/cmds/modules/cat/cat.h
r6e50466 rad7a6c9 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 */ -
uspace/app/bdsh/cmds/modules/cp/cp.c
r6e50466 rad7a6c9 108 108 for (;;) { 109 109 ssize_t res; 110 size_t written = 0; 110 111 111 112 bytes = read(fd1, buff, blen); … … 120 121 * returned less data than requested. 121 122 */ 122 bytes = write(fd2, buff , res);123 bytes = write(fd2, buff + written, res); 123 124 if (bytes < 0) 124 125 goto err; 126 written += bytes; 125 127 res -= bytes; 126 128 } while (res > 0); -
uspace/app/bdsh/cmds/modules/mkfile/mkfile.c
r6e50466 rad7a6c9 125 125 126 126 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 127 c = getopt_long(argc, argv, " pvhVfm:", long_options, &opt_ind);127 c = getopt_long(argc, argv, "s:h", long_options, &opt_ind); 128 128 switch (c) { 129 129 case 'h': -
uspace/app/bdsh/cmds/modules/mount/mount.c
r6e50466 rad7a6c9 31 31 #include <vfs/vfs.h> 32 32 #include <errno.h> 33 #include <getopt.h> 33 34 #include "config.h" 34 35 #include "util.h" … … 40 41 static const char *cmdname = "mount"; 41 42 42 /* Dispays help for mount in various levels */ 43 static struct option const long_options[] = { 44 { "help", no_argument, 0, 'h' }, 45 { 0, 0, 0, 0 } 46 }; 47 48 49 /* Displays help for mount in various levels */ 43 50 void help_cmd_mount(unsigned int level) 44 51 { … … 59 66 unsigned int argc; 60 67 const char *mopts = ""; 61 int rc ;68 int rc, c, opt_ind; 62 69 63 70 argc = cli_count_args(argv); 64 71 72 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 73 c = getopt_long(argc, argv, "h", long_options, &opt_ind); 74 switch (c) { 75 case 'h': 76 help_cmd_mount(HELP_LONG); 77 return CMD_SUCCESS; 78 } 79 } 80 65 81 if ((argc < 4) || (argc > 5)) { 66 printf("%s: invalid number of arguments. \n",82 printf("%s: invalid number of arguments. Try `mount --help'\n", 67 83 cmdname); 68 84 return CMD_FAILURE; -
uspace/app/bdsh/cmds/modules/rm/rm.c
r6e50466 rad7a6c9 101 101 } 102 102 103 static unsigned int rm_recursive_not_empty_dirs(const char *path) 104 { 105 DIR *dirp; 106 struct dirent *dp; 107 char buff[PATH_MAX]; 108 unsigned int scope; 109 unsigned int ret = 0; 110 111 dirp = opendir(path); 112 if (!dirp) { 113 /* May have been deleted between scoping it and opening it */ 114 cli_error(CL_EFAIL, "Could not open %s", path); 115 return ret; 116 } 117 118 memset(buff, 0, sizeof(buff)); 119 while ((dp = readdir(dirp))) { 120 snprintf(buff, PATH_MAX - 1, "%s/%s", path, dp->d_name); 121 scope = rm_scope(buff); 122 switch (scope) { 123 case RM_BOGUS: 124 break; 125 case RM_FILE: 126 ret += rm_single(buff); 127 break; 128 case RM_DIR: 129 ret += rm_recursive(buff); 130 break; 131 } 132 } 133 134 return ret; 135 } 136 103 137 static unsigned int rm_recursive(const char *path) 104 138 { 105 139 int rc; 140 unsigned int ret = 0; 106 141 107 142 /* First see if it will just go away */ … … 111 146 112 147 /* Its not empty, recursively scan it */ 113 cli_error(CL_ENOTSUP, 114 "Can not remove %s, directory not empty", path); 115 return 1; 148 ret = rm_recursive_not_empty_dirs(path); 149 150 /* Delete directory */ 151 rc = rmdir(path); 152 if (rc == 0) 153 return ret; 154 155 cli_error(CL_ENOTSUP, "Can not remove %s", path); 156 157 return ret + 1; 116 158 } 117 159 … … 227 269 } 228 270 memset(buff, 0, sizeof(buff)); 229 snprintf(buff, len, argv[i]);271 snprintf(buff, len, "%s", argv[i]); 230 272 231 273 scope = rm_scope(buff);
Note:
See TracChangeset
for help on using the changeset viewer.
