check_file_ascii.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #-------------------------------------------------------------------------------------------------------
  2. # Copyright (C) Microsoft. All rights reserved.
  3. # Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
  4. # Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  5. #-------------------------------------------------------------------------------------------------------
  6. ERRFILE=check_ascii.sh.err
  7. ERRFILETEMP=$ERRFILE.0
  8. # display a helpful message for someone reading the log
  9. echo "Check ascii > Checking $1"
  10. if [ ! -e $1 ]; then # the file wasn't present; not necessarily an error
  11. echo "WARNING: file not found: $1"
  12. exit 0 # don't report an error but don't run the rest of this file
  13. fi
  14. # grep for non-ascii - also exclude unprintable control characters at the end of the range
  15. # specifically include x09 (tab) as it is used in pal sources which are not excluded
  16. # from this check
  17. LC_CTYPE=C grep -nP '[^\x09-\x7E]' $1 > $ERRFILETEMP
  18. if [ $? -eq 0 ]; then # grep found matches ($?==0), so we found non-ascii in the file
  19. echo "ERROR: non-ascii characters were introduced in $1" >> $ERRFILE
  20. # Display a hexdump sample of the lines with non-ascii characters in them
  21. # Don't pollute the log with every single matching line, first 10 lines should be enough.
  22. echo "Displaying first 10 lines of text where non-ascii characters were found:" >> $ERRFILE
  23. LC_CTYPE=C grep -nP '[^\x09-\x7E]' $1 | xxd -g 1 > $ERRFILETEMP
  24. head -n 10 $ERRFILETEMP >> $ERRFILE
  25. # To help the user, display how many lines of text actually contained non-ascii characters.
  26. LINECOUNT=`python -c "file=open('$ERRFILETEMP', 'r'); print len(file.readlines())"`
  27. echo "Total lines containing non-ascii: $LINECOUNT" >> $ERRFILE
  28. echo "--------------" >> $ERRFILE # same length as '--- ERRORS ---'
  29. fi
  30. rm -f $ERRFILETEMP