config 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. while [ "$1" != "" ] ; do
  55. CMD="$1"
  56. shift
  57. case "$CMD" in
  58. --enable|-e)
  59. checkarg "$1"
  60. replace "s/# CONFIG_$ARG is not set/CONFIG_$ARG=y/"
  61. shift
  62. ;;
  63. --disable|-d)
  64. checkarg "$1"
  65. replace "s/CONFIG_$ARG=[my]/# CONFIG_$ARG is not set/"
  66. shift
  67. ;;
  68. --module|-m)
  69. checkarg "$1"
  70. replace "s/CONFIG_$ARG=y/CONFIG_$ARG=m/" \
  71. -e "s/# CONFIG_$ARG is not set/CONFIG_$ARG=m/"
  72. shift
  73. ;;
  74. --state|-s)
  75. checkarg "$1"
  76. if grep -q "# CONFIG_$ARG is not set" $FN ; then
  77. echo n
  78. else
  79. V="$(grep "^CONFIG_$ARG=" $FN)"
  80. if [ $? != 0 ] ; then
  81. echo undef
  82. else
  83. V="${V/CONFIG_$ARG=/}"
  84. V="${V/\"/}"
  85. echo "$V"
  86. fi
  87. fi
  88. shift
  89. ;;
  90. --enable-after|-E)
  91. checkarg "$1"
  92. A=$ARG
  93. checkarg "$2"
  94. B=$ARG
  95. replace "/CONFIG_$A=[my]/aCONFIG_$B=y" \
  96. -e "/# CONFIG_$ARG is not set/a/CONFIG_$ARG=y" \
  97. -e "s/# CONFIG_$ARG is not set/CONFIG_$ARG=y/"
  98. shift
  99. shift
  100. ;;
  101. --disable-after|-D)
  102. checkarg "$1"
  103. A=$ARG
  104. checkarg "$2"
  105. B=$ARG
  106. replace "/CONFIG_$A=[my]/a# CONFIG_$B is not set" \
  107. -e "/# CONFIG_$ARG is not set/a/# CONFIG_$ARG is not set" \
  108. -e "s/CONFIG_$ARG=[my]/# CONFIG_$ARG is not set/"
  109. shift
  110. shift
  111. ;;
  112. --module-after|-M)
  113. checkarg "$1"
  114. A=$ARG
  115. checkarg "$2"
  116. B=$ARG
  117. replace "/CONFIG_$A=[my]/aCONFIG_$B=m" \
  118. -e "/# CONFIG_$ARG is not set/a/CONFIG_$ARG=m" \
  119. -e "s/CONFIG_$ARG=y/CONFIG_$ARG=m/" \
  120. -e "s/# CONFIG_$ARG is not set/CONFIG_$ARG=m/"
  121. shift
  122. shift
  123. ;;
  124. # undocumented because it ignores --file (fixme)
  125. --refresh)
  126. yes "" | make oldconfig
  127. ;;
  128. *)
  129. usage
  130. ;;
  131. esac
  132. done