config 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/bash
  2. # Manipulate options in a .config file from the command line
  3. # If no prefix forced, use the default CONFIG_
  4. CONFIG_="${CONFIG_-CONFIG_}"
  5. usage() {
  6. cat >&2 <<EOL
  7. Manipulate options in a .config file from the command line.
  8. Usage:
  9. config options command ...
  10. commands:
  11. --enable|-e option Enable option
  12. --disable|-d option Disable option
  13. --module|-m option Turn option into a module
  14. --set-str option string
  15. Set option to "string"
  16. --set-val option value
  17. Set option to value
  18. --undefine|-u option Undefine option
  19. --state|-s option Print state of option (n,y,m,undef)
  20. --enable-after|-E beforeopt option
  21. Enable option directly after other option
  22. --disable-after|-D beforeopt option
  23. Disable option directly after other option
  24. --module-after|-M beforeopt option
  25. Turn option into module directly after other option
  26. commands can be repeated multiple times
  27. options:
  28. --file config-file .config file to change (default .config)
  29. --keep-case|-k Keep next symbols' case (dont' upper-case it)
  30. config doesn't check the validity of the .config file. This is done at next
  31. make time.
  32. By default, config will upper-case the given symbol. Use --keep-case to keep
  33. the case of all following symbols unchanged.
  34. config uses 'CONFIG_' as the default symbol prefix. Set the environment
  35. variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" config ...
  36. EOL
  37. exit 1
  38. }
  39. checkarg() {
  40. ARG="$1"
  41. if [ "$ARG" = "" ] ; then
  42. usage
  43. fi
  44. case "$ARG" in
  45. ${CONFIG_}*)
  46. ARG="${ARG/${CONFIG_}/}"
  47. ;;
  48. esac
  49. if [ "$MUNGE_CASE" = "yes" ] ; then
  50. ARG="`echo $ARG | tr a-z A-Z`"
  51. fi
  52. }
  53. set_var() {
  54. local name=$1 new=$2 before=$3
  55. name_re="^($name=|# $name is not set)"
  56. before_re="^($before=|# $before is not set)"
  57. if test -n "$before" && grep -Eq "$before_re" "$FN"; then
  58. sed -ri "/$before_re/a $new" "$FN"
  59. elif grep -Eq "$name_re" "$FN"; then
  60. sed -ri "s:$name_re.*:$new:" "$FN"
  61. else
  62. echo "$new" >>"$FN"
  63. fi
  64. }
  65. undef_var() {
  66. local name=$1
  67. sed -ri "/^($name=|# $name is not set)/d" "$FN"
  68. }
  69. if [ "$1" = "--file" ]; then
  70. FN="$2"
  71. if [ "$FN" = "" ] ; then
  72. usage
  73. fi
  74. shift 2
  75. else
  76. FN=.config
  77. fi
  78. if [ "$1" = "" ] ; then
  79. usage
  80. fi
  81. MUNGE_CASE=yes
  82. while [ "$1" != "" ] ; do
  83. CMD="$1"
  84. shift
  85. case "$CMD" in
  86. --keep-case|-k)
  87. MUNGE_CASE=no
  88. shift
  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