config 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/bin/bash
  2. # Manipulate options in a .config file from the command line
  3. usage() {
  4. cat >&2 <<EOL
  5. Manipulate options in a .config file from the command line.
  6. Usage:
  7. config options command ...
  8. commands:
  9. --enable|-e option Enable option
  10. --disable|-d option Disable option
  11. --module|-m option Turn option into a module
  12. --state|-s option Print state of option (n,y,m,undef)
  13. --enable-after|-E beforeopt option
  14. Enable option directly after other option
  15. --disable-after|-D beforeopt option
  16. Disable option directly after other option
  17. --module-after|-M beforeopt option
  18. Turn option into module directly after other option
  19. commands can be repeated multiple times
  20. options:
  21. --file .config file to change (default .config)
  22. config doesn't check the validity of the .config file. This is done at next
  23. make time.
  24. EOL
  25. exit 1
  26. }
  27. checkarg() {
  28. ARG="$1"
  29. if [ "$ARG" = "" ] ; then
  30. usage
  31. fi
  32. case "$ARG" in
  33. CONFIG_*)
  34. ARG="${ARG/CONFIG_/}"
  35. ;;
  36. esac
  37. ARG="`echo $ARG | tr a-z A-Z`"
  38. }
  39. set_var() {
  40. local name=$1 new=$2 before=$3
  41. name_re="^($name=|# $name is not set)"
  42. before_re="^($before=|# $before is not set)"
  43. if test -n "$before" && grep -Eq "$before_re" "$FN"; then
  44. sed -ri "/$before_re/a $new" "$FN"
  45. elif grep -Eq "$name_re" "$FN"; then
  46. sed -ri "s:$name_re.*:$new:" "$FN"
  47. else
  48. echo "$new" >>"$FN"
  49. fi
  50. }
  51. if [ "$1" = "--file" ]; then
  52. FN="$2"
  53. if [ "$FN" = "" ] ; then
  54. usage
  55. fi
  56. shift 2
  57. else
  58. FN=.config
  59. fi
  60. if [ "$1" = "" ] ; then
  61. usage
  62. fi
  63. while [ "$1" != "" ] ; do
  64. CMD="$1"
  65. shift
  66. case "$CMD" in
  67. --refresh)
  68. ;;
  69. --*-after)
  70. checkarg "$1"
  71. A=$ARG
  72. checkarg "$2"
  73. B=$ARG
  74. shift 2
  75. ;;
  76. --*)
  77. checkarg "$1"
  78. shift
  79. ;;
  80. esac
  81. case "$CMD" in
  82. --enable|-e)
  83. set_var "CONFIG_$ARG" "CONFIG_$ARG=y"
  84. ;;
  85. --disable|-d)
  86. set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set"
  87. ;;
  88. --module|-m)
  89. set_var "CONFIG_$ARG" "CONFIG_$ARG=m"
  90. ;;
  91. --state|-s)
  92. if grep -q "# CONFIG_$ARG is not set" $FN ; then
  93. echo n
  94. else
  95. V="$(grep "^CONFIG_$ARG=" $FN)"
  96. if [ $? != 0 ] ; then
  97. echo undef
  98. else
  99. V="${V/CONFIG_$ARG=/}"
  100. V="${V/\"/}"
  101. echo "$V"
  102. fi
  103. fi
  104. ;;
  105. --enable-after|-E)
  106. set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A"
  107. ;;
  108. --disable-after|-D)
  109. set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A"
  110. ;;
  111. --module-after|-M)
  112. set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A"
  113. ;;
  114. # undocumented because it ignores --file (fixme)
  115. --refresh)
  116. yes "" | make oldconfig
  117. ;;
  118. *)
  119. usage
  120. ;;
  121. esac
  122. done