-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctxopt.c
4870 lines (4102 loc) · 149 KB
/
ctxopt.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
/* ################################################################### */
/* Copyright 2020, Pierre Gentile ([email protected]) */
/* */
/* This Source Code Form is subject to the terms of the Mozilla Public */
/* License, v. 2.0. If a copy of the MPL was not distributed with this */
/* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* ################################################################### */
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <regex.h>
#include <stdarg.h>
#include <string.h>
#include "ctxopt.h"
/* ************************ */
/* Static global variables. */
/* ************************ */
static void * contexts_bst;
static void * options_bst;
state_t * cur_state;
/* Prototypes */
/* ************************** */
/* Fatal messages prototypes. */
/* ************************** */
static void (**err_functions)(errors e, state_t * state);
static void
fatal_internal(const char * format, ...);
static void
fatal(errors e, char * errmsg);
static int user_rc; /* Used by various callback functions. */
static int user_value; /* Used by various callback functions. */
static char * user_string; /* Used by various callback functions. */
static char * user_string2; /* Used by various callback functions. */
static void * user_object; /* Used by various callback functions. */
/* ************************************ */
/* Memory management static prototypes. */
/* ************************************ */
static void *
xmalloc(size_t size);
static void *
xcalloc(size_t num, size_t size);
static void *
xrealloc(void * ptr, size_t size);
static char *
xstrdup(const char * p);
static char *
xstrndup(const char * str, size_t len);
/* ********************** */
/* BST static prototypes. */
/* ********************** */
typedef struct bst_s bst_t;
typedef enum
{
preorder,
postorder,
endorder,
leaf
} walk_order_e;
#if 0 /* Unused yet. */
static void *
bst_delete(const void * vkey, void ** vrootp,
int (*compar)(const void *, const void *));
#endif
static void
bst_destroy(void * vrootp, void (*clean)(void *));
static void *
bst_find(const void * vkey, void * const * vrootp,
int (*compar)(const void *, const void *));
static void *
bst_search(void * vkey, void ** vrootp,
int (*compar)(const void *, const void *));
static void
bst_walk_recurse(const bst_t * root,
void (*action)(const void *, walk_order_e, int), int level);
static void
bst_walk(const void * vroot, void (*action)(const void *, walk_order_e, int));
/* ****************************** */
/* Linked list static prototypes. */
/* ****************************** */
typedef struct ll_node_s ll_node_t;
typedef struct ll_s ll_t;
static void
ll_append(ll_t * const list, void * const data);
static void
ll_prepend(ll_t * const list, void * const data);
static void
ll_insert_after(ll_t * const list, ll_node_t * node, void * const data);
static void
ll_insert_before(ll_t * const list, ll_node_t * node, void * const data);
static int
ll_delete(ll_t * const list, ll_node_t * node);
static void
ll_init(ll_t * list);
static ll_node_t *
ll_new_node(void);
static ll_t *
ll_new(void);
static void
ll_free(ll_t * const list, void (*)(void *));
static void
ll_destroy(ll_t * const list, void (*)(void *));
static int
ll_strarray(ll_t * list, ll_node_t * start_node, int * count, char *** array);
/* ************************** */
/* Various static prototypes. */
/* ************************** */
static void
ltrim(char * str, const char * trim_str);
static void
rtrim(char * str, const char * trim_str, size_t min);
static int
strchrcount(char * str, char c);
static int
strpref(char * s1, char * s2);
static int
stricmp(const char * s1, const char * s2);
static char *
xstrtok_r(char * str, const char * delim, char ** end);
static int
eval_yes(char * value, int * invalid);
static char *
get_word(char * str, char * buf, size_t len);
/* ************************* */
/* ctxopt static prototypes. */
/* ************************* */
typedef struct flags_s flags_t;
typedef struct opt_s opt_t;
typedef struct par_s par_t;
typedef struct ctx_s ctx_t;
typedef struct constraint_s constraint_t;
typedef struct ctx_inst_s ctx_inst_t;
typedef struct opt_inst_s opt_inst_t;
typedef struct seen_opt_s seen_opt_t;
typedef struct req_s req_t;
static char *
strtoken(char * s, char * token, size_t tok_len, char * pattern, int * pos);
static int
ctx_compare(const void * c1, const void * c2);
static void
ctx_free(void * o);
static void
ctx_inst_free(void * ci);
static void
opt_inst_free(void * oi);
static int
seen_opt_compare(const void * so1, const void * so2);
static void
incomp_bst_free(void * b);
static void
req_free(void * r);
static void
seen_opt_free(void * seen_opt);
static int
opt_compare(const void * o1, const void * o2);
static void
opt_free(void * o);
static int
par_compare(const void * a1, const void * a2);
static void
par_free(void * p);
static void
constraint_free(void * cstr);
static ctx_t *
locate_ctx(char * name);
static opt_t *
locate_opt(char * name);
static par_t *
locate_par(char * name, ctx_t * ctx);
static void
print_before_constraints(ll_t * list);
static void
print_options(ll_t * list, int * has_optional, int * has_ellipsis,
int * has_rule, int * has_generic_arg, int * has_ctx_change,
int * has_early_eval);
static void
print_explanations(int has_early_eval, int has_ctx_change, int has_generic_arg,
int has_optional, int has_ellipsis, int has_rule);
static void
bst_seen_opt_cb(const void * node, walk_order_e kind, int level);
static void
bst_seen_opt_seen_cb(const void * node, walk_order_e kind, int level);
static void
bst_print_ctx_cb(const void * node, walk_order_e kind, int level);
static void
bst_check_opt_cb(const void * node, walk_order_e kind, int level);
static void
bst_match_par_cb(const void * node, walk_order_e kind, int level);
static void
match_prefix_cb(const void * node, walk_order_e kind, int level);
static int
has_unseen_mandatory_opt(ctx_inst_t * ctx_inst, char ** missing);
static int
opt_parse(char * s, opt_t ** opt);
static int
init_opts(char * spec, ctx_t * ctx);
static int
ctxopt_build_cmdline_list(int nb_words, char ** words);
static int
opt_set_parms(char * opt_name, char * par_str);
static ctx_inst_t *
new_ctx_inst(ctx_t * ctx, ctx_inst_t * prev_ctx_inst);
static void
evaluate_ctx_inst(ctx_inst_t * ctx_inst);
/* ****************************** */
/* Fatal messages implementation. */
/* ****************************** */
/* =================================================================== */
/* Fatal error function used when a fatal condition is encountered. */
/* This function is reserved for the ctxopt internal usage. */
/* */
/* format : printf like format. */
/* ... : remaining arguments interpreted using the format argument. */
/* =================================================================== */
static void
fatal_internal(const char * format, ...)
{
va_list args;
fprintf(stderr, "CTXOPT: ");
va_start(args, format);
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
va_end(args);
exit(EXIT_FAILURE);
}
/* ====================================================================== */
/* Generic fatal error function. This one uses the global status ctxopt */
/* stored in the cur_state structure and can call custom error functions. */
/* registered by the users for a given error identifier if any. */
/* */
/* e : Error identifier responsible of the fatal error. */
/* errmsg : User's provided string specific to the error e. */
/* Note that errmsg is not used in all cases. */
/* */
/* CTXOPTMISPAR Missing parameter. */
/* CTXOPTREQPAR Option: all parameters in a required group are */
/* missing. */
/* CTXOPTMISARG Missing argument. */
/* CTXOPTUXPARG Unexpected argument. */
/* CTXOPTDUPOPT Duplicated option. */
/* CTXOPTUNKPAR Unknown parameter. */
/* CTXOPTINCOPT Incompatible option. */
/* CTXOPTCTEOPT Option: bad number of occurrences. */
/* CTXOPTCTLOPT Option: not enough occurrences. */
/* CTXOPTCTGOPT Option: too many occurrence of. */
/* CTXOPTCTEARG Arguments: bad number of occurrences. */
/* CTXOPTCTLARG Arguments: not enough occurrences. */
/* CTXOPTCTGARG Arguments: too many occurrences. */
/* ====================================================================== */
static void
fatal(errors e, char * errmsg)
{
if (err_functions[e] != NULL)
err_functions[e](e, cur_state);
else
{
switch (e)
{
case CTXOPTNOERR:
break;
case CTXOPTMISPAR:
if (cur_state->ctx_par_name != NULL)
fprintf(stderr,
"the mandatory parameter(s) %s are missing in the context "
"introduced by %s.\n",
errmsg, cur_state->ctx_par_name);
else
fprintf(stderr,
"The mandatory parameter(s) %s are missing "
"in the main context.\n",
errmsg);
free(errmsg);
break;
case CTXOPTREQPAR:
fprintf(stderr, errmsg, cur_state->req_opt_par_needed,
cur_state->req_opt_par);
break;
case CTXOPTUNXARG:
if (cur_state->cur_opt_par_name != NULL)
fprintf(stderr,
"The parameter %s takes no arguments "
"or has too many arguments.\n",
cur_state->cur_opt_par_name);
break;
case CTXOPTMISARG:
if (cur_state->pre_opt_par_name != NULL)
fprintf(stderr, "%s requires argument(s).\n",
cur_state->pre_opt_par_name);
else
fprintf(stderr, "%s requires argument(s).\n",
cur_state->cur_opt_par_name);
break;
case CTXOPTDUPOPT:
if (cur_state->pre_opt_par_name != NULL)
fprintf(stderr,
"The parameter %s can only appear once in the context "
"introduced by %s.\n",
cur_state->cur_opt_params, cur_state->ctx_par_name);
else
fprintf(stderr,
"The parameter %s can only appear once "
"in the main context.\n",
cur_state->cur_opt_params);
break;
case CTXOPTUNKPAR:
fprintf(stderr, "Unknown parameter %s.\n%s",
cur_state->cur_opt_par_name, errmsg);
break;
case CTXOPTINCOPT:
fprintf(stderr, "The parameter %s is incompatible with %s.\n",
cur_state->cur_opt_par_name, errmsg);
break;
case CTXOPTCTEOPT:
if (cur_state->ctx_par_name)
fprintf(stderr,
"The parameter %s must appear exactly %d times "
"in the context introduced by %s.\n",
cur_state->cur_opt_params, cur_state->opts_count,
cur_state->ctx_par_name);
else
fprintf(stderr,
"The parameter %s must appear exactly %d times "
"in the main context.\n",
cur_state->cur_opt_params, cur_state->opts_count);
break;
case CTXOPTCTLOPT:
if (cur_state->ctx_par_name)
fprintf(stderr,
"The parameter %s must appear less than %d times "
"in the context introduced by %s.\n",
cur_state->cur_opt_params, cur_state->opts_count,
cur_state->ctx_par_name);
else
fprintf(stderr,
"The parameter %s must appear less than %d times "
"in the main context.\n",
cur_state->cur_opt_params, cur_state->opts_count);
break;
case CTXOPTCTGOPT:
if (cur_state->ctx_par_name)
fprintf(stderr,
"The parameter %s must appear more than %d times "
"in the context introduced by %s.\n",
cur_state->cur_opt_params, cur_state->opts_count,
cur_state->ctx_par_name);
else
fprintf(stderr,
"The parameter %s must appear more than %d times "
"in the main context.\n",
cur_state->cur_opt_params, cur_state->opts_count);
break;
case CTXOPTCTEARG:
fprintf(stderr, "The parameter %s must have exactly %d arguments.\n",
cur_state->cur_opt_par_name, cur_state->opt_args_count);
break;
case CTXOPTCTLARG:
fprintf(stderr, "The parameter %s must have less than %d arguments.\n",
cur_state->cur_opt_par_name, cur_state->opt_args_count);
break;
case CTXOPTCTGARG:
fprintf(stderr, "The parameter %s must have more than %d arguments.\n",
cur_state->cur_opt_par_name, cur_state->opt_args_count);
break;
case CTXOPTERRSIZ:
break;
}
}
/* CTXOPTUNKPAR should display the full usage to help the user follow */
/* the chaining of contexts when several possible contexts have been */
/* identified. Otherwise, errmsg is the empty string and the display of */
/* the current usage is enough. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (e == CTXOPTUNKPAR && *errmsg != '\0')
ctxopt_disp_usage(continue_after);
else
ctxopt_ctx_disp_usage(cur_state->ctx_name, continue_after);
exit(e); /* Exit with the error id e as return code. */
}
/* ********************************* */
/* Memory management implementation. */
/* ********************************* */
/* ================== */
/* Customized malloc. */
/* ================== */
static void *
xmalloc(size_t size)
{
void * allocated;
size_t real_size;
real_size = (size > 0) ? size : 1;
allocated = malloc(real_size);
if (allocated == NULL)
fatal_internal("Insufficient memory (attempt to malloc %lu bytes).\n",
(unsigned long int)size);
return allocated;
}
/* ================== */
/* Customized calloc. */
/* ================== */
static void *
xcalloc(size_t n, size_t size)
{
void * allocated;
n = (n > 0) ? n : 1;
size = (size > 0) ? size : 1;
allocated = calloc(n, size);
if (allocated == NULL)
fatal_internal("Insufficient memory (attempt to calloc %lu bytes).\n",
(unsigned long int)size);
return allocated;
}
/* =================== */
/* Customized realloc. */
/* =================== */
static void *
xrealloc(void * p, size_t size)
{
void * allocated;
allocated = realloc(p, size);
if (allocated == NULL && size > 0)
fatal_internal("Insufficient memory (attempt to xrealloc %lu bytes).\n",
(unsigned long int)size);
return allocated;
}
/* ==================================== */
/* strdup implementation using xmalloc. */
/* ==================================== */
static char *
xstrdup(const char * p)
{
char * allocated;
allocated = xmalloc(strlen(p) + 1);
strcpy(allocated, p);
return allocated;
}
/* =================================================== */
/* strndup implementation using xmalloc. */
/* This version guarantees that there is a final '\0'. */
/* =================================================== */
static char *
xstrndup(const char * str, size_t len)
{
char * p;
p = memchr(str, '\0', len);
if (p)
len = p - str;
p = xmalloc(len + 1);
memcpy(p, str, len);
p[len] = '\0';
return p;
}
/* *************************** */
/* Linked list implementation. */
/* *************************** */
/* Linked list node structure. */
/* """"""""""""""""""""""""""" */
struct ll_node_s
{
void * data;
struct ll_node_s * next;
struct ll_node_s * prev;
};
/* Linked List structure. */
/* """""""""""""""""""""" */
struct ll_s
{
ll_node_t * head;
ll_node_t * tail;
long len;
};
/* ========================= */
/* Create a new linked list. */
/* ========================= */
static ll_t *
ll_new(void)
{
ll_t * ret = xmalloc(sizeof(ll_t));
ll_init(ret);
return ret;
}
/* =============================================== */
/* Free all the elements of a list (make it empty) */
/* NULL or a custom function may be used to free */
/* the sub components of the elements. */
/* =============================================== */
static void
ll_free(ll_t * const list, void (*clean)(void *))
{
ll_node_t * node;
if (list)
{
node = list->head;
while (node)
{
/* Apply a custom cleaner if not NULL. */
/* """"""""""""""""""""""""""""""""""" */
if (clean)
clean(node->data);
ll_delete(list, node);
node = list->head;
}
}
}
/* ==================================== */
/* Destroy a list and all its elements. */
/* ==================================== */
static void
ll_destroy(ll_t * list, void (*clean)(void *))
{
if (list)
{
ll_free(list, clean);
free(list);
}
}
/* ========================= */
/* Initialize a linked list. */
/* ========================= */
static void
ll_init(ll_t * list)
{
list->head = NULL;
list->tail = NULL;
list->len = 0;
}
/* ===================================================== */
/* Allocate the space for a new node in the linked list. */
/* ===================================================== */
static ll_node_t *
ll_new_node(void)
{
ll_node_t * ret = xmalloc(sizeof(ll_node_t));
return ret;
}
/* ==================================================================== */
/* Append a new node filled with its data at the end of the linked list */
/* The user is responsible for the memory management of the data. */
/* ==================================================================== */
static void
ll_append(ll_t * const list, void * const data)
{
ll_node_t * node;
node = ll_new_node(); /* ll_new_node cannot return NULL because it *
| uses xmalloc which does not return if there *
| is an allocation error. */
node->data = data;
node->next = NULL; /* This node will be the last. */
node->prev = list->tail; /* NULL if it is a new list. */
if (list->tail)
list->tail->next = node;
else
list->head = node;
list->tail = node;
++list->len; /* One more node in the list. */
}
/* ================================================================== */
/* Put a new node filled with its data at the beginning of the linked */
/* list. */
/* The user is responsible for the memory management of the data. */
/* ================================================================== */
static void
ll_prepend(ll_t * const list, void * const data)
{
ll_node_t * node;
node = ll_new_node(); /* ll_new_node cannot return NULL because it *
| uses xmalloc which does not return if there *
| is an allocation error. */
node->data = data;
node->prev = NULL; /* This node will be the first. */
node->next = list->head; /* NULL if it is a new list. */
if (list->head)
list->head->prev = node;
else
list->tail = node;
list->head = node;
++list->len; /* One more node in the list. */
}
/* ======================================================== */
/* Insert a new node before the specified node in the list. */
/* ======================================================== */
static void
ll_insert_before(ll_t * const list, ll_node_t * node, void * const data)
{
ll_node_t * new_node;
if (node->prev == NULL)
ll_prepend(list, data);
else
{
new_node = ll_new_node(); /* ll_new_node cannot return NULL because it *
| uses xmalloc which does not return if there *
| is an allocation error. */
new_node->data = data;
new_node->next = node;
new_node->prev = node->prev;
node->prev->next = new_node;
node->prev = new_node;
++list->len; /* One more node in the list. */
}
}
/* ======================================================= */
/* Insert a new node after the specified node in the list. */
/* ======================================================= */
static void
ll_insert_after(ll_t * const list, ll_node_t * node, void * const data)
{
ll_node_t * new_node;
if (node->next == NULL)
ll_append(list, data);
else
{
new_node = ll_new_node(); /* ll_new_node cannot return NULL because it *
| uses xmalloc which does not return if there *
| is an allocation error. */
new_node->data = data;
new_node->prev = node;
new_node->next = node->next;
node->next->prev = new_node;
node->next = new_node;
++list->len; /* One more node in the list. */
}
}
/* ================================================================= */
/* Remove a node from a linked list. */
/* The memory taken by the deleted node must be freed by the caller. */
/* ================================================================= */
static int
ll_delete(ll_t * const list, ll_node_t * node)
{
if (list->head == list->tail)
{
/* We delete the last remaining element from the list. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""" */
if (list->head == NULL)
return 0;
list->head = list->tail = NULL;
}
else if (node->prev == NULL)
{
/* We delete the first element from the list. */
/* """""""""""""""""""""""""""""""""""""""""" */
list->head = node->next;
list->head->prev = NULL;
}
else if (node->next == NULL)
{
/* We delete the last element from the list. */
/* """"""""""""""""""""""""""""""""""""""""" */
list->tail = node->prev;
list->tail->next = NULL;
}
else
{
/* We delete an element from the list. */
/* """"""""""""""""""""""""""""""""""" */
node->next->prev = node->prev;
node->prev->next = node->next;
}
free(node);
--list->len; /* One less node in the list. */
return 1;
}
/* ==================================================================== */
/* Allocate and fill an array of strings from a list. */
/* WARNINGS: */
/* 1) The list node must contain strings (char *). */
/* 2) The strings in the resulting array MUST NOT be freed as the are */
/* NOT copied from the strings of the list. */
/* */
/* IN list : The list from which the array is generated. */
/* IN start_node : The node of the list which will be the first node to */
/* consider to create the array. */
/* OUT: count : The number of elements of the resulting array. */
/* OUT: array : The resulting array or NULL if the list is empty. */
/* RC : : The number of elements of the resulting array. */
/* ==================================================================== */
static int
ll_strarray(ll_t * list, ll_node_t * start_node, int * count, char *** array)
{
int n = 0;
ll_node_t * node;
*count = 0;
node = start_node;
if (list == NULL || node == NULL)
{
*array = NULL;
return 0;
}
*array = xmalloc((list->len + 1) * sizeof(char *));
while (node != NULL)
{
(*array)[n++] = (char *)(node->data);
(*count)++;
node = node->next;
}
(*array)[*count] = NULL;
return *count;
}
/* ******************************************************************* */
/* BST (search.h compatible) implementation. */
/* */
/* Tree search generalized from Knuth (6.2.2) Algorithm T just like */
/* the AT&T man page says. */
/* */
/* Written by reading the System V Interface Definition, not the code. */
/* */
/* Totally public domain. */
/* ******************************************************************* */
struct bst_s
{
void * key;
struct bst_s * llink;
struct bst_s * rlink;
};
#if 0 /* Unused yet. */
/* =========================== */
/* Delete node with given key. */
/* =========================== */
static void *
bst_delete(const void * vkey, void ** vrootp,
int (*compar)(const void *, const void *))
{
bst_t ** rootp = (bst_t **)vrootp;
bst_t * p, *q, *r;
int cmp;
if (rootp == NULL || (p = *rootp) == NULL)
return NULL;
while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0)
{
p = *rootp;
rootp = (cmp < 0) ? &(*rootp)->llink /* follow llink branch */
: &(*rootp)->rlink; /* follow rlink branch */
if (*rootp == NULL)
return NULL; /* key not found */
}
r = (*rootp)->rlink; /* D1: */
if ((q = (*rootp)->llink) == NULL) /* Left NULL? */
q = r;
else if (r != NULL)
{ /* Right link is NULL? */
if (r->llink == NULL)
{ /* D2: Find successor */
r->llink = q;
q = r;
}
else
{ /* D3: Find NULL link */
for (q = r->llink; q->llink != NULL; q = r->llink)
r = q;
r->llink = q->rlink;
q->llink = (*rootp)->llink;
q->rlink = (*rootp)->rlink;
}
}
if (p != *rootp)
free(*rootp); /* D4: Free node */
*rootp = q; /* link parent to new node */
return p;
}
#endif
/* ===================================================================== */
/* Destroy a tree. */
/* The clean function pointer can be NULL, in this case the node content */
/* is not freed. */
/* ===================================================================== */
static void
bst_destroy(void * vrootp, void (*clean)(void *))
{
bst_t * root = (bst_t *)vrootp;
if (root == NULL)
return;
bst_destroy(root->llink, clean);
bst_destroy(root->rlink, clean);
if (clean)
clean((void *)root->key);
free(root);
}
/* ========================= */
/* Find a node, or return 0. */
/* ========================= */
static void *
bst_find(const void * vkey, void * const * vrootp,
int (*compar)(const void *, const void *))
{
bst_t * const * rootp = (bst_t * const *)vrootp;
if (rootp == NULL)
return NULL;
while (*rootp != NULL)
{ /* T1: */
int r;
if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
return *rootp; /* key found */
rootp = (r < 0) ? &(*rootp)->llink /* T3: follow left branch */
: &(*rootp)->rlink; /* T4: follow right branch */
}
return NULL;
}
/* ======================================= */
/* Find or inserts datum into search tree. */
/* ======================================= */
static void *
bst_search(void * vkey, void ** vrootp,
int (*compar)(const void *, const void *))
{
bst_t * q;
bst_t ** rootp = (bst_t **)vrootp;
if (rootp == NULL)
return NULL;
while (*rootp != NULL)
{ /* Knuth's T1: */