config 2.6 KB

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