File: gen_trcs

package info (click to toggle)
trscripts 1.18%2Bnmu3
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,016 kB
  • sloc: sh: 88,553; makefile: 36
file content (426 lines) | stat: -rwxr-xr-x 8,170 bytes parent folder | download | duplicates (6)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/bin/bash

#     gen_trcs  -- generates `trcs' program
#     Copyright (C) 2000 Anton Kirilov Zinoviev

#     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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#     Contact the author by e-mail: anton@lml.bas.bg, zinoviev@fmi.uni-sofia.bg

cat <<"PART"
#!/bin/bash

#     trcs  -- translates codesets
#     Copyright (C) 2000 Anton Kirilov Zinoviev

#     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 `bash' interpreter; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#     Contact the author by e-mail: anton@lml.bas.bg, zinoviev@fmi.uni-sofia.bg

function err() {
    echo -e $1 1>&2
    echo "trcs: Try \`trcs --help' for more information." 1>&2
    exit 2
}

function get_opt() {
    case $1 in
    -)
	op=arg;;
    --*)
	op=`expr $1 : '\(.*\)=.*' '|' $1`;;
    -*)
	op=`expr $1 : '\(-.\).*'`;;
    *)
	op=arg;;
    esac
}

function get_arg() {
    case $1 in
    -)
	arg='-'
	shift;;
    --*)
	arg=`expr $1 : '[^=]*=\(.*\)'`
	if [ -z "$arg" ]; then 
	    arg=$2
	    shift; shift;
	else
	    shift;
	fi;;
    -*)
	arg=`expr $1 : '-.\(.*\)'`
	if [ -z "$arg" ]; then 
	    arg=$2
	    shift; shift;
	else
	    shift;
	fi;;
    *)
	arg=$1
	shift;;
    esac
    if [ -z "$arg" ]; then
	err "trcs: Option \`${op}' requires an argument."
    fi
    args="$*"
}

function no_arg() {
    case $1 in
    --*)
	local x=`expr $1 : '[^=]*=\(.*\)'`
	if [ -n "$x" ]; then 
	    err "trcs: Option \`$1' doesn't allow an argument."
	fi;;
    -*)
	local x=`expr $1 : '-.\(.*\)'`
	if [ -n "$x" ]; then
	    x=-$x
	fi;;
    *) ;;
    esac
    shift
    args="$x $*"
}

PART

cat $1| 
while read a b c; do
    echo "function $a() {"
    echo "cat <<\"EOF\""
    cat <<"CONTROL"
0 0000
1 0001
2 0002
3 0003
4 0004
5 0005
6 0006
7 0007
8 0008
9 0009
10 000A
11 000B
12 000C
13 000D
14 000E
15 000F
16 0010
17 0011
18 0012
19 0013
20 0014
21 0015
22 0016
23 0017
24 0018
25 0019
26 001A
27 001B
28 001C
29 001D
30 001E
31 001F
CONTROL
    cat $3/$a |
	awk '{if ($1>=32) {print}}'
    echo "EOF"
    echo "}"
    echo
done

cat <<"PART"
fromcs=cp1251
tocs=cp1251

while [ -n "$1" ]; do
    get_opt $*
    case $op in
    -C|--copyright)
	no_arg $* ; set -- $args
	cat<<"EOF"
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, 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 `bash' interpreter; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
EOF
	exit;;
    -h|--help)
	no_arg $* ; set -- $args
	cat<<"EOF"
trcs 1.5
Usage: trcs [OPTION]... [FILE]...

       -C, --copyright     display copying conditions and warranty information
       -s, --gen-script    generates recoding script for latter usage
       -l, --list          list all known codesets with their aliases
       -h, --help          display this help and exit
-f BEFORE, --from=BEFORE   codeset of the source
 -t AFTER, --to=AFTER      codeset of the output

If none of -C, -s, -l, -h and their equivalents is given each FILE will be
read assuming it is coded with codeset BEFORE.  On standard output it will be
recoded so to use codeset AFTER.  If there is no FILE given `trcs' will act as
filter.  Both BEFORE and AFTER are case insensitive and default to `cp1251'.

Examples:
           trcs -fcyrillic -t koi8-r letter.txt
           trcs --from cp437 --to=mik screen.txt

Warning: Most often recoding is irreversible operation !
EOF
	exit;;
    -l|--list)
	no_arg $* ; set -- $args
PART

echo "        echo \"Known codesets with their aliases:  \""
echo -n "        echo '"

cat $1 | 
while read a b c; do
    echo -n "$a"
    if [ "$b" = "|" ]; then
	echo -n "  "
    else
	echo -n ' ('`echo $b | sed 's/|/, /g'`')  '
    fi
done
echo "'"
echo "        exit;;"

cat<<"PART"
    -f|--from)
	get_arg $* ; set -- $args
	fromcs=$arg;;
    -t|--to)
	get_arg $* ; set -- $args
	tocs=$arg;;
    -s|--gen-script)
	no_arg $* ; set -- $args
	gen=yes;;
    arg)
	get_arg $* ; set -- $args
	files="${files} ${arg}";;
    *)	err "trcs: Unknown option \`$1'." ;;
    esac
done

[ -z "$files" ] && files="-"

fromcs=`echo ${fromcs}|tr A-Z a-z`
tocs=`echo ${tocs}|tr A-Z a-z`

case ${fromcs} in
PART

cat $1 |
while read a b c; do
    if [ "$b" = "|" ]; then
	b="$a"
    else
	b="$a|$b"
    fi
    echo "$b)"
    echo "    fromcs=$a;;"
done

cat <<"PART"
*)
    err "trcs: Unknown codeset \`${fromcs}'.\ntrcs: Try trcs --list";;
esac

case ${tocs} in
PART

cat $1 |
while read a b c; do
    if [ "$b" == "|" ]; then
	b="$a"
    else
	b="$a|$b"
    fi
    echo "$b)"
    echo "    tocs=$a;;"
done

cat <<"PART"
*)
    err "trcs: Unknown codeset \`${tocs}'.\ntrcs: Try trcs --list";;
esac

tmpfile=/tmp/csrec.$$

[ -f ${tmpfile}.1 ] && err "trcs: The file ${tmpfile}.1 exists !? Try again"
touch ${tmpfile}.1

[ -f ${tmpfile}.2 ] && err "trcs: The file ${tmpfile}.2 exists !? Try again"
touch ${tmpfile}.2

trap "rm ${tmpfile}.1 ${tmpfile}.2; exit 2" HUP INT TERM

{
cat <<'EOF'
function printcode(code)
{
    if(code==45)
	{
	    printf "'\\-'";
	}
    else if(code==91)
	{
	    printf "'\\['";
	}
    else
	{
	    printf "'\\%o'", code;
	}
}

BEGIN {
EOF

${fromcs} |
awk '{printf "tu[%s]=\"%s\";\n", $1, $2;}'

${tocs} |
awk '{printf "ut[\"%s\"]=%s;\n", $2, $1;}'

cat <<EOF

PART

cat $2 | awk '{
    split($0,a);
    j=1;
    for(i=2;i<=NF;i++) 
        if ((a[1] !~ /^00[01]/) && (a[i] !~ /^00[01]/)) {
	    printf "fb[\"%s\",%s] = \"%s\";\n", a[1], j, a[i];
            j=j+1;
        }
}'

cat <<"PART"

for(i=32;i<256;i++)
    {
	t="";
	if (tu[i]!="")
	    {
		u=tu[i];
		if(ut[u]!="")
		    t=ut[u];
		else 
		    {
			j=1;
			while(fb[u,j]!="" && t=="")
			    {
				if(ut[fb[u,j]]!="")
				    t=ut[fb[u,j]];
				j++;
			    }
		    }
	    }
	if (t=="")
	    t=ut["003F"];
	if (t=="")
	    t=ut["0020"];
	tt[i]=t;
	if(tt[i]!=i)
	    notrivial="yes";
    }

print "#!/bin/sh";
print;
print "trap \"exit 0\" PIPE"
print;
if(notrivial!="yes")
    print "cat \"\$@\"";
else
    {
	print "cat \"\$@\" | tr \\\\";

	j=0;
	for(i=32;i<256;i++)
	    {
		if(j==11)
		    {
			printf "\\\\\n";
			j=0;
		    }
		if(tt[i]!=i)
		    {
			printcode(i);
			j++;
		    }
	    }

	print "  \\\\";

	j=0;
	for(i=32;i<256;i++)
	    {
		if(j==11)
		    {
			printf "\\\\\n";
			j=0;
		    }
		if(tt[i]!=i)
		    {
			printcode(tt[i]);
			j++;
		    }
	    }
    }
print;
}
EOF
} >${tmpfile}.1

awk -f ${tmpfile}.1 >${tmpfile}.2

if [ "$gen" = yes ] ; then
    cat ${tmpfile}.2
else
    sh ${tmpfile}.2 ${files}
fi

rm ${tmpfile}.[12]

PART