2012-03-11 10:57:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This program is free software: you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
more details.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
mxsh.cpp
|
|
|
|
A simple and hacky Sortix shell.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2011-11-22 11:26:47 -05:00
|
|
|
#include <sys/wait.h>
|
2012-01-22 17:46:41 -05:00
|
|
|
#include <sys/termmode.h>
|
2011-08-27 17:03:39 -04:00
|
|
|
#include <stdio.h>
|
2011-11-06 17:51:02 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2011-11-09 18:03:53 -05:00
|
|
|
#include <string.h>
|
2011-11-22 11:26:47 -05:00
|
|
|
#include <errno.h>
|
2011-11-26 05:00:45 -05:00
|
|
|
#include <error.h>
|
2011-11-24 11:42:40 -05:00
|
|
|
#include <fcntl.h>
|
2012-08-03 09:40:11 -04:00
|
|
|
#include <signal.h>
|
2012-07-24 12:51:22 -04:00
|
|
|
#include <termios.h>
|
2011-08-27 17:03:39 -04:00
|
|
|
|
2011-11-09 18:03:53 -05:00
|
|
|
int status = 0;
|
|
|
|
|
2012-10-27 13:23:42 -04:00
|
|
|
const char* getenv_safe(const char* name)
|
|
|
|
{
|
|
|
|
const char* ret = getenv(name);
|
|
|
|
return ret ? ret : "";
|
|
|
|
}
|
|
|
|
|
2012-09-08 14:55:43 -04:00
|
|
|
void on_sigint(int /*signum*/)
|
2012-08-03 09:40:11 -04:00
|
|
|
{
|
|
|
|
printf("^C\n");
|
|
|
|
}
|
|
|
|
|
2012-04-13 17:01:04 -04:00
|
|
|
void updatepwd()
|
|
|
|
{
|
|
|
|
const size_t CWD_SIZE = 512;
|
|
|
|
char cwd[CWD_SIZE];
|
|
|
|
const char* wd = getcwd(cwd, CWD_SIZE);
|
|
|
|
if ( !wd ) { wd = "?"; }
|
|
|
|
setenv("PWD", wd, 1);
|
|
|
|
}
|
|
|
|
|
2012-07-24 12:51:22 -04:00
|
|
|
void updateenv()
|
|
|
|
{
|
|
|
|
char str[128];
|
|
|
|
struct winsize ws;
|
|
|
|
if ( tcgetwinsize(0, &ws) == 0 )
|
|
|
|
{
|
|
|
|
sprintf(str, "%zu", ws.ws_col);
|
|
|
|
setenv("COLUMNS", str, 1);
|
|
|
|
sprintf(str, "%zu", ws.ws_row);
|
|
|
|
setenv("LINES", str, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-02 12:02:31 -05: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;
|
2012-04-03 14:16:02 -04:00
|
|
|
char** argv;
|
2012-03-02 12:02:31 -05:00
|
|
|
size_t cmdlen;
|
|
|
|
const char* execmode;
|
|
|
|
const char* outputfile;
|
|
|
|
pid_t childpid;
|
|
|
|
bool internal;
|
|
|
|
int internalresult;
|
|
|
|
readcmd:
|
2012-09-08 13:25:13 -04:00
|
|
|
// Collect any pending zombie processes.
|
|
|
|
while ( 0 < waitpid(-1, NULL, WNOHANG) );
|
|
|
|
|
2012-03-02 12:02:31 -05:00
|
|
|
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 ||
|
2012-03-04 10:50:44 -05:00
|
|
|
strcmp(token, ">>") == 0 ||
|
2012-03-02 12:02:31 -05:00
|
|
|
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;
|
2012-03-04 10:50:44 -05:00
|
|
|
if ( strcmp(execmode, ">") == 0 || strcmp(execmode, ">>") == 0 )
|
2012-03-02 12:02:31 -05:00
|
|
|
{
|
|
|
|
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;
|
2012-04-03 14:16:02 -04:00
|
|
|
argv = (char**) (tokens + cmdstart);
|
2012-03-02 12:02:31 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2012-04-13 17:01:04 -04:00
|
|
|
updatepwd();
|
2012-03-02 12:02:31 -05:00
|
|
|
}
|
|
|
|
if ( strcmp(argv[0], "exit") == 0 )
|
|
|
|
{
|
|
|
|
int exitcode = argv[1] ? atoi(argv[1]) : 0;
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
2012-04-03 14:16:02 -04:00
|
|
|
if ( strcmp(argv[0], "unset") == 0 )
|
|
|
|
{
|
|
|
|
internal = true;
|
|
|
|
unsetenv(argv[1] ? argv[1] : "");
|
|
|
|
}
|
|
|
|
if ( strcmp(argv[0], "clearenv") == 0 )
|
|
|
|
{
|
|
|
|
internal = true;
|
|
|
|
clearenv();
|
|
|
|
}
|
2012-03-02 12:02:31 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-04-13 16:41:07 -04:00
|
|
|
status = internalresult;
|
2012-09-09 06:21:39 -04:00
|
|
|
int exitstatus;
|
|
|
|
if ( !internal && waitpid(childpid, &exitstatus, 0) < 0 )
|
2012-03-02 12:02:31 -05:00
|
|
|
{
|
|
|
|
perror("waitpid");
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
2012-09-09 06:21:39 -04:00
|
|
|
// TODO: HACK: Most signals can't kill processes yet.
|
|
|
|
if ( WEXITSTATUS(exitstatus) == 128 + SIGINT )
|
|
|
|
printf("^C\n");
|
|
|
|
if ( WTERMSIG(status) == SIGKILL )
|
|
|
|
printf("Killed\n");
|
|
|
|
|
|
|
|
status = WEXITSTATUS(exitstatus);
|
|
|
|
|
2012-03-02 12:02:31 -05:00
|
|
|
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);
|
2012-03-04 10:50:44 -05:00
|
|
|
int flags = O_CREAT | O_WRONLY | O_APPEND;
|
|
|
|
if ( strcmp(execmode, ">") == 0 ) { flags |= O_TRUNC; }
|
|
|
|
if ( open(outputfile, flags, 0666) < 0 )
|
2012-03-02 12:02:31 -05:00
|
|
|
{
|
|
|
|
error(127, errno, "%s", outputfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 12:51:22 -04:00
|
|
|
updateenv();
|
2012-04-13 16:41:07 -04:00
|
|
|
char statusstr[32];
|
|
|
|
sprintf(statusstr, "%i", status);
|
|
|
|
setenv("?", statusstr, 1);
|
|
|
|
|
2012-04-03 14:16:02 -04:00
|
|
|
for ( char** argp = argv; *argp; argp++ )
|
|
|
|
{
|
|
|
|
char* arg = *argp;
|
|
|
|
if ( arg[0] != '$' ) { continue; }
|
|
|
|
arg = getenv(arg+1);
|
2012-09-08 14:55:43 -04:00
|
|
|
if ( !arg ) { arg = (char*) ""; }
|
2012-04-03 14:16:02 -04:00
|
|
|
*argp = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !strcmp(argv[0], "env") )
|
|
|
|
{
|
|
|
|
for ( size_t i = 0; i < envlength(); i++ )
|
|
|
|
{
|
|
|
|
printf("%s\n", getenvindexed(i));
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
2012-03-02 12:02:31 -05:00
|
|
|
|
2012-04-29 08:37:11 -04:00
|
|
|
execvp(argv[0], argv);
|
2012-03-02 12:02:31 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-03 09:40:11 -04:00
|
|
|
void get_and_run_command()
|
2011-08-27 17:03:39 -04:00
|
|
|
{
|
2012-01-22 17:46:41 -05:00
|
|
|
unsigned termmode = TERMMODE_UNICODE
|
|
|
|
| TERMMODE_SIGNAL
|
|
|
|
| TERMMODE_UTF8
|
|
|
|
| TERMMODE_LINEBUFFER
|
|
|
|
| TERMMODE_ECHO;
|
|
|
|
settermmode(0, termmode);
|
|
|
|
|
2012-10-27 13:23:42 -04:00
|
|
|
printf("root@sortix %s # ", getenv_safe("PWD"));
|
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-23 22:08:10 -05:00
|
|
|
fflush(stdout);
|
2011-08-27 17:03:39 -04:00
|
|
|
|
2012-08-05 10:17:10 -04:00
|
|
|
const size_t commandsize = 1024;
|
2011-08-27 17:03:39 -04:00
|
|
|
char command[commandsize + 1];
|
|
|
|
size_t commandused = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2012-01-22 17:46:41 -05:00
|
|
|
char c;
|
|
|
|
ssize_t bytesread = read(1, &c, sizeof(c));
|
2012-08-03 09:40:11 -04:00
|
|
|
if ( bytesread < 0 && errno == EINTR )
|
|
|
|
return;
|
2012-01-22 17:46:41 -05:00
|
|
|
if ( bytesread < 0 ) { error(64, errno, "read stdin"); }
|
2012-08-03 09:40:11 -04:00
|
|
|
if ( !bytesread )
|
|
|
|
{
|
|
|
|
if ( getppid() == 1 )
|
|
|
|
printf("\nType exit to shutdown the system.\n");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("exit\n");
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-01-22 17:46:41 -05:00
|
|
|
if ( !c ) { continue; }
|
|
|
|
if ( c == '\n' ) { break; }
|
|
|
|
if ( commandsize <= commandused ) { continue; }
|
|
|
|
command[commandused++] = c;
|
2011-08-27 17:03:39 -04:00
|
|
|
}
|
|
|
|
|
2012-01-22 17:46:41 -05:00
|
|
|
command[commandused] = '\0';
|
|
|
|
|
2011-08-27 17:03:39 -04:00
|
|
|
if ( command[0] == '\0' ) { return; }
|
|
|
|
|
2012-05-27 08:29:24 -04:00
|
|
|
if ( strchr(command, '=') && !strchr(command, ' ') && !strchr(command, '\t') )
|
2012-04-03 14:16:02 -04:00
|
|
|
{
|
|
|
|
if ( putenv(strdup(command)) ) { perror("putenv"); status = 1; return; }
|
|
|
|
status = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-09 17:18:26 -05:00
|
|
|
int argc = 0;
|
2013-03-18 14:04:34 -04:00
|
|
|
const size_t ARGV_MAX_LENGTH = 2048;
|
|
|
|
const char* argv[ARGV_MAX_LENGTH];
|
2011-11-09 18:03:53 -05:00
|
|
|
argv[0] = NULL;
|
2011-11-09 17:18:26 -05:00
|
|
|
|
2011-11-09 18:03:53 -05:00
|
|
|
bool lastwasspace = true;
|
2012-09-09 15:03:02 -04:00
|
|
|
bool escaped = false;
|
2011-11-09 17:18:26 -05:00
|
|
|
for ( size_t i = 0; i <= commandused; i++ )
|
|
|
|
{
|
|
|
|
switch ( command[i] )
|
|
|
|
{
|
2012-09-09 15:03:02 -04:00
|
|
|
case '\\':
|
|
|
|
if ( !escaped )
|
|
|
|
{
|
|
|
|
memmove(command + i, command + i + 1, commandused+1 - (i-1));
|
|
|
|
i--;
|
|
|
|
commandused--;
|
|
|
|
escaped = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-26 07:27:15 -05:00
|
|
|
case '\0':
|
2011-11-09 17:18:26 -05:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
2012-09-09 15:03:02 -04:00
|
|
|
if ( !command[i] || !escaped )
|
|
|
|
{
|
|
|
|
command[i] = 0;
|
|
|
|
lastwasspace = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-09 17:18:26 -05:00
|
|
|
default:
|
2012-09-09 15:03:02 -04:00
|
|
|
escaped = false;
|
2013-03-18 14:04:34 -04:00
|
|
|
if ( lastwasspace )
|
|
|
|
{
|
|
|
|
if ( argc == ARGV_MAX_LENGTH )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "argv max length of %zu entries hit!\n",
|
|
|
|
ARGV_MAX_LENGTH);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
argv[argc++] = command + i;
|
|
|
|
}
|
2011-11-09 17:18:26 -05:00
|
|
|
lastwasspace = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-09 18:03:53 -05:00
|
|
|
if ( !argv[0] ) { return; }
|
|
|
|
|
2012-03-02 09:00:11 -05:00
|
|
|
argv[argc] = NULL;
|
2012-03-02 12:02:31 -05:00
|
|
|
status = runcommandline(argv);
|
|
|
|
return;
|
2011-08-27 17:03:39 -04:00
|
|
|
}
|
|
|
|
|
2012-09-08 14:55:43 -04:00
|
|
|
int main(int /*argc*/, char* argv[])
|
2011-08-27 17:03:39 -04:00
|
|
|
{
|
2012-08-03 09:40:11 -04:00
|
|
|
signal(SIGINT, on_sigint);
|
2012-04-13 16:41:07 -04:00
|
|
|
char pidstr[32];
|
|
|
|
char ppidstr[32];
|
|
|
|
sprintf(pidstr, "%i", getpid());
|
|
|
|
sprintf(ppidstr, "%i", getppid());
|
|
|
|
setenv("SHELL", argv[0], 1);
|
|
|
|
setenv("$", pidstr, 1);
|
|
|
|
setenv("PPID", ppidstr, 1);
|
|
|
|
setenv("?", "0", 1);
|
2012-04-13 17:01:04 -04:00
|
|
|
updatepwd();
|
2012-08-03 09:40:11 -04:00
|
|
|
while ( true ) { get_and_run_command(); }
|
2011-08-27 17:03:39 -04:00
|
|
|
}
|