]> git.ipfire.org Git - people/ms/suricata.git/blame - scripts/setup-decoder.sh
tools: bash from env
[people/ms/suricata.git] / scripts / setup-decoder.sh
CommitLineData
ac37fd5e 1#!/usr/bin/env bash
85b00dcf
VJ
2#
3# Script to setup a new decoder.
4# Written by Victor Julien <victor@inliniac.net>
5#
6
7set -e
8#set -x
9
10function Usage {
11 echo
12 echo "$(basename $0) -- script to provision a decoder. The script"
13 echo "makes a copy of the decode-template, sets the name and updates"
14 echo " the build system."
15 echo
16 echo "Call from the 'src' directory, with one argument: the decoder name."
17 echo
18 echo "E.g. inside 'src': ../scripts/$(basename $0) ipv7"
19 echo
20}
21
22function Done {
23 echo
24 echo "Decoder $NR has been set up in $FILE_C and $FILE_H and the"
25 echo "build system has been updated."
26 echo
27 echo "The decoder should now compile cleanly. Try running 'make'."
28 echo
29 echo "Next steps are to edit the files to implement the actual"
30 echo "decoding of $NR."
31 echo
32}
33
d1ac8393
JI
34# Make sure we are running from the correct directory.
35set_dir() {
36 if [ -e ./suricata.c ]; then
37 # Do nothing.
38 true
39 elif [ -e ./src/suricata.c ]; then
40 cd src
41 else
42 echo "error: this does not appear to be a suricata source directory."
43 exit 1
44 fi
45}
46
85b00dcf
VJ
47if [ $# -ne "1" ]; then
48 Usage
49 echo "ERROR: call with one argument"
50 exit 1
51fi
52
53INPUT=$1
54# lowercase
55LC=${INPUT,,}
56#echo $LC
57# UPPERCASE
58UC=${LC^^}
59#echo $UC
60# Normal
61NR=${LC^}
62#echo $NR
63
64FILE_C="decode-${LC}.c"
65FILE_H="decode-${LC}.h"
66#echo $FILE_C
67#echo $FILE_H
68
d1ac8393
JI
69set_dir
70
85b00dcf
VJ
71if [ ! -e decode-template.c ] || [ ! -e decode-template.h ]; then
72 Usage
73 echo "ERROR: input files decode-template.c and/or decode-template.h are missing"
74 exit 1
75fi
76if [ -e $FILE_C ] || [ -e $FILE_H ]; then
77 Usage
78 echo "ERROR: file(s) $FILE_C and/or $FILE_H already exist, won't overwrite"
79 exit 1
80fi
81
82cp decode-template.c $FILE_C
83cp decode-template.h $FILE_H
84
85# search and replaces
86sed -i "s/TEMPLATE/${UC}/g" $FILE_C
87sed -i "s/TEMPLATE/${UC}/g" $FILE_H
88sed -i "s/Template/${NR}/g" $FILE_C
89sed -i "s/Template/${NR}/g" $FILE_H
90sed -i "s/template/${LC}/g" $FILE_C
91sed -i "s/template/${LC}/g" $FILE_H
92sed -i "s/decode-template.c decode-template.h \\\/decode-template.c decode-template.h \\\\\n${FILE_C} ${FILE_H} \\\/g" Makefile.am
93
499afaba
VJ
94 ed -s decode.h > /dev/null <<EOF
95/^int DecodeTEMPLATE
96t-
97s/DecodeTEMPLATE/Decode${UC}/g
98w
99EOF
100
101
85b00dcf
VJ
102Done
103exit 0