feature_to_c.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh
  2. # Convert text files to compilable C arrays.
  3. #
  4. # Copyright (C) 2007 Free Software Foundation, Inc.
  5. #
  6. # This file is part of GDB.
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. # Boston, MA 02110-1301, USA.
  22. output=$1
  23. shift
  24. if test -z "$output" || test -z "$1"; then
  25. echo "Usage: $0 OUTPUTFILE INPUTFILE..."
  26. exit 1
  27. fi
  28. if test -e "$output"; then
  29. echo "Output file \"$output\" already exists; refusing to overwrite."
  30. exit 1
  31. fi
  32. for input; do
  33. arrayname=xml_feature_`echo $input | sed 's,.*/,,; s/[-.]/_/g'`
  34. ${AWK:-awk} 'BEGIN { n = 0
  35. print "static const char '$arrayname'[] = {"
  36. for (i = 0; i < 255; i++)
  37. _ord_[sprintf("%c", i)] = i
  38. } {
  39. split($0, line, "");
  40. printf " "
  41. for (i = 1; i <= length($0); i++) {
  42. c = line[i]
  43. if (c == "'\''") {
  44. printf "'\''\\'\'''\'', "
  45. } else if (c == "\\") {
  46. printf "'\''\\\\'\'', "
  47. } else if (_ord_[c] >= 32 && _ord_[c] < 127) {
  48. printf "'\''%s'\'', ", c
  49. } else {
  50. printf "'\''\\%03o'\'', ", _ord_[c]
  51. }
  52. if (i % 10 == 0)
  53. printf "\n "
  54. }
  55. printf "'\''\\n'\'', \n"
  56. } END {
  57. print " 0 };"
  58. }' < $input >> $output
  59. done
  60. echo >> $output
  61. echo "extern const char *const xml_builtin[][2];" >> $output
  62. echo "const char *const xml_builtin[][2] = {" >> $output
  63. for input; do
  64. basename=`echo $input | sed 's,.*/,,'`
  65. arrayname=xml_feature_`echo $input | sed 's,.*/,,; s/[-.]/_/g'`
  66. echo " { \"$basename\", $arrayname }," >> $output
  67. done
  68. echo " { 0, 0 }" >> $output
  69. echo "};" >> $output