config 4.6 KB

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