config 3.2 KB

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