2012-03-11 10:57:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-02-28 10:21:06 -05:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
|
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-12-20 14:40:22 -05:00
|
|
|
sh.cpp
|
2013-07-10 09:26:01 -04:00
|
|
|
A simple and hacky Sortix shell.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2012-01-22 17:46:41 -05:00
|
|
|
#include <sys/termmode.h>
|
2014-02-28 10:21:06 -05:00
|
|
|
#include <sys/wait.h>
|
2012-10-27 14:44:39 -04:00
|
|
|
|
2013-10-16 18:25:44 -04:00
|
|
|
#include <dirent.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>
|
2014-02-28 10:21:06 -05:00
|
|
|
#include <inttypes.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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 18:25:44 -04:00
|
|
|
bool matches_simple_pattern(const char* string, const char* pattern)
|
|
|
|
{
|
|
|
|
size_t wildcard_index = strcspn(pattern, "*");
|
|
|
|
if ( !pattern[wildcard_index] )
|
|
|
|
return strcmp(string, pattern) == 0;
|
|
|
|
if ( pattern[0] == '*' && string[0] == '.' )
|
|
|
|
return false;
|
|
|
|
size_t string_length = strlen(string);
|
|
|
|
size_t pattern_length = strlen(pattern);
|
|
|
|
size_t pattern_last = pattern_length - (wildcard_index + 1);
|
|
|
|
return strncmp(string, pattern, wildcard_index) == 0 &&
|
|
|
|
strcmp(string + string_length - pattern_last,
|
|
|
|
pattern + wildcard_index + 1) == 0;
|
|
|
|
}
|
|
|
|
|
2013-06-09 07:44:34 -04:00
|
|
|
int runcommandline(const char** tokens, bool* exitexec, bool interactive)
|
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;
|
2013-06-11 18:59:19 -04:00
|
|
|
pid_t pgid = -1;
|
2012-03-02 12:02:31 -05:00
|
|
|
bool internal;
|
|
|
|
int internalresult;
|
2013-10-16 18:25:44 -04:00
|
|
|
size_t num_tokens = 0;
|
|
|
|
while ( tokens[num_tokens] )
|
|
|
|
num_tokens++;
|
2012-03-02 12:02:31 -05:00
|
|
|
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; }
|
|
|
|
}
|
|
|
|
|
2013-10-16 18:25:44 -04:00
|
|
|
for ( size_t i = cmdstart; i < cmdend; i++ )
|
|
|
|
{
|
|
|
|
const char* pattern = tokens[i];
|
|
|
|
size_t wildcard_pos = strcspn(pattern, "*");
|
|
|
|
if ( !pattern[wildcard_pos] )
|
|
|
|
continue;
|
|
|
|
bool found_slash = false;
|
|
|
|
size_t last_slash = 0;
|
|
|
|
for ( size_t i = 0; i < wildcard_pos; i++ )
|
|
|
|
if ( pattern[i] == '/' )
|
|
|
|
last_slash = i, found_slash = true;
|
|
|
|
size_t match_from = found_slash ? last_slash + 1 : 0;
|
|
|
|
DIR* dir;
|
|
|
|
size_t pattern_prefix = 0;
|
|
|
|
if ( !found_slash )
|
|
|
|
{
|
|
|
|
if ( !(dir = opendir(".")) )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char* dirpath = strdup(pattern);
|
|
|
|
if ( !dirpath )
|
|
|
|
continue;
|
|
|
|
dirpath[last_slash] = '\0';
|
|
|
|
pattern_prefix = last_slash + 1;
|
|
|
|
dir = opendir(dirpath);
|
|
|
|
free(dirpath);
|
|
|
|
if ( !dir )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size_t num_inserted = 0;
|
|
|
|
size_t last_inserted_index = i;
|
|
|
|
while ( struct dirent* entry = readdir(dir) )
|
|
|
|
{
|
|
|
|
if ( !matches_simple_pattern(entry->d_name, pattern + match_from) )
|
|
|
|
continue;
|
|
|
|
// TODO: Memory leak.
|
|
|
|
char* name = (char*) malloc(pattern_prefix + strlen(entry->d_name) + 1);
|
|
|
|
memcpy(name, pattern, pattern_prefix);
|
|
|
|
strcpy(name + pattern_prefix, entry->d_name);
|
|
|
|
if ( !name )
|
|
|
|
continue;
|
|
|
|
if ( num_inserted )
|
|
|
|
{
|
|
|
|
// TODO: Reckless modification of the tokens array.
|
|
|
|
for ( size_t n = num_tokens; n != last_inserted_index; n-- )
|
|
|
|
tokens[n+1] = tokens[n];
|
|
|
|
num_tokens++;
|
|
|
|
cmdend++;
|
|
|
|
}
|
|
|
|
// TODO: Reckless modification of the tokens array.
|
|
|
|
tokens[last_inserted_index = i + num_inserted++] = name;
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2012-03-02 12:02:31 -05:00
|
|
|
cmdnext = cmdend + 1;
|
2012-04-03 14:16:02 -04:00
|
|
|
argv = (char**) (tokens + cmdstart);
|
2012-03-02 12:02:31 -05:00
|
|
|
|
2013-10-01 18:54:27 -04:00
|
|
|
updateenv();
|
|
|
|
char statusstr[32];
|
|
|
|
sprintf(statusstr, "%i", status);
|
|
|
|
setenv("?", statusstr, 1);
|
|
|
|
|
|
|
|
for ( char** argp = argv; *argp; argp++ )
|
|
|
|
{
|
|
|
|
char* arg = *argp;
|
|
|
|
if ( arg[0] != '$' )
|
|
|
|
continue;
|
|
|
|
arg = getenv(arg+1);
|
|
|
|
if ( !arg )
|
|
|
|
arg = (char*) "";
|
|
|
|
*argp = arg;
|
|
|
|
}
|
|
|
|
|
2012-03-02 12:02:31 -05:00
|
|
|
internal = false;
|
|
|
|
internalresult = 0;
|
|
|
|
if ( strcmp(argv[0], "cd") == 0 )
|
|
|
|
{
|
|
|
|
internal = true;
|
2013-10-01 18:20:02 -04:00
|
|
|
const char* newdir = getenv_safe("HOME", "/");
|
2012-03-02 12:02:31 -05:00
|
|
|
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 )
|
|
|
|
{
|
2013-06-11 18:59:19 -04:00
|
|
|
if ( !internal )
|
|
|
|
{
|
|
|
|
if ( pgid == -1 )
|
|
|
|
pgid = childpid;
|
|
|
|
setpgid(childpid, pgid);
|
|
|
|
}
|
|
|
|
|
2012-03-02 12:02:31 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:59:19 -04:00
|
|
|
if ( strcmp(execmode, "&") == 0 )
|
|
|
|
pgid = -1;
|
|
|
|
|
2012-03-02 12:02:31 -05:00
|
|
|
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;
|
2013-06-11 20:26:22 -04:00
|
|
|
tcsetpgrp(0, pgid);
|
2012-09-09 06:21:39 -04:00
|
|
|
if ( !internal && waitpid(childpid, &exitstatus, 0) < 0 )
|
2012-03-02 12:02:31 -05:00
|
|
|
{
|
|
|
|
perror("waitpid");
|
|
|
|
return 127;
|
|
|
|
}
|
2013-06-11 20:26:22 -04:00
|
|
|
tcsetpgrp(0, getpgid(0));
|
2012-03-02 12:02:31 -05:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:59:19 -04:00
|
|
|
setpgid(0, pgid != -1 ? pgid : 0);
|
|
|
|
|
2012-03-02 12:02:31 -05:00
|
|
|
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);
|
2014-09-29 16:31:30 -04:00
|
|
|
// TODO: Is this logic right or wrong?
|
|
|
|
int flags = O_CREAT | O_WRONLY;
|
|
|
|
if ( strcmp(execmode, ">") == 0 )
|
|
|
|
flags |= O_TRUNC;
|
|
|
|
if ( strcmp(execmode, ">>") == 0 )
|
|
|
|
flags |= O_APPEND;
|
2012-03-04 10:50:44 -05:00
|
|
|
if ( open(outputfile, flags, 0666) < 0 )
|
2012-03-02 12:02:31 -05:00
|
|
|
{
|
|
|
|
error(127, errno, "%s", outputfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 08:37:11 -04:00
|
|
|
execvp(argv[0], argv);
|
2014-03-17 15:09:36 -04:00
|
|
|
if ( interactive && errno == ENOENT )
|
2013-06-09 07:44:34 -04:00
|
|
|
{
|
|
|
|
int errno_saved = errno;
|
|
|
|
execlp("command-not-found", "command-not-found", argv[0], NULL);
|
|
|
|
errno = errno_saved;
|
|
|
|
}
|
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);
|
2013-10-02 05:03:07 -04:00
|
|
|
const char* print_username = getlogin();
|
|
|
|
if ( !print_username )
|
|
|
|
print_username = getuid() == 0 ? "root" : "?";
|
2012-12-16 14:11:46 -05:00
|
|
|
const char* print_hostname = getenv_safe("HOSTNAME", "sortix");
|
|
|
|
const char* print_dir = getenv_safe("PWD", "?");
|
2013-10-01 18:20:02 -04:00
|
|
|
const char* home_dir = getenv_safe("HOME", "");
|
2013-04-16 04:30:38 -04:00
|
|
|
size_t home_dir_len = strlen(home_dir);
|
|
|
|
printf("\e[32m");
|
|
|
|
printf("%s", print_username);
|
|
|
|
printf("@");
|
|
|
|
printf("%s", print_hostname);
|
|
|
|
printf(" ");
|
|
|
|
printf("\e[36m");
|
|
|
|
if ( home_dir_len && strncmp(print_dir, home_dir, home_dir_len) == 0 )
|
|
|
|
printf("~%s", print_dir + home_dir_len);
|
|
|
|
else
|
|
|
|
printf("%s", print_dir);
|
|
|
|
printf(" ");
|
|
|
|
printf("#");
|
|
|
|
printf("\e[37m ");
|
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;
|
2014-02-28 10:21:06 -05:00
|
|
|
if ( fd < 0 )
|
2012-08-03 09:40:11 -04:00
|
|
|
{
|
2014-02-28 10:21:06 -05:00
|
|
|
int ic = fgetc(fp);
|
|
|
|
if ( ic == EOF )
|
2012-10-27 14:44:39 -04:00
|
|
|
{
|
2014-02-28 10:21:06 -05:00
|
|
|
if ( ferror(fp) )
|
|
|
|
error(64, errno, "fgetc %s", fpname);
|
|
|
|
if ( commandused )
|
|
|
|
break;
|
|
|
|
return *exitexec = true, status;
|
2012-10-27 14:44:39 -04:00
|
|
|
}
|
2014-02-28 10:21:06 -05:00
|
|
|
c = (char) (unsigned char) ic;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ssize_t bytesread = read(fd, &c, sizeof(c));
|
|
|
|
if ( bytesread < 0 && errno == EINTR )
|
2012-10-27 14:44:39 -04:00
|
|
|
return status;
|
2014-02-28 10:21:06 -05:00
|
|
|
if ( bytesread < 0 )
|
|
|
|
error(64, errno, "read %s", fpname);
|
|
|
|
if ( !bytesread && !interactive )
|
|
|
|
return *exitexec = true, status;
|
|
|
|
if ( !bytesread && interactive )
|
|
|
|
{
|
|
|
|
const char* init_pid_str = getenv("INIT_PID");
|
|
|
|
if ( !init_pid_str )
|
|
|
|
init_pid_str = "1";
|
|
|
|
pid_t init_pid = (pid_t) strtoimax(init_pid_str, NULL, 10);
|
|
|
|
if ( !init_pid )
|
|
|
|
init_pid = 1;
|
|
|
|
if ( getppid() == init_pid )
|
|
|
|
{
|
|
|
|
printf("\nType exit to shutdown the system.\n");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
printf("exit\n");
|
|
|
|
return *exitexec = true, status;
|
2012-08-03 09:40:11 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-28 10:21:06 -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
|
|
|
{
|
2014-01-16 19:14:29 -05:00
|
|
|
const char* key = command;
|
|
|
|
char* equal = strchr(command, '=');
|
|
|
|
*equal = '\0';
|
|
|
|
const char* value = equal + 1;
|
|
|
|
if ( setenv(key, value, 1) < 0 )
|
|
|
|
error(1, errno, "setenv");
|
2012-10-27 14:44:39 -04:00
|
|
|
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;
|
2013-06-09 07:44:34 -04:00
|
|
|
status = runcommandline(argv, exitexec, interactive);
|
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)
|
|
|
|
{
|
2014-02-28 10:21:06 -05:00
|
|
|
FILE* fp = fmemopen((void*) str, strlen(str), "r");
|
|
|
|
if ( !fp ) { error(0, errno, "fmemopen"); return 1; }
|
2013-01-08 09:04:39 -05:00
|
|
|
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;
|
|
|
|
}
|
2013-12-28 15:50:02 -05:00
|
|
|
char pidstr[3 * sizeof(pid_t)];
|
|
|
|
char ppidstr[3 * sizeof(pid_t)];
|
|
|
|
sprintf(pidstr, "%ji", (intmax_t) getpid());
|
|
|
|
sprintf(ppidstr, "%ji", (intmax_t) getppid());
|
2012-04-13 16:41:07 -04:00
|
|
|
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
|
|
|
}
|