123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- /* Copyright Yann Weber <asmsh@yannweb.net>
- This file is part of asmsh.
-
- asmsh is free software: you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation, either version 3 of the License, or any later version.
-
- asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- details.
-
- You should have received a copy of the GNU General Public License along
- with asmsh. If not, see <https://www.gnu.org/licenses/>.
- */
- #include "shell.h"
-
- int asmsh_init(asmsh_t *sh, const char *child_path)
- {
- bzero(sh, sizeof(asmsh_t));
- if(!(sh->env = asmsh_env(child_path)))
- {
- perror("Unable to start child env");
- asmsh_log_fatal("Unable to start child env");
- goto err;
- }
- if(!(sh->cctx = asmsh_asmc_ctx_default()))
- {
- perror("Unable to start compilation ctx");
- asmsh_log_fatal("Unable to start compilation context");
- asmsh_env_free(sh->env);
- goto err;
- }
- if(child_path)
- {
- if(!(sh->child_path = strdup(child_path)))
- {
- perror("Unable to copy child path");
- goto err_all;
- }
- }
- else
- {
- sh->child_path = NULL;
- }
- return 0;
-
- err_all:
- asmsh_asmc_ctx_free(sh->cctx);
- asmsh_env_free(sh->env);
- err:
- bzero(sh, sizeof(asmsh_t));
- return -1;
- }
-
-
- void asmsh_cleanup(asmsh_t *sh)
- {
- if(!sh){ return; }
- if(sh->cctx)
- {
- asmsh_asmc_ctx_free(sh->cctx);
- }
- if(sh->env)
- {
- asmsh_env_free(sh->env);
- }
- if(sh->child_path)
- {
- free(sh->child_path);
- }
- if(sh->last_instr)
- {
- free(sh->last_instr);
- }
- bzero(sh, sizeof(asmsh_t));
- }
-
- /** Attempt to compile and step if sucess */
- static int _compile_step(asmsh_t *sh, const char *cmd);
- /** Attempt to handle given shell internal command (starting with '.') */
- static int _handle_command(asmsh_t *sh, const char *cmd);
- int asmsh_exec(asmsh_t *sh, const char *cmd)
- {
- if(!cmd)
- {
- errno=EINVAL;
- return -1;
- }
-
- int ret;
- //lstrip whitespace
- for(; *cmd && (*cmd == ' ' || *cmd == '\t'); cmd++);
- if(!*cmd) { return 0; }
-
- switch(*cmd)
- {
- case '.':
- ret = _handle_command(sh, cmd);
- break;
- default:
- ret = _compile_step(sh, cmd);
- break;
- }
- if(!ret)
- {
- asmsh_env_update_regs(sh->env);
- }
- return ret;
- }
-
- static int _compile_step(asmsh_t *sh, const char *cmd)
- {
- int ret, status;
- asmsh_bytecode_t bcode;
-
- if(asmsh_asmc_compile(sh->cctx, cmd, &bcode) < 0)
- {
- return -1;
- }
- if(!bcode.size)
- {
- asmsh_log_error("No bytecode returned...");
- return -1;
- }
- sh->last_bcode = bcode;
- if(sh->last_instr) { free(sh->last_instr); }
- sh->last_instr = strdup(cmd);
-
- //char buf[256];
- //int _ret;
- //_ret = snprintf(buf, 255, "'%s' = 0x", cmd);
- //for(int _i=0; _i<bcode.size; _i++)
- //{
- // _ret += snprintf(buf+_ret, 255-_ret, "%02x", bcode.bytes[_i]);
- //}
- //_ret += snprintf(buf+_ret, 255-_ret, "(%d)", bcode.size);
- //buf[_ret] = '\0';
- //asmsh_log_info(buf);
-
- if(asmsh_env_write_code(sh->env, &bcode))
- {
- return -1;
- }
-
- status=0;
- ret = asmsh_env_step(sh->env, &status);
- if(ret < 0)
- {
- perror("Unable to step");
- asmsh_log_fatal("Error will child step");
- return -1;
- }
- else if(ret > 0)
- {
- return 1+WEXITSTATUS(status);
- }
- return 0;
- }
-
- static int _handle_command(asmsh_t *sh, const char *cmd)
- {
- const asmsh_cmd_t *match;
- int ret, err;
-
- asmsh_cmd_args_t *args;
-
- args = asmsh_cmd_parse(cmd);
- if(!args)
- {
- err=errno;
- asmsh_log_error("Parse error"); // TODO better error
- errno=err;
- return -1;
- }
-
- match = asmsh_cmd_match(args->cmd, strlen(args->cmd));
- if(!match)
- {
- err=errno;
- asmsh_log_error("Unknow command");
- asmsh_cmd_args_free(args);
- errno=err;
- return -1;
- }
- ret = match->cmd(sh, args);
- asmsh_cmd_args_free(args);
- return ret;
- }
|