about summary refs log tree commit diff stats
path: root/html/021check_instruction.cc.html
blob: 111355b49e0ba410c0cd14792bff7e5e0bbfe7bf (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!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 - 021check_instruction.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; }
.Special { color: #c00000; }
.traceContains { color: #008000; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.cSpecial { color: #008000; }
.Identifier { color: #fcb165; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Introduce a new transform to perform various checks in instructions before</span>
<span class="Comment">//: we start running them. It'll be extensible, so that we can add checks for</span>
<span class="Comment">//: new recipes as we extend 'run' to support them.</span>
<span class="Comment">//:</span>
<span class="Comment">//: Doing checking in a separate part complicates things, because the values</span>
<span class="Comment">//: of variables in memory and the processor (current_recipe_name,</span>
<span class="Comment">//: current_instruction) aren't available at checking time. If I had a more</span>
<span class="Comment">//: sophisticated layer system I'd introduce the simpler version first and</span>
<span class="Comment">//: transform it in a separate layer or set of layers.</span>

<span class="Delimiter">:(before &quot;End Checks&quot;)</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_instruction<span class="Delimiter">);</span>  <span class="Comment">// idempotent</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_instruction<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;--- perform checks for recipe &quot;</span> &lt;&lt; get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name &lt;&lt; end<span class="Delimiter">();</span>
  map&lt;string<span class="Delimiter">,</span> vector&lt;type_ordinal&gt; &gt; metadata<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    instruction&amp; inst = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>is_label<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">switch</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// Primitive Recipe Checks</span>
      <span class="Normal">case</span> COPY: <span class="Delimiter">{</span>
        <span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">)</span> != SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">))</span> <span class="Delimiter">{</span>
          raise &lt;&lt; maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;ingredients and products should match in '&quot;</span> &lt;&lt; inst<span class="Delimiter">.</span>original_string &lt;&lt; <span class="Constant">&quot;'</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
          <span class="Identifier">break</span><span class="Delimiter">;</span>
        <span class="Delimiter">}</span>
        <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
          <span class="Normal">if</span> <span class="Delimiter">(</span>!types_coercible<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Delimiter">{</span>
            raise &lt;&lt; maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;can't copy '&quot;</span> &lt;&lt; inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>original_string &lt;&lt; <span class="Constant">&quot;' to '&quot;</span> &lt;&lt; inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>original_string &lt;&lt; <span class="Constant">&quot;'; types don't match</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
            <span class="Identifier">goto</span> finish_checking_instruction<span class="Delimiter">;</span>
          <span class="Delimiter">}</span>
        <span class="Delimiter">}</span>
        <span class="Identifier">break</span><span class="Delimiter">;</span>
      <span class="Delimiter">}</span>
      <span class="Comment">// End Primitive Recipe Checks</span>
      <span class="Normal">default</span>: <span class="Delimiter">{</span>
        <span class="Comment">// Defined Recipe Checks</span>
        <span class="Comment">// End Defined Recipe Checks</span>
      <span class="Delimiter">}</span>
    <span class="Delimiter">}</span>
    <span class="Normal">finish_checking_instruction</span>:<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario copy_checks_reagent_count)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span><span class="Delimiter">,</span> <span class="Constant">35</span>
]
<span class="traceContains">+error: main: ingredients and products should match in '1:num &lt;- copy 34, 35'</span>

<span class="Delimiter">:(scenario write_scalar_to_array_disallowed)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
  <span class="Constant">1</span>:array:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+error: main: can't copy '34' to '1:array:num'; types don't match</span>

<span class="Delimiter">:(scenario write_scalar_to_array_disallowed_2)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
  <span class="Constant">1</span>:num<span class="Delimiter">,</span> <span class="Constant">2</span>:array:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span><span class="Delimiter">,</span> <span class="Constant">35</span>
]
<span class="traceContains">+error: main: can't copy '35' to '2:array:num'; types don't match</span>

<span class="Delimiter">:(scenario write_scalar_to_address_disallowed)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
  <span class="Constant">1</span>:address:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+error: main: can't copy '34' to '1:address:num'; types don't match</span>

<span class="Delimiter">:(scenario write_address_to_number_allowed)</span>
def main [
  <span class="Constant">1</span>:address:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>/unsafe
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:num
]
<span class="traceContains">+mem: storing 12 in location 2</span>
$error: <span class="Constant">0</span>

<span class="Delimiter">:(scenario write_boolean_to_number_allowed)</span>
def main [
  <span class="Constant">1</span>:<span class="Normal">bool</span><span class="Special"> &lt;- </span>copy <span class="Constant">1</span>/<span class="Constant">true</span>
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:<span class="Normal">bool</span>
]
<span class="traceContains">+mem: storing 1 in location 2</span>
$error: <span class="Constant">0</span>

<span class="Delimiter">:(scenario write_number_to_boolean_allowed)</span>
def main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
  <span class="Constant">2</span>:<span class="Normal">bool</span><span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:num
]
<span class="traceContains">+mem: storing 34 in location 2</span>
$error: <span class="Constant">0</span>

<span class="Delimiter">:(code)</span>
<span class="Comment">// types_match with some leniency</span>
<span class="Normal">bool</span> types_coercible<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; to<span class="Delimiter">,</span> <span class="Normal">const</span> reagent&amp; from<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>types_match<span class="Delimiter">(</span>to<span class="Delimiter">,</span> from<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_address<span class="Delimiter">(</span>from<span class="Delimiter">)</span> &amp;&amp; is_mu_number<span class="Delimiter">(</span>to<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_boolean<span class="Delimiter">(</span>from<span class="Delimiter">)</span> &amp;&amp; is_mu_number<span class="Delimiter">(</span>to<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_number<span class="Delimiter">(</span>from<span class="Delimiter">)</span> &amp;&amp; is_mu_boolean<span class="Delimiter">(</span>to<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> types_match<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; to<span class="Delimiter">,</span> <span class="Normal">const</span> reagent&amp; from<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// to sidestep type-checking, use /unsafe in the source.</span>
  <span class="Comment">// this will be highlighted in red inside vim. just for setting up some tests.</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_unsafe<span class="Delimiter">(</span>from<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>from<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_array<span class="Delimiter">(</span>to<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Comment">// End Matching Types For Literal(to)</span>
    <span class="Comment">// allow writing 0 to any address</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_address<span class="Delimiter">(</span>to<span class="Delimiter">))</span> <span class="Identifier">return</span> from<span class="Delimiter">.</span>name == <span class="Constant">&quot;0&quot;</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!to<span class="Delimiter">.</span>type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>to<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>atom &amp;&amp; to<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;boolean&quot;</span><span class="Delimiter">))</span>
      <span class="Identifier">return</span> from<span class="Delimiter">.</span>name == <span class="Constant">&quot;0&quot;</span> || from<span class="Delimiter">.</span>name == <span class="Constant">&quot;1&quot;</span><span class="Delimiter">;</span>
    <span class="Identifier">return</span> size_of<span class="Delimiter">(</span>to<span class="Delimiter">)</span> == <span class="Constant">1</span><span class="Delimiter">;</span>  <span class="Comment">// literals are always scalars</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> types_strictly_match<span class="Delimiter">(</span>to<span class="Delimiter">,</span> from<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Comment">// copy arguments because later layers will want to make changes to them</span>
<span class="Comment">// without perturbing the caller</span>
<span class="Normal">bool</span> types_strictly_match<span class="Delimiter">(</span>reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> to<span class="Delimiter">,</span> reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> from<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// End Preprocess types_strictly_match(reagent to, reagent from)</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>from<span class="Delimiter">)</span> &amp;&amp; to<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;number&quot;</span><span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Comment">// to sidestep type-checking, use /unsafe in the source.</span>
  <span class="Comment">// this will be highlighted in red inside vim. just for setting up some tests.</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_unsafe<span class="Delimiter">(</span>from<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Comment">// '_' never raises type error</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_dummy<span class="Delimiter">(</span>to<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!to<span class="Delimiter">.</span>type<span class="Delimiter">)</span> <span class="Identifier">return</span> !from<span class="Delimiter">.</span>type<span class="Delimiter">;</span>
  <span class="Identifier">return</span> types_strictly_match<span class="Delimiter">(</span>to<span class="Delimiter">.</span>type<span class="Delimiter">,</span> from<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Comment">// two types match if the second begins like the first</span>
<span class="Comment">// (trees perform the same check recursively on each subtree)</span>
<span class="Normal">bool</span> types_strictly_match<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* to<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree* from<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>from == to<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!from<span class="Delimiter">)</span> <span class="Identifier">return</span> to<span class="Delimiter">-&gt;</span>atom &amp;&amp; to<span class="Delimiter">-&gt;</span>value == <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>to<span class="Delimiter">-&gt;</span>atom &amp;&amp; !from<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Identifier">return</span> from<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>atom &amp;&amp; from<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>name == to<span class="Delimiter">-&gt;</span>name<span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>from<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!to<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>from<span class="Delimiter">-&gt;</span>value == -<span class="Constant">1</span><span class="Delimiter">)</span> <span class="Identifier">return</span> from<span class="Delimiter">-&gt;</span>name == to<span class="Delimiter">-&gt;</span>name<span class="Delimiter">;</span>
    <span class="Identifier">return</span> from<span class="Delimiter">-&gt;</span>value == to<span class="Delimiter">-&gt;</span>value<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> types_strictly_match<span class="Delimiter">(</span>to<span class="Delimiter">-&gt;</span>left<span class="Delimiter">,</span> from<span class="Delimiter">-&gt;</span>left<span class="Delimiter">)</span> &amp;&amp; types_strictly_match<span class="Delimiter">(</span>to<span class="Delimiter">-&gt;</span>right<span class="Delimiter">,</span> from<span class="Delimiter">-&gt;</span>right<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> test_unknown_type_does_not_match_unknown_type<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  reagent a<span class="Delimiter">(</span><span class="Constant">&quot;a:foo&quot;</span><span class="Delimiter">);</span>
  reagent b<span class="Delimiter">(</span><span class="Constant">&quot;b:bar&quot;</span><span class="Delimiter">);</span>
  CHECK<span class="Delimiter">(</span>!types_strictly_match<span class="Delimiter">(</span>a<span class="Delimiter">,</span> b<span class="Delimiter">));</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> test_unknown_type_matches_itself<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  reagent a<span class="Delimiter">(</span><span class="Constant">&quot;a:foo&quot;</span><span class="Delimiter">);</span>
  reagent b<span class="Delimiter">(</span><span class="Constant">&quot;b:foo&quot;</span><span class="Delimiter">);</span>
  CHECK<span class="Delimiter">(</span>types_strictly_match<span class="Delimiter">(</span>a<span class="Delimiter">,</span> b<span class="Delimiter">));</span>
<span class="Delimiter">}</span>

<span class="Comment">//: helpers</span>

<span class="Normal">bool</span> is_unsafe<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> has_property<span class="Delimiter">(</span>r<span class="Delimiter">,</span> <span class="Constant">&quot;unsafe&quot;</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> is_mu_array<span class="Delimiter">(</span>reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// End Preprocess is_mu_array(reagent r)</span>
  <span class="Identifier">return</span> is_mu_array<span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_mu_array<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>type<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  assert<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">);</span>
  <span class="Identifier">return</span> type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;array&quot;</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> is_mu_address<span class="Delimiter">(</span>reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// End Preprocess is_mu_address(reagent r)</span>
  <span class="Identifier">return</span> is_mu_address<span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_mu_address<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>type<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  assert<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">);</span>
  <span class="Identifier">return</span> type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;address&quot;</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> is_mu_boolean<span class="Delimiter">(</span>reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// End Preprocess is_mu_boolean(reagent r)</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!r<span class="Delimiter">.</span>type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>r<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!r<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> r<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;boolean&quot;</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> is_mu_number<span class="Delimiter">(</span>reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// End Preprocess is_mu_number(reagent r)</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!r<span class="Delimiter">.</span>type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!r<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>r<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    <span class="Identifier">return</span> r<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>name == <span class="Constant">&quot;literal-fractional-number&quot;</span>
        || r<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>name == <span class="Constant">&quot;literal&quot;</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;character&quot;</span><span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>  <span class="Comment">// permit arithmetic on unicode code points</span>
  <span class="Identifier">return</span> r<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;number&quot;</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> is_mu_character<span class="Delimiter">(</span>reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// End Preprocess is_mu_character(reagent r)</span>
  <span class="Identifier">return</span> is_mu_character<span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_mu_character<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>type<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> type<span class="Delimiter">-&gt;</span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;character&quot;</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> is_mu_scalar<span class="Delimiter">(</span>reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> is_mu_scalar<span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_mu_scalar<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_address<span class="Delimiter">(</span>type<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>type<span class="Delimiter">))</span>
    <span class="Identifier">return</span> type<span class="Delimiter">-&gt;</span>name != <span class="Constant">&quot;literal-string&quot;</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> size_of<span class="Delimiter">(</span>type<span class="Delimiter">)</span> == <span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
TRUE); if (mucwin) { mucwin_requires_config(mucwin); } } rosterwin_roster(); // check for change in role/affiliation } else { ProfMucWin *mucwin = wins_get_muc(room); if (mucwin && prefs_get_boolean(PREF_MUC_PRIVILEGES)) { // both changed if ((g_strcmp0(role, old_role) != 0) && (g_strcmp0(affiliation, old_affiliation) != 0)) { mucwin_role_and_affiliation_change(mucwin, role, affiliation, actor, reason); // role changed } else if (g_strcmp0(role, old_role) != 0) { mucwin_role_change(mucwin, role, actor, reason); // affiliation changed } else if (g_strcmp0(affiliation, old_affiliation) != 0) { mucwin_affiliation_change(mucwin, affiliation, actor, reason); } } } occupantswin_occupants(room); } void sv_ev_muc_occupant_online(const char *const room, const char *const nick, const char *const jid, const char *const role, const char *const affiliation, const char *const actor, const char *const reason, const char *const show, const char *const status) { Occupant *occupant = muc_roster_item(room, nick); const char *old_role = NULL; const char *old_affiliation = NULL; if (occupant) { old_role = muc_occupant_role_str(occupant); old_affiliation = muc_occupant_affiliation_str(occupant); } gboolean updated = muc_roster_add(room, nick, jid, role, affiliation, show, status); if (jid != NULL && affiliation != NULL) { muc_members_update(room, jid, affiliation); } // not yet finished joining room if (!muc_roster_complete(room)) { return; } // handle nickname change char *old_nick = muc_roster_nick_change_complete(room, nick); if (old_nick) { ProfMucWin *mucwin = wins_get_muc(room); if (mucwin) { mucwin_occupant_nick_change(mucwin, old_nick, nick); wins_private_nick_change(mucwin->roomjid, old_nick, nick); } free(old_nick); occupantswin_occupants(room); rosterwin_roster(); return; } // joined room if (!occupant) { char *muc_status_pref = prefs_get_string(PREF_STATUSES_MUC); ProfMucWin *mucwin = wins_get_muc(room); if (mucwin && g_strcmp0(muc_status_pref, "none") != 0) { mucwin_occupant_online(mucwin, nick, role, affiliation, show, status); } prefs_free_string(muc_status_pref); if (mucwin) { Jid *jidp = jid_create_from_bare_and_resource(mucwin->roomjid, nick); ProfPrivateWin *privwin = wins_get_private(jidp->fulljid); jid_destroy(jidp); if (privwin) { privwin_occupant_online(privwin); } } occupantswin_occupants(room); rosterwin_roster(); return; } // presence updated if (updated) { char *muc_status_pref = prefs_get_string(PREF_STATUSES_MUC); ProfMucWin *mucwin = wins_get_muc(room); if (mucwin && (g_strcmp0(muc_status_pref, "all") == 0)) { mucwin_occupant_presence(mucwin, nick, show, status); } prefs_free_string(muc_status_pref); occupantswin_occupants(room); // presence unchanged, check for role/affiliation change } else { ProfMucWin *mucwin = wins_get_muc(room); if (mucwin && prefs_get_boolean(PREF_MUC_PRIVILEGES)) { // both changed if ((g_strcmp0(role, old_role) != 0) && (g_strcmp0(affiliation, old_affiliation) != 0)) { mucwin_occupant_role_and_affiliation_change(mucwin, nick, role, affiliation, actor, reason); // role changed } else if (g_strcmp0(role, old_role) != 0) { mucwin_occupant_role_change(mucwin, nick, role, actor, reason); // affiliation changed } else if (g_strcmp0(affiliation, old_affiliation) != 0) { mucwin_occupant_affiliation_change(mucwin, nick, affiliation, actor, reason); } } occupantswin_occupants(room); } rosterwin_roster(); } int sv_ev_certfail(const char *const errormsg, TLSCertificate *cert) { // check profanity trusted certs if (tlscerts_exists(cert->fingerprint)) { return 1; } // check current cert char *current_fp = tlscerts_get_current(); if (current_fp && g_strcmp0(current_fp, cert->fingerprint) == 0) { return 1; } cons_show(""); cons_show_error("TLS certificate verification failed: %s", errormsg); cons_show_tlscert(cert); cons_show(""); cons_show("Use '/tls allow' to accept this certificate."); cons_show("Use '/tls always' to accept this certificate permanently."); cons_show("Use '/tls deny' to reject this certificate."); cons_show(""); ui_update(); char *cmd = ui_get_line(); while ((g_strcmp0(cmd, "/tls allow") != 0) && (g_strcmp0(cmd, "/tls always") != 0) && (g_strcmp0(cmd, "/tls deny") != 0) && (g_strcmp0(cmd, "/quit") != 0)) { cons_show("Use '/tls allow' to accept this certificate."); cons_show("Use '/tls always' to accept this certificate permanently."); cons_show("Use '/tls deny' to reject this certificate."); cons_show(""); ui_update(); free(cmd); cmd = ui_get_line(); } if (g_strcmp0(cmd, "/tls allow") == 0) { cons_show("Continuing with connection."); tlscerts_set_current(cert->fingerprint); free(cmd); return 1; } else if (g_strcmp0(cmd, "/tls always") == 0) { cons_show("Adding %s to trusted certificates.", cert->fingerprint); if (!tlscerts_exists(cert->fingerprint)) { tlscerts_add(cert); } free(cmd); return 1; } else if (g_strcmp0(cmd, "/quit") == 0) { prof_set_quit(); free(cmd); return 0; } else { cons_show("Aborting connection."); free(cmd); return 0; } } void sv_ev_lastactivity_response(const char *const from, const int seconds, const char *const msg) { Jid *jidp = jid_create(from); if (!jidp) { return; } GDateTime *now = g_date_time_new_now_local(); GDateTime *active = g_date_time_add_seconds(now, 0 - seconds); gchar *date_fmt = NULL; char *time_pref = prefs_get_string(PREF_TIME_LASTACTIVITY); date_fmt = g_date_time_format(active, time_pref); prefs_free_string(time_pref); assert(date_fmt != NULL); // full jid - last activity if (jidp->resourcepart) { if (seconds == 0) { if (msg) { cons_show("%s currently active, status: %s", from, msg); } else { cons_show("%s currently active", from); } } else { if (msg) { cons_show("%s last active %s, status: %s", from, date_fmt, msg); } else { cons_show("%s last active %s", from, date_fmt); } } // barejid - last logged in } else if (jidp->localpart) { if (seconds == 0) { if (msg) { cons_show("%s currently logged in, status: %s", from, msg); } else { cons_show("%s currently logged in", from); } } else { if (msg) { cons_show("%s last logged in %s, status: %s", from, date_fmt, msg); } else { cons_show("%s last logged in %s", from, date_fmt); } } // domain only - uptime } else { int left = seconds; int days = seconds / 86400; left = left - days * 86400; int hours = left / 3600; left = left - hours * 3600; int minutes = left / 60; left = left - minutes * 60; int seconds = left; cons_show("%s up since %s, uptime %d days, %d hrs, %d mins, %d secs", from, date_fmt, days, hours, minutes, seconds); } g_date_time_unref(now); g_date_time_unref(active); g_free(date_fmt); jid_destroy(jidp); } void sv_ev_bookmark_autojoin(Bookmark *bookmark) { char *nick = NULL; if (bookmark->nick) { nick = strdup(bookmark->nick); } else { char *account_name = session_get_account_name(); ProfAccount *account = accounts_get_account(account_name); nick = strdup(account->muc_nick); account_free(account); } log_debug("Autojoin %s with nick=%s", bookmark->barejid, nick); if (!muc_active(bookmark->barejid)) { presence_join_room(bookmark->barejid, nick, bookmark->password); muc_join(bookmark->barejid, nick, bookmark->password, TRUE); iq_room_affiliation_list(bookmark->barejid, "member", false); iq_room_affiliation_list(bookmark->barejid, "admin", false); iq_room_affiliation_list(bookmark->barejid, "owner", false); } free(nick); } static void _cut(ProfMessage *message, const char *cut) { if (strstr(message->plain, cut)) { char **split = g_strsplit(message->plain, cut, -1); free(message->plain); message->plain = g_strjoinv("", split); g_strfreev(split); } } static void _clean_incoming_message(ProfMessage *message) { _cut(message, "\u200E"); _cut(message, "\u200F"); }