Remove colors and und use default color, and use better codestyle
This commit is contained in:
parent
05f2a0e6e3
commit
1f0d5f5ea1
8 changed files with 521 additions and 523 deletions
|
@ -1,5 +1,7 @@
|
||||||
#ifndef RUSH_H_
|
#ifndef S_H_
|
||||||
#define RUSH_H_
|
#define S_H_
|
||||||
|
|
||||||
|
#define VERSION "1.0.0"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
#ifndef COLOR_H_
|
|
||||||
#define COLOR_H_
|
|
||||||
|
|
||||||
const char *lavender = "174;174;255";
|
|
||||||
const char *pink = "255;210;239";
|
|
||||||
const char *blue = "137;180;250";
|
|
||||||
|
|
||||||
char *color_text(char str[], const char *color);
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,7 +1,6 @@
|
||||||
#ifndef COMMANDS_H_
|
#ifndef COMMANDS_H_
|
||||||
#define COMMANDS_H_
|
#define COMMANDS_H_
|
||||||
|
|
||||||
char *replace_absolute_home(char *str);
|
|
||||||
int num_builtins(void);
|
int num_builtins(void);
|
||||||
bool is_builtin(char *command);
|
bool is_builtin(char *command);
|
||||||
int execute(char **args, int fd, int options);
|
int execute(char **args, int fd, int options);
|
||||||
|
|
113
src/90s.c
113
src/90s.c
|
@ -9,12 +9,12 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#include "color.h"
|
|
||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
#include "history.h"
|
#include "history.h"
|
||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
|
|
||||||
void *memalloc(size_t size) {
|
void *memalloc(size_t size)
|
||||||
|
{
|
||||||
void *ptr = malloc(size);
|
void *ptr = malloc(size);
|
||||||
if (!ptr) {
|
if (!ptr) {
|
||||||
fputs("90s: Error allocating memory\n", stderr);
|
fputs("90s: Error allocating memory\n", stderr);
|
||||||
|
@ -23,7 +23,8 @@ void *memalloc(size_t size) {
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void change_terminal_attribute(int option) {
|
void change_terminal_attribute(int option)
|
||||||
|
{
|
||||||
static struct termios oldt, newt;
|
static struct termios oldt, newt;
|
||||||
tcgetattr(STDIN_FILENO, &oldt);
|
tcgetattr(STDIN_FILENO, &oldt);
|
||||||
if (option) {
|
if (option) {
|
||||||
|
@ -35,14 +36,15 @@ void change_terminal_attribute(int option) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char **setup_path_variable(void) {
|
char **setup_path_variable(void)
|
||||||
|
{
|
||||||
char *envpath = getenv("PATH");
|
char *envpath = getenv("PATH");
|
||||||
if (envpath == NULL) {
|
if (envpath == NULL) {
|
||||||
fprintf(stderr, "90s: PATH environment variable is missing\n");
|
fprintf(stderr, "90s: PATH environment variable is missing\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
char *path_cpy = memalloc(sizeof(char) * (strlen(envpath) + 1));
|
char *path_cpy = memalloc(strlen(envpath) + 1);
|
||||||
char *path = memalloc(sizeof(char) * (strlen(envpath) + 1));
|
char *path = memalloc(strlen(envpath) + 1);
|
||||||
strcpy(path_cpy, envpath);
|
strcpy(path_cpy, envpath);
|
||||||
strcpy(path, envpath);
|
strcpy(path, envpath);
|
||||||
int path_count = 0;
|
int path_count = 0;
|
||||||
|
@ -66,7 +68,8 @@ char **setup_path_variable(void) {
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool find_command(char **paths, char *command) {
|
bool find_command(char **paths, char *command)
|
||||||
|
{
|
||||||
if (strncmp(command, "", 1) == 0) {
|
if (strncmp(command, "", 1) == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -87,19 +90,23 @@ bool find_command(char **paths, char *command) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void shiftleft(int chars) {
|
void shiftleft(int chars)
|
||||||
|
{
|
||||||
printf("\033[%dD", chars);
|
printf("\033[%dD", chars);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shiftright(int chars) {
|
void shiftright(int chars)
|
||||||
|
{
|
||||||
printf("\033[%dC", chars);
|
printf("\033[%dC", chars);
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearline(void) {
|
void clearline(void)
|
||||||
|
{
|
||||||
printf("\033[K"); // clear line to the right of cursor
|
printf("\033[K"); // clear line to the right of cursor
|
||||||
}
|
}
|
||||||
|
|
||||||
void highlight(char *buffer, char **paths) {
|
void highlight(char *buffer, char **paths)
|
||||||
|
{
|
||||||
char *cmd_part = strchr(buffer, ' ');
|
char *cmd_part = strchr(buffer, ' ');
|
||||||
char *command_without_arg = NULL;
|
char *command_without_arg = NULL;
|
||||||
int cmd_len = 0;
|
int cmd_len = 0;
|
||||||
|
@ -107,8 +114,8 @@ void highlight(char *buffer, char **paths) {
|
||||||
|
|
||||||
if (cmd_part != NULL) {
|
if (cmd_part != NULL) {
|
||||||
cmd_len = cmd_part - buffer;
|
cmd_len = cmd_part - buffer;
|
||||||
char *cmd = memalloc(sizeof(char) * (cmd_len + 1));
|
char *cmd = memalloc(cmd_len + 1);
|
||||||
command_without_arg = memalloc(sizeof(char) * (cmd_len + 1));
|
command_without_arg = memalloc(cmd_len + 1);
|
||||||
for (int i = 0; i < (cmd_part - buffer); i++) {
|
for (int i = 0; i < (cmd_part - buffer); i++) {
|
||||||
cmd[i] = buffer[i];
|
cmd[i] = buffer[i];
|
||||||
}
|
}
|
||||||
|
@ -124,28 +131,29 @@ void highlight(char *buffer, char **paths) {
|
||||||
if (valid) {
|
if (valid) {
|
||||||
if (command_without_arg != NULL) {
|
if (command_without_arg != NULL) {
|
||||||
buffer += cmd_len;
|
buffer += cmd_len;
|
||||||
printf("\x1b[38;2;166;227;161m%s\x1b[0m\x1b[38;2;255;255;255m%s\x1b[0m", command_without_arg, buffer); // print green as valid command, but only color the command, not the arguments
|
printf("\x1b[32m%s\x1b[37m%s\x1b[m", command_without_arg, buffer); // print green as valid command, but only color the command, not the arguments
|
||||||
buffer -= cmd_len;
|
buffer -= cmd_len;
|
||||||
} else {
|
} else {
|
||||||
printf("\x1b[38;2;166;227;161m%s\x1b[0m", buffer); // print green as valid command
|
printf("\x1b[32m%s\x1b[m", buffer); // print green as valid command
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (command_without_arg != NULL) {
|
if (command_without_arg != NULL) {
|
||||||
buffer += cmd_len;
|
buffer += cmd_len;
|
||||||
printf("\x1b[38;2;243;139;168m%s\x1b[0m\x1b[38;2;255;255;255m%s\x1b[0m", command_without_arg, buffer); // print red as invalid command, but only color the command, not the arguments
|
printf("\x1b[31m%s\x1b[37m%s\x1b[m", command_without_arg, buffer); // print red as invalid command, but only color the command, not the arguments
|
||||||
buffer -= cmd_len;
|
buffer -= cmd_len;
|
||||||
} else {
|
} else {
|
||||||
printf("\x1b[38;2;243;139;168m%s\x1b[0m", buffer); // print red as invalid command
|
printf("\x1b[31m%s\x1b[m", buffer); // print red as invalid command
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
free(command_without_arg);
|
free(command_without_arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *readline(char **paths) {
|
char *readline(char **paths)
|
||||||
|
{
|
||||||
int bufsize = RL_BUFSIZE;
|
int bufsize = RL_BUFSIZE;
|
||||||
int position = 0;
|
int position = 0;
|
||||||
char *buffer = memalloc(sizeof(char) * bufsize);
|
char *buffer = memalloc(bufsize);
|
||||||
|
|
||||||
bool moved = false;
|
bool moved = false;
|
||||||
bool backspaced = false;
|
bool backspaced = false;
|
||||||
|
@ -344,9 +352,10 @@ char *readline(char **paths) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// split line into arguments
|
// split line into arguments
|
||||||
char **argsplit(char *line) {
|
char **argsplit(char *line)
|
||||||
|
{
|
||||||
int bufsize = TOK_BUFSIZE, position = 0;
|
int bufsize = TOK_BUFSIZE, position = 0;
|
||||||
char **tokens = memalloc(bufsize * sizeof(char*));
|
char **tokens = memalloc(sizeof(char *) * bufsize);
|
||||||
char *token;
|
char *token;
|
||||||
|
|
||||||
token = strtok(line, TOK_DELIM);
|
token = strtok(line, TOK_DELIM);
|
||||||
|
@ -356,7 +365,7 @@ char **argsplit(char *line) {
|
||||||
|
|
||||||
if (position >= bufsize) {
|
if (position >= bufsize) {
|
||||||
bufsize += TOK_BUFSIZE;
|
bufsize += TOK_BUFSIZE;
|
||||||
tokens = realloc(tokens, bufsize * sizeof(char*));
|
tokens = realloc(tokens, sizeof(char *) * bufsize);
|
||||||
if (!tokens) {
|
if (!tokens) {
|
||||||
fprintf(stderr, "90s: Error allocating memory\n");
|
fprintf(stderr, "90s: Error allocating memory\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
@ -369,7 +378,8 @@ char **argsplit(char *line) {
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
char **modifyargs(char **args) {
|
char **modifyargs(char **args)
|
||||||
|
{
|
||||||
int num_arg = 0;
|
int num_arg = 0;
|
||||||
|
|
||||||
// check if command is ls, diff, or grep, if so, add --color=auto to the arguments
|
// check if command is ls, diff, or grep, if so, add --color=auto to the arguments
|
||||||
|
@ -392,7 +402,8 @@ char **modifyargs(char **args) {
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *trimws(char *str) {
|
char *trimws(char *str)
|
||||||
|
{
|
||||||
char *end;
|
char *end;
|
||||||
while (isspace((unsigned char) *str))
|
while (isspace((unsigned char) *str))
|
||||||
str++;
|
str++;
|
||||||
|
@ -405,7 +416,8 @@ char *trimws(char *str) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
char ***pipe_argsplit(char *line) {
|
char ***pipe_argsplit(char *line)
|
||||||
|
{
|
||||||
char ***cmdv = memalloc(sizeof(char **) * 128); // 127 commands, 1 for NULL
|
char ***cmdv = memalloc(sizeof(char **) * 128); // 127 commands, 1 for NULL
|
||||||
char **cmds = memalloc(sizeof(char *) * 128); // 127 arguments, 1 for NULL
|
char **cmds = memalloc(sizeof(char *) * 128); // 127 arguments, 1 for NULL
|
||||||
int num_arg = 0;
|
int num_arg = 0;
|
||||||
|
@ -429,34 +441,42 @@ char ***pipe_argsplit(char *line) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// continously prompt for command and execute it
|
// continously prompt for command and execute it
|
||||||
void command_loop(char **paths) {
|
void command_loop(char **paths)
|
||||||
|
{
|
||||||
char *line;
|
char *line;
|
||||||
char **args;
|
char **args;
|
||||||
int status = 1;
|
int status = 1;
|
||||||
|
|
||||||
while (status) {
|
while (status) {
|
||||||
|
/* Get current time */
|
||||||
time_t t = time(NULL);
|
time_t t = time(NULL);
|
||||||
struct tm* current_time = localtime(&t); // get current time
|
struct tm *current_time = localtime(&t);
|
||||||
char timestr[256];
|
char timestr[256];
|
||||||
char cwdstr[PATH_MAX];
|
/* Format time string */
|
||||||
if (strftime(timestr, sizeof(timestr), "[%H:%M:%S]", current_time) == 0) { // format time string
|
if (strftime(timestr, sizeof(timestr), "[%H:%M:%S]", current_time) == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (getcwd(cwdstr, sizeof(cwdstr)) == NULL) { // get current working directory
|
char cwd[PATH_MAX];
|
||||||
|
/* Get current working directory */
|
||||||
|
if (getcwd(cwd, PATH_MAX) == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
char time[256];
|
char *home = getenv("HOME");
|
||||||
strcpy(time, timestr);
|
size_t home_len = strlen(home);
|
||||||
char *modtime = memalloc(sizeof(char) * 256);
|
|
||||||
modtime = color_text(time, lavender); // lavender colored time string
|
int i = 0, j = 0;
|
||||||
char *cwd = memalloc(sizeof(char) * (PATH_MAX + 2));
|
/* Check if cwd starts with home */
|
||||||
sprintf(cwd, "[%s]", cwdstr);
|
if (home && strncmp(cwd, home, home_len) == 0) {
|
||||||
cwd = replace_absolute_home(cwd);
|
cwd[j++] = '~';
|
||||||
cwd = color_text(cwd, pink); // pink colored current directory
|
i += home_len;
|
||||||
char *arrow = memalloc(sizeof(char) * 32);
|
}
|
||||||
strcpy(arrow, "»");
|
while (cwd[i] != '\0') {
|
||||||
arrow = color_text(arrow, blue);
|
cwd[j++] = cwd[i++];
|
||||||
printf("%s %s %s ", modtime, cwd, arrow);
|
}
|
||||||
|
cwd[j] = '\0';
|
||||||
|
|
||||||
|
/* Blue time string, pink time, teal arrow */
|
||||||
|
printf("\033[34m%s\033[m \033[35m[%s] \033[36m>\033[m ", timestr, cwd);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
cmd_count = 0; // upward arrow key resets command count
|
cmd_count = 0; // upward arrow key resets command count
|
||||||
|
@ -487,17 +507,16 @@ void command_loop(char **paths) {
|
||||||
free(args);
|
free(args);
|
||||||
}
|
}
|
||||||
free(line);
|
free(line);
|
||||||
free(modtime);
|
|
||||||
free(cwd);
|
|
||||||
free(arrow);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void quit_sig(int sig) {
|
void quit_sig(int sig)
|
||||||
|
{
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
// setup
|
// setup
|
||||||
signal(SIGINT, quit_sig);
|
signal(SIGINT, quit_sig);
|
||||||
signal(SIGTERM, quit_sig);
|
signal(SIGTERM, quit_sig);
|
||||||
|
|
18
src/color.c
18
src/color.c
|
@ -1,18 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "90s.h"
|
|
||||||
|
|
||||||
// color str in place
|
|
||||||
char *color_text(char *str, const char *color) {
|
|
||||||
int size = snprintf(NULL, 0, "\x1b[38;2;%sm%s\x1b[0m", color, str) + 1; // calculate size that is needed for colored string
|
|
||||||
if (size < 0) {
|
|
||||||
fprintf(stderr, "90s: snprintf failed\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
char *buf = memalloc(size);
|
|
||||||
|
|
||||||
snprintf(buf, size, "\x1b[38;2;%sm%s\x1b[0m", color, str); // format string to buf
|
|
||||||
return buf;
|
|
||||||
}
|
|
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
int execute(char **args);
|
int execute(char **args);
|
||||||
|
|
||||||
// Function declarations for builtin commands
|
/* Builtin commands */
|
||||||
int cd(char **args);
|
int cd(char **args);
|
||||||
int help(char **args);
|
int help(char **args);
|
||||||
int quit(char **args);
|
int quit(char **args);
|
||||||
|
@ -23,7 +23,6 @@ int source(char **args);
|
||||||
int j(char **args);
|
int j(char **args);
|
||||||
int bg(char **args);
|
int bg(char **args);
|
||||||
|
|
||||||
// List of builtin commands' names
|
|
||||||
char *builtin_cmds[] = {
|
char *builtin_cmds[] = {
|
||||||
"cd",
|
"cd",
|
||||||
"help",
|
"help",
|
||||||
|
@ -38,7 +37,7 @@ char *builtin_cmds[] = {
|
||||||
int (*builtin_func[]) (char **) = {
|
int (*builtin_func[]) (char **) = {
|
||||||
&cd,
|
&cd,
|
||||||
&help,
|
&help,
|
||||||
&quit, // cant name it exit as it is taken
|
&quit, /* Can't name it exit as it is taken */
|
||||||
&history,
|
&history,
|
||||||
&export,
|
&export,
|
||||||
&source,
|
&source,
|
||||||
|
@ -53,12 +52,13 @@ char *shortcut_dirs[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
char *shortcut_expand_dirs[] = {
|
char *shortcut_expand_dirs[] = {
|
||||||
"~/.nky/Coding/C/90s",
|
"~/.nky/git/90s",
|
||||||
"~/.local/bin",
|
"~/.local/bin",
|
||||||
"/usr/local/bin",
|
"/usr/local/bin",
|
||||||
};
|
};
|
||||||
|
|
||||||
char *gethome(void) {
|
char *gethome(void)
|
||||||
|
{
|
||||||
char *home = getenv("HOME");
|
char *home = getenv("HOME");
|
||||||
if (home == NULL) {
|
if (home == NULL) {
|
||||||
fprintf(stderr, "Error: HOME environment variable not set.\n");
|
fprintf(stderr, "Error: HOME environment variable not set.\n");
|
||||||
|
@ -67,14 +67,15 @@ char *gethome(void) {
|
||||||
return home;
|
return home;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *replace_home_dir(char *str) {
|
char *replace_home_dir(char *str)
|
||||||
|
{
|
||||||
char *home_path = gethome();
|
char *home_path = gethome();
|
||||||
|
|
||||||
int path_len = strlen(str);
|
int path_len = strlen(str);
|
||||||
int home_len = strlen(home_path);
|
int home_len = strlen(home_path);
|
||||||
|
|
||||||
// Allocate memory for the new path
|
// Allocate memory for the new path
|
||||||
char* new_path = memalloc(sizeof(char) * (path_len + home_len + 1));
|
char* new_path = memalloc(path_len + home_len + 1);
|
||||||
|
|
||||||
int i = 0, j = 0;
|
int i = 0, j = 0;
|
||||||
while (str[i] != '\0') {
|
while (str[i] != '\0') {
|
||||||
|
@ -93,37 +94,14 @@ char *replace_home_dir(char *str) {
|
||||||
return new_path;
|
return new_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *replace_absolute_home(char *str) {
|
|
||||||
char *home_path = gethome();
|
|
||||||
|
|
||||||
int path_len = strlen(str);
|
|
||||||
int home_len = strlen(home_path);
|
|
||||||
|
|
||||||
// Allocate memory for the new path
|
|
||||||
char* new_path = memalloc(sizeof(char) * (path_len - home_len + 2));
|
|
||||||
|
|
||||||
int i = 0, j = 0;
|
|
||||||
while (str[i] != '\0') {
|
|
||||||
if (strncmp(&str[i], home_path, home_len) == 0) {
|
|
||||||
// Copy HOME environment variable value
|
|
||||||
new_path[j++] = '~';
|
|
||||||
i += home_len;
|
|
||||||
} else {
|
|
||||||
new_path[j++] = str[i++];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
new_path[j] = '\0';
|
|
||||||
return new_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
// number of built in commands
|
// number of built in commands
|
||||||
int num_builtins(void) {
|
int num_builtins(void) {
|
||||||
return sizeof(builtin_cmds) / sizeof(char *);
|
return sizeof(builtin_cmds) / sizeof(char *);
|
||||||
}
|
}
|
||||||
|
|
||||||
// autojump
|
// autojump
|
||||||
int j(char **args) {
|
int j(char **args)
|
||||||
|
{
|
||||||
if (args[1] == NULL) {
|
if (args[1] == NULL) {
|
||||||
fprintf(stderr, "90s: not enough arguments\n");
|
fprintf(stderr, "90s: not enough arguments\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -143,7 +121,9 @@ int j(char **args) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// change directory
|
/*
|
||||||
|
* Change directory
|
||||||
|
*/
|
||||||
int cd(char **args) {
|
int cd(char **args) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
if (args[1] == NULL) {
|
if (args[1] == NULL) {
|
||||||
|
@ -166,9 +146,12 @@ int cd(char **args) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// show help menu
|
/*
|
||||||
int help(char **args) {
|
* Show help menu
|
||||||
printf("90s %f\n", VERSION);
|
*/
|
||||||
|
int help(char **args)
|
||||||
|
{
|
||||||
|
printf("90s %s\n", VERSION);
|
||||||
printf("Built in commands:\n");
|
printf("Built in commands:\n");
|
||||||
|
|
||||||
for (int i = 0; i < num_builtins(); i++) {
|
for (int i = 0; i < num_builtins(); i++) {
|
||||||
|
@ -180,11 +163,14 @@ int help(char **args) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int quit(char **args) {
|
int quit(char **args)
|
||||||
return 0; // exit prompting loop, which also the shell
|
{
|
||||||
|
/* Exit prompt loop */
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int history(char **args) {
|
int history(char **args)
|
||||||
|
{
|
||||||
char **history = get_all_history(true);
|
char **history = get_all_history(true);
|
||||||
|
|
||||||
for (int i = 0; history[i] != NULL; ++i) {
|
for (int i = 0; history[i] != NULL; ++i) {
|
||||||
|
@ -196,8 +182,10 @@ int history(char **args) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int export(char **args) {
|
int export(char **args)
|
||||||
args++; // skip the command
|
{
|
||||||
|
/* Skip the command */
|
||||||
|
args++;
|
||||||
while (*args != NULL) {
|
while (*args != NULL) {
|
||||||
char *variable = strtok(*args, "=\n");
|
char *variable = strtok(*args, "=\n");
|
||||||
char *value = strtok(NULL, "=\n");
|
char *value = strtok(NULL, "=\n");
|
||||||
|
@ -215,7 +203,8 @@ int export(char **args) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int source(char **args) {
|
int source(char **args)
|
||||||
|
{
|
||||||
if (args[1] == NULL) {
|
if (args[1] == NULL) {
|
||||||
fprintf(stderr, "90s: not enough arguments\n");
|
fprintf(stderr, "90s: not enough arguments\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -245,7 +234,8 @@ int source(char **args) {
|
||||||
return status; // Indicate success
|
return status; // Indicate success
|
||||||
}
|
}
|
||||||
|
|
||||||
int bg(char **args) {
|
int bg(char **args)
|
||||||
|
{
|
||||||
if (args[1] == NULL) {
|
if (args[1] == NULL) {
|
||||||
fprintf(stderr, "90s: not enough arguments\n");
|
fprintf(stderr, "90s: not enough arguments\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -263,7 +253,9 @@ int bg(char **args) {
|
||||||
printf("Job %i: %s\n", job_index, search->command);
|
printf("Job %i: %s\n", job_index, search->command);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
bool is_builtin(char *command) {
|
|
||||||
|
bool is_builtin(char *command)
|
||||||
|
{
|
||||||
for (int i = 0; i < num_builtins(); i++) {
|
for (int i = 0; i < num_builtins(); i++) {
|
||||||
if (strcmp(command, builtin_cmds[i]) == 0) {
|
if (strcmp(command, builtin_cmds[i]) == 0) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -272,7 +264,8 @@ bool is_builtin(char *command) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int launch(char **args, int fd, int options) {
|
int launch(char **args, int fd, int options)
|
||||||
|
{
|
||||||
int is_bgj = (options & OPT_BGJ) ? 1 : 0;
|
int is_bgj = (options & OPT_BGJ) ? 1 : 0;
|
||||||
int redirect_stdout = (options & OPT_STDOUT) ? 1 : 0;
|
int redirect_stdout = (options & OPT_STDOUT) ? 1 : 0;
|
||||||
int redirect_stdin = (options & OPT_STDIN) ? 1 : 0;
|
int redirect_stdin = (options & OPT_STDIN) ? 1 : 0;
|
||||||
|
@ -325,7 +318,8 @@ int launch(char **args, int fd, int options) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
// execute built in commands or launch commands and wait it to terminate, return 1 to keep shell running
|
// execute built in commands or launch commands and wait it to terminate, return 1 to keep shell running
|
||||||
int execute(char **args) {
|
int execute(char **args)
|
||||||
|
{
|
||||||
if (args[0] == NULL) { // An empty command was entered.
|
if (args[0] == NULL) { // An empty command was entered.
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -407,7 +401,8 @@ int execute(char **args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute_pipe with as many pipes as needed
|
// execute_pipe with as many pipes as needed
|
||||||
int execute_pipe(char ***args) {
|
int execute_pipe(char ***args)
|
||||||
|
{
|
||||||
int pipefd[2];
|
int pipefd[2];
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int in = 0;
|
int in = 0;
|
||||||
|
|
|
@ -13,7 +13,8 @@ FILE *history_file;
|
||||||
char *histfile_path;
|
char *histfile_path;
|
||||||
int cmd_count = 0;
|
int cmd_count = 0;
|
||||||
|
|
||||||
FILE *open_history_file(char *mode) {
|
FILE *open_history_file(char *mode)
|
||||||
|
{
|
||||||
history_file = fopen(histfile_path, mode);
|
history_file = fopen(histfile_path, mode);
|
||||||
if (history_file == NULL) {
|
if (history_file == NULL) {
|
||||||
fprintf(stderr, "90s: Error opening history file\n");
|
fprintf(stderr, "90s: Error opening history file\n");
|
||||||
|
@ -21,7 +22,9 @@ FILE *open_history_file(char *mode) {
|
||||||
}
|
}
|
||||||
return history_file;
|
return history_file;
|
||||||
}
|
}
|
||||||
void check_history_file(void) {
|
|
||||||
|
void check_history_file(void)
|
||||||
|
{
|
||||||
char *env_home;
|
char *env_home;
|
||||||
env_home = getenv("XDG_CONFIG_HOME");
|
env_home = getenv("XDG_CONFIG_HOME");
|
||||||
if (env_home == NULL) {
|
if (env_home == NULL) {
|
||||||
|
@ -48,7 +51,8 @@ void check_history_file(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void save_command_history(char *args) {
|
void save_command_history(char *args)
|
||||||
|
{
|
||||||
history_file = open_history_file("a+");
|
history_file = open_history_file("a+");
|
||||||
char cmd[RL_BUFSIZE];
|
char cmd[RL_BUFSIZE];
|
||||||
cmd[0] = '\0';
|
cmd[0] = '\0';
|
||||||
|
@ -60,8 +64,10 @@ void save_command_history(char *args) {
|
||||||
fclose(history_file);
|
fclose(history_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *read_command(int direction) {
|
char *read_command(int direction)
|
||||||
if (direction == 1) { // up
|
{
|
||||||
|
/* Up */
|
||||||
|
if (direction == 1) {
|
||||||
cmd_count++;
|
cmd_count++;
|
||||||
} else { // down
|
} else { // down
|
||||||
if (cmd_count == 0) {
|
if (cmd_count == 0) {
|
||||||
|
@ -84,7 +90,8 @@ char *read_command(int direction) {
|
||||||
return history[num_history - cmd_count];
|
return history[num_history - cmd_count];
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_duplicate(char **history, int line_count, char *line) {
|
int is_duplicate(char **history, int line_count, char *line)
|
||||||
|
{
|
||||||
for (int i = 0; i < line_count; ++i) {
|
for (int i = 0; i < line_count; ++i) {
|
||||||
if (strcmp(history[i], line) == 0) {
|
if (strcmp(history[i], line) == 0) {
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -93,9 +100,10 @@ int is_duplicate(char **history, int line_count, char *line) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char **get_all_history(bool check) {
|
char **get_all_history(bool check)
|
||||||
|
{
|
||||||
history_file = open_history_file("r");
|
history_file = open_history_file("r");
|
||||||
char **history = memalloc(MAX_HISTORY * sizeof(char*));
|
char **history = memalloc(MAX_HISTORY * sizeof(char *));
|
||||||
char buffer[RL_BUFSIZE];
|
char buffer[RL_BUFSIZE];
|
||||||
int line_count = 0;
|
int line_count = 0;
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,8 @@ typedef struct job {
|
||||||
|
|
||||||
job *jobs = NULL;
|
job *jobs = NULL;
|
||||||
|
|
||||||
int num_jobs(void) {
|
int num_jobs(void)
|
||||||
|
{
|
||||||
job *current = jobs;
|
job *current = jobs;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
while (current != NULL) {
|
while (current != NULL) {
|
||||||
|
@ -23,7 +24,8 @@ int num_jobs(void) {
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int add_job(pid_t pid, char *command, bool status) {
|
int add_job(pid_t pid, char *command, bool status)
|
||||||
|
{
|
||||||
job *current = jobs;
|
job *current = jobs;
|
||||||
job *new_job = memalloc(sizeof(job));
|
job *new_job = memalloc(sizeof(job));
|
||||||
new_job->pid = pid;
|
new_job->pid = pid;
|
||||||
|
@ -45,7 +47,8 @@ int add_job(pid_t pid, char *command, bool status) {
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
job *get_job(int index) {
|
job *get_job(int index)
|
||||||
|
{
|
||||||
job *current = jobs;
|
job *current = jobs;
|
||||||
if (index == 0) {
|
if (index == 0) {
|
||||||
return current;
|
return current;
|
||||||
|
|
Loading…
Reference in a new issue