Commit Graph

123 Commits

Author SHA1 Message Date
Timothy Sample 177953c4b0 Add support for $$
* geesh/environment.scm (*root-pid*): New variable.
(get-root-pid): New public function.
* geesh/word.scm (parameter-ref): Handle '$'.
2018-12-05 16:23:51 -05:00
Timothy Sample 6b38897dc5 Initialize $PWD properly
* geesh/environment.scm (initial-pwd): New function.
(initial-variables): New function.
(*variables*): Initialize to the result of initial-variables.
2018-12-05 16:22:51 -05:00
Timothy Sample 2db92dd274 Work around missing case expressions
* geesh/eval.scm (exps->thunk): Filter out '#f' expressions.
2018-12-05 16:21:58 -05:00
Timothy Sample 79e5c6eafc Add the trap built-in
* geesh/built-ins/trap.scm: New file.
* Makefile.am: Add it.
* geesh/built-ins.scm (*special-built-ins*): Add trap.
2018-12-05 16:08:09 -05:00
Timothy Sample 3769dce584 Add the return and exit built-ins
* geesh/built-ins/return.scm: New file.
* geesh/built-ins/exit.scm: New file.
* geesh/Makefile.am: Add them.
* geesh/built-ins.scm (*special-built-ins*): Add return and exit.
2018-12-05 16:08:09 -05:00
Timothy Sample 66e89c1a05 Add return and exit semantics
* geesh/environment.scm (*return-tag*): New variable.
(call-with-return): New public function.
(sh:return): New public function.
(*atexit*): New variable.
(*exiting?*) New variable.
(set-atexit!): New public function.
(sh:exit): New public function.
* geesh/repl.scm (run-repl): Call sh:exit at the end of a script.
* geesh/shell.scm (sh:exec-let): Use call-with-return for functions.
* geesh/built-ins/dot.scm (main): Use call-with-return.
2018-12-05 16:08:09 -05:00
Timothy Sample 2fdbfe2ca4 Add a prefix to break and continue
These names are too common, and it will be even worse with 'exit'
and 'return'.

* geesh/environment.scm (break): Rename this...
(sh:break) ...to this.
(continue): Rename this...
(sh:continue) ...to this.
* geesh/built-ins/break.scm (main): Update call.
* geesh/built-ins/continue.scm (main): Ditto.
2018-12-05 16:08:09 -05:00
Timothy Sample 118750bb79 Handle here-documents everywhere
This commit fixes a problem with the old method of handling
here-documents.  We used to ignore here-documents in many places where
they are valid (e.g., between Boolean operators).  They are now valid
after any 'NEWLINE' token.  To achieve this, the handling logic was
moved out of the parser and into the lexer.  We now use a hook
mechanism to manage the necessary communication between the parser and
lexer.

* geesh/parser.scm (read-here-docs): Return strings instead of tokens.
(make-lexer): Add here-docs-hook as a keyword argument, and call it
after reading here-documents.
(make-parser): Let command-hooks transform commands, and remove
here-document handling rules.
(parse): Handle merging here-documents into commands.
* tests/parser.scm: Add tests for many less-intuitive here-document
locations.
2018-12-05 16:08:09 -05:00
Timothy Sample 8774eec56a Factor out common code from the read-sh functions
* geesh/parser.scm (make-lexer): Remove the read-sh/bracketed and
read-sh/backquoted parameters.
(parse): New function.
(->command-list): New function.
(read-sh/bracketed, read-sh/backquoted, read-sh, read-sh-all):
Simplify by using the new functions.
2018-12-05 16:08:09 -05:00
Timothy Sample c64d16f428 Handle argument special parameters
* geesh/word.scm (parameter-ref): Handle numeric argument parameters
and the argument count parameter ('$#').
2018-12-05 16:08:09 -05:00
Timothy Sample 2543ecd2bb Add the exec built-in
* geesh/built-ins/exec.scm: New file.
* Makefile.am: Add it.
* geesh/built-ins.scm (*special-built-ins*): Add exec.
2018-12-05 16:08:09 -05:00
Timothy Sample bacbb43650 Add the umask built-in
* geesh/built-ins/umask.scm: New file.
* Makefile.am: Add it.
* geesh/built-ins.scm (*built-ins*): Add umask.
2018-12-05 16:08:09 -05:00
Timothy Sample a7c5d8cceb Add the shift built-in
* geesh/built-ins/shift.scm: New file.
* Makefile.am: Add it.
* geesh/built-ins.scm (*special-built-ins*): Add shift.
2018-12-05 16:08:09 -05:00
Timothy Sample bc7e876f6d Fix handling of unknown options in set
* geesh/built-ins/set.scm (main): Continue processing options as new
arguments (i.e., 'set -- ...') after encountering an unknown option.
2018-12-05 16:08:09 -05:00
Timothy Sample 2bcef52895 Allow more character range patterns
* geesh/pattern.scm (ascii-lower-case, ascii-upper-case, asii-digits):
Remove variables.
(character-range): New function.
(parse-matching-bracket-expression): Use it to allow any character
range contained in one of the ASCII uppercase, ASCII lowercase, or
ASCII digits character sets.
2018-12-05 16:08:09 -05:00
Timothy Sample c1f8a870ed Add the dot and eval built-ins
* geesh/built-ins/utils.scm (get-evaluator): New public function.
* geesh/built-ins/dot.scm: New file.
* geesh/built-ins/eval.scm: New file.
* geesh/Makefile.am: Add them.
* geesh/built-ins.scm (*special-built-ins*): Add dot and eval.
2018-12-05 16:08:09 -05:00
Timothy Sample 50328c8c66 Respect the errexit option
* geesh/shell.scm (ignore-errexit?): New parameter.
(without-errexit): New function.
(errexit): New function.
(sh:exec-let, sh:set-redirects, sh:with-redirects, sh:subshell,
sh:substitute-command, sh:pipeline, sh:and, sh:or, sh:not, sh:while,
sh:cond): Use it to exit on nonzero status.
2018-12-05 16:08:09 -05:00
Timothy Sample af75931948 Process options passed to the set built-in
* geesh/built-ins/set.scm (option?): New function.
(option-letter?): New function.
(set-option!): New function.
(main): Set or print options as specified by the given arguments.
* tests/spec/oil.scm: Filter out a redirect test that was only passing
because set did not work.
2018-12-05 16:08:09 -05:00
Timothy Sample c83dbcbdc8 Add options to the environment
* geesh/environment.scm (*options*): New variable.
(getopt): New public function.
(setopt!): New public function.
(*option-names*): New public variable.
(*option-letters*): New public variable.
2018-12-05 16:08:09 -05:00
Timothy Sample b4a6350887 Exit subshells with the current status
* geesh/shell.scm (%subshell): Use with-continuation-barrier and
primitive-exit to make sure the child process exits with its status.
2018-12-05 16:08:09 -05:00
Timothy Sample 77f3113fd0 Set status to 0 when all sh-cond tests fail
* geesh/shell.scm (sh:cond): If no body thunk is called, set the
status to 0.
2018-12-05 16:08:09 -05:00
Timothy Sample 8c061471d3 Handle command not found with status 127
* geesh/shell.scm (sh:exec-let): Print a message and set the status to
127 when path search fails.
* tests/shell.scm: Update test.
2018-12-05 16:08:09 -05:00
Timothy Sample 2c1dd2d67e Add the colon built-in
* geesh/built-ins/colon.scm: New file.
* Makefile.am: Add it.
* geesh/built-ins.scm (*special-built-ins*): Add colon.
2018-12-05 16:08:09 -05:00
Timothy Sample a1bd8d0681 Add basic option processing to the main script
* geesh/repl.scm (run-repl): Add optional port parameter.
* scripts/geesh.in: Add '--command' and '--stdin' flags, and if
neither flag is provided, treat the first argument as a file to
interpret.
2018-12-05 16:08:09 -05:00
Timothy Sample 271301e77d Add the cd and pwd built-ins
* geesh/built-ins/cd.scm: New file.
* geesh/built-ins/pwd.scm: New file.
* Makefile.am: Add them.
* geesh/built-ins.scm (*built-ins*): Add cd and pwd.
2018-12-05 16:08:09 -05:00
Timothy Sample 4ef6907851 Globalize the environment module
Instead of passing around references to the environment, just treat it
as a global.  The old way was just the remains of an idea to make the
environment immutable and keep the interpreter from manipulating any
global state.  By making everything global and mutable, we will have
less impedance mismatch with POSIX going forward.

The following changelog is only a sketch, since nearly every function
has changed.

* geesh/environment.scm: Replace this module with one that treats the
environment as a global resource.
* tests/environment.scm: Delete file.
* Makefile.am: Remove it from the list of tests.
* geesh/shell.scm, geesh/eval.scm, geesh/repl.scm, geesh/word.scm,
geesh/built-ins/break.scm, geesh/built-ins/continue.scm,
geesh/built-ins/echo.scm, geesh/built-ins/export.scm,
geesh/built-ins/false.scm, geesh/built-ins/read.scm,
geesh/built-ins/readonly.scm, geesh/built-ins/set.scm,
geesh/built-ins/true.scm, geesh/built-ins/unset.scm: Remove 'env'
parameters and use the new environment module.
* .dir-locals.el: Update indentation of functions that no longer take
an 'env' parameter and add with-arguments, with-environ, and
with-variables from the new environment module.
* tests/shell.scm, tests/word.scm: Update environment creation and
manipulation in tests.
2018-12-05 16:08:09 -05:00
Timothy Sample 0d41cec2d6 Add the set built-in
* geesh/environment.scm: Export set-environment-arguments!.
* geesh/built-ins/set.scm: New file.
* Makefile.am: Add them.
* geesh/built-ins.scm: Add set.
2018-12-05 16:08:09 -05:00
Timothy Sample 671a926011 Add readonly built-in
* geesh/built-ins/utils.scm: New file.
* geesh/built-ins/readonly.scm: New file.
* Makefile.am: Add them.
* geesh/built-ins/export.scm (split-assignments): Move to utils.
* geesh/built-ins.scm (*special-built-ins*): Add readonly.
2018-12-05 16:08:09 -05:00
Timothy Sample c5ac41b0f8 Check for EOF in read built-in
* geesh/built-ins/read.scm (main): Check for EOF.
2018-12-05 16:08:09 -05:00
Timothy Sample 279ef0cd35 Add the export built-in
* geesh/built-ins/export.scm: New file.
* Makefile.am: Add it.
* geesh/built-ins.scm (*special-built-ins*): Add export.
2018-12-05 16:08:09 -05:00
Timothy Sample e784aaa658 Add export and read-only attributes to variables
* geesh/environment.scm (<variable>): New record type.
(make-environment): Initialize variables as records.
(var-ref): Extract value from record.
(set-var!): Inject value into record and throw an error if variable
is read-only.
(set-var-export!): New public function.
(set-var-read-only!): New public function.
(environment->environ): Only include exported variables.
2018-12-05 16:08:09 -05:00
Timothy Sample d4bb8ae438 Add 'true' built-in
* geesh/built-ins/true.scm: New file.
* Makefile.am: Add it.
* geesh/built-ins.scm (*built-ins*): Add true.
2018-12-05 16:08:09 -05:00
Timothy Sample eae9857f5e Add case and cond support
* geesh/eval.scm (eval-sh) Handle '<sh-case>' and '<sh-cond>' cases.
2018-12-05 16:08:09 -05:00
Timothy Sample 3ffcd0c498 Add case and cond semantics
* geesh/shell.scm (sh:case): New public function.
(sh:cond): New public function.
2018-12-05 16:08:09 -05:00
Timothy Sample 4d6e91565e Add while and until loop support
* geesh/eval.scm (eval-sh): Handle '<sh-while>' and '<sh-until>'
cases.
2018-12-05 16:08:09 -05:00
Timothy Sample 2335298226 Add while and until loop semantics
* geesh/shell.scm (sh:while): New public function.
(sh:until): New public function.
2018-12-05 16:08:09 -05:00
Timothy Sample 06db42088a Add 'break' and 'continue'
* geesh/environment.scm (<environment>): Add 'break-prompt' and
'continue-prompt' fields.
(make-environment): Initialize them.
* geesh/shell.scm (sh:for): Setup break and continue prompts.
* geesh/built-ins/break.scm: New file.
* geesh/built-ins/continue.scm: New file.
* Makefile.am: Add them.
* geesh/built-ins.scm (*special-built-ins*): Add break and continue.
2018-12-05 16:08:09 -05:00
Timothy Sample 1601a8d8b3 Add pattern returns from 'expand-word'
* geesh/word.scm (expand-word): Change the 'split?' keyword argument
to 'output', accepting one of 'fields', 'string', or 'pattern'.
(word->qword): Update calls.
* geesh/eval.scm (eval-word): Same as 'expand-word' above.
(eval-redir): Update call.
(eval-sh): Update calls.
2018-12-05 16:08:09 -05:00
Timothy Sample 6df6eb3f1d Add pathname expansion
* geesh/word.scm (qword->pattern): New function.
(expand-pathnames): New function.
(expand-word): Use it to expand pathnames when possible.
2018-12-05 16:08:09 -05:00
Timothy Sample e1ab2ccd94 Add pattern module
* geesh/pattern.scm: New file.
* tests/pattern.scm: New file.
* Makefile.am: Add them.
2018-12-05 16:08:09 -05:00
Timothy Sample 7e3bbda4cb Add 'unset' built-in
* geesh/environment.scm (delete-environment-vars!): New public function.
(delete-environment-functions!): New public function.
* geesh/built-ins/unset.scm: New file.
* Makefile.am: Add it.
* geesh/built-ins.scm: Enable 'unset'.
2018-12-05 16:08:09 -05:00
Timothy Sample 76bbe01edc Let the 'exec' built-in manipulate files
* geesh/shell.scm (sh:set-redirects): New public function.
* geesh/eval.scm (eval-sh): Use it to handle 'exec' specially when
called with redirects but no arguments.
2018-12-05 16:08:09 -05:00
Timothy Sample 0c0227d523 Add support for file descriptors 3 to 9
* geesh/shell.scm (*fd-count*) Increase to 10.
(current-3-port, current-4-port, current-5-port, current-6-port,
current-7-port, current-8-port, current-9-port): New variables.
(fd->current-port): Handle file descriptors 3 to 9.
2018-12-05 16:08:09 -05:00
Timothy Sample 5a4816dabf Add 'exec-let' support
* geesh/eval.scm (eval-sh): Handle '<sh-exec-let>'.
2018-12-05 16:08:09 -05:00
Timothy Sample d0e5c3f5ae Handle redirect errors in 'eval'
* geesh/eval.scm (eval-sh): Handle redirect errors.
2018-12-05 16:08:09 -05:00
Timothy Sample 0ae52599c4 Preserve input and error ports in substitutions
This was simply a mistake.

* geesh/shell.scm (sh:substitute-command): Leave 'current-input-port'
and 'current-error-port' alone.
* tests/shell.scm: Remove tests that check that input and error gets
ignored during substitutions.
2018-12-05 16:08:09 -05:00
Timothy Sample 085c35a833 Use 'parameterize' for redirects
Instead of saving and restoring redirects manually, if we let them all
be parameters (like 'current-input-port'), we can use 'parameterize'
and make Guile take of this for us.  As a side-effect of this change,
redirect errors get handled.

* geesh/shell.scm (save-and-set-redirect): Rename this...
(redir->parameter+port): ...to this and change the return value to be
a pair consisting of a parameter and a port.
(restore-saved-port): Remove function.
(sh:with-redirects): Use 'parameterize' to effect redirects rather
than updating them and restoring them manually.
2018-12-05 16:08:09 -05:00
Timothy Sample b92630b39d Flush ports before forking
There are two issues that arise due to output port buffers and
forking.  If we run Scheme code in a child process, we need to avoid
copying buffered output into the child process.  Otherwise, the
buffered output could be written twice.  If we run non-Scheme
code (i.e., exec), we need to flush the ports to ensure proper
sequencing of writes since the non-Scheme code will not know about the
buffered output and will write its output before the buffer gets
flushed.

* geesh/shell.scm (exec-utility): Flush all ports before forking.
(%subshell): Ditto.
2018-12-05 16:08:09 -05:00
Timothy Sample 4b7d2404f3 Fix handling of non-whitespace IFS separators
* geesh/word.scm (string-tokenize*): New function.
(split-fields): Use it to handle non-whitespace IFS separators.
* tests/word.scm: Add tests.
2018-12-05 16:08:09 -05:00
Timothy Sample 3e6d7830a9 Fix splitting nested words
* geesh/word.scm (split-fields): Move handling of list words into
'wedge-apart' so that they get handled in recursive calls.
* tests/word.scm: Add a test for this.
2018-12-05 16:08:01 -05:00