Skip to content

admtrv/tsh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tiny Shell

Demo

Simple interactive shell with client-server architecture via TCP sockets. Written in pure C for Linux.

Build

cmake -B build
cmake --build build

Usage

./tsh [-s|-c] [-p port] [-i addr] [-f file] [-d] [-t sec] [-h]

Options

Flag Description
-s Run as server (default)
-c Run as client
-p port Set port number (default: 4070)
-i addr Bind to specific IP (server) or connect to IP (client)
-f file Execute commands from script file before interactive mode
-d Run server as daemon (background process)
-t sec Set idle connection timeout in seconds
-h Show help message

Internal commands

Command Description
help Show help message
quit Close current connection
halt Terminate entire program
stat List all active connections
abort <n> Close connection by index
prompt <format> Set prompt format (see customization)

Special characters

Char Description
# Comment, rest of line is ignored
; Command separator
< Input redirection from file
> Output redirection to file
| Pipe, connect commands

Examples

Start server on default port:

./tsh

Start server on specific port and interface:

./tsh -s -p 8080 -i 192.168.1.5

Connect client to server:

./tsh -c -p 8080
./tsh -c -p 8080 -i 192.168.1.5

Run script file at startup:

./tsh -s -p 8080 -f startup.txt
./tsh -c -p 8080 -f commands.txt

Use special characters in shell:

echo hello ; echo world        # two commands
echo visible # hidden          # comment
ls -la | head -5               # pipe
echo data > output.txt         # output redirection
wc -l < input.txt              # input redirection

Run server as daemon:

./tsh -d -p 8080

Disconnect idle clients after 60 seconds:

./tsh -s -p 8080 -t 60

Manage connections:

12:30 admtrv@host# stat
Listening on 0.0.0.0:4070
Active connections: 2
  [0] 127.0.0.1:41520
  [1] 192.168.1.10:52314

12:30 admtrv@host# abort 0
Closing connection [0] 127.0.0.1:41520

Customization

Default prompt format is %t %u@%h%# , which produces:

12:30 admtrv@host#

Use the prompt internal command to change the format at runtime.

Format sequences

Sequence Description Example
%u Username admtrv
%h Hostname host
%t Current time (HH:MM) 12:30
%d Current directory /home/admtrv
%g Git branch or commit main
%n Newline
%c{code} ANSI color/style %c{32} = green, %c{0} = reset
%<char> Escape special character (# ; > < | and %)

Examples

Simple minimal prompt:

12:30 admtrv@host# prompt %>
> echo hello
hello
>

Classic bash prompt:

12:30 admtrv@host# prompt %u@%h:%d$
admtrv@host:~$ echo test
test
admtrv@host:~$

Cool ohmyzsh like prompt:

12:30 admtrv@host# prompt ╭%t %u %>%>%> %d (%g)%n╰$
╭12:30 admtrv >>> /home/admtrv (master)
╰$ echo cool
cool
╭12:30 admtrv >>> /home/admtrv (master)
╰$ 

Or with colors:

prompt %c{38.5.75}╭%c{0} %c{38.5.75}%u%c{0} %c{1.38.5.75}%d%c{0} %c{38.5.170}(%g)%c{0}%n%c{38.5.75}╰%c{0} %c{0}$

About

Simple interactive shell written in pure C

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors