From 1a9b14a9dc158b019bfcd571eb00f22a724067d7 Mon Sep 17 00:00:00 2001 From: Jephte CLAIN Date: Thu, 2 Oct 2014 14:53:05 +0400 Subject: [PATCH] =?UTF-8?q?possiblit=C3=A9=20de=20faire=20un=20mapping=20s?= =?UTF-8?q?ur=20les=20attributs=20avec=20format/formatcsv=20utiliser=20arr?= =?UTF-8?q?ay=5Flen()=20l=C3=A0=20ou=20c'est=20possible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ulib/ldif | 75 +++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/lib/ulib/ldif b/lib/ulib/ldif index bb88e21..c998830 100644 --- a/lib/ulib/ldif +++ b/lib/ulib/ldif @@ -538,19 +538,22 @@ function tl_format() { return 1 fi [ -n "$shell" ] && show_headers=1 - - local match_attr="$(def_match_attr "$@")" - local define_attrs= - local i=1 attr - for attr in "$@"; do - define_attrs="$define_attrs attrs[$i] = \"$attr\"; " - let i=$i+1 - done - awkrun -f show_headers="$show_headers" asep="$asep" vsep="$vsep" qsep="$qsep" escape="$escape" \ - shell="$shell" cmd="$cmd" begincmd="$begincmd" endcmd="$endcmd" localvars="$localvars" ' + local -a attrs mapattrs + local attr mapattr + for attr in "$@"; do + splitpair "$attr" mapattr attr + [ -n "$attr" ] || attr="$mapattr" + attr="${attr//:/}"; array_add attrs "$attr" + mapattr="${mapattr//:/}"; array_add mapattrs "$mapattr" + done + local match_attr="$(def_match_attr "${attrs[@]}")" + + awkrun -f show_headers:int="$show_headers" asep="$asep" vsep="$vsep" qsep="$qsep" escape="$escape" \ + shell:int="$shell" cmd="$cmd" begincmd="$begincmd" endcmd="$endcmd" localvars:int="$localvars" \ + attrs[@] mapattrs[@] ' function reset_values() { - for (i = 1; i <= length(attrs); i++) { + for (i = 1; i <= attrs_count; i++) { attr = norm_attr(attrs[i]) values[attr] = "" } @@ -565,21 +568,21 @@ function dump_headers() { if (shell) { if (localvars) { print "local attributes index" - for (i = 1; i <= length(attrs); i++) { - attr = norm_attr(attrs[i]) + for (i = 1; i <= attrs_count; i++) { + attr = norm_attr(mapattrs[i]) print "local " attr } } line = line "attributes=(" - for (i = 1; i <= length(attrs); i++) { - attr = norm_attr(attrs[i]) + for (i = 1; i <= attrs_count; i++) { + attr = norm_attr(mapattrs[i]) if (i > 1) line = line " " line = line quote_shell(attr) } line = line ")" } else { - for (i = 1; i <= length(attrs); i++) { - attr = attrs[i] + for (i = 1; i <= attrs_count; i++) { + attr = mapattrs[i] if (i > 1) line = line asep line = line attr } @@ -598,14 +601,14 @@ function __should_quote(s) { function dump_values(atbegin, value) { if (shell) { print "index=" nr - for (i = 1; i <= length(attrs); i++) { + for (i = 1; i <= attrs_count; i++) { attr = norm_attr(attrs[i]) print attr "=" values[attr] } if (!atbegin && cmd != "") print cmd } else { line = "" - for (i = 1; i <= length(attrs); i++) { + for (i = 1; i <= attrs_count; i++) { attr = norm_attr(attrs[i]) if (i > 1) line = line asep value = values[attr] @@ -635,7 +638,6 @@ function parse_value(line) { '"$match_attr"' BEGIN { - '"$define_attrs"' if (show_headers) dump_headers() reset_values() nr = -1 @@ -721,33 +723,33 @@ function tl_parsecsv() { awkrun -f \ basedn="$basedn" rdnattr="$rdnattr" \ - skip_lines="$skip_lines" parse_headers="$parse_headers" "headers[@]=headers" \ + skip_lines:int="$skip_lines" parse_headers:int="$parse_headers" headers[@] \ vsep="$vsep" ' -BEGIN { skip_lines = skip_lines + 0; parse_headers = parse_headers + 0 } NR <= skip_lines { next } parse_headers { array_parsecsv(headers, $0) + headers_count = array_len(headers) parse_headers = 0 next } { - array_parsecsv(attrs, $0) + array_parsecsv(attrvalues, $0) rdnindex = key_index(rdnattr, headers) - rdnvalue = attrs[rdnindex] + rdnvalue = attrvalues[rdnindex] if (basedn != "") { print "dn: " rdnattr "=" rdnvalue "," basedn } else { print "dn: " rdnvalue } - nbheaders = length(headers) - for (i = 1; i <= nbheaders; i++) { - attr = attrs[i] - split(attr, values, vsep) + for (i = 1; i <= headers_count; i++) { attr = headers[i] if (basedn == "" && attr == rdnattr) { continue # ne pas écrire deux fois le même attribut } - for (j = 1; j <= length(values); j++) { + attrvalue = attrvalues[i] + split(attrvalue, values, vsep) + values_count = array_len(values) + for (j = 1; j <= values_count; j++) { print attr ": " values[j] } } @@ -781,12 +783,12 @@ function tl_parsecsvmod() { awkrun -f \ basedn="$basedn" rdnattr="$rdnattr" \ - skip_lines="$skip_lines" parse_headers="$parse_headers" "headers[@]=headers" \ + skip_lines:int="$skip_lines" parse_headers:int="$parse_headers" headers[@] \ vsep="$vsep" ' -BEGIN { skip_lines = skip_lines + 0; parse_headers = parse_headers + 0 } NR <= skip_lines { next } parse_headers { array_parsecsv(headers, $0) + headers_count = array_len(headers) parse_headers = 0 next } @@ -794,10 +796,9 @@ parse_headers { array_parsecsv(attrvalues, $0) rdnindex = key_index(rdnattr, headers) rdnvalue = attrvalues[rdnindex] - nbheaders = length(headers) # déterminer d"abord s"il faut écrire l"objet printobj = 0 - for (i = 1; i <= nbheaders; i++) { + for (i = 1; i <= headers_count; i++) { attr = headers[i] if (attr == rdnattr) continue attrvalue = attrvalues[i] @@ -814,7 +815,7 @@ parse_headers { } # maintenant, écrire l"objet le cas échéant if (printobj) { - for (i = 1; i <= nbheaders; i++) { + for (i = 1; i <= headers_count; i++) { attr = headers[i] if (attr == rdnattr) continue attrvalue = attrvalues[i] @@ -829,7 +830,8 @@ parse_headers { } print changetype ": " attr split(attrvalue, values, vsep) - for (j = 1; j <= length(values); j++) { + values_count = array_len(values) + for (j = 1; j <= values_count; j++) { print attr ": " values[j] } print "-" @@ -898,6 +900,9 @@ awkrun [args] exemple l'option -i de sed format [options] attrs... Formater le flux au format ldif en données tabulaires. + Chaque attribut peut être de la forme newname:oldname pour avoir des + en-têtes avec un nom différent des noms des attributs. L'effet est le même + qu'avec awkcsv -m -h, --show-headers Afficher les en-têtes. Par défaut, les en-têtes ne sont pas affichés. -F ASEP