Guest User

Untitled

a guest
Jul 26th, 2018
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. #! /usr/bin/perl -w
  2.  
  3. #
  4. # colorgcc
  5. #
  6. # Version: 1.3.2
  7. #
  8. # $Id: colorgcc,v 1.10 1999/04/29 17:15:52 jamoyers Exp $
  9. #
  10. # A wrapper to colorize the output from compilers whose messages
  11. # match the "gcc" format.
  12. #
  13. # Requires the ANSIColor module from CPAN.
  14. #
  15. # Usage:
  16. #
  17. # In a directory that occurs in your PATH _before_ the directory
  18. # where the compiler lives, create a softlink to colorgcc for
  19. # each compiler you want to colorize:
  20. #
  21. # g++ -> colorgcc
  22. # gcc -> colorgcc
  23. # cc -> colorgcc
  24. # etc.
  25. #
  26. # That's it. When "g++" is invoked, colorgcc is run instead.
  27. # colorgcc looks at the program name to figure out which compiler to run.
  28. #
  29. # The default settings can be overridden with ~/.colorgccrc.
  30. # See the comments in the sample .colorgccrc for more information.
  31. #
  32. # Note:
  33. #
  34. # colorgcc will only emit color codes if:
  35. #
  36. # (1) Its STDOUT is a tty and
  37. # (2) the value of $TERM is not listed in the "nocolor" option.
  38. #
  39. # If colorgcc colorizes the output, the compiler's STDERR will be
  40. # combined with STDOUT. Otherwise, colorgcc just passes the output from
  41. # the compiler through without modification.
  42. #
  43. # Author: Jamie Moyers <jmoyers@geeks.com>
  44. # Started: April 20, 1999
  45. # Licence: GNU Public License
  46. #
  47. # Credits:
  48. #
  49. # I got the idea for this from a script called "color_cvs":
  50. # color_cvs .03 Adrian Likins <adrian@gimp.org> <adrian@redhat.com>
  51. #
  52. # <seh4@ix.netcom.com> (Scott Harrington)
  53. # Much improved handling of compiler command line arguments.
  54. # exec compiler when not colorizing to preserve STDOUT, STDERR.
  55. # Fixed my STDIN kludge.
  56. #
  57. # <ecarotti@athena.polito.it> (Elias S. G. Carotti)
  58. # Corrected handling of text like -DPACKAGE=\"Package\"
  59. # Spotted return code bug.
  60. #
  61. # <erwin@erwin.andreasen.org> (Erwin S. Andreasen)
  62. # <schurchi@ucsd.edu> (Steve Churchill)
  63. # Return code bug fixes.
  64. #
  65. # <rik@kde.org> (Rik Hemsley)
  66. # Found STDIN bug.
  67. #
  68. # Changes:
  69. #
  70. # 1.3.2 Better handling of command line arguments to compiler.
  71. #
  72. # If we aren't colorizing output, we just exec the compiler which
  73. # preserves the original STDOUT and STDERR.
  74. #
  75. # Removed STDIN kludge. STDIN being passed correctly now.
  76. #
  77. # 1.3.1 Added kludge to copy STDIN to the compiler's STDIN.
  78. #
  79. # 1.3.0 Now correctly returns (I hope) the return code of the compiler
  80. # process as its own.
  81. #
  82. # 1.2.1 Applied patch to handle text similar to -DPACKAGE=\"Package\".
  83. #
  84. # 1.2.0 Added tty check. If STDOUT is not a tty, don't do color.
  85. #
  86. # 1.1.0 Added the "nocolor" option to turn off the color if the terminal type
  87. # ($TERM) is listed.
  88. #
  89. # 1.0.0 Initial Version
  90.  
  91. use Term::ANSIColor;
  92. use IPC::Open3;
  93.  
  94. sub initDefaults
  95. {
  96. #$compilerPaths{"gcc"} = "/usr/bin/gcc";
  97. #$compilerPaths{"g++"} = "/usr/bin/g++";
  98. #$compilerPaths{"cc"} = "/usr/bin/gcc";
  99. #$compilerPaths{"c++"} = "/usr/bin/g++";
  100. #$compilerPaths{"g77"} = "/usr/bin/g77";
  101. #$compilerPaths{"f77"} = "/usr/bin/g77";
  102. #$compilerPaths{"gcj"} = "/usr/bin/gcj";
  103.  
  104. $nocolor{"dumb"} = "true";
  105.  
  106. $colors{"srcColor"} = color("cyan");
  107. $colors{"introColor"} = color("blue");
  108.  
  109. $colors{"warningFileNameColor"} = color("yellow");
  110. $colors{"warningNumberColor"} = color("yellow");
  111. $colors{"warningMessageColor"} = color("yellow");
  112.  
  113. $colors{"errorFileNameColor"} = color("bold red");
  114. $colors{"errorNumberColor"} = color("bold blue");
  115. $colors{"errorMessageColor"} = color("bold red");
  116.  
  117. @{$translations{"warning"}} = ();
  118. @{$translations{"error"}} = ();
  119. }
  120.  
  121. sub srcscan
  122. {
  123. # Usage: srcscan($text, $normalColor)
  124. # $text -- the text to colorize
  125. # $normalColor -- The escape sequence to use for non-source text.
  126.  
  127. # Looks for text between ` and ', and colors it srcColor.
  128.  
  129. my($line, $normalColor) = @_;
  130.  
  131. my($srcon) = color("reset") . $colors{"srcColor"};
  132. my($srcoff) = color("reset") . $normalColor;
  133.  
  134. $line = $normalColor . $line;
  135.  
  136. # This substitute replaces `foo' with `AfooB' where A is the escape
  137. # sequence that turns on the the desired source color, and B is the
  138. # escape sequence that returns to $normalColor.
  139. $line =~ s/(\`|\')(.*?)\'/\`$srcon$2$srcoff\'/g;
  140.  
  141. print($line, color("reset"));
  142. }
  143.  
  144. #
  145. # Main program
  146. #
  147.  
  148. # Set up default values for colors and compilers.
  149. initDefaults();
  150.  
  151. $compiler = "dmd";
  152. @comp_args = @ARGV;
  153.  
  154. # Check that we don't reference self
  155. die "$compiler is self-referencing"
  156. if ( -l $compiler and (stat $compiler)[1] == (stat $0)[1] );
  157.  
  158. # Get the terminal type.
  159. $terminal = $ENV{"TERM"} || "dumb";
  160.  
  161. # If it's in the list of terminal types not to color, or if
  162. # we're writing to something that's not a tty, don't do color.
  163. if (! $ENV{"CGCC_FORCE_COLOR"} && (! -t STDOUT || $nocolor{$terminal}))
  164. {
  165. exec $compiler, @comp_args
  166. or die("Couldn't exec");
  167. }
  168.  
  169. # Keep the pid of the compiler process so we can get its return
  170. # code and use that as our return code.
  171. $compiler_pid = open3('<&STDIN', \*GCCOUT, \*GCCOUT, $compiler, @comp_args);
  172. binmode(\*GCCOUT,":bytes");
  173. binmode(\*STDOUT,":bytes");
  174.  
  175. # Colorize the output from the compiler.
  176. while(<GCCOUT>)
  177. {
  178. #if (m/^(.*?):([0-9]+):(.*)$/) # filename:lineno:message
  179. if (m/^(.*?)(\([0-9]+\)): (.*)$/) # filename(lineno): message
  180. {
  181. $field1 = $1 || "";
  182. $field2 = $2 || "";
  183. $field3 = $3 || "";
  184.  
  185. # See if this is a warning message.
  186. $is_warning = 0;
  187. for $translation ("warning", @{$translations{"warning"}})
  188. {
  189. if ($field3 =~ m/\s+$translation:.*/)
  190. {
  191. $is_warning = 1;
  192. last;
  193. }
  194. }
  195.  
  196. if ($is_warning)
  197. {
  198. # Warning
  199. print($colors{"warningFileNameColor"}, "$field1:", color("reset"));
  200. print($colors{"warningNumberColor"}, "$field2:", color("reset"));
  201. srcscan($field3, $colors{"warningMessageColor"});
  202. }
  203. else
  204. {
  205. # Error
  206. print($colors{"errorFileNameColor"}, "$field1", color("reset"));
  207. print($colors{"errorNumberColor"}, "$field2: ", color("reset"));
  208. print($field3);
  209. #srcscan($field3, $colors{"errorMessageColor"});
  210. }
  211. print("\n");
  212. }
  213. elsif (m/^(<command-line>):(.*)$/) # special-location:message
  214. {
  215. $field1 = $1 || "";
  216. $field2 = $2 || "";
  217.  
  218. # See if this is a warning message.
  219. $is_warning = 0;
  220. for $translation ("warning", @{$translations{"warning"}})
  221. {
  222. if ($field2 =~ m/\s+$translation:.*/)
  223. {
  224. $is_warning = 1;
  225. last;
  226. }
  227. }
  228.  
  229. if ($is_warning)
  230. {
  231. # Warning
  232. print($colors{"warningFileNameColor"}, "$field1:", color("reset"));
  233. srcscan($field2, $colors{"warningMessageColor"});
  234. }
  235. else
  236. {
  237. # Error
  238. print($colors{"errorFileNameColor"}, "$field1:", color("reset"));
  239. srcscan($field2, $colors{"errorMessageColor"});
  240. }
  241. print("\n");
  242. }
  243. elsif (m/^(.*?):(.+):$/) # filename:message:
  244. {
  245. # No line number, treat as an "introductory" line of text.
  246. srcscan($_, $colors{"introColor"});
  247. }
  248. else # Anything else.
  249. {
  250. # Doesn't seem to be a warning or an error. Print normally.
  251. print(color("reset"), $_);
  252. }
  253. }
  254.  
  255. # Get the return code of the compiler and exit with that.
  256. waitpid($compiler_pid, 0);
  257. exit ($? >> 8);
Add Comment
Please, Sign In to add comment