Skip to content

Commit e8b32de

Browse files
committed
COMMON: code cleanup
1 parent 92acc0b commit e8b32de

File tree

8 files changed

+69
-54
lines changed

8 files changed

+69
-54
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
2014-08-22
22
Fix HASH var crash
33
Fix HASH var handling of mixed types
4+
Fix illegal UDS field variable names
45

56
2014-08-15
67
Improved runtime performance

src/common/bc.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void bc_add2i(bc_t *bc, byte code, word p1) {
114114
/*
115115
* add one command and one long int (32 bits)
116116
*/
117-
void bc_add2l(bc_t * bc, byte code, long p1) {
117+
void bc_add2l(bc_t *bc, byte code, long p1) {
118118
if (bc->count >= bc->size - 8) {
119119
bc_resize(bc, bc->size + BC_ALLOC_INCR);
120120
}
@@ -207,19 +207,17 @@ void bc_add_creal(bc_t *bc, var_num_t v) {
207207
/*
208208
* add one command and one string (see: bc_store_string)
209209
*/
210-
void bc_add2s(bc_t *bc, byte code, const char *p1) {
211-
int l = strlen(p1);
212-
if (l > BC_MAX_STORE_SIZE) {
210+
void bc_add_strn(bc_t *bc, const char *str, int len) {
211+
if (len > BC_MAX_STORE_SIZE) {
213212
sc_raise("STRING TOO BIG");
214213
} else {
215-
bc_add_code(bc, code);
216-
bc_add_dword(bc, l);
217-
218-
if (bc->count >= bc->size - l) {
214+
bc_add_code(bc, kwTYPE_STR);
215+
bc_add_dword(bc, len);
216+
if (bc->count >= bc->size - len) {
219217
bc_resize(bc, bc->size + BC_ALLOC_INCR);
220218
}
221-
memcpy(bc->ptr + bc->count, p1, l);
222-
bc->count += l;
219+
memcpy(bc->ptr + bc->count, str, len);
220+
bc->count += len;
223221
}
224222
}
225223

@@ -255,11 +253,11 @@ char *bc_store_string(bc_t *bc, char *src) {
255253
char *cstr;
256254
cstr = cstrdup(np);
257255
tmp_free(np);
258-
bc_add2s(bc, kwTYPE_STR, cstr);
256+
bc_add_strn(bc, cstr, strlen(cstr));
259257
tmp_free(cstr);
260258
} else {
261259
// normal
262-
bc_add2s(bc, kwTYPE_STR, np);
260+
bc_add_strn(bc, np, strlen(np));
263261
tmp_free(np);
264262
}
265263
p++;
@@ -285,7 +283,7 @@ char *bc_store_macro(bc_t *bc, char *src) {
285283
np = tmp_alloc(l + 1);
286284
strncpy(np, src + 1, l);
287285
np[l - 1] = '\0';
288-
bc_add2s(bc, kwTYPE_STR, np);
286+
bc_add_strn(bc, np, strlen(np));
289287
tmp_free(np);
290288

291289
p++;

src/common/bc.h

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void sc_raise(const char *fmt, ...);
4848
*
4949
* @param bc the bc structure
5050
*/
51-
void bc_create(bc_t * bc);
51+
void bc_create(bc_t *bc);
5252

5353
/**
5454
* @ingroup scan
@@ -57,7 +57,7 @@ void bc_create(bc_t * bc);
5757
*
5858
* @param bc the bc structure
5959
*/
60-
void bc_destroy(bc_t * bc);
60+
void bc_destroy(bc_t *bc);
6161

6262
/**
6363
* @ingroup scan
@@ -67,7 +67,7 @@ void bc_destroy(bc_t * bc);
6767
* @param bc the bc structure
6868
* @param newsize the new size
6969
*/
70-
void bc_resize(bc_t * bc, dword newsize);
70+
void bc_resize(bc_t *bc, dword newsize);
7171

7272
/**
7373
* @ingroup scan
@@ -77,7 +77,7 @@ void bc_resize(bc_t * bc, dword newsize);
7777
* @param bc the bc structure
7878
* @param code the byte
7979
*/
80-
void bc_add1(bc_t * bc, byte code);
80+
void bc_add1(bc_t *bc, byte code);
8181

8282
/**
8383
* @ingroup scan
@@ -87,7 +87,7 @@ void bc_add1(bc_t * bc, byte code);
8787
* @param bc the bc structure
8888
* @param code the byte
8989
*/
90-
void bc_store1(bc_t * bc, addr_t offset, byte code);
90+
void bc_store1(bc_t *bc, addr_t offset, byte code);
9191

9292
/**
9393
* @ingroup scan
@@ -97,7 +97,7 @@ void bc_store1(bc_t * bc, addr_t offset, byte code);
9797
* @param bc the bc structure
9898
* @param code the word
9999
*/
100-
void bc_add_word(bc_t * bc, word code);
100+
void bc_add_word(bc_t *bc, word code);
101101

102102
/**
103103
* @ingroup scan
@@ -107,7 +107,14 @@ void bc_add_word(bc_t * bc, word code);
107107
* @param bc the bc structure
108108
* @param code the dword
109109
*/
110-
void bc_add_dword(bc_t * bc, dword code);
110+
void bc_add_dword(bc_t *bc, dword code);
111+
112+
/**
113+
* @ingroup scan
114+
*
115+
* adds a string of the given length
116+
*/
117+
void bc_add_strn(bc_t *bc, const char *str, int len);
111118

112119
/**
113120
* @ingroup scan
@@ -118,7 +125,7 @@ void bc_add_dword(bc_t * bc, dword code);
118125
* @param str the raw-string. the string must starts with \". the string must also contains the ending \".
119126
* @return a pointer of src to the next character after the second \"
120127
*/
121-
char *bc_store_string(bc_t * bc, char *src);
128+
char *bc_store_string(bc_t *bc, char *src);
122129

123130
/**
124131
* @ingroup scan
@@ -129,7 +136,7 @@ char *bc_store_string(bc_t * bc, char *src);
129136
* @param str the raw-string. the string must starts with `. the string must also contains the ending `.
130137
* @return a pointer of src to the next character after the second `
131138
*/
132-
char *bc_store_macro(bc_t * bc, char *src);
139+
char *bc_store_macro(bc_t *bc, char *src);
133140

134141
/**
135142
* @ingroup scan
@@ -138,7 +145,7 @@ char *bc_store_macro(bc_t * bc, char *src);
138145
*
139146
* @param bc the bc structure
140147
*/
141-
void bc_eoc(bc_t * bc);
148+
void bc_eoc(bc_t *bc);
142149

143150
/**
144151
* @ingroup scan
@@ -148,7 +155,7 @@ void bc_eoc(bc_t * bc);
148155
* @param dest the destination
149156
* @param src the code to be appended to dest
150157
*/
151-
void bc_append(bc_t * dest, bc_t * src);
158+
void bc_append(bc_t *dest, bc_t *src);
152159

153160
/**
154161
* @ingroup scan
@@ -159,7 +166,7 @@ void bc_append(bc_t * dest, bc_t * src);
159166
* @param src the code to be appended to dest
160167
* @param n the size of the src to be copied
161168
*/
162-
void bc_add_n(bc_t * dest, byte * src, dword n);
169+
void bc_add_n(bc_t *dest, byte *src, dword n);
163170

164171
/**
165172
* @ingroup scan
@@ -175,7 +182,7 @@ void bc_add_n(bc_t * dest, byte * src, dword n);
175182
* @param dest the bc segment
176183
* @param idx the index of the function
177184
*/
178-
void bc_add_fcode(bc_t * dest, long idx);
185+
void bc_add_fcode(bc_t *dest, long idx);
179186

180187
/**
181188
* @ingroup scan
@@ -185,7 +192,7 @@ void bc_add_fcode(bc_t * dest, long idx);
185192
* @param dest the bc segment
186193
* @param idx the index of the procedure
187194
*/
188-
void bc_add_pcode(bc_t * dest, long idx);
195+
void bc_add_pcode(bc_t *dest, long idx);
189196

190197
/**
191198
* @ingroup scan
@@ -196,7 +203,7 @@ void bc_add_pcode(bc_t * dest, long idx);
196203
* @param lib the index of the library
197204
* @param idx the index of the function
198205
*/
199-
void bc_add_extfcode(bc_t * dest, int lib, long idx);
206+
void bc_add_extfcode(bc_t *dest, int lib, long idx);
200207

201208
/**
202209
* @ingroup scan
@@ -207,7 +214,7 @@ void bc_add_extfcode(bc_t * dest, int lib, long idx);
207214
* @param lib the index of the library
208215
* @param idx the index of the procedure
209216
*/
210-
void bc_add_extpcode(bc_t * dest, int lib, long idx);
217+
void bc_add_extpcode(bc_t *dest, int lib, long idx);
211218

212219
/**
213220
* @ingroup scan
@@ -217,7 +224,7 @@ void bc_add_extpcode(bc_t * dest, int lib, long idx);
217224
* @param bc the bc segment
218225
* @param idx the address
219226
*/
220-
void bc_add_addr(bc_t * bc, addr_t idx);
227+
void bc_add_addr(bc_t *bc, addr_t idx);
221228

222229
/**
223230
* @ingroup scan
@@ -229,7 +236,7 @@ void bc_add_addr(bc_t * bc, addr_t idx);
229236
* @param true_ip the jump-address when on true
230237
* @param false_ip the jump-address when on false
231238
*/
232-
void bc_add_ctrl(bc_t * bc, code_t code, addr_t true_ip, addr_t false_ip);
239+
void bc_add_ctrl(bc_t *bc, code_t code, addr_t true_ip, addr_t false_ip);
233240

234241
/**
235242
* @ingroup scan
@@ -239,7 +246,7 @@ void bc_add_ctrl(bc_t * bc, code_t code, addr_t true_ip, addr_t false_ip);
239246
* @param bc the bc segment
240247
* @param v the number
241248
*/
242-
void bc_add_creal(bc_t * bc, var_num_t v);
249+
void bc_add_creal(bc_t *bc, var_num_t v);
243250

244251
/**
245252
* @ingroup scan
@@ -249,6 +256,6 @@ void bc_add_creal(bc_t * bc, var_num_t v);
249256
* @param bc the bc segment
250257
* @param v the number
251258
*/
252-
void bc_add_cint(bc_t * bc, var_int_t v);
259+
void bc_add_cint(bc_t *bc, var_int_t v);
253260

254261
#endif

src/common/eval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* matrix: convert var_t to double[r][c]
2929
*/
30-
var_num_t* mat_toc(var_t *v, int32 *rows, int32 *cols) {
30+
var_num_t *mat_toc(var_t *v, int32 *rows, int32 *cols) {
3131
int i, j, pos;
3232
var_t *e;
3333
var_num_t *m;
@@ -69,7 +69,7 @@ var_num_t* mat_toc(var_t *v, int32 *rows, int32 *cols) {
6969
/**
7070
* matrix: conv. double[nr][nc] to var_t
7171
*/
72-
void mat_tov(var_t * v, var_num_t *m, int rows, int cols, int protect_col1) {
72+
void mat_tov(var_t *v, var_num_t *m, int rows, int cols, int protect_col1) {
7373
var_t *e;
7474
int i, j, pos;
7575

src/common/fs_stream.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ int stream_write(dev_file_t *f, byte *data, dword size) {
100100

101101
r = write(f->handle, data, size);
102102
if (r != (int) size) {
103-
fprintf(stderr, "error result =%d %d\n", r, size);
104103
err_file((f->last_error = errno));
105104
}
106105
return (r == (int) size);

src/common/scan.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
#include "common/messages.h"
2020
#include "languages/keywords.en.c"
2121

22-
char *comp_array_uds_field(char *p, bc_t * bc);
22+
char *comp_array_uds_field(char *p, bc_t *bc);
2323
void comp_text_line(char *text);
2424
addr_t comp_search_bc(addr_t ip, code_t code);
25-
extern void expr_parser(bc_t * bc);
25+
extern void expr_parser(bc_t *bc);
2626
extern void sc_raise2(const char *fmt, int line, const char *buff); // sberr
2727

2828
#define STRLEN(s) ((sizeof(s) / sizeof(s[0])) - 1)
@@ -574,14 +574,14 @@ int comp_check_lib(const char *name) {
574574
return 0;
575575
}
576576

577-
/*
577+
/**
578+
* create a new variable
578579
*/
579580
int comp_create_var(const char *name) {
580581
int idx = -1;
581-
582-
if (!(is_alpha(name[0]) || name[0] == '_'))
582+
if (!(is_alpha(name[0]) || name[0] == '_')) {
583583
sc_raise(MSG_WRONG_VARNAME, name);
584-
else {
584+
} else {
585585
// realloc table if it is needed
586586
if (comp_varcount >= comp_varsize) {
587587
comp_varsize += GROWSIZE;
@@ -600,7 +600,8 @@ int comp_create_var(const char *name) {
600600
return idx;
601601
}
602602

603-
/*
603+
/**
604+
* add external variable
604605
*/
605606
int comp_add_external_var(const char *name, int lib_id) {
606607
int idx;
@@ -729,13 +730,19 @@ int comp_get_uds_field_id(const char *field_name, int len) {
729730
}
730731
}
731732

733+
int result;
732734
strncpy(uds.name, field_name, len);
733-
uds.name[len] = 0;
734-
uds.field_id = ++comp_next_field_id;
735-
dbt_write(comp_udstable, comp_udscount, &uds, sizeof(comp_struct_t));
736-
comp_udscount++;
737-
738-
return uds.field_id;
735+
if (!(is_alpha(uds.name[0]) || uds.name[0] == '_')) {
736+
sc_raise(MSG_WRONG_VARNAME, uds.name);
737+
result = -1;
738+
} else {
739+
uds.name[len] = 0;
740+
uds.field_id = ++comp_next_field_id;
741+
dbt_write(comp_udstable, comp_udscount, &uds, sizeof(comp_struct_t));
742+
comp_udscount++;
743+
result = uds.field_id;
744+
}
745+
return result;
739746
}
740747

741748
/*
@@ -745,7 +752,7 @@ int comp_get_uds_field_id(const char *field_name, int len) {
745752
* then the foo variable is added as kwTYPE_UDS.
746753
*
747754
*/
748-
void comp_add_variable(bc_t * bc, const char *var_name) {
755+
void comp_add_variable(bc_t *bc, const char *var_name) {
749756
char *dot = strchr(var_name, '.');
750757

751758
if (dot != 0 && !comp_check_lib(var_name)) {
@@ -779,7 +786,6 @@ void comp_add_variable(bc_t * bc, const char *var_name) {
779786
bc_add_code(bc, kwTYPE_UDS_EL);
780787
bc_add_addr(bc, var_id);
781788
}
782-
783789
} else if (comp_check_uds(var_name)) {
784790
// uds-container
785791
// all of var_name same as dot-less portion of existing variable

src/common/var.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,6 @@ void v_set(var_t *dest, const var_t *src) {
499499
return;
500500
} else if (dest->type == V_HASH) {
501501
// lvalue struct assigned to non-struct rvalue
502-
fprintf(stderr, "hash_clear\n");
503502
hash_clear(dest);
504503
return;
505504
}

src/common/var_uds.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,13 @@ var_p_t uds_resolve_fields(const var_p_t var_p) {
118118
addr_t field_id = code_getaddr();
119119

120120
if (var_p->type != V_UDS) {
121-
v_free(var_p);
122-
var_p->type = V_UDS;
121+
if (v_is_nonzero(var_p)) {
122+
err_typemismatch();
123+
return NULL;
124+
} else {
125+
v_free(var_p);
126+
var_p->type = V_UDS;
127+
}
123128
}
124129

125130
uds_field_s *next = var_p->v.uds;

0 commit comments

Comments
 (0)