config 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. continue
  89. ;;
  90. --refresh)
  91. ;;
  92. --*-after)
  93. checkarg "$1"
  94. A=$ARG
  95. checkarg "$2"
  96. B=$ARG
  97. shift 2
  98. ;;
  99. -*)
  100. checkarg "$1"
  101. shift
  102. ;;
  103. esac
  104. case "$CMD" in
  105. --enable|-e)
  106. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
  107. ;;
  108. --disable|-d)
  109. set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
  110. ;;
  111. --module|-m)
  112. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
  113. ;;
  114. --set-str)
  115. # sed swallows one level of escaping, so we need double-escaping
  116. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
  117. shift
  118. ;;
  119. --set-val)
  120. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
  121. shift
  122. ;;
  123. --undefine|-u)
  124. undef_var "${CONFIG_}$ARG"
  125. ;;
  126. --state|-s)
  127. if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
  128. echo n
  129. else
  130. V="$(grep "^${CONFIG_}$ARG=" $FN)"
  131. if [ $? != 0 ] ; then
  132. echo undef
  133. else
  134. V="${V/#${CONFIG_}$ARG=/}"
  135. V="${V/#\"/}"
  136. V="${V/%\"/}"
  137. V="${V//\\\"/\"}"
  138. echo "${V}"
  139. fi
  140. fi
  141. ;;
  142. --enable-after|-E)
  143. set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
  144. ;;
  145. --disable-after|-D)
  146. set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
  147. ;;
  148. --module-after|-M)
  149. set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
  150. ;;
  151. # undocumented because it ignores --file (fixme)
  152. --refresh)
  153. yes "" | make oldconfig
  154. ;;
  155. *)
  156. usage
  157. ;;
  158. esac
  159. done