update NOTICE and LICENSE
[nit.git] / src / showerr.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Copyright 2008 Jean Privat <jean@pryen.org>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # Usage:
19 # showerr.sh [filelocation]...
20 #
21 # Show an colorize lines locations (with or without error messages) passed in arguments.
22 # If no arguments are given, line locations a read for the standard input (one per line)
23
24 red=$(printf "\033[0;31m")
25 bred=$(printf "\033[1;31m")
26 green=$(printf "\033[0;32m")
27 yellow=$(printf "\033[0;33m")
28 def=$(printf "\033[0m")
29
30 colorize()
31 {
32 F='\([^:]*\)'
33 D='\([0-9]*\)'
34 M='\(.*\)'
35 sed -e "s!^$F:$D,$D--$D,$D: $M!X \1 \2 \3 \4 \5 \6!" \
36 -e "s!^$F:$D,$D--$D: $M!X \1 \2 \3 \2 \4 \5!" \
37 -e "s!^$F:$D,$D: $M!X \1 \2 \3 \2 \3 \4!" \
38 -e "s!^$F:$D: $M!X \1 \2 1 \2 eol \3!" \
39 -e "s!^$F:$D\$!X \1 \2 1 \2 eol!" |
40 while read k f l1 c1 l2 c2 m; do
41 if [ "$k" != "X" ]; then
42 echo "$k $f $l1 $c1 $l2 $c2 $m"
43 else
44 # Colorize and rewrite the error message
45 m=$( echo "$m" |
46 sed -e "s!^\(Warning:\)\(.*\)!$green\1$def\2!" \
47 -e "s!^\([^:]*[Ee]rror:\)\(.*\)!$bred\1$def\2!"
48 )
49 echo "$yellow$f$def:$l1,$c1--$l2,$c2: $m"
50
51 # Colorize and show the line
52 if [ $c1 = 1 ]; then
53 start="\(\)"
54 else
55 start="\(.\{$(($c1-1))\}\)"
56 fi
57 if [ $l1 = $l2 ]; then
58 if [ $c2 = eol ]; then
59 len="\(.*\)"
60 red=""
61 else
62 len="\(.\{$(($c2 - $c1 + 1))\}\)"
63 fi
64 sed -n -e "${l1}s!^$start$len\(.*\)! \1$red\2$def\3!p" $f
65 else
66 if [ $c2 = 1 ]; then
67 start2="\(\)"
68 else
69 start2="\(.\{$(($c2-1))\}\)"
70 fi
71 # Show only the first line
72 sed -n -e "${l1}s!^$start\(.*\)! \1$red\2 (...)$def!p" $f
73 #sed -n -e "${l1}s!^$start\(.*\)! \1$red\2!p" $f
74 #if [ $l1 = $(($l2-2)) ]; then
75 # sed -n -e "$(($l1+1))s!^\(.*\)! \1!p" $f
76 #elif [ $l1 != $(($l2-1)) ]; then
77 # sed -n -e "${l1}s!^\([ ]*\).*! \1...!p" $f
78 #fi
79 #sed -n -e "${l2}s!^$start2\(.*\)! \1$def\2!p" $f
80 fi
81 fi
82 done
83 }
84
85 if [ "$#" = 0 ]; then
86 colorize
87 else
88 for x in "$@"; do
89 echo "$x" | colorize
90 done
91 fi
92