Commit Graph

98 Commits

Author SHA1 Message Date
Shannon Booth
0de2ead0e9 Shell: Validate only one directory is given to cd 2020-03-09 19:35:36 +01:00
Shannon Booth
d0fb816ac3 Shell: Implement a "cd history" (cdh) builtin
`cdh` with no arguments dumps the last 8 cd calls in history, and
`cdh [index]` can be used to cd to re-run a specific index from that
history. `cdh` itself it a thin wrapper of the `cd` builtin.

There's definitely some improvements that can be made for this command,
but this seems like a good starting point for getting a feel for it and
ideas for changing it in the future.

It's not entirely clear whether we should be storing the resolved path -
or simply just the last argument passed to cd. For now we just use the
last path passed into cd as this seemed like the better option for now.

This means:
 * invalid paths will still be stored in history (potentially useful)
 * cdh's can be repeated for duplicate directory names
 * the history looks a little nicer on the eyes

It might make sense to use resolved paths.

Closes #397
2020-03-09 19:35:36 +01:00
Andreas Kling
8f25dbf394 Shell: Fix silly stack overflow in 'cd' builtin
Let's write in C++ and we won't have as many C problems. :^)
2020-03-07 11:02:11 +01:00
Andreas Kling
4a271430f8 Shell: Set up the PWD environment variable early
This ensures that PWD is set when running "sh -c something"
2020-03-07 00:25:30 +01:00
Andreas Kling
686ade6b5a AK: Make quick_sort() a little more ergonomic
Now it actually defaults to "a < b" comparison, instead of forcing you
to provide a trivial less-than comparator. Also you can pass in any
collection type that has .begin() and .end() and we'll sort it for you.
2020-03-03 16:02:58 +01:00
howar6hill
055344f346 AK: Move the wildcard-matching implementation to StringUtils
Provide wrappers in the String and StringView classes, and add some tests.
2020-03-02 10:38:08 +01:00
Andreas Kling
24a5a6cd2c Shell: Remove bogus assertion 2020-03-01 17:08:49 +01:00
Andreas Kling
2695773dd2 Shell: Handle the "delete" key correctly
We were deleting the wrong character in the line buffer, oopsie!
2020-03-01 16:32:35 +01:00
Andreas Kling
22d0a6d92f AK: Remove unnecessary casts to size_t, after Vector changes
Now that Vector uses size_t, we can remove a whole bunch of redundant
casts to size_t.
2020-03-01 12:58:22 +01:00
Andreas Kling
559a99c104 Shell: Don't pass nonsense options to waitpid()
I had these options confused with the options for waitid()
2020-02-25 16:08:44 +01:00
Andreas Kling
ceec1a7d38 AK: Make Vector use size_t for its size and capacity 2020-02-25 14:52:35 +01:00
Shannon Booth
90f40a80f4 Shell: Make some functions const 2020-02-22 21:36:54 +01:00
Shannon Booth
859c1669f9 Shell: Add basic tilde expansion
This does not work with shell completion yet, but the basics of being
able a cd being able to expand "~" to the current user's home directory,
and "~foo" to the home directory of user "foo" is added in this commit

Work towards: #115
2020-02-22 21:36:54 +01:00
Shannon Booth
20d9c431ce Shell: Use SkipParentAndBaseDir flag in DirIterator 2020-02-15 11:40:05 +01:00
Andreas Kling
8262c1e662 Shell: Build prompt based on the PROMPT environment variable if present 2020-02-10 21:52:58 +01:00
Andreas Kling
d17e23bd27 LibCore: Remove leading C from filenames 2020-02-06 15:04:03 +01:00
Andreas Kling
2d39da5405 LibCore: Put all classes in the Core namespace and remove the leading C
I've been wanting to do this for a long time. It's time we start being
consistent about how this stuff works.

The new convention is:

- "LibFoo" is a userspace library that provides the "Foo" namespace.

That's it :^) This was pretty tedious to convert and I didn't even
start on LibGUI yet. But it's coming up next.
2020-02-02 15:15:30 +01:00
Andreas Kling
7454926765 Shell: If a command process is stopped, print the stop signal to stderr 2020-01-27 20:48:42 +01:00
Andreas Kling
c303f55b60 Shell: Don't start a new session in every new shell
The session should be started at a higher level, i.e the Terminal app.
2020-01-25 14:51:23 +01:00
Andreas Kling
ed90d39cd7 Shell: Allow empty tokens if enclosed in single or double quotes
Previously the shell parser would discard empty tokens. We now allow
them when they are enclosed in quotes (either '' or "")

This means that a command like _echo ""_ will actually pass an empty
string to /bin/echo in argv[1] now.
2020-01-25 12:16:45 +01:00
Andreas Kling
41716aebde Shell: Disable SH_DEBUG by default and tidy up command timing logging 2020-01-21 16:22:31 +01:00
Andreas Kling
6ca1a46afd Shell: Don't crash when stdout is not a TTY
Let's just pretend we have 80 columns while running non-interactively.
There are definitely nicer solutions here, and we should find them.
2020-01-19 13:52:44 +01:00
Andreas Kling
94ca55cefd Meta: Add license header to source files
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.

For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.

Going forward, all new source files should include a license header.
2020-01-18 09:45:54 +01:00
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
joshua stein
c127d16326 Build: support library and generator dependencies
Instead of directly manipulating LDFLAGS, set LIB_DEPS in each
subdirectory Makefile listing the libraries needed for
building/linking such as "LIB_DEPS = Core GUI Draw IPC Core".

This adds each library as an -L and -l argument in LDFLAGS, but
also adds the library.a file as a link dependency on the current
$(PROGRAM).  This causes the given library to be (re)built before
linking the current $(PROGRAM), but will also re-link any binaries
depending on that library when it is modified, when running make
from the root directory.

Also turn generator tools like IPCCompiler into dependencies on the
files they generate, so they are built on-demand when a particular
directory needs them.

This all allows the root Makefile to just list directories and not
care about the order, as all of the dependency tracking will figure
it out.
2019-12-25 10:11:09 +01:00
joshua stein
ac25438d54 Build: clean up build system, use one shared Makefile
Allow everything to be built from the top level directory with just
'make', cleaned with 'make clean', and installed with 'make
install'.  Also support these in any particular subdirectory.

Specifying 'make VERBOSE=1' will print each ld/g++/etc. command as
it runs.

Kernel and early host tools (IPCCompiler, etc.) are built as
object.host.o so that they don't conflict with other things built
with the cross-compiler.
2019-12-20 20:20:54 +01:00
Jesse Buhagiar
cdb00530f8 Shell: Tab completion now gives suggestions
Pushing the TAB key in the shell now prints suggestions to terminal.
This makes it easier to the user to actually see what files are
available before executing the command they currently have typed.
2019-12-17 10:28:22 +01:00
William McPherson
61f2704d58 Shell: Tab completion for paths
If the cursor is in front of a token that is not the first token, we try
to split it on the last slash. If there is a slash, the first part is
the directory to search and the second part is the token to complete.
If there is no slash, we search the current directory and use the entire
token for completion.
If we find a single match and it's a directory, we add a slash. If it's
a normal file, we add a space, unless there already is one.

Also renamed cut_mismatching_chars() parameters to be more appropriate.
2019-12-11 12:10:52 +01:00
William McPherson
4ae8d929b4 Shell: Improve tab completion behaviour
A space is added if only one match is found, but we avoid adding
redundant spaces.

We complete "empty" tokens, i.e. when the cursor is at the start of the
line or in front of a space. For example:
    mkdir test
    cd test
    touch test
    chmod +x test
    export PATH=/home/anon/test
Now if you press tab, or space and then tab, you will get "test". Notice
that you also get a space.

Completion is now done relative to the cursor. You can enter two words
and then go back and complete the first one.
2019-12-11 12:10:52 +01:00
William McPherson
244e525c73 Shell: Refactor append/insert procedure
This patch just factors out the procedure of adding characters at the
cursor position. It makes tab completion code much nicer.
2019-12-11 12:10:52 +01:00
William McPherson
bb311b970f Shell: Improve readability of cache_path()
I prefer String::format over StringBuilder here.
Also simplified a weird continue statement.
2019-12-11 12:10:52 +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
Karol Baraniecki
431abc8846 Shell: Redirectiong from multiple-digit fds 2019-12-05 17:08:37 +01:00
Karol Baraniecki
7ed8a468ec Shell: Implement specifying fds in file redirection 2019-12-05 17:08:37 +01:00
Andreas Kling
845094f9e4 Shell: Exit the shell on (interactive) EOF with empty buffer
In other words, if the user presses EOF (normally Ctrl+D), we now print
out "<EOF>" and exit the shell without error.

Fixes #701.
2019-11-01 21:44:25 +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
willmcpherson2
dccab569d2 Shell: Tab completion for programs in PATH
This patch adds a function to LineEditor that takes the current shell
buffer, searches PATH for the first program that starts with that
buffer and then compares that to any other programs starting with the
buffer to remove any mismatching characters off the end. The result is
appended to the buffer.

This may be faster with a data structure but that seems overkill.
2019-09-15 19:27:44 +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
Aaron Malpas
f44e7dc5d0 Shell: Add POSIX-compliant character escaping
POSIX.1-2017, Shells & Utilities, section 2.2
2019-09-14 12:45:01 +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