about summary refs log tree commit diff stats
path: root/html/apps/tile/table.mu.html
blob: 1f07d3b28db5f922d6d81336b5693bebbb26a81a (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
<!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 - apps/tile/table.mu</title>
<meta name="Generator" content="Vim/8.1">
<meta name="plugin-version" content="vim8.1_v1">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #c6c6c6; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.CommentedCode { color: #8a8a8a; }
.muComment { color: #005faf; }
.LineNr { }
.SpecialChar { color: #d70000; }
.Delimiter { color: #c000c0; }
.muFunction { color: #af5f00; text-decoration: underline; }
.Constant { color: #008787; }
.PreProc { color: #c000c0; }
-->
</style>

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

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  var lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<a href='https://github.com/akkartik/mu/blob/master/apps/tile/table.mu'>https://github.com/akkartik/mu/blob/master/apps/tile/table.mu</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr">  1 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='table.mu.html#L1'>initialize-table</a></span> _self: (addr <a href='data.mu.html#L47'>table</a>), n: int <span class="Delimiter">{</span>
<span id="L2" class="LineNr">  2 </span>  <span class="PreProc">var</span> self/<span class="Constant">esi</span>: (addr <a href='data.mu.html#L47'>table</a>) <span class="SpecialChar">&lt;-</span> copy _self
<span id="L3" class="LineNr">  3 </span>  <span class="PreProc">var</span> data-ah/<span class="Constant">eax</span>: (addr handle array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> self, data
<span id="L4" class="LineNr">  4 </span>  populate data-ah, n
<span id="L5" class="LineNr">  5 </span><span class="Delimiter">}</span>
<span id="L6" class="LineNr">  6 </span>
<span id="L7" class="LineNr">  7 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='table.mu.html#L7'>deep-copy-table</a></span> _src: (addr <a href='data.mu.html#L47'>table</a>), _dest: (addr <a href='data.mu.html#L47'>table</a>) <span class="Delimiter">{</span>
<span id="L8" class="LineNr">  8 </span><span class="CommentedCode">#?   print-string 0, &quot;deep-copy-table\n&quot;</span>
<span id="L9" class="LineNr">  9 </span>  <span class="PreProc">var</span> src/<span class="Constant">eax</span>: (addr <a href='data.mu.html#L47'>table</a>) <span class="SpecialChar">&lt;-</span> copy _src
<span id="L10" class="LineNr"> 10 </span>  <span class="PreProc">var</span> src-data-ah/<span class="Constant">eax</span>: (addr handle array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> src, data
<span id="L11" class="LineNr"> 11 </span>  <span class="PreProc">var</span> _src-data/<span class="Constant">eax</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *src-data-ah
<span id="L12" class="LineNr"> 12 </span>  <span class="PreProc">var</span> src-data/<span class="Constant">esi</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> copy _src-data
<span id="L13" class="LineNr"> 13 </span>  <span class="PreProc">var</span> n/<span class="Constant">ecx</span>: int <span class="SpecialChar">&lt;-</span> length src-data
<span id="L14" class="LineNr"> 14 </span>  <span class="PreProc">var</span> dest/<span class="Constant">eax</span>: (addr <a href='data.mu.html#L47'>table</a>) <span class="SpecialChar">&lt;-</span> copy _dest
<span id="L15" class="LineNr"> 15 </span>  <a href='table.mu.html#L1'>initialize-table</a> dest, n
<span id="L16" class="LineNr"> 16 </span>  <span class="PreProc">var</span> dest-data-ah/<span class="Constant">eax</span>: (addr handle array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> dest, data
<span id="L17" class="LineNr"> 17 </span>  <span class="PreProc">var</span> _dest-data/<span class="Constant">eax</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *dest-data-ah
<span id="L18" class="LineNr"> 18 </span>  <span class="PreProc">var</span> dest-data/<span class="Constant">edi</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> copy _dest-data
<span id="L19" class="LineNr"> 19 </span>  <span class="PreProc">var</span> i/<span class="Constant">eax</span>: int <span class="SpecialChar">&lt;-</span> copy <span class="Constant">0</span>
<span id="L20" class="LineNr"> 20 </span>  <span class="Delimiter">{</span>
<span id="L21" class="LineNr"> 21 </span>    compare i, n
<span id="L22" class="LineNr"> 22 </span>    <span class="PreProc">break-if-&gt;=</span>
<span id="L23" class="LineNr"> 23 </span><span class="CommentedCode">#?     print-string 0, &quot;iter\n&quot;</span>
<span id="L24" class="LineNr"> 24 </span>    $deep-copy:element: <span class="Delimiter">{</span>
<span id="L25" class="LineNr"> 25 </span>      <span class="PreProc">var</span> offset/<span class="Constant">edx</span>: (offset <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> compute-offset src-data, i
<span id="L26" class="LineNr"> 26 </span>      <span class="PreProc">var</span> src-bind/<span class="Constant">ecx</span>: (addr <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> index src-data, offset
<span id="L27" class="LineNr"> 27 </span>      <span class="PreProc">var</span> dest-bind/<span class="Constant">edx</span>: (addr <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> index dest-data, offset
<span id="L28" class="LineNr"> 28 </span>      <span class="PreProc">var</span> src-key-ah/<span class="Constant">ebx</span>: (addr handle array byte) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> src-bind, key
<span id="L29" class="LineNr"> 29 </span>      <span class="PreProc">var</span> src-key/<span class="Constant">eax</span>: (addr array byte) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *src-key-ah
<span id="L30" class="LineNr"> 30 </span>      compare src-key, <span class="Constant">0</span>
<span id="L31" class="LineNr"> 31 </span>      <span class="PreProc">break-if-=</span>
<span id="L32" class="LineNr"> 32 </span>      <span class="muComment"># copy key</span>
<span id="L33" class="LineNr"> 33 </span>      <span class="PreProc">var</span> dest-key-ah/<span class="Constant">eax</span>: (addr handle array byte) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> dest-bind, key
<span id="L34" class="LineNr"> 34 </span>      copy-object src-key-ah, dest-key-ah
<span id="L35" class="LineNr"> 35 </span>      <span class="muComment"># deep copy value</span>
<span id="L36" class="LineNr"> 36 </span>      <span class="PreProc">var</span> src-val-ah/<span class="Constant">eax</span>: (addr handle <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> src-bind, <a href='data.mu.html#L37'>value</a>
<span id="L37" class="LineNr"> 37 </span>      <span class="PreProc">var</span> _src-val/<span class="Constant">eax</span>: (addr <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *src-val-ah
<span id="L38" class="LineNr"> 38 </span>      <span class="PreProc">var</span> src-val/<span class="Constant">ecx</span>: (addr <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> copy _src-val
<span id="L39" class="LineNr"> 39 </span>      <span class="PreProc">var</span> dest-val-ah/<span class="Constant">eax</span>: (addr handle <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> dest-bind, <a href='data.mu.html#L37'>value</a>
<span id="L40" class="LineNr"> 40 </span>      <a href='../../120allocate.subx.html#L66'>allocate</a> dest-val-ah
<span id="L41" class="LineNr"> 41 </span>      <span class="PreProc">var</span> dest-val/<span class="Constant">eax</span>: (addr <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *dest-val-ah
<span id="L42" class="LineNr"> 42 </span><span class="CommentedCode">#?       print-string 0, &quot;deep copy value {\n&quot;</span>
<span id="L43" class="LineNr"> 43 </span>      <a href='value.mu.html#L338'>deep-copy-value</a> src-val, dest-val
<span id="L44" class="LineNr"> 44 </span><span class="CommentedCode">#?       print-string 0, &quot;}\n&quot;</span>
<span id="L45" class="LineNr"> 45 </span>    <span class="Delimiter">}</span>
<span id="L46" class="LineNr"> 46 </span>    i <span class="SpecialChar">&lt;-</span> increment
<span id="L47" class="LineNr"> 47 </span>    <span class="PreProc">loop</span>
<span id="L48" class="LineNr"> 48 </span>  <span class="Delimiter">}</span>
<span id="L49" class="LineNr"> 49 </span><span class="CommentedCode">#?   print-string 0, &quot;end deep-copy-table\n&quot;</span>
<span id="L50" class="LineNr"> 50 </span><span class="Delimiter">}</span>
<span id="L51" class="LineNr"> 51 </span>
<span id="L52" class="LineNr"> 52 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='table.mu.html#L52'>bind-in-table</a></span> _self: (addr <a href='data.mu.html#L47'>table</a>), key: (addr handle array byte), val: (addr <a href='data.mu.html#L37'>value</a>) <span class="Delimiter">{</span>
<span id="L53" class="LineNr"> 53 </span>  <span class="PreProc">var</span> self/<span class="Constant">esi</span>: (addr <a href='data.mu.html#L47'>table</a>) <span class="SpecialChar">&lt;-</span> copy _self
<span id="L54" class="LineNr"> 54 </span>  <span class="PreProc">var</span> data-ah/<span class="Constant">esi</span>: (addr handle array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> self, data
<span id="L55" class="LineNr"> 55 </span>  <span class="PreProc">var</span> _data/<span class="Constant">eax</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *data-ah
<span id="L56" class="LineNr"> 56 </span>  <span class="PreProc">var</span> data/<span class="Constant">esi</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> copy _data
<span id="L57" class="LineNr"> 57 </span>  <span class="PreProc">var</span> next-empty-slot-index/<span class="Constant">eax</span>: (offset <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='table.mu.html#L63'>next-empty-slot</a> data, key
<span id="L58" class="LineNr"> 58 </span>  <span class="PreProc">var</span> dest/<span class="Constant">eax</span>: (addr <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> index data, next-empty-slot-index
<span id="L59" class="LineNr"> 59 </span>  <a href='table.mu.html#L98'>make-binding</a> dest, key, val
<span id="L60" class="LineNr"> 60 </span><span class="Delimiter">}</span>
<span id="L61" class="LineNr"> 61 </span>
<span id="L62" class="LineNr"> 62 </span><span class="muComment"># manual test: full array of binds</span>
<span id="L63" class="LineNr"> 63 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='table.mu.html#L63'>next-empty-slot</a></span> _data: (addr array <a href='data.mu.html#L52'>bind</a>), key: (addr handle array byte)<span class="PreProc"> -&gt; </span>_/<span class="Constant">eax</span>: (offset <a href='data.mu.html#L52'>bind</a>) <span class="Delimiter">{</span>
<span id="L64" class="LineNr"> 64 </span>  <span class="PreProc">var</span> data/<span class="Constant">esi</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> copy _data
<span id="L65" class="LineNr"> 65 </span>  <span class="PreProc">var</span> len/<span class="Constant">ecx</span>: int <span class="SpecialChar">&lt;-</span> length data
<span id="L66" class="LineNr"> 66 </span>  <span class="PreProc">var</span> i/<span class="Constant">edx</span>: int <span class="SpecialChar">&lt;-</span> copy <span class="Constant">0</span>
<span id="L67" class="LineNr"> 67 </span>  <span class="PreProc">var</span> <a href='data.mu.html#L70'>result</a>/<span class="Constant">eax</span>: (offset <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> copy <span class="Constant">0</span>
<span id="L68" class="LineNr"> 68 </span>  $next-empty-slot:<span class="PreProc">loop</span>: <span class="Delimiter">{</span>
<span id="L69" class="LineNr"> 69 </span>    <a href='data.mu.html#L70'>result</a> <span class="SpecialChar">&lt;-</span> compute-offset data, i
<span id="L70" class="LineNr"> 70 </span>    compare i, len
<span id="L71" class="LineNr"> 71 </span>    <span class="PreProc">break-if-&gt;=</span>
<span id="L72" class="LineNr"> 72 </span>    <span class="Delimiter">{</span>
<span id="L73" class="LineNr"> 73 </span>      <span class="PreProc">var</span> target/<span class="Constant">esi</span>: (addr <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> index data, <a href='data.mu.html#L70'>result</a>
<span id="L74" class="LineNr"> 74 </span>      <span class="PreProc">var</span> target2/<span class="Constant">esi</span>: (addr handle array byte) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> target, key
<span id="L75" class="LineNr"> 75 </span>      <span class="PreProc">var</span> target3/<span class="Constant">eax</span>: (addr array byte) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *target2
<span id="L76" class="LineNr"> 76 </span>      compare target3, <span class="Constant">0</span>
<span id="L77" class="LineNr"> 77 </span>      <span class="PreProc">break-if-=</span> $next-empty-slot:<span class="PreProc">loop</span>
<span id="L78" class="LineNr"> 78 </span>      <span class="muComment"># TODO: how to indicate that key already exists? we don't want to permit rebinding</span>
<span id="L79" class="LineNr"> 79 </span>    <span class="Delimiter">}</span>
<span id="L80" class="LineNr"> 80 </span>    i <span class="SpecialChar">&lt;-</span> increment
<span id="L81" class="LineNr"> 81 </span>    <span class="PreProc">loop</span>
<span id="L82" class="LineNr"> 82 </span>  <span class="Delimiter">}</span>
<span id="L83" class="LineNr"> 83 </span>  <span class="PreProc">return</span> <a href='data.mu.html#L70'>result</a>
<span id="L84" class="LineNr"> 84 </span><span class="Delimiter">}</span>
<span id="L85" class="LineNr"> 85 </span>
<span id="L86" class="LineNr"> 86 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='table.mu.html#L86'>make-number-binding</a></span> _self: (addr <a href='data.mu.html#L52'>bind</a>), key: (addr handle array byte), _val: float <span class="Delimiter">{</span>
<span id="L87" class="LineNr"> 87 </span>  <span class="PreProc">var</span> self/<span class="Constant">esi</span>: (addr <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> copy _self
<span id="L88" class="LineNr"> 88 </span>  <span class="PreProc">var</span> dest/<span class="Constant">eax</span>: (addr handle array byte) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> self, key
<span id="L89" class="LineNr"> 89 </span>  copy-object key, dest
<span id="L90" class="LineNr"> 90 </span>  <span class="PreProc">var</span> dest2/<span class="Constant">eax</span>: (addr handle <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> self, <a href='data.mu.html#L37'>value</a>
<span id="L91" class="LineNr"> 91 </span>  <a href='../../120allocate.subx.html#L66'>allocate</a> dest2
<span id="L92" class="LineNr"> 92 </span>  <span class="PreProc">var</span> dest3/<span class="Constant">eax</span>: (addr <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *dest2
<span id="L93" class="LineNr"> 93 </span>  <span class="PreProc">var</span> dest4/<span class="Constant">eax</span>: (addr float) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> dest3, number-data
<span id="L94" class="LineNr"> 94 </span>  <span class="PreProc">var</span> val/<span class="Constant">xmm0</span>: float <span class="SpecialChar">&lt;-</span> copy _val
<span id="L95" class="LineNr"> 95 </span>  copy-to *dest4, val
<span id="L96" class="LineNr"> 96 </span><span class="Delimiter">}</span>
<span id="L97" class="LineNr"> 97 </span>
<span id="L98" class="LineNr"> 98 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='table.mu.html#L98'>make-binding</a></span> _self: (addr <a href='data.mu.html#L52'>bind</a>), key: (addr handle array byte), val: (addr <a href='data.mu.html#L37'>value</a>) <span class="Delimiter">{</span>
<span id="L99" class="LineNr"> 99 </span>  <span class="PreProc">var</span> self/<span class="Constant">esi</span>: (addr <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> copy _self
<span id="L100" class="LineNr">100 </span>  <span class="PreProc">var</span> dest/<span class="Constant">eax</span>: (addr handle array byte) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> self, key
<span id="L101" class="LineNr">101 </span>  copy-object key, dest
<span id="L102" class="LineNr">102 </span>  <span class="PreProc">var</span> dest2/<span class="Constant">eax</span>: (addr handle <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> self, <a href='data.mu.html#L37'>value</a>
<span id="L103" class="LineNr">103 </span>  <a href='../../120allocate.subx.html#L66'>allocate</a> dest2
<span id="L104" class="LineNr">104 </span>  <span class="PreProc">var</span> dest3/<span class="Constant">eax</span>: (addr <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *dest2
<span id="L105" class="LineNr">105 </span>  copy-object val, dest3
<span id="L106" class="LineNr">106 </span><span class="Delimiter">}</span>
<span id="L107" class="LineNr">107 </span>
<span id="L108" class="LineNr">108 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='table.mu.html#L108'>lookup-binding</a></span> _self: (addr <a href='data.mu.html#L47'>table</a>), key: (addr array byte), out: (addr handle <a href='data.mu.html#L37'>value</a>) <span class="Delimiter">{</span>
<span id="L109" class="LineNr">109 </span>  <span class="PreProc">var</span> self/<span class="Constant">esi</span>: (addr <a href='data.mu.html#L47'>table</a>) <span class="SpecialChar">&lt;-</span> copy _self
<span id="L110" class="LineNr">110 </span>  <span class="PreProc">var</span> data-ah/<span class="Constant">esi</span>: (addr handle array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> self, data
<span id="L111" class="LineNr">111 </span>  <span class="PreProc">var</span> _data/<span class="Constant">eax</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *data-ah
<span id="L112" class="LineNr">112 </span>  <span class="PreProc">var</span> data/<span class="Constant">esi</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> copy _data
<span id="L113" class="LineNr">113 </span>  <span class="PreProc">var</span> len/<span class="Constant">edx</span>: int <span class="SpecialChar">&lt;-</span> length data
<span id="L114" class="LineNr">114 </span>  <span class="PreProc">var</span> i/<span class="Constant">ebx</span>: int <span class="SpecialChar">&lt;-</span> copy <span class="Constant">0</span>
<span id="L115" class="LineNr">115 </span>  $lookup-binding:<span class="PreProc">loop</span>: <span class="Delimiter">{</span>
<span id="L116" class="LineNr">116 </span>    compare i, len
<span id="L117" class="LineNr">117 </span>    <span class="PreProc">break-if-&gt;=</span>
<span id="L118" class="LineNr">118 </span>    <span class="Delimiter">{</span>
<span id="L119" class="LineNr">119 </span>      <span class="PreProc">var</span> offset/<span class="Constant">edx</span>: (offset <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> compute-offset data, i
<span id="L120" class="LineNr">120 </span>      <span class="PreProc">var</span> target-bind/<span class="Constant">esi</span>: (addr <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> index data, offset
<span id="L121" class="LineNr">121 </span>      <span class="PreProc">var</span> target2/<span class="Constant">edx</span>: (addr handle array byte) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> target-bind, key
<span id="L122" class="LineNr">122 </span>      <span class="PreProc">var</span> target3/<span class="Constant">eax</span>: (addr array byte) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *target2
<span id="L123" class="LineNr">123 </span>      compare target3, <span class="Constant">0</span>
<span id="L124" class="LineNr">124 </span>      <span class="PreProc">break-if-=</span> $lookup-binding:<span class="PreProc">loop</span>
<span id="L125" class="LineNr">125 </span>      <span class="PreProc">var</span> is-match?/<span class="Constant">eax</span>: boolean <span class="SpecialChar">&lt;-</span> <a href='../../105string-equal.subx.html#L15'>string-equal?</a> target3, key
<span id="L126" class="LineNr">126 </span>      compare is-match?, <span class="Constant">0</span>  <span class="muComment"># false</span>
<span id="L127" class="LineNr">127 </span>      <span class="PreProc">break-if-=</span>
<span id="L128" class="LineNr">128 </span>      <span class="muComment"># found</span>
<span id="L129" class="LineNr">129 </span>      <span class="PreProc">var</span> target/<span class="Constant">eax</span>: (addr handle <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> target-bind, <a href='data.mu.html#L37'>value</a>
<span id="L130" class="LineNr">130 </span>      copy-object target, out
<span id="L131" class="LineNr">131 </span>      <span class="PreProc">break</span> $lookup-binding:<span class="PreProc">loop</span>
<span id="L132" class="LineNr">132 </span>    <span class="Delimiter">}</span>
<span id="L133" class="LineNr">133 </span>    i <span class="SpecialChar">&lt;-</span> increment
<span id="L134" class="LineNr">134 </span>    <span class="PreProc">loop</span>
<span id="L135" class="LineNr">135 </span>  <span class="Delimiter">}</span>
<span id="L136" class="LineNr">136 </span><span class="Delimiter">}</span>
<span id="L137" class="LineNr">137 </span>
<span id="L138" class="LineNr">138 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='table.mu.html#L138'>dump-table</a></span> _self: (addr <a href='data.mu.html#L47'>table</a>) <span class="Delimiter">{</span>
<span id="L139" class="LineNr">139 </span>  <span class="PreProc">var</span> self/<span class="Constant">esi</span>: (addr <a href='data.mu.html#L47'>table</a>) <span class="SpecialChar">&lt;-</span> copy _self
<span id="L140" class="LineNr">140 </span>  <span class="PreProc">var</span> data-ah/<span class="Constant">esi</span>: (addr handle array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> self, data
<span id="L141" class="LineNr">141 </span>  <span class="PreProc">var</span> _data/<span class="Constant">eax</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *data-ah
<span id="L142" class="LineNr">142 </span>  <span class="PreProc">var</span> data/<span class="Constant">esi</span>: (addr array <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> copy _data
<span id="L143" class="LineNr">143 </span>  <span class="PreProc">var</span> len/<span class="Constant">edx</span>: int <span class="SpecialChar">&lt;-</span> length data
<span id="L144" class="LineNr">144 </span>  <span class="PreProc">var</span> i/<span class="Constant">ebx</span>: int <span class="SpecialChar">&lt;-</span> copy <span class="Constant">0</span>
<span id="L145" class="LineNr">145 </span>  <span class="Delimiter">{</span>
<span id="L146" class="LineNr">146 </span>    compare i, len
<span id="L147" class="LineNr">147 </span>    <span class="PreProc">break-if-&gt;=</span>
<span id="L148" class="LineNr">148 </span>    <span class="PreProc">var</span> offset/<span class="Constant">edx</span>: (offset <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> compute-offset data, i
<span id="L149" class="LineNr">149 </span>    <span class="PreProc">var</span> target-bind/<span class="Constant">esi</span>: (addr <a href='data.mu.html#L52'>bind</a>) <span class="SpecialChar">&lt;-</span> index data, offset
<span id="L150" class="LineNr">150 </span>    <span class="PreProc">var</span> key-ah/<span class="Constant">edx</span>: (addr handle array byte) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> target-bind, key
<span id="L151" class="LineNr">151 </span>    <span class="PreProc">var</span> key/<span class="Constant">eax</span>: (addr array byte) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *key-ah
<span id="L152" class="LineNr">152 </span>    compare key, <span class="Constant">0</span>
<span id="L153" class="LineNr">153 </span>    <span class="PreProc">break-if-=</span>
<span id="L154" class="LineNr">154 </span>    <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, key
<span id="L155" class="LineNr">155 </span>    <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">&quot;: &quot;</span>
<span id="L156" class="LineNr">156 </span>    <span class="PreProc">var</span> val-ah/<span class="Constant">eax</span>: (addr handle <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> target-bind, <a href='data.mu.html#L37'>value</a>
<span id="L157" class="LineNr">157 </span>    <span class="PreProc">var</span> val/<span class="Constant">eax</span>: (addr <a href='data.mu.html#L37'>value</a>) <span class="SpecialChar">&lt;-</span> <a href='../../120allocate.subx.html#L256'>lookup</a> *val-ah
<span id="L158" class="LineNr">158 </span>    <span class="PreProc">var</span> <span class="PreProc">type</span>/<span class="Constant">eax</span>: (addr int) <span class="SpecialChar">&lt;-</span> <a href='../../131table.subx.html#L26'>get</a> val, <span class="PreProc">type</span>
<span id="L159" class="LineNr">159 </span>    <a href='../../405screen.mu.html#L436'>print-int32-hex</a> <span class="Constant">0</span>, *<span class="PreProc">type</span>
<span id="L160" class="LineNr">160 </span>    <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">&quot;\n&quot;</span>
<span id="L161" class="LineNr">161 </span>    i <span class="SpecialChar">&lt;-</span> increment
<span id="L162" class="LineNr">162 </span>    <span class="PreProc">loop</span>
<span id="L163" class="LineNr">163 </span>  <span class="Delimiter">}</span>
<span id="L164" class="LineNr">164 </span>  <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">&quot;\n&quot;</span>
<span id="L165" class="LineNr">165 </span><span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
7f21b4ae754e738cbb577215a039a26e28ad52'>^
d3f9d547 ^

945e8eb6 ^


d3f9d547 ^

945e8eb6 ^


e087f6d4



e4409c40 ^
b6d1143c ^
e4409c40 ^
050803d4 ^
b6d1143c ^
e4409c40 ^
e087f6d4





36863953 ^

e087f6d4


e4409c40 ^
945e8eb6 ^
e087f6d4


57bfc74f ^
e087f6d4
d3f9d547 ^

57bfc74f ^



e087f6d4



e087f6d4



c3ec4181 ^
b6d1143c ^
e087f6d4


d3f9d547 ^
e087f6d4




b6d1143c ^

e087f6d4
d3f9d547 ^

b6d1143c ^
d3f9d547 ^
b6d1143c ^
e087f6d4

b6d1143c ^
e087f6d4



36863953 ^
e087f6d4


e087f6d4
36863953 ^
57bfc74f ^

e087f6d4
36863953 ^

e087f6d4





d3f9d547 ^
e087f6d4
d3f9d547 ^
e087f6d4

36863953 ^
d3f9d547 ^


e087f6d4












57bfc74f ^
36863953 ^
57bfc74f ^
36863953 ^
e087f6d4






c3ec4181 ^
e087f6d4
c3ec4181 ^
b6d1143c ^
e087f6d4
0bafa3a3 ^
a735f5ad ^
b6d1143c ^

e47cfd56 ^






b6d1143c ^


e47cfd56 ^









b6d1143c ^
e087f6d4




d3f9d547 ^

b6d1143c ^
d3f9d547 ^
b6d1143c ^
e087f6d4

a735f5ad ^
e087f6d4


c3ec4181 ^
b6d1143c ^
c3ec4181 ^
e087f6d4


d3f9d547 ^

e087f6d4
a4a7d3a3 ^
a735f5ad ^







36863953 ^
08fc6e5c ^
79de4059 ^
e087f6d4




e4409c40 ^
e087f6d4
b6d1143c ^
e087f6d4

9c63ba04 ^
e087f6d4
e4409c40 ^
e087f6d4
b6d1143c ^
e47cfd56 ^


b6d1143c ^


e47cfd56 ^








b6d1143c ^

e47cfd56 ^

e087f6d4

b6d1143c ^
57bfc74f ^
36863953 ^
57bfc74f ^
36863953 ^
e087f6d4



c3ec4181 ^
b6d1143c ^
e087f6d4



86b4d41a ^
e087f6d4





b6d1143c ^
e087f6d4




57bfc74f ^






























86b4d41a ^

57bfc74f ^
e087f6d4

57bfc74f ^

79de4059 ^





57bfc74f ^








79de4059 ^





57bfc74f ^






e087f6d4





b6d1143c ^
e087f6d4





b6d1143c ^
e087f6d4










b6d1143c ^
e087f6d4

b6d1143c ^
e087f6d4













e087f6d4















af9be28b ^
e087f6d4


b6d1143c ^

e087f6d4

b6d1143c ^

e087f6d4





c3ec4181 ^
b6d1143c ^
c3ec4181 ^
e087f6d4














c3ec4181 ^



e087f6d4







b6d1143c ^


e087f6d4
c3ec4181 ^
36863953 ^

d3f9d547 ^
c3ec4181 ^
d3f9d547 ^
327b7c16 ^



e087f6d4
945e8eb6 ^
d3f9d547 ^
945e8eb6 ^

c3ec4181 ^
d3f9d547 ^
c3ec4181 ^
36863953 ^
c3ec4181 ^



945e8eb6 ^
c3ec4181 ^
e087f6d4




b6d1143c ^
e087f6d4



e087f6d4






































c3ec4181 ^


e087f6d4







c3ec4181 ^


e087f6d4










c3ec4181 ^


e087f6d4
c3ec4181 ^


e087f6d4










e087f6d4






















e087f6d4

c3ec4181 ^



36863953 ^
c3ec4181 ^





36863953 ^
c3ec4181 ^






36863953 ^
c3ec4181 ^


e087f6d4






e087f6d4

c3ec4181 ^

e087f6d4
c3ec4181 ^
36863953 ^
c3ec4181 ^

36863953 ^
e087f6d4

c3ec4181 ^




36863953 ^
c3ec4181 ^









36863953 ^
c3ec4181 ^
e087f6d4






b6d1143c ^

e087f6d4
b6d1143c ^


e087f6d4

b6d1143c ^
e087f6d4


d3f9d547 ^
e087f6d4






c3ec4181 ^



e087f6d4




c3ec4181 ^


e087f6d4




c3ec4181 ^


e087f6d4





c3ec4181 ^



e087f6d4


c3ec4181 ^


e087f6d4














d3f9d547 ^
e087f6d4



e087f6d4
c3ec4181 ^
b6d1143c ^

e087f6d4
c3ec4181 ^
b6d1143c ^

18024037 ^
d3f9d547 ^
e087f6d4
36863953 ^
e087f6d4



c3ec4181 ^
b6d1143c ^
e087f6d4


c3ec4181 ^


e087f6d4
b6d1143c ^


e087f6d4





36863953 ^
d3f9d547 ^
e087f6d4
d3f9d547 ^
e087f6d4
d3f9d547 ^
c3ec4181 ^


e087f6d4
36863953 ^

c3ec4181 ^


36863953 ^


e087f6d4

36863953 ^
c3ec4181 ^


e087f6d4
36863953 ^
c3ec4181 ^


e087f6d4


c3ec4181 ^
b6d1143c ^
e087f6d4


36863953 ^
e087f6d4


c3ec4181 ^


e087f6d4

e087f6d4
945e8eb6 ^
36863953 ^


b63d287c ^
b63d287c ^
373bfbe1 ^
b63d287c ^
b63d287c ^
eef116fe ^
b63d287c ^
36863953 ^
b63d287c ^
36863953 ^
86b4d41a ^
36863953 ^

945e8eb6 ^
945e8eb6 ^







57bfc74f ^


945e8eb6 ^


36863953 ^
945e8eb6 ^



57bfc74f ^
d3f9d547 ^
57bfc74f ^
d3f9d547 ^
57bfc74f ^

2f5222ea ^
443226a5 ^
2f5222ea ^
945e8eb6 ^


57bfc74f ^




c82d2a40 ^
945e8eb6 ^


57bfc74f ^

d3f9d547 ^
945e8eb6 ^





57bfc74f ^
d3f9d547 ^
57bfc74f ^
d3f9d547 ^
57bfc74f ^

2f5222ea ^
443226a5 ^
2f5222ea ^
86b4d41a ^
57bfc74f ^
945e8eb6 ^
57bfc74f ^



c82d2a40 ^
57bfc74f ^
945e8eb6 ^





















443226a5 ^
36863953 ^

945e8eb6 ^
36863953 ^
945e8eb6 ^




443226a5 ^
36863953 ^

945e8eb6 ^
36863953 ^
945e8eb6 ^







































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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
                    
                    













                         
 









                                     
                      

                     


                         
                           

      


                                                                                  
                                                                  
      
 
  

                                                              




                                              
                                         


                         




                                                                              

                                         

                                   
                      



                                          
                            
 

 


                               

                            
                            

                        
     



           




                                                                       




                        








                         
                            
                          




                                  
                            
                          






                                    



                                                                    
      
                                                         
       



                                                                                 
 
      
                                      
       








                                                                             

                              
 
              
           
             
 
                          
                       
                            







                                 
                                             
                               
      
                       
                           
      
                                               
       





                                                             
                          
                 
                    

          





















                                                           
                         
                         
                               














                                                      
                                             

      





                                                                          








                                                                          


                                                                     
                                                                 
                                        
       
                   





                                                                               


                               





                                           

                      
                            

















                                                                                    
                                  
                                                                                       



                    
                   
                                                      

                                           
                              
 
 




                                                           
                                                               
                                                                         

                                    




                                   


                               


                        



                                 
 





                                                                              
                  






                                                                                


                      
                                                                      
                     
                       
      
         
 
                                                                           













                                                                     

                                                                      

                                                       




                                                            
                                               
                                                              
                                                                      
                                                                
                                                                      
                                         
                 
                                                                     
                                                                







                                                                              

                                     
                                                                                 
                                                                     
                 
                                                    





                                                                        
                                                                                 
                                                                     
                 
                                                    












                                                              
                                                                         

                                                       
                                                                          
                                                    
                                                    
                        

                                                                  






                                                 
                                   

 


                               
 
                                       

 
               

                                       
                                




                                            







                                                                              





                                

                                                                 
                                                                  



                                                                                
                                                                                         













                                                                               
                                            
 
                                                         


                            
                                     
 


                                               
         


                                 









                                         

                                                                             
           
                                                              
                               

         
                                                                   
                                    

         


                                              





















                                                     




                                               
         
                                                   
                                   








                                
                                                      



                                                                                








                                        
                                                         



                                         





                                          
                             
        
                             



                                    
                         
                    
 

                                          


                                                                                
                                     

                                       

                                                   
             
                                                       
                                       
         


                                                 







                                    
                            
 
              
                                           





                                                





















                                                                              
 

                    
 


                      

     



                                                    





                               
               


                            



                                                    





                                         









                                                                            
                                     
















                                                                

                       
                                 


                                                     

                                                                
           
                                                                    
                                
                
                                
         
                        
     



                      










                                                                  
                                      
                                                                                
                  

                              

                                             
                      
              

                                    



                                

                         

                         
 

          
                                                                
                  
       
                                  

                  



                                     
                                                             


                                                    

                                                                                    
                                  
         

                                                 
                                                                               
                     



                                                    




                                                                              
                                                                         






                                                                       


                                                    



                 








                                                        
                                    


                                               
                      
                           
      
                           
                   

                                   
                            
     



                      
 
                                                   




             
               
             


                        




                                
               
                        

                        
                         


                   
                   

                      
               
            

      

                      
 
 

                                              

















                                                                              

                      

                                   
     
 
                            





                                                                         
     
                                                                 




                                                                     
                           









                                                         

                                          


                                         

                                                                       


                                                                



                              
            
                
      
                                            
             
     





                                               

                                         


                   
                  
                   


                       
                                          
           

                                 



                                 



                            



                                                       
                           
                                 


               
                                   




                           

                                                                
       

                                      
            
                        
     

      
                                                     



                                                               
          


                                                               
                                       
                    

                                             
                            

                                         





                                         
                      
 
                                                  

                             
                                                      


                                                                                    












                                   
                     
                     
                    
                    






                      
                                                       
   
                           
                                 
 
                             
                        

                                                                          






                                                                    


                                                                 









                                                               
                                                        




                                                               

                                         
            
                        
     

                           
                                                          


                                   
      
                                
       


                                   

                                                                          
 
                                            







                                                       
 
                                   
                                                                                




                           
                
      
                                                              

                                                          
                                    
     
                         
 
                                                                          


                                                                         


                                                                         








                                    

                        

     

                    
                                                            
                     
                     
                    
                    



              
                           
                                 



                     
                                                                         





                                                                       
                                            




                                             






























                             

                             
                         

                

                                        





                                          








                                               





                                           






                                               





                                                                        
                                                                            





                                                                       
                                                   










                                                                               
                                                  

                                                                        
                                                  













                                                                             















                         
                     


                                                                          

                                                                          

                                                                          

                                                                          





                                                                             
                            
                                  
                               














                                                               



                                                                                







                                                                   


                                                               
       
                      

                                     
                                                                                  
                           
                                                                              



                                                                           
 
                                       
                                                  

                                 
                                
                                            
         
                  



                                                         
                       
     




                                                                   
                                                                



                                                                    






































                                                                   


                                           







                                                                  


                        










                                                                    


                                   
                                           


                                        










                                                                      






















                                                                            

                        



                           
                                 





                                                            
                         






                                                                 
                     


                               






                                                                      

                           

                           
 
                                
                  

                        
                                 

      




                                                                        
                         









                                                           
                   
                       






                                                                             

                                                                           
                                                                             


                                                                               

                                                                         
                                                                           


                                                                               
                                






                                



                                                           




                                                       


                                                      




                              


                                   





                                               



                                                                     


                            


                                                                   














                                                                             
                                                                       



                                                                          
             
                                              

                                
     
                                     

                                
                   
 
                
 



                                              
                                   
                                


                              


                                    
                                           


                                                  





                                                                      
                                                         
                                
     
                              
                    
      


                                  
                                               

                                                       


                                                           


                                                   

                                                
                                         


                            
                                            
                    


                             


               
                           
                                


                            
 


                                        


                                            

                    
                
 


                                     
             
                                     
                       
        
                     
                     
                       
                  
      
               
                         

                          
 







                                         


                                        


                                                  
                                                  



                            
                      
                 
                                 
                       

                               
                                                  
                                                           
                               


                      




                                                                 
                              


      

                                       
                                   





                                                            
                      
                 
                                 
                       

                          
                                                  
                                                           
                              
                                         
            
                    



                                                                      
                             
     





















                                            
                                                       

                           
      
                         




                                            
                                                       

                           
      
                        







































                                          
#include <HTUtils.h>
#include <HTAlert.h>
#include <LYCurses.h>
#include <LYStyle.h>
#include <LYUtils.h>
#include <LYGlobalDefs.h>
#include <LYMainLoop.h>
#include <LYSignal.h>
#include <LYClean.h>
#include <LYReadCFG.h>
#include <LYStrings.h>
#include <LYCharSets.h>
#include <UCAux.h>

#include <LYexit.h>
#include <LYLeaks.h>

#if defined(VMS) && defined(__GNUC__)
#include <gnu_hacks.h>
#undef LINES
#undef COLS
#define LINES lines
#define COLS cols
extern int _NOSHARE(LINES);
extern int _NOSHARE(COLS);
#endif /* VMS && __GNUC__ */

#ifdef USE_COLOR_STYLE
#include <AttrList.h>
#include <LYHash.h>
#endif

#if defined(COLOR_CURSES)
int lynx_has_color = FALSE;
#endif

#if defined(USE_COLOR_STYLE) && !USE_COLOR_TABLE
#define COLOR_BKGD ((s_normal != NOSTYLE) ? hashStyles[s_normal].color : A_NORMAL)
#else
#define COLOR_BKGD ((COLOR_PAIRS >= 9) ? COLOR_PAIR(9) : A_NORMAL)
#endif

/*
 *  These are routines to start and stop curses and to cleanup
 *  the screen at the end.
 */

PRIVATE int dumbterm PARAMS((char *terminal));
BOOLEAN LYCursesON = FALSE;

#if USE_COLOR_TABLE || defined(USE_SLANG)
PRIVATE int Current_Attr;
#endif

#define OMIT_SCN_KEEPING 0 /* whether to omit keeping of Style_className
    in HTML.c when lss support is on. 1 to increase performance. The value
    must correspond to the value of macro OMIT_SCN_KEEPING defined in HTML.c*/


#ifdef USE_SLANG
PUBLIC unsigned int Lynx_Color_Flags = 0;
PUBLIC BOOLEAN FullRefresh = FALSE;
PUBLIC int curscr = 0;
#ifdef SLANG_MBCS_HACK
/*
 *  Will be set by size_change. - KW
 */
PUBLIC int PHYSICAL_SLtt_Screen_Cols = 10;
#endif /* SLANG_MBCS_HACK */



PUBLIC void LY_SLrefresh NOARGS
{
    if (FullRefresh) {
	SLsmg_suspend_smg();
	SLsmg_resume_smg();
	FullRefresh = FALSE;
    } else {
	SLsmg_refresh();
    }

    return;
}

/* the following renamed from LY_SLclear since it is more like erase()
   described in curses man pages than like clear(); but for USE_SLANG
   clear() is still a macro calling this, and will do the same thing as
   erase(). - kw */
PUBLIC void LY_SLerase NOARGS
{
    SLsmg_gotorc (0, 0);
    SLsmg_erase_eos ();
}

#ifdef VMS
PUBLIC void VTHome NOARGS
{
    printf("\033[;H");

    return;
}
#endif /* VMS */

PUBLIC void LYaddAttr ARGS1(
	int,		a)
{
    Current_Attr |= a;
    SLsmg_set_color(Current_Attr);
}

PUBLIC void LYsubAttr ARGS1(
	int,		a)
{
    Current_Attr &= ~a;
    SLsmg_set_color(Current_Attr);
}

PUBLIC void lynx_setup_colors NOARGS
{
    SLtt_set_color(0, NULL, DEFAULT_FG, DEFAULT_BG);
    SLtt_set_color(1, NULL, "blue",	DEFAULT_BG); /* bold */
    SLtt_set_color(2, NULL, "yellow",	"blue");     /* reverse */
    SLtt_set_color(4, NULL, "magenta",	DEFAULT_BG); /* underline */
    /*
     *	The other objects are '|'ed together to get rest.
     */
    SLtt_set_color(3, NULL, "green",	DEFAULT_BG); /* bold-reverse */
    SLtt_set_color(5, NULL, "blue",	DEFAULT_BG); /* bold-underline */
    SLtt_set_color(6, NULL, "red",	DEFAULT_BG); /* reverse-underline */
    SLtt_set_color(7, NULL, "magenta",	"cyan");     /* reverse-underline-bold */

    /*
     *	Now set monochrome attributes.
     */
    SLtt_set_mono(1, NULL, SLTT_BOLD_MASK);
    SLtt_set_mono(2, NULL, SLTT_REV_MASK);
    SLtt_set_mono(3, NULL, SLTT_REV_MASK | SLTT_BOLD_MASK);
    SLtt_set_mono(4, NULL, SLTT_ULINE_MASK);
    SLtt_set_mono(5, NULL, SLTT_ULINE_MASK | SLTT_BOLD_MASK);
    SLtt_set_mono(6, NULL, SLTT_ULINE_MASK | SLTT_REV_MASK);
    SLtt_set_mono(7, NULL, SLTT_ULINE_MASK | SLTT_BOLD_MASK | SLTT_REV_MASK);
}

PRIVATE void sl_suspend ARGS1(
	int,		sig)
{
#ifdef SIGSTOP
#ifndef VMS
    int r, c;

    lynx_enable_mouse (0);
    if (sig == SIGTSTP)
	SLsmg_suspend_smg();
    SLang_reset_tty();
    kill(getpid(),SIGSTOP);
#if SLANG_VERSION > 9929
    SLang_init_tty(-1, 0, 1);
#else
    SLang_init_tty(3, 0, 1);
#endif /* SLANG_VERSION > 9929 */
    signal(SIGTSTP, sl_suspend);
#if !defined(_WINDOWS) && !defined(__DJGPP__)
    SLtty_set_suspend_state(1);
#endif
    if (sig == SIGTSTP)
	SLsmg_resume_smg();
    /*
     *	Get new window size in case it changed.
     */
    r = SLtt_Screen_Rows;
    c = SLtt_Screen_Cols;
    size_change(0);
    if ((r != SLtt_Screen_Rows) || (c != SLtt_Screen_Cols)) {
	recent_sizechange = TRUE;
    }
    lynx_enable_mouse (1);
#endif /* !VMS */
#endif /* SIGSTOP */
   return;
}

#else  /* Not slang: */

#ifdef VMS
/*
**  This function boxes windows with graphic characters for
**  VMS curses.  Pass it the window, it's height, and it's
**  width. - FM
*/
PUBLIC void VMSbox ARGS3(
	WINDOW *,	win,
	int,		height,
	int,		width)
{
    int i;

    wmove(win, 0, 0);
    waddstr(win, "\033)0\016l");
    for (i = 1; i < width; i++)
       waddch(win, 'q');
    waddch(win, 'k');
    for (i = 1; i < height-1; i++) {
	wmove(win, i, 0);
	waddch(win, 'x');
	wmove(win, i, width-1);
	waddch(win, 'x');
    }
    wmove(win, i, 0);
    waddch(win, 'm');
    for (i = 1; i < width; i++)
       waddch(win, 'q');
    waddstr(win, "j\017");
}
#else
/*
**  This function boxes windows for non-VMS (n)curses.
**  Pass it the window. - FM
*/
PUBLIC void LYbox ARGS2(
	WINDOW *,	win,
	BOOLEAN,	formfield GCC_UNUSED)
{
    /*
     *	If the terminal is in UTF-8 mode, it probably cannot understand
     *	box drawing characters as (n)curses handles them.  (This may also
     *	be true for other display character sets, but isn't currently
     *	checked.)  In that case, substitute ASCII characters for BOXVERT
     *	and BOXHORI if they were defined to 0 for automatic use of box
     *	drawing characters.  They'll stay as they are otherwise. - KW & FM
     */
    int boxvert, boxhori;

    UCSetBoxChars(current_char_set, &boxvert, &boxhori, BOXVERT, BOXHORI);
#ifdef CSS
    if (formfield)
	wcurses_css(win, "frame", ABS_ON);
#endif
    /*
     *	If we don't have explicitly specified characters for either
     *	vertical or horizontal lines, the characters that box() would
     *	use for the corners probably also won't work well.  So we
     *	specify our own ASCII characters for the corners and call
     *	wborder() instead of box(). - kw
     */
#ifdef HAVE_WBORDER
    if (!boxvert || !boxhori)
	box(win, boxvert, boxhori);
    else if (boxvert == '*' || boxhori == '*')
	wborder(win, boxvert, boxvert, boxhori, boxhori, '*', '*', '*', '*');
    else
	wborder(win, boxvert, boxvert, boxhori, boxhori, '/', '\\', '\\', '/');
#else
    box(win, boxvert, boxhori);
#endif
#ifdef CSS
    if (formfield)
	wcurses_css(win, "frame", ABS_OFF);
#endif
}
#endif /* VMS */
#endif /* USE_SLANG */

#if defined(USE_COLOR_STYLE)
/* Ok, explanation of the USE_COLOR_STYLE styles.  The basic styles (ie non
 * HTML) are set the same as the SLANG version for ease of programming.  The
 * other styles are simply the HTML enum from HTMLDTD.h + 16.
 */
PUBLIC HTCharStyle displayStyles[DSTYLE_ELEMENTS];

/*
 * set a style's attributes - RP
 */
PUBLIC void setStyle ARGS4(int,style,int,color,int,cattr,int,mono)
{
	displayStyles[style].color=color;
	displayStyles[style].cattr=cattr;
	displayStyles[style].mono=mono;
}

PUBLIC void setHashStyle ARGS5(int,style,int,color,int,cattr,int,mono,char*,element)
{
    bucket* ds=&hashStyles[style];
    CTRACE(tfp, "CSS(SET): <%s> hash=%d, ca=%d, ma=%d\n", element, style, color, mono);
    ds->color=color;
    ds->cattr=cattr;
    ds->mono=mono;
    ds->code=style;
    FREE(ds->name);
    ds->name=malloc(sizeof(char)*(strlen(element)+2));
    if(!ds->name)
	outofmem(__FILE__, "setHashStyle");
    strcpy(ds->name, element);
}

/*
 * set the curses attributes to be color or mono - RP
 */
PRIVATE int LYAttrset ARGS3(WINDOW*,win,int,color,int,mono)
{
	CTRACE(tfp, "CSS:LYAttrset (%#x, %#x)\n", color, mono);
	if (lynx_has_color && LYShowColor >= SHOW_COLOR_ON && color > -1)
	{
		wattrset(win,color);
		return color;
	}
	if (mono > -1)
	{
		wattrset(win,mono);
		return mono;
	}
	wattrset(win,A_NORMAL);
	return A_NORMAL;
}

PUBLIC void curses_w_style ARGS3(
	WINDOW*,	win,
	int,		style,
	int,		dir)
{
#if OMIT_SCN_KEEPING
# define SPECIAL_STYLE /*(CSHASHSIZE+1) */ 88888
  /* if TRACEs are not compiled in, this macro is redundant - we neend't valid
    'ds' to stack off. */
#endif

	int YP,XP;
#if !OMIT_SCN_KEEPING
	bucket* ds= (style == NOSTYLE ? &nostyle_bucket : &hashStyles[style]);
#else
        bucket* ds= (style == NOSTYLE ?      &nostyle_bucket :
                (style== SPECIAL_STYLE ? &special_bucket :&hashStyles[style]) );
#endif


	if (!ds->name)
	{
		CTRACE(tfp, "CSS.CS:Style %d not configured\n",style);
#if !OMIT_SCN_KEEPING
		return;
#endif
	}

	CTRACE(tfp, "CSS.CS:<%s%s> (%d)\n",(dir?"":"/"),ds->name,ds->code);

	getyx (win, YP, XP);

	if (style == s_normal && dir)
	{
		wattrset(win,A_NORMAL);
		if (win==stdscr) cached_styles[YP][XP]=s_normal;
		return;
	}

	switch (dir)
	{
		/* ABS_OFF is the same as STACK_OFF for the moment */
	case STACK_OFF:
		if (last_colorattr_ptr) {
		    int last_attr = last_styles[--last_colorattr_ptr];
		    LYAttrset(win,last_attr,last_attr);
		}
		else
			LYAttrset(win,A_NORMAL,-1);
		return;

	case STACK_ON: /* remember the current attributes */
		if (last_colorattr_ptr > 127) {
		    CTRACE(tfp,"........... %s (0x%x) %s\r\n",
				"attribute cache FULL, dropping last",
				last_styles[last_colorattr_ptr],
				"in LynxChangeStyle(curses_w_style)");
		    last_colorattr_ptr--;
		}
		last_styles[last_colorattr_ptr++] = getattrs(stdscr);
		/* don't cache style changes for active links */
#if OMIT_SCN_KEEPING
                /* since we don't compute the hcode
                  to stack off in HTML.c, we don't know whether this style is
                  configured. So, we shouldn't simply return on stacking on on
                  unconfigured styles, we should push curr attrs on stack. -HV
                */
                if (!ds->name) return;
#endif
		if (style != s_alink)
		{
			CTRACE(tfp, "CACHED: <%s> @(%d,%d)\n", ds->name, YP, XP);
			if (win==stdscr) cached_styles[YP][XP]=style;
		}
		LYAttrset(win, ds->color, ds->mono);
		return;

	case ABS_ON: /* change without remembering the previous style */
		/* don't cache style changes for active links */
		if (style != s_alink)
		{
			CTRACE(tfp, "CACHED: <%s> @(%d,%d)\n", ds->name, YP, XP);
			if (win==stdscr) cached_styles[YP][XP]=style;
		}
                LYAttrset(win, ds->color, ds->mono);
		return;
	}
}

/*
 * wrapper function to set on-screen styles - RP
 */
PUBLIC void wcurses_css ARGS3(WINDOW *,win,char*,name,int,dir)
{
	int try_again=1;
	while (try_again)
	{
		int tmpHash=hash_code(name);
		CTRACE(tfp, "CSSTRIM:trying to set [%s] style - ", name);
		if (tmpHash==NOSTYLE) {
			char *class=strrchr(name, '.');
			CTRACE(tfp, "undefined, trimming at %p\n", class);
			if (class)	*class='\0';
			else		try_again=0;
		} else {
			CTRACE(tfp, "ok (%d)\n", hash_code(name));
			curses_w_style(win, hash_code(name), dir);
			try_again=0;
		}
	}
}

PUBLIC void curses_css ARGS2(char *,name,int,dir)
{
    wcurses_css(stdscr, name, dir);
}

PUBLIC void curses_style ARGS2(
	int,	style,
	int,	dir)
{
    curses_w_style(stdscr, style, dir);
}

#ifdef NOT_USED
void attribute ARGS2(int,style,int,dir)
{
    curses_style(style, dir, 0);
}
#endif
#endif /* USE_COLOR_STYLE */

#if USE_COLOR_TABLE && defined(COLOR_CURSES)
/*
 * This block of code is designed to produce the same color effects using SVr4
 * curses as the slang library's implementation in this module.  That maps the
 * SGR codes into a 0-7 index into the color table, with special treatment for
 * backgrounds.  There's a bit of convoluted (but necessary) code handling the
 * special case of initialization before 'initscr()' is called.
 * 1997/1/19 - T.E.Dickey <dickey@clark.net>
 */
PRIVATE int lynx_called_initscr;

PRIVATE struct {
    int fg, bg;
    chtype attr;
} lynx_color_cfg[] = {
    /*0*/ { DEFAULT_FG,    DEFAULT_BG,	A_NORMAL}, /* A_NORMAL */
    /*1*/ { COLOR_BLUE,    DEFAULT_BG,	A_NORMAL}, /* A_BOLD */
    /*2*/ { COLOR_YELLOW,  COLOR_BLUE,	A_BOLD},   /* A_REVERSE */
    /*3*/ { COLOR_GREEN,   DEFAULT_BG,	A_NORMAL}, /* A_REVERSE | A_BOLD */
    /*4*/ { COLOR_MAGENTA, DEFAULT_BG,	A_NORMAL}, /* A_UNDERLINE */
    /*5*/ { COLOR_BLUE,    DEFAULT_BG,	A_NORMAL}, /* A_UNDERLINE | A_BOLD */
    /*6*/ { COLOR_RED,	   DEFAULT_BG,	A_NORMAL}, /* A_UNDERLINE | A_REVERSE */
    /*7*/ { COLOR_MAGENTA, COLOR_CYAN,	A_NORMAL}  /* A_UNDERLINE | A_BOLD | A_REVERSE */
};

/*
 * Hold the codes for color-pairs here until 'initscr()' is called.
 */
PRIVATE struct {
    int fg;
    int bg;
} lynx_color_pairs[25];

/*
 * Map the SGR attributes (0-7) into ANSI colors, modified with the actual BOLD
 * attribute we'll get 16 colors.
 */
PRIVATE void LYsetWAttr ARGS1(WINDOW *, win)
{
    if (lynx_has_color && LYShowColor >= SHOW_COLOR_ON) {
	int code = 0;
	int attr = A_NORMAL;
	int offs = 1;
	static int NoColorVideo = -1;

#ifdef UNIX
	if (NoColorVideo < 0) {
		NoColorVideo = tigetnum("ncv");
	}
	if (NoColorVideo < 0)
		NoColorVideo = 0;
#endif /* UNIX */

	if (Current_Attr & A_BOLD)
		code |= 1;
	if (Current_Attr & A_REVERSE)
		code |= 2;
	if (Current_Attr & A_UNDERLINE)
		code |= 4;
	attr = lynx_color_cfg[code].attr;

	/*
	 * no_color_video isn't implemented (97/4/14) in ncurses 4.1, but may
	 * be in SVr4 (which would make this redundant for the latter).
	 */
	if ((Current_Attr & A_BOLD) && !(NoColorVideo & 33)) {
		attr |= A_BOLD;
	}

	if ((Current_Attr == A_UNDERLINE) && !(NoColorVideo & 2)) {
		attr |= A_UNDERLINE;
	}

	if (code+offs < COLOR_PAIRS) {
		attr |= COLOR_PAIR(code+offs);
	}

	wattrset(win, attr);
    } else {
	wattrset(win, Current_Attr);
    }
}

PRIVATE void lynx_map_color ARGS1(int, n)
{
    int m;

    lynx_color_pairs[n+1].fg = lynx_color_cfg[n].fg;
    lynx_color_pairs[n+1].bg = lynx_color_cfg[n].bg;

    lynx_color_pairs[n+9].fg = lynx_color_cfg[n].fg;
    lynx_color_pairs[n+9].bg = lynx_color_cfg[0].bg;

    lynx_color_pairs[n+17].fg = lynx_color_cfg[n].bg;
    lynx_color_pairs[n+17].bg = lynx_color_cfg[n].bg;

    if (lynx_called_initscr) {
	for (m = 0; m <= 16; m += 8) {
	    int pair = n + m + 1;
	    if (pair < COLOR_PAIRS)
		init_pair(pair,
		    lynx_color_pairs[pair].fg,
		    lynx_color_pairs[pair].bg);
	}
	if (n == 0 && LYShowColor >= SHOW_COLOR_ON)
	    bkgd(COLOR_BKGD | ' ');
    }
}

PUBLIC int lynx_chg_color ARGS3(
	int, color,
	int, fg,
	int, bg
	)
{
    if (fg == ERR_COLOR || bg == ERR_COLOR) return -1;
    if (color >= 0 && color < 8) {
	lynx_color_cfg[color].fg = (fg > 7) ? (fg & 7) : fg;
	lynx_color_cfg[color].bg = (bg > 7) ? (bg & 7) : bg;
	lynx_color_cfg[color].attr = ((fg > 7) && (fg & 8)) ? A_BOLD : A_NORMAL;
	lynx_map_color(color);
    } else {
	return -1;
    }
    return 0;
}

PUBLIC void lynx_set_color ARGS1(int, a)
{
    if (lynx_has_color && LYShowColor >= SHOW_COLOR_ON) {
	attrset(lynx_color_cfg[a].attr
		| (((a+1) < COLOR_PAIRS)
			? COLOR_PAIR(a+1)
			: A_NORMAL));
    }
}

PUBLIC void lynx_standout ARGS1(int, flag)
{
    if (flag)
	LYaddAttr(A_REVERSE);
    else
	LYsubAttr(A_REVERSE);
}

PRIVATE void lynx_init_colors NOARGS
{
    if (lynx_has_color) {
	size_t n, m;

	lynx_color_cfg[0].fg = default_fg;
	lynx_color_cfg[0].bg = default_bg;

	for (n = 0; n < sizeof(lynx_color_cfg)/sizeof(lynx_color_cfg[0]); n++) {
	    for (m = 0; m <= 16; m += 8) {
		int pair = n + m + 1;
		if (pair < COLOR_PAIRS)
		    init_pair(pair,
			lynx_color_pairs[pair].fg,
			lynx_color_pairs[pair].bg);
	    }
	    if (n == 0 && LYShowColor >= SHOW_COLOR_ON)
		bkgd(COLOR_BKGD | ' ');
	}
    } else if (LYShowColor != SHOW_COLOR_NEVER) {
	LYShowColor = SHOW_COLOR_OFF;
    }
}

PUBLIC void lynx_setup_colors NOARGS
{
    int n;
    for (n = 0; n < 8; n++)
	lynx_map_color(n);
}
#endif /* USE_COLOR_TABLE */

#ifdef NOTUSED
#if defined (DJGPP) && !defined (USE_SLANG)
/*
 * Sorry about making a completely new function,
 * but the real one is messy! WB
 */
PUBLIC void start_curses NOARGS
{
    static BOOLEAN first_time = TRUE;

    if(first_time)
    {
	initscr();		/* start curses */
	first_time = FALSE;
	cbreak();
	keypad(stdscr, TRUE);
	fflush(stdin);
	fflush(stdout);
	if (has_colors()) {
	    lynx_has_color = TRUE;
	    start_color();
	}
	lynx_init_colors();
	lynx_called_initscr = TRUE;

	/* Inform pdcurses that we're interested in knowing when mouse buttons
	 * are clicked.  Maybe someday pdcurses will support it.
	 */
	if (LYUseMouse)
	    lynx_enable_mouse (1);

    } else
	sock_init();

    LYCursesON = TRUE;
    clear();
    noecho();
}
#else
#endif /* defined (DJGPP) && !defined (USE_SLANG) */
#endif /* NOTUSED */


PUBLIC void start_curses NOARGS
{
#ifdef USE_SLANG
    static int slinit;

    if (LYCursesON)
	return;

    if (slinit == 0) {
	SLtt_get_terminfo();
#if defined(__DJGPP__) && !defined(DJGPP_KEYHANDLER)
	SLkp_init ();
#endif /* __DJGPP__ && !DJGPP_KEYHANDLER */

#ifdef UNIX
#if SLANG_VERSION >= 9935
	SLang_TT_Read_FD = fileno(stdin);
#endif /* SLANG_VERSION >= 9935 */
#endif /* UNIX */

	/*
	 *  Check whether a saved show_color:off override is in effect. - kw
	 */
	if (LYrcShowColor == SHOW_COLOR_NEVER) {
	    SLtt_Use_Ansi_Colors = 0;
	}
	/*
	 *  Check whether we're forcing color on. - FM
	 */
	if ((LYShowColor > 1) && (Lynx_Color_Flags & SL_LYNX_USE_COLOR))
	    SLtt_Use_Ansi_Colors = 1;
	/*
	 *  Check whether a -nocolor override is in effect. - kw
	 */
	if (Lynx_Color_Flags & SL_LYNX_OVERRIDE_COLOR)
	    SLtt_Use_Ansi_Colors = 0;
	/*
	 *  Make sure our flags are in register. - FM
	 */
	if (SLtt_Use_Ansi_Colors == 1) {
	    if (LYShowColor != SHOW_COLOR_ALWAYS) {
		LYShowColor = SHOW_COLOR_ON;
	    }
	} else {
	    if (LYShowColor != SHOW_COLOR_NEVER) {
		LYShowColor = SHOW_COLOR_OFF;
	    }
	}
	size_change(0);

#if defined(VMS) || defined(UNIX)
	SLtt_add_color_attribute(4, SLTT_ULINE_MASK);
	SLtt_add_color_attribute(5, SLTT_ULINE_MASK);
	/*
	 *  If set, the blink escape sequence will turn on high
	 *  intensity background (rxvt and maybe Linux console).
	 */
	if (LYShowColor && (Lynx_Color_Flags & SL_LYNX_USE_BLINK)) {
	    SLtt_Blink_Mode = 1;
	} else {
	    SLtt_Blink_Mode = 0;
	}
#endif /* VMS || UNIX */
    }
#ifdef __DJGPP__
    else sock_init();
#endif /* __DJGPP__ */

    slinit = 1;
    Current_Attr = 0;
#ifndef VMS
#if SLANG_VERSION > 9929
    SLang_init_tty(-1, 0, 1);
#else
    SLang_init_tty(3, 0, 1);
#endif /* SLANG_VERSION > 9929 */
#endif /* !VMS */
    SLsmg_init_smg();
    SLsmg_Display_Eight_Bit = LYlowest_eightbit[current_char_set];
    if (SLsmg_Display_Eight_Bit > 191)
	SLsmg_Display_Eight_Bit = 191; /* may print ctrl chars otherwise - kw */
    scrollok(0,0);
    SLsmg_Backspace_Moves = 1;
#ifndef VMS
#if !defined(_WINDOWS) && !defined(__DJGPP__)
    SLtty_set_suspend_state(1);
#endif /* !_WINDOWS */
#ifdef SIGTSTP
    if (!no_suspend)
	signal(SIGTSTP, sl_suspend);
#endif /* SIGTSTP */
    signal(SIGINT, cleanup_sig);
#endif /* !VMS */

   lynx_enable_mouse (1);

#else /* Using curses: */


#ifdef VMS
    /*
     *	If we are VMS then do initscr() everytime start_curses()
     *	is called!
     */
    initscr();	/* start curses */
#else  /* Unix: */

    static BOOLEAN first_time = TRUE;

    if (first_time) {
	/*
	 *  If we're not VMS then only do initscr() one time,
	 *  and one time only!
	 */
	if (initscr() == NULL) {  /* start curses */
	    fprintf(tfp, "%s\n",
		gettext("Terminal initialisation failed - unknown terminal type?"));
	    exit_immediately (-1);
	}
#if defined(SIGWINCH) && defined(NCURSES_VERSION)
	size_change(0);
	recent_sizechange = FALSE; /* prevent mainloop drawing 1st doc twice */
#endif /* SIGWINCH */
#if defined(USE_KEYMAPS) && defined(NCURSES_VERSION)
	if (-1 == lynx_initialize_keymaps ())
	    exit (-1);
#endif

	/*
	 * This is a workaround for a bug in SVr4 curses, observed on Solaris
	 * 2.4:  if your terminal's alternate-character set contains codes in
	 * the range 128-255, they'll be sign-extended in the acs_map[] table,
	 * which in turn causes their values to be emitted as 255 (0xff).
	 * "Fix" this by forcing the table to 8-bit codes (it has to be
	 * anyway).
	 */
#if defined(ALT_CHAR_SET) && !defined(NCURSES_VERSION)
	{
	    int n;
	    for (n = 0; n < 128; n++)
		if (ALT_CHAR_SET[n] & 0x80) {
		    ALT_CHAR_SET[n] &= 0xff;
		    ALT_CHAR_SET[n] |= A_ALTCHARSET;
		}
	}
#endif

#if defined(USE_COLOR_STYLE) || defined(USE_COLOR_TABLE)
	if (has_colors()) {
	    lynx_has_color = TRUE;
	    start_color();
#if HAVE_USE_DEFAULT_COLORS
	    if (use_default_colors() == OK) {
		default_fg = DEFAULT_COLOR;
		default_bg = DEFAULT_COLOR;
	    }
#endif /* HAVE_USE_DEFAULT_COLORS */
	}
#endif /* USE_COLOR_STYLE || USE_COLOR_TABLE */

#ifdef USE_COLOR_STYLE
	parse_userstyles();
#endif
	first_time = FALSE;
#if USE_COLOR_TABLE
	lynx_init_colors();
	lynx_called_initscr = TRUE;
#endif /* USE_COLOR_TABLE */
    }
#ifdef __DJGPP__
    else sock_init();
#endif /* __DJGPP__ */
#endif /* not VMS */

    /* nonl();	 */ /* seems to slow things down */

#ifdef VMS
    crmode();
    raw();
#else
#if HAVE_CBREAK
    cbreak();
#else
    crmode();
#endif /* HAVE_CBREAK */
    signal(SIGINT, cleanup_sig);
#endif /* VMS */

    noecho();

#if HAVE_KEYPAD
    keypad(stdscr,TRUE);
#endif /* HAVE_KEYPAD */

   lynx_enable_mouse (1);

    fflush(stdin);
    fflush(stdout);
    fflush(stderr);
#endif /* USE_SLANG */

#ifdef _WINDOWS
    clear();
#endif

    LYCursesON = TRUE;
}


PUBLIC void lynx_enable_mouse ARGS1(int,state)
{

#ifdef __BORLANDC__
/* modify lynx_enable_mouse() for pdcurses configuration so that mouse support
   is disabled unless -use_mouse is specified.  This is ifdef'd with
   __BORLANDC__ for the time being (WB).
*/
    HANDLE hConIn = INVALID_HANDLE_VALUE;
    hConIn = GetStdHandle(STD_INPUT_HANDLE);
    if (LYUseMouse == 0)
    {
	SetConsoleMode(hConIn, ENABLE_WINDOW_INPUT);
	FlushConsoleInputBuffer(hConIn);
	return;
    }
#endif

    if (LYUseMouse == 0)
	return;

#ifdef USE_SLANG_MOUSE
    SLtt_set_mouse_mode (state, 0);
    SLtt_flush_output ();
#else

#ifdef NCURSES_MOUSE_VERSION
#if defined(__BORLANDC__) && defined(__PDCURSES__)
    if (state)
    {
	SetConsoleMode(hConIn, ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
	FlushConsoleInputBuffer(hConIn);
    }
#else
    /* Inform ncurses that we're interested in knowing when mouse
     * button 1 is clicked.  We cannot just specify
     * BUTTON1_CLICKED | BUTTON3_CLICKED, since ncurses will try hard
     * to translate other events to single-clicks.
     * Compensate for small value of maxclick in ncurses.  */
    if (state) {
	static int was = 0;

	if (!was) {
	    int old = mouseinterval(-1);

	    was++;
	    if (old < 200)		/* Default 166 */
		mouseinterval(300);
	}
	mousemask(ALL_MOUSE_EVENTS, NULL);
    } else
	mousemask(0, NULL);
#endif /* __BORLANDC__ and __PDCURSES__ */
#endif /* NCURSES_MOUSE_VERSION */

#if defined(DJGPP) && !defined(USE_SLANG)
    if (state)
	mouse_set(BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED);
#endif
#endif				       /* NOT USE_SLANG_MOUSE */
}

PUBLIC void stop_curses NOARGS
{
    echo();
#ifdef DJGPP
    sock_exit();
#endif
#if defined (DOSPATH) && !defined(USE_SLANG)
    clrscr();
#else

    /*
     *	Fixed for better dumb terminal support.
     *	05-28-94 Lynx 2-3-1 Garrett Arch Blythe
     */
    if(LYCursesON == TRUE)	{
	 lynx_enable_mouse (0);
	 endwin();	/* stop curses */
    }

    fflush(stdout);
#endif /* DJGPP */
    fflush(stderr);

    LYCursesON = FALSE;

#if defined(SIGTSTP) && defined(USE_SLANG)
#ifndef VMS
    if (!no_suspend)
	signal(SIGTSTP, SIG_DFL);
#endif /* !VMS */
#endif /* SIGTSTP && USE_SLANG */

#ifndef VMS
    signal(SIGINT, SIG_DFL);
#endif /* !VMS */
}

#ifdef VMS
/*
 *  Check terminal type, start curses & setup terminal.
 */
PUBLIC BOOLEAN setup ARGS1(
	char *, 	terminal)
{
    int c;
    int status;
    char *dummy = 0, *cp, term[81];
#ifdef USE_SLANG
    extern void longname();
#endif /* USE_SLANG */

    /*
     *	If the display was not set by a command line option then
     *	see if it is available from the environment.
     */
    if ((cp = LYgetXDisplay()) != 0) {
	StrAllocCopy(x_display, cp);
    } else {
	FREE(x_display);
    }

    /*
     *	Get terminal type, and convert to lower case.
     */
    term[0] = '\0';
    longname(dummy, term);
    if (term[0] == '\0' && (form_get_data || form_post_data)) {
	/*
	 *  Some yoyo used these under conditions which require
	 *  -dump, so force that mode here. - FM
	 */
	dump_output_immediately = TRUE;
	LYcols = 80;
	if (keypad_mode == NUMBERS_AS_ARROWS)
	    keypad_mode = LINKS_ARE_NUMBERED;
	status = mainloop();
	(void) signal (SIGHUP, SIG_DFL);
	(void) signal (SIGTERM, SIG_DFL);
#ifdef SIGTSTP
	if (no_suspend)
	  (void) signal(SIGTSTP,SIG_DFL);
#endif /* SIGTSTP */
	exit(status);
    }
    LYLowerCase(term);

    printf("%s%s\n", gettext("Terminal ="), term);
    sleep(InfoSecs);
    if ((strlen(term) < 5) ||
	strncmp(term, "vt", 2) || !isdigit(term[2])) {
	printf("%s\n",
	    gettext("You must use a vt100, 200, etc. terminal with this program."));
	printf(CONFIRM_PROCEED, "n/y");
	c = getchar();
	if (c != 'y' && c != 'Y') {
	    printf("\n");
	    return(FALSE);
	}
	strcpy(term,"vt100");
    }

    ttopen();
    start_curses();

    LYlines = LINES;
    LYcols = COLS;
    if (LYlines <= 0)
	LYlines = 24;
    if (LYcols <= 0)
	LYcols = 80;

    return(TRUE);
}

#else	/* Not VMS: */

/*
 *  Check terminal type, start curses & setup terminal.
 */
PUBLIC BOOLEAN setup ARGS1(
	char *, 	terminal)
{
    char *term_putenv = NULL;
    char *buffer = NULL;
    char *cp;
#if defined(HAVE_SIZECHANGE) && !defined(USE_SLANG) && defined(NOTDEFINED)
/*
 *  Hack to deal with a problem in sysV curses, that screen can't be
 *  resized to greater than the size used by initscr, which can only
 *  be called once.  So set environment variables LINES and COLUMNS
 *  to some suitably large size to force initscr to allocate enough
 *  space.  Later we get the real window size for setting LYlines
 *  and LYcols. - AJL & FM
 *
 *  Has problems, so we don't use this hack, but the code is here
 *  if someone wants to play with it some more. - FM
 */
    char *lines_putenv = NULL;
    char *cols_putenv = NULL;

    if (getenv("LINES") == NULL && getenv("COLUMNS") == NULL) {
	StrAllocCopy(lines_putenv, "LINES=120");
	(void) putenv(lines_putenv);
	StrAllocCopy(cols_putenv, "COLUMNS=240");
	(void) putenv(cols_putenv);
    }
#endif /* HAVE_SIZECHANGE && !USE_SLANG && NOTDEFINED */

   /*
    *  If the display was not set by a command line option then
    *  see if it is available from the environment .
    */
    if ((cp = LYgetXDisplay()) != NULL) {
	StrAllocCopy(x_display, cp);
    } else {
	FREE(x_display);
    }

    if (terminal != NULL) {
	HTSprintf0(&term_putenv, "TERM=%.106s", terminal);
	(void) putenv(term_putenv);
    }

    /*
     *	Query the terminal type.
     */
    if (dumbterm(getenv("TERM"))) {
	char *s;

	printf("\n\n  %s\n\n", gettext("Your Terminal type is unknown!"));
	printf("  %s [vt100] ", gettext("Enter a terminal type:"));

	if (LYSafeGets(&buffer, stdin) != 0)
	    if ((s = strchr(buffer, '\n')) != NULL)
		*s = '\0';

	if (buffer == 0 || *buffer == 0)
	    StrAllocCopy(buffer,"vt100");

	HTSprintf0(&term_putenv,"TERM=%.106s", buffer);
	FREE(buffer);

	(void) putenv(term_putenv);
	printf("\n%s %s\n", gettext("TERMINAL TYPE IS SET TO"), getenv("TERM"));
	sleep(MESSAGESECS);
    }

    start_curses();

#if HAVE_TTYTYPE
    /*
     *	Get terminal type (strip 'dec-' from vms style types).
     */
    if (strncmp((CONST char*)ttytype, "dec-vt", 6) == 0) {
	(void) setterm(ttytype + 4);
    }
#endif /* HAVE_TTYTYPE */

#if defined(HAVE_SIZECHANGE) && !defined(USE_SLANG) && defined(NOTDEFINED)
    if (lines_putenv != NULL) {
	/*
	 *  Use SIGWINCH handler to set the true window size. - AJL && FM
	 *
	 *  Has problems, so we don't use this hack, but the code is here
	 *  if someone wants to play with it some more. - FM
	 */
	size_change(0);
	lines_putenv[6] = '\0';
	(void) putenv(lines_putenv);
	cols_putenv[8] = '\0';
	(void) putenv(cols_putenv);
	FREE(lines_putenv);
	FREE(cols_putenv);
    } else {
	LYlines = LINES;
	LYcols = COLS;
    }
#else
    LYlines = LINES;
    LYcols = COLS;
#endif /* HAVE_SIZECHANGE && !USE_SLANG && USE_NOTDEFINED */
    if (LYlines <= 0)
	LYlines = 24;
    if (LYcols <= 0)
	LYcols = 80;

    return(1);
}

PRIVATE int dumbterm ARGS1(
	char *, 	terminal)
{
    int dumb = FALSE;

    /*
     *	Began checking for terminal == NULL in case that TERM environment
     *	variable is not set.  Thanks to Dick Wesseling (ftu@fi.ruu.nl).
     */
    if (terminal == NULL ||
	!strcasecomp(terminal, "network") ||
	!strcasecomp(terminal, "unknown") ||
	!strcasecomp(terminal, "dialup")  ||
	!strcasecomp(terminal, "dumb")	  ||
	!strcasecomp(terminal, "switch")  ||
	!strcasecomp(terminal, "ethernet")  )
	dumb = TRUE;
    return(dumb);
}

#ifdef FANCY_CURSES
#ifndef USE_COLOR_STYLE
#if USE_COLOR_TABLE
PUBLIC void LYaddWAttr ARGS2(
	WINDOW *,	win,
	int,		a)
{
    Current_Attr |= a;
    LYsetWAttr(win);
}

PUBLIC void LYaddAttr ARGS1(
	int,		a)
{
    LYaddWAttr(stdscr, a);
}

PUBLIC void LYsubWAttr ARGS2(
	WINDOW *,	win,
	int,		a)
{
    Current_Attr &= ~a;
    LYsetWAttr(win);
}

PUBLIC void LYsubAttr ARGS1(
	int,		a)
{
    LYsubWAttr(stdscr, a);
}
#endif /* USE_COLOR_TABLE */
#endif /* !USE_COLOR_STYLE */
#endif /* FANCY_CURSES */
#endif /* VMS */

PUBLIC void LYstartTargetEmphasis NOARGS
{
#ifdef USE_COLOR_STYLE
    if (s_whereis != NOSTYLE) {
	curses_style(s_whereis, STACK_ON);
	return;
    }
#endif
#if defined(FANCY_CURSES) || defined(USE_SLANG)
    start_bold();
    start_reverse();
#endif /* FANCY_CURSES || USE_SLANG */
    start_underline();
}

PUBLIC void LYstopTargetEmphasis NOARGS
{
#ifdef USE_COLOR_STYLE
    if (s_whereis != NOSTYLE) {
	curses_style(s_whereis, STACK_OFF);
	return;
    }
#endif
    stop_underline();
#if defined(FANCY_CURSES) || defined(USE_SLANG)
    stop_reverse();
    stop_bold();
#endif /* FANCY_CURSES || USE_SLANG */
}

#ifdef VMS
/*
 *	Cut-down termio --
 *		Do character-oriented stream input for Jeff.
 *		Code ripped off from Micro-Emacs 3.7 by Daniel Lawrence.
 *
 *		Ever-so-slightly modified by Kathryn Huxtable.	29-Jan-1991.
 *		Cut down for Lou.  8 Sep 1992.
 *		Cut down farther for Lou.  19 Apr 1993.
 *			We don't set PASSALL or PASTHRU since we don't
 *			want to block CTRL/C, CTRL/Y, CTRL/S or CTRL/Q.
 *			Simply setting NOECHO and doing timed reads
 *			is sufficient.
 *		Further mods by Fote.  29-June-1993
 *			ttopen() and ttclose() are now terminal initialization
 *			 and restoration procedures, called once at startup
 *			 and at exit, respectively, of the LYNX image.
 *			ttclose() should be called before an exit from LYNX
 *			 no matter how the exit is invoked.
 *			setup(terminal) does the ttopen().
 *			cleanup() calls cleanup_files() and ttclose().
 *			ttgetc() now handles NOECHO and NOFLITR (instead of
 *			 setting the terminal itself to NOECHO in ttopen()).
 *			VMSsignal() added for handling both Ctrl-C *and* Ctrl-Y
 *			 interrupts, and disabling system response to Ctrl-T.
 *		Further mods by Fote.  15-Dec-1993
 *			Added edit handler in ttopen() which will invoke
 *			 VMSexit() and behave intelligently on ACCVIO's.
 *		Further mods by Fote.  29-Dec-1993
 *			Simplified ttgetc().
 *		Further mods by Fote.  16-Jan-1994
 *			Added code in ttopen() which will invoke VMSVersion()
 *			 to get the version of VMS as VersionVMS for use by
 *			 by new or modified interrupt or spawning routines.
 *		Further mods by Fote.  27-Jan-1994
 *			Added back a typeahead() which supports 'z' or 'Z' as
 *			an "Zap transfer" command via HTCheckForInterrupt()
 *			in LYUtils.c.
 */

#include <descrip.h>
#include <iodef.h>
#include <ssdef.h>
#include <msgdef.h>
#include <ttdef.h>
#include <tt2def.h>
#include <libclidef.h>
#include <lib$routines.h>
#include <starlet.h>
#include <clidef.h>
#include <syidef.h>
#ifdef signal
#undef signal
#endif /* signal */
#include <signal.h>
#ifdef system
#undef system
#endif /* system */
#include <processes.h>
#include <LYVMSdef.h>

#define EFN	0			/* Event flag			*/

static	unsigned char buffer[20];	/* Input buffer 		*/
static	int	in_pos, in_len; 	/* For escape sequences 	*/
static	int	oldmode[3];		/* Old TTY mode bits		*/
static	int	newmode[3];		/* New TTY mode bits		*/
static	short	iochan; 		/* TTY I/O channel		*/
static	$DESCRIPTOR(term_nam_dsc,"TT"); /* Descriptor for iochan	*/
static	unsigned long mask = LIB$M_CLI_CTRLY|LIB$M_CLI_CTRLT; /* ^Y and ^T */
static	unsigned long old_msk;		/* Saved control mask		*/
static	short	trap_flag = FALSE;	/* TRUE if AST is set		*/
BOOLEAN DidCleanup = FALSE;		/* Exit handler flag		*/
static char VersionVMS[20];		/* Version of VMS		*/

PUBLIC int VMSVersion ARGS2(
	char *, 	VerString,
	int,		VerLen)
{
     unsigned long status, itm_cod = SYI$_VERSION;
     int i, verlen = 0;
     struct dsc$descriptor version;
     char *m;

     version.dsc$a_pointer = VerString;
     version.dsc$w_length = VerLen - 1;
     version.dsc$b_dtype = DSC$K_DTYPE_B;
     version.dsc$b_class = DSC$K_CLASS_S;

     status = lib$getsyi(&itm_cod, 0, &version, &verlen, 0, 0);
     if (!(status&1) || verlen == 0)
	  return 0;

     /*
      *  Cut out trailing spaces
      */
     for (m = VerString+verlen, i = verlen-1; i > 0 && VerString[i] == ' '; --i)
	  *(--m) = '\0';

     return strlen(VerString)+1;	/* Transmit ending 0 too */
}

PUBLIC void VMSexit NOARGS
{
    /*
     *	If we get here and DidCleanup is not set, it was via an
     *	ACCVIO, or outofmemory forced exit, so make *sure* we
     *	attempt a cleanup and reset the terminal.
     */
    if (!DidCleanup) {
	if (LYOutOfMemory == FALSE) {
	    fprintf(stderr,
gettext("\nA Fatal error has occurred in %s Ver. %s\n"), LYNX_NAME, LYNX_VERSION);
	    fprintf(stderr,
gettext("\nPlease notify your system administrator to confirm a bug, and if\n\
confirmed, to notify the lynx-dev list.  Bug reports should have concise\n\
descriptions of the command and/or URL which causes the problem, the\n\
operating system name with version number, the TCPIP implementation, the\n\
TRACEBACK if it can be captured, and any other relevant information.\n"));

	    if (LYTraceLogFP == NULL) {
		fprintf(stderr,RETURN_TO_CLEANUP);
		(void) getchar();
	    }
	} else if (LYCursesON) {
	    HTAlert(MEMORY_EXHAUSTED_ABORT);
	}
	cleanup();
    }
    if (LYOutOfMemory == TRUE) {
	printf("\r\n%s\r\n\r\n", MEMORY_EXHAUSTED_ABORT);
	fflush(stdout);
	fflush(stderr);
    }
}

/*
 *	TTOPEN --
 *		This function is called once to set up the terminal
 *		device streams.  It translates TT until it finds
 *		the terminal, then assigns a channel to it, sets it
 *		to EDIT, and sets up the Ctrl-C and Ctrl-Y interrupt
 *		handling.
 */
PUBLIC int ttopen NOARGS
{
	extern	void cleanup_sig();
	int	iosb[2];
	int	status;
	static unsigned long condition;
	static struct _exit_block {
	    unsigned long forward;
	    unsigned long address;
	    unsigned long zero;
	    unsigned long condition;
	} exit_handler_block;

	status = sys$assign( &term_nam_dsc, &iochan, 0, 0 );
	if( status != SS$_NORMAL )
		exit( status );

	status = sys$qiow( EFN, iochan, IO$_SENSEMODE, &iosb, 0, 0,
			  &oldmode, sizeof(oldmode), 0, 0, 0, 0 );
	if( status != SS$_NORMAL )
		exit( status );

	status = iosb[0] & 0xFFFF;
	if( status != SS$_NORMAL )
		exit( status );

	newmode[0] = oldmode[0];
	newmode[1] = oldmode[1];
	newmode[2] = oldmode[2] | TT2$M_EDIT;

	status = sys$qiow( EFN, iochan, IO$_SETMODE, &iosb, 0, 0,
			  &newmode, sizeof(newmode), 0, 0, 0, 0 );
	if( status != SS$_NORMAL )
		exit( status );

	status = iosb[0] & 0xFFFF;
	if( status != SS$_NORMAL )
		exit( status );

	/*
	 *  Declare the exit handler block.
	 */
	exit_handler_block.forward   = 0;
	exit_handler_block.address   = (unsigned long) &VMSexit;
	exit_handler_block.zero      = 0;
	exit_handler_block.condition = (unsigned long) &condition;
	status = sys$dclexh(&exit_handler_block);
	if (status != SS$_NORMAL)
		exit( status );

	/*
	 *  Set the AST.
	 */
	lib$disable_ctrl(&mask, &old_msk);
	trap_flag = TRUE;
	status = sys$qiow ( EFN, iochan,
			    IO$_SETMODE|IO$M_CTRLCAST|IO$M_CTRLYAST,
			    &iosb, 0, 0,
			    &cleanup_sig, SIGINT, 0, 0, 0, 0 );
	if ( status != SS$_NORMAL ) {
		lib$enable_ctrl(&old_msk);
		exit ( status );
	}

	/*
	 *  Get the version of VMS.
	 */
	if (VMSVersion(VersionVMS, 20) < 3)
		/*
		 *  Load zeros on error.
		 */
		strcpy(VersionVMS, "V0.0-0");

	return(0);
}	/*  ttopen  */

/*
 *	TTCLOSE --
 *		This function gets called just before we go back home
 *		to the command interpreter.  It puts the terminal back
 *		in a reasonable state.
 */
PUBLIC int ttclose NOARGS
{
	int	status;
	int	iosb[1];

	status = sys$qiow( EFN, iochan, IO$_SETMODE, &iosb, 0, 0,
			  &oldmode, sizeof(oldmode), 0, 0, 0, 0 );

	if( status != SS$_NORMAL || (iosb[0] & 0xFFFF) != SS$_NORMAL )
		exit( status );

	if (trap_flag) {
	    status = sys$dassgn (iochan);
	    status = lib$enable_ctrl(&old_msk);
	    trap_flag = FALSE;
	}
	return(0);
}	/*  ttclose  */

/*
 *	TTGETC --
 *		Read a character from the terminal, with NOECHO and NOFILTR.
 */
PUBLIC int ttgetc NOARGS
{
    int status;
    unsigned short iosb[4];

    if (in_pos < in_len)
	return(buffer[in_pos++]);

    status = sys$qiow(EFN, iochan,
		      IO$_READVBLK|IO$M_NOECHO|IO$M_NOFILTR,
		      &iosb, 0, 0,
		      &buffer, 1, 0, 0, 0, 0);
    if ((status&1) == 1)
	status = iosb[0];
    if (status == SS$_PARTESCAPE) {
	/*
	 *  Escape sequence in progress.  Fake a successful read.
	 */
	status = 1;
    }
    if ((status&1) != 1 && status != SS$_DATAOVERUN)
	exit(status);
    in_pos = 1;
    in_len = iosb[1] + iosb[3];
    return(buffer[0]);
}

/*
 *	TYPEAHEAD -- Fote Macrides 27-Jan-1994
 *		Check whether a keystroke has been entered, and return
 *		 it, or -1 if none was entered.
 */
PUBLIC int typeahead NOARGS
{
    int status;
    unsigned short iosb[4];

    if (dump_output_immediately)
	return -1;

    if (in_pos < in_len)
	return(buffer[in_pos++]);

again:
    status = sys$qiow (EFN, iochan,
		       IO$_READVBLK|IO$M_TIMED|IO$M_NOECHO|IO$M_NOFILTR,
		       &iosb, 0, 0,
		       &buffer, 1, 0, 0, 0, 0);
    if ((status&1) == 1)
	status = iosb[0];
    if (status == SS$_PARTESCAPE) {
	/*
	 *  Escape sequence in progress, finish reading it.
	 */
	goto again;
    }

    in_pos = 1;
    in_len = iosb[1] + iosb[3];
    if (status == SS$_TIMEOUT || status == SS$_DATAOVERUN)
	return(-1);
    return (buffer[0]);
}

/*
 *	VMSSIGNAL -- Fote Macrides 29-Jun-1993
 *		Sets up AST for both Ctrl-C and Ctrl-Y, with system response
 *		 to Ctrl-T disabled.  If called with a sig other than SIGINT,
 *		 it will use the C library's system(sig, func).
 *		The equivalent of VMSsignal(SIGINT, cleanup_sig) is done on
 *		 intialization by ttopen(), so don't do it again.
 *		VMSsignal(SIGINT, SIG_DFL) is treated as a call to ttclose().
 *		Call VMSsignal(SIGINT, SIG_IGN) before system() calls to
 *		 enable Ctrl-C and Ctrl-Y in the subprocess, and then call
 *		 VMSsignal(SIG_INT, cleanup_sig) on return from the subprocess.
 *		For func's which set flags and do not invoke an exit from
 *		 LYNX, the func should reassert itself.
 *		The VMS signal() calls do not fully emulate the Unix calls,
 *		 and VMSsignal() is just a "helper", also not a full emulation.
 */

PUBLIC void VMSsignal (sig,func)
int sig;
void (*func)();
{
	int status;
	short iosb[4];
	static int SIG_IGN_flag;

	/*
	 *  Pass all signals other than SIGINT to signal().
	 *  Also pass SIGINT to signal() if we're dumping.
	 */
	if (sig != SIGINT || dump_output_immediately) {
	    signal(sig, func);
	    return;
	}

	/*
	 *  If func is SIG_DFL, treat it as ttclose().
	 */
	if (func == SIG_DFL) {
	    ttclose();
	    return;
	}

	/*
	 *  Clear any previous AST.
	 */
	if (trap_flag) {
	    status = sys$dassgn (iochan);
	    status = lib$enable_ctrl(&old_msk);
	    trap_flag = FALSE;
	}

	/*
	 *  If func is SIG_IGN, leave the TT channel closed and the
	 *  system response to interrupts enabled for system() calls.
	 */
	if (func == SIG_IGN)
	    return;

	/*
	 *  If we get to here, we have a LYNX func, so set the AST.
	 */
	lib$disable_ctrl(&mask, &old_msk);
	trap_flag = TRUE;
	status = sys$assign (&term_nam_dsc, &iochan, 0, 0 );
	status = sys$qiow ( EFN, iochan,
			    IO$_SETMODE|IO$M_CTRLCAST|IO$M_CTRLYAST,
			    &iosb, 0, 0,
			    func, SIGINT, 0, 0, 0, 0 );

}	/* VMSsignal */

/*
 *  DCLspawn_exception, spawn_DCLprocess, DCLsystem -- F.Macrides 16-Jan-1994
 *	Exception-handler routines for regulating interrupts and enabling
 *	Control-T during spawns.  Includes TRUSTED flag for versions of VMS
 *	which require it in captive accounts.  This code should be used
 *	instead of the VAXC or DECC system(), by including LYUtils.h in
 *	modules which have system() calls.  It helps ensure that we return
 *	to Lynx instead of breaking out to DCL if a user issues interrupts
 *	or generates an ACCVIO during spawns.
 */
#ifdef __DECC
PRIVATE unsigned int DCLspawn_exception ARGS2(
	void *, 	sigarr,
	void *, 	mecharr)
#else
PRIVATE int DCLspawn_exception ARGS2(
	void *, 	sigarr,
	void *, 	mecharr)
#endif /* __DECC */
{
     int status;

     status = lib$sig_to_ret(sigarr, mecharr);
     return(SS$_UNWIND);
}

PRIVATE int spawn_DCLprocess ARGS1(
	char *, 	command)
{
     int status;
     unsigned long Status = 0;
     /*
      *  Keep DECC from complaining.
      */
     struct dsc$descriptor_s  command_desc;
     command_desc.dsc$w_length	= strlen(command);
     command_desc.dsc$b_class	= DSC$K_CLASS_S;
     command_desc.dsc$b_dtype	= DSC$K_DTYPE_T;
     command_desc.dsc$a_pointer = command;

     VAXC$ESTABLISH(DCLspawn_exception);

#ifdef __ALPHA /** OpenVMS/AXP lacked the TRUSTED flag before v6.1 **/
     if (VersionVMS[1] > '6' ||
	 (VersionVMS[1] == '6' && VersionVMS[2] == '.' &&
	  VersionVMS[3] >= '1'))
#else
     if (VersionVMS[1] >= '6')
#endif /* __ALPHA */
     {
	 /*
	  *  Include TRUSTED flag.
	  */
	 unsigned long trusted = CLI$M_TRUSTED;
	 status = lib$spawn(&command_desc,0,0,&trusted,
			    0,0,&Status);
	 /*
	  *  If it was invalid, try again without the flag.
	  */
	 if (status == LIB$_INVARG)
	    status = lib$spawn(&command_desc,0,0,0,
			       0,0,&Status );
     } else
	 status = lib$spawn(&command_desc,0,0,0,
			    0,0,&Status);
     /*
      *  Return -1 on error.
      */
     if ((status&1) != 1 || (Status&1) != 1)
	 return(-1);
     /*
      *  Return 0 on success.
      */
     return(0);
}

PUBLIC int DCLsystem ARGS1(
	char *, 	command)
{
     int status;
     extern void controlc();

     VMSsignal(SIGINT, SIG_IGN);
     status = spawn_DCLprocess(command);
     VMSsignal(SIGINT, cleanup_sig);
     /*
      *  Returns 0 on success, -1 any error.
      */
     return(status);
}
#endif /* VMS */

PUBLIC void lynx_force_repaint NOARGS
{
#if defined(COLOR_CURSES)
    chtype a;
    if (LYShowColor >= SHOW_COLOR_ON)
	a = COLOR_BKGD;
    else
	a = A_NORMAL;
    bkgdset(a | ' ');
#ifndef USE_COLOR_STYLE
    bkgd(a | ' ');
#endif
    attrset(a);
#endif /* COLOR_CURSES */
    clearok(curscr, TRUE);
}

PUBLIC void lynx_start_title_color NOARGS
{
}

PUBLIC void lynx_stop_title_color NOARGS
{
}

PUBLIC void lynx_start_link_color ARGS2(
	int,	flag,
	int,	pending)
{
    if (flag) {
	/* makes some terminals work wrong because
	 * they can't handle two attributes at the
	 * same time
	 */
	/* start_bold();  */
	start_reverse();
#if defined(USE_SLANG)
#ifndef __DJGPP__
	if (SLtt_Use_Ansi_Colors)
#endif /* !__DJGPP__ */
	    start_underline ();
#endif /* USE_SLANG */
#if defined(FANCY_CURSES) && defined(COLOR_CURSES)
	if (lynx_has_color && LYShowColor >= SHOW_COLOR_ON)
	    start_underline ();
#endif /* USE_SLANG */
     } else {
	start_bold();
	/*
	 *  Make sure when flag is OFF that "unhighlighted" links
	 *  will be underlined if appropriate. - LE & FM
	 */
	if (pending)
	    start_underline();
     }
}

PUBLIC void lynx_stop_link_color ARGS2(
	int,	flag,
	int,	pending GCC_UNUSED)
{
#ifdef USE_COLOR_STYLE
    LynxChangeStyle(flag == ON ? s_alink : s_a, ABS_OFF, 0);
#else
    if (flag) {
	stop_reverse();
#if defined(USE_SLANG)
#ifndef __DJGPP__
	if (SLtt_Use_Ansi_Colors)
#endif /* !__DJGPP__ */
	stop_underline ();
#endif /* USE_SLANG */
#if defined(FANCY_CURSES) && defined(COLOR_CURSES)
	if (lynx_has_color && LYShowColor >= SHOW_COLOR_ON)
	    stop_underline ();
#endif /* FANCY_CURSES && COLOR_CURSES */
    } else {
	stop_bold();
	/*
	 *  If underlining was turned on above, turn it off. - LE & FM
	 */
	if (pending)
	    stop_underline();
    }
#endif
}


PUBLIC void lynx_stop_target_color NOARGS
{
   stop_underline();
   stop_reverse();
   stop_bold();
}

PUBLIC void lynx_start_target_color NOARGS
{
   start_bold();
   start_reverse();
   start_underline();
}


PUBLIC void lynx_start_status_color NOARGS
{
#if USE_COLOR_TABLE && defined(COLOR_CURSES)
    if (lynx_has_color && LYShowColor >= SHOW_COLOR_ON)
	lynx_set_color (2);
    else
#endif
	start_reverse ();
}

PUBLIC void lynx_stop_status_color NOARGS
{
#if USE_COLOR_TABLE && defined(COLOR_CURSES)
    if (lynx_has_color && LYShowColor >= SHOW_COLOR_ON)
	lynx_set_color (0);
    else
#endif
	stop_reverse ();
}

PUBLIC void lynx_start_h1_color NOARGS
{
   if (bold_H1 || bold_headers)
     start_bold();
}

PUBLIC void lynx_stop_h1_color NOARGS
{
   if (bold_H1 || bold_headers)
     stop_bold();
}

PUBLIC void lynx_start_prompt_color NOARGS
{
   start_reverse ();
}

PUBLIC void lynx_stop_prompt_color NOARGS
{
   stop_reverse ();
}

PUBLIC void lynx_start_radio_color NOARGS
{
   start_bold ();
}

PUBLIC void lynx_stop_radio_color NOARGS
{
   stop_bold ();
}

PUBLIC void lynx_stop_all_colors NOARGS
{
   stop_underline ();
   stop_reverse ();
   stop_bold ();
}