2011-01-26 02:59:19 +01:00
#!/usr/bin/perl
# use warnings;
use strict ;
2012-09-01 07:20:01 +02:00
use feature "switch" ;
2011-01-26 02:59:19 +01:00
use IPC::Open2 ;
2011-02-04 03:50:52 +01:00
use Text::Balanced qw( extract_bracketed extract_delimited ) ;
2011-01-26 02:59:19 +01:00
use IO::Socket ;
use LWP::UserAgent ;
2014-04-03 09:47:19 +02:00
use Time::HiRes qw/gettimeofday/ ;
2011-01-26 02:59:19 +01:00
2011-02-08 02:24:12 +01:00
my $ debug = 0 ;
2014-05-31 03:20:31 +02:00
$ SIG { INT } = sub { cleanup ( ) ; exit 1 ; } ;
my $ compiler_client ;
sub cleanup {
close $ compiler_client if defined $ compiler_client ;
}
2011-02-01 01:41:51 +01:00
my $ USE_LOCAL = defined $ ENV { 'CC_LOCAL' } ;
2012-01-28 08:39:13 +01:00
my $ MAX_UNDO_HISTORY = 1000000 ;
2011-01-26 02:59:19 +01:00
my $ output = "" ;
my $ nooutput = 'No output.' ;
2012-11-21 20:01:10 +01:00
my $ warn_unterminated_define = 0 ;
2013-09-13 13:03:50 +02:00
my $ save_last_code = 0 ;
2013-09-13 13:32:15 +02:00
my $ unshift_last_code = 0 ;
my $ only_show = 0 ;
2012-11-21 20:01:10 +01:00
2011-01-26 02:59:19 +01:00
my % languages = (
2014-05-02 02:43:42 +02:00
'C11' = > "gcc -std=c11 -pedantic -Wall -Wextra -Wno-unused -Wfloat-equal -Wshadow -Wfatal-errors" ,
'C99' = > "gcc -std=c99 -pedantic -Wall -Wextra -Wno-unused -Wfloat-equal -Wshadow -Wfatal-errors" ,
'C89' = > "gcc -std=c89 -pedantic -Wall -Wextra -Wno-unused -Wfloat-equal -Wshadow -Wfatal-errors" ,
2011-01-26 02:59:19 +01:00
) ;
my % preludes = (
2014-03-06 22:52:44 +01:00
'C99' = > "#define _XOPEN_SOURCE 9001\n#define __USE_XOPEN\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\n#include <complex.h>\n#include <math.h>\n#include <tgmath.h>\n#include <limits.h>\n#include <sys/types.h>\n#include <stdint.h>\n#include <stdbool.h>\n#include <stddef.h>\n#include <stdarg.h>\n#include <ctype.h>\n#include <inttypes.h>\n#include <float.h>\n#include <errno.h>\n#include <time.h>\n#include <assert.h>\n#include <locale.h>\n#include <wchar.h>\n#include <fenv.h>\n#inclue <iso646.h>\n#include <setjmp.h>\n#include <signal.h>\n#include <prelude.h>\n\n" ,
2014-05-31 03:20:31 +02:00
'C11' = > "#define _XOPEN_SOURCE 9001\n#define __USE_XOPEN\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\n#include <math.h>\n#include <limits.h>\n#include <sys/types.h>\n#include <stdint.h>\n#include <stdbool.h>\n#include <stddef.h>\n#include <stdarg.h>\n#include <stdnoreturn.h>\n#include <stdalign.h>\n#include <ctype.h>\n#include <inttypes.h>\n#include <float.h>\n#include <errno.h>\n#include <time.h>\n#include <assert.h>\n#include <complex.h>\n#include <setjmp.h>\n#include <wchar.h>\n#include <wctype.h>\n#include <tgmath.h>\n#include <fenv.h>\n#include <locale.h>\n#include <iso646.h>\n#include <signal.h>\n#include <prelude.h>\n\n" ,
2014-03-06 22:52:44 +01:00
'C89' = > "#define _XOPEN_SOURCE 9001\n#define __USE_XOPEN\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\n#include <math.h>\n#include <limits.h>\n#include <sys/types.h>\n#include <stdint.h>\n#include <errno.h>\n#include <ctype.h>\n#include <assert.h>\n#include <locale.h>\n#include <setjmp.h>\n#include <signal.h>\n#include <prelude.h>\n\n" ,
2011-02-08 02:27:45 +01:00
) ;
2011-01-26 02:59:19 +01:00
sub pretty {
my $ code = join '' , @ _ ;
my $ result ;
2012-02-15 23:00:58 +01:00
open my $ fh , ">prog.c" or die "Couldn't write prog.c: $!" ;
print $ fh $ code ;
close $ fh ;
2012-12-29 17:19:51 +01:00
system ( "astyle" , "-UHfenq" , "prog.c" ) ;
2012-02-15 23:00:58 +01:00
open $ fh , "<prog.c" or die "Couldn't read prog.c: $!" ;
$ result = join '' , <$fh> ;
close $ fh ;
2011-01-26 02:59:19 +01:00
return $ result ;
}
sub paste_codepad {
my $ text = join ( ' ' , @ _ ) ;
$ text =~ s/(.{120})\s/$1\n/g ;
my $ ua = LWP::UserAgent - > new ( ) ;
$ ua - > agent ( "Mozilla/5.0" ) ;
push @ { $ ua - > requests_redirectable } , 'POST' ;
my % post = ( 'lang' = > 'C' , 'code' = > $ text , 'private' = > 'True' , 'submit' = > 'Submit' ) ;
my $ response = $ ua - > post ( "http://codepad.org" , \ % post ) ;
2012-02-15 23:00:58 +01:00
if ( not $ response - > is_success ) {
return $ response - > status_line ;
}
2011-01-26 02:59:19 +01:00
2012-02-15 23:00:58 +01:00
return $ response - > request - > uri ;
2011-01-26 02:59:19 +01:00
}
2012-02-02 05:14:38 +01:00
2012-02-15 23:00:58 +01:00
sub paste_sprunge {
2012-02-02 05:14:38 +01:00
my $ text = join ( ' ' , @ _ ) ;
$ text =~ s/(.{120})\s/$1\n/g ;
my $ ua = LWP::UserAgent - > new ( ) ;
$ ua - > agent ( "Mozilla/5.0" ) ;
$ ua - > requests_redirectable ( [ ] ) ;
my % post = ( 'sprunge' = > $ text , 'submit' = > 'Submit' ) ;
my $ response = $ ua - > post ( "http://sprunge.us" , \ % post ) ;
if ( not $ response - > is_success ) {
return $ response - > status_line ;
}
my $ result = $ response - > content ;
$ result =~ s/^\s+// ;
$ result =~ s/\s+$/?c/ ;
return $ result ;
}
2011-01-26 02:59:19 +01:00
sub compile {
2011-02-01 01:41:51 +01:00
my ( $ lang , $ code , $ args , $ input , $ local ) = @ _ ;
my ( $ compiler , $ compiler_output , $ pid ) ;
2011-02-08 02:27:45 +01:00
2011-02-01 01:41:51 +01:00
if ( defined $ local and $ local != 0 ) {
print "Using local compiler instead of virtual machine\n" ;
$ pid = open2 ( $ compiler_output , $ compiler , './compiler_vm_server.pl' ) || die "repl failed: $@\n" ;
print "Started compiler, pid: $pid\n" ;
} else {
2012-01-28 08:39:13 +01:00
$ compiler = IO::Socket::INET - > new ( PeerAddr = > '127.0.0.1' , PeerPort = > '3333' , Proto = > 'tcp' , Type = > SOCK_STREAM ) ;
2011-02-01 01:41:51 +01:00
die "Could not create socket: $!" unless $ compiler ;
$ compiler_output = $ compiler ;
2014-05-31 03:20:31 +02:00
$ compiler_client = $ compiler ;
2011-02-01 01:41:51 +01:00
}
2011-01-26 02:59:19 +01:00
2012-10-24 14:26:18 +02:00
my $ date = time ;
print $ compiler "compile:$lang:$args:$input:$date\n" ;
2011-02-01 01:41:51 +01:00
print $ compiler "$code\n" ;
print $ compiler "compile:end\n" ;
2011-01-26 02:59:19 +01:00
my $ result = "" ;
my $ got_result = 0 ;
2011-02-01 01:41:51 +01:00
while ( my $ line = <$compiler_output> ) {
2011-01-26 02:59:19 +01:00
$ line =~ s/[\r\n]+$// ;
2012-07-22 21:22:30 +02:00
last if $ line =~ /^result:end$/ ;
2011-01-26 02:59:19 +01:00
if ( $ line =~ /^result:/ ) {
$ line =~ s/^result:// ;
$ result . = $ line ;
$ got_result = 1 ;
next ;
}
if ( $ got_result ) {
$ result . = $ line . "\n" ;
}
}
2011-02-01 01:41:51 +01:00
close $ compiler ;
close $ output if defined $ output ;
waitpid ( $ pid , 0 ) if defined $ pid ;
2011-01-26 02:59:19 +01:00
return $ result ;
}
2014-03-04 22:40:51 +01:00
if ( $# ARGV < 2 ) {
2014-05-31 03:20:31 +02:00
print "Usage: cc [-compiler options] [-options] <code> [-stdin=input]\n" ;
2014-03-04 22:40:51 +01:00
# usage for shell: cc <nick> <channel> [-compiler -options] <code> [-stdin=input]
2011-01-26 02:59:19 +01:00
exit 0 ;
}
my $ nick = shift @ ARGV ;
2014-03-04 22:40:51 +01:00
my $ channel = lc shift @ ARGV ;
2011-01-26 02:59:19 +01:00
my $ code = join ' ' , @ ARGV ;
my @ last_code ;
2011-02-08 02:24:12 +01:00
print " code: [$code]\n" if $ debug ;
2014-03-05 20:58:46 +01:00
my $ subcode = $ code ;
while ( $ subcode =~ s/^\s*(-[^ ]+)\s*// ) { }
my $ copy_code ;
if ( $ subcode =~ s/^\s*copy\s+(\S+)\s*// ) {
my $ copy = $ 1 ;
if ( open FILE , "< history/$copy.hist" ) {
$ copy_code = <FILE> ;
close FILE ;
goto COPY_ERROR if not $ copy_code ; ;
chomp $ copy_code ;
} else {
goto COPY_ERROR ;
}
goto COPY_SUCCESS ;
COPY_ERROR:
print "$nick: No history for $copy.\n" ;
exit 0 ;
COPY_SUCCESS:
$ code = $ copy_code ;
$ only_show = 1 ;
$ save_last_code = 1 ;
}
if ( $ subcode =~ m/^\s*(?:and\s+)?(?:diff|show)\s+(\S+)\s*$/ ) {
$ channel = $ 1 ;
}
2014-03-04 22:40:51 +01:00
if ( open FILE , "< history/$channel.hist" ) {
2011-01-26 02:59:19 +01:00
while ( my $ line = <FILE> ) {
chomp $ line ;
push @ last_code , $ line ;
}
close FILE ;
}
2014-03-05 20:58:46 +01:00
unshift @ last_code , $ copy_code if defined $ copy_code ;
2013-09-13 13:32:15 +02:00
2014-03-05 20:58:46 +01:00
if ( $ subcode =~ m/^\s*(?:and\s+)?show(?:\s+\S+)?\s*$/i ) {
2011-01-26 02:59:19 +01:00
if ( defined $ last_code [ 0 ] ) {
print "$nick: $last_code[0]\n" ;
} else {
print "$nick: No recent code to show.\n"
}
exit 0 ;
}
2014-03-05 20:58:46 +01:00
if ( $ subcode =~ m/^\s*(?:and\s+)?diff(?:\s+\S+)?\s*$/i ) {
2013-08-22 22:41:54 +02:00
if ( $# last_code < 1 ) {
print "$nick: Not enough recent code to diff.\n"
} else {
use Text::WordDiff ;
2014-05-31 03:20:31 +02:00
my $ diff = word_diff ( \ $ last_code [ 1 ] , \ $ last_code [ 0 ] , { STYLE = > 'Diff' } ) ;
2013-08-24 17:01:31 +02:00
if ( $ diff !~ /(?:<del>|<ins>)/ ) {
$ diff = "No difference." ;
} else {
2013-08-27 02:42:17 +02:00
$ diff =~ s/<del>(.*?)(\s+)<\/del>/<del>$1<\/del>$2/g ;
$ diff =~ s/<ins>(.*?)(\s+)<\/ins>/<ins>$1<\/ins>$2/g ;
2014-02-24 08:25:50 +01:00
$ diff =~ s/<del>((?:(?!<del>).)*)<\/del>\s*<ins>((?:(?!<ins>).)*)<\/ins>/`replaced $1 with $2`/g ;
$ diff =~ s/<del>(.*?)<\/del>/`removed $1`/g ;
$ diff =~ s/<ins>(.*?)<\/ins>/`inserted $1`/g ;
2013-08-24 17:01:31 +02:00
}
2013-08-22 22:41:54 +02:00
print "$nick: $diff\n" ;
}
exit 0 ;
}
2014-03-05 20:58:46 +01:00
my $ got_run ;
2011-01-26 02:59:19 +01:00
2014-03-05 20:58:46 +01:00
if ( $ subcode =~ m/^\s*(?:and\s+)?(run|paste)\s*$/i ) {
2011-01-26 02:59:19 +01:00
$ got_run = lc $ 1 ;
if ( defined $ last_code [ 0 ] ) {
$ code = $ last_code [ 0 ] ;
2014-03-05 20:58:46 +01:00
$ only_show = 0 ;
2011-01-26 02:59:19 +01:00
} else {
print "$nick: No recent code to $got_run.\n" ;
exit 0 ;
}
} else {
my $ got_undo = 0 ;
my $ got_sub = 0 ;
while ( $ subcode =~ s/^\s*(and)?\s*undo// ) {
splice @ last_code , 0 , 1 ;
if ( not defined $ last_code [ 0 ] ) {
print "$nick: No more undos remaining.\n" ;
exit 0 ;
} else {
$ code = $ last_code [ 0 ] ;
$ got_undo = 1 ;
}
}
my @ replacements ;
my $ prevchange = $ last_code [ 0 ] ;
my $ got_changes = 0 ;
2014-04-02 02:23:14 +02:00
my $ last_keyword ;
2011-01-26 02:59:19 +01:00
while ( 1 ) {
$ got_sub = 0 ;
2013-08-16 19:28:17 +02:00
#$got_changes = 0;
2011-01-26 02:59:19 +01:00
2014-04-02 02:23:14 +02:00
$ subcode =~ s/^\s*and\s+'/and $last_keyword '/ if defined $ last_keyword ;
2011-01-26 02:59:19 +01:00
if ( $ subcode =~ m/^\s*(and)?\s*remove \s*([^']+)?\s*'/ ) {
2014-04-02 02:23:14 +02:00
$ last_keyword = 'remove' ;
2011-01-26 02:59:19 +01:00
my $ modifier = 'first' ;
$ subcode =~ s/^\s*(and)?\s*// ;
$ subcode =~ s/remove\s*([^']+)?\s*//i ;
$ modifier = $ 1 if defined $ 1 ;
$ modifier =~ s/\s+$// ;
my ( $ e , $ r ) = extract_delimited ( $ subcode , "'" ) ;
my $ text ;
if ( defined $ e ) {
$ text = $ e ;
$ text =~ s/^'// ;
$ text =~ s/'$// ;
$ subcode = "replace $modifier '$text' with ''$r" ;
} else {
2013-06-18 00:25:05 +02:00
print "$nick: Unbalanced single quotes. Usage: cc remove [all, first, .., tenth, last] 'text' [and ...]\n" ;
2011-01-26 02:59:19 +01:00
exit 0 ;
}
next ;
}
if ( $ subcode =~ s/^\s*(and)?\s*prepend '// ) {
2014-04-02 02:23:14 +02:00
$ last_keyword = 'prepend' ;
2011-01-26 02:59:19 +01:00
$ subcode = "'$subcode" ;
my ( $ e , $ r ) = extract_delimited ( $ subcode , "'" ) ;
my $ text ;
if ( defined $ e ) {
$ text = $ e ;
$ text =~ s/^'// ;
$ text =~ s/'$// ;
$ subcode = $ r ;
$ got_sub = 1 ;
$ got_changes = 1 ;
if ( not defined $ prevchange ) {
print "$nick: No recent code to prepend to.\n" ;
exit 0 ;
}
$ code = $ prevchange ;
$ code =~ s/^/$text / ;
$ prevchange = $ code ;
} else {
2013-06-18 00:25:05 +02:00
print "$nick: Unbalanced single quotes. Usage: cc prepend 'text' [and ...]\n" ;
2011-01-26 02:59:19 +01:00
exit 0 ;
}
next ;
}
if ( $ subcode =~ s/^\s*(and)?\s*append '// ) {
2014-04-02 02:23:14 +02:00
$ last_keyword = 'append' ;
2011-01-26 02:59:19 +01:00
$ subcode = "'$subcode" ;
my ( $ e , $ r ) = extract_delimited ( $ subcode , "'" ) ;
my $ text ;
if ( defined $ e ) {
$ text = $ e ;
$ text =~ s/^'// ;
$ text =~ s/'$// ;
$ subcode = $ r ;
$ got_sub = 1 ;
$ got_changes = 1 ;
if ( not defined $ prevchange ) {
print "$nick: No recent code to append to.\n" ;
exit 0 ;
}
$ code = $ prevchange ;
$ code =~ s/$/ $text/ ;
$ prevchange = $ code ;
} else {
2013-06-18 00:25:05 +02:00
print "$nick: Unbalanced single quotes. Usage: cc append 'text' [and ...]\n" ;
2011-01-26 02:59:19 +01:00
exit 0 ;
}
next ;
}
2013-09-13 11:29:24 +02:00
if ( $ subcode =~ m/^\s*(and)?\s*replace\s*([^']+)?\s*'.*'\s*with\s*'.*?'/i ) {
2014-04-02 02:23:14 +02:00
$ last_keyword = 'replace' ;
2011-01-26 02:59:19 +01:00
$ got_sub = 1 ;
my $ modifier = 'first' ;
$ subcode =~ s/^\s*(and)?\s*// ;
$ subcode =~ s/replace\s*([^']+)?\s*//i ;
$ modifier = $ 1 if defined $ 1 ;
$ modifier =~ s/\s+$// ;
my ( $ from , $ to ) ;
my ( $ e , $ r ) = extract_delimited ( $ subcode , "'" ) ;
if ( defined $ e ) {
$ from = $ e ;
$ from =~ s/^'// ;
$ from =~ s/'$// ;
$ from = quotemeta $ from ;
$ subcode = $ r ;
$ subcode =~ s/\s*with\s*//i ;
} else {
2013-06-18 00:25:05 +02:00
print "$nick: Unbalanced single quotes. Usage: cc replace 'from' with 'to' [and ...]\n" ;
2011-01-26 02:59:19 +01:00
exit 0 ;
}
( $ e , $ r ) = extract_delimited ( $ subcode , "'" ) ;
if ( defined $ e ) {
$ to = $ e ;
$ to =~ s/^'// ;
$ to =~ s/'$// ;
$ subcode = $ r ;
} else {
2013-06-18 00:25:05 +02:00
print "$nick: Unbalanced single quotes. Usage: cc replace 'from' with 'to' [and replace ... with ... [and ...]]\n" ;
2011-01-26 02:59:19 +01:00
exit 0 ;
}
given ( $ modifier ) {
when ( $ _ eq 'all' ) { }
when ( $ _ eq 'last' ) { }
when ( $ _ eq 'first' ) { $ modifier = 1 ; }
when ( $ _ eq 'second' ) { $ modifier = 2 ; }
when ( $ _ eq 'third' ) { $ modifier = 3 ; }
when ( $ _ eq 'fourth' ) { $ modifier = 4 ; }
when ( $ _ eq 'fifth' ) { $ modifier = 5 ; }
when ( $ _ eq 'sixth' ) { $ modifier = 6 ; }
when ( $ _ eq 'seventh' ) { $ modifier = 7 ; }
when ( $ _ eq 'eighth' ) { $ modifier = 8 ; }
when ( $ _ eq 'nineth' ) { $ modifier = 9 ; }
when ( $ _ eq 'tenth' ) { $ modifier = 10 ; }
default { print "$nick: Bad replacement modifier '$modifier'; valid modifiers are 'all', 'first', 'second', ..., 'tenth', 'last'\n" ; exit 0 ; }
}
my $ replacement = { } ;
$ replacement - > { 'from' } = $ from ;
$ replacement - > { 'to' } = $ to ;
$ replacement - > { 'modifier' } = $ modifier ;
push @ replacements , $ replacement ;
next ;
}
if ( $ subcode =~ m/^\s*(and)?\s*s\/.*\// ) {
2014-04-02 02:23:14 +02:00
$ last_keyword = undef ;
2011-01-26 02:59:19 +01:00
$ got_sub = 1 ;
$ subcode =~ s/^\s*(and)?\s*s// ;
my ( $ regex , $ to ) ;
my ( $ e , $ r ) = extract_delimited ( $ subcode , '/' ) ;
if ( defined $ e ) {
$ regex = $ e ;
$ regex =~ s/^\/// ;
$ regex =~ s/\/$// ;
$ subcode = "/$r" ;
} else {
2013-06-18 00:25:05 +02:00
print "$nick: Unbalanced slashes. Usage: cc s/regex/substitution/[gi] [and s/.../.../ [and ...]]\n" ;
2011-01-26 02:59:19 +01:00
exit 0 ;
}
( $ e , $ r ) = extract_delimited ( $ subcode , '/' ) ;
if ( defined $ e ) {
$ to = $ e ;
$ to =~ s/^\/// ;
$ to =~ s/\/$// ;
$ subcode = $ r ;
} else {
2013-06-18 00:25:05 +02:00
print "$nick: Unbalanced slashes. Usage: cc s/regex/substitution/[gi] [and s/.../.../ [and ...]]\n" ;
2011-01-26 02:59:19 +01:00
exit 0 ;
}
my $ suffix ;
$ suffix = $ 1 if $ subcode =~ s/^([^ ]+)// ;
if ( length $ suffix and $ suffix =~ m/[^gi]/ ) {
print "$nick: Bad regex modifier '$suffix'. Only 'i' and 'g' are allowed.\n" ;
exit 0 ;
}
if ( defined $ prevchange ) {
$ code = $ prevchange ;
} else {
print "$nick: No recent code to change.\n" ;
exit 0 ;
}
my $ ret = eval {
my ( $ ret , $ a , $ b , $ c , $ d , $ e , $ f , $ g , $ h , $ i , $ before , $ after ) ;
2011-02-08 02:27:45 +01:00
2011-01-26 02:59:19 +01:00
if ( not length $ suffix ) {
$ ret = $ code =~ s | $ regex | $ to | ;
( $ a , $ b , $ c , $ d , $ e , $ f , $ g , $ h , $ i ) = ( $ 1 , $ 2 , $ 3 , $ 4 , $ 5 , $ 6 , $ 7 , $ 8 , $ 9 ) ;
$ before = $` ;
$ after = $' ;
} elsif ( $ suffix =~ /^i$/ ) {
$ ret = $ code =~ s | $ regex | $ to | i ;
( $ a , $ b , $ c , $ d , $ e , $ f , $ g , $ h , $ i ) = ( $ 1 , $ 2 , $ 3 , $ 4 , $ 5 , $ 6 , $ 7 , $ 8 , $ 9 ) ;
$ before = $` ;
$ after = $' ;
} elsif ( $ suffix =~ /^g$/ ) {
$ ret = $ code =~ s | $ regex | $ to | g ;
( $ a , $ b , $ c , $ d , $ e , $ f , $ g , $ h , $ i ) = ( $ 1 , $ 2 , $ 3 , $ 4 , $ 5 , $ 6 , $ 7 , $ 8 , $ 9 ) ;
$ before = $` ;
$ after = $' ;
} elsif ( $ suffix =~ /^ig$/ or $ suffix =~ /^gi$/ ) {
$ ret = $ code =~ s | $ regex | $ to | gi ;
( $ a , $ b , $ c , $ d , $ e , $ f , $ g , $ h , $ i ) = ( $ 1 , $ 2 , $ 3 , $ 4 , $ 5 , $ 6 , $ 7 , $ 8 , $ 9 ) ;
$ before = $` ;
$ after = $' ;
}
if ( $ ret ) {
$ code =~ s/\$1/$a/g ;
$ code =~ s/\$2/$b/g ;
$ code =~ s/\$3/$c/g ;
$ code =~ s/\$4/$d/g ;
$ code =~ s/\$5/$e/g ;
$ code =~ s/\$6/$f/g ;
$ code =~ s/\$7/$g/g ;
$ code =~ s/\$8/$h/g ;
$ code =~ s/\$9/$i/g ;
$ code =~ s/\$`/$before/g ;
$ code =~ s/\$'/$after/g ;
}
return $ ret ;
} ;
if ( $@ ) {
2013-08-27 02:42:17 +02:00
my $ foo = $@ ;
$ foo =~ s/ at \.\/compiler_vm_client.pl line \d+\.\s*// ;
print "$nick: $foo\n" ;
2011-01-26 02:59:19 +01:00
exit 0 ;
}
if ( $ ret ) {
$ got_changes = 1 ;
}
$ prevchange = $ code ;
}
if ( $ got_sub and not $ got_changes ) {
print "$nick: No substitutions made.\n" ;
exit 0 ;
} elsif ( $ got_sub and $ got_changes ) {
next ;
}
last ;
}
if ( $# replacements > - 1 ) {
2014-04-29 19:00:51 +02:00
use re::engine::RE2 - strict = > 1 ;
2011-01-26 02:59:19 +01:00
@ replacements = sort { $ a - > { 'from' } cmp $ b - > { 'from' } or $ a - > { 'modifier' } <=> $ b - > { 'modifier' } } @ replacements ;
my ( $ previous_from , $ previous_modifier ) ;
foreach my $ replacement ( @ replacements ) {
my $ from = $ replacement - > { 'from' } ;
my $ to = $ replacement - > { 'to' } ;
my $ modifier = $ replacement - > { 'modifier' } ;
if ( defined $ previous_from ) {
if ( $ previous_from eq $ from and $ previous_modifier =~ /^\d+$/ ) {
$ modifier -= $ modifier - $ previous_modifier ;
}
}
if ( defined $ prevchange ) {
$ code = $ prevchange ;
} else {
print "$nick: No recent code to change.\n" ;
exit 0 ;
}
my $ ret = eval {
my $ got_change ;
my ( $ first_char , $ last_char , $ first_bound , $ last_bound ) ;
$ first_char = $ 1 if $ from =~ m/^(.)/ ;
$ last_char = $ 1 if $ from =~ m/(.)$/ ;
if ( $ first_char =~ /\W/ ) {
$ first_bound = '.' ;
} else {
$ first_bound = '\b' ;
}
if ( $ last_char =~ /\W/ ) {
$ last_bound = '\B' ;
} else {
$ last_bound = '\b' ;
}
if ( $ modifier eq 'all' ) {
2013-06-18 00:25:05 +02:00
if ( $ code =~ s/($first_bound)$from($last_bound)/$1$to$2/g ) {
2011-01-26 02:59:19 +01:00
$ got_change = 1 ;
}
} elsif ( $ modifier eq 'last' ) {
if ( $ code =~ s/(.*)($first_bound)$from($last_bound)/$1$2$to$3/ ) {
$ got_change = 1 ;
}
} else {
my $ count = 0 ;
my $ unescaped = $ from ;
$ unescaped =~ s/\\//g ;
2014-04-29 19:00:51 +02:00
if ( $ code =~ s/($first_bound)$from($last_bound)/if(++$count == $modifier) { "$1$to$2"; } else { "$1$unescaped$2"; }/ge ) {
2011-01-26 02:59:19 +01:00
$ got_change = 1 ;
}
}
return $ got_change ;
} ;
if ( $@ ) {
2013-08-27 02:42:17 +02:00
my $ foo = $@ ;
$ foo =~ s/ at \.\/compiler_vm_client.pl line \d+\.\s*// ;
print "$nick: $foo\n" ;
2011-01-26 02:59:19 +01:00
exit 0 ;
}
if ( $ ret ) {
$ got_sub = 1 ;
$ got_changes = 1 ;
}
$ prevchange = $ code ;
$ previous_from = $ from ;
$ previous_modifier = $ modifier ;
}
2013-09-13 11:29:24 +02:00
if ( not $ got_changes ) {
2011-01-26 02:59:19 +01:00
print "$nick: No replacements made.\n" ;
exit 0 ;
}
}
2013-09-13 13:32:15 +02:00
$ save_last_code = 1 ;
2014-03-05 20:58:46 +01:00
unless ( $ got_undo and not $ got_changes ) {
$ unshift_last_code = 1 unless $ copy_code and not $ got_changes ;
}
if ( $ copy_code and $ got_changes ) {
$ only_show = 0 ;
2011-01-26 02:59:19 +01:00
}
2013-08-16 19:28:17 +02:00
if ( $ got_undo and not $ got_changes ) {
2013-09-13 13:32:15 +02:00
$ only_show = 1 ;
2011-01-26 02:59:19 +01:00
}
}
2013-09-13 13:03:50 +02:00
my $ lang = "C11" ;
2012-01-28 08:39:13 +01:00
$ lang = uc $ 1 if $ code =~ s/-lang=([^\b\s]+)//i ;
2013-09-13 13:03:50 +02:00
my $ input = "" ;
2012-07-22 21:22:30 +02:00
$ input = $ 1 if $ code =~ s/-(?:input|stdin)=(.*)$//i ;
2013-09-13 13:03:50 +02:00
my $ extracted_args = '' ;
my $ got_paste = undef ;
$ got_paste = 1 and $ extracted_args . = "-paste " if $ code =~ s/(?<=\s)*-paste\s*//i ;
my $ got_nomain = undef ;
$ got_nomain = 1 and $ extracted_args . = "-nomain " if $ code =~ s/(?<=\s)*-nomain\s*//i ;
2014-05-02 02:43:42 +02:00
my $ include_args = "" ;
while ( $ code =~ s/-include\s+(\S+)\s+// ) {
$ include_args . = "#include <$1> " ;
}
2013-09-13 13:03:50 +02:00
my $ args = "" ;
2011-01-26 02:59:19 +01:00
$ args . = "$1 " while $ code =~ s/^\s*(-[^ ]+)\s*// ;
$ args =~ s/\s+$// ;
2014-05-02 02:43:42 +02:00
$ code = "$include_args$code" ;
2013-09-13 13:03:50 +02:00
if ( $ save_last_code ) {
2013-09-13 13:32:15 +02:00
if ( $ unshift_last_code ) {
unshift @ last_code , $ extracted_args . ( length $ args ? $ args . ' ' . $ code : $ code ) ;
}
2014-03-04 22:40:51 +01:00
open FILE , "> history/$channel.hist" ;
2013-09-13 13:03:50 +02:00
my $ i = 0 ;
foreach my $ line ( @ last_code ) {
last if ( + + $ i > $ MAX_UNDO_HISTORY ) ;
print FILE "$line\n" ;
}
close FILE ;
}
2013-09-13 13:32:15 +02:00
if ( $ only_show ) {
print "$nick: $code\n" ;
exit 0 ;
}
2014-03-05 20:58:46 +01:00
unless ( $ got_run and $ copy_code ) {
2011-01-26 02:59:19 +01:00
open FILE , ">> log.txt" ;
2012-02-02 05:14:38 +01:00
print FILE "------------------------------------------------------------------------\n" ;
2011-01-26 02:59:19 +01:00
print FILE localtime ( ) . "\n" ;
print FILE "$nick: $code\n" ;
}
my $ found = 0 ;
my @ langs ;
foreach my $ l ( sort { uc $ a cmp uc $ b } keys % languages ) {
2012-10-24 14:26:18 +02:00
push @ langs , sprintf ( "%s => %s" , $ l , $ languages { $ l } ) ;
2011-01-26 02:59:19 +01:00
if ( uc $ lang eq uc $ l ) {
$ lang = $ l ;
$ found = 1 ;
}
}
if ( not $ found ) {
2012-10-24 14:26:18 +02:00
print "$nick: Invalid language '$lang'. Supported languages are:\n" , ( join ",\n" , @ langs ) , "\n; For additional languages try the cc2 command." ;
2011-01-26 02:59:19 +01:00
exit 0 ;
}
2012-02-11 06:05:49 +01:00
print "code before: [$code]\n" if $ debug ;
2012-09-01 07:20:01 +02:00
# replace \n outside of quotes with literal newline
my $ new_code = "" ;
use constant {
NORMAL = > 0 ,
DOUBLE_QUOTED = > 1 ,
SINGLE_QUOTED = > 2 ,
} ;
my $ state = NORMAL ;
my $ escaped = 0 ;
2012-09-03 09:02:17 +02:00
while ( $ code =~ m/(.)/gs ) {
2012-09-01 07:20:01 +02:00
my $ ch = $ 1 ;
given ( $ ch ) {
when ( '\\' ) {
if ( $ escaped == 0 ) {
$ escaped = 1 ;
next ;
}
}
if ( $ state == NORMAL ) {
when ( $ _ eq '"' and not $ escaped ) {
$ state = DOUBLE_QUOTED ;
}
when ( $ _ eq "'" and not $ escaped ) {
$ state = SINGLE_QUOTED ;
}
when ( $ _ eq 'n' and $ escaped == 1 ) {
$ ch = "\n" ;
$ escaped = 0 ;
}
}
if ( $ state == DOUBLE_QUOTED ) {
when ( $ _ eq '"' and not $ escaped ) {
$ state = NORMAL ;
}
}
if ( $ state == SINGLE_QUOTED ) {
when ( $ _ eq "'" and not $ escaped ) {
$ state = NORMAL ;
}
}
}
$ new_code . = '\\' and $ escaped = 0 if $ escaped ;
$ new_code . = $ ch ;
}
$ code = $ new_code ;
2011-01-26 02:59:19 +01:00
2012-09-17 09:12:16 +02:00
print "code after \\n replacement: [$code]\n" if $ debug ;
my $ single_quote = 0 ;
my $ double_quote = 0 ;
my $ parens = 0 ;
my $ escaped = 0 ;
my $ cpp = 0 ; # preprocessor
while ( $ code =~ m/(.)/msg ) {
my $ ch = $ 1 ;
my $ pos = pos $ code ;
print "adding newlines, ch = [$ch], parens: $parens, cpp: $cpp, single: $single_quote, double: $double_quote, escaped: $escaped, pos: $pos\n" if $ debug >= 10 ;
if ( $ ch eq '\\' ) {
$ escaped = not $ escaped ;
} elsif ( $ ch eq '#' and not $ cpp and not $ escaped and not $ single_quote and not $ double_quote ) {
$ cpp = 1 ;
2013-10-11 16:50:29 +02:00
2013-10-12 18:35:23 +02:00
if ( $ code =~ m/include\s*[<"]([^>"]*)[>"]/msg ) {
2013-10-11 16:50:29 +02:00
my $ match = $ 1 ;
$ pos = pos $ code ;
substr ( $ code , $ pos , 0 ) = "\n" ;
pos $ code = $ pos ;
$ cpp = 0 ;
2013-10-25 09:33:43 +02:00
} else {
pos $ code = $ pos ;
2013-10-11 16:50:29 +02:00
}
2012-09-17 09:12:16 +02:00
} elsif ( $ ch eq '"' ) {
2013-10-11 16:50:29 +02:00
$ double_quote = not $ double_quote unless $ escaped or $ single_quote ;
2012-09-17 09:12:16 +02:00
$ escaped = 0 ;
} elsif ( $ ch eq '(' and not $ single_quote and not $ double_quote ) {
$ parens + + ;
} elsif ( $ ch eq ')' and not $ single_quote and not $ double_quote ) {
$ parens - - ;
$ parens = 0 if $ parens < 0 ;
} elsif ( $ ch eq ';' and not $ cpp and not $ single_quote and not $ double_quote and $ parens == 0 ) {
if ( not substr ( $ code , $ pos , 1 ) =~ m/[\n\r]/ ) {
substr ( $ code , $ pos , 0 ) = "\n" ;
pos $ code = $ pos + 1 ;
}
} elsif ( $ ch eq "'" ) {
2013-10-11 16:50:29 +02:00
$ single_quote = not $ single_quote unless $ escaped or $ double_quote ;
2012-09-17 09:12:16 +02:00
$ escaped = 0 ;
} elsif ( $ ch eq 'n' and $ escaped ) {
if ( not $ single_quote and not $ double_quote ) {
print "added newline\n" if $ debug >= 10 ;
substr ( $ code , $ pos - 2 , 2 ) = "\n" ;
pos $ code = $ pos ;
$ cpp = 0 ;
}
$ escaped = 0 ;
} elsif ( $ ch eq '{' and not $ cpp and not $ single_quote and not $ double_quote ) {
if ( not substr ( $ code , $ pos , 1 ) =~ m/[\n\r]/ ) {
substr ( $ code , $ pos , 0 ) = "\n" ;
pos $ code = $ pos + 1 ;
}
} elsif ( $ ch eq '}' and not $ cpp and not $ single_quote and not $ double_quote ) {
if ( not substr ( $ code , $ pos , 1 ) =~ m/[\n\r;]/ ) {
substr ( $ code , $ pos , 0 ) = "\n" ;
pos $ code = $ pos + 1 ;
}
} elsif ( $ ch eq "\n" and $ cpp and not $ single_quote and not $ double_quote ) {
$ cpp = 0 ;
} else {
$ escaped = 0 ;
}
}
print "code after \\n additions: [$code]\n" if $ debug ;
2012-02-11 06:05:49 +01:00
2013-10-11 16:50:29 +02:00
# white-out contents of quoted literals
my $ white_code = $ code ;
$ white_code =~ s/(?:\"((?:\\\"|(?!\").)*)\")/'"' . ('-' x length $1) . '"'/ge ;
$ white_code =~ s/(?:\'((?:\\\'|(?!\').)*)\')/"'" . ('-' x length $1) . "'"/ge ;
2011-01-29 21:50:44 +01:00
my $ precode ;
2013-10-11 16:50:29 +02:00
if ( $ white_code =~ m/#include/ ) {
2012-07-22 21:22:30 +02:00
$ precode = $ code ;
2011-01-29 21:50:44 +01:00
} else {
$ precode = $ preludes { $ lang } . $ code ;
}
2011-01-26 02:59:19 +01:00
$ code = '' ;
2012-09-03 20:48:47 +02:00
print "--- precode: [$precode]\n" if $ debug ;
2012-11-21 20:01:10 +01:00
if ( $ lang eq 'C89' or $ lang eq 'C99' or $ lang eq 'C11' or $ lang eq 'C++' ) {
2011-01-26 02:59:19 +01:00
my $ has_main = 0 ;
2011-02-08 02:27:45 +01:00
2011-01-26 02:59:19 +01:00
my $ prelude = '' ;
2013-09-13 13:03:50 +02:00
while ( $ precode =~ s/^\s*(#.*\n{1,2})//g ) {
2012-09-03 20:48:47 +02:00
$ prelude . = $ 1 ;
}
2013-06-18 00:25:05 +02:00
if ( $ precode =~ m/^\s*(#.*)/ms ) {
2012-11-21 20:01:10 +01:00
my $ line = $ 1 ;
if ( $ line !~ m/\n/ ) {
$ warn_unterminated_define = 1 ;
}
}
2012-03-21 16:58:07 +01:00
print "*** prelude: [$prelude]\n precode: [$precode]\n" if $ debug ;
2011-01-26 02:59:19 +01:00
my $ preprecode = $ precode ;
2011-02-08 02:24:12 +01:00
# white-out contents of quoted literals
$ preprecode =~ s/(?:\"((?:\\\"|(?!\").)*)\")/'"' . ('-' x length $1) . '"'/ge ;
$ preprecode =~ s/(?:\'((?:\\\'|(?!\').)*)\')/"'" . ('-' x length $1) . "'"/ge ;
2013-08-27 14:49:14 +02:00
# strip C and C++ style comments
if ( $ lang eq 'C89' or $ args =~ m/-std=(gnu89|c89)/i ) {
$ preprecode =~ s #/\*[^*]*\*+([^/*][^*]*\*+)*/# #gs;
$ preprecode =~ s #|//([^\\]|[^\n][\n]?)*?\n|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse;
} else {
$ preprecode =~ s #|//([^\\]|[^\n][\n]?)*?\n|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse;
$ preprecode =~ s #/\*[^*]*\*+([^/*][^*]*\*+)*/# #gs;
}
2011-02-08 02:24:12 +01:00
print "preprecode: [$preprecode]\n" if $ debug ;
2012-02-11 06:05:49 +01:00
print "looking for functions, has main: $has_main\n" if $ debug >= 2 ;
2014-04-26 17:17:11 +02:00
my $ func_regex = qr/^([ *\w]+)\s+([ ()*\w]+)\s*\(([^;{]*)\s*\)\s*({.*|<%.*|\?\?<.*)/ ims ;
2012-03-21 16:58:07 +01:00
2011-02-08 02:24:12 +01:00
# look for potential functions to extract
2012-09-03 20:48:47 +02:00
while ( $ preprecode =~ /$func_regex/ms ) {
2011-02-08 02:24:12 +01:00
my ( $ pre_ret , $ pre_ident , $ pre_params , $ pre_potential_body ) = ( $ 1 , $ 2 , $ 3 , $ 4 ) ;
2012-11-18 20:18:56 +01:00
print "looking for functions, found [$pre_ret][$pre_ident][$pre_params][$pre_potential_body], has main: $has_main\n" if $ debug >= 1 ;
2012-02-11 06:05:49 +01:00
2011-02-08 02:24:12 +01:00
# find the pos at which this function lives, for extracting from precode
$ preprecode =~ m/(\Q$pre_ret\E\s+\Q$pre_ident\E\s*\(\s*\Q$pre_params\E\s*\)\s*\Q$pre_potential_body\E)/g ;
my $ extract_pos = ( pos $ preprecode ) - ( length $ 1 ) ;
# now that we have the pos, substitute out the extracted potential function from preprecode
2012-09-03 20:48:47 +02:00
$ preprecode =~ s/$func_regex//ms ;
2011-02-08 02:24:12 +01:00
# create tmpcode object that starts from extract pos, to skip any quoted code
my $ tmpcode = substr ( $ precode , $ extract_pos ) ;
2011-02-10 02:32:03 +01:00
print "tmpcode: [$tmpcode]\n" if $ debug ;
2011-02-08 02:24:12 +01:00
$ precode = substr ( $ precode , 0 , $ extract_pos ) ;
print "precode: [$precode]\n" if $ debug ;
2012-03-21 16:58:07 +01:00
$ tmpcode =~ m/$func_regex/ms ;
2011-01-26 02:59:19 +01:00
my ( $ ret , $ ident , $ params , $ potential_body ) = ( $ 1 , $ 2 , $ 3 , $ 4 ) ;
2012-09-03 20:48:47 +02:00
print "1st extract: [$ret][$ident][$params][$potential_body]\n" if $ debug ;
2011-02-08 02:24:12 +01:00
2011-01-26 02:59:19 +01:00
$ ret =~ s/^\s+// ;
$ ret =~ s/\s+$// ;
2012-02-29 02:10:12 +01:00
if ( not length $ ret or $ ret eq "else" or $ ret eq "while" or $ ret eq "if" or $ ret eq "for" or $ ident eq "for" or $ ident eq "while" or $ ident eq "if" ) {
2011-01-26 02:59:19 +01:00
$ precode . = "$ret $ident ($params) $potential_body" ;
next ;
} else {
2012-09-03 20:48:47 +02:00
$ tmpcode =~ s/$func_regex//ms ;
2011-01-26 02:59:19 +01:00
}
2013-08-16 19:28:17 +02:00
$ potential_body =~ s/^\s*<%/{/ms ;
$ potential_body =~ s/%>\s*$/}/ms ;
$ potential_body =~ s/^\s*\?\?</{/ms ;
$ potential_body =~ s/\?\?>$/}/ms ;
2011-02-04 03:50:52 +01:00
my @ extract = extract_bracketed ( $ potential_body , '{}' ) ;
2011-01-26 02:59:19 +01:00
my $ body ;
if ( not defined $ extract [ 0 ] ) {
2013-06-18 00:25:05 +02:00
if ( $ debug == 0 ) {
print "error: unmatched brackets\n" ;
} else {
print "error: unmatched brackets for function '$ident';\n" ;
print "body: [$potential_body]\n" ;
}
2011-02-08 02:24:12 +01:00
exit ;
2011-01-26 02:59:19 +01:00
} else {
$ body = $ extract [ 0 ] ;
$ preprecode . = $ extract [ 1 ] ;
$ precode . = $ extract [ 1 ] ;
}
2011-02-08 02:24:12 +01:00
2012-09-03 20:48:47 +02:00
print "final extract: [$ret][$ident][$params][$body]\n" if $ debug ;
2011-01-26 02:59:19 +01:00
$ code . = "$ret $ident($params) $body\n\n" ;
2014-04-26 17:17:11 +02:00
$ has_main = 1 if $ ident =~ m/^\s*\(?\s*main\s*\)?\s*$/ ;
2011-01-26 02:59:19 +01:00
}
$ precode =~ s/^\s+// ;
$ precode =~ s/\s+$// ;
2012-02-29 02:10:12 +01:00
$ precode =~ s/^{(.*)}$/$1/s ;
2012-12-30 09:14:27 +01:00
if ( not $ has_main and not $ got_nomain ) {
2013-09-16 12:35:32 +02:00
$ code = "$prelude\n$code" . "int main(void) {\n$precode\n;\nreturn 0;\n}\n" ;
2011-02-08 02:24:12 +01:00
$ nooutput = "No warnings, errors or output." ;
2011-01-26 02:59:19 +01:00
} else {
2012-03-21 16:58:07 +01:00
print "code: [$code]; precode: [$precode]\n" if $ debug ;
2013-09-16 12:35:32 +02:00
$ code = "$prelude\n$precode\n\n$code\n" ;
2011-02-08 02:24:12 +01:00
$ nooutput = "No warnings, errors or output." ;
2011-01-26 02:59:19 +01:00
}
} else {
$ code = $ precode ;
}
2012-02-11 06:05:49 +01:00
print "after func extract, code: [$code]\n" if $ debug ;
2011-01-26 02:59:19 +01:00
$ code =~ s/\|n/\n/g ;
$ code =~ s/^\s+// ;
$ code =~ s/\s+$// ;
2012-09-17 09:12:16 +02:00
$ code =~ s/;\s*;\n/;\n/gs ;
2013-08-27 14:49:14 +02:00
$ code =~ s/;(\s*\/\*.*?\*\/\s*);\n/;$1/gs ;
$ code =~ s/;(\s*\/\/.*?\s*);\n/;$1/gs ;
2012-11-18 20:18:56 +01:00
$ code =~ s/({|})\n\s*;\n/$1\n/gs ;
2012-02-11 06:05:49 +01:00
$ code =~ s/(?:\n\n)+/\n\n/g ;
print "final code: [$code]\n" if $ debug ;
2011-01-26 02:59:19 +01:00
2014-02-27 17:31:31 +01:00
$ input = `fortune -u -s` if not length $ input ;
2014-02-24 08:25:50 +01:00
$ input =~ s/[\n\r\t]/ /msg ;
$ input =~ s/:/ - /g ;
$ input =~ s/\s+/ /g ;
2011-01-26 02:59:19 +01:00
2014-03-05 20:58:46 +01:00
print FILE "$nick: [lang:$lang][args:$args][input:$input]\n" , pretty ( $ code ) , "\n" unless $ got_run and $ copy_code ;
2012-02-15 23:00:58 +01:00
2013-10-11 16:50:29 +02:00
my $ pretty_code = pretty $ code ;
2014-02-27 17:31:31 +01:00
$ args . = ' -paste' if defined $ got_paste or $ got_run eq "paste" ;
2013-10-11 16:50:29 +02:00
$ output = compile ( $ lang , $ pretty_code , $ args , $ input , $ USE_LOCAL ) ;
2014-02-27 17:31:31 +01:00
$ args =~ s/ -paste$// if defined $ got_paste or $ got_run eq "paste" ;
2011-01-26 02:59:19 +01:00
2011-02-08 02:24:12 +01:00
if ( $ output =~ m/^\s*$/ ) {
2011-02-08 02:27:45 +01:00
$ output = $ nooutput
2011-02-08 02:24:12 +01:00
} else {
2014-03-05 20:58:46 +01:00
unless ( $ got_run and $ copy_code ) {
2012-02-02 05:14:38 +01:00
print FILE localtime ( ) . "\n" ;
print FILE "$output\n" ;
}
2012-02-29 02:10:12 +01:00
2014-05-08 15:07:24 +02:00
$ output =~ s/In file included from .*?:\d+:\d+.\s*from prog.c:\d+.\s*//msg ;
2014-04-02 02:23:14 +02:00
$ output =~ s/In file included from .*?:\d+:\d+.\s*//msg ;
2014-05-08 15:07:24 +02:00
$ output =~ s/\s*from prog.c:\d+.\s*//g ;
2014-03-01 15:42:08 +01:00
$ output =~ s/prog: prog.c:\d+: [^:]+: Assertion/Assertion/g ;
2014-03-01 21:25:42 +01:00
$ output =~ s , /usr/i nclude / [ ^ : ] + : \ d + : \ d + : \ s + , , g ;
2014-03-01 15:42:08 +01:00
2013-06-18 00:25:05 +02:00
unless ( defined $ got_paste or ( defined $ got_run and $ got_run eq "paste" ) ) {
$ output =~ s/ Line \d+ ://g ;
$ output =~ s/prog\.c:[:\d]*//g ;
} else {
2013-10-11 16:50:29 +02:00
$ output =~ s/prog\.c:(\d+)/\n$1/g ;
2013-06-18 00:25:05 +02:00
$ output =~ s/prog\.c://g ;
}
2014-03-01 15:42:08 +01:00
$ output =~ s/;?\s?__PRETTY_FUNCTION__ = "[^"]+"//g ;
2014-05-08 15:07:24 +02:00
$ output =~ s/(\d+:\d+:\s*)*cc1: (all\s+)?warnings being treated as errors// ;
2013-09-13 13:03:50 +02:00
$ output =~ s/(\d+:\d+:\s*)* \(first use in this function\)//g ;
$ output =~ s/(\d+:\d+:\s*)*error: \(Each undeclared identifier is reported only once.*?\)//msg ;
$ output =~ s/(\d+:\d+:\s*)*ld: warning: cannot find entry symbol _start; defaulting to [^ ]+// ;
2014-05-31 03:20:31 +02:00
# $output =~ s/(\d+:\d+:\s*)*error: (.*?) error/error: $1; error/msg;
2013-09-13 13:03:50 +02:00
$ output =~ s/(\d+:\d+:\s*)*\/tmp\/.*\.o://g ;
$ output =~ s/(\d+:\d+:\s*)*collect2: ld returned \d+ exit status//g ;
2011-02-08 02:27:45 +01:00
$ output =~ s/\(\.text\+[^)]+\)://g ;
$ output =~ s/\[ In/[In/ ;
2013-09-13 13:03:50 +02:00
$ output =~ s/(\d+:\d+:\s*)*warning: Can't read pathname for load map: Input.output error.//g ;
2011-02-08 02:27:45 +01:00
my $ left_quote = chr ( 226 ) . chr ( 128 ) . chr ( 152 ) ;
my $ right_quote = chr ( 226 ) . chr ( 128 ) . chr ( 153 ) ;
2013-06-18 00:25:05 +02:00
$ output =~ s/$left_quote/'/msg ;
$ output =~ s/$right_quote/'/msg ;
$ output =~ s/`/'/msg ;
2012-02-09 19:48:45 +01:00
$ output =~ s/\t/ /g ;
2014-03-01 21:25:42 +01:00
if ( $ output =~ /In function '([^']+)':/ ) {
if ( $ 1 eq 'main' ) {
$ output =~ s/(\d+:\d+:\s*)*\s?In function .main.:\s*//g ;
} else {
$ output =~ s/(\d+:\d+:\s*)*\s?In function .main.:\s?/In function 'main':/g ;
}
}
2014-03-01 15:42:08 +01:00
$ output =~ s/(\d+:\d+:\s*)*warning: unknown conversion type character 'b' in format \[-Wformat=?\]\s+(\d+:\d+:\s*)*warning: too many arguments for format \[-Wformat-extra-args\]/info: %b is a candide extension/g ;
$ output =~ s/(\d+:\d+:\s*)*warning: unknown conversion type character 'b' in format \[-Wformat=?\]//g ;
2012-01-28 08:39:13 +01:00
$ output =~ s/\s\(core dumped\)/./ ;
2012-02-09 19:48:45 +01:00
# $output =~ s/\[\s+/[/g;
2012-01-28 08:39:13 +01:00
$ output =~ s/ \[enabled by default\]//g ;
$ output =~ s/initializer\s+warning: \(near/initializer (near/g ;
2013-09-13 13:03:50 +02:00
$ output =~ s/(\d+:\d+:\s*)*note: each undeclared identifier is reported only once for each function it appears in//g ;
2012-01-28 08:39:13 +01:00
$ output =~ s/\(gdb\)//g ;
2012-02-02 05:14:38 +01:00
$ output =~ s/", '\\(\d{3})' <repeats \d+ times>,? ?"/\\$1/g ;
$ output =~ s/, '\\(\d{3})' <repeats \d+ times>\s*//g ;
$ output =~ s/(\\000)+/\\0/g ;
2012-02-09 19:48:45 +01:00
$ output =~ s/\\0[^">']+/\\0/g ;
2012-09-03 09:02:17 +02:00
$ output =~ s/= (\d+) '\\0'/= $1/g ;
2012-02-02 05:14:38 +01:00
$ output =~ s/\\0"/"/g ;
$ output =~ s/"\\0/"/g ;
$ output =~ s/\.\.\.>/>/g ;
2012-02-04 07:02:52 +01:00
$ output =~ s/(\\\d{3})+//g ;
2012-02-09 19:48:45 +01:00
$ output =~ s/<\s*included at \/home\/compiler\/>\s*//g ;
$ output =~ s/\s*compilation terminated due to -Wfatal-errors\.//g ;
$ output =~ s/^======= Backtrace.*\[vsyscall\]\s*$//ms ;
$ output =~ s/glibc detected \*\*\* \/home\/compiler\/prog: // ;
$ output =~ s/: \/home\/compiler\/prog terminated// ;
2012-02-29 02:10:12 +01:00
$ output =~ s/<Defined at \/home\/compiler\/>/<Defined at \/home\/compiler\/prog.c:0>/g ;
$ output =~ s/\s*In file included from\s+\/usr\/include\/.*?:\d+:\d+:\s*/, /g ;
$ output =~ s/\s*collect2: error: ld returned 1 exit status//g ;
$ output =~ s/In function\s*`main':\s*\/home\/compiler\/ undefined reference to/error: undefined reference to/g ;
2012-07-22 21:22:30 +02:00
$ output =~ s/\/home\/compiler\///g ;
2012-03-12 04:33:54 +01:00
$ output =~ s/compilation terminated.// ;
2013-08-16 19:28:17 +02:00
$ output =~ s/<'(.*)' = char>/<'$1' = int>/g ;
2012-10-24 14:26:18 +02:00
$ output =~ s/= (-?\d+) ''/= $1/g ;
2012-08-18 05:28:10 +02:00
$ output =~ s/, <incomplete sequence >//g ;
2014-05-02 02:43:42 +02:00
$ output =~ s/\s*warning: shadowed declaration is here \[-Wshadow\]//g unless $ got_paste or $ got_run eq 'paste' ;
2012-09-01 07:20:01 +02:00
$ output =~ s/preprocessor macro>\s+<at\s+>/preprocessor macro>/g ;
2012-10-05 03:59:04 +02:00
$ output =~ s/<No symbol table is loaded. Use the "file" command.>\s*//g ;
$ output =~ s/cc1: all warnings being treated as; errors//g ;
2012-11-02 23:08:20 +01:00
$ output =~ s/, note: this is the location of the previous definition//g ;
2014-02-24 08:25:50 +01:00
$ output =~ s/ called by gdb \(\) at statement: void gdb\(\) { __asm__\(""\); }//g ;
# $output =~ s/(?<!\s)(expands to:)/ $1/g;
my $ removed_warning = 0 ;
$ removed_warning + + if $ output =~ s/warning: ISO C forbids nested functions \[-pedantic\]\s*//g ;
if ( $ removed_warning ) {
$ output =~ s/^\[\]\s*// ;
}
2012-08-18 05:28:10 +02:00
# remove duplicate warnings/infos
2012-09-01 07:20:01 +02:00
$ output =~ s/(\[*.*warning:.*?\s*)\1/$1/g ;
2012-08-18 05:28:10 +02:00
$ output =~ s/(info: .*?\s)\1/$1/g ;
$ output =~ s/^\[\s+(warning:|info:)/[$1/ ; # remove leading spaces in first warning/info
2012-03-12 04:33:54 +01:00
# splint
$ output =~ s/Splint 3.1.2 --- 03 May 2009\s*// ;
$ output =~ s/Finished checking --- \d+ code warning\s*// ;
2014-03-05 20:58:46 +01:00
print FILE "splint: [$output]\n" unless $ got_run and $ copy_code ;
2012-03-12 04:33:54 +01:00
$ output =~ s/\s*\(in function main\)\s*Fresh\s*storage\s*.*?\s*not\s*released.*?reference\s+to\s+it\s+is\s+lost.\s*//msg ;
$ output =~ s/\s*\(in function main\)\s*//g ;
$ output =~ s/\s*\(Use\s+.*?\s+to\s+inhibit\s+warning\)//msg ;
$ output =~ s/Suspect modification of observer/Suspect modification of string-literal/g ;
$ output =~ s/Storage\s*declared\s*with\s*observer\s*is\s*possibly\s*modified.\s*Observer\s*storage\s*may\s*not\s*be\s*modified./Such modification is undefined-behavior./gs ;
$ output =~ s/Storage\s*(.*?)?\s*becomes observer\s*//g ;
$ output =~ s/Fresh storage .*? created\s*//g ;
$ output =~ s/Storage .*? becomes null\s*//g ;
$ output =~ s/To\s*make\s*char\s*and\s*int\s*types\s*equivalent,\s*use\s*\+charint.\s*//gs ;
$ output =~ s/To\s*ignore\s*signs\s*in\s*type\s*comparisons\s*use\s*\+ignoresigns\s*//gs ;
$ output =~ s/Fresh storage/Allocated storage/g ;
$ output =~ s/derived\s*from\s*.*?\s*precondition:\s*requires\s*maxSet\(.*?\)\s*>=\s*maxRead\(.*?\)\s*//gs ;
$ output =~ s/\s*needed\s*to\s*satisfy\s*precondition:\s*requires\s*max.*?\(.*?\)\s*>=\s*\d+//gs ;
$ output =~ s/\s*needed\s*to\s*satisfy\s*precondition:\s*requires\s*max.*?\(.*?\)\s*>=\s*.*?@//gs ;
$ output =~ s/\s*To allow all numeric types to match, use \+relaxtypes.//g ;
$ output =~ s/\s*Corresponding format code//g ;
$ output =~ s/Command Line: Setting .*? redundant with current value\s*//g ;
# $output =~ s/maxSet\((.*?)\s*@\s*\)/$1/g;
$ output =~ s/\s*Unable to resolve constraint: requires .*? >= [^ \]]+//gs ;
$ output =~ s/\s*To\s*allow\s*arbitrary\s*integral\s*types\s*to\s*match\s*any\s*integral\s*type,\s*use\s*\+matchanyintegral.//gs ;
2012-12-29 17:19:51 +01:00
$ output =~ s/<Function "main" not defined.>\s+//g ;
$ output =~ s/Make breakpoint pending on future shared library load\? \(y or \[n\]\) \[answered N; input not from terminal\]//g ;
2012-03-12 04:33:54 +01:00
$ output =~ s/\s*Storage\s*.*?\s*becomes\s*static//gs ;
2012-03-21 16:54:43 +01:00
$ output =~ s/Possibly\s*null\s*storage\s*passed\s*as\s*non-null\s*param:/Possibly null storage passed to function:/g ;
2012-03-12 04:33:54 +01:00
$ output =~ s/A\s*possibly\s*null\s*pointer\s*is\s*passed\s*as\s*a\s*parameter\s*corresponding\s*to\s*a\s*formal\s*parameter\s*with\s*no\s*\/\*\@null\@\*\/\s*annotation.\s*If\s*NULL\s*may\s*be\s*used\s*for\s*this\s*parameter,\s*add\s*a\s*\/\*\@null\@\*\/\s*annotation\s*to\s*the\s*function\s*parameter\s*declaration./A possibly null pointer is passed as a parameter to a function./gs ;
2012-08-18 05:28:10 +02:00
$ output =~ s/ called by \?\? \(\)//g ;
2013-02-05 12:14:19 +01:00
$ output =~ s/\s*Copyright\s*\(C\)\s*\d+\s*Free\s*Software\s*Foundation,\s*Inc.\s*This\s*is\s*free\s*software;\s*see\s*the\s*source\s*for\s*copying\s*conditions.\s*\s*There\s*is\s*NO\s*warranty;\s*not\s*even\s*for\s*MERCHANTABILITY\s*or\s*FITNESS\s*FOR\s*A\s*PARTICULAR\s*PURPOSE.//gs ;
2013-08-16 19:28:17 +02:00
$ output =~ s/\s*process\s*\d+\s*is\s*executing\s*new\s*program:\s*.*?\s*Error\s*in\s*re-setting\s*breakpoint\s*\d+:\s*.*?No\s*symbol\s*table\s*is\s*loaded.\s*\s*Use\s*the\s*"file"\s*command.//s ;
2013-10-11 16:50:29 +02:00
$ output =~ s/\](\d+:\d+:\s*)*warning:/]\n$1warning:/g ;
$ output =~ s/\](\d+:\d+:\s*)*error:/]\n$1error:/g ;
2011-02-08 02:24:12 +01:00
}
2011-01-26 02:59:19 +01:00
2012-11-21 20:01:10 +01:00
if ( $ warn_unterminated_define == 1 ) {
if ( $ output =~ m/^\[(warning:|info:)/ ) {
2013-06-18 00:25:05 +02:00
$ output =~ s/^\[/[warning: preprocessor directive not terminated by \\n, the remainder of the line will be part of this directive / ;
2012-11-21 20:01:10 +01:00
} else {
2013-06-18 00:25:05 +02:00
$ output =~ s/^/[warning: preprocessor directive not terminated by \\n, the remainder of the line will be part of this directive] / ;
2012-11-21 20:01:10 +01:00
}
}
2014-03-05 20:58:46 +01:00
unless ( $ got_run and $ copy_code ) {
2012-02-02 05:14:38 +01:00
print FILE "$nick: $output\n" ;
2011-01-26 02:59:19 +01:00
close FILE ;
}
2012-11-14 19:36:04 +01:00
if ( defined $ got_paste or ( defined $ got_run and $ got_run eq "paste" ) ) {
2013-09-13 13:03:50 +02:00
my $ flags = "" ;
$ extracted_args =~ s/-paste //g ;
if ( length $ extracted_args ) {
$ extracted_args =~ s/\s*$// ;
$ flags . = '[' . $ extracted_args . '] ' ;
}
if ( length $ args ) {
$ flags . = "gcc " . $ args . " -o prog prog.c" ;
} else {
$ flags . = $ languages { $ lang } . " -o prog prog.c" ;
}
2013-10-11 16:50:29 +02:00
$ pretty_code . = "\n\n/************* COMPILER FLAGS *************\n$flags\n************** COMPILER FLAGS *************/\n" ;
2013-09-13 13:03:50 +02:00
$ output =~ s/\s+$// ;
2013-10-11 16:50:29 +02:00
$ pretty_code . = "\n/************* OUTPUT *************\n$output\n************** OUTPUT *************/\n" ;
2013-09-13 13:03:50 +02:00
2013-10-11 16:50:29 +02:00
my $ uri = paste_sprunge ( $ pretty_code ) ;
2013-09-13 13:03:50 +02:00
2012-11-02 23:08:20 +01:00
print "$nick: $uri\n" ;
exit 0 ;
}
2014-04-07 09:24:14 +02:00
if ( length $ output > 22 and open FILE , "< history/$channel.last-output" ) {
2014-04-03 01:48:43 +02:00
my $ last_output ;
2014-04-03 09:47:19 +02:00
my $ time = <FILE> ;
2014-04-03 01:48:43 +02:00
2014-04-03 09:47:19 +02:00
if ( gettimeofday - $ time > 60 * 10 ) {
close FILE ;
} else {
while ( my $ line = <FILE> ) {
$ last_output . = $ line ;
}
close FILE ;
2014-04-03 01:48:43 +02:00
2014-04-03 09:47:19 +02:00
if ( $ last_output eq $ output ) {
print "$nick: Same output.\n" ;
exit 0 ;
}
2014-04-03 01:48:43 +02:00
}
}
2011-01-26 02:59:19 +01:00
print "$nick: $output\n" ;
2014-04-03 01:48:43 +02:00
open FILE , "> history/$channel.last-output" or die "Couldn't open $channel.last-output: $!" ;
2014-04-03 09:47:19 +02:00
my $ now = gettimeofday ;
print FILE "$now\n" ;
2014-04-03 01:48:43 +02:00
print FILE "$output" ;
close FILE ;