Commit graph

51 commits

Author SHA1 Message Date
Andreas Kling
1b9f4c7c40 Shell: Fix broken debug logging about waiting on children 2020-01-12 10:03:06 +01:00
Andreas Kling
1934a1ec0b Shell: Use pledge() 2020-01-11 21:33:07 +01:00
Andreas Kling
6f4c380d95 AK: Use size_t for the length of strings
Using int was a mistake. This patch changes String, StringImpl,
StringView and StringBuilder to use size_t instead of int for lengths.
Obviously a lot of code needs to change as a result of this.
2019-12-09 17:51:21 +01:00
Andreas Kling
cd55f76727 Shell: Use _exit() in the forked child if execvp() fails
If we can't find an executable to exec() after forking, we don't want
to run the atexit() handlers in the child process. Just use _exit()
instead to avoid this.

This was causing us to write out the shell history to ~/.history every
time a "command not found" error was printed.
2019-12-07 22:40:54 +01:00
William McPherson
aa8b40dce6 Shell: Cache PATH for faster tab completion
This patch reduces the O(n) tab completion to something like O(log(n)).
The cache is just a sorted vector of strings and we binary search it to
get a string matching our input, and then check the surrounding strings
to see if we need to remove any characters. Also we no longer stat each
file every time.

Also added an #include in BinarySearch since it was using size_t. Oops.

If `export` is called, we recache. Need to implement the `hash` builtin
for when an executable has been added to a directory in PATH.
2019-12-05 17:09:22 +01:00
Drew Stratford
058c8337df Shell: Properly set and restore termios settings.
Previously, we did not properly restore termios settings
after running built-in commands. This has been fixed
by ensuring that we only change the termios settings
when we are forking a child process.
2019-10-30 14:35:39 +01:00
Drew Stratford
58f67c1ccb Shell: Update termios settings to match line discipline.
Shell.cpp uses its own line discipline which handles
echoing and line editing. Because of this we disable
ICANON and ECHO so that we don't get duplicate characters
or weird line editing errors.

We also revert these settings just before running a command.
This is so that commands may run with proper line editing
and echoing.
2019-10-20 10:51:12 +02:00
Andreas Kling
8f45a259fc ByteBuffer: Remove pointer() in favor of data()
We had two ways to get the data inside a ByteBuffer. That was silly.
2019-09-30 08:57:01 +02:00
Andreas Kling
8d550c174e LibCore: Convert CFile to ObjectPtr 2019-09-21 20:50:06 +02:00
Andreas Kling
3596522d23 Shell: Add a "time" builtin to show how long a command took to run 2019-09-16 19:46:34 +02:00
Jesse Buhagiar
2976e889e9 Shell: Fixed pushd and popd
Fixed a few issues with both `pushd` and `popd`. There was a few
typos etcetera causing it to behave errantly in certain situations.
2019-09-15 16:02:44 +02:00
Jesse Buhagiar
ecdaf991c6 Shell: Added pushd, popd and dirs builtins
Added a few builtin functions to the shell to make navigating a bit
easier in the terminal.

`pushd` allows a user to "push" the current directory to the directory
stack, and then `cd` to the new directory.
`popd` allows the used to take the directory on the top of the stack
off before `cd`'ing to it.
`dirs` gives the state of the current directory stack.

This is only a partial implementation of the `bash` version
(gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html)
, and doesn't include any of the +N or -N commands as of yet.
2019-09-15 14:16:40 +02:00
Andreas Kling
b7bedab28a Shell: Support extremely naive shell script execution
This patch allows passing a script as an argument to the Shell program.
We will read the specified line by line and pass them through the Shell
command interpreter.

This is not very powerful, but it's a start :^)
2019-09-14 21:20:13 +02:00
Tim Morgan
af6948afe0 Shell: fix crash when using cd - and OLDPWD is null 2019-09-13 15:12:16 +02:00
Tim Morgan
093961d2d9 Shell: Remember previous working dir
...and allow switching back to it with `cd -`

Partially addresses #397
2019-09-13 10:48:04 +02:00
MinusGix
8920ece8f6 Shell: Add support for special parameter that returns PID 2019-09-10 14:51:55 +02:00
MinusGix
91a609d945 Shell: Add support for special parameter that expands to return-code of last program executed 2019-09-10 14:51:55 +02:00
MinusGix
2bd181a14b Shell: Add support for getting environment variable values 2019-09-10 14:51:55 +02:00
Andreas Kling
f4042903b9 Shell: Okay I keep messing up this history file code.. actually fix it!
It's not safe to return a CFile by-value. CFile is a CObjects and they
are honestly not very good at being values..
2019-09-04 19:01:34 +02:00
Andreas Kling
0b1981ddae Shell: Oops, don't exit() when ~/.history does not exist 2019-09-04 15:16:07 +02:00
Andreas Kling
ce837d157f Shell: Don't open ~/.history for writing on startup
When we only want to read the file, we should open it for reading.
2019-09-04 14:59:29 +02:00
Conrad Pankoff
6bb6176762 Shell: Support semicolons for separating commands 2019-09-01 12:44:33 +02:00
Andreas Kling
3363426a6b Shell: Put failed command exit statuses in the debug output instead.
It was kinda unpleasant to always see "So-and-so exited with status 123."
2019-07-25 15:27:13 +02:00
Andreas Kling
7c3b2e0728 Shell: Simply print "cmd: Command not found." for ENOENT on execution.
This looks a little nicer than 'execvp(cmd): No such file or directory'
2019-07-25 07:05:38 +02:00
Andreas Kling
253e391bfc Shell: Implement support for terminal clearing with ^L.
Make LineEditor::get_line() responsible for printing the prompt. That way
we can re-prompt after clearing the screen on ^L.

This makes the Serenity Terminal feel a little bit more like home :^)
2019-07-19 20:01:46 +02:00
Andreas Kling
3073ea7d84 Kernel: Add support for the WSTOPPED flag to the waitpid() syscall.
This makes waitpid() return when a child process is stopped via a signal.
Use this in Shell to catch stopped children and return control to the
command line. :^)

Fixes #298.
2019-07-14 11:35:49 +02:00
Andreas Kling
aa2cfed6b0 Shell: Remove some unused code. 2019-07-09 13:51:41 +02:00
Andreas Kling
fc4022d173 Shell: Handle SIGWINCH to get a nice behavior when resizing.
When resizing the terminal, we now clear the entire current line and reset
the shell's LineEditor input state. This makes it look and feel kinda the
same as xterm.

Fixes #286.
2019-07-08 19:06:06 +02:00
Andreas Kling
0e75aba7c3 StringView: Rename characters() to characters_without_null_termination().
This should make you think twice before trying to use the const char* from
a StringView as if it's a null-terminated string.
2019-07-08 15:38:44 +02:00
CallumAttryde
e38c3bce15 add ~/.history file for Shell, stores entire command log and loads recent commands into history buffer 2019-06-23 22:08:12 +02:00
Sergey Bugaev
802612f665 Shell: Implement more advanced globbing.
A glob has to be resolved against the directory corresponding to
the part of the path it is found in, not the current directory.
For example, in /usr/i*/AK/, the glob has to be resolved inside
/usr. Moreover, an argument can contain more than one glob, such
as /u*/*/?, in which case they have to be resolved recursively.

In case a glob matches nothing, the argument should be used as is.
2019-06-14 06:24:02 +02:00
Andreas Kling
f7ede145b4 Shell: Run clang-format on everything. 2019-06-07 11:49:21 +02:00
Andreas Kling
08cd75ac4b Kernel: Rename FileDescriptor to FileDescription.
After reading a bunch of POSIX specs, I've learned that a file descriptor
is the number that refers to a file description, not the description itself.
So this patch renames FileDescriptor to FileDescription, and Process now has
FileDescription* file_description(int fd).
2019-06-07 09:36:51 +02:00
Andreas Kling
ecb72dd991 Shell: Print the name of each process whose exit status we're reporting. 2019-06-06 11:00:48 +02:00
Andreas Kling
036d808e96 Shell: Check the exit status of all spawned child processes. 2019-06-06 11:00:48 +02:00
Andreas Kling
848044b74c Shell: Separate fd rewirings from redirections.
This was unnecessarily confusing. When we build up a chain of commands
connected by pipes, we now store the file descriptors of each end of
these pipes as rewirings in a vector. The rewirings are then put into
effect by calls to dup2().
2019-06-04 20:36:08 +02:00
Andreas Kling
6b585f9dd7 Shell: Fix an error message incorrectly complaining about lstat() failure.
The syscall used is actually stat(), so let's be correct about that.
2019-05-31 06:07:09 +02:00
Andreas Kling
07c3cc01ec LibC: Move wait-related stuff to <sys/wait.h>. #POSIX 2019-05-30 15:12:09 +02:00
Andreas Kling
96db775ac1 LibC: Add setenv().
If I'm understanding the standard C library correctly, setenv() copies while
putenv() does not. That's really confusing and putenv() basically sucks.
To know which environment variables to free on replacement and which ones to
leave alone, we keep track of the ones malloced by setenv in a side table.

This patch also moves Shell to using setenv() instead of putenv().

Fixes #29.
2019-05-30 03:22:18 +02:00
Robin Burchell
9947ee9566 Shell: Allow * and ? wildcard expansion in arguments
Should also presumably allow for escaping and such, but this is a start.
Fixes #112.
2019-05-28 00:17:39 +02:00
Andreas Kling
dd595fe865 Shell: A '>' redirection target should be truncated. 2019-05-26 14:57:12 +02:00
Andreas Kling
0fa098845f Shell: When a command is terminated by a signal, print signal description.
Previously we were only printing the signal number (except for SIGINT.)
2019-05-26 02:35:25 +02:00
Robin Burchell
aee99b05a6 Shell: Add append operator (>>)
Fixes #93.
2019-05-26 01:32:05 +02:00
Robin Burchell
abb7455163 LibC/Shell: Add unsetenv(), and unset builtin calling it in Shell. 2019-05-16 14:32:09 +02:00
Robin Burchell
b2dd12daac LibC: Change putenv (and getenv) to not copy, but directly return the environ values.
This is in keeping with how putenv should function. It does mean that
the shell's export command now leaks, but that's not a difficult fix.

Contributes to #29.
2019-05-16 14:32:09 +02:00
Andreas Kling
43604bf71a LibC+Shell: Make system() actually work.
system() will now fork off a child process and execute the command via
/bin/sh -c. There are probably some things to fix here, but it's a start.
2019-05-13 04:52:55 +02:00
Andreas Kling
c838a2e652 Shell: Add "umask" builtin for reading/writing the shell's umask. 2019-05-10 17:39:30 +02:00
Andreas Kling
d53941a466 Shell: Support basic line editing with left/right arrow keys. 2019-05-07 04:17:05 +02:00
Andreas Kling
16a5a76445 Shell: Add "history" command that shows command history. 2019-05-07 01:43:21 +02:00
Andreas Kling
ba7364b43b Shell: Move line editing to a separate class.
To be clear, there isn't really any line editing yet. But there is
going to be, so let's have it in its own class.
2019-05-07 01:39:41 +02:00