From 0fd4acb85e7ca2665bfd92dd78396d9e90565ee5 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Fri, 18 Apr 2008 19:23:30 -0500 Subject: [PATCH] shell: Add pipeline syntax --- extra/shell/parser/parser.factor | 78 +++++++++++++++++++------------- extra/shell/shell.factor | 34 +++++++++++--- 2 files changed, 74 insertions(+), 38 deletions(-) diff --git a/extra/shell/parser/parser.factor b/extra/shell/parser/parser.factor index 4e3ae8069c..c5c352c313 100644 --- a/extra/shell/parser/parser.factor +++ b/extra/shell/parser/parser.factor @@ -1,27 +1,43 @@ -USING: kernel arrays strings sequences sequences.deep peg peg.ebnf ; +USING: kernel arrays strings sequences sequences.deep accessors peg peg.ebnf + newfx ; IN: shell.parser ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -TUPLE: incantation command stdin stdout background ; - -! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - +TUPLE: basic-expr command stdin stdout background ; +TUPLE: pipeline-expr commands stdin stdout background ; TUPLE: single-quoted-expr expr ; TUPLE: double-quoted-expr expr ; TUPLE: back-quoted-expr expr ; TUPLE: glob-expr expr ; - -TUPLE: variable-expr expr ; +TUPLE: variable-expr expr ; +TUPLE: factor-expr expr ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -: single-quoted-expr boa ; -: double-quoted-expr boa ; -: back-quoted-expr boa ; -: glob-expr boa ; +: ast>basic-expr ( ast -- obj ) first4 basic-expr boa ; + +: ast>pipeline-expr ( ast -- obj ) + pipeline-expr new + over [ 1st ] [ 4th [ 1st ] map ] [ 5th ] tri suffix prefix-on >>commands + over 2nd >>stdin + over 5th >>stdout + swap 6th >>background ; + +: ast>single-quoted-expr ( ast -- obj ) + 2nd >string single-quoted-expr boa ; + +: ast>double-quoted-expr ( ast -- obj ) + 2nd >string double-quoted-expr boa ; + +: ast>back-quoted-expr ( ast -- obj ) + 2nd >string back-quoted-expr boa ; + +: ast>glob-expr ( ast -- obj ) flatten concat glob-expr boa ; + +: ast>variable-expr ( ast -- obj ) 2nd variable-expr boa ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -33,45 +49,43 @@ tab = "\t" white = (space | tab) -whitespace = (white)* => [[ drop ignore ]] +_ = (white)* => [[ drop ignore ]] -squote = "'" +sq = "'" +dq = '"' +bq = "`" -single-quoted = squote (!(squote) .)* squote => [[ second >string ]] +single-quoted = sq (!(sq) .)* sq => [[ ast>single-quoted-expr ]] +double-quoted = dq (!(dq) .)* dq => [[ ast>double-quoted-expr ]] +back-quoted = bq (!(bq) .)* bq => [[ ast>back-quoted-expr ]] -dquote = '"' - -double-quoted = dquote (!(dquote) .)* dquote => [[ second >string ]] - -bquote = "`" - -back-quoted = bquote (!(bquote) .)* bquote => [[ second >string ]] - -variable = "$" other => [[ second variable-expr boa ]] +variable = "$" other => [[ ast>variable-expr ]] glob-char = ("*" | "?") non-glob-char = !(glob-char | white) . -glob-beginning-string = (non-glob-char)* [[ >string ]] +glob-beginning-string = (non-glob-char)* => [[ >string ]] -glob-rest-string = (non-glob-char)+ [[ >string ]] +glob-rest-string = (non-glob-char)+ => [[ >string ]] -glob = glob-beginning-string glob-char (glob-rest-string | glob-char)* => [[ flatten concat ]] +glob = glob-beginning-string glob-char (glob-rest-string | glob-char)* => [[ ast>glob-expr ]] -other = (!(white | "&" | ">" | ">>" | "<") .)+ => [[ >string ]] +other = (!(white | "&" | ">" | ">>" | "<" | "|") .)+ => [[ >string ]] element = (single-quoted | double-quoted | back-quoted | variable | glob | other) -to-file = ">" whitespace other => [[ second ]] +command = (element _)+ -in-file = "<" whitespace other => [[ second ]] +to-file = ">" _ other => [[ second ]] +in-file = "<" _ other => [[ second ]] +ap-file = ">>" _ other => [[ second ]] -ap-file = ">>" whitespace other => [[ second ]] +basic = _ command _ (in-file)? _ (to-file | ap-file)? _ ("&")? => [[ ast>basic-expr ]] -redirection = (in-file)? whitespace (to-file | ap-file)? +pipeline = _ command _ (in-file)? _ "|" _ (command _ "|" _)* command _ (to-file | ap-file)? _ ("&")? => [[ ast>pipeline-expr ]] -line = (element whitespace)+ (in-file)? whitespace (to-file | ap-file)? whitespace ("&")? => [[ first4 incantation boa ]] +submission = (pipeline | basic) ;EBNF diff --git a/extra/shell/shell.factor b/extra/shell/shell.factor index f36b6f6400..7482f388f1 100644 --- a/extra/shell/shell.factor +++ b/extra/shell/shell.factor @@ -49,22 +49,44 @@ METHOD: expand { object } ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -: run-incantation ( incantation -- ) +: run-sword ( basic-expr -- ) command>> unclip "shell" lookup execute ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: run-foreground ( process -- ) + [ try-process ] [ print-error drop ] recover ; + +: run-background ( process -- ) run-detached drop ; + +: run-basic-expr ( basic-expr -- ) over command>> expansion >>command over stdin>> >>stdin over stdout>> >>stdout swap background>> - [ run-detached drop ] - [ [ try-process ] [ print-error drop ] recover ] + [ run-background ] + [ run-foreground ] if ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -: chant ( incantation -- ) +: basic-chant ( basic-expr -- ) dup command>> first swords member-of? - [ command>> unclip "shell" lookup execute ] - [ run-incantation ] + [ run-sword ] + [ run-basic-expr ] + if ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: pipeline-chant ( pipeline-chant -- ) + drop "ix: pipelines not supported" print ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: chant ( obj -- ) + dup basic-expr? + [ basic-chant ] + [ pipeline-chant ] if ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!