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>
|
2012-10-27 14:44:39 -04:00
|
|
|
|
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-10-27 14:44:39 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-07-24 12:51:22 -04:00
|
|
|
#include <termios.h>
|
2012-10-27 14:44:39 -04:00
|
|
|
#include <unistd.h>
|
2011-08-27 17:03:39 -04:00
|
|
|
|
2011-11-09 18:03:53 -05:00
|
|
|
int status = 0;
|
|
|
|
|
2012-12-16 14:11:46 -05:00
|
|
|
const char* getenv_safe(const char* name, const char* def = "")
|
2012-10-27 13:23:42 -04:00
|
|
|
{
|
|
|
|
const char* ret = getenv(name);
|
2012-12-16 14:11:46 -05:00
|
|
|
return ret ? ret : def;
|
2012-10-27 13:23:42 -04:00
|
|
|
}
|
|
|
|
|
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-10-27 14:44:39 -04:00
|
|
|
int runcommandline(const char** tokens, bool* exitexec)
|
2012-03-02 12:02:31 -05:00
|
|
|
{
|
|
|
|
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;
|
2012-10-27 14:44:39 -04:00
|
|
|
*exitexec = true;
|
|
|
|
return exitcode;
|
2012-03-02 12:02:31 -05:00
|
|
|
}
|
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-12-09 13:54:01 -05:00
|
|
|
int get_and_run_command(FILE* fp, const char* fpname, bool interactive,
|
|
|
|
bool exit_on_error, bool* exitexec)
|
2011-08-27 17:03:39 -04:00
|
|
|
{
|
2012-10-27 14:44:39 -04:00
|
|
|
int fd = fileno(fp);
|
|
|
|
|
|
|
|
if ( interactive )
|
|
|
|
{
|
|
|
|
unsigned termmode = TERMMODE_UNICODE
|
|
|
|
| TERMMODE_SIGNAL
|
|
|
|
| TERMMODE_UTF8
|
|
|
|
| TERMMODE_LINEBUFFER
|
|
|
|
| TERMMODE_ECHO;
|
2012-11-07 15:20:19 -05:00
|
|
|
settermmode(fd, termmode);
|
2012-12-16 14:11:46 -05:00
|
|
|
const char* print_username = getenv_safe("USERNAME", "root");
|
|
|
|
const char* print_hostname = getenv_safe("HOSTNAME", "sortix");
|
|
|
|
const char* print_dir = getenv_safe("PWD", "?");
|
|
|
|
printf("\e[32m%s@%s \e[36m%s #\e[37m ", print_username, print_hostname,
|
|
|
|
print_dir);
|
2012-10-27 14:44:39 -04:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* command = NULL;
|
|
|
|
static size_t commandlen = 1024;
|
|
|
|
if ( !command )
|
|
|
|
commandlen = 1024,
|
|
|
|
command = (char*) malloc((commandlen+1) * sizeof(char));
|
|
|
|
if ( !command )
|
|
|
|
error(64, errno, "malloc");
|
2011-08-27 17:03:39 -04:00
|
|
|
|
2012-10-27 14:44:39 -04:00
|
|
|
size_t commandused = 0;
|
|
|
|
bool commented = false;
|
|
|
|
bool escaped = false;
|
2011-08-27 17:03:39 -04:00
|
|
|
while (true)
|
|
|
|
{
|
2012-01-22 17:46:41 -05:00
|
|
|
char c;
|
2012-10-27 14:44:39 -04:00
|
|
|
ssize_t bytesread = read(fd, &c, sizeof(c));
|
2012-08-03 09:40:11 -04:00
|
|
|
if ( bytesread < 0 && errno == EINTR )
|
2012-10-27 14:44:39 -04:00
|
|
|
return status;
|
|
|
|
if ( bytesread < 0 )
|
|
|
|
error(64, errno, "read %s", fpname);
|
2012-08-03 09:40:11 -04:00
|
|
|
if ( !bytesread )
|
|
|
|
{
|
2012-10-27 14:44:39 -04:00
|
|
|
if ( !interactive )
|
|
|
|
{
|
|
|
|
*exitexec = true;
|
|
|
|
return status;
|
|
|
|
}
|
2012-08-03 09:40:11 -04:00
|
|
|
if ( getppid() == 1 )
|
|
|
|
{
|
2012-10-27 14:44:39 -04:00
|
|
|
printf("\nType exit to shutdown the system.\n");
|
|
|
|
return status;
|
2012-08-03 09:40:11 -04:00
|
|
|
}
|
2012-10-27 14:44:39 -04:00
|
|
|
printf("exit\n");
|
|
|
|
*exitexec = true;
|
|
|
|
return status;
|
2012-08-03 09:40:11 -04:00
|
|
|
}
|
2012-01-22 17:46:41 -05:00
|
|
|
if ( !c ) { continue; }
|
2012-10-27 14:44:39 -04:00
|
|
|
if ( c == '\n' && !escaped )
|
|
|
|
break;
|
|
|
|
if ( commented )
|
|
|
|
continue;
|
|
|
|
if ( c == '\\' && !escaped )
|
|
|
|
{
|
|
|
|
escaped = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( !escaped )
|
|
|
|
{
|
|
|
|
if ( c == '#' )
|
|
|
|
{
|
|
|
|
commented = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
escaped = false;
|
|
|
|
if ( commandused == commandlen )
|
|
|
|
{
|
|
|
|
size_t newlen = commandlen * 2;
|
|
|
|
size_t newsize = (newlen+1) * sizeof(char);
|
|
|
|
char* newcommand = (char*) realloc(command, newsize);
|
|
|
|
if ( !newcommand )
|
|
|
|
error(64, errno, "realloc");
|
|
|
|
command = newcommand;
|
|
|
|
commandlen = newsize;
|
|
|
|
}
|
2012-01-22 17:46:41 -05:00
|
|
|
command[commandused++] = c;
|
2011-08-27 17:03:39 -04:00
|
|
|
}
|
|
|
|
|
2012-01-22 17:46:41 -05:00
|
|
|
command[commandused] = '\0';
|
|
|
|
|
2012-10-27 14:44:39 -04:00
|
|
|
if ( command[0] == '\0' )
|
|
|
|
return status;
|
2011-08-27 17:03:39 -04:00
|
|
|
|
2012-05-27 08:29:24 -04:00
|
|
|
if ( strchr(command, '=') && !strchr(command, ' ') && !strchr(command, '\t') )
|
2012-04-03 14:16:02 -04:00
|
|
|
{
|
2012-10-27 14:44:39 -04:00
|
|
|
if ( putenv(strdup(command)) )
|
|
|
|
error(1, errno, "putenv");
|
|
|
|
return status = 0;
|
2012-04-03 14:16:02 -04:00
|
|
|
}
|
|
|
|
|
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-10-27 14:44:39 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-27 14:44:39 -04:00
|
|
|
if ( !argv[0] )
|
|
|
|
return status;
|
2011-11-09 18:03:53 -05:00
|
|
|
|
2012-03-02 09:00:11 -05:00
|
|
|
argv[argc] = NULL;
|
2012-10-27 14:44:39 -04:00
|
|
|
status = runcommandline(argv, exitexec);
|
2012-12-09 13:54:01 -05:00
|
|
|
if ( status && exit_on_error )
|
|
|
|
*exitexec = true;
|
2012-10-27 14:44:39 -04:00
|
|
|
return status;
|
2011-08-27 17:03:39 -04:00
|
|
|
}
|
|
|
|
|
2012-11-07 15:20:19 -05:00
|
|
|
void load_argv_variables(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
char varname[sizeof(int) * 3];
|
|
|
|
for ( int i = 0; i < argc; i++ )
|
|
|
|
sprintf(varname, "%i", i),
|
|
|
|
setenv(varname, argv[i], 1);
|
|
|
|
}
|
|
|
|
|
2012-12-09 13:54:01 -05:00
|
|
|
int run(FILE* fp, int argc, char* argv[], const char* name, bool interactive,
|
|
|
|
bool exit_on_error)
|
2012-11-07 15:20:19 -05:00
|
|
|
{
|
|
|
|
load_argv_variables(argc, argv);
|
|
|
|
bool exitexec = false;
|
|
|
|
int exitstatus;
|
|
|
|
do
|
2012-12-09 13:54:01 -05:00
|
|
|
exitstatus = get_and_run_command(fp, name, interactive, exit_on_error,
|
|
|
|
&exitexec);
|
2012-11-07 15:20:19 -05:00
|
|
|
while ( !exitexec );
|
|
|
|
return exitstatus;
|
|
|
|
}
|
|
|
|
|
2012-12-09 13:54:01 -05:00
|
|
|
int run_interactive(int argc, char* argv[], bool exit_on_error)
|
2011-08-27 17:03:39 -04:00
|
|
|
{
|
2012-08-03 09:40:11 -04:00
|
|
|
signal(SIGINT, on_sigint);
|
2012-12-09 13:54:01 -05:00
|
|
|
return run(stdin, argc, argv, "<stdin>", true, exit_on_error);
|
2012-11-07 15:20:19 -05:00
|
|
|
}
|
|
|
|
|
2012-12-09 13:54:01 -05:00
|
|
|
int run_stdin(int argc, char* argv[], bool exit_on_error)
|
2012-11-07 15:20:19 -05:00
|
|
|
{
|
2012-12-09 13:54:01 -05:00
|
|
|
return run(stdin, argc, argv, "<stdin>", false, exit_on_error);
|
2012-11-07 15:20:19 -05:00
|
|
|
}
|
|
|
|
|
2013-01-08 09:04:39 -05:00
|
|
|
int run_string(int argc, char* argv[], const char* str, bool exit_on_error)
|
|
|
|
{
|
|
|
|
// TODO: Implement fmemopen and open_memstream.
|
|
|
|
char unique_ish[64];
|
|
|
|
snprintf(unique_ish, sizeof(unique_ish), "/tmp/shinput.%i", getpid());
|
|
|
|
FILE* fp = fopen(unique_ish, "w");
|
|
|
|
if ( !fp ) { error(0, errno, "write-open: %s", unique_ish); return 1; }
|
|
|
|
if ( fputs(str, fp) == EOF ) { fclose(fp); error(0, errno, "write: %s", unique_ish); return 1; }
|
|
|
|
if ( fputs("\n", fp) == EOF ) { fclose(fp); error(0, errno, "write: %s", unique_ish); return 1; }
|
|
|
|
fclose(fp);
|
|
|
|
fp = fopen(unique_ish, "r");
|
|
|
|
if ( !fp ) { error(0, errno, "read-open: %s", unique_ish); return 1; }
|
|
|
|
int ret = run(fp, argc, argv, "<command-line>", false, exit_on_error);
|
|
|
|
fclose(fp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-09 13:54:01 -05:00
|
|
|
int run_script(const char* path, int argc, char* argv[], bool exit_on_error)
|
2012-11-07 15:20:19 -05:00
|
|
|
{
|
|
|
|
FILE* fp = fopen(path, "r");
|
|
|
|
if ( !fp ) { error(0, errno, "%s", path); return 127; }
|
2012-12-09 13:54:01 -05:00
|
|
|
int ret = run(fp, argc, argv, path, false, exit_on_error);
|
2012-11-07 15:20:19 -05:00
|
|
|
fclose(fp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2012-12-09 13:54:01 -05:00
|
|
|
bool exit_on_error = false;
|
|
|
|
if ( 2 <= argc && !strcmp(argv[1], "-e") )
|
|
|
|
{
|
|
|
|
exit_on_error = true;
|
|
|
|
argc--;
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
argv[i] = argv[i+1];
|
|
|
|
argv[argc] = NULL;
|
|
|
|
}
|
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();
|
2013-01-08 09:04:39 -05:00
|
|
|
if ( 2 <= argc && !strcmp(argv[1], "-c") )
|
|
|
|
return run_string(argc, argv, argv[2], exit_on_error);
|
2012-11-07 15:20:19 -05:00
|
|
|
if ( 1 < argc )
|
2012-12-09 13:54:01 -05:00
|
|
|
return run_script(argv[1], argc-1, argv+1, exit_on_error);
|
2012-11-07 15:20:19 -05:00
|
|
|
if ( isatty(0) )
|
2012-12-09 13:54:01 -05:00
|
|
|
return run_interactive(argc, argv, exit_on_error);
|
|
|
|
return run_stdin(argc, argv, exit_on_error);
|
2011-08-27 17:03:39 -04:00
|
|
|
}
|