|
Packit Service |
a2ae7a |
(*
|
|
Packit Service |
a2ae7a |
Module: Util
|
|
Packit Service |
a2ae7a |
Generic module providing useful primitives
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
Author: David Lutterkort
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
About: License
|
|
Packit Service |
a2ae7a |
This file is licensed under the LGPLv2+, like the rest of Augeas.
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
module Util =
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(*
|
|
Packit Service |
a2ae7a |
Variable: del_str
|
|
Packit Service |
a2ae7a |
Delete a string and default to it
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
Parameters:
|
|
Packit Service |
a2ae7a |
s:string - the string to delete and default to
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let del_str (s:string) = del s s
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(*
|
|
Packit Service |
a2ae7a |
Variable: del_ws
|
|
Packit Service |
a2ae7a |
Delete mandatory whitespace
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let del_ws = del /[ \t]+/
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(*
|
|
Packit Service |
a2ae7a |
Variable: del_ws_spc
|
|
Packit Service |
a2ae7a |
Delete mandatory whitespace, default to single space
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let del_ws_spc = del_ws " "
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(*
|
|
Packit Service |
a2ae7a |
Variable: del_ws_tab
|
|
Packit Service |
a2ae7a |
Delete mandatory whitespace, default to single tab
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let del_ws_tab = del_ws "\t"
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(*
|
|
Packit Service |
a2ae7a |
Variable: del_opt_ws
|
|
Packit Service |
a2ae7a |
Delete optional whitespace
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let del_opt_ws = del /[ \t]*/
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(*
|
|
Packit Service |
a2ae7a |
Variable: eol
|
|
Packit Service |
a2ae7a |
Delete end of line, including optional trailing whitespace
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let eol = del /[ \t]*\n/ "\n"
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(*
|
|
Packit Service |
a2ae7a |
Variable: doseol
|
|
Packit Service |
a2ae7a |
Delete end of line with optional carriage return,
|
|
Packit Service |
a2ae7a |
including optional trailing whitespace
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let doseol = del /[ \t]*\r?\n/ "\n"
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(*
|
|
Packit Service |
a2ae7a |
Variable: indent
|
|
Packit Service |
a2ae7a |
Delete indentation, including leading whitespace
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let indent = del /[ \t]*/ ""
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* Group: Comment
|
|
Packit Service |
a2ae7a |
This is a general definition of comment
|
|
Packit Service |
a2ae7a |
It allows indentation for comments, removes the leading and trailing spaces
|
|
Packit Service |
a2ae7a |
of comments and stores them in nodes, except for empty comments which are
|
|
Packit Service |
a2ae7a |
ignored together with empty lines
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: comment_generic_seteol
|
|
Packit Service |
a2ae7a |
Map comments and set default comment sign
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
let comment_generic_seteol (r:regexp) (d:string) (eol:lens) =
|
|
Packit Service |
a2ae7a |
[ label "#comment" . del r d
|
|
Packit Service |
a2ae7a |
. store /([^ \t\r\n].*[^ \t\r\n]|[^ \t\r\n])/ . eol ]
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: comment_generic
|
|
Packit Service |
a2ae7a |
Map comments and set default comment sign
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
let comment_generic (r:regexp) (d:string) =
|
|
Packit Service |
a2ae7a |
comment_generic_seteol r d doseol
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: comment
|
|
Packit Service |
a2ae7a |
Map comments into "#comment" nodes
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let comment = comment_generic /[ \t]*#[ \t]*/ "# "
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: comment_noindent
|
|
Packit Service |
a2ae7a |
Map comments into "#comment" nodes, without indentation
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let comment_noindent = comment_generic /#[ \t]*/ "# "
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: comment_eol
|
|
Packit Service |
a2ae7a |
Map eol comments into "#comment" nodes
|
|
Packit Service |
a2ae7a |
Add a space before # for end of line comments
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let comment_eol = comment_generic /[ \t]*#[ \t]*/ " # "
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: comment_or_eol
|
|
Packit Service |
a2ae7a |
A <comment_eol> or <eol>, with an optional empty comment *)
|
|
Packit Service |
a2ae7a |
let comment_or_eol = comment_eol | (del /[ \t]*(#[ \t]*)?\n/ "\n")
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: comment_multiline
|
|
Packit Service |
a2ae7a |
A C-style multiline comment *)
|
|
Packit Service |
a2ae7a |
let comment_multiline =
|
|
Packit Service |
a2ae7a |
let mline_re = (/[^ \t\r\n].*[^ \t\r\n]|[^ \t\r\n]/ - /.*\*\/.*/) in
|
|
Packit Service |
a2ae7a |
let mline = [ seq "mline"
|
|
Packit Service |
a2ae7a |
. del /[ \t\r\n]*/ "\n"
|
|
Packit Service |
a2ae7a |
. store mline_re ] in
|
|
Packit Service |
a2ae7a |
[ label "#mcomment" . del /[ \t]*\/\*/ "/*"
|
|
Packit Service |
a2ae7a |
. counter "mline"
|
|
Packit Service |
a2ae7a |
. mline . (eol . mline)*
|
|
Packit Service |
a2ae7a |
. del /[ \t\r\n]*\*\/[ \t]*\r?\n/ "\n*/\n" ]
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: comment_c_style
|
|
Packit Service |
a2ae7a |
A comment line, C-style *)
|
|
Packit Service |
a2ae7a |
let comment_c_style =
|
|
Packit Service |
a2ae7a |
comment_generic /[ \t]*\/\/[ \t]*/ "// "
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: comment_c_style_or_hash
|
|
Packit Service |
a2ae7a |
A comment line, C-style or hash *)
|
|
Packit Service |
a2ae7a |
let comment_c_style_or_hash =
|
|
Packit Service |
a2ae7a |
comment_generic /[ \t]*((\/\/)|#)[ \t]*/ "// "
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: empty_generic
|
|
Packit Service |
a2ae7a |
A generic definition of <empty>
|
|
Packit Service |
a2ae7a |
Map empty lines, including empty comments *)
|
|
Packit Service |
a2ae7a |
let empty_generic (r:regexp) =
|
|
Packit Service |
a2ae7a |
[ del r "" . del_str "\n" ]
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* Variable: empty_generic_re *)
|
|
Packit Service |
a2ae7a |
let empty_generic_re = /[ \t]*#?[ \t]*/
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: empty
|
|
Packit Service |
a2ae7a |
Map empty lines, including empty comments *)
|
|
Packit Service |
a2ae7a |
let empty = empty_generic empty_generic_re
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* Variable: empty_c_style_re *)
|
|
Packit Service |
a2ae7a |
let empty_c_style_re = /[ \t]*((\/\/)|(\/\*[ \t]*\*\/))?[ \t]*/
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: empty_c_style
|
|
Packit Service |
a2ae7a |
Map empty lines, including C-style empty comment *)
|
|
Packit Service |
a2ae7a |
let empty_c_style = empty_generic empty_c_style_re
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: empty_any
|
|
Packit Service |
a2ae7a |
Either <empty> or <empty_c_style> *)
|
|
Packit Service |
a2ae7a |
let empty_any = empty_generic (empty_generic_re | empty_c_style_re)
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: empty_generic_dos
|
|
Packit Service |
a2ae7a |
A generic definition of <empty> with dos newlines
|
|
Packit Service |
a2ae7a |
Map empty lines, including empty comments *)
|
|
Packit Service |
a2ae7a |
let empty_generic_dos (r:regexp) =
|
|
Packit Service |
a2ae7a |
[ del r "" . del /\r?\n/ "\n" ]
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: empty_dos *)
|
|
Packit Service |
a2ae7a |
let empty_dos =
|
|
Packit Service |
a2ae7a |
empty_generic_dos /[ \t]*#?[ \t]*/
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: Split *)
|
|
Packit Service |
a2ae7a |
(* Split (SEP . ELT)* into an array-like tree where each match for ELT *)
|
|
Packit Service |
a2ae7a |
(* appears in a separate subtree. The labels for the subtrees are *)
|
|
Packit Service |
a2ae7a |
(* consecutive numbers, starting at 0 *)
|
|
Packit Service |
a2ae7a |
let split (elt:lens) (sep:lens) =
|
|
Packit Service |
a2ae7a |
let sym = gensym "split" in
|
|
Packit Service |
a2ae7a |
counter sym . ( [ seq sym . sep . elt ] ) *
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* View: delim *)
|
|
Packit Service |
a2ae7a |
let delim (op:string) = del (/[ \t]*/ . op . /[ \t]*/)
|
|
Packit Service |
a2ae7a |
(" " . op . " ")
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* Group: Exclusions
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
Variable: stdexcl
|
|
Packit Service |
a2ae7a |
Exclusion for files that are commonly not wanted/needed
|
|
Packit Service |
a2ae7a |
*)
|
|
Packit Service |
a2ae7a |
let stdexcl = (excl "*~") .
|
|
Packit Service |
a2ae7a |
(excl "*.rpmnew") .
|
|
Packit Service |
a2ae7a |
(excl "*.rpmsave") .
|
|
Packit Service |
a2ae7a |
(excl "*.dpkg-old") .
|
|
Packit Service |
a2ae7a |
(excl "*.dpkg-new") .
|
|
Packit Service |
a2ae7a |
(excl "*.dpkg-bak") .
|
|
Packit Service |
a2ae7a |
(excl "*.dpkg-dist") .
|
|
Packit Service |
a2ae7a |
(excl "*.augsave") .
|
|
Packit Service |
a2ae7a |
(excl "*.augnew") .
|
|
Packit Service |
a2ae7a |
(excl "*.bak") .
|
|
Packit Service |
a2ae7a |
(excl "*.old") .
|
|
Packit Service |
a2ae7a |
(excl "#*#")
|