Changeset a35b458 in mainline for uspace/app/bdsh
- Timestamp:
- 2018-03-02T20:10:49Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- Location:
- uspace/app/bdsh
- Files:
-
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/builtin_cmds.c
r3061bc1 ra35b458 107 107 108 108 cmd += builtin; 109 109 110 110 iostate_t *old_iostate = get_iostate(); 111 111 set_iostate(new_iostate); 112 112 113 113 if (NULL != cmd->entry) { 114 114 rc = ((int)cmd->entry(argv, usr)); … … 116 116 rc = CL_ENOENT; 117 117 } 118 118 119 119 set_iostate(old_iostate); 120 120 -
uspace/app/bdsh/cmds/mod_cmds.c
r3061bc1 ra35b458 129 129 130 130 mod += module; 131 131 132 132 iostate_t *old_iostate = get_iostate(); 133 133 set_iostate(new_iostate); … … 138 138 rc = CL_ENOENT; 139 139 } 140 140 141 141 set_iostate(old_iostate); 142 142 -
uspace/app/bdsh/cmds/modules/cat/cat.c
r3061bc1 ra35b458 110 110 console_set_pos(console, 0, console_rows-1); 111 111 console_set_color(console, COLOR_WHITE, COLOR_BLUE, 0); 112 112 113 113 printf("ENTER/SPACE/PAGE DOWN - next page, " 114 114 "ESC/Q - quit, C - continue unpaged"); 115 115 fflush(stdout); 116 116 117 117 console_set_style(console, STYLE_NORMAL); 118 118 } … … 122 122 cons_event_t ev; 123 123 kbd_event_t *kev; 124 124 125 125 while (true) { 126 126 if (!console_get_event(console, &ev)) { … … 129 129 if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS) { 130 130 kev = &ev.ev.key; 131 131 132 132 if (kev->key == KC_ESCAPE || kev->key == KC_Q) { 133 133 should_quit = true; … … 190 190 191 191 bool reading_stdin = dash_represents_stdin && (str_cmp(fname, "-") == 0); 192 192 193 193 if (reading_stdin) { 194 194 fd = fileno(stdin); … … 201 201 } 202 202 } 203 203 204 204 if (fd < 0) { 205 205 printf("Unable to open %s\n", fname); … … 255 255 } 256 256 } 257 257 258 258 rc = vfs_read(fd, &pos, buff + copied_bytes, bytes_to_read, 259 259 &bytes); … … 283 283 paged_char(c); 284 284 } 285 285 286 286 } 287 287 count += bytes; 288 288 reads++; 289 289 } 290 290 291 291 if (reading_stdin) 292 292 fflush(stdout); … … 317 317 sysarg_t rows, cols; 318 318 errno_t rc; 319 319 320 320 /* 321 321 * reset global state … … 392 392 if (buffer < 4) 393 393 buffer = CAT_DEFAULT_BUFLEN; 394 394 395 395 if (more) { 396 396 rc = console_get_size(console, &cols, &rows); -
uspace/app/bdsh/cmds/modules/cmp/cmp.c
r3061bc1 ra35b458 120 120 unsigned int argc; 121 121 int c, opt_ind; 122 122 123 123 argc = cli_count_args(argv); 124 124 -
uspace/app/bdsh/cmds/modules/cp/cp.c
r3061bc1 ra35b458 196 196 str_cpy(src_path, src_len + 1, src); 197 197 str_rtrim(src_path, '/'); 198 198 199 199 /* Get the last component name from the src path */ 200 200 src_fname = get_last_path_component(src_path); 201 201 202 202 /* Initialize dest_path with the dest argument */ 203 203 str_cpy(dest_path, PATH_MAX, dest); … … 206 206 /* e.g. cp file_name /data */ 207 207 /* e.g. cp file_name /data/ */ 208 208 209 209 /* dest is a directory, 210 210 * append the src filename to it. -
uspace/app/bdsh/cmds/modules/kcon/kcon.c
r3061bc1 ra35b458 43 43 { 44 44 printf("`kcon' switches to the kernel debug console.\n"); 45 45 46 46 if (level != HELP_SHORT) 47 47 printf("Usage: %s\n", cmdname); 48 48 49 49 return; 50 50 } … … 54 54 { 55 55 unsigned int argc = cli_count_args(argv); 56 56 57 57 if (argc != 1) { 58 58 printf("%s - incorrect number of arguments. Try `%s --help'\n", … … 60 60 return CMD_FAILURE; 61 61 } 62 62 63 63 if (console_kcon()) 64 64 return CMD_SUCCESS; -
uspace/app/bdsh/cmds/modules/ls/ls.c
r3061bc1 ra35b458 111 111 struct dir_elem_t const *da = a; 112 112 struct dir_elem_t const *db = b; 113 113 114 114 if ((da->s.is_directory && db->s.is_file) || 115 115 ((da->s.is_directory == db->s.is_directory) && … … 141 141 struct dir_elem_t *tosort; 142 142 struct dirent *dp; 143 143 144 144 if (!dirp) 145 145 return -1; … … 150 150 return -1; 151 151 } 152 152 153 153 tosort = (struct dir_elem_t *) malloc(alloc_blocks * sizeof(*tosort)); 154 154 if (!tosort) { … … 157 157 return -1; 158 158 } 159 159 160 160 while ((dp = readdir(dirp))) { 161 161 if (nbdirs + 1 > alloc_blocks) { 162 162 alloc_blocks += alloc_blocks; 163 163 164 164 tmp = (struct dir_elem_t *) realloc(tosort, 165 165 alloc_blocks * sizeof(struct dir_elem_t)); … … 170 170 tosort = tmp; 171 171 } 172 172 173 173 /* fill the name field */ 174 174 tosort[nbdirs].name = (char *) malloc(str_size(dp->d_name) + 1); … … 189 189 } 190 190 } 191 191 192 192 if (ls.sort) 193 193 qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), ls_cmp); 194 194 195 195 for (i = 0; i < nbdirs; i++) 196 196 ls_print(&tosort[i]); … … 214 214 } 215 215 } 216 216 217 217 out: 218 218 for(i = 0; i < nbdirs; i++) … … 239 239 DIR *subdirp; 240 240 struct dir_elem_t *dir_list; 241 241 242 242 const char * const trailing_slash = "/"; 243 243 … … 294 294 } 295 295 } 296 296 297 297 ret = CMD_SUCCESS; 298 298 … … 355 355 356 356 argc = cli_count_args(argv); 357 357 358 358 for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) { 359 359 c = getopt_long(argc, argv, "hur", long_options, &opt_ind); -
uspace/app/bdsh/cmds/modules/mkfile/mkfile.c
r3061bc1 ra35b458 171 171 if (create_sparse && file_size > 0) { 172 172 const char byte = 0x00; 173 173 174 174 pos = file_size - 1; 175 175 rc = vfs_write(fd, &pos, &byte, sizeof(char), &nwritten); -
uspace/app/bdsh/cmds/modules/printf/printf.c
r3061bc1 ra35b458 55 55 } 56 56 57 57 58 58 return; 59 59 } -
uspace/app/bdsh/cmds/modules/rm/rm.c
r3061bc1 ra35b458 189 189 190 190 closedir(dirp); 191 191 192 192 return ret; 193 193 } -
uspace/app/bdsh/cmds/modules/touch/touch.c
r3061bc1 ra35b458 69 69 cmdname); 70 70 } 71 71 72 72 return; 73 73 } … … 85 85 int fd = -1; 86 86 char *buff = NULL; 87 87 88 88 DIR *dirp; 89 89 90 90 for (c = 0, optreset = 1, optind = 0, longind = 0; c != -1; ) { 91 91 c = getopt_long(argc, argv, "c", long_options, &longind); … … 96 96 } 97 97 } 98 98 99 99 if (argc - optind < 1) { 100 100 printf("%s: Incorrect number of arguments. Try `help %s extended'\n", … … 102 102 return CMD_FAILURE; 103 103 } 104 104 105 105 for (i = optind; argv[i] != NULL; i++) { 106 106 buff = str_dup(argv[i]); … … 110 110 continue; 111 111 } 112 112 113 113 dirp = opendir(buff); 114 114 if (dirp) { … … 119 119 continue; 120 120 } 121 121 122 122 /* Check whether file exists if -c (--no-create) option is given */ 123 123 if ((!no_create) || … … 128 128 } 129 129 } 130 130 131 131 if (fd < 0) { 132 132 cli_error(CL_EFAIL, "Could not update or create `%s'", buff); … … 138 138 fd = -1; 139 139 } 140 140 141 141 free(buff); 142 142 } 143 143 144 144 if (ret) 145 145 return CMD_FAILURE; -
uspace/app/bdsh/compl.c
r3061bc1 ra35b458 95 95 char *dirname = NULL; 96 96 errno_t retval; 97 97 98 98 token_t *tokens = calloc(WORD_MAX, sizeof(token_t)); 99 99 if (tokens == NULL) { … … 101 101 goto error; 102 102 } 103 103 104 104 size_t pref_size; 105 105 char *rpath_sep; … … 108 108 ssize_t current_token; 109 109 size_t tokens_length; 110 110 111 111 cs = calloc(1, sizeof(compl_t)); 112 112 if (!cs) { … … 114 114 goto error; 115 115 } 116 116 117 117 /* Convert text buffer to string */ 118 118 stext = wstr_to_astr(text); … … 121 121 goto error; 122 122 } 123 123 124 124 /* Tokenize the input string */ 125 125 retval = tok_init(&tok, stext, tokens, WORD_MAX); … … 127 127 goto error; 128 128 } 129 129 130 130 retval = tok_tokenize(&tok, &tokens_length); 131 131 if (retval != EOK) { 132 132 goto error; 133 133 } 134 134 135 135 /* Find the current token */ 136 136 for (current_token = 0; current_token < (ssize_t) tokens_length; … … 138 138 token_t *t = &tokens[current_token]; 139 139 size_t end = t->char_start + t->char_length; 140 140 141 141 /* 142 142 * Check if the caret lies inside the token or immediately … … 147 147 } 148 148 } 149 149 150 150 if (tokens_length == 0) 151 151 current_token = -1; 152 152 153 153 if ((current_token >= 0) && (tokens[current_token].type != TOKTYPE_SPACE)) 154 154 *cstart = tokens[current_token].char_start; 155 155 else 156 156 *cstart = pos; 157 157 158 158 /* 159 159 * Extract the prefix being completed … … 183 183 if ((prev_token >= 0) && (tokens[prev_token].type == TOKTYPE_SPACE)) 184 184 prev_token--; 185 185 186 186 /* 187 187 * It is a command if it is the first token or if it immediately … … 236 236 237 237 cs->prefix_len = str_length(cs->prefix); 238 238 239 239 tok_fini(&tok); 240 240 … … 244 244 error: 245 245 /* Error cleanup */ 246 246 247 247 tok_fini(&tok); 248 248 … … 258 258 if ((cs != NULL) && (cs->prefix != NULL)) 259 259 free(cs->prefix); 260 260 261 261 if (dirname != NULL) 262 262 free(dirname); 263 263 264 264 if (prefix != NULL) 265 265 free(prefix); 266 266 267 267 if (stext != NULL) 268 268 free(stext); 269 269 270 270 if (cs != NULL) 271 271 free(cs); 272 272 273 273 if (tokens != NULL) 274 274 free(tokens); -
uspace/app/bdsh/exec.c
r3061bc1 ra35b458 105 105 tmp = str_dup(find_command(cmd)); 106 106 free(found); 107 107 108 108 files[0] = io->stdin; 109 109 files[1] = io->stdout; 110 110 files[2] = io->stderr; 111 111 112 112 for (i = 0; i < 3 && files[i] != NULL; i++) { 113 113 vfs_fhandle(files[i], &file_handles[i]); … … 123 123 return 1; 124 124 } 125 125 126 126 rc = task_wait(&twait, &texit, &retval); 127 127 if (rc != EOK) { -
uspace/app/bdsh/input.c
r3061bc1 ra35b458 71 71 return ENOMEM; 72 72 token_t *tokens = tokens_buf; 73 73 74 74 char *cmd[WORD_MAX]; 75 75 errno_t rc = EOK; … … 89 89 goto finit; 90 90 } 91 91 92 92 size_t tokens_length; 93 93 rc = tok_tokenize(&tok, &tokens_length); … … 95 95 goto finit; 96 96 } 97 97 98 98 if (tokens_length > 0 && tokens[0].type == TOKTYPE_SPACE) { 99 99 tokens++; 100 100 tokens_length--; 101 101 } 102 102 103 103 if (tokens_length > 0 && tokens[tokens_length-1].type == TOKTYPE_SPACE) { 104 104 tokens_length--; 105 105 } 106 106 107 107 /* Until full support for pipes is implemented, allow for a simple case: 108 108 * [from <file> |] command [| to <file>] … … 121 121 } 122 122 } 123 123 124 124 unsigned int cmd_token_start = 0; 125 125 unsigned int cmd_token_end = tokens_length; 126 126 127 127 processed_pipes = 0; 128 128 129 129 /* Check if the first part (from <file> |) is present */ 130 130 if (pipe_count > 0 && (pipe_pos[0] == 3 || pipe_pos[0] == 4) && str_cmp(tokens[0].text, "from") == 0) { … … 134 134 processed_pipes++; 135 135 } 136 136 137 137 /* Check if the second part (| to <file>) is present */ 138 138 if ((pipe_count - processed_pipes) > 0 && … … 146 146 processed_pipes++; 147 147 } 148 148 149 149 if (processed_pipes != pipe_count) { 150 150 print_pipe_usage(); … … 152 152 goto finit; 153 153 } 154 154 155 155 /* Convert tokens of the command to string array */ 156 156 unsigned int cmd_pos = 0; … … 161 161 } 162 162 cmd[cmd_pos++] = NULL; 163 163 164 164 if (cmd[0] == NULL) { 165 165 print_pipe_usage(); … … 167 167 goto finit; 168 168 } 169 169 170 170 iostate_t new_iostate = { 171 171 .stdin = stdin, … … 173 173 .stderr = stderr 174 174 }; 175 175 176 176 FILE *from = NULL; 177 177 FILE *to = NULL; 178 178 179 179 if (redir_from) { 180 180 from = fopen(redir_from, "r"); … … 186 186 new_iostate.stdin = from; 187 187 } 188 189 188 189 190 190 if (redir_to) { 191 191 to = fopen(redir_to, "w"); … … 203 203 rc = EINVAL; 204 204 } 205 205 206 206 finit_with_files: 207 207 if (from != NULL) { … … 211 211 fclose(to); 212 212 } 213 213 214 214 finit: 215 215 if (NULL != usr->line) { … … 230 230 printf("from filename | command ... | to filename\n"); 231 231 printf("command ... | to filename\n"); 232 232 233 233 } 234 234 … … 236 236 { 237 237 int id = 0; 238 238 239 239 /* We have rubbish */ 240 240 if (NULL == cmd[0]) { 241 241 return CL_ENOENT; 242 242 } 243 243 244 244 /* Is it a builtin command ? */ 245 245 if ((id = (is_builtin(cmd[0]))) > -1) { 246 246 return run_builtin(id, cmd, usr, new_iostate); 247 247 } 248 248 249 249 /* Is it a module ? */ 250 250 if ((id = (is_module(cmd[0]))) > -1) { … … 260 260 char *str; 261 261 errno_t rc; 262 262 263 263 tinput_set_prompt(tinput, usr->prompt); 264 264 -
uspace/app/bdsh/scli.c
r3061bc1 ra35b458 99 99 { 100 100 errno_t ret = 0; 101 101 102 102 stdiostate.stdin = stdin; 103 103 stdiostate.stdout = stdout; -
uspace/app/bdsh/tok.c
r3061bc1 ra35b458 59 59 tok->in_char_offset = 0; 60 60 tok->last_in_char_offset = 0; 61 61 62 62 tok->outtok = out_tokens; 63 63 tok->outtok_offset = 0; 64 64 tok->outtok_size = max_tokens; 65 65 66 66 /* Prepare a buffer where all the token strings will be stored */ 67 67 size_t len = str_size(input) + max_tokens + 1; 68 68 char *tmp = malloc(len); 69 69 70 70 if (tmp == NULL) { 71 71 return ENOMEM; 72 72 } 73 73 74 74 tok->outbuf = tmp; 75 75 tok->outbuf_offset = 0; 76 76 tok->outbuf_size = len; 77 77 tok->outbuf_last_start = 0; 78 78 79 79 return EOK; 80 80 } … … 93 93 errno_t rc; 94 94 wchar_t next_char; 95 95 96 96 /* Read the input line char by char and append tokens */ 97 97 while ((next_char = tok_look_char(tok)) != 0) { … … 113 113 } 114 114 tok_push_token(tok); 115 115 116 116 } 117 117 else if (next_char == '|') { … … 125 125 } 126 126 } 127 127 128 128 tok_start_token(tok, TOKTYPE_PIPE); 129 129 130 130 rc = tok_push_char(tok, tok_get_char(tok)); 131 131 if (rc != EOK) { 132 132 return rc; 133 133 } 134 134 135 135 rc = tok_push_token(tok); 136 136 if (rc != EOK) { … … 163 163 } 164 164 } 165 165 166 166 /* Push the last token */ 167 167 if (tok_pending_chars(tok)) { … … 171 171 } 172 172 } 173 173 174 174 *tokens_length = tok->outtok_offset; 175 175 176 176 return EOK; 177 177 } … … 182 182 errno_t rc; 183 183 wchar_t next_char; 184 184 185 185 while ((next_char = tok_look_char(tok)) != 0) { 186 186 if (next_char == '\'') { … … 193 193 return rc; 194 194 } 195 195 196 196 /* Swallow the additional one in the input */ 197 197 tok_get_char(tok); … … 209 209 } 210 210 } 211 211 212 212 /* If we are here, the string run to the end without being closed */ 213 213 return EINVAL; … … 249 249 return EOVERFLOW; 250 250 } 251 251 252 252 if (tok->outbuf_offset >= tok->outbuf_size) { 253 253 return EOVERFLOW; 254 254 } 255 255 256 256 tok->outbuf[tok->outbuf_offset++] = 0; 257 257 token_t *tokinfo = &tok->outtok[tok->outtok_offset++]; … … 263 263 tokinfo->char_length = tok->in_char_offset - tok->last_in_char_offset; 264 264 tok->outbuf_last_start = tok->outbuf_offset; 265 265 266 266 /* We have consumed the first char of the next token already */ 267 267 tok->last_in_offset = tok->in_offset; 268 268 tok->last_in_char_offset = tok->in_char_offset; 269 269 270 270 return EOK; 271 271 } -
uspace/app/bdsh/tok.h
r3061bc1 ra35b458 51 51 size_t in_char_offset; 52 52 size_t last_in_char_offset; 53 53 54 54 char *outbuf; 55 55 size_t outbuf_offset; 56 56 size_t outbuf_size; 57 57 size_t outbuf_last_start; 58 58 59 59 token_t *outtok; 60 60 token_type_t current_type;
Note:
See TracChangeset
for help on using the changeset viewer.