config 2.8 KB

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