package morsmall

  1. Overview
  2. Docs

Source file safePrinter.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
(***************************************************************************)
(*                                 Morsmall                                *)
(*                      A concise AST for POSIX shell                      *)
(*                                                                         *)
(*  Copyright (C) 2017,2018,2019 Yann Régis-Gianas, Ralf Treinen,          *)
(*  Nicolas Jeannerod                                                      *)
(*                                                                         *)
(*  This program is free software: you can redistribute it and/or modify   *)
(*  it under the terms of the GNU General Public License as published by   *)
(*  the Free Software Foundation, either version 3 of the License, or      *)
(*  (at your option) any later version.                                    *)
(*                                                                         *)
(*  This program is distributed in the hope that it will be useful,        *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of         *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *)
(*  GNU General Public License for more details.                           *)
(*                                                                         *)
(*  You should have received a copy of the GNU General Public License      *)
(*  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *)
(***************************************************************************)

let fpf = Format.fprintf
open AST

(* AST.name *)

let rec pp_name ppf =
  fpf ppf "%s"

(* AST.word_component *)

and pp_word_component ppf = function (*FIXME*)
  | Literal literal ->
     fpf ppf "%s" literal
  | DoubleQuoted _word ->
     assert false
  | Variable (variable, attribute) ->
     assert (attribute = NoAttribute);
     fpf ppf "${%s}" variable
  | Subshell command_list ->
     fpf ppf "$(%a)" pp_command'_list command_list
  | GlobAll ->
     fpf ppf "*"
  | GlobAny ->
     fpf ppf "?"
  | BracketExpression _bracket_expression ->
     assert false

(* AST.word *)

and pp_word ppf = function
  | [] -> assert false
  | [e] -> pp_word_component ppf e
  | h :: q -> fpf ppf "%a%a" pp_word_component h pp_word q

and pp_word' ppf word' =
  Location.on_located (pp_word ppf) word'

and pp_words ppf = function
  | [] -> ()
  | [word] ->
     pp_word ppf word
  | word :: words ->
     fpf ppf "%a %a"
       pp_word word
       pp_words words

and pp_words' ppf = function
  | [] -> ()
  | [word'] ->
     pp_word' ppf word'
  | word' :: words' ->
     fpf ppf "%a %a"
       pp_word' word'
       pp_words' words'

(* AST.pattern *)

and pp_pattern ppf = function
  | [] -> ()
  | [word] ->
     pp_word ppf word
  | word :: pattern ->
     fpf ppf "%a|%a"
       pp_word word
       pp_pattern pattern

and pp_pattern' ppf pattern' =
  Location.on_located (pp_pattern ppf) pattern'

(* AST.assignement *)

and pp_assignment ppf (variable, word) =
  fpf ppf "%a=%a"
    pp_name variable
    pp_word word

and pp_assignment' ppf assignment' =
  Location.on_located (pp_assignment ppf) assignment'

and pp_assignments ppf = function
  | [] -> ()
  | [assignment] ->
     pp_assignment ppf assignment
  | assignment :: assignments ->
     fpf ppf "%a %a"
       pp_assignment assignment
       pp_assignments assignments

and pp_assignments' ppf = function
  | [] -> ()
  | [assignment'] ->
     pp_assignment' ppf assignment'
  | assignment' :: assignments' ->
     fpf ppf "%a %a"
       pp_assignment' assignment'
       pp_assignments' assignments'

and pp_redirection_kind ppf k =
  fpf ppf "%s"
    (match k with
     | Input -> "<" | InputDuplicate -> "<&"
     | Output -> ">" | OutputDuplicate -> ">&" | OutputAppend -> ">>"
     | InputOutput -> "<>" | OutputClobber -> ">|")

(* AST.program *)

and pp_program ppf = function
  | [] -> ()
  | [command'] ->
     pp_command' ppf command'
  | command' :: program ->
     fpf ppf "%a@\n%a"
       pp_command' command'
       pp_program program

(* AST.command *)

and pp_command ppf (command : command) =
  fpf ppf "{ ";
  (
    match command with

    | Async command ->
       pp_command ppf command

    | Seq (command1, command2) ->
       fpf ppf "%a;%a"
         pp_command' command1
         pp_command' command2

    | And (command1, command2) ->
       fpf ppf "%a&&%a"
         pp_command' command1
         pp_command' command2

    | Or (command1, command2) ->
       fpf ppf "%a||%a"
         pp_command' command1
         pp_command' command2

    | Not command ->
       fpf ppf "! %a"
         pp_command' command

    | Pipe (command1, command2) ->
       fpf ppf "%a|%a"
         pp_command' command1
         pp_command' command2

    | Subshell command ->
       fpf ppf "(%a)"
         pp_command' command

    | If (test, body, None) ->
       fpf ppf "if %a;then %a;fi"
         pp_command' test
         pp_command' body

    | If (test, body, Some rest) ->
       fpf ppf "if %a;then %a;else %a;fi"
         pp_command' test
         pp_command' body
         pp_command' rest

    | For (variable, None, body) ->
       fpf ppf "for %a;do %a;done"
         pp_name variable
         pp_command' body

    | For (variable, Some words, body) ->
       fpf ppf "for %a in %a;do %a;done"
         pp_name variable
         pp_words words
         pp_command' body

    | Case (word, items) ->
       fpf ppf "case %a in" pp_word word;
       List.iter
         (fun item ->
           match item.Location.value with
           | (pattern, None) ->
              fpf ppf " %a) ;;" pp_pattern' pattern
           | (pattern, Some body') ->
              fpf ppf " %a) %a;;" pp_pattern' pattern pp_command' body')
         items;
       fpf ppf " esac"

    | While (test, body) ->
       fpf ppf "while %a;do %a;done"
         pp_command' test
         pp_command' body

    | Until (test, body) ->
       fpf ppf "until %a;do %a;done"
         pp_command' test
         pp_command' body

    | Function (name, body) ->
       fpf ppf "%a()%a"
         pp_name name
         pp_command' body

    | Simple ([], []) ->
       failwith "SafePrinter.pp_command': ill-formed command: Simple([], [])"
    | Simple ([], words) ->
       fpf ppf "%a" pp_words' words
    | Simple (assignments, words) ->
       fpf ppf "%a %a"
         pp_assignments' assignments
         pp_words' words

    | Redirection (command, descr, kind, file) ->
       (* The space is required because "the [descriptor] must be delimited from any preceding text". *)
       fpf ppf "%a %d%a%a"
         pp_command' command
         descr
         pp_redirection_kind kind
         pp_word file

    | HereDocument (command, descr, content) ->
       (* if content.value.[String.length content.value - 1] <> '\n' then
        *   failwith "SafePrinter.pp_command': ill-formed here-document: the content must end with a newline"; *) (*FIXME*)
       let eof = "EOF" in (*FIXME*)
       fpf ppf "%a %d<<%s\n%a%s\n"
         pp_command' command
         descr
         eof
         pp_word' content
         eof
  );
  fpf ppf "%s}" (match command with Async _ -> "&" | HereDocument _ -> "" | _ -> ";")

and pp_command' ppf command' =
  Location.on_located (pp_command ppf) command'

and pp_command_list ppf = function
  | [] -> ()
  | [command] ->
     pp_command ppf command
  | command :: command_list ->
     fpf ppf "%a@\n%a"
       pp_command command
       pp_command_list command_list

and pp_command'_list ppf = function
  | [] -> ()
  | [command'] -> pp_command' ppf command'
  | command' :: command'_list ->
     fpf ppf "%a@\n%a"
       pp_command' command'
       pp_command'_list command'_list