-
Notifications
You must be signed in to change notification settings - Fork 3
/
auto_comp.c
1046 lines (926 loc) · 28 KB
/
auto_comp.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <dirent.h>
#include <strings.h>
#include "filebench.h"
#include "auto_comp.h"
#define VARNAME_MAXLEN 128
#define FILENAME_MAXLEN 128
#define MALLOC_STEP 64
#define CSUF_CMD " "
#define CSUF_ARG " "
#define CSUF_LVARNAME "="
#define CSUF_RVARNAME ","
#define CSUF_ATTRNAME "="
#define ATTR_LIST_SEP ','
#define ATTR_ASSIGN_OP '='
#define VAR_ASSIGN_OP '='
#define VAR_PREFIX '$'
typedef char ac_fname_t[FILENAME_MAXLEN];
typedef struct ac_fname_cache {
ac_fname_t *fnc_buf;
int fnc_bufsize;
time_t fnc_mtime;
} ac_fname_cache_t;
typedef enum ac_match_result {
MATCH_DONE,
MATCH_CONT
} ac_match_result_t;
/*
* We parse an user input line into multiple blank separated strings.
* The last string is always the one user wants to complete, the other
* preceding strings set up the context on how to complete the last one.
*
* ac_str_t repsents one such a string, which can be of the following
* types:
*
* STRTYPE_COMPLETE - the string is one of the preceding strings.
* STRTYPE_INCOMPLETE - the string is the one being completed, user
* has inputted at least one character for it.
* STRTYPE_NULL - the string is the one being completed, user
* has inputted nothing for it.
*
* ac_str_t structure has the following members:
*
* startp - the start position of the string in the user input buffer
* endp - the end position of the string in the user input buffer
* strtype - the type of the string. It can be of the following values:
* STRTYPE_COMPLETE, STRTYPE_INCOMPLETE, STRTYPE_NULL,
* and STRTYPE_INVALID.
*/
typedef enum ac_strtype {
STRTYPE_COMPLETE,
STRTYPE_INCOMPLETE,
STRTYPE_NULL,
STRTYPE_INVALID
} ac_strtype_t;
typedef struct ac_str {
const char *startp;
const char *endp;
ac_strtype_t strtype;
} ac_str_t;
#define STR_NUM 3
typedef struct ac_inputline {
ac_str_t strs[STR_NUM];
} ac_inputline_t;
/*
* ac_iter represents a general interface to access a list of values for
* matching user input string. The structure has the following methods:
*
* bind - bind the iterator to a list, and save a pointer to user
* passed space in nlistpp.
* reset - reset internal index pointer to point to list head.
* get_nextstr - this is the method that does the real work. It
* walks through the list and returns string associated with
* the current item. It can also return some other thing the
* caller is interested via the user passed space pointed by
* nlistpp. In our case, that is a pointer to a list which
* contains all possible values for the next string in user
* input.
*
* It has the following data members:
*
* listp - a pointer to the list to be iterated through
* curp - index pointer to maintain position when iterating
* nlistpp - a pointer to user passed space for returning a list of
* values for the next string in user input
*/
typedef struct ac_iter {
void *listp;
void *curp;
void *nlistpp;
void (*bind)(struct ac_iter *, void *, void *);
void (*reset)(struct ac_iter *);
const char *(*get_nextstr)(struct ac_iter *);
} ac_iter_t;
/*
* We consider a filebench command is composed of a sequence of tokens
* (ie., command name, argument name, attribute name, etc.). Many of
* these tokens have limited string values. These values, as well as
* their dependencies, are used to complete user input string.
*
* There are the following tokens:
*
* TOKTYPE_CMD - command name
* TOKTYPE_ARG - argument name
* TOKTYPE_ATTRNAME - attribute name
* TOKTYPE_ATTRVAL - attribute value
* TOKTYPE_LVARNAME - variable name, used on left side of assign
* operator
* TOKTYPE_RVARNAME - variable name, used on right side of assign
* operator
* TOKTYPE_VARVAL - variable value
* TOKTYPE_LOADFILE - load file name
* TOKTYPE_ATTRLIST - pseudo token type for attribute list
* TOKTYPE_VARLIST - pseudo token type for variable list
* TOKTYPE_NULL - pseudo token type for aborting auto-completion
*
* The reason why there are two different token types for variable name
* is because, depending on its position, there are different requirements
* on how to do completion and display matching results. See more details
* in lvarname_iter and rvarname_iter definition.
*
* Attribute list and variable list are not really a single token. Instead
* they contain multiple tokens, and thus have different requirements on
* how to complete them. TOKTYPE_ATTRLIST and TOKTYPE_VARLIST are
* introduced to to solve this issue. See more details below on
* get_curtok() function in ac_tokinfo_t structure.
*
* ac_tokval_t represents a string value a token can have. The structure
* also contains a pointer to a ac_tvlist_t structure, which represents
* all possible values for the next token in the same command.
*
* str - the token's string value
* nlistp - a list which contains string values for the token
* that follows
*
* ac_tvlist_t represents all possible values for a token. These values
* are stored in an ac_tokval_t array. The structure also has a member
* toktype, which is used to index an ac_tokinfo_t array to get the
* information on how to access and use the associated value list.
*
* vals - a list of string values for this token
* toktype - the token's type
*
* ac_tokinfo_t contains information on how to access and use the
* string values of a specific token. Among them, the most important
* thing is an iterator to access the value list. The reason to use
* iterator is to encapsulate list implementation details. That is
* necessary because some tokens have dynamic values(for example,
* argument of load command), which cannot be predefined using
* ac_tokval_t array.
*
* ac_tokinfo_t structure has the following members:
*
* toktype - token type
* iter - iterator to access the token's value list
* cont_suffix - continuation suffix for this token. See note 1
* below on what is continuation suffix.
* get_curtok - a function to parse a multi-token user string.
* It parse that string and returns the word being
* completed and its token type. See note 2 below.
*
* Notes:
*
* 1) Continuation suffix is a convenient feature provided by libtecla.
* A continuation suffix is a string which is automatically appended
* to a fully completed string. For example, if a command name is
* fully completed, a blank space will be appended to it. This is
* very convenient because it not only saves typing, but also gives
* user an indication to continue.
*
* 2) get_curtok() function is a trick to support user input strings
* which have multiple tokens. Take attribute list as an example,
* although we defined a token type TOKTYPE_ATTRLIST for it, it is
* not a token actually, instead it contains multiple tokens like
* attribute name, attribute value, etc., and attribute value can
* be either a literal string or a variable name prefixed with a
* '$' sign. For this reason, get_curtok() function is needed to
* parse that string to get the word being completed and its token
* type so that we can match the word against with the proper value
* list.
*/
typedef enum ac_toktype {
TOKTYPE_CMD,
TOKTYPE_ARG,
TOKTYPE_ATTRNAME,
TOKTYPE_ATTRVAL,
TOKTYPE_LVARNAME,
TOKTYPE_RVARNAME,
TOKTYPE_VARVAL,
TOKTYPE_LOADFILE,
TOKTYPE_ATTRLIST,
TOKTYPE_VARLIST,
TOKTYPE_NULL
} ac_toktype_t;
typedef ac_toktype_t (*ac_get_curtok_func_t)(ac_str_t *);
typedef struct ac_tokinfo {
ac_toktype_t toktype;
ac_iter_t *iter;
char *cont_suffix;
ac_get_curtok_func_t get_curtok;
} ac_tokinfo_t;
typedef struct ac_tokval {
char *str;
struct ac_tvlist *nlistp;
} ac_tokval_t;
typedef struct ac_tvlist {
ac_tokval_t *vals;
ac_toktype_t toktype;
} ac_tvlist_t;
/*
* Variables and prototypes
*/
static void common_bind(ac_iter_t *, void *, void *);
static void common_reset(ac_iter_t *);
static void varname_bind(ac_iter_t *, void *, void *);
static void loadfile_bind(ac_iter_t *, void *, void *);
static const char *get_next_tokval(ac_iter_t *);
static const char *get_next_lvarname(ac_iter_t *);
static const char *get_next_rvarname(ac_iter_t *);
static const char *get_next_loadfile(ac_iter_t *);
static ac_toktype_t parse_attr_list(ac_str_t *);
static ac_toktype_t parse_var_list(ac_str_t *);
static ac_iter_t tokval_iter = {
NULL,
NULL,
NULL,
common_bind,
common_reset,
get_next_tokval
};
static ac_iter_t lvarname_iter = {
NULL,
NULL,
NULL,
varname_bind,
common_reset,
get_next_lvarname
};
static ac_iter_t rvarname_iter = {
NULL,
NULL,
NULL,
varname_bind,
common_reset,
get_next_rvarname
};
static ac_iter_t loadfile_iter = {
NULL,
NULL,
NULL,
loadfile_bind,
common_reset,
get_next_loadfile
};
/*
* Note: We use toktype to index into this array, so for each toktype,
* there must be one element in the array, and in the same order
* as that toktype is defined in ac_toktype.
*/
static ac_tokinfo_t token_info[] = {
{ TOKTYPE_CMD, &tokval_iter, CSUF_CMD, NULL },
{ TOKTYPE_ARG, &tokval_iter, CSUF_ARG, NULL },
{ TOKTYPE_ATTRNAME, &tokval_iter, CSUF_ATTRNAME, NULL },
{ TOKTYPE_ATTRVAL, NULL, NULL, NULL },
{ TOKTYPE_LVARNAME, &lvarname_iter, CSUF_LVARNAME, NULL },
{ TOKTYPE_RVARNAME, &rvarname_iter, CSUF_RVARNAME, NULL },
{ TOKTYPE_VARVAL, NULL, NULL, NULL },
{ TOKTYPE_LOADFILE, &loadfile_iter, CSUF_ARG, NULL },
{ TOKTYPE_ATTRLIST, NULL, NULL, parse_attr_list },
{ TOKTYPE_VARLIST, NULL, NULL, parse_var_list },
{ TOKTYPE_NULL, NULL, NULL, NULL }
};
static ac_tokval_t event_attrnames[] = {
{ "rate", NULL},
{ NULL, NULL}
};
static ac_tvlist_t event_attrs = {
event_attrnames,
TOKTYPE_ATTRLIST
};
static ac_tokval_t file_attrnames[] = {
{ "path", NULL },
{ "reuse", NULL },
{ "prealloc", NULL },
{ "paralloc", NULL },
{ NULL, NULL }
};
static ac_tvlist_t file_attrs = {
file_attrnames,
TOKTYPE_ATTRLIST
};
static ac_tokval_t fileset_attrnames[] = {
{ "size", NULL },
{ "path", NULL },
{ "dirwidth", NULL },
{ "prealloc", NULL },
{ "filesizegamma", NULL },
{ "dirgamma", NULL },
{ "cached", NULL },
{ "entries", NULL },
{ NULL, NULL }
};
static ac_tvlist_t fileset_attrs = {
fileset_attrnames,
TOKTYPE_ATTRLIST
};
static ac_tokval_t process_attrnames[] = {
{ "nice", NULL },
{ "instances", NULL },
{ NULL, NULL }
};
static ac_tvlist_t process_attrs = {
process_attrnames,
TOKTYPE_ATTRLIST
};
static ac_tokval_t create_argnames[] = {
{ "file", NULL },
{ "fileset", NULL },
{ "process", NULL },
{ NULL, NULL }
};
static ac_tvlist_t create_args = {
create_argnames,
TOKTYPE_ARG
};
static ac_tokval_t define_argnames[] = {
{ "file", &file_attrs },
{ "fileset", &fileset_attrs },
{ "process", &process_attrs },
{ NULL, NULL }
};
static ac_tvlist_t define_args = {
define_argnames,
TOKTYPE_ARG
};
static ac_tvlist_t load_args = {
NULL,
TOKTYPE_LOADFILE
};
static ac_tvlist_t set_args = {
NULL,
TOKTYPE_VARLIST
};
static ac_tokval_t shutdown_argnames[] = {
{ "process", NULL },
{ NULL, NULL }
};
static ac_tvlist_t shutdown_args = {
shutdown_argnames,
TOKTYPE_ARG
};
static ac_tokval_t stats_argnames[] = {
{ "clear", NULL },
{ "directory", NULL },
{ "command", NULL },
{ "dump", NULL },
{ "xmldump", NULL },
{ NULL, NULL }
};
static ac_tvlist_t stats_args = {
stats_argnames,
TOKTYPE_ARG
};
static ac_tokval_t fb_cmdnames[] = {
{ "create", &create_args },
{ "define", &define_args },
{ "debug", NULL },
{ "echo", NULL },
{ "eventgen", &event_attrs },
{ "foreach", NULL },
{ "help", NULL },
{ "list", NULL },
{ "load", &load_args },
{ "log", NULL },
{ "quit", NULL },
{ "run", NULL },
{ "set", &set_args },
{ "shutdown", &shutdown_args },
{ "sleep", NULL },
{ "stats", &stats_args },
{ "system", NULL },
{ "usage", NULL },
{ "vars", NULL },
{ "version", NULL },
{ NULL, NULL },
};
static ac_tvlist_t fb_cmds = {
fb_cmdnames,
TOKTYPE_CMD
};
static ac_fname_cache_t loadnames = { NULL, 0, 0 };
static int search_loadfiles(ac_fname_cache_t *);
static void parse_user_input(const char *, int, ac_inputline_t *);
static int compare_string(ac_str_t *, const char *, boolean_t, const char **);
static ac_match_result_t match_string(WordCompletion *, const char *, int,
ac_str_t *, ac_iter_t *, const char *);
/*
* Bind the iterator to the passed list
*/
static void
common_bind(ac_iter_t *iterp, void *listp, void *nlistpp)
{
iterp->listp = listp;
iterp->nlistpp = nlistpp;
}
/*
* Reset index pointer to point to list head
*/
static void
common_reset(ac_iter_t *iterp)
{
iterp->curp = iterp->listp;
}
/*
* Walk through an array of ac_tokval_t structures and return string
* of each item.
*/
static const char *
get_next_tokval(ac_iter_t *iterp)
{
ac_tokval_t *listp = iterp->listp; /* list head */
ac_tokval_t *curp = iterp->curp; /* index pointer */
/* user passed variable for returning value list for next token */
ac_tvlist_t **nlistpp = iterp->nlistpp;
const char *p;
if (listp == NULL || curp == NULL)
return (NULL);
/* get the current item's string */
p = curp->str;
/*
* save the current item's address into a user passed variable
*/
if (nlistpp != NULL)
*nlistpp = curp->nlistp;
/* advance the index pointer */
iterp->curp = ++curp;
return (p);
}
/*
* Bind the iterator to filebench_shm->shm_var_list
*/
/* ARGSUSED */
static void
varname_bind(ac_iter_t *iterp, void *listp, void * nlistpp)
{
iterp->listp = filebench_shm->shm_var_list;
iterp->nlistpp = nlistpp;
}
/*
* Walk through a linked list of var_t type structures and return name
* of each variable with a preceding '$' sign
*/
static const char *
get_next_lvarname(ac_iter_t *iterp)
{
static char buf[VARNAME_MAXLEN];
var_t *listp = iterp->listp; /* list head */
var_t *curp = iterp->curp; /* index pointer */
/* User passed variable for returning value list for next token */
ac_tvlist_t **nlistpp = iterp->nlistpp;
const char *p;
if (listp == NULL || curp == NULL)
return (NULL);
/* Get current variable's name, copy it to buf, with a '$' prefix */
p = curp->var_name;
(void) snprintf(buf, sizeof (buf), "$%s", p);
/* No information for the next input string */
if (nlistpp != NULL)
*nlistpp = NULL;
/* Advance the index pointer */
iterp->curp = curp->var_next;
return (buf);
}
/*
* Walk through a linked list of var_t type structures and return name
* of each variable
*/
static const char *
get_next_rvarname(ac_iter_t *iterp)
{
var_t *listp = iterp->listp; /* list head */
var_t *curp = iterp->curp; /* index pointer */
/* User passed variable for returning value list for next item */
ac_tvlist_t **nlistpp = iterp->nlistpp;
const char *p;
if (listp == NULL || curp == NULL)
return (NULL);
/* Get current variable's name */
p = curp->var_name;
/* No information for the next input string */
if (nlistpp != NULL)
*nlistpp = NULL;
/* Advance the index pointer */
iterp->curp = curp->var_next;
return (p);
}
/*
* Bind the iterator to loadnames.fnc_buf, which is an ac_fname_t array
* and contains up-to-date workload file names. The function calls
* search_loadfiles() to update the cache before the binding.
*/
/* ARGSUSED */
static void
loadfile_bind(ac_iter_t *iterp, void *listp, void * nlistpp)
{
/* Check loadfile name cache, update it if needed */
(void) search_loadfiles(&loadnames);
iterp->listp = loadnames.fnc_buf;
iterp->nlistpp = nlistpp;
}
/*
* Walk through a string(ac_fname_t, more exactly) array and return each
* string, until a NULL iterm is encountered.
*/
static const char *
get_next_loadfile(ac_iter_t *iterp)
{
ac_fname_t *listp = iterp->listp; /* list head */
ac_fname_t *curp = iterp->curp; /* index pointer */
/* User passed variable for returning value list for next item */
ac_tvlist_t **nlistpp = iterp->nlistpp;
const char *p;
if (listp == NULL || curp == NULL)
return (NULL);
/*
* Get current file name. If an NULL item is encountered, it means
* this is the end of the list. In that case, we need to set p to
* NULL to indicate to the caller that the end of the list is reached.
*/
p = (char *)curp;
if (*p == '\0')
p = NULL;
/* No information for the next input string */
if (nlistpp != NULL)
*nlistpp = NULL;
/* Advance the index pointer */
iterp->curp = ++curp;
return (p);
}
/*
* Search for available workload files in workload direcotry and
* update workload name cache.
*/
static int
search_loadfiles(ac_fname_cache_t *fnamecache)
{
DIR *dirp;
struct dirent *fp;
struct stat dstat;
time_t mtime;
ac_fname_t *buf;
int bufsize = MALLOC_STEP;
int len, i;
if (stat(FILEBENCHDIR"/workloads", &dstat) != 0)
return (-1);
mtime = dstat.st_mtime;
/* Return if there is no change since last time */
if (mtime == fnamecache->fnc_mtime)
return (0);
/* Get loadfile names and cache it */
if ((buf = malloc(sizeof (ac_fname_t) * bufsize)) == NULL)
return (-1);
if ((dirp = opendir(FILEBENCHDIR"/workloads")) == NULL)
return (-1);
i = 0;
while ((fp = readdir(dirp)) != NULL) {
len = strlen(fp->d_name);
if (len <= 2 || (fp->d_name)[len - 2] != '.' ||
(fp->d_name)[len - 1] != 'f')
continue;
if (i == bufsize) {
bufsize += MALLOC_STEP;
if ((buf = realloc(buf, sizeof (ac_fname_t) *
bufsize)) == NULL)
return (-1);
}
(void) snprintf(buf[i], FILENAME_MAXLEN, "%s", fp->d_name);
if (len -2 <= FILENAME_MAXLEN - 1) {
/* Remove .f suffix in file name */
buf[i][len -2] = '\0';
}
i++;
}
/* Added a NULL iterm as the array's terminator */
buf[i][0] = '\0';
if (fnamecache->fnc_bufsize != 0)
free(fnamecache->fnc_buf);
fnamecache->fnc_buf = buf;
fnamecache->fnc_bufsize = bufsize;
fnamecache->fnc_mtime = mtime;
return (0);
}
/*
* Parse user input line into a list of blank separated strings, and
* save the result in the passed ac_inputline_t structure. line and word_end
* parameters are passed from libtecla library. line points to user input
* buffer, and word_end is the index of the last character of user input.
*/
/* ARGSUSED */
static void
parse_user_input(const char *line, int word_end, ac_inputline_t *input)
{
const char *p = line;
int i;
/* Reset all fileds */
for (i = 0; i < STR_NUM; i++) {
input->strs[i].startp = NULL;
input->strs[i].endp = NULL;
input->strs[i].strtype = STRTYPE_INVALID;
}
/*
* Parse user input. We don't use word_end to do boundary checking,
* instead we take advantage of the fact that the passed line
* parameter is always terminated by '\0'.
*/
for (i = 0; i < STR_NUM; i++) {
/* Skip leading blank spaces */
while (*p == ' ')
p++;
if (*p == '\0') {
/*
* User input nothing for the string being input
* before he pressed TAB. We use STR_NULL flag
* to indicate this so that match_str() will list
* all available candidates.
*/
input->strs[i].startp = p;
input->strs[i].strtype = STRTYPE_NULL;
return;
}
/* Recoard the start and end of the string */
input->strs[i].startp = p;
while ((*p != ' ') && (*p != '\0'))
p++;
input->strs[i].endp = p - 1;
if (*p == '\0') {
input->strs[i].strtype = STRTYPE_INCOMPLETE;
return;
} else {
/* The string is followed by a blank space */
input->strs[i].strtype = STRTYPE_COMPLETE;
}
}
}
/*
* Parse an input string which is an attribue list, get the current word
* user wants to complete, and return its token type.
*
* An atribute list has the following format:
*
* name1=val,name2=$var,...
*
* The function modifies the passed acstr string on success to point to
* the word being completed.
*/
static ac_toktype_t
parse_attr_list(ac_str_t *acstr)
{
const char *p;
if (acstr->strtype == STRTYPE_COMPLETE) {
/*
* User has input a complete string for attribute list
* return TOKTYPE_NULL to abort the matching.
*/
return (TOKTYPE_ATTRLIST);
} else if (acstr->strtype == STRTYPE_NULL) {
/*
* User haven't input anything for the attribute list,
* he must be trying to list all attribute names.
*/
return (TOKTYPE_ATTRNAME);
}
/*
* The string may contain multiple comma separated "name=value"
* items. Try to find the last one and move startp to point to it.
*/
for (p = acstr->endp; p >= acstr->startp && *p != ATTR_LIST_SEP; p--) {}
if (p == acstr->endp) {
/*
* The last character of the string is ',', which means
* user is trying to list all attribute names.
*/
acstr->startp = p + 1;
acstr->strtype = STRTYPE_NULL;
return (TOKTYPE_ATTRNAME);
} else if (p > acstr->startp) {
/*
* Found ',' between starp and endp, move startp pointer
* to point to the last item.
*/
acstr->startp = p + 1;
}
/*
* Now startp points to the last "name=value" item. Search in
* the characters user has input for this item:
*
* a) if there isn't '=' character, user is inputting attribute name
* b) if there is a '=' character and it is followed by a '$',
* user is inputting variable name
* c) if there is a '=' character and it isn't followed by a '$',
* user is inputting a literal string as attribute value.
*/
for (p = acstr->startp; p <= acstr->endp; p++) {
if (*p == ATTR_ASSIGN_OP) {
/* Found "=" operator in the string */
if (*(p + 1) == VAR_PREFIX) {
acstr->startp = p + 2;
if (*acstr->startp != '\0')
acstr->strtype = STRTYPE_INCOMPLETE;
else
acstr->strtype = STRTYPE_NULL;
return (TOKTYPE_RVARNAME);
} else {
return (TOKTYPE_ATTRVAL);
}
}
}
/* Didn't find '=' operator, the string must be an attribute name */
return (TOKTYPE_ATTRNAME);
}
/*
* Parse an input string which is a variable list, get the current word
* user wants to complete, and return its token type.
*
* A varaible list has the following format:
*
* $varname=value
*
* The function modifies the passed acstr string on success to point to
* the word being completed.
*/
static ac_toktype_t
parse_var_list(ac_str_t *acstr)
{
const char *p;
if (acstr->strtype == STRTYPE_COMPLETE) {
/*
* User has input a complete string for var list
* return TOKTYPE_NULL to abort the matching.
*/
return (TOKTYPE_NULL);
} else if (acstr->strtype == STRTYPE_NULL) {
/*
* User haven't input anything for the attribute list,
* he must be trying to list all available var names.
*/
return (TOKTYPE_LVARNAME);
}
/*
* Search in what user has input:
*
* a) if there isn't a '=' character, user is inputting var name
* b) if there is a '=' character, user is inputting var value
*/
for (p = acstr->startp; p <= acstr->endp; p++) {
if (*p == VAR_ASSIGN_OP)
return (TOKTYPE_VARVAL);
}
/* Didn't find '=' operator, user must be inputting an var name */
return (TOKTYPE_LVARNAME);
}
/*
* Compare two strings acstr and str. acstr is a string of ac_str_t type,
* str is a normal string. If issub is B_TRUE, the function checks if
* acstr is a sub-string of str, starting from index 0; otherwise it checks
* if acstr and str are exactly the same.
*
* The function returns 0 on success and -1 on failure. When it succeeds,
* it also set restp to point to the rest part of the normal string.
*/
static int
compare_string(ac_str_t *acstr, const char *str, boolean_t issub,
const char **restp)
{
const char *p, *q;
for (p = acstr->startp, q = str; (p <= acstr->endp) && (*q != '\0');
p++, q++) {
if (*p != *q)
return (-1);
}
if (p == acstr->endp + 1) {
if (*q == '\0' || issub == B_TRUE) {
if (restp != NULL)
*restp = q;
return (0);
}
}
return (-1);
}
/*
* Use the passed iterp iterator to access a list of string values to
* look for those matches with acstr, an user input string to be completed.
*
* cpl, line, work_end, and cont_suffix are parameters needed by
* cpl_add_completion(), which adds matched entries to libtecla.
*
* Since user input line may have multiple strings, the function is
* expected to be called multiple times to match those strings one
* by one until the last one is reached.
*
* The multi-step matching process also means the function should provide
* a way to indicate to the caller whether to continue or abort the
* whole matching process. The function does that with the following
* return values:
*
* MATCH_DONE - the matching for the whole user input is done. This
* can mean either some items are found or none is found.
* In either case, the caller shouldn't continue to
* match the rest strings, either because there is
* no strings left, or because the matching for the
* current string failed so there is no need to check
* further.
* MATCH_CONT - the matching for the current string succeeds, but
* user needs to continue to match the rest strings.
*/
static ac_match_result_t
match_string(WordCompletion *cpl, const char *line, int word_end,
ac_str_t *acstr, ac_iter_t *iterp, const char *cont_suffix)
{
const char *str, *restp;
iterp->reset(iterp);
if (acstr->strtype == STRTYPE_COMPLETE) {
while ((str = iterp->get_nextstr(iterp)) != NULL) {
if (!compare_string(acstr, str, B_FALSE, NULL)) {
/* Continue to check rest strings */
return (MATCH_CONT);
}
}
} else if (acstr->strtype == STRTYPE_NULL) {
/* User input nothing. List all available strings */
while ((str = iterp->get_nextstr(iterp)) != NULL) {
(void) cpl_add_completion(cpl, line,
acstr->startp - line, word_end, str,
NULL, cont_suffix);
}
} else if (acstr->strtype == STRTYPE_INCOMPLETE) {
while ((str = iterp->get_nextstr(iterp)) != NULL) {
if (!compare_string(acstr, str, B_TRUE, &restp)) {
/* It matches! Add it. */
(void) cpl_add_completion(cpl, line,
acstr->startp - line, word_end, restp,
NULL, cont_suffix);
}
}
}
return (MATCH_DONE);
}
/*
* This is the interface between filebench and libtecla for auto-
* completion. It is called by libtecla whenever user initiates a
* auto-completion request(ie., pressing TAB key).
*
* The function calls parse_user_input() to parse user input into
* multiple strings, then it calls match_string() to match each
* string in user input in sequence until either the last string
* is reached and completed or the the matching fails.
*/
/* ARGSUSED */
CPL_MATCH_FN(command_complete)
{
ac_inputline_t inputline;