config 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/bash
  2. # Manipulate options in a .config file from the command line
  3. myname=${0##*/}
  4. # If no prefix forced, use the default CONFIG_
  5. CONFIG_="${CONFIG_-CONFIG_}"
  6. usage() {
  7. cat >&2 <<EOL
  8. Manipulate options in a .config file from the command line.
  9. Usage:
  10. $myname options command ...
  11. commands:
  12. --enable|-e option Enable option
  13. --disable|-d option Disable option
  14. --module|-m option Turn option into a module
  15. --set-str option string
  16. Set option to "string"
  17. --set-val option value
  18. Set option to value
  19. --undefine|-u option Undefine option
  20. --state|-s option Print state of option (n,y,m,undef)
  21. --enable-after|-E beforeopt option
  22. Enable option directly after other option
  23. --disable-after|-D beforeopt option
  24. Disable option directly after other option
  25. --module-after|-M beforeopt option
  26. Turn option into module directly after other option
  27. commands can be repeated multiple times
  28. options:
  29. --file config-file .config file to change (default .config)
  30. --keep-case|-k Keep next symbols' case (dont' upper-case it)
  31. $myname doesn't check the validity of the .config file. This is done at next
  32. make time.
  33. By default, $myname will upper-case the given symbol. Use --keep-case to keep
  34. the case of all following symbols unchanged.
  35. $myname uses 'CONFIG_' as the default symbol prefix. Set the environment
  36. variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ...
  37. EOL
  38. exit 1
  39. }
  40. checkarg() {
  41. ARG="$1"
  42. if [ "$ARG" = "" ] ; then
  43. usage
  44. fi
  45. case "$ARG" in
  46. ${CONFIG_}*)
  47. ARG="${ARG/${CONFIG_}/}"
  48. ;;
  49. esac
  50. if [ "$MUNGE_CASE" = "yes" ] ; then
  51. ARG="`echo $ARG | tr a-z A-Z`"
  52. fi
  53. }
  54. set_var() {
  55. local name=$1 new=$2 before=$3
  56. name_re="^($name=|# $name is not set)"
  57. before_re="^($before=|# $before is not set)"
  58. if test -n "$before" && grep -Eq "$before_re" "$FN"; then
  59. sed -ri "/$before_re/a $new" "$FN"
  60. elif grep -Eq "$name_re" "$FN"; then
  61. sed -ri "s:$name_re.*:$new:" "$FN"
  62. else
  63. echo "$new" >>"$FN"
  64. fi
  65. }
  66. undef_var() {
  67. local name=$1
  68. sed -ri "/^($name=|# $name is not set)/d" "$FN"
  69. }
  70. if [ "$1" = "--file" ]; then
  71. FN="$2"
  72. if [ "$FN" = "" ] ; then
  73. usage
  74. fi
  75. shift 2
  76. else
  77. FN=.config
  78. fi
  79. if [ "$1" = "" ] ; then
  80. usage
  81. fi
  82. MUNGE_CASE=yes
  83. while [ "$1" != "" ] ; do
  84. CMD="$1"
  85. shift
  86. case "$CMD" in
  87. --keep-case|-k)
  88. MUNGE_CASE=no
  89. continue
  90. ;;
  91. --refresh)
  92. ;;
  93. --*-after)
  94. checkarg "$1"
  95. A=$ARG
  96. checkarg "$2"
  97. B=$ARG
  98. shift 2
  99. ;;
  100. -*)
  101. checkarg "$1"
  102. shift
  103. ;;
  104. esac
  105. case "$CMD" in
  106. --enable|-e)
  107. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
  108. ;;
  109. --disable|-d)
  110. set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
  111. ;;
  112. --module|-m)
  113. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
  114. ;;
  115. --set-str)
  116. # sed swallows one level of escaping, so we need double-escaping
  117. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
  118. shift
  119. ;;
  120. --set-val)
  121. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
  122. shift
  123. ;;
  124. --undefine|-u)
  125. undef_var "${CONFIG_}$ARG"
  126. ;;
  127. --state|-s)
  128. if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
  129. echo n
  130. else
  131. V="$(grep "^${CONFIG_}$ARG=" $FN)"
  132. if [ $? != 0 ] ; then
  133. echo undef
  134. else
  135. V="${V/#${CONFIG_}$ARG=/}"
  136. V="${V/#\"/}"
  137. V="${V/%\"/}"
  138. V="${V//\\\"/\"}"
  139. echo "${V}"
  140. fi
  141. fi
  142. ;;
  143. --enable-after|-E)
  144. set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
  145. ;;
  146. --disable-after|-D)
  147. set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
  148. ;;
  149. --module-after|-M)
  150. set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
  151. ;;
  152. # undocumented because it ignores --file (fixme)
  153. --refresh)
  154. yes "" | make oldconfig
  155. ;;
  156. *)
  157. usage
  158. ;;
  159. esac
  160. done