-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.c
More file actions
1060 lines (985 loc) · 37.4 KB
/
Copy pathvariable.c
File metadata and controls
1060 lines (985 loc) · 37.4 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "variable.h"
#include "memory.h"
#include "io.h"
#include "error.h"
#include "expr.h"
#include "execute.h"
#include "strconv.h"
#include "rstring.h"
#include "map.h"
#include "iterator.h"
#define INITIAL_TABLE_SIZE 10
VariableTable* variable_table = NULL;
void init_variables() {
variable_table = create_variable_table();
}
VariableTable* create_variable_table() {
VariableTable* table = rmalloc(sizeof(VariableTable));
table->variables = rmalloc(INITIAL_TABLE_SIZE * sizeof(Variable));
table->size = 0;
table->capacity = INITIAL_TABLE_SIZE;
return table;
}
void free_variable_table(VariableTable* table) {
register int i;
for (i = 0; i < table->size; i++) {
string__free(table->variables[i].name);
string__free(table->variables[i].str);
free_va_value(&table->variables[i].value);
}
rfree(table->variables);
rfree(table);
}
static void ensure_capacity(VariableTable* table) {
if (table->size >= table->capacity) {
table->capacity *= 2;
table->variables = rrealloc(table->variables, (size_t)(table->capacity * (int)sizeof(Variable)));
}
}
Variable* create_new_variable(VariableTable* table, const string name, VariableType type) {
Variable* existing = get_variable(table, name);
if (existing != NULL) return existing;
if (table->size >= table->capacity) ensure_capacity(table);
Variable* var = &table->variables[table->size++];
var->name = string__from(name);
var->str = _SLIT0;
var->flags = 0;
var->array_size = 0;
memset(&var->value, 0, sizeof(va_value_t));
var->value.type = type;
switch (type) {
case VAR_STRING:
var->value._str = _SLIT0;
break;
case VAR_INTEGER:
var->value._number = 0;
break;
case VAR_ASSOCIATIVE_ARRAY:
var->value._map = create_map_with_func(vfree_va_value);
break;
case VAR_ARRAY:
var->value._array = create_array(sizeof(va_value_t));
break;
default:
break;
}
return var;
}
void process_string_variable(Variable* var) {
if (!var->value.type == VAR_STRING) return;
if (string__is_null_or_empty(var->value._str)) return;
if (is_variable_flag_set(&var->flags, VarFlag_Uppercase)) rstring__upper(var->value._str);
else if (is_variable_flag_set(&var->flags, VarFlag_Lowercase)) rstring__lower(var->value._str);
}
void process_exported_variable(Variable* var) {
if (is_variable_flag_set(&var->flags, VarFlag_Exported)) {
string value = va_value_to_string(&var->value);
setenv(var->name.str, value.str, 1);
string__free(value);
}
}
Variable* set_variable(VariableTable* table, const string name, const string value, VariableType type, bool readonly) {
Variable* var = get_variable(table, name);
if (var == NULL) {
var = create_new_variable(table, name, type);
} else {
if (is_variable_flag_set(&var->flags, VarFlag_ReadOnly)) {
print_error(_SLIT("Cannot modify readonly variable"));
return NULL;
}
string__free(var->str);
free_va_value(&var->value);
}
var->str = string__from(value);
var->value.type = type;
switch (type) {
case VAR_STRING:
var->value._str = string__remove_quotes(value);
string__free(var->str);
var->str = string__from(var->value._str);
process_string_variable(var);
break;
case VAR_INTEGER:
StrconvResult result = ratoll(value, &var->value._number);
if (result.is_err) {
print_error(_SLIT("Failed to convert string to integer"));
return NULL;
}
break;
case VAR_ARRAY:
if (var->value._array.data) array_free(&var->value._array);
parse_and_set_array(variable_table, name, value);
break;
case VAR_ASSOCIATIVE_ARRAY:
if (var->value._map) map_free(var->value._map);
parse_and_set_associative_array(variable_table, name, value);
break;
default:
break;
}
if (readonly) set_variable_flag(&var->flags, VarFlag_ReadOnly);
process_exported_variable(var);
return var;
}
Variable* get_variable(VariableTable* table, const string name) {
register int i;
for (i = 0; i < table->size; i++) {
if (string__compare(table->variables[i].name, name) == 0) {
Variable* var = &table->variables[i];
if (var->value.type == VAR_NAMEREF) {
Variable* resolved = resolve_nameref(var);
if (resolved == NULL) {
print_error(_SLIT("Failed to resolve nameref"));
return NULL;
}
return resolved;
}
return var;
}
}
return NULL;
}
void unset_variable(VariableTable* table, const string name) {
register int i;
for (i = 0; i < table->size; i++) {
if (string__compare(table->variables[i].name, name) == 0) {
if (is_variable_flag_set(&table->variables[i].flags, VarFlag_ReadOnly)) {
print_error(_SLIT("Cannot unset readonly variable"));
return;
}
string__free(table->variables[i].name);
string__free(table->variables[i].str);
free_va_value(&table->variables[i].value);
memmove(&table->variables[i], &table->variables[i + 1], (size_t)((table->size - i - 1) * (int)sizeof(Variable)));
table->size--;
return;
}
}
}
void parse_and_set_array(VariableTable* table, string name, string value) {
if (string__is_null_or_empty(value) || table == NULL) return;
Variable* existing = get_variable(table, name);
if (existing != NULL) {
if (is_variable_flag_set(&existing->flags, VarFlag_ReadOnly)) {
print_error(_SLIT("Cannot modify readonly variable"));
return;
}
unset_variable(table, name);
}
if (value.str[0] != '(' || value.str[value.len - 1] != ')') {
print_error(_SLIT("Invalid array format"));
return;
}
Variable* var = create_new_variable(table, name, VAR_ARRAY);
array_free(&var->value._array);
var->value = string_to_va_value(value, VAR_ARRAY);
}
void parse_and_set_associative_array(VariableTable* table, string name, string input) {
if (string__is_null_or_empty(input) || table == NULL) return;
Variable* existing = get_variable(table, name);
if (existing != NULL) {
if (is_variable_flag_set(&existing->flags, VarFlag_ReadOnly)) {
print_error(_SLIT("Cannot modify readonly variable"));
return;
}
unset_variable(table, name);
}
Variable* var = create_new_variable(table, name, VAR_ASSOCIATIVE_ARRAY);
if (input.str[0] != '{' || input.str[input.len - 1] != '}') {
print_error(_SLIT("Invalid input format"));
return;
}
map_free(var->value._map);
var->value = string_to_va_value(input, VAR_ASSOCIATIVE_ARRAY);
}
void array_set_element(VariableTable* table, const string name, size_t index, const string value) {
Variable* var = get_variable(table, name);
if (var == NULL || var->value.type != VAR_ARRAY) {
print_error(_SLIT("Variable is not an array"));
return;
}
if (index >= var->value._array.size) {
print_error(_SLIT("Index out of bounds"));
return;
}
VariableType type = parse_variable_type(value);
va_value_t new_value = string_to_va_value(value, type);
array_index_set(&var->value._array, index, &new_value);
}
bool do_not_expand_this_builtin(const string name) {
string do_not_expand[] = {_SLIT("declare"), _SLIT("export"), _SLIT("readonly"), _SLIT("set"), _SLIT("unset")};
for (int i = 0; (unsigned long)i < sizeof(do_not_expand) / sizeof(string); i++) {
if (string__compare(name, do_not_expand[i]) == 0)
return true;
}
return false;
}
VariableType parse_variable_type(string value) {
if (string__is_null_or_empty(value))
return VAR_STRING;
rstring__trim(value);
if (string__is_null_or_empty(value))
return VAR_STRING;
if ((string__startswith(value, _SLIT("\"")) && string__endswith(value, _SLIT("\""))) ||
(string__startswith(value, _SLIT("'")) && string__endswith(value, _SLIT("'"))))
return VAR_STRING;
if (string__isdigit(value))
return VAR_INTEGER;
if (string__startswith(value, _SLIT("(")) && string__endswith(value, _SLIT(")")))
return VAR_ARRAY;
if (string__startswith(value, _SLIT("{")) && string__endswith(value, _SLIT("}")))
return VAR_ASSOCIATIVE_ARRAY;
return VAR_STRING;
}
Variable* resolve_nameref(Variable* var) {
if (var == NULL || var->value.type != VAR_NAMEREF) {
return var;
}
Variable* resolved = var;
int depth = 0;
while (resolved != NULL && resolved->value.type == VAR_NAMEREF) {
if (depth++ > 100) {
print_error(_SLIT("Too many levels of indirection"));
return NULL;
}
resolved = get_variable(variable_table, resolved->value._str);
if (resolved == NULL) {
print_error(_SLIT("Variable not found"));
return NULL;
}
if (resolved->value.type != VAR_NAMEREF) break;
}
return resolved;
}
void set_associative_array_variable(VariableTable* table, const string name, const string key, const string value) {
Variable* var = get_variable(table, name);
if (var == NULL) var = create_new_variable(table, name, VAR_ASSOCIATIVE_ARRAY);
if (var->value.type != VAR_ASSOCIATIVE_ARRAY) {
print_error(_SLIT("Variable is not an associative array"));
return;
}
VariableType vt = parse_variable_type(value);
va_value_t new_value = string_to_va_value(value, vt);
map_insert(var->value._map, key.str, &new_value, sizeof(va_value_t));
}
string va_value_default_string(const VariableType type) {
string result = _SLIT0;
switch (type) {
case VAR_STRING:
result = _SLIT("\"\"");
break;
case VAR_INTEGER:
result = _SLIT("0");
break;
case VAR_ARRAY:
result = _SLIT("()");
break;
case VAR_ASSOCIATIVE_ARRAY:
result = _SLIT("{}");
break;
default:
break;
}
return result;
}
string va_value_to_string(const va_value_t* value) {
string result = _SLIT0;
switch (value->type) {
case VAR_STRING:
result = string__from(value->_str);
break;
case VAR_INTEGER:
StrconvResult r = rlltos(value->_number);
if (r.is_err) {
print_error(_SLIT("Failed to convert integer to string"));
return _SLIT0;
}
result = *(string*)(r.value);
break;
case VAR_ARRAY: {
StringBuilder sb = string_builder__new();
string_builder__append_char(&sb, '(');
register size_t i;
for (i = 0; i < value->_array.size; ++i) {
va_value_t element = *(va_value_t*)array_checked_get(value->_array, i);
string element_str = va_value_to_string(&element);
string_builder__append(&sb, element_str);
string__free(element_str);
if (i < value->_array.size - 1)
string_builder__append_char(&sb, ' ');
}
string_builder__append_char(&sb, ')');
result = string_builder__to_string(&sb);
string_builder__free(&sb);
break;
}
case VAR_ASSOCIATIVE_ARRAY: {
StringBuilder sb = string_builder__new();
MapIterator it = map_iterator(value->_map);
string_builder__append_char(&sb, '{');
while (map_has_next(&it)) {
const char* key = map_next(&it);
va_value_t* element = map_iterator_get_value(&it, NULL);
string element_str = va_value_to_string(element);
string_builder__append_char(&sb, '[');
string_builder__append_cstr(&sb, key);
string_builder__append_char(&sb, ']');
string_builder__append_char(&sb, '=');
string_builder__append(&sb, element_str);
if (map_has_next(&it))
string_builder__append_char(&sb, ' ');
string__free(element_str);
}
string_builder__append_char(&sb, '}');
result = string_builder__to_string(&sb);
string_builder__free(&sb);
break;
}
default:
result = _SLIT0;
break;
}
return result;
}
static ssize_t identify_value_string_end(const string str) {
ssize_t start = 0;
bool in_quotes = false;
register ssize_t i;
while (start < (ssize_t)str.len && str.str[start] == ' ')
++start;
if (str.str[start] == '"') {
string temp = string__substring(str, start + 1);
ssize_t index = string__indexof(temp, _SLIT("\""));
string__free(temp);
return (index != -1) ? start + index + 2 : -1;
}
if (isdigit(str.str[start])) {
for (i = start; i < (ssize_t)str.len; i++)
if (!isdigit(str.str[i])) return i;
return (ssize_t)str.len;
}
if (str.str[start] == '(') {
for (i = start + 1; i < (ssize_t)str.len; i++) {
if (str.str[i] == '"') in_quotes = !in_quotes;
else if (str.str[i] == ')' && !in_quotes) return i + 1;
}
return -1;
}
if (str.str[start] == '{') {
for (i = start + 1; i < (ssize_t)str.len; i++) {
if (str.str[i] == '"') in_quotes = !in_quotes;
else if (str.str[i] == '}' && !in_quotes) return i + 1;
}
return -1;
}
for (i = start; i < (ssize_t)str.len; i++)
if (str.str[i] != ' ') return i;
return -1;
}
static ssize_t find_key_value_terminator(const string str) {
if (str.str[0] != '[') return -1;
ssize_t keyend = 0;
register size_t i;
for (i = 1; i < str.len; i++) {
if (!isalnum(str.str[i]) && str.str[i] != '_') {
keyend = (ssize_t)i;
break;
}
}
if (str.str[keyend] != ']' || str.str[keyend + 1] != '=') return -1;
string temp = string__substring(str, keyend + 2);
ssize_t value_end = identify_value_string_end(temp);
string__free(temp);
if (value_end == -1) return -1;
return keyend + 3 + value_end;
}
va_value_t string_to_va_value(const string str, VariableType type) {
va_value_t result;
result.type = type;
switch (type) {
case VAR_STRING:
result._str = string__remove_quotes(str);
break;
case VAR_INTEGER: {
long long num;
StrconvResult r = ratoll(str, &num);
if (r.is_err) {
result._number = 0;
print_error(_SLIT("Failed to convert string to integer"));
} else {
result._number = num;
}
break;
}
case VAR_ARRAY: {
result._array = create_array(sizeof(va_value_t));
string trimmed = string__substring(str, 1, (ssize_t)str.len - 1);
string input = string__trim(trimmed);
while (input.len > 0) {
ssize_t last_index = identify_value_string_end(input);
if (last_index == -1) {
print_error(_SLIT("Invalid array format"));
register size_t i;
for (i = 0; i < result._array.size; i++)
free_va_value((va_value_t*)array_checked_get(result._array, i));
array_free(&result._array);
break;
}
string value = string__substring(input, 0, last_index+1);
{
string temp = string__trim(value);
string__free(value);
value = temp;
}
VariableType vt = parse_variable_type(value);
va_value_t new_value = string_to_va_value(value, vt);
array_push(&result._array, &new_value);
string__free(value);
{
string temp = string__substring(input, last_index+1);
string__free(input);
input = temp;
}
}
string__free(input);
string__free(trimmed);
break;
}
case VAR_ASSOCIATIVE_ARRAY: {
result._map = create_map_with_func(vfree_va_value);
string trimmed = string__substring(str, 1, (ssize_t)str.len - 1);
string input = string__trim(trimmed);
while (input.len > 0) {
ssize_t last_index = find_key_value_terminator(input);
if (last_index == -1) {
print_error(_SLIT("Invalid key-value pair format"));
map_free(result._map);
break;
}
ssize_t keyend = string__indexof(input, _SLIT("]"));
string key = string__substring(input, 1, keyend);
string value = string__substring(input, keyend + 2, last_index);
{
string temp = string__trim(value);
string__free(value);
value = temp;
}
VariableType vt = parse_variable_type(value);
va_value_t new_value = string_to_va_value(value, vt);
map_insert(result._map, key.str, &new_value, sizeof(va_value_t));
string__free(key);
string__free(value);
{
string temp = string__substring(input, last_index+1);
string__free(input);
input = temp;
}
}
string__free(input);
string__free(trimmed);
break;
}
default:
result.type = VAR_STRING;
result._str = _SLIT0;
break;
}
return result;
}
void free_va_value(va_value_t* value) {
if (value == NULL) return;
switch (value->type) {
case VAR_STRING:
string__free(value->_str);
break;
case VAR_ARRAY:
register size_t i;
for (i = 0; i < value->_array.size; i++) {
va_value_t* elem = array_checked_get(value->_array, i);
free_va_value(elem);
}
array_free(&value->_array);
break;
case VAR_ASSOCIATIVE_ARRAY:
if (value->_map != NULL)
map_free(value->_map);
break;
case VAR_NAMEREF:
string__free(value->_str);
default:
break;
}
}
void vfree_va_value(void* value) {
free_va_value((va_value_t*)value);
rfree(value);
}
void free_variable(Variable* var) {
if (var == NULL) return;
string__free(var->name);
string__free(var->str);
free_va_value(&var->value);
rfree(var);
}
void cleanup_variables() {
free_variable_table(variable_table);
}
string expand_variables(VariableTable* table, const string input) {
StringBuilder sb = string_builder__new();
ssize_t p = 0;
while (p < (ssize_t)input.len) {
if (input.str[p] == '$') {
if (p + 1 < (ssize_t)input.len && input.str[p + 1] == '{') {
ssize_t end;
{
string temp = string__substring(input, p + 2);
end = string__indexof(temp, _SLIT("}"));
string__free(temp);
}
if (end != -1) {
end += p + 2;
string var_name = string__substring(input, p + 2, end);
string open_bracket = _SLIT("[");
string close_bracket = _SLIT("]");
string colon = _SLIT(":");
string hash = _SLIT("#");
string slash = _SLIT("/");
string percent = _SLIT("%");
string question = _SLIT("?");
string exclamation = _SLIT("!");
string caret = _SLIT("^");
string comma = _SLIT(",");
string at = _SLIT("@");
ssize_t open_bracket_pos = string__indexof(var_name, open_bracket);
ssize_t close_bracket_pos = string__lastindexof(var_name, close_bracket);
ssize_t colon_pos = string__indexof(var_name, colon);
ssize_t hash_pos = string__indexof(var_name, hash);
ssize_t slash_pos = string__indexof(var_name, slash);
ssize_t percent_pos = string__indexof(var_name, percent);
ssize_t question_pos = string__indexof(var_name, question);
ssize_t exclamation_pos = string__indexof(var_name, exclamation);
ssize_t caret_pos = string__indexof(var_name, caret);
ssize_t comma_pos = string__indexof(var_name, comma);
ssize_t at_pos = string__indexof(var_name, at);
if (open_bracket_pos != -1 && close_bracket_pos != -1 && open_bracket_pos < close_bracket_pos) {
string name = string__substring(var_name, 0, open_bracket_pos);
string key = string__substring(var_name, open_bracket_pos + 1, close_bracket_pos);
Variable* var = get_variable(table, name);
if (var && var->value.type == VAR_ASSOCIATIVE_ARRAY) {
va_value_t value;
size_t value_size;
MapResult map_result = map_get(var->value._map, key.str, &value, &value_size);
if (!map_result.is_err) {
string str_value = va_value_to_string(&value);
string_builder__append(&sb, str_value);
string__free(str_value);
}
} else if (var && var->value.type == VAR_ARRAY) {
long long index;
StrconvResult sres = ratoll(key, &index);
if (!sres.is_err) {
if (index >= 0 && index < (long long)var->value._array.size) {
va_value_t* value = array_checked_get(var->value._array, (size_t)index);
string str_value = va_value_to_string(value);
string_builder__append(&sb, str_value);
string__free(str_value);
}
}
}
string__free(name);
string__free(key);
} else if (hash_pos != -1) {
if (var_name.str[0] == '#') {
string vname = string__substring(var_name, hash_pos + 1);
Variable* var = get_variable(table, vname);
string__free(vname);
if (var) {
size_t length = string__length(var->str);
char length_str[20];
snprintf(length_str, sizeof(length_str), "%zu", length);
string_builder__append_cstr(&sb, length_str);
}
} else {
string prefix = string__substring(var_name, 0, hash_pos);
string pattern = string__substring(var_name, hash_pos + 1);
bool is_longest_match = (pattern.str[0] == '#');
if (is_longest_match) {
pattern = string__substring(pattern, 1);
}
Variable* var = get_variable(table, prefix);
if (var) {
string value = string__from(var->str);
string new_value = string__remove_prefix(value, pattern, is_longest_match);
string_builder__append(&sb, new_value);
string__free(value);
string__free(new_value);
}
}
} else if (exclamation_pos != -1) {
string indirect_var_name = string__substring(var_name, 1);
if (indirect_var_name.str[indirect_var_name.len - 1] == '*' || indirect_var_name.str[indirect_var_name.len - 1] == '@') {
{
string temp = string__substring(indirect_var_name, 0, (ssize_t)indirect_var_name.len - 1);
string__free(indirect_var_name);
indirect_var_name = temp;
}
register int i;
for (i = 0; i < table->size; i++) {
string temp = string__from(table->variables[i].name);
if (string__startswith(temp, indirect_var_name)) {
string_builder__append(&sb, temp);
string_builder__append_char(&sb, ' ');
}
string__free(temp);
}
if (sb.len > 0 && sb.buffer[sb.len - 1] == ' ') {
sb.len--;
}
} else {
Variable* indirect_var = get_variable(table, indirect_var_name);
if (indirect_var) {
Variable* target_var = get_variable(table, indirect_var->str);
if (target_var) {
string_builder__append(&sb, target_var->str);
}
}
}
string__free(indirect_var_name);
} else if (caret_pos != -1) {
string name = string__substring(var_name, 0, caret_pos);
string pattern = string__substring(var_name, caret_pos);
Variable* var = get_variable(table, name);
string__free(name);
if (var) {
string value = string__from(var->str);
bool convert_all = (pattern.str[0] == '^' && pattern.len > 1 && pattern.str[1] == '^');
{
string temp = string__substring(pattern, (convert_all) ? 2 : 1);
string__free(pattern);
pattern = temp;
}
register size_t i;
if (pattern.len == 0) {
for (i = 0; i < value.len; i++)
if (convert_all || (!convert_all && i == 0)) value.str[i] = (char)toupper(value.str[i]);
} else {
bool first_found = false;
for (i = 0; i < value.len; i++) {
string temp = string__substring(value, (ssize_t)i, (ssize_t)i+1);
if (string__contains(pattern, temp)) {
if (convert_all || (!convert_all && !first_found)) {
value.str[i] = (char)toupper(value.str[i]);
if (!first_found) first_found = true;
}
}
string__free(temp);
}
}
string_builder__append(&sb, value);
string__free(value);
}
string__free(pattern);
} else if (comma_pos != -1) {
string name = string__substring(var_name, 0, comma_pos);
string pattern = string__substring(var_name, comma_pos);
Variable* var = get_variable(table, name);
string__free(name);
if (var) {
string value = string__from(var->str);
bool convert_all = (pattern.str[0] == ',' && pattern.len > 1 && pattern.str[1] == ',');
{
string temp = string__substring(pattern, (convert_all) ? 2 : 1);
string__free(pattern);
pattern = temp;
}
register size_t i;
if (pattern.len == 0) {
for (i = 0; i < value.len; i++)
if (convert_all || (!convert_all && i == 0)) value.str[i] = (char)tolower(value.str[i]);
} else {
bool first_found = false;
for (i = 0; i < value.len; i++) {
string temp = string__substring(value, (ssize_t)i, (ssize_t)i+1);
if (string__contains(pattern, temp)) {
if (convert_all || (!convert_all && !first_found)) {
value.str[i] = (char)tolower(value.str[i]);
if (!first_found) first_found = true;
}
}
string__free(temp);
}
}
string_builder__append(&sb, value);
string__free(value);
}
string__free(pattern);
} else if (colon_pos != -1 && !(var_name.str[colon_pos + 1] == '?' || var_name.str[colon_pos + 1] == '-' || var_name.str[colon_pos + 1] == '=' || var_name.str[colon_pos + 1] == '+')) {
string name = string__substring(var_name, 0, colon_pos);
string offset_str = string__substring(var_name, colon_pos + 1);
Variable* var = get_variable(table, name);
string__free(name);
if (var) {
long offset;
char* endptr;
offset = strtol(offset_str.str, &endptr, 10);
string _endptr = string__new(endptr);
string length_str = (endptr[0] == ':') ? string__substring(_endptr, 1) : _SLIT0;
string__free(_endptr);
size_t var_len = string__length(var->str);
if (offset < 0) {
offset = (long)var_len + offset;
}
if (length_str.len > 0) {
long length = strtol(length_str.str, NULL, 10);
if (offset >= 0 && length > 0 && offset + length <= (long)var_len) {
string temp = string__substring(var->str, offset, offset + length);
string_builder__append(&sb, temp);
string__free(temp);
}
} else {
if (offset >= 0 && offset < (long)var_len) {
string temp = string__substring(var->str, offset);
string_builder__append(&sb, temp);
string__free(temp);
}
}
string__free(length_str);
}
string__free(offset_str);
} else if (at_pos != -1) {
string name = string__substring(var_name, 0, at_pos);
Variable* var = get_variable(table, name);
string__free(name);
if (var) {
if (var_name.str[at_pos + 1] == 'Q') {
string_builder__append_char(&sb, '\'');
register size_t i;
for (i = 0; i < var->str.len; i++) {
if (var->str.str[i] == '\'') {
string_builder__append_cstr(&sb, "'\\''");
} else {
string_builder__append_char(&sb, var->str.str[i]);
}
}
string_builder__append_char(&sb, '\'');
} else if (var_name.str[at_pos + 1] == 'E') {
print_error(_SLIT("Not implemented"));
}
}
} else if (colon_pos != -1 && (var_name.str[colon_pos + 1] == '-' || var_name.str[colon_pos + 1] == '=' || var_name.str[colon_pos + 1] == '+')) {
string name = string__substring(var_name, 0, colon_pos);
string value = string__substring(var_name, colon_pos + 2);
char operation = var_name.str[colon_pos + 1];
Variable* var = get_variable(table, name);
if (operation == '+') {
if (var && var->str.len > 0) {
string_builder__append(&sb, value);
}
} else {
if (!var || var->str.len == 0) {
if (operation == '=') {
var = set_variable(table, name, value, parse_variable_type(value), false);
string__free(value);
value = string__from(var->str);
}
string_builder__append(&sb, value);
} else {
string_builder__append(&sb, var->str);
}
}
string__free(name);
string__free(value);
} else if (question_pos != -1) {
string name = string__substring(var_name, 0, question_pos);
string error_message = string__substring(var_name, question_pos + 1);
Variable* var = get_variable(table, name);
if (!var || var->str.len == 0) {
string_builder__append(&sb, _SLIT("ERROR: "));
string_builder__append(&sb, error_message);
} else {
string_builder__append(&sb, var->str);
}
string__free(name);
string__free(error_message);
} else if (slash_pos != -1) {
string name = string__substring(var_name, 0, slash_pos);
string pattern = string__substring(var_name, slash_pos + 1);
bool replace_all = (pattern.str[0] == '/');
if (replace_all) {
string temp = string__substring(pattern, 1);
string__free(pattern);
pattern = temp;
}
ssize_t replacement_pos = string__indexof(pattern, slash);
Variable* var = get_variable(table, name);
string__free(name);
if (replacement_pos != -1) {
string replacement = string__substring(pattern, replacement_pos + 1);
{
string temp = string__substring(pattern, 0, replacement_pos);
string__free(pattern);
pattern = temp;
}
if (var) {
string new_value = string__replace_all(var->str, pattern, replacement);
if (!replace_all) {
string temp = string__replace(var->str, pattern, replacement);
string__free(new_value);
new_value = temp;
}
string_builder__append(&sb, new_value);
string__free(new_value);
}
string__free(replacement);
} else {
if (var) {
string temp_result = string__from(var->str);
while (true) {
ssize_t pos = string__indexof(temp_result, pattern);
if (pos == -1) break;
string before = string__substring(temp_result, 0, pos);
string after = string__substring(temp_result, pos + (ssize_t)pattern.len);
string_builder__clear(&sb);
string_builder__append(&sb, before);
string_builder__append(&sb, after);
string__free(before);
string__free(after);
string__free(temp_result);
temp_result = string_builder__to_string(&sb);
string_builder__clear(&sb);
if (!replace_all) break;
}
string_builder__append(&sb, temp_result);
string__free(temp_result);
}
}
string__free(pattern);
} else if (percent_pos != -1) {
string name = string__substring(var_name, 0, percent_pos);
string pattern = string__substring(var_name, percent_pos + 1);
Variable* var = get_variable(table, name);
if (var) {
bool greedy = (pattern.str[0] == '%');
if (greedy) {
string new_pattern = string__substring(pattern, 1);
string__free(pattern);
pattern = new_pattern;
}
string new_value = string__remove_suffix(var->str, pattern, greedy);
string_builder__append(&sb, new_value);
string__free(new_value);
}
string__free(name);
string__free(pattern);
} else {
Variable* var = get_variable(table, var_name);
if (var) {
string_builder__append(&sb, var->str);
}
}
p = end + 1;
string__free(var_name);
continue;
}
} else if (p + 1 < (ssize_t)input.len && input.str[p + 1] == '?') {
char exit_status[20];
snprintf(exit_status, sizeof(exit_status), "%d", WEXITSTATUS(system(NULL)));
string_builder__append_cstr(&sb, exit_status);
p += 2;
continue;
} else if (p + 1 < (ssize_t)input.len && input.str[p + 1] == '!') {
string_builder__append_cstr(&sb, "LAST_BG_PID");
p += 2;
continue;
} else if (p + 1 < (ssize_t)input.len && (isalpha(input.str[p + 1]) || input.str[p + 1] == '_')) {
ssize_t var_start = p + 1;
ssize_t var_end = var_start;
while (var_end < (ssize_t)input.len && (isalnum(input.str[var_end]) || input.str[var_end] == '_')) var_end++;
string var_name = string__substring(input, var_start, var_end);
Variable* var = get_variable(table, var_name);
if (var) {
if (var->value.type == VAR_ARRAY || var->value.type == VAR_ASSOCIATIVE_ARRAY) {
ssize_t array_index_start;
{
string temp = string__substring(input, var_end);
array_index_start = string__indexof(temp, _SLIT("["));
string__free(temp);
}
if (array_index_start != -1) {
array_index_start += var_end;
ssize_t array_index_end;
{
string temp = string__substring(input, array_index_start);
array_index_end = string__indexof(temp, _SLIT("]"));
string__free(temp);
}
if (array_index_end != -1) {
array_index_end += array_index_start;
string index_str = string__substring(input, array_index_start + 1, array_index_end);
string expanded_index = expand_variables(table, index_str);
string__free(index_str);
switch (var->value.type) {
case VAR_ARRAY: {
long long index;