To understand the machine, you must build the machine. 🖥️ A robust, modular Unix shell bridging low-level kernel interactions with cloud-based AI.
Most developers use shells (Bash, Zsh) every day without knowing how they work. ShellOrigin (kaiVen) is a custom implementation of a command-line processor that handles process creation, memory allocation, file descriptor hijacking, and signal handling manually.
Recently upgraded for modern workflows, this shell features a native AI integration module, proving that the magic of the terminal—and cloud APIs—is just C code and HTTP protocols under the hood.
- 🔄 Interactive REPL: A custom "Read-Eval-Print Loop" featuring a dynamic, ANSI-colored prompt that actively tracks your Current Working Directory (
getcwd). - 🚀 Process Execution: Launches standard Linux programs (
ls,grep,vi,gcc) utilizing the classicfork()andexecvp()system calls. - 🛡️ Signal Trapping: Intercepts
SIGINT(Ctrl+C) via<signal.h>to prevent accidental shell termination while safely killing runaway child processes. - 📂 Output Redirection: Implements file descriptor hijacking (
dup2) to route standard output streams into files (e.g.,echo "test" > file.txt). - 🤖 Cloud AI Integration: A deeply integrated
suggestcommand that makes secure, memory-safe HTTP POST requests to the Google Gemini API for real-time CLI assistance. - 🛠️ Native Built-ins: Custom implementations of internal state-modifying commands:
cd(Change Directory)help(Documentation)suggest(AI CLI Assistant)exit(Graceful termination)
The codebase has evolved from a monolithic script into a professional, multi-file modular architecture.
The heartbeat of the shell is an infinite loop handling the Initialize → Parse → Execute lifecycle.
- Process Cloning: Uses
fork()&execvp()to create child processes for external binaries. - File Descriptors: Parses the
>token, opens target files usingopen(), and reroutesSTDOUT_FILENOusingdup2()prior to execution. - Zombie Prevention: The parent process explicitly suspends using
waitpid()until the child terminates cleanly.
Unlike high-level languages, C lacks native internet capabilities. This module relies heavily on libcurl and custom memory management.
- Defensive Memory Allocation: Incoming network packets arrive in unpredictable chunk sizes. The
write_memory_callbackfunction uses dynamicrealloc()arithmetic to stretch a memory buffer in real-time without overflowing. - JSON Syntax Tree: Parses the raw HTTP response using
cJSON, utilizing strict defensive type-checking (cJSON_IsArray,cJSON_IsString) to prevent segmentation faults during payload extraction. - Environment Security: API keys are strictly fetched via
getenv(), preventing hardcoded credentials in the source code.
You can run ShellOrigin on any standard Linux distribution or Windows Subsystem for Linux (WSL).
You must install the libcurl development headers to compile the networking module.
sudo apt-get update
sudo apt-get install libcurl4-openssl-dev
git clone [https://github.com/karnVen/ShellOrigin.git](https://github.com/karnVen/ShellOrigin.git)
cd ShellOrigin
For the suggest AI command to function, export your Gemini API key to your local environment. (Tip: Add this to your ~/.bashrc or ~/.zshrc to make it permanent).
export GEMINI_API_KEY="your_actual_api_key_here"
Use the GCC compiler to link the core shell, the AI module, the JSON parser, and the curl library.
gcc main.c ai_module.c cJSON.c -o kaiVen -lcurl
./kaiVen
Example Session:
kaiVen:/home/karnVen/ShellOrigin$ ls -la
kaiVen:/home/karnVen/ShellOrigin$ echo "Systems engineering" > notes.txt
kaiVen:/home/karnVen/ShellOrigin$ suggest how to debug a segmentation fault in C
kaiVen:/home/karnVen/ShellOrigin$ exit
- Pipes (
|): Implementing inter-process communication by wiring thestdoutof one child to thestdinof another. - Input Redirection (
<): Reading standard input from files via descriptor hijacking. - Command History: Implementing buffer tracking for Up-arrow command recall.
- Shell architecture inspired by the classic Stephen Brennan "Write a Shell in C" tutorial.
- JSON parsing handled by the lightweight, single-file
cJSONlibrary by Dave Gamble. - Built by Team Karnage.
Karan Singh
- 🐙 GitHub: @karnVen
- 💼 LinkedIn: Karan Singh
Would you like me to draft an updated one-liner for your main profile README to reflect this new "kaiVen" AI integration?