=== modified file 'escape.pl' --- escape.pl 2012-05-17 18:49:59 +0000 +++ escape.pl 2011-11-26 19:12:18 +0000 @@ -1,6 +1,6 @@ % vim: ft=prolog textwidth=80 tabstop=4 softtabstop=4 shiftwidth=4 expandtab -:- module(escape, [str/2, is_str/1, quoting/4, escape/3]). +:- module(escape, [str/2, quoting/4, escape/3]). :- use_module(library(apply)). :- use_module(library(lists)). @@ -34,10 +34,7 @@ % str_dlist(X, Joined, End) :- - ( is_list(X) -> ( maplist(integer, X) - -> append(X, End, Joined) - ; throw(not_a_proper_string(X)) - ) + ( is_list(X) -> append(X, End, Joined) ; atom(X) -> name(X, String), append(String, End, Joined) ; X = variable(Name) -> Joined = [variable(Name)|End] @@ -53,8 +50,6 @@ str(X, S) :- str_dlist(X, S, []). -is_str(X) :- atom(X); X=variable(_); X=_+_; X=_+/+_; (is_list(X), maplist(integer, X)). - %%%%%%%%%%%%%%%%%% % Shell escaping % %%%%%%%%%%%%%%%%%% === modified file 'shell.pl' --- shell.pl 2012-05-17 18:49:59 +0000 +++ shell.pl 2012-05-17 01:48:06 +0000 @@ -1,13 +1,12 @@ % vim: ft=prolog textwidth=80 tabstop=4 softtabstop=4 shiftwidth=4 expandtab :- module(shell, [ - op(600,xfy,($:-)), -% op(800,fx,(#)), + op(800,xfy,($:-)), ($:-)/2, +% op(800,fx,(#)), (#)/1, cmd_format/2, - cmd_print/1, cmds_print/1, cmd_repr/1, - cmds_repr/1 + cmds_repr/1, ]). :- use_module(library(apply)). @@ -32,10 +31,6 @@ { str(In, Out) }, prepend(Out). -runnable([_|_]). -runnable(echo(_)). -runnable(chroot(_,_)). - cmd_escape_runnable([Arg]) --> escape_dcg(Arg). cmd_escape_runnable([Arg1,Arg2|Rest]) --> @@ -49,13 +44,7 @@ cmd_escape_runnable(chroot(Dir, Args)) --> cmd_escape_runnable([Dir +/+ 'chroot.sh'|Args]). -cmd_escape_backticks(Runnable) --> - { runnable(Runnable) }, - prepend("$( "), - cmd_escape_runnable(Runnable), - prepend(" )"). - -check_returncode --> prepend(" || exit $?\n"). +check_returncode --> [" || exit $?\n"]. cmd_tokens(Runnable) --> cmd_escape_runnable(Runnable), @@ -73,28 +62,29 @@ escape_dcg(File), check_returncode. -cmd_tokens(Variable = Value) --> - str_dcg(Variable), - prepend("="), - ( { runnable(Value) } - -> prepend("\""), - cmd_escape_backticks(Value), - prepend("\"") - ; { is_str(Value) } - -> escape_dcg(Value) - ; { Value = Source $:- Default } - -> prepend("\"${"), - str_dcg(Source), - prepend(":-"), - ( { runnable(Default) } - -> cmd_escape_backticks(Default) - ; { is_str(Value) } - -> escape_dcg(Default) - ; throw(incomprehensible_default(Default)) - ), - prepend("}\"") - ; throw(incomprehensible_rhs(Value)) - ), +cmd_tokens(Variable = Runnable) --> + str_dcg(Variable), + prepend("=\"$( "), + cmd_escape_runnable(Runnable), + prepend(" )\""), + check_returncode. + +cmd_tokens(Variable = Source $:- String) --> + str_dcg(Variable), + prepend("=\"${"), + str_dcg(Source), + prepend(":-"), + escape_dcg(String) + prepend("}\""), + check_returncode. + +cmd_tokens(Variable = Source $:- Runnable) --> + str_dcg(Variable), + prepend("=\"${"), + str_dcg(Source), + prepend(":-$( "), + cmd_escape_runnable(String) + prepend(" )}\""), check_returncode. cmd_tokens(Variable = array(Runnable)) --> @@ -104,9 +94,9 @@ prepend(") )"), check_returncode. -cmd_tokens(comment(Text)) --> +cmd_tokens(# Text) --> prepend("\n# "), - { str(Text, String) }, + { str(Text, String) } make_comment(String), prepend("\n"). @@ -117,30 +107,30 @@ cmd_tokens(run_outarray(List, Var)) --> cmd_tokens(Var = array(List)). cmd_tokens(echo_append(String, File)) --> cmd_tokens(echo(String) >> File). cmd_tokens(echo_replace(String, File)) --> cmd_tokens(echo(String) > File). -% cmd_tokens(comment(Text)) --> cmd_tokens(# Text). +cmd_tokens(comment(Text)) --> cmd_tokens(# Text). make_comment([ ]) --> []. make_comment([Char|Rest]) :- ( { [Char] = "\n" } - -> prepend("\n# ") + -> prepend("\n# "), ; prepend(Char) ), make_comment(Rest). -cmd_format(Cmd, String) :- phrase(cmd_tokens(Cmd), String, []). - -cmd_print(Cmd) :- - cmd_format(Cmd, String), !, - name(Name, String), - write(Name). +cmd_format(Cmd, String) :- phrase(cmd_tokens(Cmd), [], String). cmds_print(Commands) :- - maplist(cmd_print, Commands). + maplist(cmd_format, Commands, Lines), !, + str_join("\n", Lines, String), + name(Name,String), + nl, + write(Name), + nl. -print_solution(String) :- - name(Text, String), +print_solution(Solution) :- + str_join('\n', Solution, String), name(Text, String), write(Text), - nl. + nl, nl. cmd_repr(Command) :- write('representation of: '), writeq(Command), write(' ::'), nl,