2011-11-22 17:26:47 +01:00
|
|
|
#include <sys/wait.h>
|
2012-01-22 23:46:41 +01:00
|
|
|
#include <sys/termmode.h>
|
2011-08-27 23:03:39 +02:00
|
|
|
#include <stdio.h>
|
2011-11-06 23:51:02 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2011-11-10 00:03:53 +01:00
|
|
|
#include <string.h>
|
2011-11-22 17:26:47 +01:00
|
|
|
#include <errno.h>
|
2011-11-26 11:00:45 +01:00
|
|
|
#include <error.h>
|
2011-11-24 17:42:40 +01:00
|
|
|
#include <fcntl.h>
|
2011-08-27 23:03:39 +02:00
|
|
|
|
2011-11-10 00:03:53 +01:00
|
|
|
int status = 0;
|
|
|
|
|
2012-03-02 18:02:31 +01:00
|
|
|
int runcommandline(const char** tokens)
|
|
|
|
{
|
|
|
|
int result = 127;
|
|
|
|
size_t cmdnext = 0;
|
|
|
|
size_t cmdstart;
|
|
|
|
size_t cmdend;
|
|
|
|
bool lastcmd = false;
|
|
|
|
int pipein = 0;
|
|
|
|
int pipeout = 1;
|
|
|
|
int pipeinnext = 0;
|
|
|
|
char* const* argv;
|
|
|
|
size_t cmdlen;
|
|
|
|
const char* execmode;
|
|
|
|
const char* outputfile;
|
|
|
|
pid_t childpid;
|
|
|
|
bool internal;
|
|
|
|
int internalresult;
|
|
|
|
readcmd:
|
|
|
|
cmdstart = cmdnext;
|
|
|
|
for ( cmdend = cmdstart; true; cmdend++ )
|
|
|
|
{
|
|
|
|
const char* token = tokens[cmdend];
|
|
|
|
if ( !token ||
|
|
|
|
strcmp(token, ";") == 0 ||
|
|
|
|
strcmp(token, "&") == 0 ||
|
|
|
|
strcmp(token, "|") == 0 ||
|
|
|
|
strcmp(token, ">") == 0 ||
|
|
|
|
false )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdlen = cmdend - cmdstart;
|
|
|
|
if ( !cmdlen ) { fprintf(stderr, "expected command\n"); goto out; }
|
|
|
|
execmode = tokens[cmdend];
|
|
|
|
if ( !execmode ) { lastcmd = true; execmode = ";"; }
|
|
|
|
tokens[cmdend] = NULL;
|
|
|
|
|
|
|
|
if ( strcmp(execmode, "|") == 0 )
|
|
|
|
{
|
|
|
|
int pipes[2];
|
|
|
|
if ( pipe(pipes) ) { perror("pipe"); goto out; }
|
|
|
|
if ( pipeout != 1 ) { close(pipeout); } pipeout = pipes[1];
|
|
|
|
if ( pipeinnext != 0 ) { close(pipeinnext); } pipeinnext = pipes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
outputfile = NULL;
|
|
|
|
if ( strcmp(execmode, ">") == 0 )
|
|
|
|
{
|
|
|
|
outputfile = tokens[cmdend+1];
|
|
|
|
if ( !outputfile ) { fprintf(stderr, "expected filename\n"); goto out; }
|
|
|
|
const char* nexttok = tokens[cmdend+2];
|
|
|
|
if ( nexttok ) { fprintf(stderr, "too many filenames\n"); goto out; }
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdnext = cmdend + 1;
|
|
|
|
argv = (char* const*) (tokens + cmdstart);
|
|
|
|
|
|
|
|
internal = false;
|
|
|
|
internalresult = 0;
|
|
|
|
if ( strcmp(argv[0], "cd") == 0 )
|
|
|
|
{
|
|
|
|
internal = true;
|
|
|
|
const char* newdir = "/";
|
|
|
|
if ( argv[1] ) { newdir = argv[1]; }
|
|
|
|
if ( chdir(newdir) )
|
|
|
|
{
|
|
|
|
error(0, errno, "cd: %s", newdir);
|
|
|
|
internalresult = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( strcmp(argv[0], "exit") == 0 )
|
|
|
|
{
|
|
|
|
int exitcode = argv[1] ? atoi(argv[1]) : 0;
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
childpid = internal ? getpid() : fork();
|
|
|
|
if ( childpid < 0 ) { perror("fork"); goto out; }
|
|
|
|
if ( childpid )
|
|
|
|
{
|
|
|
|
if ( pipein != 0 ) { close(pipein); pipein = 0; }
|
|
|
|
if ( pipeout != 1 ) { close(pipeout); pipeout = 1; }
|
|
|
|
if ( pipeinnext != 0 ) { pipein = pipeinnext; pipeinnext = 0; }
|
|
|
|
|
|
|
|
if ( strcmp(execmode, "&") == 0 && !tokens[cmdnext] )
|
|
|
|
{
|
|
|
|
result = 0; goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strcmp(execmode, "&") == 0 || strcmp(execmode, "|") == 0 )
|
|
|
|
{
|
|
|
|
goto readcmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int status = internalresult;
|
|
|
|
if ( !internal && waitpid(childpid, &status, 0) < 0 )
|
|
|
|
{
|
|
|
|
perror("waitpid");
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strcmp(execmode, ";") == 0 && tokens[cmdnext] && !lastcmd )
|
|
|
|
{
|
|
|
|
goto readcmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = status;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( pipeinnext != 0 ) { close(pipeinnext); }
|
|
|
|
|
|
|
|
if ( pipein != 0 )
|
|
|
|
{
|
|
|
|
close(0);
|
|
|
|
dup(pipein);
|
|
|
|
close(pipein);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( pipeout != 1 )
|
|
|
|
{
|
|
|
|
close(1);
|
|
|
|
dup(pipeout);
|
|
|
|
close(pipeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( outputfile )
|
|
|
|
{
|
|
|
|
close(1);
|
|
|
|
if ( open(outputfile, O_CREAT | O_WRONLY | O_TRUNC | O_APPEND) < 0 )
|
|
|
|
{
|
|
|
|
error(127, errno, "%s", outputfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
execv(argv[0], argv);
|
|
|
|
error(127, errno, "%s", argv[0]);
|
|
|
|
return 127;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if ( pipein != 0 ) { close(pipein); }
|
|
|
|
if ( pipeout != 1 ) { close(pipeout); }
|
|
|
|
if ( pipeinnext != 0 ) { close(pipeout); }
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-08-27 23:03:39 +02:00
|
|
|
void command()
|
|
|
|
{
|
2012-01-22 23:46:41 +01:00
|
|
|
unsigned termmode = TERMMODE_UNICODE
|
|
|
|
| TERMMODE_SIGNAL
|
|
|
|
| TERMMODE_UTF8
|
|
|
|
| TERMMODE_LINEBUFFER
|
|
|
|
| TERMMODE_ECHO;
|
|
|
|
settermmode(0, termmode);
|
|
|
|
|
2011-11-21 12:19:57 +01:00
|
|
|
const size_t CWD_SIZE = 512;
|
|
|
|
char cwd[CWD_SIZE];
|
|
|
|
const char* wd = getcwd(cwd, CWD_SIZE);
|
|
|
|
if ( !wd ) { wd = "?"; }
|
|
|
|
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-24 04:08:10 +01:00
|
|
|
printf("root@sortix %s # ", wd);
|
|
|
|
fflush(stdout);
|
2011-08-27 23:03:39 +02:00
|
|
|
|
|
|
|
const size_t commandsize = 128;
|
|
|
|
char command[commandsize + 1];
|
|
|
|
size_t commandused = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2012-01-22 23:46:41 +01:00
|
|
|
char c;
|
|
|
|
ssize_t bytesread = read(1, &c, sizeof(c));
|
|
|
|
if ( bytesread < 0 ) { error(64, errno, "read stdin"); }
|
|
|
|
if ( !bytesread ) { break; }
|
|
|
|
if ( !c ) { continue; }
|
|
|
|
if ( c == '\n' ) { break; }
|
|
|
|
if ( commandsize <= commandused ) { continue; }
|
|
|
|
command[commandused++] = c;
|
2011-08-27 23:03:39 +02:00
|
|
|
}
|
|
|
|
|
2012-01-22 23:46:41 +01:00
|
|
|
command[commandused] = '\0';
|
|
|
|
|
2011-08-27 23:03:39 +02:00
|
|
|
if ( command[0] == '\0' ) { return; }
|
|
|
|
|
2012-01-22 23:46:41 +01:00
|
|
|
if ( strcmp(command, "$?") == 0 ) { printf("%u\n", status); status = 0; return; }
|
2012-03-02 15:00:11 +01:00
|
|
|
if ( strcmp(command, "$$") == 0 ) { printf("%u\n", getpid()); status = 0; return; }
|
|
|
|
if ( strcmp(command, "$PPID") == 0 ) { printf("%u\n", getppid()); status = 0; return; }
|
2011-09-21 20:52:29 +02:00
|
|
|
|
2011-11-09 23:18:26 +01:00
|
|
|
int argc = 0;
|
|
|
|
const char* argv[256];
|
2011-11-10 00:03:53 +01:00
|
|
|
argv[0] = NULL;
|
2011-11-09 23:18:26 +01:00
|
|
|
|
2011-11-10 00:03:53 +01:00
|
|
|
bool lastwasspace = true;
|
2011-11-09 23:18:26 +01:00
|
|
|
for ( size_t i = 0; i <= commandused; i++ )
|
|
|
|
{
|
|
|
|
switch ( command[i] )
|
|
|
|
{
|
2011-11-26 13:27:15 +01:00
|
|
|
case '\0':
|
2011-11-09 23:18:26 +01:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
command[i] = 0;
|
|
|
|
lastwasspace = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ( lastwasspace ) { argv[argc++] = command + i; }
|
|
|
|
lastwasspace = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-10 00:03:53 +01:00
|
|
|
if ( !argv[0] ) { return; }
|
|
|
|
|
2012-03-02 15:00:11 +01:00
|
|
|
argv[argc] = NULL;
|
2012-03-02 18:02:31 +01:00
|
|
|
status = runcommandline(argv);
|
|
|
|
return;
|
2011-08-27 23:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
while ( true ) { command(); }
|
|
|
|
}
|
|
|
|
|