-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigure
executable file
·481 lines (402 loc) · 11.9 KB
/
configure
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/bin/sh
#
# pksh - The Packet Shell
#
# R. Carbone ([email protected])
# 2008-2009, 2022
#
# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
#
#
# This script attempts to configure and compile [pksh] on your Linux system.
#
# Based on:
# rlibc - C library of useful functions
# tcsh - C shell with file name completion and command line editing
# pcap - Packet Capture library
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
#
# Addionatal Software requirements:
# awk
# git
#
AWK=/usr/bin/gawk
if ! [ -x "$(command -v $AWK)" ]; then
echo 'Error: $AWK is not installed.' >&2
exit 1
fi
if ! [ -x "$(command -v git)" ]; then
echo 'Error: git is not installed.' >&2
exit 1
fi
# =-=-=-=-=-=-=-=-
# The name of the game
PACKAGE=pksh
# Main header file
HEADER=pksh.h
# Project directories tree
ROOTDIR=`pwd`
SRCDIR=$ROOTDIR
# Level 1
EXTDIR=$ROOTDIR/src
PUBDIR=$ROOTDIR/3rdparty
SHDIR=$ROOTDIR/shell
#
# Required git modules
#
RLIBCGIT=https://github.com/rcarbone/rlibc
TCSHGIT=https://github.com/tcsh-org/tcsh
# Directories
RLIBCDIR=$PUBDIR/rlibc/src
TCSHDIR=$PUBDIR/tcsh
# Required Libraries
RLIBC=$RLIBCDIR/librlibc.a
PKSHLIB=../src/libpksh.a
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-==-==-==-==-==-==-
# All tcsh builtin extensions (please keep them alphabetically sorted)
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-==-==-==-==-==-==-
EXTRACMDS="about"
EXTRACMDS="$EXTRACMDS bytes"
EXTRACMDS="$EXTRACMDS help"
EXTRACMDS="$EXTRACMDS license"
EXTRACMDS="$EXTRACMDS packets"
EXTRACMDS="$EXTRACMDS pkarp"
EXTRACMDS="$EXTRACMDS pkclose"
EXTRACMDS="$EXTRACMDS pkdev"
EXTRACMDS="$EXTRACMDS pkenable"
EXTRACMDS="$EXTRACMDS pkfilter"
EXTRACMDS="$EXTRACMDS pkfinger"
EXTRACMDS="$EXTRACMDS pkhelp"
EXTRACMDS="$EXTRACMDS pkhosts"
EXTRACMDS="$EXTRACMDS pklast"
EXTRACMDS="$EXTRACMDS pkopen"
EXTRACMDS="$EXTRACMDS pkstatus"
EXTRACMDS="$EXTRACMDS pkswap"
EXTRACMDS="$EXTRACMDS pkuptime"
EXTRACMDS="$EXTRACMDS pkwho"
EXTRACMDS="$EXTRACMDS protocols"
# EXTRACMDS="$EXTRACMDS services"
EXTRACMDS="$EXTRACMDS throughput"
EXTRACMDS="$EXTRACMDS version"
# EXTRACMDS="$EXTRACMDS when"
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Functions
# rlibc
do_rlibc()
{
cd $PUBDIR
echo "rlibc - C library of useful functions"
if [ ! -d $RLIBCDIR ]; then
echo -n " Cloning rlibc ... "
git clone -q $RLIBCGIT
echo "done!"
fi
cd $RLIBCDIR
if [ ! -f $RLIBC ]; then
echo -n " Compiling ... "
make 1> /dev/null 2>&1
echo "done!"
else
echo " Found in $RLIBC"
fi
}
# tcsh
do_tcsh()
{
cd $PUBDIR
echo "tcsh - C shell with file name completion and command line editing"
if [ ! -d $TCSHDIR ]; then
echo -n " Cloning tcsh ... "
git clone -q $TCSHGIT
echo "done!"
fi
}
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Eval max length of commands
maxn()
{
longest=0
for cmd in $EXTRACMDS; do
n=${#cmd}
if [ $longest -lt $n ]; then
longest=$n
fi
done
echo $longest
}
#
# Return the line in the tcsh file [sh.init.c] where a specific command
# *must* be inserted to have the given extension $cmd full funtional.
#
# Warning: order in the array bfunc[] of [biltins] in file [sh.init.c]
# is relevant in order to have the extensions available.
#
linenumber()
{
cmd=`echo $1`
file=`echo $2`
# Please keep them alphabetically sorted
case $cmd in
about) before=alias ;;
bytes) before=case ;;
help) before=history ;;
license) after=kill ;;
packets) after=onintr ;;
pkarp) after=packets ;;
pkclose) after=pkarp ;;
pkdev) after=pkclose ;;
pkenable) after=pkdev ;;
pkfilter) after=pkenable ;;
pkfinger) after=pkfilter ;;
pkhelp) after=pkfinger ;;
pkhosts) after=pkhelp ;;
pklast) after=pkhosts ;;
pkopen) after=pklast ;;
pkstatus) after=pkopen ;;
pkswap) after=pkstatus ;;
pkuptime) after=pkswap ;;
pkwho) after=pkuptime ;;
protocols) after=printenv ;;
# services) after=protocols ;;
throughput) before=time ;;
version) before=wait ;;
# when) before=where ;;
*) before=xxx after=xxx ;;
esac
row=0
if [ "$before" != "" -a "$before" != "xxx" ]; then
row=`grep -n \"$before\" $file | head -1 | cut -d":" -f 1`
elif [ "$after" != "" -a "$after" != "xxx" ]; then
row=`grep -n \"$after\" $file | head -1 | cut -d":" -f 1`
row=`expr $row + 1`
fi
echo $row
}
#
# Main processing
#
# Development tree
echo "$PACKAGE development tree"
echo " ."
echo " |-- `basename $PUBDIR`"
echo " | |-- rlibc - (github clone)"
echo " | \`-- `basename $TCSHDIR` - (github clone)"
echo " |-- src - Implementation of builtins for network monitoring"
echo " \`-- shell - Hack of the tcsh for network monitoring"
echo
# Move to the root download directory
if [ ! -d $PUBDIR ]; then
mkdir $PUBDIR
fi
# rlibc
do_rlibc
# tcsh
do_tcsh
# -=-=-=-=-=-=-=-=-=-=
# Start patching tcsh
# -=-=-=-=-=-=-=-=-=-=
# evaluate EXTRA items to be included in Makefile.in
USRLIBS="$PKSHLIB $RLIBC"
EXTRAFLAGS="-I$RLIBCDIR -I../src"
EXTRALIBS="$USRLIBS -lm -lpcap -lpthread"
# extra source files
EXTRASRCS="$HEADER $PACKAGE-tcsh-wrap.c"
# extra object files
EXTRAOBJS="$PACKAGE-tcsh-wrap.o"
# -=-=-=-=-=-=-=-=-=-=-=-=-=
# Move to the source directory
cd $SRCDIR
# Create and populate the main [shell] directory
if [ ! -d $SHDIR ]; then
echo
echo -n "Populating main shell directory [$SHDIR] ..."
cp -r $TCSHDIR $SHDIR
rm -rf $SHDIR/.git $SHDIR/.gitignore
echo "Done!"
fi
echo
echo "Patching now tcsh sources ............."
#
# Patch file $HEADER
#
echo -n " patching header file $HEADER ........ "
cp $EXTDIR/$HEADER $SHDIR/$HEADER
for cmd in `echo $EXTRACMDS | sort`; do
cat $SHDIR/$HEADER | echo "int $PACKAGE"_$cmd "(int argc, char * argv []);" >> $SHDIR/$HEADER
done
echo "done!"
#
# Patch src file tcsh-wrap.c
#
echo -n " patching source file tcsh-wrap.c ... "
cp $EXTDIR/tcsh-wrap.c $SHDIR/$PACKAGE-tcsh-wrap.c
echo >> $SHDIR/$PACKAGE-tcsh-wrap.c
for cmd in `echo $EXTRACMDS`; do
echo "void tcsh_$cmd (Char ** v, struct command * c) { USE(c); tcsh_xxx (v, pksh_$cmd); }" >> $SHDIR/$PACKAGE-tcsh-wrap.c
done
echo "done!"
# Move to the build directory
cd $SHDIR
#
# Patch config/linux for tool chain changes
#
echo -n " patching config/linux .............. "
if [ ! -f config/linux.ORG ]; then
cp config/linux config/linux.ORG
fi
cp config/linux.ORG config/linux.tmp
cat config/linux.tmp | sed -e 's|SVID_SOURCE|DEFAULT_SOURCE|g' -e 's|BSD_SOURCE|DEFAULT_SOURCE|g' > config/linux
rm -f config/linux.tmp
echo "done!"
#
# Patch Makefile.in
#
# 1. change the name to the executable from tcsh to $PACKAGE
# 2. change value of CPPFLAGS
# 3. add search path for project header file to EXTRAFLAGS
# 4. add more to EXTRALIBS
# 5. add ${EXTRASRCS} to the macro SHSRCS
# 6. add ${EXTRAOBJS} to the macro SHOBJS
# 7. add macro EXTRASRCS= with project source files
# 8. add macro EXTRAOBJS= with project object files
# 9. change name of the executable (no extension of Unix)
# 10. change the main target to use name $PACKAGE
#
echo -n " patching Makefile.in ............... "
if [ ! -f Makefile.in.ORG ]; then
cp Makefile.in Makefile.in.ORG
fi
# 1. change the name to the executable
cp Makefile.in Makefile.in.tmp
cat Makefile.in.tmp | sed -e "s|/tcsh|/$PACKAGE|" > Makefile.in
# 2. change value of CPPFLAGS
cp Makefile.in Makefile.in.tmp
cat Makefile.in.tmp | sed -e "s|CPPFLAGS=.*|CPPFLAGS=-I./|" > Makefile.in
# 3. add search path for project header file to EXTRAFLAGS
cp Makefile.in Makefile.in.tmp
cat Makefile.in.tmp | sed -e "s|EXTRAFLAGS =\(.*\)|EXTRAFLAGS =\1 $EXTRAFLAGS|" > Makefile.in
# 4. add more to EXTRALIBS
cp Makefile.in Makefile.in.tmp
cat Makefile.in.tmp | sed -e "s|EXTRALIBS =\(.*\)|EXTRALIBS =\1 $EXTRALIBS|" > Makefile.in
# 5. add $EXTRASRCS to the macro SHSRCS
cp Makefile.in Makefile.in.tmp
cat Makefile.in.tmp | sed -e 's|SHSRCS=\(.*\)|SHSRCS=\1 ${EXTRASRCS}|' > Makefile.in
# 6. add $EXTRAOBJS to the macro SHOBJS
cp Makefile.in Makefile.in.tmp
cat Makefile.in.tmp | sed -e 's|OBJS= ${SHOBJS}\(.*\)|OBJS= ${SHOBJS}\1 ${EXTRAOBJS}|' > Makefile.in
# 7. add macro EXTRASRCS= with project source files
line=`grep -n "SHSRCS=" Makefile.in | cut -d":" -f 1`
cp Makefile.in Makefile.in.tmp
cat Makefile.in.tmp | sed "$line i EXTRASRCS= $EXTRASRCS" > Makefile.in
# 8. add macro EXTRAOBJS= with project object files
line=`grep -n "OBJS=" Makefile.in | tail -1 | cut -d":" -f 1`
cp Makefile.in Makefile.in.tmp
cat Makefile.in.tmp | sed "$line i EXTRAOBJS= $EXTRAOBJS" > Makefile.in
# 9. change name of the executable
cp Makefile.in Makefile.in.tmp
cat Makefile.in.tmp | sed -e "s|tcsh\$(EXEEXT)|$PACKAGE|" > Makefile.in
# 10. change the main target to use name $PACKAGE
cp Makefile.in Makefile.in.tmp
cat Makefile.in.tmp | sed -e "s|$PACKAGE:.*|$PACKAGE: \${OBJS} \${LIBES} ${USRLIBS} |" > Makefile.in
rm -f Makefile.in.tmp
# rm -f Makefile
echo "done!"
#
# Patch sh.c
#
# o add [$PACKAGE] initialization function
#
echo -n " patching sh.c ...................... "
if [ ! -f sh.c.ORG ]; then
cp sh.c sh.c.ORG
fi
# Add [$PACKAGE] call to the inizialization function
cp sh.c.ORG sh.c.tmp
line=`grep -n 'process(setintr)' sh.c.tmp | cut -d":" -f 1`
cat sh.c.tmp | sed "$line i {extern void pksh_init (char * progname, int quiet); pksh_init (progname, fast); }" > sh.c
rm -f sh.c.tmp
echo "done!"
#
# Patch sh.decls.h
#
# o add function declarations
#
echo -n " patching sh.decls.h ................ "
if [ ! -f sh.decls.h.ORG ]; then
cp sh.decls.h sh.decls.h.ORG
fi
cp sh.decls.h.ORG sh.decls.h
for cmd in $EXTRACMDS; do
cp sh.decls.h sh.decls.h.tmp
line=`grep -n '#define' sh.decls.h.tmp | head -1 | cut -d":" -f 1`
line=`expr $line + 2`
cat sh.decls.h.tmp | sed "$line i extern void tcsh_$cmd (Char **, struct command *);" > sh.decls.h
done
cp sh.decls.h sh.decls.h.tmp
line=`grep -n '#define' sh.decls.h.tmp | head -1 | cut -d":" -f 1`
line=`expr $line + 2`
cat sh.decls.h.tmp | sed "$line i /* $PACKAGE extension */" > sh.decls.h
rm -f sh.decls.h.tmp
echo "done!"
#
# Patch sh.init.c
#
# o add entries to the table of builtins => const struct biltins bfunc[]
#
echo " patching sh.init.c ................. "
if [ ! -f sh.init.c.ORG ]; then
cp sh.init.c sh.init.c.ORG
fi
# Add commands to the table of builtins
maxname=`maxn`
cp sh.init.c.ORG sh.init.c
# Patch [while] command to add a trailing comma in the event it serves to add commands as last
cp sh.init.c sh.init.c.tmp
cat sh.init.c.tmp | sed -e 's|{ "while", dowhile, 1, INF }|{ "while", dowhile, 1, INF },|' > sh.init.c
for cmd in $EXTRACMDS; do
cp sh.init.c sh.init.c.tmp
line=`linenumber $cmd sh.init.c.tmp`
if [ "$line" != "" -a "$line" != "0" ]; then
cat sh.init.c.tmp | sed "$line i { \"$cmd\", tcsh_$cmd, 0, INF }, /* $PACKAGE extension */" > sh.init.c
echo $maxname $cmd $line | $AWK '{ printf (" %-*.*s .................... ok! line [%s]\n", $1, $1, $2, $3) }'
else
echo $maxname $cmd | $AWK '{ printf (" %-*.*s .................... failed! - Please add case in linenumber()\n", $1, $1, $2) }'
exit 0
fi
if [ "$line" = "1" ]; then
echo " Error: Wrong specification"
exit 0
fi
done
rm -f sh.init.c.tmp
echo
# Attempt to configure and compile the shell
cd $SHDIR
# Force reconfigure
rm -f config.log
if [ ! -f config.status ]; then
echo -n " Configuring $PACKAGE ... "
./configure CFLAGS='-g -O2' 1> /dev/null 2>&1
echo "done!"
fi
make sh.err.h tc.const.h 1> /dev/null 2>&1
cd ../src/
make
cd $SHDIR
echo -n " Compiling $PACKAGE ..... "
# force recompile and link
rm -f sh.init.o
make 1> ERRORS-MAKE 2>&1
if [ -x pksh ]; then
echo "done!"
rm -f ERRORS-MAKE
echo
echo "pksh, the Packet shell, is available in shell/pksh"
echo "It's a good idea to run it now ;)"
else
echo "failed! Please check for errors in shell/ERRORS-MAKE"
fi
echo
echo "That's all folks!"