mirror of
https://github.com/zoriya/ash.git
synced 2026-05-30 09:38:51 +00:00
Merge branch 'master' of github.com:AnonymusRaccoon/ash into localvars
This commit is contained in:
@@ -24,7 +24,7 @@ typedef struct binding
|
||||
extern const key_function_t key_functions[];
|
||||
extern const binding_t emacs_bindings[];
|
||||
|
||||
int get_emacs_bindings_size();
|
||||
int get_emacs_bindings_size(void);
|
||||
|
||||
int buffer_get_display_pos(buffer_t *buffer);
|
||||
|
||||
|
||||
@@ -43,7 +43,8 @@ static binding_t *get_binding(const char *c, env_t *env)
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static void new_binding(const char *c, int (*func)(int, buffer_t *, env_t *), env_t *env)
|
||||
static void new_binding(const char *c, int (*func)(int, buffer_t *, env_t *)\
|
||||
, env_t *env)
|
||||
{
|
||||
int key = my_parsechar(c);
|
||||
int size = 0;
|
||||
|
||||
@@ -8,11 +8,13 @@
|
||||
#include "shell.h"
|
||||
#include "builtin.h"
|
||||
#include "redirections.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <malloc.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int run_builtin(const builtin *cmd, char **a, redirection *inout[2], env_t *env)
|
||||
{
|
||||
@@ -63,9 +65,30 @@ int builtin_cd(char **argv, env_t *env)
|
||||
return (0);
|
||||
}
|
||||
|
||||
bool my_strisnum(char *str)
|
||||
{
|
||||
for (int i = 0; str[i]; i++) {
|
||||
if (!isdigit(str[i]))
|
||||
return (false);
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
int builtin_exit(char **argv, env_t *env)
|
||||
{
|
||||
if (argv[1])
|
||||
env->vars = my_setenv(env->vars, "?", "1");
|
||||
char *ptr = argv[1];
|
||||
|
||||
if (!ptr) {
|
||||
env->vars = my_setenv(env->vars, "?", "0");
|
||||
return (-1);
|
||||
}
|
||||
if (argv[1][0] == '-') {
|
||||
ptr = &ptr[1];
|
||||
}
|
||||
if (!my_strisnum(ptr)) {
|
||||
dprintf(2, "exit: Expression Syntax.\n");
|
||||
return (0);
|
||||
}
|
||||
env->vars = my_setenv(env->vars, "?", argv[1]);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ int delete_char_command(int key, buffer_t *buffer, env_t *env)
|
||||
|
||||
int newline_command(int key, buffer_t *buffer, env_t *env)
|
||||
{
|
||||
int ret;
|
||||
int ret = 0;
|
||||
|
||||
env->vars = my_unsetenv(env->vars, "eof");
|
||||
if (env->window)
|
||||
|
||||
@@ -48,7 +48,7 @@ const binding_t emacs_bindings[] = {
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
int get_emacs_bindings_size()
|
||||
int get_emacs_bindings_size(void)
|
||||
{
|
||||
return sizeof(emacs_bindings);
|
||||
}
|
||||
@@ -21,7 +21,10 @@ char *get_var_value(char *var, env_t *env)
|
||||
value = my_getenv(env->vars, var);
|
||||
if (value)
|
||||
return (value);
|
||||
printf("%s: Undefined variable.\n", var);
|
||||
if (!var[0])
|
||||
printf("Illegal variable name.\n");
|
||||
else
|
||||
printf("%s: Undefined variable.\n", var);
|
||||
env->vars = my_setenv(env->vars, "?", "1");
|
||||
return (NULL);
|
||||
}
|
||||
@@ -31,10 +34,12 @@ int get_var_name(char *ptr, char **name)
|
||||
int length = 0;
|
||||
|
||||
for (int i = 1; ptr[i] && ptr[i] != ' ' && ptr[i] != '$'; i++) {
|
||||
if (ptr[i] == '"')
|
||||
break;
|
||||
length++;
|
||||
}
|
||||
*name = strndup(&ptr[1], length);
|
||||
return (length + 1);
|
||||
return (length - 1);
|
||||
}
|
||||
|
||||
void rm_n_char(char *ptr, int n)
|
||||
@@ -51,22 +56,22 @@ char *process_vars(char *cmd, env_t *env)
|
||||
{
|
||||
char *name = NULL;
|
||||
char *value;
|
||||
int new_index;
|
||||
int new_index = 0;
|
||||
int length = strlen(cmd);
|
||||
bool parse = true;
|
||||
|
||||
for (int i = 0; i < length; i++, length = strlen(cmd)) {
|
||||
if (cmd[i] != '$')
|
||||
for (int i = 0; i < length; i += 1 + new_index, length = strlen(cmd)) {
|
||||
if (cmd[i] == '\'' || cmd[i] == '`')
|
||||
parse = !parse;
|
||||
if (cmd[i] != '$' || !parse)
|
||||
continue;
|
||||
new_index = get_var_name(&cmd[i], &name);
|
||||
value = get_var_value(name, env);
|
||||
if (!value)
|
||||
if (!(value = get_var_value(name, env)))
|
||||
return (NULL);
|
||||
cmd = realloc(cmd, sizeof(char) * (strlen(value) + length + 2));
|
||||
if (!cmd)
|
||||
if (!(cmd = realloc(cmd, sizeof(char) * (strlen(value) + length + 2))))
|
||||
return (NULL);
|
||||
rm_n_char(&cmd[i], strlen(name) + 1);
|
||||
insert_substring(cmd, value, i + 1);
|
||||
i += new_index;
|
||||
free(name);
|
||||
}
|
||||
return (cmd);
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ int prompt_run(char *cmd, redirection *inout[2], env_t *env, redirection *cmds)
|
||||
char **argv = parse_input(cmd, env, &parser);
|
||||
int ret = -2;
|
||||
|
||||
if (!argv)
|
||||
if (!argv || !argv[0])
|
||||
return (0);
|
||||
if (**argv == '!' && argv[0][1] && argv[0][1] != ' ')
|
||||
ret = run_builtin(&builtins[5], argv, inout, env);
|
||||
|
||||
@@ -81,8 +81,10 @@ int run_with_redirections(char *cmd, env_t *env, redirection *input)
|
||||
|
||||
bool command_format_is_invalid(char **cmds, env_t *env, int *return_values)
|
||||
{
|
||||
if (cmds[0] && !cmds[0][count_trailing_spaces(cmds[0])] && !cmds[1])
|
||||
return (false);
|
||||
for (int i = 0; cmds[i]; i++) {
|
||||
if (!cmds[i] || !cmds[i][count_trailing_spaces(cmds[i])]
|
||||
if (!cmds[i] || !cmds[i][count_trailing_spaces(cmds[i])]
|
||||
|| split_is_invalid(cmds, return_values, i)) {
|
||||
dprintf(2, "Invalid null command.\n");
|
||||
env->vars = my_setenv(env->vars, "?", "1");
|
||||
|
||||
@@ -37,7 +37,7 @@ static int get_splitted_count(char *str, char **delims)
|
||||
delim = get_delimiter(str + i, delims);
|
||||
if (delim) {
|
||||
count++;
|
||||
i += strlen(delim);
|
||||
i += strlen(delim) - 1;
|
||||
}
|
||||
}
|
||||
return (count);
|
||||
|
||||
Reference in New Issue
Block a user