-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibnvl.bash
169 lines (142 loc) · 2.6 KB
/
libnvl.bash
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
#
# NVL CODE START
# NVL version 0, lib version 1.0
# NAME strict level 0 excluding \0
# VAL strict level 0
# Dependencies: wc, dd
#
nvl_reset()
{
NVL_NAME=
NVL_LEN=
NVL_VAL=
NVL_LINENO=1
NVL_ERRMSG=
}
nvl_reset
nvl_header_read()
{
local line
nvl_reset
if ! IFS= read -r -n5 line; then
if [ -z "$line" ]; then
return 1
fi
fi
NVL_LINENO=$((NVL_LINENO + 1))
if [ "$line" = "NVL0" ]; then
return 0
fi
NVL_ERRMSG="NVL: there is no nvl formatted data"
return 1
}
nvl_header_write()
{
echo "NVL0"
}
nvl_rec_read()
{
local cnt fname
NVL_NAME=
NVL_LEN=
NVL_VAL=
if ! IFS= read -r -d= NVL_NAME; then
if [ "$NVL_NAME" ]; then
NVL_ERRMSG="NVL: can't find name"
fi
return 1
fi
if ! IFS= read -r -d: NVL_LEN; then
NVL_ERRMSG="NVL: $NVL_NAME: can't find length"
return 1
fi
if [ -z "$NVL_LEN" ]; then
IFS= read -r NVL_VAL
NVL_LINENO=$((NVL_LINENO + 1))
fname=`_nvl_get_keyval "$NVL_NAME" "$@"`
if [ "$fname" ]; then
echo -n "$NVL_VAL" > "$fname"
NVL_VAL=""
fi
return 0
fi
if ! [ "$NVL_LEN" -ge 0 ]; then
NVL_ERRMSG="NVL: $NVL_NAME: length should be positive integer"
return 1
fi
fname=`_nvl_get_keyval "$NVL_NAME" "$@"`
if [ "$fname" ]; then
if ! dd of="$fname" bs=1 count=$NVL_LEN status=none; then
NVL_ERRMSG="NVL: $NVL_NAME: can't save the value to the file '$fname'"
return 1
fi
cnt=`wc -l "$fname"`
cnt=${cnt%% *}
else
NVL_VAL=`dd bs=1 count=$NVL_LEN status=none; echo -n E`
if [ $? -ne 0 ]; then
NVL_ERRMSG="NVL: $NVL_NAME: can't read the value"
return 1
fi
NVL_VAL=${NVL_VAL%E}
cnt=`echo -n "$NVL_VAL" | wc -l`
fi
NVL_LINENO=$((NVL_LINENO + cnt))
# Read the last newline
read -r -d "" -N1 cnt
if [ $? -ne 0 ]; then
NVL_ERRMSG="NVL: $NVL_NAME: can't read newline after value"
return 1
fi
if [ "$cnt" != "
" ]; then
NVL_ERRMSG="NVL: $NVL_NAME: there is no newline after value"
return 1
fi
NVL_LINENO=$((NVL_LINENO + 1))
return 0
}
_nvl_get_keyval()
{
local kv key want
want="$1"
shift
for kv in "$@"; do
key="${kv%%=*}"
if [ "$key" = "$want" ] || [ "$key" = "*" ]; then
echo "${kv#*=}"
return 0
fi
done
echo ""
return 1
}
nvl_rec_write()
{
local N=$1
local V=$2
if [ "${N#*=}" != $N ]; then
NVL_ERRMSG="NVL: name shouldn't contain '=': '$N'"
return 1
fi
echo "$N=`echo $V | wc -c`:$V"
return 0
}
nvl_rec_write_from_file()
{
local N=$1
local V=$2
if [ "${N#*=}" != $N ]; then
NVL_ERRMSG="NVL: name shouldn't contain '=': '$N'"
return 1
fi
echo -n "$N="`wc -c "$V"`:
if ! cat "$V"; then
NVL_ERRMSG="NVL: can't write a value from a file '$V'"
return 1
fi
return 0
}
#
# NVL CODE END
#