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
|
#!/usr/bin/mawk -We
# *********************************************************************
# Written by and copyright Carlo Strozzi <carlos@linux.it>.
#
# tabletosh: convert the last record of a NoSQL table into sh(1)
# variable assignments.
# Copyright (C) 1998-2001 Carlo Strozzi <carlos@linux.it>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# 2001-02-13 Ported to NoSQL v3
# 2001-02-21 Added option '-s'
# 2001-04-18 Added inline help
# 2001-08-17 Added stdio portability
# 2001-09-03 Added options '-C' and '-L'
#
# $Id$
# *********************************************************************
BEGIN {
NULL = "" ; FS = OFS = "\t"
while (ARGV[++i] != NULL) {
if (ARGV[i] == "-p" || ARGV[i] == "--prefix") prefix = ARGV[++i]
else if ( ARGV[i] == "-c" || ARGV[i] == "--compute" ) comp = 1
else if (ARGV[i] == "-t" || ARGV[i] == "--trim") trim = 1
else if (ARGV[i] == "-T" || ARGV[i] == "--trunc") trunc = ARGV[++i]
else if (ARGV[i] == "-K" || ARGV[i] == "--key") key = ARGV[++i]
else if (ARGV[i] == "-R" || ARGV[i] == "--readonly") readonly = 1
else if (ARGV[i] == "-e" || ARGV[i] == "--export") export = 1
else if (ARGV[i] == "-i" || ARGV[i] == "--input") i_file = ARGV[++i]
else if (ARGV[i] == "-o" || ARGV[i] == "--output") o_file = ARGV[++i]
else if (ARGV[i] == "-s" || ARGV[i] == "--strip-names") {
s_pattern = ARGV[++i]
}
else if (ARGV[i] == "-C" || ARGV[i] == "--max-cols") {
max_cols = ARGV[++i]
}
else if (ARGV[i] == "-L" || ARGV[i] == "--max-rows") {
max_rows = ARGV[++i]
}
else if (ARGV[i] == "-h" || ARGV[i] == "--help") {
system("grep -v '^#' @DOCPATH@/tabletosh.txt")
rc = 1
exit(rc)
}
}
ARGC = 1 # Fix argv[]
if (o_file == NULL) o_file = "@STDOUT@"
if (i_file != NULL) { ARGV[1] = i_file; ARGC = 2 }
}
#
# Main loop
#
# Column names.
NR == 1 {
i = 0
while (++i <= NF) {
if (!P[$i]) {
if (i == 1) auto_col = $i
else auto_col = auto_col " " $i
}
if (pick_last) P[$i] = i
else {
if (!P[$i]) P[$i] = i
}
}
split(auto_col, c_names, " ")
next
}
# Dashline.
NR == 2 { next }
# Table body.
{
if (key != NULL) {
if ($1 == key) got_key = 1
else next
} else {
if (max_rows && NR > max_rows) next
}
# Make sure we output something valid in any case.
if (!comp) printf("{ :; ") > o_file
i = 0
while (P[c_names[++i]]) { # Process each field in turn.
if (max_cols && i > max_cols) break
field = $P[c_names[i]]
if (trunc) field = substr(field,1,trunc) # Truncate if requested.
# Unescape tabs and newlines first.
if (!comp) field = cNoSqlUnescape(field)
if (trim) { sub(/^ +/, NULL, field); sub(/ +$/, NULL, field) }
# Escape sh(1)/mawk(1) special characters in string.
field = cMawkBug(field) # Escape backslashes
gsub(/"/, "\\\"", field) # Escape double-quotes
s_name = c_names[i]
if (s_pattern != NULL) sub(s_pattern, NULL, s_name)
if (!comp) {
gsub(/\$/, "\\$", field)
gsub(/`/, "\\`", field)
if (export) printf(" export") > o_file
printf(" %s%s=\"%s\";", prefix, s_name, field) > o_file
if (readonly) printf(" readonly %s%s;", prefix, s_name) > o_file
} else {
printf(" %s%s=\"%s\";", prefix, s_name, field) > o_file
}
}
if (!comp) printf(" }") > o_file
printf("\n") > o_file
}
END {
if (rc) exit(rc)
if ((NR < 3) || (key != NULL && !got_key)) {
# No data rows in the input table; print empty assignments.
# Make sure we output something valid in any case.
if (!comp) printf("{ :;") > o_file
i = 0
while (P[c_names[++i]]) {
if (max_cols && i > max_cols) break
s_name = c_names[i]
if (s_pattern != NULL) sub(s_pattern, NULL, s_name)
if (!comp) {
if (export) printf(" export") > o_file
printf(" %s%s=\"%s\";", prefix, s_name, field) > o_file
if (readonly)
printf(" readonly %s%s;", prefix, s_name) > o_file
} else {
printf(" %s%s=\"%s\";", prefix, s_name, field) > o_file
}
}
if (!comp) printf(" }") > o_file
printf("\n") > o_file # In case the output goes to a file.
}
}
# *********************************************************************
# cNoSqlUnescape(string)
#
# Takes a string and translates any unescaped '\t' and '\n' strings into
# physical tabs and newlines respectively. Returns the converted string.
# *********************************************************************
function cNoSqlUnescape(s, S,i,s_length,a,escaped) {
s_length = split(s, a, "")
s_length++ # Cope with s_length==1
while (++i <= s_length) {
if (a[i] == "\\" && !escaped) { escaped = 1; continue }
if (a[i] == "n" && escaped) { S = S "\n"; escaped = 0; continue }
if (a[i] == "t" && escaped) { S = S "\t"; escaped = 0; continue }
if (escaped) { S = S "\\" a[i]; escaped = 0; continue }
S = S a[i]
}
return S
}
# *********************************************************************
# cMawkBug(string)
#
# Takes a string and turns all '\' characters into their escaped form
# '\\'. Returns the escaped string. This could be done with just a gsub(),
# but mawk(1) has a bug that makes it behave differently from other awk
# implementations:
#
# gsub(/\\/, "\\\\", field) # This works with both gawk(1)
# # and the original nawk(1).
#
# gsub(/\\/, "\\\\\\", field) # This works just with mawk(1),
# # otherwise it produces more
# # backslashes than necessary,
# # which looks rather obvious.
#
# *********************************************************************
function cMawkBug(s, a,i,j,S) {
i = split(s, a, "\\")
S = a[1]
for (j = 2; j <= i; j++) S = S "\\\\" a[j]
return S
}
#
# End of program.
#
|