Skip to content

Commit e38d76c

Browse files
committed
feat(hamon): fix inconsistencies, implement basic shell functionality.
1 parent 3459f5b commit e38d76c

14 files changed

+303
-37
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 3-Clause License
22

3-
Copyright (c) 2024, h4rl
3+
Copyright (c) 2024, tatsoku
44

55
Redistribution and use in source and binary forms, with or without
66
modification, are permitted provided that the following conditions are met:

README.md

+42-17
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
# hamon!
22

3-
or <abbr title="Hamon Shell">hsh</abbr>
3+
or **H**amon **SH**ell aka hsh.
44

55
<sub> 波紋 - Ripple </sub>
66

7-
87
A homemade shell in C.
98

10-
Compiled with [clang](https://github.com/llvm/llvm-project/tree/main/clang) or [gcc](https://gcc.gnu.org) & linked with [mold](https://github.com/rui314/mold).
9+
Compiled with
10+
[clang](https://github.com/llvm/llvm-project/tree/main/clang) or
11+
[gcc](https://gcc.gnu.org) & linked with [mold](https://github.com/rui314/mold).
1112

1213
![[LICENSE](https://github.com/h4rldev/hshell/blob/main/LICENSE)](https://img.shields.io/github/license/h4rldev/hshell?style=flat-square)
1314
![[OpenSSF Scorecard](https://securityscorecards.dev/viewer/?uri=github.com/h4rldev/hshell)](https://api.securityscorecards.dev/projects/github.com/h4rldev/hshell/badge?style=flat-square)
1415

1516
> \[!NOTE\]
1617
> This project is in no way functional yet!
1718
19+
## Usage
20+
21+
```sh
22+
./hsh {args}
23+
```
24+
1825
## Installation
1926

20-
> Classic installation through package manager and such, won't be available for a very long time.
21-
>
22-
> So you will have to build it yourself.
23-
>
24-
> Oh you want to know how?
27+
Classic installation through package manager and such, won't be available for a very long time.
28+
29+
So you will have to build it yourself.
30+
31+
Oh you want to know how?
2532

2633
### With Just
2734

28-
> If you want the easy version, be sure to install [just](https://github.com/casey/just)
35+
If you want the easy version, be sure to install [just](https://github.com/casey/just)
2936

3037
..and run
3138

@@ -34,24 +41,42 @@ just fix_perms
3441
just build
3542
```
3643

37-
> And you'll find it in ./bin
44+
And you'll find it in ./bin
3845

3946
### Without Just
4047

41-
> Here's we're it gets "difficult". It's just a tiny bit more commands.
48+
Here's where it gets "difficult". It's just a tiny bit more commands.
4249

4350
```sh
4451
chmod +x ./scripts/build.sh
4552
./scripts/build.sh -c
4653
./scripts/build.sh -l hsh
4754
```
4855

49-
> _et voilà!_ Now it should be located in ./bin/ aswell.
56+
_et voilà!_ Now it should be located in ./bin/ aswell.
5057

51-
## Usage
58+
## Contributing
5259

53-
```sh
54-
./hsh {args}
55-
```
60+
Contribution is simple, make sure you're running an editor that works with C.
61+
62+
Such as:
63+
64+
- neovim
65+
- emacs
66+
- vscode/codium
67+
- jetbrains
68+
- whatever!
69+
70+
Just don't clutter the repo with config files for your editor.
71+
72+
You will also need to either have a shell that supports bash scripts,
73+
unless you want to port
74+
[build.sh](https://github.com/tatsoku/hamon/blob/main/scripts/build.sh)
75+
for Powershell or Batch,
76+
which is ick, but I'll gladly accept a pr for it.
77+
78+
Also, one final thing, make sure you have pre-commit hooks.
79+
80+
_direnv users who don't use nix, i'm sorry :(_
5681

57-
> Made with :heart: by h4rl
82+
Made with :heart: by tatsoku-org!

SECURITY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ So updates will be master for now.
1313

1414
> __IF__ you do find a vulnerability.
1515
16-
Make an issue [here!](https://github.com/h4rldev/hshell/issues)
16+
Make an issue [here!](https://github.com/tatsoku/hamon/issues)
1717

1818
Thanks!

justfile

+3-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ default:
33

44
@fix_perms:
55
chmod +x scripts/build.sh
6-
chmod +x scripts/clear_vgcores.sh
76

8-
@compile-debug name="hsh":
9-
scripts/build.sh -c {{name}} --debug
10-
11-
@compile name="hsh":
12-
scripts/build.sh -c {{name}}
7+
@compile extra_args="":
8+
scripts/build.sh -c {{extra_args}}
139

1410
@link name="hsh":
1511
scripts/build.sh -l {{name}}
@@ -24,7 +20,7 @@ default:
2420
./bin/{{name}} {{args}}
2521

2622
@debug args="" name="hsh":
27-
just compile-debug {{name}}
23+
just compile --debug
2824
just link {{name}}
2925
valgrind ./bin/{{name}} {{args}}
3026

scripts/build.sh

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env bash
22

3-
# hShell: build.sh
3+
# hamon!: build.sh
44
#
5-
# built with this inspirational message in mind: Why tf would I use make
5+
# built with this inspirational message in mind: Why tf would we use make
66
#
77
# Licensed under the:
88
# BSD 3-Clause License
99
#
10-
# Copyright (c) 2024, h4rl
10+
# Copyright (c) 2024, tatsoku
1111
#
1212
# Redistribution and use in source and binary forms, with or without
1313
# modification, are permitted provided that the following conditions are met:
@@ -35,13 +35,13 @@
3535
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3636
#
3737
#
38-
# Made with <3 by h4rl.
38+
# Made with <3 by tatsoku-org!
3939

4040
shopt -s nullglob
4141

4242
__NAME__="hamon!: build.sh"
4343
__AUTHOR__="h4rl"
44-
__DESCRIPTION__="Compiles & links hShell into a executable."
44+
__DESCRIPTION__="Compiles & links hamon into an executable."
4545
__LICENSE__="BSD 3-Clause License"
4646
__VERSION__="0.1.0"
4747

@@ -70,9 +70,7 @@ else
7070
DEBUG=false
7171
fi
7272

73-
#CFLAGS=$(pkg-config --cflags gtk+-3.0)
7473
CFLAGS="-O3"
75-
#LINKER_FLAGS="$(pkg-config --libs gtk+-3.0) -lcurl -ljansson"
7674
#LINKER_FLAGS=""
7775

7876
if [[ ! -d ${OUT} ]]; then

shell.nix

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pkgs.mkShell {
1010
mold
1111
pre-commit
1212
bash-language-server
13+
valgrind
1314
];
1415
shellHook = ''
1516
'';

src/hamon/builtins.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "headers/builtins.h"
2+
3+
int builtin_echo(int argc, char *argv[]) {
4+
char *message = {0};
5+
6+
if (argc > 2) {
7+
message = argv[2];
8+
if (strncmp(argv[1], "-E", 2) == 0) {
9+
fwrite(message, 1, strlen(argv[2]), stdout);
10+
return 0;
11+
}
12+
13+
if (strncmp(argv[1], "-n", 2) == 0) {
14+
printf("%s", message);
15+
return 0;
16+
}
17+
} else {
18+
message = argv[1];
19+
}
20+
21+
puts(message);
22+
return 0;
23+
}
24+
25+
int builtin_exit(int argc, char *argv[]) {
26+
int code = (int)(unsigned long)argv[1];
27+
exit(code);
28+
return 0;
29+
}
30+
31+
int builtin_cd(int argc, char *argv[]) {
32+
puts("cd: I don't feel like doing this yet, soz");
33+
return 0;
34+
}
35+
36+
int builtin_help(int argc, char *argv[]) {
37+
puts("help: I don't feel like doing this yet, soz");
38+
return 0;
39+
}
40+
41+
// TODO: figure this shit out
42+
43+
int check_builtins(int argc, char *argv[]) {
44+
const char *builtin_strs[] = {"cd", "help", "echo", "exit"};
45+
int (*builtin_funcs[])() = {&builtin_cd, &builtin_help, &builtin_echo,
46+
&builtin_exit};
47+
int num_builtins = sizeof(builtin_strs) / sizeof(char *);
48+
49+
return 0;
50+
}

src/hamon/config.c

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
/*
1111
* Default configuration:
12+
* ▄▄ ▄▄
13+
* ██ ██
14+
* ██▄████▄ ▄█████▄ ████▄██▄ ▄████▄ ██▄████▄ ██
15+
* ██▀ ██ ▀ ▄▄▄██ ██ ██ ██ ██▀ ▀██ ██▀ ██ ██
16+
* ██ ██ ▄██▀▀▀██ ██ ██ ██ ██ ██ ██ ██ ▀▀
17+
* ██ ██ ██▄▄▄███ ██ ██ ██ ▀██▄▄██▀ ██ ██ ▄▄
18+
* ▀▀ ▀▀ ▀▀▀▀ ▀▀ ▀▀ ▀▀ ▀▀ ▀▀▀▀ ▀▀ ▀▀ ▀▀
1219
*
1320
* # !! Main configuration for theming and prompt
1421
* # More advanced theming will happen at a way later date.
@@ -66,6 +73,13 @@
6673
*/
6774

6875
const char *default_config =
76+
"# ▄▄ ▄▄\n"
77+
"# ██ ██\n"
78+
"# ██▄████▄ ▄█████▄ ████▄██▄ ▄████▄ ██▄████▄ ██\n"
79+
"# ██▀ ██ ▀ ▄▄▄██ ██ ██ ██ ██▀ ▀██ ██▀ ██ ██\n"
80+
"# ██ ██ ▄██▀▀▀██ ██ ██ ██ ██ ██ ██ ██ ▀▀\n"
81+
"# ██ ██ ██▄▄▄███ ██ ██ ██ ▀██▄▄██▀ ██ ██ ▄▄\n"
82+
"# ▀▀ ▀▀ ▀▀▀▀ ▀▀ ▀▀ ▀▀ ▀▀ ▀▀▀▀ ▀▀ ▀▀ ▀▀\n\n"
6983
"# !! Main configuration for theming and prompt\n"
7084
"# More advanced theming will happen at a way later date.\n\n"
7185
"[prompt]\n"

src/hamon/exec.c

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#define COLORS
2+
3+
#include "headers/exec.h"
4+
#include "headers/escape.h"
5+
6+
#if _WIN32
7+
8+
BOOL FindExecutableInPath(LPCSTR executable, LPCSTR *path_found) {
9+
LPCSTR env_var = getenv("PATH");
10+
if (env_var == NULL)
11+
fprintf(stderr,
12+
RED "!%s Failed to find path variable, is your windows pc ok?",
13+
CLEAR) return FALSE;
14+
15+
LPCSTR path = env_var;
16+
do {
17+
size_t len = strlen(path);
18+
if (len > 0 && path[len - 1] == ';') {
19+
path[len - 1] = '\0'; // Remove trailing semicolon
20+
}
21+
if (_stricmp(executable, path) == 0) {
22+
*path_found = path;
23+
return TRUE;
24+
}
25+
path += len + 1; // Move to next directory in PATH
26+
} while (*path);
27+
28+
return FALSE;
29+
}
30+
31+
#endif
32+
33+
int execute(char *executable, char **argv, int status) {
34+
35+
#if __linux__
36+
pid_t pid = fork();
37+
38+
if (pid < 0) {
39+
perror("failed to fork");
40+
} else if (pid == 0) {
41+
if (execvp(executable, argv) == -1) {
42+
if (errno == ENOENT) {
43+
fprintf(stderr, RED "!%s %s: Command not found\n", CLEAR, executable);
44+
} else {
45+
perror("Failed to exec to childprocess. (execvp)");
46+
}
47+
return -1;
48+
}
49+
} else {
50+
wait(&status);
51+
}
52+
53+
#elif _WIN32
54+
55+
LPCSTR win_executable = (LPCSTR)executable;
56+
LPCSTR path_found = {0};
57+
58+
if (find_executable_in_path(win_executable, &path_found)) {
59+
STARTUPINFO start_i;
60+
PROCESS_INFORMATION proc_i;
61+
62+
ZeroMemory(&start_i, sizeof(start_i));
63+
start_i.cb = sizeof(start_i);
64+
ZeroMemory(&proc_i, sizeof(proc_i));
65+
66+
LPSTR full_path = malloc(strlen(path_found) + strlen(win_executable) + 2);
67+
strlcpy(full_path, path_found, strlen(full_path));
68+
strlcat(full_path, "\\", 2);
69+
strlcat(full_path, win_executable, strlen(win_executable));
70+
71+
if (!CreateProcess(0, full_path, 0, 0, FALSE, 0, 0, 0, &start_i, &proc_i)) {
72+
fprintf(stderr, RED "!%s CreateProcess failed (%d).\n", CLEAR,
73+
GetLastError());
74+
return -1;
75+
}
76+
77+
CloseHandle(proc_i.hProcess);
78+
CloseHandle(proc_i.hThread);
79+
free(full_path);
80+
} else {
81+
// Signify nonexistent executable.
82+
return 1;
83+
}
84+
85+
#endif
86+
return 0;
87+
}

src/hamon/headers/builtins.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef BUILTINS_H
2+
#define BUILTINS_H
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
int builtin_echo(int argc, char *argv[]);
9+
int builtin_exit(int argc, char *argv[]);
10+
int builtin_cd(int argc, char *argv[]);
11+
int builtin_help(int argc, char *argv[]);
12+
int check_builtins(int argc, char *argv[]);
13+
14+
#endif // !BUILTINS_H

src/hamon/headers/escape.h

+17
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@
6262

6363
#endif
6464

65+
#ifdef CURSORS
66+
67+
#define ENABLE "l"
68+
#define DISABLE "h"
69+
#define SET DISABLE
70+
71+
#define HIDE_CURSOR BASE "?25" DISABLE
72+
#define SHOW_CURSOR BASE "?25" ENABLE
73+
#define CURSOR_NO_BLINK BASE "?5" DISABLE
74+
#define CURSOR_BLINK BASE "?5" ENABLE
75+
#define BLOCK_CURSOR BASE "7" SET
76+
#define UNDERLINE_CURSOR BASE "4" SET
77+
#define BLINK_BLOCK_CURSOR BASE "5" SET
78+
#define VERTICAL_CURSOR BASE "5 q"
79+
80+
#endif
81+
6582
// Global clear color/graphics mode string.
6683
#define CLEAR "\033[0m"
6784

0 commit comments

Comments
 (0)