1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/utils/mxsh.cpp

142 lines
3 KiB
C++
Raw Normal View History

#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2011-11-09 18:03:53 -05:00
#include <string.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
Implemented the fork() system call and what it needed to work properly. This commit got completely out of control. Added the fork(), getpid(), getppid(), sleep(), usleep() system calls, and aliases in the Maxsi:: namespace. Fixed a bug where zero-byte allocation would fail. Worked on the DescriptorTable class which now works and can fork. Got rid of some massive print-registers statements and replaced them with the portable InterruptRegisters::LogRegisters() function. Removed the SysExecuteOld function and replaced it with Process::Execute(). Rewrote the boot sequence in kernel.cpp such that it now loads the system idle process 'idle' as PID 0, and the initization process 'init' as PID 1. Rewrote the SIGINT hack. Processes now maintain a family-tree structure and keep track of their threads. PIDs are now allocated using a simple hack. Virtual memory per-process can now be allocated using a simple hack. Processes can now be forked. Fixed the Process::Execute function such that it now resets the stack pointer to where the stack actually is - not just a magic value. Removed the old and ugly Process::_endcodesection hack. Rewrote the scheduler into a much cleaner and faster version. Debug code is now moved to designated functions. The noop kernel-thread has been replaced by a simple user-space infinite-loop program 'idle'. The Thread class has been seperated from the Scheduler except in Scheduler- related code. Thread::{Save,Load}Registers has been improved and has been moved to $(CPU)/thread.cpp. Threads can now be forked. A new CreateThread function creates threads properly and portably. Added a MicrosecondsSinceBoot() function. Fixed a crucial bug in MemoryManagement::Fork(). Added an 'idle' user-space program that is a noop infinite loop, which is used by the scheduler when there is nothing to do. Rewrote the 'init' program such that it now forks off a shell, instead of becoming the shell. Added the $$ (current PID) and $PPID (parent PPID) variables to the shell.
2011-09-21 14:52:29 -04:00
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
#include <libmaxsi/sortix-keyboard.h>
Implemented the fork() system call and what it needed to work properly. This commit got completely out of control. Added the fork(), getpid(), getppid(), sleep(), usleep() system calls, and aliases in the Maxsi:: namespace. Fixed a bug where zero-byte allocation would fail. Worked on the DescriptorTable class which now works and can fork. Got rid of some massive print-registers statements and replaced them with the portable InterruptRegisters::LogRegisters() function. Removed the SysExecuteOld function and replaced it with Process::Execute(). Rewrote the boot sequence in kernel.cpp such that it now loads the system idle process 'idle' as PID 0, and the initization process 'init' as PID 1. Rewrote the SIGINT hack. Processes now maintain a family-tree structure and keep track of their threads. PIDs are now allocated using a simple hack. Virtual memory per-process can now be allocated using a simple hack. Processes can now be forked. Fixed the Process::Execute function such that it now resets the stack pointer to where the stack actually is - not just a magic value. Removed the old and ugly Process::_endcodesection hack. Rewrote the scheduler into a much cleaner and faster version. Debug code is now moved to designated functions. The noop kernel-thread has been replaced by a simple user-space infinite-loop program 'idle'. The Thread class has been seperated from the Scheduler except in Scheduler- related code. Thread::{Save,Load}Registers has been improved and has been moved to $(CPU)/thread.cpp. Threads can now be forked. A new CreateThread function creates threads properly and portably. Added a MicrosecondsSinceBoot() function. Fixed a crucial bug in MemoryManagement::Fork(). Added an 'idle' user-space program that is a noop infinite loop, which is used by the scheduler when there is nothing to do. Rewrote the 'init' program such that it now forks off a shell, instead of becoming the shell. Added the $$ (current PID) and $PPID (parent PPID) variables to the shell.
2011-09-21 14:52:29 -04:00
#include <libmaxsi/string.h>
using namespace Maxsi;
2011-11-09 18:03:53 -05:00
int status = 0;
void command()
{
const size_t CWD_SIZE = 512;
char cwd[CWD_SIZE];
const char* wd = getcwd(cwd, CWD_SIZE);
if ( !wd ) { wd = "?"; }
printf("root@sortix %s # ", wd);
fflush(stdout);
const size_t commandsize = 128;
char command[commandsize + 1];
size_t commandused = 0;
while (true)
{
unsigned method = System::Keyboard::POLL;
uint32_t codepoint = System::Keyboard::ReceiveKeystroke(method);
if ( codepoint == 0 ) { continue; }
if ( codepoint & Maxsi::Keyboard::DEPRESSED ) { continue; }
if ( codepoint >= 0x80 ) { continue; }
if ( codepoint == '\b' )
{
if ( 0 < commandused ) { printf("\b"); fflush(stdout); commandused--; }
continue;
}
if ( commandsize <= commandused && codepoint != '\n' ) { continue; }
char msg[2]; msg[0] = codepoint; msg[1] = '\0';
2011-11-23 08:07:02 -05:00
printf("%s", msg);
fflush(stdout);
if ( codepoint == '\n' ) { command[commandused] = '\0'; break; }
command[commandused++] = codepoint;
}
if ( command[0] == '\0' ) { return; }
2011-11-09 18:03:53 -05:00
if ( String::Compare(command, "$?") == 0 ) { printf("%u\n", status); status = 0; return; }
if ( String::Compare(command, "$$") == 0 ) { printf("%u\n", Process::GetPID()); status = 0; return; }
if ( String::Compare(command, "$PPID") == 0 ) { printf("%u\n", Process::GetParentPID()); status = 0; return; }
Implemented the fork() system call and what it needed to work properly. This commit got completely out of control. Added the fork(), getpid(), getppid(), sleep(), usleep() system calls, and aliases in the Maxsi:: namespace. Fixed a bug where zero-byte allocation would fail. Worked on the DescriptorTable class which now works and can fork. Got rid of some massive print-registers statements and replaced them with the portable InterruptRegisters::LogRegisters() function. Removed the SysExecuteOld function and replaced it with Process::Execute(). Rewrote the boot sequence in kernel.cpp such that it now loads the system idle process 'idle' as PID 0, and the initization process 'init' as PID 1. Rewrote the SIGINT hack. Processes now maintain a family-tree structure and keep track of their threads. PIDs are now allocated using a simple hack. Virtual memory per-process can now be allocated using a simple hack. Processes can now be forked. Fixed the Process::Execute function such that it now resets the stack pointer to where the stack actually is - not just a magic value. Removed the old and ugly Process::_endcodesection hack. Rewrote the scheduler into a much cleaner and faster version. Debug code is now moved to designated functions. The noop kernel-thread has been replaced by a simple user-space infinite-loop program 'idle'. The Thread class has been seperated from the Scheduler except in Scheduler- related code. Thread::{Save,Load}Registers has been improved and has been moved to $(CPU)/thread.cpp. Threads can now be forked. A new CreateThread function creates threads properly and portably. Added a MicrosecondsSinceBoot() function. Fixed a crucial bug in MemoryManagement::Fork(). Added an 'idle' user-space program that is a noop infinite loop, which is used by the scheduler when there is nothing to do. Rewrote the 'init' program such that it now forks off a shell, instead of becoming the shell. Added the $$ (current PID) and $PPID (parent PPID) variables to the shell.
2011-09-21 14:52:29 -04:00
2011-11-09 17:18:26 -05:00
int argc = 0;
const char* argv[256];
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;
2011-11-09 17:18:26 -05:00
for ( size_t i = 0; i <= commandused; i++ )
{
switch ( command[i] )
{
2011-11-26 07:27:15 -05:00
case '\0':
2011-11-09 17:18:26 -05:00
case ' ':
case '\t':
case '\n':
command[i] = 0;
lastwasspace = true;
break;
default:
if ( lastwasspace ) { argv[argc++] = command + i; }
lastwasspace = false;
}
}
2011-11-09 18:03:53 -05:00
if ( !argv[0] ) { return; }
if ( strcmp(argv[0], "exit") == 0 )
{
const char* status = "1";
if ( 1 < argc ) { status = argv[1]; }
exit(atoi(status));
}
if ( strcmp(argv[0], "cd") == 0 )
{
status = 0;
const char* newdir = "/";
if ( 1 < argc ) { newdir = argv[1]; }
if ( chdir(newdir) )
{
error(0, errno, "cd: %s", newdir);
status = 1;
}
return;
}
2011-11-09 18:03:53 -05:00
pid_t child = fork();
if ( child < 0 ) { perror("fork"); status = 1; return; }
2011-11-09 18:03:53 -05:00
if ( child != 0 )
{
pid_t childpid = wait(&status);
return;
}
if ( 3 <= argc )
{
if ( strcmp(argv[argc-2], ">") == 0 )
{
const char* file = argv[argc-1];
int outfd = open(file, O_CREAT | O_WRONLY | O_TRUNC | O_APPEND);
if ( outfd < 0 ) { error(127, errno, "%s", file); exit(127); }
close(1);
dup(outfd);
close(outfd);
argc -= 2;
}
}
// Replace the current process with another process image.
2011-11-09 17:18:26 -05:00
Process::Execute(argv[0], argc, argv);
// This is clever. This only happens if the program didn't change.
error(127, errno, "%s", argv[0]);
}
int main(int argc, char* argv[])
{
while ( true ) { command(); }
}