Initial import from http://www.droids-corp.org/hg/libcmdline/rev/db316e4289a1
[libcmdline.git] / src / genconf / confnode.h
1 #ifndef _CONFNODE_H_
2 #define _CONFNODE_H_
3
4 /***************/
5 /* node type specific functions */
6 /***************/
7
8 typedef int (confnode_new_t)(struct confnode *n, const struct line *line);
9 typedef void (confnode_free_t)(struct confnode *n);
10 typedef int (confnode_add_attr_t)(struct confnode *n, const struct line *line,
11                                   const char *linebuf);
12 typedef int (confnode_close_dir_t)(struct confnode *n, const struct line *line);
13 typedef int (confnode_dotconfig_write_t)(const struct confnode *n, FILE *f);
14 typedef int (confnode_strvalue_to_boolvalue_t)(const struct confnode *n,
15                                                const char *strvalue);
16 typedef int (confnode_set_user_strvalue_t)(struct confnode *n,
17                                            const char *strvalue);
18 typedef int (confnode_get_user_strvalue_t)(const struct confnode *n, char *buf,
19                                            unsigned buflen);
20 typedef int (confnode_set_default_strvalue_t)(struct confnode *n,
21                                               const char *strvalue);
22 typedef int (confnode_get_default_strvalue_t)(const struct confnode *n, char *buf,
23                                               unsigned buflen);
24 typedef const char *(confnode_get_type_str_t)(const struct confnode *n);
25 typedef void (confnode_display_short_t)(const struct confnode *n);
26 typedef void (confnode_display_long_t)(const struct confnode *n);
27
28 /* all node-specific operations */
29 struct confnode_ops {
30         /* alloc / free */
31         confnode_new_t *new;
32         confnode_free_t *free;
33
34         /* conf parse */
35         confnode_add_attr_t *add_attr;
36         confnode_close_dir_t *close_dir;
37
38         /* dotconfig */
39         confnode_dotconfig_write_t *dotconfig_write;
40
41         /* convert value */
42         confnode_strvalue_to_boolvalue_t *strvalue_to_boolvalue;
43
44         /* user value */
45         confnode_set_user_strvalue_t *set_user_strvalue;
46         confnode_get_user_strvalue_t *get_user_strvalue;
47
48         /* default value */
49         confnode_set_default_strvalue_t *set_default_strvalue;
50         confnode_get_default_strvalue_t *get_default_strvalue;
51
52         /* display */
53         confnode_get_type_str_t *get_type_str;
54         confnode_display_short_t *display_short;
55         confnode_display_long_t *display_long;
56 };
57
58 /***************/
59 /* generic node functions */
60 /***************/
61
62 /* alloc a new root node */
63 struct confnode *confnode_new_root(void);
64
65 /* Parse the line, and return a new confnode if the line is a valid
66  * new confnode line (example: "menuconfig FOO\n"). Else, return
67  * NULL. */
68 struct confnode *confnode_new(const struct line *line);
69
70 /* Free the node given as argument, and all its associated data */
71 void confnode_free(struct confnode *n);
72
73 /* Parse a line to get an attribute of confnode n. Return 0 if it
74  * matches an attribute for this node, else return -1. */
75 int confnode_add_attr(struct confnode *n, const struct line *line,
76                       const char *linebuf);
77
78 /* Parse a line and check if it closes the current node,
79  * "endmenu" closes "menu" node): in this case return 0. Else, return
80  * -1. */
81 int confnode_close_dir(struct confnode *n, const struct line *line);
82
83 /* write config value in file f in a dotconfig-like manner. Return 0
84  * on success. */
85 int confnode_dotconfig_write(const struct confnode *n, FILE *f);
86
87 /* Convert string value into boolean value (mainly used for
88  * dependancies). Return -1 on error, else return the boolean value (0
89  * or 1). */
90 int confnode_strvalue_to_boolvalue(const struct confnode *n, const char *strvalue);
91
92 /* Get the string value that the user asked to set for this node
93  * 'n'. This function does not check if dependancies are met. Return 0
94  * on success, in this case the string is copied in buffer 'buf' of
95  * len 'buflen'. Return -1 if the value cannot be read. */
96 int confnode_get_user_strvalue(const struct confnode *n, char *buf, unsigned buflen);
97
98 /* Set the string value of the node n. Return 0 on success, or -1 if
99  * the value cannot be set. */
100 int confnode_set_user_strvalue(struct confnode *n, const char *strvalue);
101
102 /* Get user boolean value of the node. Return 0 if disabled, 1 if
103  * enabled, and -1 on error. */
104 int confnode_get_user_boolvalue(const struct confnode *n);
105
106 /* Set the default string value of the node n. Return 0 on success, or
107  * -1 if the value cannot be set. */
108 int confnode_set_default_strvalue(struct confnode *n, const char *strvalue);
109
110 /* Get the default string value for this node 'n'. This function does
111  * not check if dependancies are met. Return 0 on success, in this
112  * case the string is copied in buffer 'buf' of len 'buflen'. Return
113  * -1 if the value cannot be read. */
114 int confnode_get_default_strvalue(const struct confnode *n, char *buf, unsigned buflen);
115
116 /* Get string value of a node, including dep check. Return -1 on
117  * error. Return the boolean value of the node (0 or 1) on success: in
118  * this case, and if buf is no NULL, buf is filled with the strvalue. */
119 int confnode_get_value(const struct confnode *n, char *buf, unsigned buflen);
120
121 /* Get real boolean value of the node, checking deps first. Return 0
122  * if disabled, 1 if enabled, and -1 on error. */
123 int confnode_get_boolvalue(const struct confnode *n);
124
125 /* Return a string identifying the node type ("config", "menuconfig",
126  * "choice", ...) */
127 const char *confnode_get_type_str(const struct confnode *n);
128
129 /* Print one-line view of the node. */
130 void confnode_display_short(const struct confnode *n);
131
132 /* Print a detailed view of the node. */
133 void confnode_display_long(const struct confnode *n);
134
135 /* display the path of the current node */
136 void conf_display_path(const struct confnode *n);
137
138 /* register all nodes types */
139 void confnode_register_all(void);
140
141 /* structure describing a confnode type */
142 TAILQ_HEAD(confnode_type_list, confnode_type);
143 struct confnode_type {
144         TAILQ_ENTRY(confnode_type) next;
145         struct confnode_ops ops;
146 };
147
148 /* global list of all configuration node types */
149 extern struct confnode_type_list confnode_type_list;
150
151 #define MAX_NAME_SIZE 128
152 #define MAX_PROMPT_SIZE MAX_NAME_SIZE
153 #define MAX_VALUE_SIZE MAX_NAME_SIZE
154 #define MAX_HELP_SIZE 1024
155
156 TAILQ_HEAD(expr_node_list, expr_node);
157
158
159 TAILQ_HEAD(optname_list, optname);
160 struct optname {
161         TAILQ_ENTRY(optname) next;
162         char *name;
163 };
164
165 /*
166  * This structure defines a configuration node, for instance a "menu"
167  * entry, or a "bool" entry.
168  */
169 TAILQ_HEAD(confnode_list, confnode);
170 struct confnode {
171         TAILQ_ENTRY(confnode) next;  /* next in children list */
172         TAILQ_ENTRY(confnode) hnext; /* next in hash table */
173         TAILQ_ENTRY(confnode) user_next; /* next in user list */
174 #define CONFNODE_F_IS_DIR           0x0001 /* node can contain other nodes*/
175 #define CONFNODE_F_NO_SET_DEPS      0x0002 /* does not support the "requires" attribute */
176 #define CONFNODE_F_NO_SET_PROMPT    0x0004 /* does not support the "prompt" attribute */
177 #define CONFNODE_F_NO_SET_DEFAULT   0x0008 /* does not support the "default" attribute */
178 #define CONFNODE_F_IS_ROOT          0x0010 /* the root node */
179 #define CONFNODE_F_QUOTE_VALUE      0x0020 /* value needs quote to be displayed */
180 #define CONFNODE_F_INVISIBLE        0x0040 /* never seen from cmdline (like the IF node) */
181 #define CONFNODE_F_BOOL             0x0080 /* Stored value is a boolean */
182 #define CONFNODE_F_INT              0x0100 /* Stored value is a integer */
183 #define CONFNODE_F_STR              0x0200 /* Stored value is a string */
184 #define CONFNODE_F_CHOICE           0x0400 /* Stored value is a choice */
185 #define CONFNODE_F_FORCE_CHILD_DEPS 0x0800 /* if node is disabled, disable all children */
186 #define CONFNODE_F_NO_DEPS          0x1000 /* bypass dependency checks */
187 #define CONFNODE_F_NO_NAME          0x2000 /* node has no name (if, comments, ...) */
188         unsigned flags;
189         const struct confnode_ops *ops;
190         char name[MAX_NAME_SIZE];
191         char prompt[MAX_PROMPT_SIZE];
192         char help[MAX_HELP_SIZE];
193         char default_value[MAX_VALUE_SIZE];
194         char value[MAX_VALUE_SIZE];
195         struct confnode_list children;
196         struct confnode *parent;
197         struct expr_node_list depend;
198 };
199
200 #endif /* _CONFNODE_H_ */