/ base / scripts / mode-control
mode-control
  1  #!/bin/bash
  2  #
  3  # RTAI immed/piped interrupt dispatching mode control.
  4  #
  5  # Copyright (c) 2005 Michael Neuhauser, Firmix Software GmbH (mike@firmix.at)
  6  #
  7  # Usage:
  8  #	mode-control
  9  #	mode-control check
 10  #		Check current overall dispatching mode.
 11  #
 12  #	mode-control immed|piped
 13  #		Set new overall dispatching mode to specified value (i.e. copy
 14  #		foo.immed to foo.c etc.). Will fail if there is at least one
 15  #		file that is diffent to both is .piped AND .immed variant (i.e.
 16  #		file dispatching mode is DIRTY, if at least one file is DIRTY,
 17  #		the overall dispatching mode is DIRTY too).
 18  #
 19  #	mode-control -f|--force immed|piped
 20  #		Set new overall dispatching mode to specified value, even if
 21  #		overall dispatching mode is DIRTY. Warning! You might loose
 22  #		source code changes if you do this!
 23  #		
 24  # File modes:
 25  #	IMMED	source file foo.x is the same as foo.immed
 26  #	PIPED	source file foo.x is the same as foo.piped
 27  #	DIRTY	source file foo.x is neither the same as foo.piped
 28  #		nor the same as foo.immed
 29  #	
 30  # Overall modes:
 31  #	IMMED	all source files are the same as their .immed variant
 32  #	PIPED	all source files are the same as their .piped variant
 33  #	MIXED	some source files are the same as their .piped variant,
 34  #		some equal their .immed variant but none are DIRTY
 35  #	DIRTY	at least one source file is DIRTY
 36  #
 37  #
 38  # This program is free software; you can redistribute it and/or modify it under
 39  # the terms of the GNU General Public License as published by the Free
 40  # Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139, USA; either
 41  # version 2 of the License, or (at your option) any later version.
 42  #
 43  # This program is distributed in the hope that it will be useful, but WITHOUT
 44  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 45  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 46  # details.
 47  #
 48  # You should have received a copy of the GNU General Public License along with
 49  # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 50  # Place - Suite 330, Boston, MA 02111-1307, USA.
 51  #
 52  
 53  # source code files that are affected by dispatching mode (currently, only .c &
 54  # .h files will work with the code below)
 55  files='../arch/arm/hal/hal.c
 56      ../arch/i386/hal/hal.c 
 57      ../include/asm-arm/rtai_hal.h
 58      ../include/asm-i386/rtai_hal.h 
 59      ../sched/sched.c'
 60  
 61  function get_current_mode() {
 62      echo "Current dispatching mode of files:"
 63      overall_mode=''
 64      for i in $files; do
 65  	piped=${i/%.[ch]/.piped}
 66  	immed=${i/%.[ch]/.immed}
 67  	if cmp -s $i $piped; then
 68  	    # active == piped
 69  	    echo -e "  PIPED\t$i"
 70  	    file_mode='PIPED'
 71  	elif cmp -s $i $immed; then
 72  	    # active == immed
 73  	    echo -e "  IMMED\t$i"
 74  	    file_mode='IMMED'
 75  	else
 76  	    # active != immed && active != piped
 77  	    echo -e "  DIRTY\t$i"
 78  	    file_mode='DIRTY'
 79  	fi
 80  	if [ -z "$overall_mode" ]; then
 81  	    # overall mode was never set
 82  	    overall_mode=$file_mode 
 83  	else
 84  	    # overall mode was set at least once
 85  	    case $overall_mode in
 86  	    IMMED|PIPED)
 87  		# until now all other files had the same mode
 88  		if [ "$file_mode" == 'DIRTY' ]; then
 89  		    overall_mode='DIRTY'
 90  		else
 91  		    # file mode is PIPED|IMMED and different than overall mode
 92  		    # -> either overall mode is not changed or becomes MIXED
 93  		    [ "$overall_mode" != "$file_mode" ] && overall_mode='MIXED'
 94  		fi
 95  		;;
 96  	    MIXED)
 97  		# from MIXED we go only to DIRTY (if file is DIRTY)
 98  		[ "$file_mode" == 'DIRTY' ] && overall_mode='DIRTY'
 99  		;;
100  	    DIRTY)
101  		# once overall mode is dirty it stays there
102  		;;
103  	    esac
104  	fi
105      done
106      echo "=> Current overall dispatching mode: $overall_mode"
107      case $overall_mode in
108      IMMED|PIPED|MIXED)
109  	return 0;;		# allow mode to be changed
110      *)
111  	if [ "$force" ]; then
112  	    echo ''
113  	    echo "** Overall dispatching mode is $overall_mode,"
114  	    echo "** but user has requested forced mode change!"
115  	    return 0		# force mode change
116  	else
117  	    return 1		# prevent mode change (changes might be lost)
118  	fi
119  	;;
120      esac
121  }
122  
123  function set_mode() {
124      mode="$1"
125      echo ''
126      echo "Setting overall dispatching mode to: $(echo $mode | tr a-z A-Z)"
127      for i in $files; do
128  	src=${i/%.[ch]/.$mode}
129  	printf "  %-36s -> %s\n" $src $i
130  	cp $src $i
131      done
132  }
133  
134  # check for force option
135  if [ "x$1" == 'x-f' -o "x$1" == 'x--force' ]; then
136      force=1
137      shift
138  else
139      force=''
140  fi
141  
142  case "$1" in
143  ""|check)
144      get_current_mode
145      exit $?
146      ;;
147  piped|immed)
148      if get_current_mode; then
149  	set_mode "$1"
150      else
151  	echo ''
152  	echo 'At least one file is dirty, i.e. not the same as either its .piped or'
153  	echo '.immed variant! (See output above for details.) The dispatching mode will'
154  	echo 'not be set as this might result in lost code changes. Please fix this'
155  	echo 'manually or force a mode set by specifying the -f|--force option.'
156      fi
157      ;;
158  *)
159      echo ''
160      echo "Unknown command '$1'" >&2
161      exit 1
162      ;;
163  esac