Commit Graph

1729 Commits

Author SHA1 Message Date
Stephen Heumann
988683c572 Fix "wait" builtin to work correctly in no-parameters case
This was another issue where wait() was being called with SIGCHLD masked, and therefore never returning.
2015-01-16 18:14:55 -06:00
Stephen Heumann
bb88e739e5 Fix bug where using a 16-bit signal mask caused some signal handlers not to be installed.
This manifested itself in interactive hush shells getting suspended when ^Z was typed (and maybe also other ways).
2015-01-15 20:34:20 -06:00
Stephen Heumann
3380dae27a If hush is invoked as a login shell, set up a handler so it will be invoked when system() is called. 2015-01-01 17:33:01 -06:00
Stephen Heumann
01dbfccea6 Reset the terminal to our pgrp after groups, functions, and builtins, not just external command invocations.
This addresses a problem where something invoked in a group or function (e.g. gsh) could change the terminal's pgrp, and this would result in hush getting backgrounded.

We can still get strange behavior when a backgrounded child process changes the pgrp. Trying to deal with this would be inherently racy. Gsh also has problems in this scenario.
2014-12-28 12:22:37 -06:00
Stephen Heumann
9033a24562 Add support for additional startup files that will be sourced either always, for login shells, or for interactive shells. 2014-12-27 21:59:45 -06:00
Stephen Heumann
6dfc5eadb3 Changes to support GNO-style PATH in type and source commands, and in tab completion. 2014-12-26 21:49:55 -06:00
Stephen Heumann
79371e2122 Update test-running script to work on GNO (although it seems to hang on some tests) 2014-12-25 15:36:05 -06:00
Stephen Heumann
3e96c3390f Check for TERM changes every time we read input.
Also, fix possible crashes when termcap entries aren't found.
2014-12-24 14:54:06 -06:00
Stephen Heumann
cdad130f39 Add a "prefix" builtin for setting GS/OS prefixes 2014-12-22 11:18:38 -06:00
Stephen Heumann
dc3caf92e7 Use a ProDOS-compatible filename for saved history file on GNO. 2014-12-16 16:02:24 -06:00
Stephen Heumann
0c9ebf3839 Fix problem with the read builtin where it wouldn't finish until you typed an additional character beyond the return that was supposed to terminate it. 2014-12-14 22:06:27 -06:00
Stephen Heumann
aca9256f97 Address problem where commands to be run in subshells were getting mangled by command-line parsing.
They are now encoded to prevent this.
2014-12-09 19:52:58 -06:00
Stephen Heumann
2d16296748 Work around an ORCA/C preprocessor bug that was causing shell conditionals not to work properly.
The bug is that an #if condition may be mis-evaluated if it immediately follows a use of a function-like macro. I also modified a couple other places that could possibly trigger the bug (including a possibly more reliable fix for the instance of this problem I worked around earlier in include/xatonum.h).
2014-12-04 11:42:48 -06:00
Stephen Heumann
fb9b298ca4 Implement "close-on-exec" functionality for GNO.
We do this by maintaining a mask (for each pid) giving the fds to be closed on exec. We wrap functions that close fds so that their close-on-exec bits can be cleared at that point.

This implementation may close the fds even if the execve operation ultimately fails.
2014-12-03 21:04:46 -06:00
Stephen Heumann
e97a83d054 Add code to check and report our stack usage if HUSH_STACKCHECK is defined.
This only covers exec'd instances of hush (run by the user, or by re_execute_shell), not the forked, pre-exec part.
2014-12-02 20:53:45 -06:00
Stephen Heumann
52c59f6da1 Properly reset getopt in each invocation of getopt32() on BSD-type systems, including GNO and OS X.
Also, fix issue where callers of getopt32() weren't properly detecting errors on GNO due to a size mismatch.

This avoids strange behavior when commands using getopt32 (like export) are invoked multiple times, sometimes with invalid arguments.
2014-12-02 15:31:50 -06:00
Stephen Heumann
1ac41557b2 Replace stack-allocated "struct utsname" in hush_main with dynamic allocation. 2014-12-02 13:56:38 -06:00
Stephen Heumann
81652ca9c4 Always copy the variable strings from environ to our variable structures in hush_main, instead of trying to share them.
This avoids problems stemming from the fact that GNO's environment implementation may deallocate those strings, in some cases before we're done with them. It also allows us to uppercase the variable names, although since we made them case-insensitive this only matters for display purposes (in "set" output).
2014-12-02 13:42:18 -06:00
Stephen Heumann
468751ba5f Switch the vfork implementation to use procrecvtim() for waiting, and not signal the parent from the child.
This avoids a problem where one of the SIGALRM signals could be delivered after the original SIGALRM handler was restored, which would normally cause the process to terminate.

For now, we just rely on polling in a loop to determine when the child is done. This isn't optimal, but should be OK. If procsend() worked, we could use that in the child to signal the parent. However, procsend() is broken in GNO 2.0.6 -- it seems that it actually tries to invoke the send() call for sockets, but with the wrong arguments, leading to a crash.
2014-12-01 21:05:13 -06:00
Stephen Heumann
334678c6e9 Change various uses of signal masks to use 32-bit values (previously half the signals were essentially cut off). 2014-11-25 17:43:14 -06:00
Stephen Heumann
8b9da06832 Fix bug that was causing parse errors on shell control constructs (loops, if, case).
The bug was related to overflowing the 16-bit size of int.
2014-11-25 17:08:43 -06:00
Stephen Heumann
abcab533f4 Fix to make ... and $(...) command substitution work correctly.
This was a 1 -> STDOUT_FILENO change that I missed previously.
2014-11-23 15:47:39 -06:00
Stephen Heumann
036cea4dbe Add support to our execve for running scripts, which may have a #! line giving the interpreter to use.
We have to do this ourselves, since the GNO kernel's _execve doesn't do it for us.
2014-11-22 21:27:11 -06:00
Stephen Heumann
3e7c0d0ace Add our own version of execve for GNO, which can quote arguments and can remove variables from the environment if needed.
Our versions of the execv* functions have been moved to their own file. They are now used for all the exec* calls in hush.
2014-11-22 16:53:59 -06:00
Stephen Heumann
8728cdf2b8 Encode functions and certain variables passed to subshells as command line arguments, to ensure they aren't mangled by ORCA/C's command line parsing. 2014-11-22 15:10:35 -06:00
Stephen Heumann
2eedd5e9b3 Implement our own version of execvp for GNO, which quotes arguments containing spaces or tabs.
This should allow most typical cases of arguments with spaces to work correctly, although it will still break down in some cases. We can't do this perfectly, because we're ultimately dependent on the argument parsing code in the target application, and at least the code generated by ORCA/C doesn't give us a way to make an arbitrary string be treated as a single argument.
2014-11-22 12:55:07 -06:00
Stephen Heumann
5c972aa397 Fix our handling of process groups for job control, working around a couple GNO bugs.
*Most significantly, we avoid using setpgid(), because it doesn't work and in fact corrupts the kernel's process group table.
*Also, work around tctpgrp() returning garbage instead of 0 on success.

This adds an implementation of tcsetpgrp that works by reading the process tables to find a process in the appropriate group. This isn't used for the main job control operations, though, since it might be relatively slow.

At this point, basic job control seems to work.
2014-11-21 23:05:38 -06:00
Stephen Heumann
72634b98b9 In redirection operations, map fds 0/1/2 to 1/2/3 so they work as stdin/stdout/stderr, preserving compatibility with Unix shell scripts.
Also do a similar mapping for fds passed to "read -u" and "test -t".
2014-11-20 23:12:01 -06:00
Stephen Heumann
432950e315 In glob, factor out "stat" call and structure to a separate function. This reduces stack usage in the recursive glob function. 2014-11-20 21:08:21 -06:00
Stephen Heumann
62d8398fcf Fix to assign valid pgrps to child processes. On GNO, we can't just use the pid as a pgrp; we must call tcnewpgrp instead. 2014-11-20 17:53:33 -06:00
Stephen Heumann
4d60cd1043 Fixes for environment variable handling on GNO:
* Push/pop environment to make sure it is isolated from our parents and children.
* Make all environment vars (and shell vars) case-insensitive, consistent with GNO's internal handling of environment vars.
* Wrap putenv and unsetenv to make sure they are called with lower-case variable names, which is necessary to maintain consistency between the environ array and the kernel's internal representation of variables.
2014-11-20 13:21:23 -06:00
Stephen Heumann
d958c364f9 Merge commit 'f5add44981b43490376ea0dfed1420dba09a3a75' 2014-11-19 18:49:57 -06:00
Denys Vlasenko
f5add44981 typo fix in comment
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2014-11-20 01:43:30 +01:00
Stephen Heumann
ed1fff2c7b Merge commit '8a475def9e3e21f780ebcf07dd607b26ceb00ea8'
Includes a fix for a potential overflow when doing shell arithmetic division.
2014-11-18 21:16:35 -06:00
Stephen Heumann
60b6415214 Add our own implementation of fnmatch (taken from NetBSD), because the one in GNO 2.0.6's libc is broken.
The version in libc both matches things it shouldn't and doesn't match things it should, in (at least) cases where a pattern includes additional stuff beyond a *.
2014-11-18 20:51:04 -06:00
Denys Vlasenko
8a475def9e ash,hush: do not segfault on $((2**63 / -1))
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2014-11-18 14:32:58 +01:00
Denys Vlasenko
08a5dab181 ash: fix handling of negative start value in ${v:start:len}
function                                             old     new   delta
subevalvar                                          1140    1168     +28

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2014-11-17 20:27:18 +01:00
Stephen Heumann
5f6121c091 Add initial support for getting terminal information from termcap, instead of hard-coding vt100-style codes.
This currently only supports output, and still doesn't quite display correctly in some cases.
2014-11-11 20:58:06 -06:00
Stephen Heumann
c37475dcbc Change vfork_and_run implementation to inspect the kernel's process tables to determine whether the child has really exec'd or exited.
This should avoid strange behavior due to races when the parent has resumed but the child is still running the exec* code in libc, which mainly manifests itself when running at low speed.

We also change to signaling the child's completion with SIGALRM, and setting an extra alarm in the parent in case the child doesn't actually do it.
2014-11-10 16:08:24 -06:00
Stephen Heumann
6d5e65b9df Fix corruption and hanging resulting from using _exit in forked processes by calling QuitGS directly instead.
GNO's _exit (contrary to its man page) does clean-up for stuff like the memory allocator, which is inappropriate in a forked child process and leads to hangs and crashes.
2014-11-09 18:49:58 -06:00
Stephen Heumann
b3a419646b Changes to handle the use of \r as newline on the IIgs. 2014-11-08 19:58:11 -06:00
Stephen Heumann
c8de71ae9d Changes so that hush can successfully re-execute itself:
*initialize the "environ" variable
*Reduce the stack size to 8192 (This should still be further reduced based on analysis of usage.)
2014-11-08 18:03:25 -06:00
Stephen Heumann
bff0844d44 Add code to get the path of the current executable, which we need to re-execute the shell.
On the GS, this uses GS/OS calls. Versions for OS X and Linux are also included.
2014-11-07 23:06:34 -06:00
Stephen Heumann
6bd3b140ea Misc. fixes related to file handling:
*Use .null instead of /dev/null
*Account for GNO's dup2(), which non-standardly returns 0 on success
*Always call open with appropriate number of arguments
*Use STDIN_FILENO instead of (implicitly) 0
2014-11-07 19:23:04 -06:00
Stephen Heumann
cae994bc22 Add an emulation of waitpid that should be better than the one in libc.
In particular, it will buffer information about children other than the one being waited for, so it isn't lost. It can also emulate a non-blocking wait by arranging to interrupt the wait with a signal.
2014-11-07 12:12:18 -06:00
Stephen Heumann
9bc48e5ebf Add synchronization to prevent the parent from running concurrently with the child until the child execs or terminates.
Our approach is to have the parent wait for SIGUSR2, and send that from the child just before it calls exec* or _exit.
2014-11-06 23:29:42 -06:00
Stephen Heumann
f52d5394bf Change code using 0/1/2 for stdin/stdout/stderr file descriptors to use macros instead, to properly deal with their different numbers on GNO. 2014-11-06 18:14:15 -06:00
Stephen Heumann
3b1d003ea7 Handle globals more simply, declaring the "G" structure containing them directly in shell/hush.c.
This allows the "G" struct to show up in the debugger, so it can easily be inspected.
2014-11-06 13:08:38 -06:00
Stephen Heumann
9320c1e704 Set things up so hush can be build with debug code turned on.
This involved breaking things up into more segments in debug mode, since the code is larger. I also had to remove some unused extern definitions, which were causing link errors when debug code was enabled.

To enable debug code, pass "DEBUG=1" to make or build.gs.
2014-11-06 11:42:17 -06:00
Stephen Heumann
a3426d2782 Remove C99isms in hush debug code to address compilation errors when debugging stuff enabled. 2014-11-05 23:28:24 -06:00