# Signal-Interrupting Quines

Quining has a long history - if you haven't heard of it, click the tag and read up a little on it.

Output an infinite series of 1s (with no other spacing [newlines, spaces, etc.]) UNTIL SIGINT (typically CTRL-C) is called. When it is called, output the program source.

## Rules

• It must be a valid quine:
• You may use any language.
• As this is a code golf, the shortest answer wins!
• "Language": Truth Machine Source: 1 – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Nov 6 '15 at 9:44
• @n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ That's pretty great, actually. I'm not sure if I'd accept it, because it doesn't output the source at the end, it just stops putting out the source. – Addison Crump Nov 6 '15 at 9:57
• How do you know the last character is not the program outputting its source? :P (It's a joke answer, anyway) – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Nov 6 '15 at 10:10
• ¯\_(ツ)_/¯ API, maybe? – Addison Crump Nov 6 '15 at 10:16

## Python 3, 76 bytes

s="while 1:\n try:print(end='1')\n except:-print('s=%r;exec(s)'%s)";exec(s)


Note that the byte count includes a trailing newline. This also uses -print to error out after quining.

# Pyth, 25 bytes

.xf!p1)jN*2]".xf!p1)jN*2]


A modification of the standard Pyth quine to add a try-except function.

• Would using # work for this? It would save 2 bytes. – Mike Bufardeci Nov 6 '15 at 14:35
• @MikeBufardeci I tried that, but couldn't get to work. Give it a shot yourself. – isaacg Nov 6 '15 at 15:33

# AutoIt, 488429 362 bytes

My brain hurts, this is too meta.

$1=BinaryToString$2=Chr(34)
$s="FileWrite('a','#include<Misc.au3>'&@LF&'Do'&@LF&'ToolTip(1)'&@LF&'Until _IsPressed(Chr(49)&Chr(66))')+RunWait(@AutoItExe&' a')" Execute($s)
$x="$1=BinaryToString\n$2=Chr(34)\n%sExecute($s)\n$x=%s\nClipPut(StringFormat($x,$1(0x223D7324)&$s&$1(0x0A0D22),$2&$x&$2))"
ClipPut(StringFormat($x,$1(0x223D7324)&$s&$1(0x0A0D22),$2&$x&$2))  This is quite interesting in the way that it compiles a child-process which in return keeps outputting 1 to the ToolTip API until ESC is pressed. If ESC is pressed, the child process kills itself and the parent (this quine) resumes execution and dumps it's source to the clipboard. You have to run this from the editor. BTW: This creates an auxiliary file 'a' on your disk. • Auxiliary files! \o/ – Addison Crump Nov 6 '15 at 10:46 # C, 239221206 172 Bytes Definitely could be a lot shorter, but I had to post something on PPCG eventually. *s="*s=%c%s%c,r;h(s){r=1;}main(){signal(2,h);while(!r)printf(%c1%c);printf(s,34,s,34,34,34);}",r;h(s){r=1;}main(){signal(2,h);while(!r)printf("1");printf(s,34,s,34,34,34);}  Compiles with gcc 5.2.1 (with various warnings). ## Haskell, 206 bytes import Control.Exception;main=catch(putStr o)e;o='1':o;e::SomeException->IO();e _=putStr(s++show s);s="import Control.Exception;main=catch(putStr o)e;o='1':o;e::SomeException->IO();e _=putStr(s++show s);s="  # C#, 339 Bytes using d=System.Console;class c{static bool k=1>0;static void Main(){d.CancelKeyPress+=delegate{k=1<0;var s="using d=System.Console;class c{{static bool k=1>0;static void Main(){{d.CancelKeyPress+=delegate{{k=1<0;var s={0}{1}{0};d.WriteLine(s,(char)34,s);}};while(k){{d.Write(1);}}}}}}";d.WriteLine(s,(char)34,s);};while(k){d.Write(1);}}}  • Nice Trick - thanks @LegionMammal978 – Stephan Schinkel Nov 6 '15 at 11:47 • Also, delegate can be replaced with ()=>. Why isn't k a local variable? – LegionMammal978 Nov 6 '15 at 11:53 # Perl 5.10+, 64 bytes perl -E '$_=q{$SIG{INT}=sub{say"$_=q{_};eval";die};{print 1;redo}};eval'  Requires Perl 5.10+ for say, which can be enabled with either -M5.010 or -E. ## How it works This is yet another variation of the following quine, which I seem to use on every quine challenge: _=q{say"$_=q{$_};eval"};eval


Broken down:

perl -E '
$_=q{ # store contents of quine in$_
$SIG{INT}=sub{ # install handler for SIGINT say"\$_=q{$_};eval"; # print quine die # break out of eval }; { print 1; # print "1" redo # restart block } }; eval # eval$_, executing its contents as code
'

• Oops, initially was printing the 1s separated by newlines. Fixed at a cost of 2 bytes. – ThisSuitIsBlackNot Nov 6 '15 at 18:43