Roland McGrath 11487c5
#!/bin/bash
Roland McGrath 11487c5
Roland McGrath 11487c5
SRC=($(ls config-* 2>/dev/null))
Roland McGrath 11487c5
TGT=($(ls kernel-*/linux-*.noarch/configs/kernel-2.6.*-*.config \
Roland McGrath 11487c5
          kernel-*/linux-*.noarch/configs/kernel-2.6.*-*-debug.config 2>/dev/null))
Roland McGrath 11487c5
TGT1=(${TGT[*]#kernel-*/linux-*.noarch/configs/kernel-2.6.*-})
Roland McGrath 11487c5
Roland McGrath 11487c5
ALL_OPTS="cdfimn"
Roland McGrath 11487c5
if [ $# -lt 2 ] ; then
Roland McGrath 11487c5
   echo -e "Usage:\n   $(basename $0) [-$ALL_OPTS] input target\n"
Roland McGrath 11487c5
   echo -e " Sort input config file into the same order as the target\n"
Roland McGrath 11487c5
   echo -e "      -c: insert comments about non-matching/impossible items"
Roland McGrath 11487c5
   echo -e "      -d: show raw unsorted output with extra debug text"
Roland McGrath 11487c5
   echo -e "      -f: force output to match what is in the target config,"
Roland McGrath 11487c5
   echo -e "          and/or remove impossible config items"
Roland McGrath 11487c5
   echo -e "      -i: find impossible config items"
Roland McGrath 11487c5
   echo -e "      -m: find changed config items"
Roland McGrath 11487c5
   echo -e "      -n: do not sort output\n"
Roland McGrath 11487c5
   echo -e "   input: source config file" ' [' "${SRC[*]#config-}" ']\n'
Roland McGrath 11487c5
   echo -e "  target: output arch name" ' [' "${TGT1[*]%.config}" ']\n'
Roland McGrath 11487c5
   exit 1
Roland McGrath 11487c5
fi
Roland McGrath 11487c5
Roland McGrath 11487c5
while getopts "$ALL_OPTS" OPTION ; do
Roland McGrath 11487c5
case $OPTION in
Roland McGrath 11487c5
c)
Roland McGrath 11487c5
   ADDCOMMENT=1 ;;
Roland McGrath 11487c5
d)
Roland McGrath 11487c5
   DEBUG=1 ;;
Roland McGrath 11487c5
f)
Roland McGrath 11487c5
   FORCE=1 ;;
Roland McGrath 11487c5
i)
Roland McGrath 11487c5
   FIND_IMPOSS=1 ;;
Roland McGrath 11487c5
m)
Roland McGrath 11487c5
   FIND_CHANGED=1 ;;
Roland McGrath 11487c5
n)
Roland McGrath 11487c5
   NOSORT=1 ;;
Roland McGrath 11487c5
\?)
Roland McGrath 11487c5
   exit 2 ;;
Roland McGrath 11487c5
esac
Roland McGrath 11487c5
done
Roland McGrath 11487c5
Roland McGrath 11487c5
if [ "$FORCE" -a "$ADDCOMMENT" ] ; then
Roland McGrath 11487c5
	echo "-f and -c options cannot be used together"
Roland McGrath 11487c5
	exit 2
Roland McGrath 11487c5
fi
Roland McGrath 11487c5
Roland McGrath 11487c5
shift $((OPTIND-1))
Roland McGrath 11487c5
Roland McGrath 11487c5
TEMPFILES="xx00 xx01 xx98 xx99"
Roland McGrath 11487c5
TEMPLEFT=
Roland McGrath 11487c5
for FILE in $TEMPFILES ; do
Roland McGrath 11487c5
   [ -f "$FILE" ] && TEMPLEFT="Y"
Roland McGrath 11487c5
done
Roland McGrath 11487c5
if [ "$TEMPLEFT" ] ; then
Roland McGrath 11487c5
   echo "WARNING! Output files named xx?? already exist." >&2
Roland McGrath 11487c5
   read -p "Press <Enter> to erase files, or Ctrl-C to exit..."
Roland McGrath 11487c5
   echo >&2
Roland McGrath 11487c5
fi
Roland McGrath 11487c5
rm -f $TEMPFILES
Roland McGrath 11487c5
Roland McGrath 11487c5
SRCFILE=config-$1
Roland McGrath 11487c5
[ ! -f $SRCFILE ] && echo "Input file" $SRCFILE "missing" && exit 2
Roland McGrath 11487c5
Roland McGrath 11487c5
TGTFILE=kernel-*/linux-*.noarch/configs/kernel-2.6.*-$2.config
Roland McGrath 11487c5
[ ! -f $TGTFILE ] && echo "No target file matching" $TGTFILE "exists" && exit 2
Roland McGrath 11487c5
Roland McGrath 11487c5
[ "$FIND_IMPOSS" ] && \
Roland McGrath 11487c5
   find kernel-*/*.noarch -name Kconfig\* -type f \
Roland McGrath 11487c5
   | xargs egrep -s -h '^[[:space:]]*(menu)?config[[:space:]]+' \
Roland McGrath 11487c5
   | sed -r 's/^[[:space:]]*(menu)?config[[:space:]]+/CONFIG_/' \
Roland McGrath 11487c5
   | sort | uniq >xx98
Roland McGrath 11487c5
Roland McGrath 11487c5
extract_optname() {
Roland McGrath 11487c5
         # extract the option name from $TEXT, setting $OPTNAME
Roland McGrath 11487c5
         OPTNAME=
Roland McGrath 11487c5
         if [ "${TEXT:0:7}" = "CONFIG_" ] ; then
Roland McGrath 11487c5
            OPTNAME=${TEXT%%=*}
Roland McGrath 11487c5
         elif [ "${TEXT:0:9}" = "# CONFIG_" ] ; then
Roland McGrath 11487c5
            OPTNAME=${TEXT%" is not set"}
Roland McGrath 11487c5
            OPTNAME=${OPTNAME#\# }
Roland McGrath 11487c5
         fi
Roland McGrath 11487c5
}
Roland McGrath 11487c5
Roland McGrath 11487c5
print_saved_comments() {
Roland McGrath 11487c5
      if [ $IX -gt 0 ] ; then
Roland McGrath 11487c5
         [ "$DEBUG" ] && echo "            ->" $IX "comments were saved"
Roland McGrath 11487c5
         (( IX-- ))
Roland McGrath 11487c5
         for IX in $(seq 0 $IX) ; do
Roland McGrath 11487c5
            echo "$LINE":"${SAVECOMMENT[$IX]}"
Roland McGrath 11487c5
         done
Roland McGrath 11487c5
         unset SAVECOMMENT
Roland McGrath 11487c5
         IX=0
Roland McGrath 11487c5
      fi
Roland McGrath 11487c5
}
Roland McGrath 11487c5
Roland McGrath 11487c5
assign_line_number() {
Roland McGrath 11487c5
      # use input line numbers if not sorting
Roland McGrath 11487c5
      [ "$NOSORT" ] && LINE=$IN
Roland McGrath 11487c5
      # make sure it has a line number
Roland McGrath 11487c5
      [ -z "$LINE" ] && LINE=999999
Roland McGrath 11487c5
}
Roland McGrath 11487c5
Roland McGrath 11487c5
IX=0
Roland McGrath 11487c5
IN=0
Roland McGrath 11487c5
declare -a SAVECOMMENT
Roland McGrath 11487c5
Roland McGrath 11487c5
cat ${SRCFILE} | {
Roland McGrath 11487c5
while read TEXT ; do
Roland McGrath 11487c5
Roland McGrath 11487c5
   LINE=
Roland McGrath 11487c5
   COMMENT=
Roland McGrath 11487c5
Roland McGrath 11487c5
   # replace empty lines
Roland McGrath 11487c5
   [ -z "$TEXT" ] && TEXT='//'
Roland McGrath 11487c5
Roland McGrath 11487c5
   if [ "${TEXT:0:7}" = "CONFIG_" -o "${TEXT:0:9}" = "# CONFIG_" ] ; then
Roland McGrath 11487c5
Roland McGrath 11487c5
      LINE=$(grep -n "^$TEXT" $TGTFILE | head -1 | cut -f 1 -d ':')
Roland McGrath 11487c5
      if [ -z "$LINE" ] ; then
Roland McGrath 11487c5
         [ "$DEBUG" ] && echo "nofind      ->" "$TEXT"
Roland McGrath 11487c5
Roland McGrath 11487c5
         extract_optname
Roland McGrath 11487c5
         if [ "$OPTNAME" ] ; then
Roland McGrath 11487c5
Roland McGrath 11487c5
            if [ "$FIND_CHANGED" ] ; then
Roland McGrath 11487c5
               for FINDTEXT in "^${OPTNAME}=" "^# ${OPTNAME} is not set" ; do
Roland McGrath 11487c5
                  if [ -z "$LINE" ] ; then
Roland McGrath 11487c5
                     [ "$DEBUG" ] && echo "looking for ->" "$FINDTEXT"
Roland McGrath 11487c5
                     LINE=$(grep -n "$FINDTEXT" $TGTFILE | head -1 | cut -f 1 -d ':')
Roland McGrath 11487c5
                     if [ "$LINE" ] ; then
Roland McGrath 11487c5
                        CHANGED=$(grep "$FINDTEXT" $TGTFILE | head -1)
Roland McGrath 11487c5
                        if [ "$FORCE" ] ; then
Roland McGrath 11487c5
                           TEXT=$CHANGED
Roland McGrath 11487c5
                           [ "$DEBUG" ] && echo 'forced      ->' "$TEXT"
Roland McGrath 11487c5
                        else
Roland McGrath 11487c5
                           if [ "$ADDCOMMENT" ] ; then
Roland McGrath 11487c5
                              if [ ${CHANGED:0:1} = '#' ] ; then
Roland McGrath 11487c5
                                 NEWOPT="not set"
Roland McGrath 11487c5
                              else
Roland McGrath 11487c5
                                 NEWOPT=${CHANGED#$OPTNAME}
Roland McGrath 11487c5
                              fi
Roland McGrath 11487c5
                              COMMENT="# -- Next option changed to \"${NEWOPT}\" at target line $LINE --"
Roland McGrath 11487c5
                           fi
Roland McGrath 11487c5
                        fi
Roland McGrath 11487c5
                     fi
Roland McGrath 11487c5
                  fi
Roland McGrath 11487c5
               done
Roland McGrath 11487c5
            fi
Roland McGrath 11487c5
Roland McGrath 11487c5
            if [ "$FIND_IMPOSS" -a -z "$LINE" -a -z "$COMMENT" ] ; then
Roland McGrath 11487c5
               POSSIBLE=$(grep -n "^$OPTNAME" xx98)
Roland McGrath 11487c5
               if [ -z "$POSSIBLE" ] ; then
Roland McGrath 11487c5
                  if [ "$ADDCOMMENT" ] ; then
Roland McGrath 11487c5
                     COMMENT="# -- Next option is impossible --"
Roland McGrath 11487c5
                  elif [ "$FORCE" ] ; then
Roland McGrath 11487c5
                     [ "$DEBUG" ] && echo 'impossible  ->' "$TEXT"
Roland McGrath 11487c5
                     TEXT=""
Roland McGrath 11487c5
                  fi
Roland McGrath 11487c5
               fi
Roland McGrath 11487c5
            fi
Roland McGrath 11487c5
Roland McGrath 11487c5
         fi
Roland McGrath 11487c5
Roland McGrath 11487c5
      fi
Roland McGrath 11487c5
Roland McGrath 11487c5
   else
Roland McGrath 11487c5
      # not a config variable
Roland McGrath 11487c5
      COMMENT="$TEXT"
Roland McGrath 11487c5
      TEXT=
Roland McGrath 11487c5
   fi
Roland McGrath 11487c5
Roland McGrath 11487c5
   [ "$DEBUG" -a "$COMMENT" ] && echo "comment     ->" "$LINE" "$COMMENT"
Roland McGrath 11487c5
   [ "$DEBUG" -a "$TEXT" ]    && echo "text        ->" "$LINE" "$TEXT"
Roland McGrath 11487c5
Roland McGrath 11487c5
   if [ "$TEXT" ] ; then
Roland McGrath 11487c5
Roland McGrath 11487c5
      assign_line_number
Roland McGrath 11487c5
Roland McGrath 11487c5
      # print the saved comments first
Roland McGrath 11487c5
      print_saved_comments
Roland McGrath 11487c5
      # now print the latest comment and text
Roland McGrath 11487c5
      [ "$COMMENT" ] && echo "$LINE":"$COMMENT"
Roland McGrath 11487c5
      echo "$LINE":"$TEXT"
Roland McGrath 11487c5
Roland McGrath 11487c5
   elif [ "$COMMENT" ] ; then
Roland McGrath 11487c5
Roland McGrath 11487c5
      # no output yet, save the comment
Roland McGrath 11487c5
      SAVECOMMENT[$IX]="$COMMENT"
Roland McGrath 11487c5
      let IX++
Roland McGrath 11487c5
      [ "$DEBUG" ] && echo 'savecomment (#'${IX}')'
Roland McGrath 11487c5
Roland McGrath 11487c5
   fi
Roland McGrath 11487c5
Roland McGrath 11487c5
   let IN++
Roland McGrath 11487c5
Roland McGrath 11487c5
done
Roland McGrath 11487c5
# flush the buffers
Roland McGrath 11487c5
assign_line_number
Roland McGrath 11487c5
print_saved_comments
Roland McGrath 11487c5
[ "$DEBUG" ] && echo "$IN lines read from input"
Roland McGrath 11487c5
} >xx99
Roland McGrath 11487c5
Roland McGrath 11487c5
if [ "$DEBUG" ] ; then
Roland McGrath 11487c5
   # just show the raw output with debug info, then exit
Roland McGrath 11487c5
   cat xx99
Roland McGrath 11487c5
else
Roland McGrath 11487c5
Roland McGrath 11487c5
   # split output into two files, for matched and unmatched items
Roland McGrath 11487c5
   cat xx99 | sort -s -t ":" -k 1g | csplit -k -s - /^999999/ 2>/dev/null
Roland McGrath 11487c5
Roland McGrath 11487c5
   cat xx00 | cut -f 2- -d ':' | sed 's/^\/\/$//'
Roland McGrath 11487c5
   if [ -s xx01 ] ; then
Roland McGrath 11487c5
      echo
Roland McGrath 11487c5
      echo '# ------------  UNMATCHED OPTIONS  ------------'
Roland McGrath 11487c5
      echo
Roland McGrath 11487c5
      cat xx01 | cut -f 2- -d ':' | sed 's/^\/\/$//'
Roland McGrath 11487c5
   fi
Roland McGrath 11487c5
Roland McGrath 11487c5
fi
Roland McGrath 11487c5
Roland McGrath 11487c5
rm -f $TEMPFILES