Skip to content

AstorDG/codecrafters-shell-zig

Repository files navigation

progress-banner

This is a project made from the "Build Your Own Shell" Challenge on CodeCrafters

Technical Implementation

Architecture Overview

This shell follows a classic read-eval-print loop (REPL) pattern with three main components:

  1. Input Layer: Buffered I/O using buffers for both stdin and stdout, minimizing system calls
  2. Command Parser: Simple space-delimited tokenizer
  3. Command Dispatcher: Enum-based switch statement that routes to built-in handlers or external execution

Built-in Command System

Built-ins are handled through a compile-time generated enum (built_in_commands) mapped from command strings. The dispatcher uses std.meta.stringToEnum() for O(1) lookup, falling back to external command execution for unrecognized commands. Each built-in receives the remaining token iterator for argument processing.

External Command Execution

For non-built-in commands, the shell:

  1. PATH Traversal: Parses the PATH environment variable and iterates through directories
  2. Executable Discovery: Opens each directory, checks file permissions (mode & 0o111) then runs the given executable.
  3. Process Spawning: Uses std.process.Child to spawn a child process with collected arguments

Memory Management

Uses Zig's GeneralPurposeAllocator for all dynamic allocations. Key patterns:

  • Path strings from environment variables are allocated upfront and freed after the main loop
  • Executable paths discovered during PATH traversal are allocated per-search and immediately freed after use
  • Process argument lists use std.ArrayList with toOwnedSlice() for clean handoff to the child process

I/O System

Implements buffered I/O to reduce syscalls:

  • Output: writerStreaming with manual flush after each prompt and command output
  • Input: readerStreaming with takeDelimiter('\n') for line-based input reading

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors