setup-decoder.sh 2.2 KB

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