config 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. --state|-s option Print state of option (n,y,m,undef)
  13. --enable-after|-E beforeopt option
  14. Enable option directly after other option
  15. --disable-after|-D beforeopt option
  16. Disable option directly after other option
  17. --module-after|-M beforeopt option
  18. Turn option into module directly after other option
  19. commands can be repeated multiple times
  20. options:
  21. --file .config file to change (default .config)
  22. config doesn't check the validity of the .config file. This is done at next
  23. make time.
  24. The options need to be already in the file before they can be changed,
  25. but sometimes you can cheat with the --*-after options.
  26. EOL
  27. exit 1
  28. }
  29. checkarg() {
  30. ARG="$1"
  31. if [ "$ARG" = "" ] ; then
  32. usage
  33. fi
  34. case "$ARG" in
  35. CONFIG_*)
  36. ARG="${ARG/CONFIG_/}"
  37. ;;
  38. esac
  39. ARG="`echo $ARG | tr a-z A-Z`"
  40. }
  41. replace() {
  42. sed -i -e "$@" $FN
  43. }
  44. if [ "$1" = "--file" ]; then
  45. FN="$2"
  46. if [ "$FN" = "" ] ; then
  47. usage
  48. fi
  49. shift
  50. shift
  51. else
  52. FN=.config
  53. fi
  54. if [ "$1" = "" ] ; then
  55. usage
  56. fi
  57. while [ "$1" != "" ] ; do
  58. CMD="$1"
  59. shift
  60. case "$CMD" in
  61. --enable|-e)
  62. checkarg "$1"
  63. replace "s/# CONFIG_$ARG is not set/CONFIG_$ARG=y/"
  64. shift
  65. ;;
  66. --disable|-d)
  67. checkarg "$1"
  68. replace "s/CONFIG_$ARG=[my]/# CONFIG_$ARG is not set/"
  69. shift
  70. ;;
  71. --module|-m)
  72. checkarg "$1"
  73. replace "s/CONFIG_$ARG=y/CONFIG_$ARG=m/" \
  74. -e "s/# CONFIG_$ARG is not set/CONFIG_$ARG=m/"
  75. shift
  76. ;;
  77. --state|-s)
  78. checkarg "$1"
  79. if grep -q "# CONFIG_$ARG is not set" $FN ; then
  80. echo n
  81. else
  82. V="$(grep "^CONFIG_$ARG=" $FN)"
  83. if [ $? != 0 ] ; then
  84. echo undef
  85. else
  86. V="${V/CONFIG_$ARG=/}"
  87. V="${V/\"/}"
  88. echo "$V"
  89. fi
  90. fi
  91. shift
  92. ;;
  93. --enable-after|-E)
  94. checkarg "$1"
  95. A=$ARG
  96. checkarg "$2"
  97. B=$ARG
  98. replace "/CONFIG_$A=[my]/aCONFIG_$B=y" \
  99. -e "/# CONFIG_$ARG is not set/a/CONFIG_$ARG=y" \
  100. -e "s/# CONFIG_$ARG is not set/CONFIG_$ARG=y/"
  101. shift
  102. shift
  103. ;;
  104. --disable-after|-D)
  105. checkarg "$1"
  106. A=$ARG
  107. checkarg "$2"
  108. B=$ARG
  109. replace "/CONFIG_$A=[my]/a# CONFIG_$B is not set" \
  110. -e "/# CONFIG_$ARG is not set/a/# CONFIG_$ARG is not set" \
  111. -e "s/CONFIG_$ARG=[my]/# CONFIG_$ARG is not set/"
  112. shift
  113. shift
  114. ;;
  115. --module-after|-M)
  116. checkarg "$1"
  117. A=$ARG
  118. checkarg "$2"
  119. B=$ARG
  120. replace "/CONFIG_$A=[my]/aCONFIG_$B=m" \
  121. -e "/# CONFIG_$ARG is not set/a/CONFIG_$ARG=m" \
  122. -e "s/CONFIG_$ARG=y/CONFIG_$ARG=m/" \
  123. -e "s/# CONFIG_$ARG is not set/CONFIG_$ARG=m/"
  124. shift
  125. shift
  126. ;;
  127. # undocumented because it ignores --file (fixme)
  128. --refresh)
  129. yes "" | make oldconfig
  130. ;;
  131. *)
  132. usage
  133. ;;
  134. esac
  135. done