autocomplete.h (2372B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * Broadcom autocomplete 8 */ 9 10 #ifndef _INCLUDE_AUTOCOMPLETE_H 11 #define _INCLUDE_AUTOCOMPLETE_H 12 13 14 #define AUTOCOMPLETE_MAX_STRING_LEN 1024 15 16 typedef struct autocomplete_node_s { 17 char *keyword; 18 struct autocomplete_node_s *first_child; 19 struct autocomplete_node_s *next_sibling; 20 struct autocomplete_node_s *parent; 21 char is_option; 22 char is_checked; 23 } autocomplete_node_t; 24 25 /* 26 * use this function to add nodes to the autocomplete mechanism 27 * for adding children to this node, use the return value of this function 28 * use null for new tree of autocompletion 29 * 30 * for example, this calls sequence 31 * 32 * ac_node1 = autocomplete_node_add(unit, NULL, "kw1"); 33 * ac_node2 = autocomplete_node_add(unit, NULL, "kw2"); 34 * autocomplete_node_add(unit, ac_node1, "kw11"); 35 * autocomplete_node_add(unit, ac_node1, "kw12"); 36 * autocomplete_node_add(unit, ac_node2, "kw21"); 37 * 38 * will create this tree: 39 * 40 * root 41 * / \ 42 * / \ 43 * kw1 kw2 44 * / \ \ 45 * kw11 kw12 kw21 46 */ 47 48 autocomplete_node_t *autocomplete_node_add(int unit, autocomplete_node_t *parent, char *keyword, char is_option); 49 50 autocomplete_node_t *autocomplete_find_root(int unit, char *keyword); 51 52 53 /* find node by array of strings terminating with NULL 54 * for example, if we want to find the node kw21 in the drawing above 55 * we can find it this way: 56 * char *keywords[3]; 57 * autocomplete_node_t *kw21; 58 * keywords[0] = "kw2"; 59 * keywords[1] = "kw21"; 60 * keywords[2] = NULL; 61 * kw21 = autocomplete_find_node(unit, keywords); 62 * 63 * the function returns the node or NULL if not found 64 */ 65 autocomplete_node_t *autocomplete_find_node(int unit, char **keywords); 66 67 /* 68 * use this function to remove that has been created by autocomplete_node_add 69 * this function remove all of the tree recursively 70 */ 71 72 void autocomplete_node_delete(int unit, autocomplete_node_t *node); 73 74 /* 75 * use this function to remove the whole tree (per unit) of the autocomplete 76 */ 77 78 void autocomplete_delete_all(int unit); 79 80 /* 81 * print autocompletion suggestions 82 */ 83 char* autocomplete_print(int unit, char* input, char* prompt); 84 85 #endif /* _INCLUDE_AUTOCOMPLETE_H */