annotate lwasm/strings.c @ 577:e49d24f4a9a5

Correct bug in the object file output code leading to stack corruption It turns out leaving a pointer to a stack allocated temporary in a persistent data structure is not conducive to correct program operation. Undo the export check setup in the object file output sequence so a pointer to stack allocated memory is not left hanging when the function returns. This seems to correct at least one mysterious crash bug, and possibly others. Thanks to Boisy Pitre for reporting the crash bug that led to this discovery, as well as a previous crash bug that likely has the same root cause. Additional thanks to Ciaran Anscomb whose debugger wielding wizardry revealed the exact location of this particular bit of unbrilliance.
author William Astle <lost@l-w.ca>
date Sat, 03 Aug 2024 14:30:06 -0600
parents 724bcc4508bc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
519
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
2 strings.c
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
3 Copyright © 2021 William Astle
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
4
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
5 This file is part of LWASM.
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
6
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
10 version.
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
11
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
15 more details.
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
16
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
19
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
20 Contains stuff associated with generalized string parsing including
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
21 interpolation.
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
22
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
23 A general string is enclosed in double quotes. Within the string, the
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
24 following is supported:
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
25
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
26 %(VAR): a string variable defined with SETSTR
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
27 %[SYM]: the value of SYM; must be constant on pass 1 or will resolve to ""
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
28 \": a literal "
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
29 \%: a literal %
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
30 \n: a newline
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
31 \r: a carriage return
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
32 \t: a tab
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
33 \f: a form feed
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
34 \e: ESC (0x1b)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
35 \\: a backslash
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
36 \xXX: an 8 bit value where XX are hex digits
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
37
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
38 */
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
39
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
40 #include <ctype.h>
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
41 #include <string.h>
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
42
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
43 #include <lw_alloc.h>
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
44 #include <lw_dict.h>
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
45 #include <lw_string.h>
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
46 #include <lw_strbuf.h>
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
47
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
48 #include "lwasm.h"
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
49 #include "instab.h"
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
50
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
51 void lwasm_stringvar_unset(asmstate_t *as, char *strname)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
52 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
53 if (!(as -> stringvars))
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
54 return;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
55 lw_dict_unset(as -> stringvars, strname);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
56 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
57
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
58 void lwasm_stringvar_set(asmstate_t *as, char *strname, char *strval)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
59 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
60 if (!(as -> stringvars))
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
61 as -> stringvars = lw_dict_create();
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
62 lw_dict_set(as -> stringvars, strname, strval);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
63 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
64
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
65 char *lwasm_stringvar_get(asmstate_t *as, char *strname)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
66 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
67 if (!(as -> stringvars))
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
68 return "";
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
69 return lw_dict_get(as -> stringvars, strname);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
70 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
71
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
72 PARSEFUNC(pseudo_parse_setstr)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
73 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
74 char *t1;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
75 char *strname;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
76 char *strval;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
77
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
78 l -> len = 0;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
79 if (!**p)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
80 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
81 lwasm_register_error(as, l, E_OPERAND_BAD);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
82 return;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
83 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
84
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
85 for (t1 = *p; *t1 && *t1 != '='; t1++)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
86 /* do nothing */;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
87 strname = lw_alloc(t1 - *p + 1);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
88 strncpy(strname, *p, t1 - *p);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
89 strname[t1 - *p] = '\0';
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
90 *p = t1;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
91 if (**p == '\0')
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
92 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
93 lwasm_stringvar_unset(l -> as, strname);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
94 lw_free(strname);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
95 return;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
96 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
97 (*p)++;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
98 strval = lwasm_parse_general_string(l, p);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
99 if (!strval)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
100 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
101 lwasm_stringvar_unset(l -> as, strname);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
102 lw_free(strname);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
103 return;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
104 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
105 lwasm_stringvar_set(l -> as, strname, strval);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
106 lw_free(strval);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
107 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
108
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
109 char *lwasm_parse_general_string(line_t *l, char **p)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
110 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
111 struct lw_strbuf *sb;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
112
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
113 if (!**p || isspace(**p))
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
114 return lw_strdup("");
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
115 if (**p != '"')
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
116 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
117 lwasm_register_error(l -> as, l, E_OPERAND_BAD);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
118 return NULL;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
119 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
120
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
121 (*p)++;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
122 sb = lw_strbuf_new();
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
123 while (**p && **p != '"')
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
124 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
125 switch (**p)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
126 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
127 case '\\':
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
128 if ((*p)[1])
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
129 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
130 (*p)++;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
131 switch (**p)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
132 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
133 case 'n':
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
134 lw_strbuf_add(sb, 10);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
135 break;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
136
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
137 case 'r':
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
138 lw_strbuf_add(sb, 13);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
139 break;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
140
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
141 case 't':
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
142 lw_strbuf_add(sb, 9);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
143 break;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
144
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
145 case 'f':
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
146 lw_strbuf_add(sb, 12);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
147 break;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
148
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
149 case 'e':
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
150 lw_strbuf_add(sb, 27);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
151 break;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
152
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
153 case 'x':
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
154 if ((*p)[1] && (*p)[2])
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
155 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
156 int d1 = (*p)[1];
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
157 int d2 = (*p)[2];
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
158 if (d1 < '0' || (d1 > '9' && d1 < 'A') || (d1 > 'F' && d1 < 'a') || d1 > 'f' ||
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
159 d2 < '0' || (d2 > '9' && d2 < 'A') || (d2 > 'F' && d2 < 'a') || d2 > 'f')
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
160 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
161 lw_strbuf_add(sb, 'x');
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
162 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
163 else
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
164 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
165 (*p) += 2;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
166 d1 -= '0';
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
167 d2 -= '0';
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
168 if (d1 > 9)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
169 d1 -= 7;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
170 if (d1 > 15)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
171 d1 -= 32;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
172 if (d2 > 9)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
173 d2 -= 7;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
174 if (d2 > 15)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
175 d2 -= 32;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
176 lw_strbuf_add(sb, (d1 << 4) | d2);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
177 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
178 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
179 else
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
180 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
181 lw_strbuf_add(sb, 'x');
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
182 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
183 break;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
184
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
185 default:
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
186 lw_strbuf_add(sb, **p);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
187 break;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
188 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
189 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
190 break;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
191
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
192 case '%':
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
193 (*p)++;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
194 if (**p == '(')
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
195 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
196 char *t1;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
197 // string var
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
198 for (t1 = *p + 1; *t1 && *t1 != ')' && *t1 != '"'; t1++)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
199 /* do nothing */ ;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
200 if (*t1 != ')')
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
201 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
202 lw_strbuf_add(sb, '%');
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
203 (*p)--;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
204 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
205 else
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
206 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
207 char *strname;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
208 strname = lw_alloc(t1 - *p);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
209 strncpy(strname, *p + 1, t1 - *p);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
210 strname[t1 - *p - 1] = '\0';
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
211 *p = t1;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
212 t1 = lwasm_stringvar_get(l -> as, strname);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
213 lw_free(strname);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
214 for (strname = t1; *strname; strname++)
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
215 lw_strbuf_add(sb, *strname);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
216 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
217 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
218 else
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
219 {
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
220 // unknown % sequence; back up and output the %
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
221 (*p)--;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
222 lw_strbuf_add(sb, '%');
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
223 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
224 break;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
225
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
226 default:
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
227 lw_strbuf_add(sb, **p);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
228 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
229 (*p)++;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
230 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
231 if (**p == '"')
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
232 (*p)++;
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
233 return lw_strbuf_end(sb);
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
234 }
724bcc4508bc Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff changeset
235