| 1 |
(* |
| 2 |
* This file is part of Coccinelle, licensed under the terms of the GPL v2. |
| 3 |
* See copyright.txt in the Coccinelle source code for more information. |
| 4 |
* The Coccinelle source code can be obtained at http://coccinelle.lip6.fr |
| 5 |
*) |
| 6 |
|
| 7 |
module UI = User_input |
| 8 |
|
| 9 |
(* ------------------------------------------------------------------------- *) |
| 10 |
|
| 11 |
(* Interactive commandline mode for getting (local) user input. *) |
| 12 |
|
| 13 |
(* ------------------------------------------------------------------------- *) |
| 14 |
(* GENERAL PURPOSE FUNCTIONS *) |
| 15 |
|
| 16 |
(* name of the file to save to. Is set at the entry point. *) |
| 17 |
let name = ref "" |
| 18 |
|
| 19 |
(* persistence *) |
| 20 |
let write_file ~file s = |
| 21 |
let chan = |
| 22 |
try open_out file |
| 23 |
with Sys_error msg -> |
| 24 |
let msg = "Error: Invalid filename: " ^ file ^ ". Message: " ^ msg in |
| 25 |
failwith msg in |
| 26 |
try |
| 27 |
(output_string chan s; close_out chan) |
| 28 |
with Sys_error msg -> |
| 29 |
let msg = "Error: failed writing to " ^ file ^ ". Message: " ^ msg in |
| 30 |
failwith msg |
| 31 |
|
| 32 |
(* termination *) |
| 33 |
let exit() = print_string "\n~*~ GOODBYE! ~*~\n"; exit 0 |
| 34 |
|
| 35 |
(* allow the user to save the current progress in a config file *) |
| 36 |
let save t = |
| 37 |
|
| 38 |
(* unparse the current configurations and save them to a file *) |
| 39 |
let save' t name = |
| 40 |
let unp = UI.unparse t in |
| 41 |
let _ = write_file ~file:name unp in |
| 42 |
print_string ("\nSaved progress to " ^ name ^ "!\n" ^ "-------------\n") in |
| 43 |
|
| 44 |
let name = !name in |
| 45 |
let _ = print_string ("\nSave progress to " ^name^"?\n") in |
| 46 |
let _ = print_string ("Options:\n" ^ |
| 47 |
" Type y(es) or press <enter> to save.\n"^ |
| 48 |
" Type n(o) to not save.\n"^ |
| 49 |
" Write another filename to save to.\n") in |
| 50 |
|
| 51 |
match Stdcompat.String.lowercase_ascii (read_line()) with |
| 52 |
| "" | "y" | "yes" -> save' t name |
| 53 |
| "n" | "no" -> () |
| 54 |
| file -> save' t file |
| 55 |
|
| 56 |
let get_input_save t = |
| 57 |
let input = read_line() in |
| 58 |
if input = "q()" then (save t; exit()) else input |
| 59 |
|
| 60 |
let get_input() = |
| 61 |
let input = read_line() in |
| 62 |
if input = "q()" then exit() else input |
| 63 |
|
| 64 |
let print_error msg = |
| 65 |
print_string "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"; |
| 66 |
print_string msg; |
| 67 |
print_string "\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n" |
| 68 |
|
| 69 |
|
| 70 |
(* ------------------------------------------------------------------------- *) |
| 71 |
(* INTERACTIVE USER INPUT GETTERS *) |
| 72 |
|
| 73 |
let rec get_description() = |
| 74 |
print_string "\nWrite a description for the Coccinelle script (required):\n"; |
| 75 |
match get_input() with |
| 76 |
| "" -> get_description() |
| 77 |
| x -> x |
| 78 |
|
| 79 |
let rec get_confidence() = |
| 80 |
print_string ("\nSpecify a confidence level for the script (required):\n" ^ |
| 81 |
"Options: l(ow), m(oderate), h(igh).\n"); |
| 82 |
try |
| 83 |
UI.Confidence.from_string (get_input()) |
| 84 |
with UI.Confidence.Not_confidence _ -> |
| 85 |
(print_error "Error: Ill-formed confidence level!"; get_confidence()) |
| 86 |
|
| 87 |
let get_limitations t = |
| 88 |
let rec get first t = |
| 89 |
print_string ("\nSpecify a" ^ (if first then "" else "nother") ^ |
| 90 |
" limitation for the script or press <enter> to continue:\n"); |
| 91 |
match get_input_save t with |
| 92 |
| "" -> t |
| 93 |
| x -> get false (UI.add_limit x t) in |
| 94 |
get true t |
| 95 |
|
| 96 |
let get_keywords t = |
| 97 |
print_string ("\nSpecify keywords for the script or press <enter> to " ^ |
| 98 |
"continue:\n"); |
| 99 |
let keys = get_input_save t in |
| 100 |
UI.set_keys keys t |
| 101 |
|
| 102 |
let get_options t = |
| 103 |
print_string ("\nSpecify options for the script or press <enter> to " ^ |
| 104 |
"continue:\n"); |
| 105 |
let options = get_input_save t in UI.set_options options t |
| 106 |
|
| 107 |
let get_url t = |
| 108 |
print_string ("\nSpecify an URL for the script or press <enter> to " ^ |
| 109 |
"continue:\n"); |
| 110 |
let url = get_input_save t in UI.set_url url t |
| 111 |
|
| 112 |
let get_authors t = |
| 113 |
let rec get first t = |
| 114 |
print_string ("\nSpecify an" ^ (if first then "" else "other") ^ |
| 115 |
" author for the script or press <enter> to continue:\n" ^ |
| 116 |
"Standard format is: <author name>, <affiliation>. <license>.\n"); |
| 117 |
match get_input_save t with |
| 118 |
| "" -> t |
| 119 |
| x -> get false (UI.add_author x t) in |
| 120 |
get true t |
| 121 |
|
| 122 |
let get_comments t = |
| 123 |
print_string ("\nWrite any further comments for the script or press " ^ |
| 124 |
"<enter> to continue:\n"); |
| 125 |
let comments = get_input_save t in UI.set_comments comments t |
| 126 |
|
| 127 |
(* get org or report msg. strict denotes whether it is required. *) |
| 128 |
let rec get_message pmsg strict t = |
| 129 |
print_string pmsg; |
| 130 |
let msg = get_input_save t in |
| 131 |
if msg = "" && strict then |
| 132 |
|
| 133 |
(* message required but none given; try again *) |
| 134 |
get_message pmsg strict t |
| 135 |
|
| 136 |
else begin |
| 137 |
|
| 138 |
(* if there are no format variables, just use the message alone*) |
| 139 |
let pcts = UI.count_format_vars msg in |
| 140 |
if pcts = 0 then (msg,[]) |
| 141 |
else begin |
| 142 |
|
| 143 |
(* there were format variables; allow user to declare them *) |
| 144 |
let _ = print_string ( |
| 145 |
"\nDeclare the " ^(string_of_int pcts) ^" variable(s) " ^ |
| 146 |
"used in the message, in order, separated by comma.\n" ^ |
| 147 |
"Inherited metavariables are declared by <rulename>.<metavarname>.\n" |
| 148 |
) in |
| 149 |
let mv = get_input_save t in |
| 150 |
let mv = Str.split (Str.regexp " *, *") mv in |
| 151 |
|
| 152 |
(* if user declared format variables successfully, return *) |
| 153 |
if List.length mv = pcts |
| 154 |
then (msg, mv) |
| 155 |
else |
| 156 |
|
| 157 |
(* the number of format variables did not match the actual message *) |
| 158 |
let _ = print_error ( |
| 159 |
"Error: Ill-formed message; " ^ |
| 160 |
"number of format variables does not match number of declared " ^ |
| 161 |
"metavariables. Try again." |
| 162 |
) in |
| 163 |
get_message pmsg strict t |
| 164 |
end |
| 165 |
end |
| 166 |
|
| 167 |
(* gets rulename if there is currently none (rule starting on ...). |
| 168 |
* if there is one, just return the same. |
| 169 |
*) |
| 170 |
let get_name r t = |
| 171 |
let rec get_name' r = |
| 172 |
print_string ("\nSpecify a name for the " ^ r ^ ":\n"); |
| 173 |
let newnm = get_input_save t in |
| 174 |
try |
| 175 |
UI.check_name newnm t; |
| 176 |
newnm |
| 177 |
with Failure m -> |
| 178 |
print_error m; |
| 179 |
get_name' r in |
| 180 |
if String.contains r ' ' then get_name' r else r |
| 181 |
|
| 182 |
(* returns rule mapped to user-specified org and report messages + new name. *) |
| 183 |
let get_rule (rulename : string) (t : UI.t) = |
| 184 |
let _ = print_string ("\nHandling rule \"" ^ rulename ^ "\" ...") in |
| 185 |
let nm = get_name rulename t in |
| 186 |
let _ = print_string ("\n~ Getting messages for org and report mode ~\n\n" ^ |
| 187 |
"No quotes necessary around the message.\n" ^ |
| 188 |
"It is possible to include formatted variables like %s (Python style), " ^ |
| 189 |
"you will get a chance to declare them afterwards.\n" ^ |
| 190 |
"Example message: Unneeded variable \\\"%s\\\". Return \\\"%s\\\".\n" ^ |
| 191 |
"Example format variables: x, other_rule.y. \n") in |
| 192 |
let org = get_message ("\nRule \"" ^ nm ^ "\". Write a message"^ |
| 193 |
" for org mode:\n") true t in |
| 194 |
let (repmsg,repmvs) = get_message ("\nRule \"" ^ nm ^ "\". Write a message" ^ |
| 195 |
" for report or press <enter> to reuse the org message.\n") false t in |
| 196 |
let report = if repmsg = "" then org else (repmsg,repmvs) in |
| 197 |
UI.Rule.make ~rule_name:nm ~org ~report |
| 198 |
|
| 199 |
|
| 200 |
(* ------------------------------------------------------------------------- *) |
| 201 |
(* ENTRY POINT *) |
| 202 |
|
| 203 |
let interact ~rule_names ~config_name = |
| 204 |
name := config_name; |
| 205 |
print_string ("\n~*~ WELCOME TO SPGEN INTERACTIVE MODE ~*~\n\n" ^ |
| 206 |
"At any time, write q() to quit the program.\n"); |
| 207 |
let description = get_description() in |
| 208 |
let confidence = get_confidence() in |
| 209 |
let t = UI.make ~description ~confidence in |
| 210 |
let t = get_limitations t in |
| 211 |
let t = get_keywords t in |
| 212 |
let t = get_options t in |
| 213 |
let t = get_authors t in |
| 214 |
let t = get_url t in |
| 215 |
let t = get_comments t in |
| 216 |
let rec add t r = |
| 217 |
try UI.add_rule ~rule_name:r (get_rule r t) t |
| 218 |
with Failure msg -> |
| 219 |
print_error msg; |
| 220 |
add t r in |
| 221 |
let t = List.fold_left add t rule_names in |
| 222 |
let _ = save t in |
| 223 |
t |