config 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. --state|-s option Print state of option (n,y,m,undef)
  19. --enable-after|-E beforeopt option
  20. Enable option directly after other option
  21. --disable-after|-D beforeopt option
  22. Disable option directly after other option
  23. --module-after|-M beforeopt option
  24. Turn option into module directly after other option
  25. commands can be repeated multiple times
  26. options:
  27. --file config-file .config file to change (default .config)
  28. --keep-case|-k Keep next symbols' case (dont' upper-case it)
  29. config doesn't check the validity of the .config file. This is done at next
  30. make time.
  31. By default, config will upper-case the given symbol. Use --keep-case to keep
  32. the case of all following symbols unchanged.
  33. config uses 'CONFIG_' as the default symbol prefix. Set the environment
  34. variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" config ...
  35. EOL
  36. exit 1
  37. }
  38. checkarg() {
  39. ARG="$1"
  40. if [ "$ARG" = "" ] ; then
  41. usage
  42. fi
  43. case "$ARG" in
  44. ${CONFIG_}*)
  45. ARG="${ARG/${CONFIG_}/}"
  46. ;;
  47. esac
  48. if [ "$MUNGE_CASE" = "yes" ] ; then
  49. ARG="`echo $ARG | tr a-z A-Z`"
  50. fi
  51. }
  52. set_var() {
  53. local name=$1 new=$2 before=$3
  54. name_re="^($name=|# $name is not set)"
  55. before_re="^($before=|# $before is not set)"
  56. if test -n "$before" && grep -Eq "$before_re" "$FN"; then
  57. sed -ri "/$before_re/a $new" "$FN"
  58. elif grep -Eq "$name_re" "$FN"; then
  59. sed -ri "s:$name_re.*:$new:" "$FN"
  60. else
  61. echo "$new" >>"$FN"
  62. fi
  63. }
  64. if [ "$1" = "--file" ]; then
  65. FN="$2"
  66. if [ "$FN" = "" ] ; then
  67. usage
  68. fi
  69. shift 2
  70. else
  71. FN=.config
  72. fi
  73. if [ "$1" = "" ] ; then
  74. usage
  75. fi
  76. MUNGE_CASE=yes
  77. while [ "$1" != "" ] ; do
  78. CMD="$1"
  79. shift
  80. case "$CMD" in
  81. --keep-case|-k)
  82. MUNGE_CASE=no
  83. shift
  84. continue
  85. ;;
  86. --refresh)
  87. ;;
  88. --*-after)
  89. checkarg "$1"
  90. A=$ARG
  91. checkarg "$2"
  92. B=$ARG
  93. shift 2
  94. ;;
  95. -*)
  96. checkarg "$1"
  97. shift
  98. ;;
  99. esac
  100. case "$CMD" in
  101. --enable|-e)
  102. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
  103. ;;
  104. --disable|-d)
  105. set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
  106. ;;
  107. --module|-m)
  108. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
  109. ;;
  110. --set-str)
  111. # sed swallows one level of escaping, so we need double-escaping
  112. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
  113. shift
  114. ;;
  115. --set-val)
  116. set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
  117. shift
  118. ;;
  119. --state|-s)
  120. if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
  121. echo n
  122. else
  123. V="$(grep "^${CONFIG_}$ARG=" $FN)"
  124. if [ $? != 0 ] ; then
  125. echo undef
  126. else
  127. V="${V/#${CONFIG_}$ARG=/}"
  128. V="${V/#\"/}"
  129. V="${V/%\"/}"
  130. V="${V/\\\"/\"}"
  131. echo "${V}"
  132. fi
  133. fi
  134. ;;
  135. --enable-after|-E)
  136. set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
  137. ;;
  138. --disable-after|-D)
  139. set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
  140. ;;
  141. --module-after|-M)
  142. set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
  143. ;;
  144. # undocumented because it ignores --file (fixme)
  145. --refresh)
  146. yes "" | make oldconfig
  147. ;;
  148. *)
  149. usage
  150. ;;
  151. esac
  152. done