2014-06-08 11:21:23 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2014-06-08 10:13:29 +04:00
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2013-2014 Kevin Lange
|
2014-06-08 11:21:23 +04:00
|
|
|
*
|
2011-12-07 05:46:35 +04:00
|
|
|
* E-Shell
|
|
|
|
*
|
2012-07-07 08:08:28 +04:00
|
|
|
* This is the "experimental shell". It provides
|
|
|
|
* a somewhat unix-like shell environment, but does
|
|
|
|
* not include a parser any advanced functionality.
|
|
|
|
* It simply cuts its input into arguments and executes
|
|
|
|
* programs.
|
2011-12-07 05:46:35 +04:00
|
|
|
*/
|
|
|
|
|
2013-05-29 09:00:27 +04:00
|
|
|
#define _XOPEN_SOURCE
|
|
|
|
|
2011-12-07 05:46:35 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2012-01-25 10:19:52 +04:00
|
|
|
#include <time.h>
|
2012-10-07 10:48:46 +04:00
|
|
|
#include <dirent.h>
|
2013-05-06 02:00:24 +04:00
|
|
|
#include <signal.h>
|
2013-05-29 09:00:27 +04:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <termios.h>
|
2014-04-29 11:28:41 +04:00
|
|
|
#include <errno.h>
|
2011-12-07 05:46:35 +04:00
|
|
|
|
2013-05-23 08:59:28 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/wait.h>
|
2013-05-29 09:00:27 +04:00
|
|
|
#include <sys/utsname.h>
|
2013-05-23 08:59:28 +04:00
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
#include "lib/list.h"
|
2012-10-09 09:33:46 +04:00
|
|
|
#include "lib/kbd.h"
|
2015-04-04 22:33:30 +03:00
|
|
|
#include "lib/rline.h"
|
2012-10-07 10:01:39 +04:00
|
|
|
|
2014-05-31 03:23:40 +04:00
|
|
|
#define PIPE_TOKEN "\xFF\xFFPIPE\xFF\xFF"
|
|
|
|
|
2012-10-08 07:46:35 +04:00
|
|
|
/* A shell command is like a C program */
|
2012-10-07 10:01:39 +04:00
|
|
|
typedef uint32_t(*shell_command_t) (int argc, char ** argv);
|
2012-10-08 07:46:35 +04:00
|
|
|
|
|
|
|
/* We have a static array that fits a certain number of them. */
|
|
|
|
#define SHELL_COMMANDS 512
|
|
|
|
char * shell_commands[SHELL_COMMANDS]; /* Command names */
|
|
|
|
shell_command_t shell_pointers[SHELL_COMMANDS]; /* Command functions */
|
|
|
|
|
|
|
|
/* This is the number of actual commands installed */
|
2012-10-07 10:01:39 +04:00
|
|
|
uint32_t shell_commands_len = 0;
|
|
|
|
|
2012-10-08 07:46:35 +04:00
|
|
|
/* We also support history through a circular buffer. */
|
2012-10-07 10:01:39 +04:00
|
|
|
#define SHELL_HISTORY_ENTRIES 128
|
|
|
|
char * shell_history[SHELL_HISTORY_ENTRIES];
|
2013-05-29 09:00:27 +04:00
|
|
|
int shell_history_count = 0;
|
|
|
|
int shell_history_offset = 0;
|
|
|
|
int shell_scroll = 0;
|
2012-10-07 10:01:39 +04:00
|
|
|
char shell_temp[1024];
|
|
|
|
|
2013-03-28 11:12:48 +04:00
|
|
|
int shell_interactive = 1;
|
2013-04-14 07:21:40 +04:00
|
|
|
int shell_force_raw = 0;
|
2013-03-28 11:12:48 +04:00
|
|
|
|
2012-10-08 07:46:35 +04:00
|
|
|
int pid; /* Process ID of the shell */
|
2012-10-07 10:01:39 +04:00
|
|
|
|
2013-05-29 09:00:27 +04:00
|
|
|
char * shell_history_prev(int item);
|
2012-10-07 20:34:10 +04:00
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
void shell_history_insert(char * str) {
|
2012-10-08 02:58:30 +04:00
|
|
|
if (str[strlen(str)-1] == '\n') {
|
|
|
|
str[strlen(str)-1] = '\0';
|
|
|
|
}
|
2012-10-07 10:01:39 +04:00
|
|
|
if (shell_history_count) {
|
2012-10-07 20:34:10 +04:00
|
|
|
if (!strcmp(str, shell_history_prev(1))) {
|
2012-10-07 10:01:39 +04:00
|
|
|
free(str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shell_history_count == SHELL_HISTORY_ENTRIES) {
|
|
|
|
free(shell_history[shell_history_offset]);
|
|
|
|
shell_history[shell_history_offset] = str;
|
|
|
|
shell_history_offset = (shell_history_offset + 1) % SHELL_HISTORY_ENTRIES;
|
|
|
|
} else {
|
|
|
|
shell_history[shell_history_count] = str;
|
|
|
|
shell_history_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 23:22:39 +04:00
|
|
|
void shell_history_append_line(char * str) {
|
|
|
|
if (shell_history_count) {
|
|
|
|
char ** s = &shell_history[(shell_history_count - 1 + shell_history_offset) % SHELL_HISTORY_ENTRIES];
|
|
|
|
char * c = malloc(strlen(*s) + strlen(str) + 2);
|
|
|
|
sprintf(c, "%s\n%s", *s, str);
|
|
|
|
if (c[strlen(c)-1] == '\n') {
|
|
|
|
c[strlen(c)-1] = '\0';
|
|
|
|
}
|
|
|
|
free(*s);
|
|
|
|
*s = c;
|
|
|
|
} else {
|
|
|
|
/* wat */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 09:00:27 +04:00
|
|
|
char * shell_history_get(int item) {
|
2012-10-07 10:01:39 +04:00
|
|
|
return shell_history[(item + shell_history_offset) % SHELL_HISTORY_ENTRIES];
|
|
|
|
}
|
|
|
|
|
2013-05-29 09:00:27 +04:00
|
|
|
char * shell_history_prev(int item) {
|
2012-10-07 10:01:39 +04:00
|
|
|
return shell_history_get(shell_history_count - item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void shell_install_command(char * name, shell_command_t func) {
|
|
|
|
if (shell_commands_len == SHELL_COMMANDS) {
|
|
|
|
fprintf(stderr, "Ran out of space for static shell commands. The maximum number of commands is %d\n", SHELL_COMMANDS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
shell_commands[shell_commands_len] = name;
|
|
|
|
shell_pointers[shell_commands_len] = func;
|
|
|
|
shell_commands_len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
shell_command_t shell_find(char * str) {
|
|
|
|
for (uint32_t i = 0; i < shell_commands_len; ++i) {
|
|
|
|
if (!strcmp(str, shell_commands[i])) {
|
|
|
|
return shell_pointers[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-29 09:00:27 +04:00
|
|
|
struct termios old;
|
|
|
|
|
|
|
|
void set_unbuffered() {
|
|
|
|
tcgetattr(fileno(stdin), &old);
|
|
|
|
struct termios new = old;
|
|
|
|
new.c_lflag &= (~ICANON & ~ECHO);
|
|
|
|
tcsetattr(fileno(stdin), TCSAFLUSH, &new);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_buffered() {
|
|
|
|
tcsetattr(fileno(stdin), TCSAFLUSH, &old);
|
|
|
|
}
|
2012-10-07 10:01:39 +04:00
|
|
|
|
|
|
|
void install_commands();
|
|
|
|
|
|
|
|
/* Maximum command length */
|
2012-01-28 04:04:39 +04:00
|
|
|
#define LINE_LEN 4096
|
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
/* Current working directory */
|
2012-01-25 10:19:52 +04:00
|
|
|
char cwd[1024] = {'/',0};
|
2012-10-07 10:01:39 +04:00
|
|
|
|
|
|
|
/* Username */
|
2012-01-28 04:04:39 +04:00
|
|
|
char username[1024];
|
2012-10-07 10:01:39 +04:00
|
|
|
|
|
|
|
/* Hostname for prompt */
|
2012-01-30 22:10:53 +04:00
|
|
|
char _hostname[256];
|
2012-01-28 04:04:39 +04:00
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
/* function to update the cached username */
|
2013-05-29 09:00:27 +04:00
|
|
|
void getuser() {
|
2014-08-27 09:12:33 +04:00
|
|
|
char * tmp = getenv("USER");
|
|
|
|
if (tmp) {
|
|
|
|
strcpy(username, tmp);
|
|
|
|
} else {
|
|
|
|
sprintf(username, "%d", getuid());
|
2012-01-28 04:04:39 +04:00
|
|
|
}
|
|
|
|
}
|
2012-01-25 10:19:52 +04:00
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
/* function to update the cached hostname */
|
2013-05-29 09:00:27 +04:00
|
|
|
void gethost() {
|
|
|
|
struct utsname buf;
|
|
|
|
|
|
|
|
uname(&buf);
|
|
|
|
|
|
|
|
int len = strlen(buf.nodename);
|
|
|
|
memcpy(_hostname, buf.nodename, len+1);
|
2012-01-30 22:10:53 +04:00
|
|
|
}
|
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
/* Draw the user prompt */
|
2012-01-26 00:57:27 +04:00
|
|
|
void draw_prompt(int ret) {
|
2012-10-07 10:01:39 +04:00
|
|
|
/* Get the time */
|
2012-01-25 10:19:52 +04:00
|
|
|
struct tm * timeinfo;
|
|
|
|
struct timeval now;
|
2013-05-23 08:59:28 +04:00
|
|
|
gettimeofday(&now, NULL); //time(NULL);
|
2012-01-25 10:19:52 +04:00
|
|
|
timeinfo = localtime((time_t *)&now.tv_sec);
|
2012-10-07 10:01:39 +04:00
|
|
|
|
|
|
|
/* Format the date and time for prompt display */
|
2012-01-25 10:19:52 +04:00
|
|
|
char date_buffer[80];
|
|
|
|
strftime(date_buffer, 80, "%m/%d", timeinfo);
|
|
|
|
char time_buffer[80];
|
|
|
|
strftime(time_buffer, 80, "%H:%M:%S", timeinfo);
|
2012-10-07 10:01:39 +04:00
|
|
|
|
|
|
|
/* Print the working directory in there, too */
|
2014-06-08 11:21:23 +04:00
|
|
|
getcwd(cwd, 512);
|
|
|
|
char _cwd[512];
|
|
|
|
strncpy(_cwd, cwd, 512);
|
|
|
|
|
|
|
|
char * home = getenv("HOME");
|
|
|
|
if (home && strstr(cwd, home) == cwd) {
|
|
|
|
char * c = cwd + strlen(home);
|
|
|
|
if (*c == '/' || *c == 0) {
|
|
|
|
sprintf(_cwd, "~%s", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-08 12:33:40 +04:00
|
|
|
/* Print the prompt. */
|
|
|
|
printf("\033]1;%s@%s:%s\007", username, _hostname, _cwd);
|
|
|
|
printf("\033[s\033[400C\033[16D\033[1m\033[38;5;59m[\033[38;5;173m%s \033[38;5;167m%s\033[38;5;59m]\033[u\033[38;5;221m%s\033[38;5;59m@\033[38;5;81m%s ",
|
|
|
|
date_buffer, time_buffer,
|
|
|
|
username, _hostname);
|
|
|
|
if (ret != 0) {
|
|
|
|
printf("\033[38;5;167m%d ", ret);
|
|
|
|
}
|
|
|
|
|
2014-06-08 11:21:23 +04:00
|
|
|
printf("\033[0m%s%s\033[0m ", _cwd, getuid() == 0 ? "\033[1;38;5;196m#" : "\033[1;38;5;47m$");
|
2012-01-25 10:19:52 +04:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:40:44 +04:00
|
|
|
uint32_t child = 0;
|
|
|
|
|
2013-05-06 02:00:24 +04:00
|
|
|
void sig_pass(int sig) {
|
2012-10-07 10:01:39 +04:00
|
|
|
/* Interrupt handler */
|
2012-02-08 12:40:44 +04:00
|
|
|
if (child) {
|
2013-05-06 02:00:24 +04:00
|
|
|
kill(child, sig);
|
2012-02-08 12:40:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
void redraw_prompt_func(rline_context_t * context) {
|
|
|
|
draw_prompt(0);
|
|
|
|
}
|
|
|
|
|
2012-10-07 22:01:00 +04:00
|
|
|
void draw_prompt_c() {
|
|
|
|
printf("> ");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
void redraw_prompt_func_c(rline_context_t * context) {
|
|
|
|
draw_prompt_c();
|
|
|
|
}
|
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
void tab_complete_func(rline_context_t * context) {
|
|
|
|
char buf[1024];
|
|
|
|
char * pch;
|
|
|
|
char * cmd;
|
|
|
|
char * save;
|
|
|
|
|
|
|
|
memcpy(buf, context->buffer, 1024);
|
|
|
|
|
|
|
|
pch = strtok_r(buf, " ", &save);
|
|
|
|
cmd = pch;
|
|
|
|
|
|
|
|
char * argv[1024];
|
|
|
|
int argc = 0;
|
|
|
|
|
|
|
|
if (!cmd) {
|
|
|
|
argv[0] = "";
|
|
|
|
argc = 1;
|
|
|
|
} else {
|
|
|
|
while (pch != NULL) {
|
2012-10-07 10:48:46 +04:00
|
|
|
argv[argc] = (char *)pch;
|
2012-10-07 10:01:39 +04:00
|
|
|
++argc;
|
|
|
|
pch = strtok_r(NULL, " ", &save);
|
2011-12-07 05:46:35 +04:00
|
|
|
}
|
2012-10-07 10:01:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
argv[argc] = NULL;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
if (context->buffer[strlen(context->buffer) - 1] == ' ' || argc == 0) {
|
2012-10-07 10:48:46 +04:00
|
|
|
if (!context->tabbed) {
|
|
|
|
context->tabbed = 1;
|
|
|
|
return;
|
|
|
|
}
|
2012-10-07 10:01:39 +04:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
for (int i = 0; i < shell_commands_len; ++i) {
|
|
|
|
fprintf(stderr, "%s", shell_commands[i]);
|
|
|
|
if (i < shell_commands_len - 1) {
|
|
|
|
fprintf(stderr, ", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2013-05-23 08:59:28 +04:00
|
|
|
context->callbacks->redraw_prompt(context);
|
2012-10-07 10:01:39 +04:00
|
|
|
rline_redraw(context);
|
|
|
|
return;
|
|
|
|
} else {
|
2012-10-07 19:45:00 +04:00
|
|
|
int j = 0;
|
|
|
|
list_t * matches = list_create();
|
2012-10-07 10:01:39 +04:00
|
|
|
char * match = NULL;
|
|
|
|
for (int i = 0; i < shell_commands_len; ++i) {
|
|
|
|
if (strstr(shell_commands[i], argv[0]) == shell_commands[i]) {
|
2012-10-07 19:45:00 +04:00
|
|
|
list_insert(matches, shell_commands[i]);
|
2012-10-07 10:01:39 +04:00
|
|
|
match = shell_commands[i];
|
2012-01-27 23:10:58 +04:00
|
|
|
}
|
2012-10-07 10:01:39 +04:00
|
|
|
}
|
2012-10-07 19:45:00 +04:00
|
|
|
if (matches->length == 0) {
|
|
|
|
list_free(matches);
|
|
|
|
return;
|
|
|
|
} else if (matches->length == 1) {
|
2012-10-07 10:01:39 +04:00
|
|
|
for (int j = 0; j < strlen(context->buffer); ++j) {
|
|
|
|
printf("\010 \010");
|
|
|
|
}
|
2013-04-03 02:16:12 +04:00
|
|
|
printf("%s", match);
|
2012-10-07 10:01:39 +04:00
|
|
|
fflush(stdout);
|
|
|
|
memcpy(context->buffer, match, strlen(match) + 1);
|
|
|
|
context->collected = strlen(context->buffer);
|
|
|
|
context->offset = context->collected;
|
2012-10-07 19:45:00 +04:00
|
|
|
list_free(matches);
|
2012-10-07 10:01:39 +04:00
|
|
|
return;
|
2012-10-07 19:45:00 +04:00
|
|
|
} else {
|
2012-10-07 10:48:46 +04:00
|
|
|
if (!context->tabbed) {
|
|
|
|
context->tabbed = 1;
|
2012-10-07 19:45:00 +04:00
|
|
|
list_free(matches);
|
2012-10-07 10:48:46 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-10-07 19:45:00 +04:00
|
|
|
j = matches->length;
|
2012-10-07 11:18:55 +04:00
|
|
|
char tmp[1024];
|
|
|
|
memcpy(tmp, argv[0], strlen(argv[0])+1);
|
2012-10-07 19:45:00 +04:00
|
|
|
while (j == matches->length) {
|
2012-10-07 11:18:55 +04:00
|
|
|
j = 0;
|
|
|
|
int x = strlen(tmp);
|
|
|
|
tmp[x] = match[x];
|
|
|
|
tmp[x+1] = '\0';
|
2012-10-07 19:45:00 +04:00
|
|
|
node_t * node;
|
|
|
|
foreach(node, matches) {
|
|
|
|
char * match = (char *)node->value;
|
|
|
|
if (strstr(match, tmp) == match) {
|
2012-10-07 11:18:55 +04:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp[strlen(tmp)-1] = '\0';
|
|
|
|
memcpy(context->buffer, tmp, strlen(tmp) + 1);
|
|
|
|
context->collected = strlen(context->buffer);
|
|
|
|
context->offset = context->collected;
|
|
|
|
j = 0;
|
2012-10-07 10:01:39 +04:00
|
|
|
fprintf(stderr, "\n");
|
2012-10-07 19:45:00 +04:00
|
|
|
node_t * node;
|
|
|
|
foreach(node, matches) {
|
|
|
|
char * match = (char *)node->value;
|
|
|
|
fprintf(stderr, "%s", match);
|
|
|
|
++j;
|
|
|
|
if (j < matches->length) {
|
|
|
|
fprintf(stderr, ", ");
|
2012-10-07 10:01:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2013-05-23 08:59:28 +04:00
|
|
|
context->callbacks->redraw_prompt(context);
|
2012-10-08 07:57:58 +04:00
|
|
|
fprintf(stderr, "\033[s");
|
2012-10-07 10:01:39 +04:00
|
|
|
rline_redraw(context);
|
2012-10-07 19:45:00 +04:00
|
|
|
list_free(matches);
|
2012-10-07 10:01:39 +04:00
|
|
|
return;
|
2012-01-27 23:10:58 +04:00
|
|
|
}
|
|
|
|
}
|
2012-10-07 10:01:39 +04:00
|
|
|
} else {
|
2012-10-07 10:48:46 +04:00
|
|
|
/* XXX Should complete to file names here */
|
2012-10-07 10:01:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 20:34:10 +04:00
|
|
|
void reverse_search(rline_context_t * context) {
|
|
|
|
char input[512] = {0};
|
2013-05-29 09:00:27 +04:00
|
|
|
int collected = 0;
|
2012-10-07 20:34:10 +04:00
|
|
|
int start_at = 0;
|
2012-10-07 23:22:39 +04:00
|
|
|
fprintf(stderr, "\033[G\033[s");
|
|
|
|
fflush(stderr);
|
2014-04-15 08:03:23 +04:00
|
|
|
key_event_state_t kbd_state = {0};
|
2012-10-07 20:34:10 +04:00
|
|
|
while (1) {
|
|
|
|
/* Find matches */
|
|
|
|
char * match = "";
|
|
|
|
int match_index = 0;
|
|
|
|
try_rev_search_again:
|
|
|
|
if (collected) {
|
|
|
|
for (int i = start_at; i < shell_history_count; i++) {
|
|
|
|
char * c = shell_history_prev(i+1);
|
|
|
|
if (strstr(c, input)) {
|
|
|
|
match = c;
|
|
|
|
match_index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!strcmp(match,"")) {
|
|
|
|
if (start_at) {
|
|
|
|
start_at = 0;
|
|
|
|
goto try_rev_search_again;
|
|
|
|
}
|
|
|
|
collected--;
|
|
|
|
input[collected] = '\0';
|
|
|
|
if (collected) {
|
|
|
|
goto try_rev_search_again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-07 23:22:39 +04:00
|
|
|
fprintf(stderr, "\033[u(reverse-i-search)`%s': %s\033[K", input, match);
|
2012-10-07 20:34:10 +04:00
|
|
|
fflush(stderr);
|
2014-04-15 08:03:23 +04:00
|
|
|
|
|
|
|
uint32_t key_sym = kbd_key(&kbd_state, fgetc(stdin));
|
2012-10-07 20:34:10 +04:00
|
|
|
switch (key_sym) {
|
|
|
|
case KEY_BACKSPACE:
|
|
|
|
if (collected > 0) {
|
|
|
|
collected--;
|
|
|
|
input[collected] = '\0';
|
|
|
|
start_at = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_CTRL_C:
|
|
|
|
printf("^C\n");
|
|
|
|
return;
|
|
|
|
case KEY_CTRL_R:
|
|
|
|
start_at = match_index + 1;
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
memcpy(context->buffer, match, strlen(match) + 1);
|
|
|
|
context->collected = strlen(match);
|
|
|
|
context->offset = context->collected;
|
2013-05-23 08:59:28 +04:00
|
|
|
if (context->callbacks->redraw_prompt) {
|
|
|
|
fprintf(stderr, "\033[G\033[K");
|
|
|
|
context->callbacks->redraw_prompt(context);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\033[s");
|
|
|
|
rline_redraw_clean(context);
|
|
|
|
fprintf(stderr, "\n");
|
2012-10-07 20:34:10 +04:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
if (key_sym < KEY_NORMAL_MAX) {
|
|
|
|
input[collected] = (char)key_sym;
|
|
|
|
collected++;
|
|
|
|
input[collected] = '\0';
|
|
|
|
start_at = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
void history_previous(rline_context_t * context) {
|
|
|
|
if (shell_scroll == 0) {
|
|
|
|
memcpy(shell_temp, context->buffer, strlen(context->buffer) + 1);
|
|
|
|
}
|
|
|
|
if (shell_scroll < shell_history_count) {
|
|
|
|
shell_scroll++;
|
2013-05-29 09:00:27 +04:00
|
|
|
for (int i = 0; i < strlen(context->buffer); ++i) {
|
2012-10-07 10:01:39 +04:00
|
|
|
printf("\010 \010");
|
2012-03-09 09:36:40 +04:00
|
|
|
}
|
2012-10-07 10:01:39 +04:00
|
|
|
char * h = shell_history_prev(shell_scroll);
|
|
|
|
memcpy(context->buffer, h, strlen(h) + 1);
|
2012-10-07 23:22:39 +04:00
|
|
|
printf("\033[u%s\033[K", h);
|
2012-10-07 10:01:39 +04:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
context->collected = strlen(context->buffer);
|
|
|
|
context->offset = context->collected;
|
|
|
|
}
|
2011-12-08 01:08:40 +04:00
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
void history_next(rline_context_t * context) {
|
|
|
|
if (shell_scroll > 1) {
|
|
|
|
shell_scroll--;
|
2013-05-29 09:00:27 +04:00
|
|
|
for (int i = 0; i < strlen(context->buffer); ++i) {
|
2012-10-07 10:01:39 +04:00
|
|
|
printf("\010 \010");
|
|
|
|
}
|
|
|
|
char * h = shell_history_prev(shell_scroll);
|
|
|
|
memcpy(context->buffer, h, strlen(h) + 1);
|
|
|
|
printf("%s", h);
|
|
|
|
fflush(stdout);
|
|
|
|
} else if (shell_scroll == 1) {
|
2013-05-29 09:00:27 +04:00
|
|
|
for (int i = 0; i < strlen(context->buffer); ++i) {
|
2012-10-07 10:01:39 +04:00
|
|
|
printf("\010 \010");
|
|
|
|
}
|
|
|
|
shell_scroll = 0;
|
|
|
|
memcpy(context->buffer, shell_temp, strlen(shell_temp) + 1);
|
2012-10-07 23:22:39 +04:00
|
|
|
printf("\033[u%s\033[K", context->buffer);
|
2012-10-07 10:01:39 +04:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
context->collected = strlen(context->buffer);
|
|
|
|
context->offset = context->collected;
|
|
|
|
}
|
|
|
|
|
2012-10-07 22:01:00 +04:00
|
|
|
void add_argument(list_t * argv, char * buf) {
|
|
|
|
char * c = malloc(strlen(buf) + 1);
|
|
|
|
memcpy(c, buf, strlen(buf) + 1);
|
2012-10-07 10:01:39 +04:00
|
|
|
|
2012-10-07 22:01:00 +04:00
|
|
|
list_insert(argv, c);
|
|
|
|
}
|
|
|
|
|
2013-05-29 09:00:27 +04:00
|
|
|
int read_entry(char * buffer) {
|
2012-10-07 22:01:00 +04:00
|
|
|
rline_callbacks_t callbacks = {
|
|
|
|
tab_complete_func, redraw_prompt_func, NULL,
|
|
|
|
history_previous, history_next,
|
|
|
|
NULL, NULL, reverse_search
|
|
|
|
};
|
|
|
|
set_unbuffered();
|
2013-05-29 09:00:27 +04:00
|
|
|
int buffer_size = rline((char *)buffer, LINE_LEN, &callbacks);
|
2012-10-07 22:01:00 +04:00
|
|
|
set_buffered();
|
|
|
|
return buffer_size;
|
|
|
|
}
|
|
|
|
|
2013-05-29 09:00:27 +04:00
|
|
|
int read_entry_continued(char * buffer) {
|
2012-10-07 22:01:00 +04:00
|
|
|
rline_callbacks_t callbacks = {
|
|
|
|
tab_complete_func, redraw_prompt_func_c, NULL,
|
|
|
|
history_previous, history_next,
|
|
|
|
NULL, NULL, reverse_search
|
|
|
|
};
|
|
|
|
set_unbuffered();
|
2013-05-29 09:00:27 +04:00
|
|
|
int buffer_size = rline((char *)buffer, LINE_LEN, &callbacks);
|
2012-10-07 22:01:00 +04:00
|
|
|
set_buffered();
|
|
|
|
return buffer_size;
|
|
|
|
}
|
|
|
|
|
2013-05-29 09:00:27 +04:00
|
|
|
int variable_char(uint8_t c) {
|
2012-10-08 07:46:35 +04:00
|
|
|
if (c >= 65 && c <= 90) return 1;
|
|
|
|
if (c >= 97 && c <= 122) return 1;
|
|
|
|
if (c >= 48 && c <= 57) return 1;
|
|
|
|
if (c == 95) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:50:19 +04:00
|
|
|
void run_cmd(char ** args) {
|
|
|
|
int i = execvp(*args, args);
|
|
|
|
shell_command_t func = shell_find(*args);
|
|
|
|
if (func) {
|
|
|
|
int argc = 0;
|
|
|
|
while (args[argc]) {
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
i = func(argc, args);
|
|
|
|
} else {
|
|
|
|
if (i != 0) {
|
|
|
|
fprintf(stderr, "%s: Command not found\n", *args);
|
|
|
|
i = 127;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(i);
|
|
|
|
}
|
|
|
|
|
2013-05-29 09:00:27 +04:00
|
|
|
int shell_exec(char * buffer, int buffer_size) {
|
2012-10-07 10:01:39 +04:00
|
|
|
|
|
|
|
/* Read previous history entries */
|
|
|
|
if (buffer[0] == '!') {
|
|
|
|
uint32_t x = atoi((char *)((uintptr_t)buffer + 1));
|
|
|
|
if (x <= shell_history_count) {
|
|
|
|
buffer = shell_history_get(x - 1);
|
|
|
|
buffer_size = strlen(buffer);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "esh: !%d: event not found\n", x);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 20:34:10 +04:00
|
|
|
char * history = malloc(strlen(buffer) + 1);
|
|
|
|
memcpy(history, buffer, strlen(buffer) + 1);
|
|
|
|
|
2012-10-07 22:01:00 +04:00
|
|
|
if (buffer[0] != ' ' && buffer[0] != '\n') {
|
2012-10-07 11:18:55 +04:00
|
|
|
shell_history_insert(history);
|
2012-10-07 20:34:10 +04:00
|
|
|
} else {
|
|
|
|
free(history);
|
2012-10-07 11:18:55 +04:00
|
|
|
}
|
2012-10-07 10:01:39 +04:00
|
|
|
|
|
|
|
char * argv[1024];
|
|
|
|
int tokenid = 0;
|
|
|
|
|
2012-10-07 22:01:00 +04:00
|
|
|
char quoted = 0;
|
|
|
|
char backtick = 0;
|
|
|
|
char buffer_[512] = {0};
|
|
|
|
int collected = 0;
|
|
|
|
|
|
|
|
list_t * args = list_create();
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
char * p = buffer;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
switch (*p) {
|
2012-10-08 07:46:35 +04:00
|
|
|
case '$':
|
|
|
|
if (quoted != '\'') {
|
|
|
|
p++;
|
|
|
|
char var[100];
|
|
|
|
int coll = 0;
|
|
|
|
if (*p == '{') {
|
|
|
|
p++;
|
|
|
|
while (*p != '}' && *p != '\0' && (coll < 100)) {
|
|
|
|
var[coll] = *p;
|
|
|
|
coll++;
|
|
|
|
var[coll] = '\0';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (*p == '}') {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (*p != '\0' && variable_char(*p) && (coll < 100)) {
|
|
|
|
var[coll] = *p;
|
|
|
|
coll++;
|
|
|
|
var[coll] = '\0';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
2012-10-08 11:17:50 +04:00
|
|
|
char *c = getenv(var);
|
|
|
|
if (c) {
|
2012-10-08 07:46:35 +04:00
|
|
|
backtick = 0;
|
|
|
|
for (int i = 0; i < strlen(c); ++i) {
|
|
|
|
buffer_[collected] = c[i];
|
|
|
|
collected++;
|
|
|
|
}
|
|
|
|
buffer_[collected] = '\0';
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto _next;
|
2012-10-07 22:01:00 +04:00
|
|
|
case '\"':
|
|
|
|
if (quoted == '\"') {
|
|
|
|
if (backtick) {
|
|
|
|
goto _just_add;
|
|
|
|
}
|
|
|
|
quoted = 0;
|
|
|
|
goto _next;
|
|
|
|
} else if (!quoted) {
|
|
|
|
quoted = *p;
|
|
|
|
goto _next;
|
|
|
|
}
|
|
|
|
goto _just_add;
|
|
|
|
case '\'':
|
|
|
|
if (quoted == '\'') {
|
|
|
|
if (backtick) {
|
|
|
|
goto _just_add;
|
|
|
|
}
|
|
|
|
quoted = 0;
|
|
|
|
goto _next;
|
|
|
|
} else if (!quoted) {
|
|
|
|
quoted = *p;
|
|
|
|
goto _next;
|
|
|
|
}
|
|
|
|
goto _just_add;
|
|
|
|
case '\\':
|
2012-10-07 22:09:51 +04:00
|
|
|
if (backtick) {
|
|
|
|
goto _just_add;
|
|
|
|
}
|
2012-10-07 22:01:00 +04:00
|
|
|
backtick = 1;
|
|
|
|
goto _next;
|
|
|
|
case ' ':
|
|
|
|
if (backtick) {
|
|
|
|
goto _just_add;
|
|
|
|
}
|
|
|
|
if (!quoted) {
|
|
|
|
goto _new_arg;
|
|
|
|
}
|
|
|
|
goto _just_add;
|
|
|
|
case '\n':
|
|
|
|
if (!quoted) {
|
|
|
|
goto _done;
|
|
|
|
}
|
|
|
|
goto _just_add;
|
2014-05-31 03:23:40 +04:00
|
|
|
case '|':
|
|
|
|
if (!quoted && !backtick && !collected) {
|
|
|
|
collected = sprintf(buffer_, "%s", PIPE_TOKEN);
|
|
|
|
goto _new_arg;
|
|
|
|
}
|
2012-10-07 22:01:00 +04:00
|
|
|
default:
|
|
|
|
if (backtick) {
|
|
|
|
buffer_[collected] = '\\';
|
|
|
|
collected++;
|
|
|
|
buffer_[collected] = '\0';
|
|
|
|
}
|
|
|
|
_just_add:
|
|
|
|
backtick = 0;
|
|
|
|
buffer_[collected] = *p;
|
|
|
|
collected++;
|
|
|
|
buffer_[collected] = '\0';
|
|
|
|
goto _next;
|
|
|
|
}
|
|
|
|
|
|
|
|
_new_arg:
|
|
|
|
backtick = 0;
|
|
|
|
if (collected) {
|
|
|
|
add_argument(args, buffer_);
|
|
|
|
buffer_[0] = '\0';
|
|
|
|
collected = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_next:
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
_done:
|
|
|
|
|
|
|
|
if (quoted) {
|
2013-03-28 11:12:48 +04:00
|
|
|
if (shell_interactive) {
|
|
|
|
draw_prompt_c();
|
|
|
|
buffer_size = read_entry_continued(buffer);
|
|
|
|
shell_history_append_line(buffer);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Syntax error: Unterminated quoted string.\n");
|
|
|
|
return 127;
|
|
|
|
}
|
2012-10-07 22:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (collected) {
|
|
|
|
add_argument(args, buffer_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2012-10-07 10:01:39 +04:00
|
|
|
}
|
|
|
|
|
2014-05-31 03:23:40 +04:00
|
|
|
int cmdi = 0;
|
|
|
|
char ** arg_starts[100] = { &argv[0], NULL };
|
|
|
|
int argcs[100] = {0};
|
|
|
|
|
2012-10-07 22:01:00 +04:00
|
|
|
int i = 0;
|
|
|
|
foreach(node, args) {
|
|
|
|
char * c = node->value;
|
|
|
|
|
2014-05-31 03:23:40 +04:00
|
|
|
if (!strcmp(c, PIPE_TOKEN)) {
|
|
|
|
argv[i] = 0;
|
|
|
|
i++;
|
|
|
|
cmdi++;
|
|
|
|
arg_starts[cmdi] = &argv[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-10-07 22:01:00 +04:00
|
|
|
argv[i] = c;
|
|
|
|
i++;
|
2014-05-31 03:23:40 +04:00
|
|
|
argcs[cmdi]++;
|
2012-10-07 22:01:00 +04:00
|
|
|
}
|
2014-05-31 03:23:40 +04:00
|
|
|
argv[i] = NULL;
|
2012-10-07 22:01:00 +04:00
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_free(args);
|
|
|
|
|
2014-05-31 03:23:40 +04:00
|
|
|
char * cmd = *arg_starts[0];
|
2012-10-07 22:01:00 +04:00
|
|
|
tokenid = i;
|
|
|
|
|
2014-05-31 07:50:19 +04:00
|
|
|
unsigned int child_pid;
|
|
|
|
|
|
|
|
int nowait = (!strcmp(argv[tokenid-1],"&"));
|
|
|
|
if (nowait) {
|
|
|
|
argv[tokenid-1] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shell_force_raw) set_unbuffered();
|
|
|
|
|
2014-05-31 03:23:40 +04:00
|
|
|
if (cmdi > 0) {
|
|
|
|
int last_output[2];
|
|
|
|
pipe(last_output);
|
2014-05-31 07:50:19 +04:00
|
|
|
child_pid = fork();
|
|
|
|
if (!child_pid) {
|
2014-05-31 03:23:40 +04:00
|
|
|
dup2(last_output[1], STDOUT_FILENO);
|
|
|
|
close(last_output[0]);
|
2014-05-31 07:50:19 +04:00
|
|
|
run_cmd(arg_starts[0]);
|
2014-05-31 03:23:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 1; j < cmdi; ++j) {
|
|
|
|
int tmp_out[2];
|
|
|
|
pipe(tmp_out);
|
|
|
|
if (!fork()) {
|
|
|
|
dup2(tmp_out[1], STDOUT_FILENO);
|
|
|
|
dup2(last_output[0], STDIN_FILENO);
|
|
|
|
close(tmp_out[0]);
|
|
|
|
close(last_output[1]);
|
2014-05-31 07:50:19 +04:00
|
|
|
run_cmd(arg_starts[j]);
|
2014-05-31 03:23:40 +04:00
|
|
|
}
|
|
|
|
close(last_output[0]);
|
|
|
|
close(last_output[1]);
|
|
|
|
last_output[0] = tmp_out[0];
|
|
|
|
last_output[1] = tmp_out[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fork()) {
|
|
|
|
dup2(last_output[0], STDIN_FILENO);
|
|
|
|
close(last_output[1]);
|
2014-05-31 07:50:19 +04:00
|
|
|
run_cmd(arg_starts[cmdi]);
|
2014-05-31 03:23:40 +04:00
|
|
|
}
|
|
|
|
close(last_output[0]);
|
|
|
|
close(last_output[1]);
|
|
|
|
|
|
|
|
/* Now execute the last piece and wait on all of them */
|
2014-05-31 07:50:19 +04:00
|
|
|
} else {
|
|
|
|
shell_command_t func = shell_find(*arg_starts[0]);
|
|
|
|
if (func) {
|
|
|
|
return func(argcs[0], arg_starts[0]);
|
|
|
|
} else {
|
|
|
|
child_pid = fork();
|
|
|
|
if (!child_pid) {
|
|
|
|
run_cmd(arg_starts[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-31 03:23:40 +04:00
|
|
|
|
2014-05-31 07:50:19 +04:00
|
|
|
tcsetpgrp(STDIN_FILENO, child_pid);
|
|
|
|
int ret_code = 0;
|
|
|
|
if (!nowait) {
|
|
|
|
child = child_pid;
|
2014-05-31 03:23:40 +04:00
|
|
|
int pid;
|
|
|
|
do {
|
|
|
|
pid = waitpid(-1, &ret_code, 0);
|
|
|
|
} while (pid != -1 || (pid == -1 && errno != ECHILD));
|
|
|
|
child = 0;
|
2011-12-07 05:46:35 +04:00
|
|
|
}
|
2014-05-31 07:50:19 +04:00
|
|
|
tcsetpgrp(STDIN_FILENO, getpid());
|
|
|
|
free(cmd);
|
|
|
|
return ret_code;
|
2012-10-07 10:01:39 +04:00
|
|
|
}
|
|
|
|
|
2012-10-07 10:48:46 +04:00
|
|
|
void add_path_contents() {
|
|
|
|
DIR * dirp = opendir("/bin");
|
|
|
|
|
|
|
|
struct dirent * ent = readdir(dirp);
|
|
|
|
while (ent != NULL) {
|
|
|
|
if (ent->d_name[0] != '.') {
|
|
|
|
char * s = malloc(sizeof(char) * (strlen(ent->d_name) + 1));
|
|
|
|
memcpy(s, ent->d_name, strlen(ent->d_name) + 1);
|
|
|
|
shell_install_command(s, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ent = readdir(dirp);
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct command {
|
|
|
|
char * string;
|
|
|
|
void * func;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int comp_shell_commands(const void *p1, const void *p2) {
|
|
|
|
return strcmp(((struct command *)p1)->string, ((struct command *)p2)->string);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sort_commands() {
|
|
|
|
struct command commands[SHELL_COMMANDS];
|
|
|
|
for (int i = 0; i < shell_commands_len; ++i) {
|
|
|
|
commands[i].string = shell_commands[i];
|
|
|
|
commands[i].func = shell_pointers[i];
|
|
|
|
}
|
|
|
|
qsort(&commands, shell_commands_len, sizeof(struct command), comp_shell_commands);
|
|
|
|
for (int i = 0; i < shell_commands_len; ++i) {
|
|
|
|
shell_commands[i] = commands[i].string;
|
|
|
|
shell_pointers[i] = commands[i].func;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 11:12:48 +04:00
|
|
|
void show_usage(int argc, char * argv[]) {
|
|
|
|
printf(
|
|
|
|
"Esh: The Experimental Shell\n"
|
|
|
|
"\n"
|
|
|
|
"usage: %s [-lha] [path]\n"
|
|
|
|
"\n"
|
|
|
|
" -c \033[4mcmd\033[0m \033[3mparse and execute cmd\033[0m\n"
|
|
|
|
//-c cmd \033[...
|
|
|
|
" -? \033[3mshow this help text\033[0m\n"
|
|
|
|
"\n", argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
int main(int argc, char ** argv) {
|
2012-10-08 07:54:11 +04:00
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
int nowait = 0;
|
|
|
|
int free_cmd = 0;
|
|
|
|
int last_ret = 0;
|
|
|
|
|
|
|
|
pid = getpid();
|
|
|
|
|
2013-05-23 08:59:28 +04:00
|
|
|
signal(SIGINT, sig_pass);
|
|
|
|
signal(SIGWINCH, sig_pass);
|
2012-10-07 10:01:39 +04:00
|
|
|
|
2013-05-29 09:00:27 +04:00
|
|
|
getuser();
|
|
|
|
gethost();
|
2012-10-07 10:01:39 +04:00
|
|
|
|
|
|
|
install_commands();
|
2012-10-07 10:48:46 +04:00
|
|
|
add_path_contents();
|
|
|
|
sort_commands();
|
2013-03-28 11:12:48 +04:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
int index, c;
|
|
|
|
while ((c = getopt(argc, argv, "c:?")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
shell_interactive = 0;
|
|
|
|
return shell_exec(optarg, strlen(optarg));
|
|
|
|
case '?':
|
|
|
|
show_usage(argc, argv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shell_interactive = 1;
|
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
while (1) {
|
|
|
|
draw_prompt(last_ret);
|
|
|
|
char buffer[LINE_LEN] = {0};
|
|
|
|
int buffer_size;
|
|
|
|
|
2012-10-07 22:01:00 +04:00
|
|
|
buffer_size = read_entry(buffer);
|
2012-10-07 10:01:39 +04:00
|
|
|
last_ret = shell_exec(buffer, buffer_size);
|
|
|
|
shell_scroll = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-07 05:46:35 +04:00
|
|
|
exit:
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-07 10:01:39 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* cd [path]
|
|
|
|
*/
|
|
|
|
uint32_t shell_cmd_cd(int argc, char * argv[]) {
|
|
|
|
if (argc > 1) {
|
|
|
|
if (chdir(argv[1])) {
|
2013-06-30 05:48:57 +04:00
|
|
|
goto cd_error;
|
2012-10-07 10:01:39 +04:00
|
|
|
} /* else success */
|
|
|
|
} else /* argc < 2 */ {
|
2014-06-08 11:21:23 +04:00
|
|
|
char * home = getenv("HOME");
|
|
|
|
if (home) {
|
|
|
|
if (chdir(home)) {
|
|
|
|
goto cd_error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char home_path[512];
|
|
|
|
sprintf(home_path, "/home/%s", username);
|
|
|
|
if (chdir(home_path)) {
|
|
|
|
goto cd_error;
|
|
|
|
}
|
2012-10-07 10:01:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2013-06-30 05:48:57 +04:00
|
|
|
cd_error:
|
|
|
|
fprintf(stderr, "%s: could not cd '%s': no such file or directory\n", argv[0], argv[1]);
|
|
|
|
return 1;
|
2012-10-07 10:01:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* history
|
|
|
|
*/
|
|
|
|
uint32_t shell_cmd_history(int argc, char * argv[]) {
|
2013-05-29 09:00:27 +04:00
|
|
|
for (int i = 0; i < shell_history_count; ++i) {
|
2012-10-07 10:01:39 +04:00
|
|
|
printf("%d\t%s\n", i + 1, shell_history_get(i));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-08 07:46:35 +04:00
|
|
|
uint32_t shell_cmd_test(int argc, char * argv[]) {
|
|
|
|
printf("%d arguments.\n", argc);
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
printf("%d -> %s\n", i, argv[i]);
|
|
|
|
}
|
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t shell_cmd_export(int argc, char * argv[]) {
|
|
|
|
if (argc > 1) {
|
2012-10-08 11:17:50 +04:00
|
|
|
putenv(argv[1]);
|
2012-10-08 07:46:35 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-08 07:54:11 +04:00
|
|
|
uint32_t shell_cmd_exit(int argc, char * argv[]) {
|
|
|
|
if (argc > 1) {
|
|
|
|
exit(atoi(argv[1]));
|
|
|
|
} else {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-14 07:17:29 +04:00
|
|
|
uint32_t shell_cmd_set(int argc, char * argv[]) {
|
|
|
|
char * term = getenv("TERM");
|
|
|
|
if (!term || strstr(term, "toaru") != term) {
|
|
|
|
fprintf(stderr, "Unrecognized terminal. These commands are for the とある terminal only.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "%s: expected argument\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "alpha")) {
|
|
|
|
if (argc < 3) {
|
|
|
|
fprintf(stderr, "%s %s [0 or 1]\n", argv[0], argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int i = atoi(argv[2]);
|
|
|
|
if (i) {
|
|
|
|
printf("\033[2001z");
|
|
|
|
} else {
|
|
|
|
printf("\033[2000z");
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
} else if (!strcmp(argv[1], "scale")) {
|
|
|
|
if (argc < 3) {
|
|
|
|
fprintf(stderr, "%s %s [floating point size, 1.0 = normal]\n", argv[0], argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("\033[1555;%sz", argv[2]);
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
} else if (!strcmp(argv[1], "size")) {
|
|
|
|
if (argc < 4) {
|
|
|
|
fprintf(stderr, "%s %s [width] [height]\n", argv[0], argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("\033[3000;%s;%sz", argv[2], argv[3]);
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
2013-04-14 07:21:40 +04:00
|
|
|
} else if (!strcmp(argv[1], "force-raw")) {
|
|
|
|
shell_force_raw = 1;
|
|
|
|
return 0;
|
|
|
|
} else if (!strcmp(argv[1], "no-force-raw")) {
|
|
|
|
shell_force_raw = 0;
|
|
|
|
return 0;
|
2012-10-14 07:17:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 10:01:39 +04:00
|
|
|
void install_commands() {
|
|
|
|
shell_install_command("cd", shell_cmd_cd);
|
|
|
|
shell_install_command("history", shell_cmd_history);
|
2012-10-08 07:46:35 +04:00
|
|
|
shell_install_command("export", shell_cmd_export);
|
|
|
|
shell_install_command("test", shell_cmd_test);
|
2012-10-08 07:54:11 +04:00
|
|
|
shell_install_command("exit", shell_cmd_exit);
|
2012-10-14 07:17:29 +04:00
|
|
|
shell_install_command("set", shell_cmd_set);
|
2012-10-07 10:01:39 +04:00
|
|
|
}
|