-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_tools.c
1220 lines (1161 loc) · 34.2 KB
/
cmd_tools.c
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
/*
Command prompt for DeaDBeeF
Copyright (C) 2018 Jakub Wasylków <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <deadbeef/deadbeef.h>
#include "cmd_tools.h"
#include "common.h"
extern DB_functions_t *deadbeef;
// default argv size, will be reallocated if needed
#define ARGV_MAX 16
// default property size, will be reallocated if needed
#define PROPERTIES_MAX 8
char ** temp_table = 0;
// cmd_tab_complete functions try to complete last argv argument
// it is best to call these functions from macros
// tab-complete after table
char * cmd_tab_complete_table (const char **table, char **argv, int iter) {
int argc;
for (argc = 0; argv[argc] != NULL; argc++);
int quotesc = 0;
{
int i;
for (i = 0; argv[argc-1][i]; i++) {
if (argv[argc-1][i] == '\"')
quotesc++;
}
}
if (quotesc >= 2) {
if (iter == 0) {
return strdup ("");
}
else
return NULL;
}
char *to_extend = strdup_unescaped(argv[argc-1]);// malloc (16 + strlen(argv[i-1]));
int to_extend_spaces = 0;
int i;
for (i = 0; to_extend[i]; i++) {
if (to_extend[i] == ' ')
to_extend_spaces++;
}
// Try to detect if there are names which include space, if that's the case put every option in quotes
// sometimes it's useful, sometimes not
int enquoteme = 0;
{
if (strlen(to_extend) > 0) {
for (i = 0; table[i] != NULL; i++) {
if (strncmp (table[i], to_extend, strlen(to_extend)) == 0 && has_spaces(table[i])) {
enquoteme = 1;
break;
}
}
}
}
int j = 0;
for (i = 0; table[i] != NULL; i++) {
if (strncmp (table[i], to_extend, strlen(to_extend)) == 0) {
if (iter == j) {
free (to_extend);
char *ret = 0;
if (enquoteme)
ret = strdup_quoted (table[i]);
else
ret = strdup_escaped (table[i]);
return ret;
}
else {
j++;
}
}
}
free (to_extend);
return NULL;
}
// tab-complete playlists
char * cmd_tab_complete_playlists (char **argv, int iter) {
if (iter == 0) {
int count = deadbeef->plt_get_count ();
temp_table = malloc ((count+1) * sizeof(char *));
int i;
ddb_playlist_t *plt;
char buffer[256];
for (i = 0; 1; i++) {
plt = deadbeef->plt_get_for_idx (i);
if (!plt) {
break;
}
deadbeef->plt_get_title (plt, buffer, 256);
deadbeef->plt_unref (plt);
temp_table[i] = strdup (buffer);
}
temp_table[i] = NULL;
}
// artist table generated;
char *ret = cmd_tab_complete_table ((const char **)temp_table, argv, iter);
if (!ret) {
int i;
for (i = 0; temp_table[i]; i++) {
free (temp_table[i]);
}
free (temp_table);
temp_table = 0;
}
return ret;
}
// tab completion after ATA (artist/title/album)
char * cmd_tab_complete_ata (char * artist_o, char * title_o, char * album_o, char ** argv, int iter) {
if (iter == 0) {
ddb_playlist_t *plt = deadbeef->plt_get_curr();
if (!plt) {
return NULL;
}
int it_count = deadbeef->plt_get_item_count (plt, PL_MAIN);
if (!it_count) {
return NULL;
}
// find what we are looking for
int argc;
for (argc = 0; argv[argc]; argc++);
char *looking_for = 0;
// one of X_o should have same adress as argv[argc-1]
if (artist_o == argv[argc-1]) {
looking_for = "artist";
}
else if (title_o == argv[argc-1]) {
looking_for = "title";
}
else if (album_o == argv[argc-1]) {
looking_for = "album";
}
else {
return NULL;
}
temp_table = malloc ((it_count+1) * sizeof(char *));
int i;
int j = 0;
char * artist = 0;
char * title = 0;
char * album = 0;
if (artist_o && strcmp (artist_o, "*"))
artist = strdup_unescaped (artist_o);
if (title_o && strcmp (title_o, "*"))
title = strdup_unescaped (title_o);
if (album_o && strcmp (album_o, "*"))
album = strdup_unescaped (album_o);
// search if it already exists
int required = 0;
artist ? required++ : 0;
title ? required++ : 0;
album ? required++ : 0;
required--;
DB_metaInfo_t* meta;
DB_playItem_t *item;
for (i = 0; 1; i++) {
item = deadbeef->pl_get_for_idx (i);
if (!item) {
break;
}
int matches = 0;
//
if (strcmp (looking_for, "artist")) {
meta = deadbeef->pl_meta_for_key (item, "artist");
if (artist && meta) {
if (strcmp (artist,meta->value) == 0) {
matches++;
}
}
}
if (strcmp (looking_for, "title")) {
meta = deadbeef->pl_meta_for_key (item, "title");
if (title && meta) {
if (strcmp (title,meta->value) == 0) {
matches++;
}
}
}
if (strcmp (looking_for, "album")) {
meta = deadbeef->pl_meta_for_key (item, "album");
if (album && meta) {
if (strcmp (album,meta->value) == 0) {
matches++;
}
}
}
meta = deadbeef->pl_meta_for_key (item, looking_for);
if (matches >= required && meta) {
// search if it already exists
int found = 0;
{
int h;
for (h = 0; h < j; h++) {
if (strcmp (temp_table[h], meta->value) == 0) {
found = 1;
break;
}
}
}
if (!found) {
temp_table[j] = strdup (meta->value);
j++;
}
}
deadbeef->pl_item_unref (item);
}
temp_table[j] = NULL;
deadbeef->plt_unref (plt);
// realloc back
if (j+1 < it_count)
temp_table = realloc (temp_table, (j+1) * sizeof (char *));
}
// artist table generated;
char *ret = cmd_tab_complete_table ((const char **)temp_table, argv, iter);
if (!ret) {
int i;
for (i = 0; temp_table[i]; i++) {
free (temp_table[i]);
}
free (temp_table);
temp_table = 0;
}
return ret;
}
// tab completion after metadata
char * cmd_tab_complete_meta (char * meta_s, char **argv, int iter) {
if (iter == 0) {
ddb_playlist_t *plt = deadbeef->plt_get_curr();
if (!plt) {
return NULL;
}
int it_count = deadbeef->plt_get_item_count (plt, PL_MAIN);
if (!it_count) {
return NULL;
}
temp_table = malloc ((it_count+1) * sizeof(char *));
int i;
int j = 0;
DB_metaInfo_t* meta;
DB_playItem_t *item;
for (i = 0; 1; i++) {
item = deadbeef->pl_get_for_idx (i);
if (!item) {
break;
}
meta = deadbeef->pl_meta_for_key (item, meta_s);
if (!meta) {
deadbeef->pl_item_unref (item);
continue;
}
// search if it already exists
int found = 0;
{
int h;
for (h = 0; h < j; h++) {
if (strcmp (temp_table[h], meta->value) == 0) {
found = 1;
break;
}
}
}
if (!found) {
temp_table[j] = strdup (meta->value);
j++;
}
deadbeef->pl_item_unref (item);
}
temp_table[j] = NULL;
deadbeef->plt_unref (plt);
// realloc back
if (i < it_count)
temp_table = realloc (temp_table, (i+1) * sizeof (char *));
}
// artist table generated;
char *ret = cmd_tab_complete_table ((const char **)temp_table, argv, iter);
if (!ret) {
int i;
for (i = 0; temp_table[i]; i++) {
free (temp_table[i]);
}
free (temp_table);
temp_table = 0;
}
return ret;
}
// tab-complete after properties
char * cmd_tab_complete_properties (struct property **table, char **argv, int iter) {
int i;
int o = 0;
int size = properties_count(table)+1;
const char * strings[size];
for (i = 0; table[i]; i++) {
if (i >= size) {
break;
}
if (is_property_available (table[i])) {
strings[o] = table[i]->key;
o++;
}
}
strings[o] = NULL;
return cmd_tab_complete_table (strings, argv, iter);
}
// get item based on data (artist/album/title and playlist)
DB_playItem_t* cmd_get_item (ddb_playlist_t *playlist, char * artist_o, char * album_o, char * title_o) {
ddb_playlist_t *plt;
if (!playlist) {
plt = deadbeef->plt_get_curr();
}
else {
plt = playlist;
}
if (!plt) {
return NULL;
}
int it_count = deadbeef->plt_get_item_count (plt, PL_MAIN);
if (!it_count) {
return NULL;
}
char * artist = 0;
char * title = 0;
char * album = 0;
if (artist_o && strcmp (artist_o, "*"))
artist = strdup_unescaped (artist_o);
if (title_o && strcmp (title_o, "*"))
title = strdup_unescaped (title_o);
if (album_o && strcmp (album_o, "*"))
album = strdup_unescaped (album_o);
int i;
DB_metaInfo_t* meta;
DB_playItem_t *item;
for (i = 0; 1; i++) {
item = deadbeef->plt_get_item_for_idx (plt, i, PL_MAIN);
if (!item) {
break;
}
// search if it already exists
int matches = 0;
int required = 0;
artist ? required++ : 0;
title ? required++ : 0;
album ? required++ : 0;
if (!required) {
break;
}
{
meta = deadbeef->pl_meta_for_key (item, "artist");
if (artist && meta) {
if (strcmp(artist,meta->value) == 0) {
matches++;
}
}
meta = deadbeef->pl_meta_for_key (item, "title");
if (title && meta) {
if (strcmp(title,meta->value) == 0) {
matches++;
}
}
meta = deadbeef->pl_meta_for_key (item, "album");
if (album && meta) {
if (strcmp(album,meta->value) == 0) {
matches++;
}
}
}
if (matches >= required) {
if (!playlist) {
deadbeef->plt_unref (plt);
}
free (artist);
free (album);
free (title);
return item;
}
else {
deadbeef->pl_item_unref (item);
}
}
free (artist);
free (album);
free (title);
return NULL;
}
// get playlist from escaped string after name
ddb_playlist_t* cmd_get_playlist (char * name) {
char * string = strdup_unescaped (name);
int i = 0;
ddb_playlist_t * plt;
char buffer[256];
for (i = 0;(plt = deadbeef->plt_get_for_idx (i)); i++) {
if (!plt) {
break;
}
deadbeef->plt_get_title (plt, buffer, 256);
if (strcmp(string, buffer) == 0) {
free (string);
return plt;
}
deadbeef->plt_unref (plt);
}
free (string);
return NULL;
}
// detect spaces in string
int has_spaces (const char * string) {
int contains_spaces = 0;
int i;
for (i = 0; string[i]; i++) {
if (string[i] == ' ') {
contains_spaces = 1;
break;
}
}
return contains_spaces;
}
// enquote string (put in "") and duplicate
char * strdup_quoted (const char * string) {
int contains_spaces = 1;
int i;
for (i = 0; string[i]; i++) {
if (string[i] == ' ') {
contains_spaces = 1;
break;
}
}
char * ptr;
if (contains_spaces) {
ptr = malloc (strlen(string) + 1 + 2);
ptr[0] = '\"'; ptr[1] = 0;
strcat (ptr, string);
strcat (ptr, "\"");
}
else {
ptr = strdup (string);
}
return ptr;
}
// escape string (put in "") if needed and duplicate
char * strdup_escaped (const char * string) {
int contains_spaces = 0;
int i;
for (i = 0; string[i]; i++) {
if (string[i] == ' ') {
contains_spaces = 1;
break;
}
}
char * ptr;
if (contains_spaces) {
ptr = malloc (strlen(string) + 1 + 2 + 6);
ptr[0] = '\"'; ptr[1] = 0;
strcat (ptr, string);
strcat (ptr, "\"");
}
else {
ptr = strdup (string);
}
return ptr;
}
// remove "" from string and duplicate
char * strdup_unescaped (const char * string) {
int contains_app = 0;
int i;
for (i = 0; string[i]; i++) {
if (string[i] == '\"') {
contains_app = 1;
break;
}
}
char * ptr;
if (contains_app) {
ptr = malloc (strlen(string));
int i;
int j = 0;
int ap_count = 0;
for (i = 0; string[i]; i++) {
if (string[i] == '\"' && (i == 0 || string[i+1] == 0)) {
ap_count++;
continue;
}
else {
ptr[j++] = string[i];
}
}
ptr[j] = 0;
}
else {
ptr = strdup (string);
}
return ptr;
}
// ARGV
int argv_count (char **argv) {
int i;
for (i = 0; argv[i] != NULL; i++);
return i;
}
char ** argv_alloc (char * cmd) {
// build argv list
char * pch;
int argv_size = ARGV_MAX;
char ** argv = malloc (argv_size * sizeof(char *));
if (!cmd) {
argv[0] = NULL;
return argv;
}
char buffer[strlen(cmd)+1];
strcpy (buffer, cmd);
int vi = 0;
// quotes_multi is some kind of hack which allows to have quotes inside quotes (only on begin)
int quotes_multi = 0;
#define SPECIAL_CHAR 27
{
int q;
int spec_mode_on = 0;
for (q = 0; buffer[q] != 0; q++) {
if (buffer[q] == '\"') {
if (buffer[q+1] == '\"' && quotes_multi == 0) {
quotes_multi = 3;
spec_mode_on = 1;
continue;
}//
if (quotes_multi > 0) {
quotes_multi--;
if (!quotes_multi)
spec_mode_on = 0;
continue;
}
spec_mode_on = !spec_mode_on;
continue;
}
if (spec_mode_on && buffer[q] == ' ') {
buffer[q] = SPECIAL_CHAR;
}
}
if (spec_mode_on) {
// need to support incomplete names for tab completion
//free (argv);
//return NULL;
}
}
pch = strtok (buffer," ");
while (pch != NULL) {
argv[vi] = strdup (pch);
vi++;
if (vi >= argv_size) {
argv_size *= 2;
argv = realloc (argv, argv_size * sizeof(char *));
if (!argv) {
printf ("Failed to realloc memory!\n");
return NULL;
}
}
pch = strtok (NULL, " ");
}
argv[vi] = NULL;
{
int i;
for (i = 0; argv[i] != NULL; i++) {
// for every char
int j;
for (j = 0; argv[i][j] != 0; j++) {
if (argv[i][j] == SPECIAL_CHAR)
argv[i][j] = ' ';
}
}
}
return argv;
}
void argv_free (char ** argv) {
int i;
for (i = 0; argv[i] != NULL; i++)
free (argv[i]);
free (argv);
return;
}
int argv_cat (char ** to, char ** from) {
int i;
for (i = 0; to[i] != NULL; i++);
int j;
for (j = 0; from[j] != NULL; j++) {
to[i] = strdup (from[j]);
i++;
}
to[i] = NULL;
return 0;
}
// Properties
int properties_count (property_t **properties) {
int i;
for (i = 0; properties[i] != NULL; i++);
return i;
}
property_t * property_get (property_t **properties, const char * key) {
int i;
for (i = 0; properties[i] != NULL; i++) {
if (strcmp(properties[i]->key, key) == 0) {
property_update (properties[i]);
return properties[i];
}
}
// try num
char * endptr;
int num = strtol (key, &endptr, 10);
if (endptr != key && num < i) {
property_update (properties[num]);
return properties[num];
}
return NULL;
}
int property_set (property_t *property, const char * value) {
// todo error checking
printf ("property_set called but has no error checking!\n");
// todo evaluate prop_curr->type_string
// char *config_argv[] = {"config_i", argv[3], "string", argv[4], NULL };
//settings_config (4, config_argv, -1);
if (is_property_available (property) == 0) {
property_t *need = property_requires (property);
printf ("Property requires %s to be enabled!\n", need->key);
return -1;
}
if (property->type == TYPE_SELECT || property->type == TYPE_SELECT_S) {
char *value_unescaped = strdup_unescaped (value);
int i;
for (i = 0; property->val_possible[i]; i++) {
if (strcmp (property->val_possible[i], value_unescaped) == 0) {
if (property->type == TYPE_SELECT_S) {
printf ("%s = %s\n", property->key, value);
deadbeef->conf_set_str (property->key, value_unescaped);
free (value_unescaped);
return 0;
}
else {
printf ("%s = %s\n", property->key, value);
free (value_unescaped);
deadbeef->conf_set_int (property->key, i);
return 0;
}
}
}
free (value_unescaped);
// try to set as num
char *endptr;
int num = strtol (value, &endptr, 10);
if (value != endptr && num < property->type_count) {
if (property->type == TYPE_SELECT_S) {
deadbeef->conf_set_str (property->key, property->val_possible[num]);
}
else {
deadbeef->conf_set_int (property->key, num);
}
printf ("%s = \"%s\"\n", property->key, property->val_possible[num]);
return 0;
}
printf ("Failed to set property %s.\n", property->key);
return -1;
}
printf ("Debug: setting key directly to option\n");
printf ("%s = %s\n", property->key, value);
deadbeef->conf_set_str (property->key, value);
return 0;
}
int properties_print (property_t ** properties) {
int i;
for (i = 0; properties[i]; i++) {
const char * color = KNRM;
if (is_property_available (properties[i]) == 0)
color = KDIM;
printf ("%s%d: ", color, i);
property_print (properties[i]);
printf ("\n" KNRM);
}
return 0;
}
int property_print (property_t * prop) {
property_update (prop);
printf ("%s\n", prop->name);
int len = 64;
if (prop->val) {
len = strlen(prop->def) + 1;
}
char value_b[len + 2];
if (prop->val) {
if (has_spaces(prop->val)) {
sprintf (value_b, "\"%s\"", prop->val);
}
else {
strcpy (value_b, prop->val);
}
}
else {
sprintf (value_b, "(%s)", _("None"));
}
len = 64;
if (prop->def) {
len = strlen(prop->def) + 1;
}
char def_b[len + 2];
if (prop->def) {
if (has_spaces(prop->def)) {
sprintf (def_b, "\"%s\"", prop->def);
}
else {
strcpy (def_b, prop->def);
}
}
else {
sprintf (def_b, "(%s)", _("None"));
}
char *default_s = _("Default");
printf ("%s = %s\n(%s: %s)\n", prop->key, value_b, default_s, def_b);
return 0;
}
int is_property_available (property_t *prop) {
if ((prop->type == TYPE_SELECT || prop->type == TYPE_SELECT_S) && prop->type_count == 0) {
// no option
return 0;
}
property_t *ret = property_requires (prop);
if (ret) {
return 0;
}
return 1;
}
property_t * property_requires (property_t *prop) {
property_t *check = prop;
while ((check = check->requires)) {
property_update (check);
if (check->val) {
char *endptr;
int num = strtol (check->val, &endptr, 10);
if (check->val == endptr) {
printf ("check->val to num failed, assuming true?\n");
continue;
}
if (num == 0) {
return check;
}
continue;
}
else {
printf ("%s: check->val is empty, should check def?\n", check->key);
continue;
}
}
return NULL;
}
property_t ** properties_alloc (const char * cmd) {
// build properties list
int properties_size = PROPERTIES_MAX;
property_t ** properties = malloc (properties_size * sizeof(struct property *));
if (!cmd) {
// return empty list
properties[0] = NULL;
return properties;
}
char buffer[strlen(cmd)+1];
strcpy (buffer, cmd);
char * ptr = buffer;
char * nl_ptr = ptr;
char * endptr = strrchr (buffer, 0);
int i;
int to_break = 0;
for (i = 0; ptr < endptr; i++) {
if (i >= properties_size) {
properties_size *= 2;
properties = realloc (properties, properties_size * sizeof(char *));
if (!properties) {
printf ("Failed to realloc memory!\n");
return NULL;
}
}
//
nl_ptr = strchr (ptr, '\n');
if (!nl_ptr) {
// probably last char
to_break = 1;
}
else {
*nl_ptr = 0;
}
properties[i] = property_alloc (ptr);
if (/*v2 !properties[i] || */to_break) {
// out of options
i++;
break;
}
else {
if (!properties[i]) {
i--;
}
// next
ptr = ++nl_ptr;
}
}
properties[i] = NULL;
return properties;
}
char *property_groups_curr[5] = {NULL};
struct property *requires_curr[5] = {NULL};
struct property * property_alloc (char * string) {
if (!string || strlen(string) == 0) {
return NULL;
}
struct property * temp = malloc (sizeof(struct property));
memset (temp, 0, sizeof(struct property));
if (!temp) {
return NULL;
}
// todo escape chars
char string_f[strlen(string)+1];
strcpy (string_f, string);
// strip last chars ;\n
{
char * semicolon = strrchr (string_f, ';');
if (semicolon) {
*semicolon = 0;
}
}
char ** argv_o = argv_alloc (string_f);
char ** argv = argv_o;
if (!argv[0]) {
argv_free (argv);
free (temp);
return NULL;
}
// PROPERTY name type key def
int set_curr_require = 0;
if (strcmp (argv[0], "property") != 0) {
do {
// property
if (strcmp (argv[0], "group_begin") == 0) {
int i;
for (i = 0; property_groups_curr[i]; i++);
if (i >= 5) {
argv_free (argv_o);
free (temp);
printf ("Nested groups reached max!\n");
return NULL;
}
if (argv[1][0] == ';') {
property_groups_curr[i] = "";
property_groups_curr[i+1] = NULL;
}
else {
property_groups_curr[i] = strdup (argv[1]);
}
argv_free (argv_o);
free (temp);
return NULL;
}
else if (strcmp (argv[0], "group_require") == 0) {
argv++;
set_curr_require = 1;
break;
}
else if (strcmp (argv[0], "group_end") == 0) {
argv_free (argv_o);
free (temp);
int i;
for (i = 0; property_groups_curr[i]; i++);
if (i-1 >= 0) {
property_groups_curr[i-1] = NULL;
requires_curr[i-1] = NULL;
}
return NULL;
}
else if (strcmp (argv[0], "separator") == 0) {
argv_free (argv_o);
free (temp);
return NULL;
}
printf ("unknown item \"%s\"\n", argv[0]);
argv_free (argv);
free (temp);
return NULL;
}
while (0);
}
if (argv_count (argv) < 5) {
printf ("property for string \"%s\": not enough arguments\n", string);
argv_free (argv);
free (temp);
return NULL;
}
temp->name = strdup_unescaped (argv[1]);
// type
if (strcmp (argv[2], "entry") == 0) {
temp->type = TYPE_ENTRY;
temp->type_string = "entry";
}
else if (strcmp (argv[2], "password") == 0) {
temp->type = TYPE_PASSWORD;
temp->type_string = "password";
}
else if (strcmp (argv[2], "file") == 0) {
temp->type = TYPE_FILE;
temp->type_string = "file";
}
else if (strcmp (argv[2], "checkbox") == 0) {
temp->type = TYPE_CHECKBOX;
temp->type_string = "checkbox";
}
else if (strncmp (argv[2], "hscale", 6) == 0) {
temp->type = TYPE_HSCALE;
temp->type_string = "hscale";
// options
{
char *pt = argv[2]+7;
char *array[3] = {pt, NULL, NULL};
array[1] = strchr (array[0], ',') + 1;
array[2] = strchr (array[1], ',') + 1;
{
char *comma = pt;
while ((comma = strchr (comma, ','))) {
*comma = 0;
}
}
char *endptr = NULL;
temp->type_min = strtol (array[0], &endptr, 10);
if (array[0] == endptr) {
printf ("type min failed\n");
}
temp->type_max = strtol (array[1], &endptr, 10);
if (array[0] == endptr) {
printf ("type max failed\n");
}
temp->type_step = strtol (array[2], &endptr, 10);
if (array[0] == endptr) {
printf ("type step failed\n");
}
}
}
else if (strncmp (argv[2], "spinbtn", 7) == 0) {