config 4.5 KB

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