config 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. txt_append() {
  55. local anchor="$1"
  56. local insert="$2"
  57. local infile="$3"
  58. local tmpfile="$infile.swp"
  59. # sed append cmd: 'a\' + newline + text + newline
  60. cmd="$(printf "a\\%b$insert" "\n")"
  61. sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
  62. # replace original file with the edited one
  63. mv "$tmpfile" "$infile"
  64. }
  65. txt_subst() {
  66. local before="$1"
  67. local after="$2"
  68. local infile="$3"
  69. local tmpfile="$infile.swp"
  70. sed -e "s:$before:$after:" "$infile" >"$tmpfile"
  71. # replace original file with the edited one
  72. mv "$tmpfile" "$infile"
  73. }
  74. txt_delete() {
  75. local text="$1"
  76. local infile="$2"
  77. local tmpfile="$infile.swp"
  78. sed -e "/$text/d" "$infile" >"$tmpfile"
  79. # replace original file with the edited one
  80. mv "$tmpfile" "$infile"
  81. }
  82. set_var() {
  83. local name=$1 new=$2 before=$3
  84. name_re="^($name=|# $name is not set)"
  85. before_re="^($before=|# $before is not set)"
  86. if test -n "$before" && grep -Eq "$before_re" "$FN"; then
  87. txt_append "^$before=" "$new" "$FN"
  88. txt_append "^# $before is not set" "$new" "$FN"
  89. elif grep -Eq "$name_re" "$FN"; then
  90. txt_subst "^$name=.*" "$new" "$FN"
  91. txt_subst "^# $name is not set" "$new" "$FN"
  92. else
  93. echo "$new" >>"$FN"
  94. fi
  95. }
  96. undef_var() {
  97. local name=$1
  98. txt_delete "^$name=" "$FN"
  99. txt_delete "^# $name is not set" "$FN"
  100. }
  101. if [ "$1" = "--file" ]; then
  102. FN="$2"
  103. if [ "$FN" = "" ] ; then
  104. usage
  105. fi
  106. shift 2
  107. else
  108. FN=.config
  109. fi
  110. if [ "$1" = "" ] ; then
  111. usage
  112. fi
  113. MUNGE_CASE=yes
  114. while [ "$1" != "" ] ; do
  115. CMD="$1"
  116. shift
  117. case "$CMD" in
  118. --keep-case|-k)
  119. MUNGE_CASE=no
  120. continue
  121. ;;
  122. --refresh)
  123. ;;
  124. --*-after|-E|-D|-M)
  125. checkarg "$1"
  126. A=$ARG
  127. checkarg "$2"
  128. B=$ARG
  129. shift 2
  130. ;;
  131. -*)
  132. checkarg "$1"
  133. shift
  134. ;;
  135. esac
  136. case "$CMD" in
  137. --enable|-e)
  138. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
  139. ;;
  140. --disable|-d)
  141. set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
  142. ;;
  143. --module|-m)
  144. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
  145. ;;
  146. --set-str)
  147. # sed swallows one level of escaping, so we need double-escaping
  148. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
  149. shift
  150. ;;
  151. --set-val)
  152. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
  153. shift
  154. ;;
  155. --undefine|-u)
  156. undef_var "${CONFIG_}$ARG"
  157. ;;
  158. --state|-s)
  159. if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
  160. echo n
  161. else
  162. V="$(grep "^${CONFIG_}$ARG=" $FN)"
  163. if [ $? != 0 ] ; then
  164. echo undef
  165. else
  166. V="${V/#${CONFIG_}$ARG=/}"
  167. V="${V/#\"/}"
  168. V="${V/%\"/}"
  169. V="${V//\\\"/\"}"
  170. echo "${V}"
  171. fi
  172. fi
  173. ;;
  174. --enable-after|-E)
  175. set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
  176. ;;
  177. --disable-after|-D)
  178. set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
  179. ;;
  180. --module-after|-M)
  181. set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
  182. ;;
  183. # undocumented because it ignores --file (fixme)
  184. --refresh)
  185. yes "" | make oldconfig
  186. ;;
  187. *)
  188. usage
  189. ;;
  190. esac
  191. done