Changeset bc73be3 in mainline for uspace/app/bdsh/cmds/modules
- Timestamp:
- 2019-06-27T08:51:20Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8add15e0
- Parents:
- ad40b74b (diff), aeba767 (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:
-
- 2 added
- 4 edited
-
ls/ls.c (modified) (12 diffs)
-
ls/ls.h (modified) (2 diffs)
-
module_aliases.c (added)
-
module_aliases.h (modified) (1 diff)
-
modules.c (added)
-
modules.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/ls/ls.c
rad40b74b rbc73be3 42 42 #include <vfs/vfs.h> 43 43 #include <str.h> 44 #include <cap.h> 44 45 45 46 #include "ls.h" … … 58 59 { "unsort", no_argument, 0, 'u' }, 59 60 { "recursive", no_argument, 0, 'r' }, 61 { "exact-size", no_argument, 0, 'e' }, 62 { "single-column", no_argument, 0, '1' }, 60 63 { 0, 0, 0, 0 } 61 64 }; … … 63 66 /* Prototypes for the ls command, excluding entry points. */ 64 67 static unsigned int ls_start(ls_job_t *); 65 static void ls_print(struct dir_elem_t *); 66 static int ls_cmp(const void *, const void *); 68 static errno_t ls_print(struct dir_elem_t *); 69 static errno_t ls_print_single_column(struct dir_elem_t *); 70 static int ls_cmp_type_name(const void *, const void *); 71 static int ls_cmp_name(const void *, const void *); 67 72 static signed int ls_scan_dir(const char *, DIR *, struct dir_elem_t **); 68 73 static unsigned int ls_recursive(const char *, DIR *); … … 74 79 ls->sort = 1; 75 80 81 ls->exact_size = false; 82 ls->single_column = false; 83 ls->printer = ls_print; 76 84 return 1; 77 85 } … … 88 96 * @param de Directory element. 89 97 */ 90 static void ls_print(struct dir_elem_t *de) 91 { 92 if (de->s.is_file) 93 printf("%-40s\t%llu\n", de->name, (long long) de->s.size); 94 else if (de->s.is_directory) 95 printf("%-40s\t<dir>\n", de->name); 98 static errno_t ls_print(struct dir_elem_t *de) 99 { 100 int width = 13; 101 102 if (de->s.is_file) { 103 if (ls.exact_size) { 104 printf("%-40s\t%*llu\n", de->name, width, (long long) de->s.size); 105 return EOK; 106 } 107 108 cap_spec_t cap; 109 cap_from_blocks(de->s.size, 1, &cap); 110 cap_simplify(&cap); 111 112 char *rptr; 113 errno_t rc = cap_format(&cap, &rptr); 114 if (rc != EOK) { 115 return rc; 116 } 117 118 char *sep = str_rchr(rptr, ' '); 119 if (sep == NULL) { 120 free(rptr); 121 return ENOENT; 122 } 123 124 *sep = '\0'; 125 126 printf("%-40s\t%*s %2s\n", de->name, width - 3, rptr, sep + 1); 127 free(rptr); 128 } else if (de->s.is_directory) 129 printf("%-40s\t%*s\n", de->name, width, "<dir>"); 96 130 else 97 131 printf("%-40s\n", de->name); 132 133 return EOK; 134 } 135 136 static errno_t ls_print_single_column(struct dir_elem_t *de) 137 { 138 if (de->s.is_file) { 139 printf("%s\n", de->name); 140 } else { 141 printf("%s/\n", de->name); 142 } 143 144 return EOK; 98 145 } 99 146 … … 109 156 * @return -1 if a < b, 1 otherwise. 110 157 */ 111 static int ls_cmp (const void *a, const void *b)158 static int ls_cmp_type_name(const void *a, const void *b) 112 159 { 113 160 struct dir_elem_t const *da = a; … … 120 167 else 121 168 return 1; 169 } 170 171 /** Compare directories/files per name 172 * 173 * This comparision ignores the type of 174 * the node. Sorted will strictly by name. 175 * 176 */ 177 static int ls_cmp_name(const void *a, const void *b) 178 { 179 struct dir_elem_t const *da = a; 180 struct dir_elem_t const *db = b; 181 182 return str_cmp(da->name, db->name); 122 183 } 123 184 … … 192 253 } 193 254 194 if (ls.sort) 195 qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), ls_cmp); 196 197 for (i = 0; i < nbdirs; i++) 198 ls_print(&tosort[i]); 255 if (ls.sort) { 256 int (*compar)(const void *, const void *); 257 compar = ls.single_column ? ls_cmp_name : ls_cmp_type_name; 258 qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), compar); 259 } 260 261 for (i = 0; i < nbdirs; i++) { 262 if (ls.printer(&tosort[i]) != EOK) { 263 cli_error(CL_ENOMEM, "%s: Out of memory", cmdname); 264 goto out; 265 } 266 } 199 267 200 268 /* Populate the directory list. */ … … 333 401 "If not path is given, the current working directory is used.\n" 334 402 "Options:\n" 335 " -h, --help A short option summary\n" 336 " -u, --unsort Do not sort directory entries\n" 337 " -r, --recursive List subdirectories recursively\n", 403 " -h, --help A short option summary\n" 404 " -u, --unsort Do not sort directory entries\n" 405 " -r, --recursive List subdirectories recursively\n" 406 " -e, --exact-size File sizes will be unformatted (raw bytes count)\n" 407 " -1, --single-column Only the names will be returned\n", 338 408 cmdname); 339 409 } … … 364 434 365 435 while (c != -1) { 366 c = getopt_long(argc, argv, "hur ", long_options, &opt_ind);436 c = getopt_long(argc, argv, "hure1", long_options, &opt_ind); 367 437 switch (c) { 368 438 case 'h': … … 375 445 ls.recursive = 1; 376 446 break; 447 case 'e': 448 ls.exact_size = true; 449 break; 450 case '1': 451 ls.single_column = true; 452 ls.printer = ls_print_single_column; 453 break; 377 454 } 378 455 } … … 400 477 switch (scope) { 401 478 case LS_FILE: 402 ls_print(&de); 479 if (ls.printer(&de) != EOK) { 480 cli_error(CL_ENOMEM, "%s: Out of memory", cmdname); 481 return CMD_FAILURE; 482 } 403 483 break; 404 484 case LS_DIR: -
uspace/app/bdsh/cmds/modules/ls/ls.h
rad40b74b rbc73be3 8 8 #define LS_FILE 1 9 9 #define LS_DIR 2 10 11 typedef struct {12 /* Options set at runtime. */13 unsigned int recursive;14 unsigned int sort;15 16 } ls_job_t;17 10 18 11 /** Structure to represent a directory entry. … … 26 19 }; 27 20 21 typedef struct { 22 /* Options set at runtime. */ 23 unsigned int recursive; 24 unsigned int sort; 25 26 bool single_column; 27 bool exact_size; 28 29 errno_t (*printer)(struct dir_elem_t *); 30 } ls_job_t; 31 28 32 #endif -
uspace/app/bdsh/cmds/modules/module_aliases.h
rad40b74b rbc73be3 30 30 #define MODULE_ALIASES_H 31 31 32 /* 33 * Modules that declare multiple names for themselves but use the 34 * same entry functions are aliases. This array helps to determine if 35 * a module is an alias, as such it can be invoked differently. 36 * format is alias , real_name 37 */ 38 39 /* 40 * So far, this is only used in the help display but could be used to 41 * handle a module differently even prior to reaching its entry code. 42 * For instance, 'exit' could behave differently than 'quit', prior to 43 * the entry point being reached. 44 */ 45 46 const char *mod_aliases[] = { 47 "ren", "mv", 48 "umount", "unmount", 49 NULL, NULL 50 }; 32 extern const char *mod_aliases[]; 51 33 52 34 #endif -
uspace/app/bdsh/cmds/modules/modules.h
rad40b74b rbc73be3 30 30 #define MODULES_H 31 31 32 /* 33 * Each built in function has two files, one being an entry.h file which 34 * prototypes the run/help entry functions, the other being a .def file 35 * which fills the modules[] array according to the cmd_t structure 36 * defined in cmds.h. 37 * 38 * To add or remove a module, just make a new directory in cmds/modules 39 * for it and copy the 'show' example for basics, then include it here. 40 * (or reverse the process to remove one) 41 * 42 * NOTE: See module_ aliases.h as well, this is where aliases (commands that 43 * share an entry point with others) are indexed 44 */ 32 #include "../cmds.h" 33 #include "modules.h" 45 34 46 #include "config.h" 47 48 /* Prototypes for each module's entry (help/exec) points */ 49 50 #include "help/entry.h" 51 #include "mkdir/entry.h" 52 #include "mkfile/entry.h" 53 #include "rm/entry.h" 54 #include "cat/entry.h" 55 #include "touch/entry.h" 56 #include "ls/entry.h" 57 #include "pwd/entry.h" 58 #include "sleep/entry.h" 59 #include "cp/entry.h" 60 #include "mv/entry.h" 61 #include "mount/entry.h" 62 #include "unmount/entry.h" 63 #include "kcon/entry.h" 64 #include "printf/entry.h" 65 #include "echo/entry.h" 66 #include "cmp/entry.h" 67 68 /* 69 * Each .def function fills the module_t struct with the individual name, entry 70 * point, help entry point, etc. You can use config.h to control what modules 71 * are loaded based on what libraries exist on the system. 72 */ 73 74 module_t modules[] = { 75 #include "help/help_def.inc" 76 #include "mkdir/mkdir_def.inc" 77 #include "mkfile/mkfile_def.inc" 78 #include "rm/rm_def.inc" 79 #include "cat/cat_def.inc" 80 #include "touch/touch_def.inc" 81 #include "ls/ls_def.inc" 82 #include "pwd/pwd_def.inc" 83 #include "sleep/sleep_def.inc" 84 #include "cp/cp_def.inc" 85 #include "mv/mv_def.inc" 86 #include "mount/mount_def.inc" 87 #include "unmount/unmount_def.inc" 88 #include "kcon/kcon_def.inc" 89 #include "printf/printf_def.inc" 90 #include "echo/echo_def.inc" 91 #include "cmp/cmp_def.inc" 92 93 { NULL, NULL, NULL, NULL } 94 }; 35 extern module_t modules[]; 95 36 96 37 #endif
Note:
See TracChangeset
for help on using the changeset viewer.
