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