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
|
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <libgen.h>
#include <sys/stat.h>
#include "lutris.h"
#include "net.h"
#define PERCENT_SPACE "%20"
static const struct Command lutris_commands[] = {
{ .name = "info", .func = lutris_info, .description = "show information about a lutris script" },
{ .name = "install", .func = lutris_install, .description = "install a lutris script" },
{ .name = "list", .func = lutris_list, .description = "list installers for a game"},
{ .name = "search", .func = lutris_search, .description = "search for lutris installers" },
};
static const struct Flag lutris_flags[] = {
{ .name = "help", .variant = TWO, .returns = 1, .func = lutris_help, .description = "show this message"},
{ .name = "no-net", .variant = TWO, .returns = 0, .func = set_no_net, .description = "run commands without commitment"}
};
static char* getpwd()
{
char* pwd = malloc(255);
pwd = getcwd(pwd, 255);
return pwd;
}
COMMAND_GROUP_FUNC(lutris)
COMMAND(lutris, search)
{
if (argc == 2 && argv[1][0] != '\0')
{
// argv being modifyable is not always a given so lets
// lets make a mutable copy
char* str = strdup(argv[1]);
if (!str) return 1;
// in the case we need to do replacing we allocate more
// since we'll free in anyway
// the smallest strlen can return is 0 the longest
// escapeString can return is strlen*3
size_t allocsize = strlen(str) * 3;
str = realloc(str, allocsize);
if (!str) return 1;
lutris_escapeString(str, allocsize);
char* url = malloc(strlen(GAME_SEARCH_API) + strlen(str));
sprintf(url, GAME_SEARCH_API, str);
struct json_object* queryresult = fetchJSON(url);
free(str);
free(url);
if (queryresult)
{
struct json_object* searchresults;
json_object_object_get_ex(queryresult, "results", &searchresults);
if (searchresults)
{
struct json_object* values[2];
size_t arrlen = json_object_array_length(searchresults);
for (size_t i = 0; i < arrlen; ++i)
{
struct json_object* entry = json_object_array_get_idx(searchresults, i);
json_object_object_get_ex(entry, "name", &values[0]);
json_object_object_get_ex(entry, "slug", &values[1]);
printf("%s - %s\n", json_object_get_string(values[0]), json_object_get_string(values[1]));
}
}
json_object_put(queryresult);
}
}
else
{
puts(USAGE_STR " lutris search <text>\nSearch for games and list their slug");
}
return EXIT_SUCCESS;
}
COMMAND(lutris, list)
{
if (argc == 2)
{
// argv being modifyable is not always a given so lets
// lets make a mutable copy
char* str = strdup(argv[1]);
if (!str) return 1;
// in the case we need to do replacing we allocate more
// since we'll free in anyway
// the smallest strlen can return is 0 the longest
// escapeString can return is strlen*3
size_t allocsize = strlen(str) * 3;
str = realloc(str, allocsize);
if (!str) return 1;
lutris_escapeString(str, allocsize);
char* url = malloc(strlen(GAME_INSTALLER_API) + strlen(str));
sprintf(url, GAME_INSTALLER_API, str);
struct json_object* queryresult = fetchJSON(url);
free(str);
free(url);
if (queryresult)
{
struct json_object* installers;
json_object_object_get_ex(queryresult, "installers", &installers);
if (installers)
{
struct json_object* values[4];
size_t arrlen = json_object_array_length(installers);
for (size_t i = 0; i < arrlen; ++i)
{
struct json_object* entry = json_object_array_get_idx(installers, i);
json_object_object_get_ex(entry, "slug", &values[0]);
json_object_object_get_ex(entry, "runner", &values[1]);
json_object_object_get_ex(entry, "name", &values[2]);
json_object_object_get_ex(entry, "version", &values[3]);
printf("%s - (%s) %s %s\n", json_object_get_string(values[0]), json_object_get_string(values[1]), json_object_get_string(values[2]), json_object_get_string(values[3]));
}
}
json_object_put(queryresult);
}
}
else
{
puts(USAGE_STR " lutris list <slug>\nList game installers");
}
return EXIT_SUCCESS;
}
COMMAND(lutris, install)
{
if (argc == 2)
{
struct script_t installer = lutris_getInstaller(argv[1]);
if (installer.error == NONE)
{
printf("Install %s - %s to the current directory?\n\nNOTE:\tpolecat will use the wine that is on your path\n\t\tit is suggested to install the right wine version\n\t\tand use `wine env` to put it on the path\n\n(y/N)\n", installer.name, installer.version);
int inp = getchar();
if (inp == 'y' || inp == 'Y')
{
// fetch all files required by installer
for (size_t i = 0; i < installer.filecount; ++i)
{
char* filename = basename(installer.files[i]->url);
if (isFile(filename))
{
printf("%s found. Not Downloading\n", filename);
}
else
{
printf("Dowloading %s\n", filename);
downloadToFile(installer.files[i]->url, filename, 1);
}
}
for (size_t i = 0; i < installer.directivecount; ++i)
{
switch(installer.directives[i]->command)
{
case move:
// TODO
break;
case merge:
// TODO
break;
case extract:
// TODO
break;
case copy:
// TODO
break;
case chmodx:
chmod(installer.directives[i]->arguments[0], S_IXUSR);
break;
case execute:
// TODO
break;
case write_file:
case write_json:
// TODO
break;
case write_config:
// TODO
break;
case input_menu:
installer.variablecount++;
installer.variables = realloc(installer.variables, installer.variablecount * sizeof(void*));
installer.variables[installer.variablecount-1] = malloc(sizeof(struct list_t));
installer.variables[installer.variablecount-1]->key = installer.directives[i]->arguments[0];
installer.variables[installer.variablecount-1]->type = value_string;
installer.variables[installer.variablecount-1]->value.str = installer.directives[i]->arguments[1];
break;
case insert_disc:
// TODO
break;
case task:
parseVar(&installer.directives[i]->arguments[0], installer.variables, installer.variablecount);
#ifndef _WIN32 // TODO
setenv("WINEPREFIX", installer.directives[i]->arguments[0], 1);
#endif
switch(installer.directives[i]->task)
{
case wineexec:
printf("WINEEXEC\n");
break;
case winetricks:
printf("WINETRICKS\n");
break;
case create_prefix:
printf("CREATE_PREFIX\n");
#ifndef _WIN32 // TODO
setenv("WINEDEBUG", "-all", 1);
#endif
system("wineboot");
#ifndef _WIN32 // TODO
unsetenv("WINEDEBUG");
#endif
break;
case set_regedit:
printf("SET_REGEDIT\n");
break;
case winekill:
printf("WINEKILL\n");
break;
case TASKKEYWORDMAX:
UNREACHABLE
case no_task:
break;
case unknown_task:
printf("Unknown Task\n");
break;
}
break;
case unknown_keyword:
puts("Unknown directive\nIf you see this please report it with your system information and steps to reproduce this.");
break;
default:
UNREACHABLE
break;
}
}
}
}
else
{
switch(installer.error)
{
case NO_JSON:
case NO_SLUG:
puts("No Installer with that ID was found");
break;
case NO_SCRIPT:
puts("Installer has no script");
break;
case NO_INSTALLER:
puts("Script has no install directives");
break;
default:
UNREACHABLE
break;
}
}
lutris_freeInstaller(&installer);
}
return EXIT_SUCCESS;
}
COMMAND(lutris, info)
{
if (argc == 2)
{
struct script_t installer = lutris_getInstaller(argv[1]);
if (installer.error > NO_SLUG || installer.error == NONE)
{
printf("[%s]", runnerStr[installer.runner]);
if (installer.wine) printf("[%s]", installer.wine);
printf(" %s - %s\n", installer.name, installer.version);
if (installer.description) puts(installer.description);
if (installer.notes) puts(installer.notes);
if (installer.variablecount)
{
puts("\nVariables:");
for (size_t i = 0; i < installer.variablecount; ++i)
{
printf("\t%s -> ", installer.variables[i]->key);
switch (installer.variables[i]->type)
{
case value_function:
{
char* out = installer.variables[i]->value.func();
puts(out);
free(out);
}
break;
case value_string:
puts(installer.variables[i]->value.str);
break;
}
}
}
if (installer.filecount)
{
puts("\nFiles:");
for (size_t i = 0; i < installer.filecount; ++i)
{
printf("\t%s -> %s\n", installer.files[i]->name, installer.files[i]->url);
}
}
if (installer.directivecount)
{
puts("\nDirectives:");
for (size_t i = 0; i < installer.directivecount; ++i)
{
printf("\t%s", keywordstr[installer.directives[i]->command]);
if (installer.directives[i]->task != no_task) printf(" %s", taskKeywordstr[installer.directives[i]->task]);
for (size_t j = 0; j < installer.directives[i]->size; ++j)
{
printf(" %s", installer.directives[i]->arguments[j]);
}
puts("");
}
}
}
else
{
printf("Couldn't find an installer by the name '%s'\n", argv[1]);
}
lutris_freeInstaller(&installer);
}
else
{
puts(USAGE_STR " lutris info <installer-name>\nThe Name of an installer can be found by viewing the install script of an installer");
}
return EXIT_SUCCESS;
}
COMMAND_HELP(lutris, " lutris")
void lutris_escapeString(char* str, size_t size)
{
char* tail = str + size;
while (str != tail)
{
switch (*str)
{
// from rfc3986 https://tools.ietf.org/html/rfc3986#page-13
// we generally don't want delimiters
// so we strip them if found
case ':':
case '/':
case '?':
case '#':
case '[':
case ']':
case '@':
case '!':
case '$':
case '&':
case '\'':
case '(':
case ')':
case '*':
case '+':
case ',':
case ';':
case '=':
// since dest and src overlap we need a non overlapping solution
memmove(str, str+1, (size_t)(tail-str));
break;
case ' ':
// spaces are special and need to be encoded
memmove(str+3, str+1, (size_t)(tail-str-3));
memcpy(str, PERCENT_SPACE, strlen(PERCENT_SPACE));
str += 3;
if (str >= tail) str[3] = '\0';
break;
/* fallthrough */
default:
++str;
}
}
}
struct script_t lutris_getInstaller(char* installername)
{
struct script_t installer;
installer.name = NULL;
installer.version = NULL;
installer.runner = unknown_runner;
installer.description = NULL;
installer.notes = NULL;
installer.wine = NULL;
installer.directives = NULL;
installer.directivecount = 0;
installer.files = NULL;
installer.filecount = 0;
installer.error = NONE;
installer.variables = NULL;
installer.variablecount = 0;
if (installername)
{
char installerurl[PATH_MAX];
strncpy(installerurl, INSTALLER_API, sizeof(installerurl));
strncat(installerurl, installername, sizeof(installerurl) - strlen(installerurl) - 1);
struct json_object* installerjson = fetchJSON(installerurl);
if (installerjson)
{
struct json_object* count, *results, *slug;
json_object_object_get_ex(installerjson, "count", &count);
json_object_object_get_ex(installerjson, "results", &results);
slug = json_object_array_get_idx(results, 0);
if (json_object_get_int(count) == 1)
{
struct json_object* script, *scriptinstall, *files;
if(json_object_object_get_ex(slug, "script", &script))
{
{
struct json_object* name, *version, *runner, *description, *notes, *winever;
const char* namestr, *versionstr, *runnerstr, *descriptionstr, *notesstr, *winestr;
json_object_object_get_ex(slug, "name", &name);
namestr = json_object_get_string(name);
installer.name = malloc( strlen(namestr) * sizeof(char) +1 );
strcpy(installer.name, namestr);
json_object_object_get_ex(slug, "version", &version);
versionstr = json_object_get_string(version);
installer.version = malloc( strlen(versionstr) * sizeof(char) +1 );
strcpy(installer.version, versionstr);
json_object_object_get_ex(slug, "runner", &runner);
runnerstr = json_object_get_string(runner);
for (int i = 0; i < RUNNERMAX; ++i)
{
if(!strcmp(runnerstr, runnerStr[i]))
{
installer.runner = (enum runner_t)i;
break;
}
}
json_object_object_get_ex(slug, "description", &description);
if (description)
{
descriptionstr = json_object_get_string(description);
installer.description = malloc( strlen(descriptionstr) * sizeof(char) +1 );
strcpy(installer.description, descriptionstr);
}
json_object_object_get_ex(slug, "notes", ¬es);
if (notes)
{
notesstr = json_object_get_string(notes);
installer.notes = malloc( strlen(notesstr) * sizeof(char) +1 );
strcpy(installer.notes, notesstr);
}
json_object_object_get_ex(script, "wine", &winever);
json_object_object_get_ex(winever, "version", &winever);
if (winever)
{
winestr = json_object_get_string(winever);
installer.wine = malloc( strlen(winestr) * sizeof(char) +1 );
strcpy(installer.wine, winestr);
}
}
if (json_object_object_get_ex(script, "files", &files))
{
installer.filecount = json_object_array_length(files);
// we need to assign variables for all files and for the GAMEDIR
installer.variablecount = installer.filecount + 1;
installer.files = malloc(installer.filecount * sizeof(void*));
installer.variables = malloc(installer.variablecount * sizeof(void*));
for (size_t i = 0; i < installer.filecount; ++i)
{
struct json_object* file = json_object_array_get_idx(files, i);
struct lh_entry* entry = json_object_get_object(file)->head;
installer.files[i] = malloc(sizeof(struct file_t));
{
size_t namelen = strlen((char*)entry->k);
installer.files[i]->name = malloc(namelen * sizeof(char) +1);
strcpy(installer.files[i]->name, (char*)entry->k);
}
const char* urlstr;
if(json_object_get_type((struct json_object*)entry->v) == json_type_object)
{
struct json_object* url;
json_object_object_get_ex((struct json_object*)entry->v, "url", &url);
urlstr = json_object_get_string(url);
}
else
{
urlstr = json_object_get_string((struct json_object*)entry->v);
}
{
size_t urllen = strlen(urlstr);
installer.files[i]->url = malloc(urllen * sizeof(char) +1);
strcpy(installer.files[i]->url, urlstr);
}
installer.variables[i] = malloc(sizeof(struct list_t));
installer.variables[i]->key = installer.files[i]->name;
installer.variables[i]->type = value_string;
installer.variables[i]->value.str = basename(installer.files[i]->url);
}
installer.variables[installer.variablecount-1] = malloc(sizeof(struct list_t));
installer.variables[installer.variablecount-1]->key = "GAMEDIR";
installer.variables[installer.variablecount-1]->type = value_function;
installer.variables[installer.variablecount-1]->value.func = getpwd;
}
if (json_object_object_get_ex(script, "installer", &scriptinstall))
{
installer.directivecount = json_object_array_length(scriptinstall);
installer.directives = malloc(installer.directivecount * sizeof(void*));
for (size_t i = 0; i < installer.directivecount; ++i)
{
struct json_object* step = json_object_array_get_idx(scriptinstall, i);
struct json_object* directive;
installer.directives[i] = malloc(sizeof(struct directive_t));
installer.directives[i]->size = 0;
installer.directives[i]->command = unknown_keyword;
installer.directives[i]->task = no_task;
for (int l = 0; l < KEYWORDMAX; ++l)
{
if (json_object_object_get_ex(step, keywordstr[l], &directive))
{
struct json_object* options[5];
switch (l)
{
case move:
case copy:
case merge:
json_object_object_get_ex(directive, "src", &options[0]);
json_object_object_get_ex(directive, "dst", &options[1]);
installer.directives[i]->size = 2;
break;
case extract:
json_object_object_get_ex(directive, "file", &options[0]);
installer.directives[i]->size = 1;
break;
case chmodx:
options[0] = directive;
installer.directives[i]->size = 1;
break;
case execute:
if(!json_object_object_get_ex(directive, "command", &options[0])) json_object_object_get_ex(directive, "file", &options[0]);
installer.directives[i]->size = 1;
break;
case write_file:
json_object_object_get_ex(directive, "file", &options[0]);
json_object_object_get_ex(directive, "content", &options[1]);
installer.directives[i]->size = 2;
break;
case write_config:
json_object_object_get_ex(directive, "file", &options[0]);
json_object_object_get_ex(directive, "section", &options[1]);
json_object_object_get_ex(directive, "key", &options[2]);
json_object_object_get_ex(directive, "value", &options[3]);
installer.directives[i]->size = 4;
break;
case write_json:
json_object_object_get_ex(directive, "file", &options[0]);
json_object_object_get_ex(directive, "data", &options[1]);
installer.directives[i]->size = 2;
break;
case input_menu:
json_object_object_get_ex(directive, "id", &options[0]);
json_object_object_get_ex(directive, "preselect", &options[1]);
json_object_object_get_ex(directive, "description", &options[2]);
installer.directives[i]->size = 3;
break;
case insert_disc:
json_object_object_get_ex(directive, "requires", &options[0]);
installer.directives[i]->size = 1;
break;
case task:
json_object_object_get_ex(directive, "name", &options[0]);
const char* name = json_object_get_string(options[0]);
for (int k = 0; k < TASKKEYWORDMAX; ++k)
{
if (!strcmp(name, taskKeywordstr[k]))
{
switch(k)
{
case wineexec:
json_object_object_get_ex(directive, "prefix", &options[1]);
json_object_object_get_ex(directive, "executable", &options[2]);
installer.directives[i]->size = 2;
break;
case winetricks:
json_object_object_get_ex(directive, "prefix", &options[1]);
json_object_object_get_ex(directive, "app", &options[2]);
installer.directives[i]->size = 2;
break;
case create_prefix:
case winekill:
json_object_object_get_ex(directive, "prefix", &options[1]);
installer.directives[i]->size = 1;
break;
case set_regedit:
json_object_object_get_ex(directive, "prefix", &options[1]);
json_object_object_get_ex(directive, "path", &options[2]);
json_object_object_get_ex(directive, "key", &options[3]);
json_object_object_get_ex(directive, "value", &options[4]);
installer.directives[i]->size = 4;
break;
}
installer.directives[i]->task = (enum task_t)k;
break;
}
}
break;
}
installer.directives[i]->command = (enum keyword_t)l;
const char* str;
uint8_t offset = 0;
if (installer.directives[i]->task != no_task)
{
offset = 1;
}
installer.directives[i]->arguments = malloc(installer.directives[i]->size * sizeof(char*));
for (size_t j = 0; j < installer.directives[i]->size; ++j)
{
str = json_object_get_string(options[j+offset]);
// there will always be some attributes ommited
// from an installer, just replace those with an empty string
if (!str) str = "";
installer.directives[i]->arguments[j] = malloc(strlen(str) * sizeof(char) +1);
strcpy(installer.directives[i]->arguments[j], str);
}
break;
}
}
}
}
else installer.error = NO_INSTALLER;
}
else installer.error = NO_SCRIPT;
}
else installer.error = NO_SLUG;
json_object_put(installerjson);
}
else installer.error = NO_JSON;
}
return installer;
}
void lutris_freeInstaller(struct script_t* installer)
{
if (installer)
{
free(installer->name);
free(installer->version);
free(installer->description);
free(installer->notes);
free(installer->wine);
if (installer->directives)
{
for (size_t i = 0; i < installer->directivecount; ++i)
{
for (size_t j = 0; j < installer->directives[i]->size; ++j)
{
free(installer->directives[i]->arguments[j]);
}
free(installer->directives[i]->arguments);
free(installer->directives[i]);
}
free(installer->directives);
}
if (installer->files)
{
for (size_t i = 0; i < installer->filecount; ++i)
{
free(installer->files[i]->name);
free(installer->files[i]->url);
free(installer->files[i]);
}
free(installer->files);
}
if (installer->variables)
{
for (size_t i = 0; i < installer->variablecount; ++i)
{
free(installer->variables[i]);
}
free(installer->variables);
}
}
}
size_t parseVar(char** pvar, struct list_t** variables, size_t variable_count)
{
char* var = *pvar;
char* head = var;
char* tail, *end;
char* buf, *value;
size_t offset;
size_t keylen, backlen;
size_t varcount = 0;
while (*head != '\0')
{
if (*head == VARIABLESIGN)
{
// ensure sanity by clearing variables
buf = NULL;
value = NULL;
end = strlen(var) + var;
offset = (size_t)(head-var);
// find variable key
tail = ++head;
while (*tail <= 'z' && *tail >= 'A') // we ONLY accept ascii variables
{
++tail;
}
// setup variables for reuse
keylen = (size_t)(tail-head);
backlen = (size_t)(end-tail);
if (keylen > 0)
{
// resolve variable key and store it
buf = malloc(keylen+1);
strncpy(buf, head, keylen+1);
buf[keylen] = '\0';
for (size_t i = 0; i < variable_count; ++i)
{
if (strncmp(variables[i]->key, buf, keylen+1) == 0)
{
switch (variables[i]->type)
{
case value_string:
value = strdup(variables[i]->value.str);
break;
case value_function:
value = variables[i]->value.func();
break;
default:
UNREACHABLE
break;
}
}
}
free(buf);
}
if (!value) continue;
// copy everything from variable key end to end of string
buf = malloc(backlen+1);
strncpy(buf, tail, backlen+1);
size_t varsize = offset + strlen(value) + backlen + 1;
var = realloc(var, varsize);
// end of the string up until the variable
// we cannot fetch this after the realloc because it points to completly different memory making pointer math impossible
var[offset] = '\0';
strncat(var, value, varsize - strlen(var) - 1);
strncat(var, buf, varsize - strlen(var) - 1);
// cleanup
free(value);
free(buf);
varcount++;
// bring your head back to the world of living memory
head = var + offset;
}
head++;
}
// point pvar back to the string
*pvar = var;
return varcount;
}
|