summary refs log tree commit diff stats
path: root/ranger.py
blob: 06b8653106f9fbde29d19135b83a58648cd31135 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
# coding=utf-8
#
# Ranger: Explore your forest of files from inside your terminal
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# ----------------------------------------------------------------------------
#
# An embedded shell script. It allows you to change the directory
# after you exit ranger by starting it with: source ranger ranger
"""":
if [ $1 ]; then
	$@ && cd "$(grep \^\' ~/.ranger/bookmarks | cut -b3-)"
else
	echo "usage: source path/to/ranger.py path/to/ranger.py"
fi
return 1
"""

# Redefine the docstring, since the previous one was hijacked to
# embed a shellscript.
__doc__ = """Ranger - file browser for the unix terminal"""

# Importing the main method may fail if the ranger directory
# is neither in the same directory as this file, nor in one of
# pythons global import paths.
try:
	from ranger.__main__ import main
except ImportError:
	import sys
	if '-d' not in sys.argv and '--debug' not in sys.argv:
		print("Can't import the main module.")
		print("To run an uninstalled copy of ranger,")
		print("launch ranger.py in the top directory.")
	else:
		raise
else:
	main()
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 001help.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.SalientComment { color: #00ffff; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.cSpecial { color: #008000; }
.Identifier { color: #fcb165; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.PreProc { color: #800080; }
.CommentedCode { color: #6c6c6c; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Everything this project/binary supports.</span>
<span class="Comment">//: This should give you a sense for what to look forward to in later layers.</span>

<span class="Delimiter">:(before &quot;End Commandline Parsing&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>argc &lt;= <span class="Constant">1</span> || is_equal<span class="Delimiter">(</span>argv[<span class="Constant">1</span>]<span class="Delimiter">,</span> <span class="Constant">&quot;--help&quot;</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
  <span class="Comment">// this is the functionality later layers will provide</span>
  <span class="Comment">// currently no automated tests for commandline arg parsing</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>argc &lt;= <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    cerr &lt;&lt; <span class="Constant">&quot;Please provide a Mu program to run.</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
         &lt;&lt; <span class="Constant">&quot;</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  cerr &lt;&lt; <span class="Constant">&quot;Usage:</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;  mu [options] [test] [files]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;or:</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;  mu [options] [test] [files] -- [ingredients for function/recipe 'main']</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;Square brackets surround optional arguments.</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;Examples:</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;  To load files and run 'main':</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;    mu file1.mu file2.mu ...</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;  To run all tests:</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;    mu test</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;  To load files and then run all tests:</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;    mu test file1.mu file2.mu ...</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;  To load files and run only the tests in explicitly loaded files (for apps):</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;    mu --test-only-app test file1.mu file2.mu ...</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;  To load all files with a numeric prefix in a directory:</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;    mu directory1 directory2 ...</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;  You can test directories just like files.</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;    mu test directory1 directory2 ...</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;  To pass ingredients to a mu program, provide them after '--':</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;    mu file_or_dir1 file_or_dir2 ... -- ingredient1 ingredient2 ...</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;  To browse a trace generated by a previous run:</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       &lt;&lt; <span class="Constant">&quot;    mu browse-trace file</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       <span class="Delimiter">;</span>
  <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">//: Support for option parsing.</span>
<span class="Comment">//: Options always begin with '--' and are always the first arguments. An</span>
<span class="Comment">//: option will never follow a non-option.</span>
<span class="Delimiter">:(before &quot;End Commandline Parsing&quot;)</span>
<span class="Normal">char</span>** arg = &amp;argv[<span class="Constant">1</span>]<span class="Delimiter">;</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>argc &gt; <span class="Constant">1</span> &amp;&amp; starts_with<span class="Delimiter">(</span>*arg<span class="Delimiter">,</span> <span class="Constant">&quot;--&quot;</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span><span class="Constant">false</span><span class="Delimiter">)</span>
    <span class="Delimiter">;</span>  <span class="Comment">// no-op branch just so any further additions can consistently always start with 'else'</span>
  <span class="Comment">// End Commandline Options(*arg)</span>
  <span class="Normal">else</span>
    cerr &lt;&lt; <span class="Constant">&quot;skipping unknown option &quot;</span> &lt;&lt; *arg &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
  --argc<span class="Delimiter">;</span> ++argv<span class="Delimiter">;</span> ++arg<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="SalientComment">//:: Helper function used by the above fragment of code (and later layers too,</span>
<span class="SalientComment">//:: who knows?).</span>
<span class="Comment">//: The :(code) directive appends function definitions to the end of the</span>
<span class="Comment">//: project. Regardless of where functions are defined, we can call them</span>
<span class="Comment">//: anywhere we like as long as we format the function header in a specific</span>
<span class="Comment">//: way: put it all on a single line without indent, end the line with ') {'</span>
<span class="Comment">//: and no trailing whitespace. As long as functions uniformly start this</span>
<span class="Comment">//: way, our makefile contains a little command to automatically generate</span>
<span class="Comment">//: declarations for them.</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> is_equal<span class="Delimiter">(</span><span class="Normal">char</span>* s<span class="Delimiter">,</span> <span class="Normal">const</span> <span class="Normal">char</span>* lit<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> strncmp<span class="Delimiter">(</span>s<span class="Delimiter">,</span> lit<span class="Delimiter">,</span> strlen<span class="Delimiter">(</span>lit<span class="Delimiter">))</span> == <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> starts_with<span class="Delimiter">(</span><span class="Normal">const</span> string&amp; s<span class="Delimiter">,</span> <span class="Normal">const</span> string&amp; pat<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> s<span class="Delimiter">.</span>substr<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">,</span> pat<span class="Delimiter">.</span>size<span class="Delimiter">())</span> == pat<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">//: I'll throw some style conventions here for want of a better place for them.</span>
<span class="Comment">//: As a rule I hate style guides. Do what you want, that's my motto. But since</span>
<span class="Comment">//: we're dealing with C/C++, the one big thing we want to avoid is undefined</span>
<span class="Comment">//: behavior. If a compiler ever encounters undefined behavior it can make</span>
<span class="Comment">//: your program do anything it wants.</span>
<span class="Comment">//:</span>
<span class="Comment">//: For reference, my checklist of undefined behaviors to watch out for:</span>
<span class="Comment">//:   out-of-bounds access</span>
<span class="Comment">//:   uninitialized variables</span>
<span class="Comment">//:   use after free</span>
<span class="Comment">//:   dereferencing invalid pointers: null, a new of size 0, others</span>
<span class="Comment">//:</span>
<span class="Comment">//:   casting a large number to a type too small to hold it</span>
<span class="Comment">//:</span>
<span class="Comment">//:   integer overflow</span>
<span class="Comment">//:   division by zero and other undefined expressions</span>
<span class="Comment">//:   left-shift by negative count</span>
<span class="Comment">//:   shifting values by more than or equal to the number of bits they contain</span>
<span class="Comment">//:   bitwise operations on signed numbers</span>
<span class="Comment">//:</span>
<span class="Comment">//:   Converting pointers to types of different alignment requirements</span>
<span class="Comment">//:     T* -&gt; void* -&gt; T*: defined</span>
<span class="Comment">//:     T* -&gt; U* -&gt; T*: defined if non-function pointers and alignment requirements are same</span>
<span class="Comment">//:     function pointers may be cast to other function pointers</span>
<span class="Comment">//:</span>
<span class="Comment">//:       Casting a numeric value into a value that can't be represented by the target type (either directly or via static_cast)</span>
<span class="Comment">//:</span>
<span class="Comment">//: To guard against these, some conventions:</span>
<span class="Comment">//:</span>
<span class="Comment">//: 0. Initialize all primitive variables in functions and constructors.</span>
<span class="Comment">//:</span>
<span class="Comment">//: 1. Minimize use of pointers and pointer arithmetic. Avoid 'new' and</span>
<span class="Comment">//: 'delete' as far as possible. Rely on STL to perform memory management to</span>
<span class="Comment">//: avoid use-after-free issues (and memory leaks).</span>
<span class="Comment">//:</span>
<span class="Comment">//: 2. Avoid naked arrays to avoid out-of-bounds access. Never use operator[]</span>
<span class="Comment">//: except with map. Use at() with STL vectors and so on.</span>
<span class="Comment">//:</span>
<span class="Comment">//: 3. Valgrind all the things.</span>
<span class="Comment">//:</span>
<span class="Comment">//: 4. Avoid unsigned numbers. Not strictly an undefined-behavior issue, but</span>
<span class="Comment">//: the extra range doesn't matter, and it's one less confusing category of</span>
<span class="Comment">//: interaction gotchas to worry about.</span>
<span class="Comment">//:</span>
<span class="Comment">//: Corollary: don't use the size() method on containers, since it returns an</span>
<span class="Comment">//: unsigned and that'll cause warnings about mixing signed and unsigned,</span>
<span class="Comment">//: yadda-yadda. Instead use this macro below to perform an unsafe cast to</span>
<span class="Comment">//: signed. We'll just give up immediately if a container's ever too large.</span>
<span class="Comment">//: Basically, Mu is not concerned about this being a little slower than it</span>
<span class="Comment">//: could be. (<a href="https://gist.github.com/rygorous/e0f055bfb74e3d5f0af20690759de5a7)">https://gist.github.com/rygorous/e0f055bfb74e3d5f0af20690759de5a7)</a></span>
<span class="Comment">//:</span>
<span class="Comment">//: Addendum to corollary: We're going to uniformly use int everywhere, to</span>
<span class="Comment">//: indicate that we're oblivious to number size, and since Clang on 32-bit</span>
<span class="Comment">//: platforms doesn't yet support multiplication over 64-bit integers, and</span>
<span class="Comment">//: since multiplying two integers seems like a more common situation to end</span>
<span class="Comment">//: up in than integer overflow.</span>
<span class="Delimiter">:(before &quot;End Includes&quot;)</span>
<span class="PreProc">#define SIZE(X) (assert((X)</span><span class="Delimiter">.</span><span class="PreProc">size() &lt; (</span><span class="Constant">1LL</span><span class="PreProc">&lt;&lt;(</span><span class="Normal">sizeof</span><span class="PreProc">(</span><span class="Normal">int</span><span class="PreProc">)*</span><span class="Constant">8</span><span class="PreProc">-</span><span class="Constant">2</span><span class="PreProc">)))</span><span class="Delimiter">,</span><span class="PreProc"> </span><span class="Normal">static_cast</span><span class="PreProc">&lt;</span><span class="Normal">int</span><span class="PreProc">&gt;((X)</span><span class="Delimiter">.</span><span class="PreProc">size()))</span>

<span class="Comment">//: 5. Integer overflow is guarded against at runtime using the -ftrapv flag</span>
<span class="Comment">//: to the compiler, supported by Clang (GCC version only works sometimes:</span>
<span class="Comment">//: <a href="http://stackoverflow.com/questions/20851061/how-to-make-gcc-ftrapv-work).">http://stackoverflow.com/questions/20851061/how-to-make-gcc-ftrapv-work).</a></span>
<span class="Delimiter">:(before &quot;atexit(teardown)&quot;)</span>
initialize_signal_handlers<span class="Delimiter">();</span>  <span class="Comment">// not always necessary, but doesn't hurt</span>
<span class="CommentedCode">//? cerr &lt;&lt; INT_MAX+1 &lt;&lt; '\n';  // test overflow</span>
<span class="CommentedCode">//? assert(false);  // test SIGABRT</span>
<span class="Delimiter">:(code)</span>
<span class="Comment">// based on <a href="https://spin.atomicobject.com/2013/01/13/exceptions-stack-traces-c">https://spin.atomicobject.com/2013/01/13/exceptions-stack-traces-c</a></span>
<span class="Normal">void</span> initialize_signal_handlers<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Normal">struct</span> sigaction action<span class="Delimiter">;</span>
  bzero<span class="Delimiter">(</span>&amp;action<span class="Delimiter">,</span> <span class="Normal">sizeof</span><span class="Delimiter">(</span>action<span class="Delimiter">));</span>
  action<span class="Delimiter">.</span>sa_sigaction = dump_and_exit<span class="Delimiter">;</span>
  sigemptyset<span class="Delimiter">(</span>&amp;action<span class="Delimiter">.</span>sa_mask<span class="Delimiter">);</span>
  sigaction<span class="Delimiter">(</span><span class="Constant">SIGABRT</span><span class="Delimiter">,</span> &amp;action<span class="Delimiter">,</span> <span class="Constant">NULL</span><span class="Delimiter">);</span>  <span class="Comment">// assert() failure or integer overflow on linux (with -ftrapv)</span>
  sigaction<span class="Delimiter">(</span><span class="Constant">SIGILL</span><span class="Delimiter">,</span>  &amp;action<span class="Delimiter">,</span> <span class="Constant">NULL</span><span class="Delimiter">);</span>  <span class="Comment">// integer overflow on OS X (with -ftrapv)</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> dump_and_exit<span class="Delimiter">(</span><span class="Normal">int</span> sig<span class="Delimiter">,</span> unused siginfo_t* dummy1<span class="Delimiter">,</span> unused <span class="Normal">void</span>* dummy2<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">switch</span> <span class="Delimiter">(</span>sig<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">case</span> <span class="Constant">SIGABRT</span>:
<span class="PreProc">      #ifndef __APPLE__</span>
        cerr &lt;&lt; <span class="Constant">&quot;SIGABRT: might be an integer overflow if it wasn't an assert() failure</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">;</span>
        _Exit<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
<span class="PreProc">      #endif</span>
      <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Normal">case</span> <span class="Constant">SIGILL</span>:
<span class="PreProc">      #ifdef __APPLE__</span>
        cerr &lt;&lt; <span class="Constant">&quot;SIGILL: most likely caused by integer overflow</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">;</span>
        _Exit<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
<span class="PreProc">      #endif</span>
      <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Normal">default</span>:
      <span class="Identifier">break</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Includes&quot;)</span>
<span class="PreProc">#include </span><span class="Constant">&lt;signal.h&gt;</span>

<span class="Comment">//: For good measure we'll also enable SIGFPE.</span>
<span class="Delimiter">:(before &quot;atexit(teardown)&quot;)</span>
feenableexcept<span class="Delimiter">(</span>FE_OVERFLOW | FE_UNDERFLOW<span class="Delimiter">);</span>
<span class="CommentedCode">//? assert(sizeof(int) == 4 &amp;&amp; sizeof(float) == 4);</span>
<span class="CommentedCode">//? //                          | exp   |  mantissa</span>
<span class="CommentedCode">//? int smallest_subnormal = 0b00000000000000000000000000000001;</span>
<span class="CommentedCode">//? float smallest_subnormal_f = *reinterpret_cast&lt;int*&gt;(&amp;smallest_subnormal);</span>
<span class="CommentedCode">//? cerr &lt;&lt; &quot;ε/2: &quot; &lt;&lt; smallest_subnormal_f/2 &lt;&lt; &quot; (underflow)\n&quot;;  // test SIGFPE</span>
<span class="Delimiter">:(before &quot;End Includes&quot;)</span>
<span class="PreProc">#include </span><span class="Constant">&lt;fenv.h&gt;</span>
<span class="Delimiter">:(code)</span>
<span class="PreProc">#ifdef __APPLE__</span>
<span class="Comment">// Public domain polyfill for feenableexcept on OS X</span>
<span class="Comment">// <a href="http://www-personal.umich.edu/~williams/archive/computation/fe-handling-example.c">http://www-personal.umich.edu/~williams/archive/computation/fe-handling-example.c</a></span>
<span class="Normal">inline</span> <span class="Normal">int</span> feenableexcept <span class="Delimiter">(</span><span class="Normal">unsigned</span> <span class="Normal">int</span> excepts<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">static</span> fenv_t fenv<span class="Delimiter">;</span>
  <span class="Normal">unsigned</span> <span class="Normal">int</span> new_excepts = excepts &amp; FE_ALL_EXCEPT<span class="Delimiter">;</span>
  <span class="Normal">unsigned</span> <span class="Normal">int</span> old_excepts<span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>fegetenv<span class="Delimiter">(</span>&amp;fenv<span class="Delimiter">))</span> <span class="Identifier">return</span> -<span class="Constant">1</span><span class="Delimiter">;</span>
  old_excepts = fenv<span class="Delimiter">.</span>__control &amp; FE_ALL_EXCEPT<span class="Delimiter">;</span>
  fenv<span class="Delimiter">.</span>__control &amp;= ~new_excepts<span class="Delimiter">;</span>
  fenv<span class="Delimiter">.</span>__mxcsr   &amp;= ~<span class="Delimiter">(</span>new_excepts &lt;&lt; <span class="Constant">7</span><span class="Delimiter">);</span>
  <span class="Identifier">return</span> fesetenv<span class="Delimiter">(</span>&amp;fenv<span class="Delimiter">)</span> ? -<span class="Constant">1</span> : old_excepts<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="PreProc">#endif</span>

<span class="Comment">//: 6. Map's operator[] being non-const is fucking evil.</span>
<span class="Delimiter">:(before &quot;Globals&quot;)</span>  <span class="Comment">// can't generate prototypes for these</span>
<span class="Comment">// from <a href="http://stackoverflow.com/questions/152643/idiomatic-c-for-reading-from-a-const-map">http://stackoverflow.com/questions/152643/idiomatic-c-for-reading-from-a-const-map</a></span>
<span class="Normal">template</span>&lt;<span class="Normal">typename</span> T&gt; <span class="Normal">typename</span> T::mapped_type&amp; get<span class="Delimiter">(</span>T&amp; map<span class="Delimiter">,</span> <span class="Normal">typename</span> T::key_type <span class="Normal">const</span>&amp; key<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">typename</span> T::iterator iter<span class="Delimiter">(</span>map<span class="Delimiter">.</span>find<span class="Delimiter">(</span>key<span class="Delimiter">));</span>
  assert<span class="Delimiter">(</span>iter != map<span class="Delimiter">.</span>end<span class="Delimiter">());</span>
  <span class="Identifier">return</span> iter<span class="Delimiter">-&gt;</span>second<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">template</span>&lt;<span class="Normal">typename</span> T&gt; <span class="Normal">typename</span> T::mapped_type <span class="Normal">const</span>&amp; get<span class="Delimiter">(</span><span class="Normal">const</span> T&amp; map<span class="Delimiter">,</span> <span class="Normal">typename</span> T::key_type <span class="Normal">const</span>&amp; key<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">typename</span> T::const_iterator iter<span class="Delimiter">(</span>map<span class="Delimiter">.</span>find<span class="Delimiter">(</span>key<span class="Delimiter">));</span>
  assert<span class="Delimiter">(</span>iter != map<span class="Delimiter">.</span>end<span class="Delimiter">());</span>
  <span class="Identifier">return</span> iter<span class="Delimiter">-&gt;</span>second<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">template</span>&lt;<span class="Normal">typename</span> T&gt; <span class="Normal">typename</span> T::mapped_type <span class="Normal">const</span>&amp; put<span class="Delimiter">(</span>T&amp; map<span class="Delimiter">,</span> <span class="Normal">typename</span> T::key_type <span class="Normal">const</span>&amp; key<span class="Delimiter">,</span> <span class="Normal">typename</span> T::mapped_type <span class="Normal">const</span>&amp; value<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  map[key] = value<span class="Delimiter">;</span>
  <span class="Identifier">return</span> map[key]<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">template</span>&lt;<span class="Normal">typename</span> T&gt; <span class="Normal">bool</span> contains_key<span class="Delimiter">(</span>T&amp; map<span class="Delimiter">,</span> <span class="Normal">typename</span> T::key_type <span class="Normal">const</span>&amp; key<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> map<span class="Delimiter">.</span>find<span class="Delimiter">(</span>key<span class="Delimiter">)</span> != map<span class="Delimiter">.</span>end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">template</span>&lt;<span class="Normal">typename</span> T&gt; <span class="Normal">typename</span> T::mapped_type&amp; get_or_insert<span class="Delimiter">(</span>T&amp; map<span class="Delimiter">,</span> <span class="Normal">typename</span> T::key_type <span class="Normal">const</span>&amp; key<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> map[key]<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">//: The contract: any container that relies on get_or_insert should never call</span>
<span class="Comment">//: contains_key.</span>

<span class="Comment">//: 7. istreams are a royal pain in the arse. You have to be careful about</span>
<span class="Comment">//: what subclass you try to putback into. You have to watch out for the pesky</span>
<span class="Comment">//: failbit and badbit. Just avoid eof() and use this helper instead.</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> has_data<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> in &amp;&amp; !in<span class="Delimiter">.</span>eof<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Includes&quot;)</span>
<span class="PreProc">#include </span><span class="Constant">&lt;assert.h&gt;</span>

<span class="PreProc">#include </span><span class="Constant">&lt;iostream&gt;</span>
<span class="Normal">using</span> std::istream<span class="Delimiter">;</span>
<span class="Normal">using</span> std::ostream<span class="Delimiter">;</span>
<span class="Normal">using</span> std::iostream<span class="Delimiter">;</span>
<span class="Normal">using</span> std::cin<span class="Delimiter">;</span>
<span class="Normal">using</span> std::cout<span class="Delimiter">;</span>
<span class="Normal">using</span> std::cerr<span class="Delimiter">;</span>
<span class="PreProc">#include </span><span class="Constant">&lt;iomanip&gt;</span>

<span class="PreProc">#include </span><span class="Constant">&lt;cstring&gt;</span>
<span class="PreProc">#include </span><span class="Constant">&lt;string&gt;</span>
<span class="Normal">using</span> std::string<span class="Delimiter">;</span>

<span class="PreProc">#define unused  __attribute__((unused))</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->