-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkom.h
1129 lines (961 loc) · 40.5 KB
/
linkom.h
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
/*
This file is part of Linkom Library
Copyright (c) 2016 Hernán Di Pietro
Wide-char Safe string functions
Copyright 1999 Alexandre Julliard
Copyright 2000 Jon Griffiths
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef __LINKOM_H__f82e85a35f914826a6af35aa02839625
#define __LINKOM_H__f82e85a35f914826a6af35aa02839625
#ifdef __cplusplus
extern "C" {
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#if defined (__GNUC__)
#define __STDC_WANT_LIB_EXT1__ 1
#include <wchar.h>
#include <errno.h>
#define _wcsdup wcsdup
#define _wcsicmp wcscasecmp
#define wcstok_s wcstok
#define _LK_MIN(a,b) ( (a) < (b) ? (a) : (b) )
static int wcscpy_s(wchar_t* dest, size_t numElem, const wchar_t* src)
{
size_t size = 0;
if(!dest || !numElem)
return EINVAL;
dest[0] = 0;
if (!src)
return EINVAL;
size = wcslen(src) + 1;
if (size > numElem)
return ERANGE;
memcpy( dest, src, size * sizeof( wchar_t ));
return 0;
}
static int wcscat_s(wchar_t* dst, size_t numElem, const wchar_t* src)
{
wchar_t* ptr = dst;
if (!dst || numElem==0)
return EINVAL;
if (!src)
{
dst[0] = 0;
return EINVAL;
}
while (ptr < dst + numElem && *ptr != 0) ptr++;
while (ptr < dst + numElem)
{
if ((*ptr++ = *src++) == 0) return 0;
}
dst[0] = 0;
return ERANGE;
}
static int wcsncpy_s(wchar_t* dst, size_t numElem, const wchar_t* src,
size_t count)
{
size_t size = 0;
if (!dst || !numElem)
return EINVAL;
dst[0] = 0;
if (!src)
return EINVAL;
size = _LK_MIN(wcslen(src), count);
if (size >= numElem)
{
return ERANGE;
}
memcpy ( dst, src, size * sizeof (wchar_t) );
dst[size] = 0;
return 0;
}
#endif
#ifdef LK_ENABLE_TRACE
#define _LKTRACE(...) fprintf (stderr, __VA_ARGS__)
#define _LKTRACEW(...) fwprintf(stderr, __VA_ARGS__)
#else
#define _LKTRACE(...)
#define _LKTRACEW(...)
#endif
#define LK_ARRAYSIZE(x) ((sizeof(x) / sizeof(*(x))) / \
(size_t)(!(sizeof(x) % sizeof(*(x)))))
/* ---------------------------------------------------------------------------
Error codes (inspired by MS HRESULTs)
---------------------------------------------------------------------------*/
typedef enum
{
LK_R_OK = 0x00000000,
LK_R_FALSE = 0x00000000,
LK_R_TRUE = 0x00000001,
LK_E_MALLOC = 0xEEEE0001,
LK_E_NULLPOINTER = 0xEEEE0002,
LK_E_UNINITIALIZED = 0xEEEE0003,
LK_E_INVALIDARG = 0xEEEE0004,
LK_E_ALREADYINIT = 0xEEEE0005,
LK_E_LISTFULL = 0xEEEE0006,
LK_E_NOTFOUND = 0xEEEE0007,
LK_E_OUTOFBOUNDS = 0xEEEE0008,
LK_E_INVALIDSYNTAX = 0xEEEE1000,
LK_E_UNKNOWNARG = 0xEEEE1001,
LK_E_DUPLICATEARG = 0xEEEE1002,
LK_E_DUPLICATETOKEN = 0xEEEE1003,
LK_E_TYPEMISMATCH = 0xEEEE1004,
LK_E_MISSINGARG = 0xEEEE1005,
LK_E_MISSINGVALUE = 0xEEEE1006,
LK_E_MISSINGNAMARG = 0xEEEE1007,
LK_E_DUPLICATEGROUPITEM = 0xEEEE1008,
LK_E_CONTRADICTARG = 0xEEEE1009,
LK_E_NOGROUPMEMBERCHOSEN= 0xEEEE100A,
LK_E_TOKENLENGTH = 0xEEEE100B,
LK_E_TOKENCOUNT = 0xEEEE100C,
LK_E_GROUPCOUNT = 0xEEEE100D,
LK_E_GROUPMEMBERCOUNT = 0xEEEE100E,
LK_E_VALUELENGTH = 0xEEEE100F,
LK_E_UNDEFINED = 0xEEEE1100,
LK_E_INSUFFICIENTBUFFER = 0xEEEE1101
} LK_RESULT;
#define LK_SUCCESS(x) (( (x) == 0)|| ((x)==1))
/* ---------------------------------------------------------------------------
Error message table
---------------------------------------------------------------------------*/
typedef struct _lk_msgtab
{
LK_RESULT res;
wchar_t* wErrMsg;
} LK_MESSAGETABLE;
static const LK_MESSAGETABLE g_lkErrTable[] = {
LK_R_OK, L"Operation successful",
LK_E_MALLOC, L"Memory allocation error",
LK_E_NULLPOINTER, L"Null pointer argument",
LK_E_UNINITIALIZED, L"Library uninitialized",
LK_E_INVALIDARG, L"Invalid argument: %s",
LK_E_ALREADYINIT, L"Already initialized",
LK_E_LISTFULL, L"List full",
LK_E_NOTFOUND, L"Parameter not found: %s",
LK_E_OUTOFBOUNDS, L"Argument out of bounds",
LK_E_INVALIDSYNTAX, L"Invalid syntax specification",
LK_E_UNKNOWNARG, L"Unknown argument: %s",
LK_E_DUPLICATEARG, L"Duplicate argument: %s",
LK_E_DUPLICATETOKEN, L"Duplicate syntax token: %s",
LK_E_TYPEMISMATCH, L"Type mismatch for argument: %s",
LK_E_MISSINGARG, L"Required argument missing: %s",
LK_E_MISSINGVALUE, L"Required value missing for parameter: %s",
LK_E_MISSINGNAMARG, L"Required named argument missing: %s",
LK_E_DUPLICATEGROUPITEM, L"Duplicate group argument: %s",
LK_E_CONTRADICTARG, L"Contradictory arguments specified: %s",
LK_E_NOGROUPMEMBERCHOSEN, L"Required group argument missing: %s",
LK_E_TOKENLENGTH, L"Token length exceeded",
LK_E_TOKENCOUNT, L"Token count exceeded",
LK_E_GROUPCOUNT, L"Group count exceeded",
LK_E_GROUPMEMBERCOUNT, L"Group member count exceeded",
LK_E_VALUELENGTH, L"Parameter value length exceeded",
LK_E_UNDEFINED, L"Parameter is undefined",
LK_E_INSUFFICIENTBUFFER, L"Specified buffer size is insufficient"
};
#define LK_RETURN_STERR(_lkerr) { _Lk_StoreExtErr(_lkerr, 0); return _lkerr; }
#define LK_RETURN_STERR1(_lkerr,_arg) { _Lk_StoreExtErr(_lkerr, _arg); return _lkerr; }
/* ---------------------------------------------------------------------------
Tunable limits, modify as you wish
---------------------------------------------------------------------------*/
#ifndef LK_DEFINE_USER_LIMITS
#define LK_MAX_TOKENARG_LEN 256 /* Maximum argument and token length */
#define LK_MAX_TOKENARG_COUNT 256 /* Maximum argument and token count */
#define LK_MAX_GROUP_COUNT 32 /* Maximum group count */
#define LK_MAX_PARAMVALUE_LEN 2048 /* Max str length for param. values*/
#endif
#define LK_MAX_ERRORBUFFER_LEN 1024
#define LK_MAX_GROUP_DESCRIPTION 256
/* ---------------------------------------------------------------------------
Token List types
---------------------------------------------------------------------------*/
typedef enum tagLK_TOKEN_TYPE
{
LK_TOKEN_TYPE_NAMED_ARGUMENT,
LK_TOKEN_TYPE_PARAMETER,
LK_TOKEN_TYPE_PARAMETER_VAL,
LK_TOKEN_TYPE_PARAMETER_VAL_OPT,
LK_TOKEN_TYPE_PARAMETER_GROUP
} LK_TOKEN_TYPE;
typedef struct tagLK_TOKEN_ITEM
{
wchar_t name[LK_MAX_TOKENARG_LEN];
LK_TOKEN_TYPE type;
int memberOf; // mutex-group member,if any
} LK_TOKEN_ITEM;
typedef struct tagLK_TOKEN_LIST_ENTRY
{
bool isOptional;
LK_TOKEN_ITEM item;
} LK_TOKEN_LIST_ENTRY;
static wchar_t* g_lkTypeStr[] =
{
L"NAMED_ARGUMENT", L"PARAMETER", L"PARAMETER_VAL",
L"PARAMETER_VAL_OPT", L"GROUP"
};
/* This is a special name specifying a non-parameter argument
that's waiting to be matched against a syntax token */
#define LK_UNMAPPED_ARG_NAME L"\0xFA\00\0xFB\00\0xFC\00\0xFD\00\00\00"
/* ----------------------------------------------------------------------------
Argument List types
---------------------------------------------------------------------------*/
typedef struct tagARGUMENT
{
wchar_t name[LK_MAX_TOKENARG_LEN];
wchar_t value[LK_MAX_PARAMVALUE_LEN];
bool hasValue;
bool isParam;
bool isMapped;
bool isProcessed;
int matchingGroup;
} LK_ARGUMENT;
typedef struct tagARGLIST
{
LK_ARGUMENT item[LK_MAX_TOKENARG_COUNT];
size_t count;
} LK_ARGLIST;
/* ----------------------------------------------------------------------------
Group data
---------------------------------------------------------------------------*/
#define LK_NOGROUP -1
typedef struct tagLK_GROUPINFO
{
wchar_t* selectedParam;
bool isOptional;
wchar_t description[LK_MAX_GROUP_DESCRIPTION];
} LK_GROUPINFO;
/* --------------------------------------------------------------------------
Globals
--------------------------------------------------------------------------*/
static LK_ARGLIST g_argList;
static LK_TOKEN_LIST_ENTRY* g_tokenList;
static size_t g_tokenCount, g_tokenMaxCount;
static bool g_syntaxValid;
static size_t g_groupCount;
static LK_GROUPINFO g_groupInfo[LK_MAX_GROUP_COUNT];
static wchar_t g_exErrBuffer[LK_MAX_ERRORBUFFER_LEN];
/* --------------------------------------------------------------------------*/
LK_RESULT _Lk_TokenListInit(size_t maxCount)
{
if (g_tokenList)
return LK_E_ALREADYINIT;
if (maxCount == 0)
return LK_E_INVALIDARG;
g_tokenList = (LK_TOKEN_LIST_ENTRY*)malloc(maxCount * sizeof (LK_TOKEN_LIST_ENTRY));
if (!g_tokenList)
return LK_E_MALLOC;
g_tokenCount = 0;
g_tokenMaxCount = maxCount;
g_groupCount = 0;
return LK_R_OK;
}
/* --------------------------------------------------------------------------*/
LK_RESULT _Lk_TokenListClear()
{
if (g_tokenMaxCount == 0)
return LK_E_UNINITIALIZED;
size_t i;
for (i = 0 ; i < g_tokenMaxCount; ++i)
memset (&g_tokenList[i], 0, sizeof(LK_TOKEN_LIST_ENTRY));
return LK_R_OK;
}
/* --------------------------------------------------------------------------*/
LK_RESULT _Lk_TokenFindName(const wchar_t* name)
{
for (size_t i = 0; i < g_tokenCount; ++i)
{
//wprintf(L"_Lk_TokenFindName (%s) == (%s)\n", g_tokenList[i].item.name, name);
if (!_wcsicmp(g_tokenList[i].item.name, name))
return LK_R_OK;
}
return LK_E_NOTFOUND;
}
/* --------------------------------------------------------------------------*/
LK_RESULT _Lk_TokenAdd(LK_TOKEN_ITEM* item, bool isOptional, bool allowDuplicate)
{
if (g_tokenCount == g_tokenMaxCount)
return LK_E_LISTFULL;
//wprintf(L">>_Lk_TokenAdd(%s)\n", item->name);
if (!allowDuplicate && (_Lk_TokenFindName(item->name) == LK_R_OK))
return LK_E_DUPLICATETOKEN;
g_tokenList[g_tokenCount].isOptional = isOptional;
g_tokenList[g_tokenCount].item.type = item->type;
g_tokenList[g_tokenCount].item.memberOf = item->memberOf;
wcscpy_s(g_tokenList[g_tokenCount].item.name,
LK_MAX_TOKENARG_LEN, item->name);
g_tokenCount++;
return LK_R_OK;
}
/* --------------------------------------------------------------------------*/
size_t _Lk_TokenGetCount() { return g_tokenCount; }
/* --------------------------------------------------------------------------*/
LK_RESULT _Lk_TokenGet(size_t idx, LK_TOKEN_LIST_ENTRY** pListEntry)
{
if (idx >= g_tokenCount)
return LK_E_OUTOFBOUNDS;
*pListEntry = &g_tokenList[idx];
return LK_R_OK;
}
void _Lk_TokenListFree()
{
if (g_tokenList)
{
free (g_tokenList);
g_tokenList = NULL;
g_tokenCount = 0;
g_tokenMaxCount=0;
}
}
/* ---------------------------------------------------------------------------*/
LK_RESULT _Lk_ClassifyTokenR(wchar_t* token, bool isOptional)
{
assert(token);
_LKTRACEW(L" %s (%s,%d)\n", __FUNCTION__, token, isOptional);
const size_t len = wcslen(token);
if (len > LK_MAX_TOKENARG_LEN)
return LK_E_TOKENLENGTH;
if ((token[0] == L'[') && (token[len-1] == L']') && !isOptional)
{
token[len-1] = L'\0';
return _Lk_ClassifyTokenR(&token[1], true);
}
// minimal accepted form: {/x|/y} (length:7)
else if ((token[0] == L'{') && (token[len-1] == L'}') && len >= 7)
{
LK_RESULT r;
if (g_groupCount == LK_MAX_GROUP_COUNT)
return LK_E_GROUPCOUNT;
g_groupInfo[g_groupCount].isOptional = isOptional;
g_groupInfo[g_groupCount].selectedParam = NULL;
wcsncpy_s(g_groupInfo[g_groupCount].description, LK_MAX_GROUP_DESCRIPTION, &token[0], len);
LK_TOKEN_ITEM item;
_LKTRACEW(L" Group-type token, decomposing...\n");
int i = 1, iArgStart = i;
do
{
if(token[i] == L'|' || token[i] == L'}')
{
if (token[iArgStart] != L'/')
return LK_E_INVALIDSYNTAX;
memset (item.name, 0, LK_MAX_TOKENARG_LEN);
wcsncpy_s(item.name, LK_MAX_TOKENARG_LEN, &token[iArgStart], i - iArgStart);
if (item.name[wcslen(item.name)-1] == L'}' )
item.name[wcslen(item.name)-1] = 0;
size_t namelen = wcslen(item.name);
_LKTRACEW(L" item.name = %s\n", item.name);
size_t k = namelen-1;
if (item.name[k] == L':' || item.name[k] == L'=')
{
_LKTRACEW(L" item.type = LK_TOKEN_TYPE_PARAMETER_VAL\n");
item.type = LK_TOKEN_TYPE_PARAMETER_VAL;
item.name[k] = L'\0';
}
else if (item.name[k] == L'?')
{
if (namelen == 2) // Hack to accept '/?' which is fairly common
{
_LKTRACEW(L" item.type = LK_TOKEN_TYPE_PARAMETER\n");
item.type = LK_TOKEN_TYPE_PARAMETER;
item.name[k+1] = L'\0';
}
else
{
_LKTRACEW(L" item.type = LK_TOKEN_TYPE_PARAMETER_VAL_OPT\n");
item.type = LK_TOKEN_TYPE_PARAMETER_VAL_OPT;
item.name[k] = L'\0';
}
}
else
{
_LKTRACEW(L" item.type = LK_TOKEN_TYPE_PARAMETER\n");
item.type = LK_TOKEN_TYPE_PARAMETER;
item.name[k+1] = L'\0';
}
_LKTRACEW(L" item.memberOf = %Iu\n", g_groupCount);
item.memberOf = (int) g_groupCount;
r = _Lk_TokenAdd(&item, isOptional, false);
if (r == LK_R_OK)
iArgStart = i + 1;
else
{
//printf("TkListAdditem res=0x%08x \n", r);
if (r == LK_E_DUPLICATETOKEN)
return LK_E_DUPLICATEGROUPITEM;
break;
}
}
} while (token[i++]);
g_groupCount++;
return LK_R_OK;
}
else if (token[0] == L'$' && len > 1)
{
LK_TOKEN_ITEM item;
item.memberOf = LK_NOGROUP;
wcsncpy_s(item.name, LK_MAX_TOKENARG_LEN, token, LK_MAX_TOKENARG_LEN);
if (!isOptional)
{
_LKTRACEW(L" item.type = LK_TOKEN_TYPE_NAMED_ARGUMENT\n");
item.type = LK_TOKEN_TYPE_NAMED_ARGUMENT;
return _Lk_TokenAdd(&item, isOptional, false);
}
}
else if (token[0] == L'/')
{
LK_TOKEN_ITEM item;
item.memberOf = LK_NOGROUP;
wcsncpy_s(item.name, LK_MAX_TOKENARG_LEN, token, LK_MAX_TOKENARG_LEN);
size_t namelen = wcslen(item.name);
// Check if it's a parameter with (optional) value associated
size_t k = namelen-1;
if (token[k] == L':' || token[k] == L'=')
{
_LKTRACEW(L" item.type = LK_TOKEN_TYPE_PARAMETER_VAL\n");
item.type = LK_TOKEN_TYPE_PARAMETER_VAL;
item.name[k] = L'\0';
}
else if (token[k] == L'?')
{
if (namelen == 2) // Hack to accept '/?' which is fairly common
{
_LKTRACEW(L" item.type = LK_TOKEN_TYPE_PARAMETER\n");
item.type = LK_TOKEN_TYPE_PARAMETER;
item.name[k+1] = L'\0';
}
else
{
_LKTRACEW(L" item.type = LK_TOKEN_TYPE_PARAMETER_VAL_OPT\n");
item.type = LK_TOKEN_TYPE_PARAMETER_VAL_OPT;
item.name[k] = L'\0';
}
}
else
{
_LKTRACEW(L" item.type = LK_TOKEN_TYPE_PARAMETER\n");
item.type = LK_TOKEN_TYPE_PARAMETER;
item.name[k+1] = L'\0';
}
return _Lk_TokenAdd(&item, isOptional, false);
}
return LK_E_INVALIDSYNTAX;
}
/* --------------------------------------------------------------------------*/
LK_RESULT _Lk_ClassifyToken(wchar_t* token)
{
return _Lk_ClassifyTokenR(token, false);
}
/* ---------------------------------------------------------------------------*/
LK_RESULT _Lk_FindArgumentName(wchar_t* name, bool fMarkAsProcessed) {
for (size_t i = 0; i < g_argList.count; i++) {
if (!_wcsicmp(name, g_argList.item[i].name)) {
if (fMarkAsProcessed)
g_argList.item[i].isProcessed = true;
return LK_R_TRUE;
}
}
return LK_R_FALSE;
}
/*----------------------------------------------------------------------------*/
LK_RESULT _Lk_FindUnprocessedArg(wchar_t* outArg) {
for (size_t i = 0; i < g_argList.count; i++) {
if (!g_argList.item[i].isProcessed)
{
// If this an unprocessed named-argument, may be still is
// unmapped to any token, so item[i].name is empty; in that
// case we use the value instead, to return data.
if (g_argList.item[i].name == NULL || wcslen(g_argList.item[i].name) == 0)
wcscpy_s(outArg, LK_MAX_TOKENARG_LEN, g_argList.item[i].value);
else
wcscpy_s(outArg, LK_MAX_TOKENARG_LEN, g_argList.item[i].name);
return LK_R_TRUE;
}
}
return LK_R_FALSE;
}
/*----------------------------------------------------------------------------*/
LK_RESULT _Lk_EvalGroups(wchar_t* invalidArg)
{
size_t i;
LK_TOKEN_LIST_ENTRY* pTok = NULL;
wchar_t pTokName[LK_MAX_TOKENARG_LEN];
for (i = 0; i < g_groupCount; i++)
g_groupInfo[i].selectedParam = NULL;
for (i = 0; i < _Lk_TokenGetCount(); i++)
{
_Lk_TokenGet(i, &pTok);
if ( pTok->item.memberOf != LK_NOGROUP )
{
memset(pTokName, 0, sizeof(pTokName));
_LKTRACEW(L"_Lk_EvalGroups: Evaluating token %s (group:%d)\n", pTok->item.name,pTok->item.memberOf);
size_t j;
for (j = 0; j < g_argList.count; j++)
{
if (!_wcsicmp(g_argList.item[j].name, pTok->item.name))
{
if (g_groupInfo[pTok->item.memberOf].selectedParam == NULL)
{
if (pTok->item.type == LK_TOKEN_TYPE_PARAMETER_VAL && !g_argList.item[j].hasValue)
{
_LKTRACEW(L"_Lk_EvalGroups: Argument %s potentially matched for group : %d, but missing value\n",
g_argList.item[j].name, pTok->item.memberOf);
wcscpy_s(invalidArg, LK_MAX_TOKENARG_LEN, g_argList.item[j].name);
return LK_E_MISSINGVALUE;
}
_LKTRACEW(L"_Lk_EvalGroups: Argument %s matched for group : %d\n", g_argList.item[j].name,pTok->item.memberOf);
g_groupInfo[pTok->item.memberOf].selectedParam = g_argList.item[j].name;
g_argList.item[j].isProcessed = true;
break;
}
else
{
//Already got an argument from this group
//Return the parameters we detected as contradictory
size_t cch = wcslen(g_groupInfo[pTok->item.memberOf].selectedParam)
+ wcslen(g_argList.item[j].name) + 2;
wchar_t* invArgData = (wchar_t*) calloc (cch, sizeof(wchar_t));
if (!invArgData)
return LK_E_MALLOC;
wcscpy_s(invArgData,cch, g_groupInfo[pTok->item.memberOf].selectedParam);
wcscat_s(invArgData, cch, L" ");
wcscat_s(invArgData, cch, g_argList.item[j].name);
wcscpy_s(invalidArg, LK_MAX_TOKENARG_LEN, invArgData);
free(invArgData);
return LK_E_CONTRADICTARG;
}
}
}
}
}
for (i = 0; i < g_groupCount; i++)
{
if (!g_groupInfo[i].selectedParam && !g_groupInfo[i].isOptional)
{
wcscpy_s(invalidArg, LK_MAX_TOKENARG_LEN, g_groupInfo[i].description);
return LK_E_NOGROUPMEMBERCHOSEN;
}
}
return LK_R_OK;
}
/* ---------------------------------------------------------------------------*/
LK_RESULT _Lk_MapNamedArgs() {
_LKTRACE("_Lk_MapNamedArgs\n");
LK_TOKEN_LIST_ENTRY* pTok = NULL;
size_t i = 0, j = 0;
for (i = 0; i < _Lk_TokenGetCount(); i++) {
_Lk_TokenGet(i, &pTok);
_LKTRACEW(L" Got token %Iu/%Iu name=%s \t[%s] (Opt=%d) \n", i + 1, _Lk_TokenGetCount(),
pTok->item.name, g_lkTypeStr[pTok->item.type], pTok->isOptional);
if (pTok->item.type == LK_TOKEN_TYPE_NAMED_ARGUMENT) {
// Search a command line argument to map with
// this named-argument token
for (j = 0; j < g_argList.count; j++) {
if (!(g_argList.item[j].isParam || g_argList.item[j].isMapped)) {
g_argList.item[j].isMapped = true;
g_argList.item[j].isProcessed = true;
break;
}
}
wcscpy_s(g_argList.item[j].name, LK_MAX_TOKENARG_LEN, pTok->item.name);
_LKTRACEW(L" Mapped named argument %s\n", pTok->item.name);
}
}
return LK_R_OK;
}
/*---------------------------------------------------------------------------*/
LK_RESULT _Lk_GetParamValue(const wchar_t* paramName, wchar_t** paramValue) {
if (!paramName | !paramValue | (paramName && wcslen(paramName) == 0))
return LK_E_INVALIDARG;
size_t i;
for (i = 0; i < g_argList.count; ++i) {
if (!_wcsicmp(paramName, g_argList.item[i].name)) {
if (g_argList.item[i].hasValue) {
*paramValue = g_argList.item[i].value;
return LK_R_OK;
} else {
*paramValue = NULL;
return LK_E_UNDEFINED;
}
}
}
*paramValue = NULL;
return LK_E_NOTFOUND;
}
/*---------------------------------------------------------------------------*/
LK_RESULT _Lk_EvalArgs(wchar_t* invalidArg) {
_LKTRACE("_Lk_EvalArgs\n");
LK_RESULT r = LK_R_OK;
if (LK_R_OK != (r = _Lk_EvalGroups(invalidArg)))
return r;
if (LK_R_OK != (r = _Lk_MapNamedArgs()))
return r;
size_t i;
LK_TOKEN_LIST_ENTRY* pTok = NULL;
for (i = 0; i < _Lk_TokenGetCount(); i++)
{
_Lk_TokenGet(i, &pTok);
if (pTok->item.memberOf != LK_NOGROUP && g_groupInfo[pTok->item.memberOf].selectedParam)
{
_LKTRACEW(L"_Lk_EvalArgs: IGNORING token %s: Member of group %d, but already got a parameter mapped\n",
pTok->item.name, pTok->item.memberOf);
continue;
}
switch (pTok->item.type)
{
case LK_TOKEN_TYPE_PARAMETER:
if (_Lk_FindArgumentName(pTok->item.name, true) == LK_R_FALSE && !pTok->isOptional)
{
_LKTRACEW(L"_Lk_EvalArgs: Required argument NOT FOUND: %s\n", pTok->item.name);
wcscpy_s(invalidArg, LK_MAX_TOKENARG_LEN, pTok->item.name);
return LK_E_MISSINGARG;
}
break;
case LK_TOKEN_TYPE_PARAMETER_VAL:
if (_Lk_FindArgumentName(pTok->item.name, true) == LK_R_FALSE && !pTok->isOptional)
{
_LKTRACEW(L"_Lk_EvalArgs: Required argument NOT FOUND: %s\n", pTok->item.name);
wcscpy_s(invalidArg, LK_MAX_TOKENARG_LEN, pTok->item.name);
return LK_E_MISSINGARG;
}
if (!pTok->isOptional)
{
wchar_t* paramVal;
if (_Lk_GetParamValue(pTok->item.name, ¶mVal) != LK_R_OK)
{
wcscpy_s(invalidArg, LK_MAX_TOKENARG_LEN, pTok->item.name);
return LK_E_MISSINGVALUE;
}
}
break;
case LK_TOKEN_TYPE_PARAMETER_VAL_OPT:
if (_Lk_FindArgumentName(pTok->item.name, true) == LK_R_FALSE && !pTok->isOptional) {
_LKTRACEW(L"_Lk_EvalArgs: Required argument NOT FOUND: %s\n", pTok->item.name);
wcscpy_s(invalidArg, LK_MAX_TOKENARG_LEN, pTok->item.name);
return LK_E_MISSINGARG;
}
break;
case LK_TOKEN_TYPE_NAMED_ARGUMENT:
break;
default:
assert(false);
break;
}
}
if (_Lk_FindUnprocessedArg(invalidArg) == LK_R_TRUE)
return LK_E_INVALIDARG;
return LK_R_OK;
}
/*---------------------------------------------------------------------------*/
LK_RESULT _Lk_VerifyParamNames(wchar_t* invalidArg) {
LK_TOKEN_LIST_ENTRY* pTok;
bool fOk;
for (size_t i = 0; i < g_argList.count; i++) {
if (g_argList.item[i].name[0] == L'/') {
fOk = false;
for (size_t j = 0; j < _Lk_TokenGetCount(); j++) {
_Lk_TokenGet(j, &pTok);
if (pTok->item.type == LK_TOKEN_TYPE_PARAMETER ||
pTok->item.type == LK_TOKEN_TYPE_PARAMETER_VAL ||
pTok->item.type == LK_TOKEN_TYPE_PARAMETER_VAL_OPT) {
if (! _wcsicmp(pTok->item.name, g_argList.item[i].name)) {
fOk = true;
break;
}
}
}
if (!fOk) {
wcscpy_s(invalidArg, LK_MAX_TOKENARG_LEN, g_argList.item[i].name);
return LK_E_INVALIDARG;
}
}
}
return LK_R_OK;
}
/*---------------------------------------------------------------------------*/
LK_RESULT _Lk_ValidateArgV(int argc, wchar_t** argv, wchar_t* invalidArg)
{
_LKTRACE("_Lk_ValidateArgV\n");
// Do first pass to arguments: verify duplicates, basic style, etc.
//
int i;
size_t j;
for (i = 1; i < argc; i++)
{
// Check if there are duplicates
_LKTRACE(" Verifying duplicates...\n");
for (j = 0; j < g_argList.count; ++j)
{
if ( (_wcsicmp(argv[i], g_argList.item[j].name) == 0)
&&(g_argList.item[j].name[0] == L'/'))
{
wcscpy_s(invalidArg, LK_MAX_TOKENARG_LEN, argv[i]);
_LKTRACEW(L" Duplicate %s argument found\n", argv[i]);
return LK_E_DUPLICATEARG;
}
}
// If argument includes '=' or ':', strip value and parameter name
if (argv[i][0] == L'/')
{
_LKTRACEW(L" %s: Parameter argument\n", argv[i]);
wchar_t* delim = NULL;
g_argList.item[j].isParam = true;
if ( (delim = wcschr(argv[i], L':')) ||
(delim = wcschr(argv[i], L'=')) )
{
if ( *(delim+1) == L'\0') // delimiter present but no value!
{
wcscpy_s(invalidArg, LK_MAX_TOKENARG_LEN, argv[i]);
return LK_E_MISSINGVALUE;
}
else
{
if (wcslen(delim + 1) > LK_MAX_PARAMVALUE_LEN)
{
wcsncpy_s(invalidArg, LK_MAX_TOKENARG_LEN, argv[i], delim - argv[i]);
return LK_E_VALUELENGTH;
}
wcsncpy_s(g_argList.item[j].name, LK_MAX_TOKENARG_LEN, argv[i],
delim - argv[i]);
wcscpy_s(g_argList.item[j].value, LK_MAX_PARAMVALUE_LEN,
delim + 1);
g_argList.item[j].hasValue = true;
}
}
else
{
wcscpy_s(g_argList.item[j].name, LK_MAX_TOKENARG_LEN, argv[i]);
g_argList.item[j].hasValue = false;
}
}
else // non-parameter argument
{
_LKTRACEW(L" %s: Non-Parameter argument \n", argv[i]);
g_argList.item[j].isParam = false;
g_argList.item[j].hasValue = true;
wcscpy_s(g_argList.item[j].value, LK_MAX_PARAMVALUE_LEN, argv[i]);
wcscpy_s(g_argList.item[j].name, LK_MAX_TOKENARG_LEN, LK_UNMAPPED_ARG_NAME);
}
g_argList.item[j].matchingGroup = LK_NOGROUP;
g_argList.item[j].isMapped = false;
g_argList.item[j].isProcessed = false;
g_argList.count = j+1;
}
return LK_R_OK;
}
void _Lk_StoreExtErr(LK_RESULT e, const wchar_t* arg)
{
for (size_t i = 0; i < LK_ARRAYSIZE(g_lkErrTable); ++i)
{
if (g_lkErrTable[i].res == e) {
if (arg)
swprintf(g_exErrBuffer, LK_MAX_ERRORBUFFER_LEN, g_lkErrTable[i].wErrMsg, arg);
else
wcscpy_s(g_exErrBuffer, LK_MAX_ERRORBUFFER_LEN, g_lkErrTable[i].wErrMsg);
return;
}
}
wcscpy_s(g_exErrBuffer, LK_MAX_ERRORBUFFER_LEN, L"Undefined error");
}
/******************************************************************************
Public API begins here
***************************************************************************/
/*---------------------------------------------------------------------------*/
LK_RESULT LkInit()
{
LK_RESULT r = _Lk_TokenListInit(LK_MAX_TOKENARG_COUNT);
LK_RETURN_STERR(r);
}
/*---------------------------------------------------------------------------*/
LK_RESULT LkFree(void)
{
_Lk_TokenListFree();
LK_RETURN_STERR(LK_R_OK);
}
/* ---------------------------------------------------------------------------
Sets the syntax for the command-line to be validated against.
syntax: The syntax specification string, with tokens separated by
spaces.
invalidToken: A pointer to a character array to store the token that
caused an error, if it can be determined.
cchBuf: The size of the invalidArg buffer, in characters. Must
be >= LK_MAX_TOKENARG_LEN.
Returns: LK_R_OK if successful.
LK_E_MALLOC if memory allocation error occurred.
LK_E_TOKENCOUNT if number of specified token exceeds the constant
LK_MAX_TOKENARG_COUNT.
LK_E_UNINITIALIZED if library is not initialized yet.
LK_E_NULLPOINTER if NULL pointer is passed in call.
LK_E_INVALIDSYNTAX if syntax string is not allowed.
Other error codes can be returned. (See LK_RESULT)
--------------------------------------------------------------------------*/
LK_RESULT LkSetSyntax(const wchar_t* syntax, wchar_t* invalidToken, size_t cchBuf)
{
_LKTRACE("_Lk_SetSyntax\n");
if (cchBuf < LK_MAX_TOKENARG_LEN)
LK_RETURN_STERR(LK_E_INSUFFICIENTBUFFER);
if (!g_tokenList)
LK_RETURN_STERR(LK_E_UNINITIALIZED);
if (!syntax || !invalidToken)
LK_RETURN_STERR(LK_E_NULLPOINTER);
_Lk_TokenListClear();
g_syntaxValid = true;
g_tokenCount = 0;
g_groupCount = 0;
LK_RESULT res;
int i = 0;
int iTokStart = 0;
while(1)
{
if (syntax[i] == L' ' || syntax[i] == L'\0')
{
size_t cbToken = sizeof(wchar_t) * (i - iTokStart);
if (cbToken > LK_MAX_TOKENARG_LEN * sizeof(wchar_t))
LK_RETURN_STERR(LK_E_TOKENLENGTH);
wchar_t* token = (wchar_t * )malloc(cbToken + sizeof(wchar_t));
if (!token)
LK_RETURN_STERR(LK_E_MALLOC);
memcpy(token, syntax + iTokStart, cbToken);
token[cbToken/sizeof(wchar_t)] = L'\0';
res = _Lk_ClassifyToken(token);
if (LK_SUCCESS(res)){
free(token);
}
else
{
_LKTRACEW(L" Token classification fail (%s) (0x%08x)\n", token, res);
g_syntaxValid = false;
wcscpy_s(invalidToken, LK_MAX_TOKENARG_LEN, token);
return res;
}
if (syntax[i] == L'\0')
break;
while (syntax[++i] == L' '); /* Ignore multiple spaces */
iTokStart = i;
if (g_tokenCount == LK_MAX_TOKENARG_COUNT)
LK_RETURN_STERR(LK_E_TOKENCOUNT);
}
else
i++;
};
_LKTRACEW(L" Function successful\n");
LK_RETURN_STERR(LK_R_OK);
}
/* ---------------------------------------------------------------------------
Returns if the specified parameter or named-argument is present.