+/yash
git clone https://git.julia.blue/+/yash
4587801
master
1#include <assert.h> 2#include <ctype.h> 3#include <setjmp.h> 4#include <signal.h> 5#include <stdbool.h> 6#include <stdint.h> 7#include <stdio.h> 8#include <stdlib.h> 9#include <stdnoreturn.h> 10#include <string.h> 11 12#include <fcntl.h> 13#include <readline/history.h> 14#include <readline/readline.h> 15#include <sys/stat.h> 16#include <sys/wait.h> 17#include <unistd.h> 18 19#define MAX_JOBS 32 20#define MAX_ARGS 1024 21#define DEBUG_ENABLE false 22#define DEBUG (format ,args ...) \ 23 if (DEBUG_ENABLE) \ 24 printf(format, ##args) 25 26#include <stdarg.h> 27 28typedef struct { 29char * command ; 30pid_t group_id ; 31pid_t pipe_id ; 32bool running ; 33int8_t number ; 34int8_t next ; 35int8_t prev ; 36bool occupied ; 37bool main_exited ; 38bool do_pipe ; 39bool pipe_exited ; 40}Job ; 41 42static volatile Job jobs [MAX_JOBS ]; 43 44static sigjmp_buf jump_buf ; 45static volatile sig_atomic_t first_job = -1 ; 46static volatile sig_atomic_t recent_job = -1 ; 47static volatile sig_atomic_t foreground_job = -1 ; 48 49static void push_job (int j ) { 50if (recent_job == -1 ) { 51first_job = j ; 52 }else { 53jobs [recent_job ].next = (int8_t )j ; 54 } 55jobs [j ].prev = (int8_t )recent_job ; 56jobs [j ].next = -1 ; 57recent_job = j ; 58} 59 60static void remove_job (int j ) { 61if (jobs [j ].prev != -1 ) { 62jobs [jobs [j ].prev ].next = jobs [j ].next ; 63 }else { 64assert (first_job == j ); 65first_job = jobs [j ].next ; 66 } 67if (jobs [j ].next != -1 ) { 68jobs [jobs [j ].next ].prev = jobs [j ].prev ; 69 }else { 70assert (recent_job == j ); 71recent_job = jobs [j ].prev ; 72 } 73} 74 75static void handle_finished_jobs () { 76DEBUG ("going through finished stuff\n" ); 77for (int i = 0 ;i < MAX_JOBS ;++ i ) { 78if (jobs [i ].occupied && jobs [i ].main_exited && jobs [i ].pipe_exited ) { 79printf ("[%d]+ %-20s %s &\n" ,jobs [i ].number ,"Done" , 80jobs [i ].command ); 81free (jobs [i ].command ); 82jobs [i ].occupied = false; 83 } 84 } 85} 86 87static int max (int a ,int b ) { 88return a > b ?a :b ; 89} 90 91static char * next_token (char ** line_ptr ) { 92char * line = * line_ptr ; 93while (* line && isspace (* line )) {++ line ; } 94if (!* line ) { 95return NULL ; 96 } 97char * start = line ; 98char c = * line ; 99if (c == '|' || c == '<' || c == '>' || c == '&' ) { 100* line_ptr = line + 1 ; 101return strndup (start ,1 ); 102 }else if (c == '2' && * (line + 1 )== '>' ) { 103* line_ptr = line + 2 ; 104return strndup (start ,2 ); 105 } 106do { 107c = *++ line ; 108 }while (c && !isspace (c )&& c != '|' && c != '<' && c != '>' && c != '&' ); 109* line_ptr = line ; 110return strndup (start , (size_t )(line - start )); 111} 112 113static char * main_args [MAX_ARGS ]; 114static char * pipe_args [MAX_ARGS ]; 115static int main_redir_in ,main_redir_out ,main_redir_err ; 116static int pipe_redir_in ,pipe_redir_out ,pipe_redir_err ; 117static pid_t main_pid ; 118static pid_t pipe_pid ; 119static bool launch_bg ; 120static bool do_pipe ; 121 122static bool parse_line (char * line ,bool after_pipe ) { 123do_pipe = false; 124launch_bg = false; 125size_t arg_num = 0 ; 126int redir_in = -1 ,redir_out = -1 ,redir_err = -1 ; 127char ** args = after_pipe ?pipe_args :main_args ; 128for (int i = 0 ;args [i ];++ i ) {free (args [i ]); } 129char * current ; 130while ((current = next_token (& line ))) { 131if (!strcmp ("<" ,current )) { 132if (!(current = next_token (& line ))) { 133DEBUG ("No file passed for redirection\n" ); 134return false; 135 } 136if ((redir_in = open (current ,O_RDONLY ))== -1 ) { 137printf ("yash: %s: No such file or directory\n" ,current ); 138return false; 139 } 140 }else if (!strcmp (">" ,current )) { 141if (!(current = next_token (& line ))) { 142DEBUG ("No file passed for redirection\n" ); 143 } 144 (redir_out = open (current ,O_CREAT |O_WRONLY , 145S_IRUSR |S_IWUSR |S_IRGRP |S_IWGRP |S_IROTH )); 146 }else if (!strcmp ("2>" ,current )) { 147if (!(current = next_token (& line ))) { 148DEBUG ("No file passed for redirection\n" ); 149return false; 150 } 151redir_err = open (current ,O_CREAT |O_WRONLY , 152S_IRUSR |S_IWUSR |S_IRGRP |S_IWGRP |S_IROTH ); 153 }else if (!strcmp ("|" ,current )) { 154if (!parse_line (line , true)) { 155DEBUG ("Error parsing pipe command\n" ); 156 } 157do_pipe = true; 158break ; 159 }else if (!strcmp ("&" ,current )) { 160launch_bg = true; 161break ; 162 }else { 163args [arg_num ++ ]= current ; 164 } 165 } 166if (after_pipe ) { 167pipe_redir_err = redir_err ; 168pipe_redir_in = redir_in ; 169pipe_redir_out = redir_out ; 170 }else { 171main_redir_err = redir_err ; 172main_redir_in = redir_in ; 173main_redir_out = redir_out ; 174 } 175args [arg_num ]= 0 ; 176return true; 177} 178 179static void line (char * orig ) { 180char * line = orig ; 181if (!line ) { 182for (int i = first_job ;i != -1 ;i = jobs [i ].next ) { 183if (jobs [i ].occupied ) { 184kill (- jobs [i ].group_id ,SIGKILL ); 185 } 186 } 187exit (0 ); 188 } 189add_history (line ); 190 191char * command = next_token (& line ); 192if (!command ) { 193return ; 194 }else if (!strcmp ("fg" ,command )) { 195if (recent_job != -1 ) { 196foreground_job = recent_job ; 197puts (jobs [recent_job ].command ); 198kill (- jobs [recent_job ].group_id ,SIGCONT ); 199remove_job (recent_job ); 200 } 201return ; 202 }else if (!strcmp ("bg" ,command )) { 203int i = recent_job ; 204while (i != -1 && jobs [i ].running ) {i = jobs [i ].prev ; } 205if (i != -1 ) { 206jobs [i ].running = true; 207printf ("[%d]+ %s &\n" ,jobs [i ].number ,jobs [i ].command ); 208DEBUG ("Sending SIGCONT to %d\n" ,jobs [i ].group_id ); 209kill (- jobs [i ].group_id ,SIGCONT ); 210 } 211return ; 212 }else if (!strcmp ("jobs" ,command )) { 213int i = first_job ; 214while (i != -1 ) { 215printf ("[%d]%c %-20s %s%s\n" ,jobs [i ].number , 216i == recent_job ?'+' :'-' , 217jobs [i ].running ?"Running" :"Stopped" ,jobs [i ].command , 218jobs [i ].running ?" &" :"" ); 219i = jobs [i ].next ; 220 } 221return ; 222 } 223 224if (!parse_line (orig , false)) { 225return ; 226 } 227if (launch_bg ) {// strip & and resulting trailing whitespace 228char * end = strchr (orig ,'&' ); 229if (end ) { 230-- end ; 231while (isspace (* end )) {-- end ; } 232*++ end = (char )'\0' ; 233 } 234 } 235 236int pipe_ends [2 ]; 237if (do_pipe ) { 238pipe (pipe_ends ); 239 } 240 241if ((main_pid = fork ())) { 242if (do_pipe ) { 243if (!(pipe_pid = fork ())) { 244setpgid (0 ,main_pid ); 245signal (SIGINT ,SIG_DFL ); 246signal (SIGTSTP ,SIG_DFL ); 247signal (SIGCHLD ,SIG_DFL ); 248dup2 (pipe_ends [0 ],0 ); 249close (pipe_ends [1 ]); 250if (pipe_redir_in != -1 ) { 251dup2 (pipe_redir_in ,0 ); 252 } 253if (pipe_redir_out != -1 ) { 254dup2 (pipe_redir_out ,1 ); 255 } 256if (pipe_redir_err != -1 ) { 257dup2 (pipe_redir_err ,2 ); 258 } 259if (execvp (pipe_args [0 ],pipe_args )== -1 ) { 260DEBUG ("Error calling exec for pipe\n" ); 261exit (1 ); 262 } 263 } 264close (pipe_ends [0 ]); 265close (pipe_ends [1 ]); 266 } 267 }else { 268setpgid (0 ,0 ); 269signal (SIGINT ,SIG_DFL ); 270signal (SIGTSTP ,SIG_DFL ); 271signal (SIGCHLD ,SIG_DFL ); 272if (do_pipe ) { 273dup2 (pipe_ends [1 ],1 ); 274close (pipe_ends [0 ]); 275 } 276if (main_redir_in != -1 ) { 277dup2 (main_redir_in ,0 ); 278 } 279if (main_redir_out != -1 ) { 280dup2 (main_redir_out ,1 ); 281 } 282if (main_redir_err != -1 ) { 283dup2 (main_redir_err ,2 ); 284 } 285if (execvp (main_args [0 ],main_args )== -1 ) { 286DEBUG ("Error calling exec\n" ); 287exit (1 ); 288 } 289 } 290 291int i ; 292for (i = 0 ;i < MAX_JOBS ;++ i ) { 293if (!jobs [i ].occupied ) { 294break ; 295 } 296 } 297jobs [i ].occupied = true; 298jobs [i ].pipe_id = do_pipe ?pipe_pid :-1 ; 299jobs [i ].group_id = main_pid ; 300jobs [i ].command = strdup (orig ); 301jobs [i ].running = true; 302jobs [i ].running = true; 303jobs [i ].main_exited = false; 304jobs [i ].pipe_exited = !do_pipe ; 305int8_t biggest = 0 ; 306int temp = first_job ; 307while (temp != -1 ) { 308biggest = (int8_t )max (jobs [temp ].number ,biggest ); 309temp = jobs [temp ].next ; 310 } 311jobs [i ].number = biggest + 1 ; 312if (launch_bg ) { 313push_job (i ); 314 }else { 315foreground_job = i ; 316 } 317DEBUG ("Launching %d (%d) as job #%d\n" ,main_pid ,pipe_pid ,jobs [i ].number ); 318} 319 320static void handle_sigint (int sig ) { 321assert (sig == SIGINT ); 322if (foreground_job != -1 ) { 323kill (- jobs [foreground_job ].group_id ,SIGINT ); 324jobs [foreground_job ].occupied = false; 325foreground_job = -1 ; 326DEBUG ("\nexited\n" ); 327 }else { 328siglongjmp (jump_buf ,0 ); 329 } 330} 331 332static void handle_sigtstp (int sig ) { 333assert (sig == SIGTSTP ); 334if (foreground_job != -1 ) { 335kill (- jobs [foreground_job ].group_id ,SIGTSTP ); 336jobs [foreground_job ].running = false; 337push_job (foreground_job ); 338foreground_job = -1 ; 339return ; 340 } 341DEBUG ("Attempted to stop shell process\n" ); 342} 343 344static void handle_sigchld (int sig ) { 345assert (sig == SIGCHLD ); 346pid_t pid ; 347int status ; 348while ((pid = waitpid (-1 ,& status ,WNOHANG |WUNTRACED ))!= -1 ) { 349if (pid == 0 ) { 350return ; 351 } 352if (WIFEXITED (status )|| WIFSIGNALED (status )) { 353DEBUG ("Exited: %d(%d) %d(%d)\n" ,WIFEXITED (status ), 354WEXITSTATUS (status ),WIFSIGNALED (status ),WTERMSIG (status )); 355if (jobs [foreground_job ].group_id == pid ) { 356jobs [foreground_job ].main_exited = true; 357if (jobs [foreground_job ].pipe_exited ) { 358jobs [foreground_job ].occupied = false; 359DEBUG ("Removing foreground: %s\n" , 360jobs [foreground_job ].command ); 361free (jobs [foreground_job ].command ); 362foreground_job = -1 ; 363 } 364 }else if (jobs [foreground_job ].pipe_id == pid ) { 365jobs [foreground_job ].pipe_exited = true; 366if (jobs [foreground_job ].main_exited ) { 367jobs [foreground_job ].occupied = false; 368DEBUG ("Removing foreground: %s\n" , 369jobs [foreground_job ].command ); 370free (jobs [foreground_job ].command ); 371foreground_job = -1 ; 372 } 373 }else { 374for (int i = first_job ;i != -1 ;i = jobs [i ].next ) { 375if (jobs [i ].group_id == pid ) { 376jobs [i ].main_exited = true; 377if (jobs [i ].pipe_exited ) { 378DEBUG ("Removing %d\n" ,jobs [i ].number ); 379remove_job (i ); 380 } 381 }else if (!jobs [i ].pipe_exited && jobs [i ].pipe_id == pid ) { 382jobs [i ].pipe_exited = true; 383if (jobs [i ].main_exited ) { 384DEBUG ("Removing %d\n" ,jobs [i ].number ); 385remove_job (i ); 386 } 387 } 388 } 389 } 390 }else if (WIFSTOPPED (status )) { 391DEBUG ("stopped\n" ); 392 }else { 393DEBUG ("ERROR unexpected wait status\n" ); 394 } 395DEBUG ("PID: %d, status: %d\n" ,pid ,status ); 396 } 397} 398 399int main () { 400signal (SIGINT ,handle_sigint ); 401signal (SIGTSTP ,handle_sigtstp ); 402signal (SIGCHLD ,handle_sigchld ); 403for (int i = 0 ;i < MAX_JOBS ;++ i ) {jobs [i ].occupied = false; } 404while (true) { 405// SIGINT during readline prints a new prompt 406while (sigsetjmp (jump_buf , true)) {putchar ('\n' ); } 407char * raw_input = readline ("# " ); 408handle_finished_jobs (); 409line (raw_input ); 410free (raw_input ); 411while (foreground_job != -1 ) { 412DEBUG ("pausing...\n" ); 413pause (); 414 } 415 } 416}