Skip to content

Commit

Permalink
feat: It finally compiles, but it doesn't run.
Browse files Browse the repository at this point in the history
  • Loading branch information
h4rldev committed Sep 6, 2024
1 parent b209998 commit f442ab9
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .clangd
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
CompileFlags:
Add: [-Wall, -Wextra, -I./]
Add: [-Wall, -Wextra, -I./include/]
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}\\build\\bin\\hsh.exe",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ _et voilà!_ Now it should be located in ./bin/ aswell.

## Contributing

Contribution is simple, make sure you're running an editor that works with C.
Contribution is simple, make sure you're running an editor that works with C and is using clang for compilation.

Check warning

Code scanning / Markdownlint (reported by Codacy)

Expected: 80; Actual: 112 Warning

Expected: 80; Actual: 112

Such as:

Expand All @@ -84,6 +84,13 @@ which is ick, but I'll gladly accept a pr for it.

Also, one final thing, make sure you have pre-commit hooks.

> \[!NOTE\]
> Windows people!
Just copy and paste the win_compile_commands.json in build and change it so it fits your path.

Check warning

Code scanning / Markdownlint (reported by Codacy)

Expected: 80; Actual: 94 Warning

Expected: 80; Actual: 94

Don't forget to rename it to compile_commands.json!

_direnv users who don't use nix, i'm sorry :(_

Made with :heart: by tatsoku-org!
4 changes: 2 additions & 2 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ if [[ ${OSTYPE} != "msys" ]]; then
LINKER_FLAGS+=" -fuse-ld=mold"
else
CFLAGS+=" -I/usr/include"
LINKER_FLAGS="-lbsd ${LINKER_FLAGS}"
LINKER_FLAGS+=" -lbsd -L/usr/lib"
fi

if [[ ${2} == "--debug" ]]; then
Expand Down Expand Up @@ -435,7 +435,7 @@ clear_vgcores() {
fix_clangd() {
cmake -Bbuild -DCMAKE_EXPORT_COMPILE_COMMANDS=ON >/dev/null
mv build/compile_commands.json .
find build -mindepth 1 ! -path "build/bin" ! -path "build/out" ! -path "build/tests" ! -name "build.sh" -exec rm -rf {} +
find build -mindepth 1 ! -path "build/bin" ! -path "build/win_compile_commands.json" ! -path "build/out" ! -path "build/tests" ! -name "build.sh" -exec rm -rf {} +
}

case $1 in
Expand Down
8 changes: 8 additions & 0 deletions build/win_compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"directory": "C:\\msys64\\home\\h4rl\\projects\\hamon-1\\build",
"command": "C:\\msys64\\clang64\\bin\\cc.exe -IC:\\msys64\\home\\h4rl\\projects\\hamon-1\\include -IC:\\msys64\\usr\\include -o CMakeFiles\\main.dir\\src\\main.c.o -c C:\\msys64\\home\\h4rl\\projects\\hamon-1\\src\\main.c",
"file": "C:\\msys64\\home\\h4rl\\projects\\hamon-1\\src\\main.c",
"output": "CMakeFiles\\main.dir\\src\\main.c.o"
}
]
7 changes: 6 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ default:
just link {{name}}
./build/bin/{{name}} {{args}}

@debug args="" name="hsh": fix_perms
@debug-linux args="" name="hsh": fix_perms
just compile --debug
just link {{name}}
valgrind ./build/bin/{{name}} {{args}}

@debug-windows args="" name="hsh": fix_perms
just compile --debug
just link {{name}}
lldb ./build/bin/{{name}}.exe {{args}}

@clear_cores: fix_perms
build/build.sh -vg

Expand Down
4 changes: 2 additions & 2 deletions src/hamon/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ int builtin_cd(int argc, char *argv[], char *const *envp) {
#elif _WIN32
char cwd_buf[MAX_PATH] = {0};

GetCurrentDirectory(MAX_PATH, (LPWSTR)cwd_buf);
GetCurrentDirectory(MAX_PATH, (LPSTR)cwd_buf);
strlcpy(last_dir, cwd_buf, MAX_PATH);

if (strncmp(path, "~", 1) == 0) {
Expand All @@ -178,7 +178,7 @@ int builtin_cd(int argc, char *argv[], char *const *envp) {
strlcpy(path, last_dir, MAX_PATH);
}

if (!SetCurrentDirectory((LPWSTR)path)) {
if (!SetCurrentDirectory((LPSTR)path)) {
win_perror("Couldn't change directory.");
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/hamon/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ int gen_default_config(void) {
}

size_t length = strlcpy(config_buffer, default_config, config_buffer_size);
if (length > sizeof(config_buffer_size)) {
if (length > config_buffer_size) {
free(config_buffer);
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/hamon/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void win_perror(const char *msg) {
0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, 0, 0);

printf("%s: %s\n", msg, lpMsgBuf);
printf("%s: %s\n", msg, (LPTSTR)lpMsgBuf);
LocalFree(lpMsgBuf);
}
#endif
7 changes: 4 additions & 3 deletions src/hamon/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ BOOL find_executable_in_path(LPCSTR executable, LPCSTR *path_found) {
return FALSE;
}
#endif

#ifdef __linux__
char *get_exec_path(const char *name, char *const *envp) {
char path[4096] = {0};

Expand All @@ -78,6 +78,7 @@ char *get_exec_path(const char *name, char *const *envp) {
}
return 0;
}
#endif

int execute(char *executable, char *argv[], char *const *envp) {
#ifdef __linux__
Expand All @@ -103,15 +104,16 @@ int execute(char *executable, char *argv[], char *const *envp) {
} else if (pid == 0) {
if (execve(path, argv, envp) != 0) {
perror("Failed to exec to childprocess (execve)");
free(path);
exit(-1);
} else {
free(path);
exit(0);
}
} else {
wait(&status);
free(path);
}

#elif _WIN32

LPCSTR win_executable = (LPCSTR)executable;
Expand Down Expand Up @@ -147,6 +149,5 @@ int execute(char *executable, char *argv[], char *const *envp) {
}

#endif
free(path);
return 0;
}
23 changes: 22 additions & 1 deletion src/hamon/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ char *get_username(void) {

// char *assemble_prompt() { return ">"; }

#ifdef _WIN32
char **get_environment_variables() {
LPCH env_strings = GetEnvironmentStrings();
LPCH ptr = env_strings;
char **env_array = calloc(4096, sizeof(char *));
int count = 0;

while (*ptr != '\0' && count < 4096 - 1) {
size_t len = strlen(ptr);

Check notice

Code scanning / Flawfinder (reported by Codacy)

Does not handle strings that are not \0-terminated; if given one it may perform an over-read (it could cause a crash if unprotected) (CWE-126). Note

Does not handle strings that are not \0-terminated; if given one it may perform an over-read (it could cause a crash if unprotected) (CWE-126).
env_array[count] = malloc(len + 1);
strcpy(env_array[count], ptr);

Check failure

Code scanning / Flawfinder (reported by Codacy)

Does not check for buffer overflows when copying to destination [MS-banned] (CWE-120). Consider using snprintf, strcpy_s, or strlcpy (warning: strncpy easily misused). Error

Does not check for buffer overflows when copying to destination [MS-banned] (CWE-120). Consider using snprintf, strcpy_s, or strlcpy (warning: strncpy easily misused).
ptr += len + 1;
count++;
}

env_array[count] = NULL;
FreeEnvironmentStrings(env_strings);
return env_array;
}
#endif

bool is_env_format(const char *str) {
const char *equals = strchr(str, '=');
return equals != 0 && equals != str && *(equals + 1) != '\0';
Expand Down Expand Up @@ -104,7 +125,7 @@ int init_prompt(void) {
#ifdef __linux__
char **envp = __environ;
#elif _WIN32
char **envp = _dupenv_s(NULL, NULL, 0);
char **envp = get_environment_variables();
#endif
int argc = 0;
int env_count = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ int main(int argc, char *argv[]) {
gen_default_config();

// init_shell();
return init_prompt();
// return init_prompt();
}

0 comments on commit f442ab9

Please sign in to comment.