Return ByteArray as "value" for FieldInfo's with type FT_NONE (which has data).
[metze/wireshark/wip.git] / epan / uat.h
1 /*
2  *  uat.h
3  *
4  *  $Id$
5  *
6  *  User Accessible Tables
7  *  Mantain an array of user accessible data strucures
8  *
9  * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
10  *
11  * Wireshark - Network traffic analyzer
12  * By Gerald Combs <gerald@wireshark.org>
13  * Copyright 2001 Gerald Combs
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28  */
29
30 #ifndef __UAT_H__
31 #define __UAT_H__
32
33 #include "ws_symbol_export.h"
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif /* __cplusplus */
38
39 /*
40  * uat mantains a dynamically allocated table accessible to the user
41  * via a file and/or gui tables.
42  *
43  * the file is located either in userdir(when first read or when writen) or
44  * in datadir for defaults (read only , it will be always written to userdir).
45  *
46  * the behaviour of the table is controlled by a series of callbacks
47  * the caller must provide.
48  *
49  * BEWARE that the user can change an uat at (almost) any time,
50  * That is pointers to records in an uat are valid only during the call
51  * to the function that obtains them (do not store them).
52  *
53  * UATs are meant for short tables of user data (passwords and such) there's
54  * no quick access, you must iterate through them each time to fetch the record
55  * you are looking for. Use uat_dup() or uat_se_dup() if necessary.
56  *
57  * Only users via gui or editing the file can add/remove records your code cannot.
58  */
59
60 /* obscure data type to handle an uat */
61 typedef struct _uat_t uat_t;
62 /********************************************
63  * Callbacks:
64  * these instruct uat on how to deal with user info and data in records
65  ********************************************/
66
67 /********
68  * Callbacks dealing with the entire table
69  ********/
70
71 /*
72  * Post-Update CB
73  *
74  * to be called after to the table has being edited
75  * Will be called once the user clicks the Apply or OK button
76  * optional
77  */
78 typedef void (*uat_post_update_cb_t)(void);
79
80
81 /********
82  * Callbacks dealing with records (these deal with entire records)
83  ********/
84
85 /*
86  * Copy CB
87  * used to copy a record
88  * optional, memcpy will be used if not given
89  * copy(dest,orig,len)
90  */
91 typedef void* (*uat_copy_cb_t)(void*, const void*, size_t);
92
93 /*
94  *
95  * Free CB
96  *
97  * destroy a record's child data
98  * (do not free the container, it will be handled by uat)
99  * it is optional, no child data will be freed if no present
100  * free(record)
101  */
102 typedef void (*uat_free_cb_t)(void*);
103
104 /*
105  * Update CB
106  *
107  * to be called after any record fields had been updated
108  * optional, record will be updated always if not given
109  * update(record,&error)
110  */
111 typedef void (*uat_update_cb_t)(void* , const char** );
112
113
114 /*******
115  * Callbacks for single fields (these deal with single values)
116  * the caller should provide one of these for every field!
117  ********/
118
119 /*
120  * given an input string (ptr, len) checks if the value is OK for a field in the record.
121  * it will return TRUE if OK or else
122  * it will return FALSE and may set *error to inform the user on what's
123  * wrong with the given input
124  * optional, if not given any input is considered OK and the set cb will be called
125  * chk(record, ptr, len, chk_data, fld_data, &error)
126  */
127 typedef gboolean (*uat_fld_chk_cb_t)(void*, const char*, unsigned, const void*, const void*, const char**);
128
129 /*
130  * Set Field CB
131  *
132  * given an input string (ptr, len) sets the value of a field in the record,
133  * it will return TRUE if OK or else
134  * it will return FALSE and may set *error to inform the user on what's
135  * wrong with the given input
136  * it is mandatory
137  * set(record, ptr, len, set_data, fld_data)
138  */
139 typedef void (*uat_fld_set_cb_t)(void*, const char*, unsigned, const void*, const void*);
140
141 /*
142  * given a record returns a string representation of the field
143  * mandatory
144  * tostr(record, &out_ptr, &out_len, tostr_data, fld_data)
145  */
146 typedef void (*uat_fld_tostr_cb_t)(void*, const char**, unsigned*, const void*, const void*);
147
148 /***********
149  * Text Mode
150  *
151  * used for file and dialog representation of fields in columns,
152  * when the file is read it modifies the way the value is passed back to the fld_set_cb
153  * (see definition bellow for description)
154  ***********/
155
156 typedef enum _uat_text_mode_t {
157         PT_TXTMOD_NONE,
158         /* not used */
159
160         PT_TXTMOD_STRING,
161         /*
162          file:
163                  reads:
164                          ,"\x20\x00\x30", as " \00",3
165                          ,"", as "",0
166                          ,, as NULL,0
167                  writes:
168                          ,"\x20\x30\x00\x20", for " 0\0 ",4
169                          ,"", for *, 0
170                          ,, for NULL, *
171          dialog:
172                  accepts \x?? and other escapes
173                  gets "",0 on empty string
174          */
175         PT_TXTMOD_HEXBYTES,
176         /*
177          file:
178                  reads:
179                          ,A1b2C3d4, as "\001\002\003\004",4
180                          ,, as NULL,0
181                  writes:
182                          ,, on NULL, *
183                          ,a1b2c3d4, on "\001\002\003\004",4
184          dialog:
185                  "a1b2c3d4" as "\001\002\003\004",4
186                  "a1 b2:c3d4" as "\001\002\003\004",4
187                  "" as NULL,0
188                  "invalid" as NULL,3
189                  "a1b" as NULL, 1
190          */
191         PT_TXTMOD_ENUM,
192
193         PT_TXTMOD_FILENAME,
194         /* processed like a PT_TXTMOD_STRING, but shows a filename dialog */
195         PT_TXTMOD_DIRECTORYNAME
196         /* processed like a PT_TXTMOD_STRING, but shows a directory dialog */
197 } uat_text_mode_t;
198
199 /*
200  * Fields
201  *
202  *
203  */
204 typedef struct _uat_field_t {
205         const char* name;
206         const char* title;
207         uat_text_mode_t mode;
208
209         struct {
210                 uat_fld_chk_cb_t chk;
211                 uat_fld_set_cb_t set;
212                 uat_fld_tostr_cb_t tostr;
213         } cb;
214
215         struct {
216                 const void* chk;
217                 const void* set;
218                 const void* tostr;
219         } cbdata;
220
221         const void* fld_data;
222
223         const char* desc;
224         struct _fld_data_t* priv;
225 } uat_field_t;
226
227 #define FLDFILL NULL
228 #define UAT_END_FIELDS {NULL,NULL,PT_TXTMOD_NONE,{0,0,0},{0,0,0},0,0,FLDFILL}
229
230 /*
231  * Flags to indicate what the settings in this UAT affect.
232  * This is used when UATs are changed interactively, to indicate what needs
233  * to be redone when the UAT is changed.
234  */
235 #define UAT_AFFECTS_DISSECTION  0x00000001      /* affects packet dissection */
236 #define UAT_AFFECTS_FIELDS      0x00000002      /* affects what named fields exist */
237
238 /** Create a new uat
239  *
240  * @param name The name of the table
241  * @param size The size of the structure
242  * @param filename The filename to be used (either in userdir or datadir)
243  * @param from_profile TRUE if profile directory to be used
244  * @param data_ptr A pointer to a null terminated array of pointers to the data
245  * @param num_items_ptr A pointer with number of items
246  * @param flags flags indicating what this UAT affects
247  * @param help A pointer to help text
248  * @param copy_cb A function that copies the data in the struct
249  * @param update_cb Will be called when a record is updated
250  * @param free_cb Will be called to destroy a struct in the dataset
251  * @param post_update_cb Will be called once the user clicks the Apply or OK button
252  * @param flds_array A pointer to an array of uat_field_t structs
253  *
254  * @return A freshly-allocated and populated uat_t struct.
255  */
256 WS_DLL_PUBLIC
257 uat_t* uat_new(const char* name,
258                            size_t size,
259                            const char* filename,
260                            gboolean from_profile,
261                            void** data_ptr,
262                            guint* num_items_ptr,
263                            guint flags,
264                            const char* help,
265                            uat_copy_cb_t copy_cb,
266                            uat_update_cb_t update_cb,
267                            uat_free_cb_t free_cb,
268                            uat_post_update_cb_t post_update_cb,
269                            uat_field_t* flds_array);
270
271 /** Populate a uat using its file.
272  *
273  * @param uat_in Pointer to a uat. Must not be NULL.
274  * @param err Upon failure, points to an error string.
275  *
276  * @return TRUE on success, FALSE on failure.
277  */
278 WS_DLL_PUBLIC
279 gboolean uat_load(uat_t* uat_in, const char** err);
280
281 /** Create or update a single uat entry using a string.
282  *
283  * @param uat_in Pointer to a uat. Must not be NULL.
284  * @param entry The string representation of the entry. Format must match
285  * what's written to the uat's output file.
286  * @param err Upon failure, points to an error string.
287  *
288  * @return TRUE on success, FALSE on failure.
289  */
290 gboolean uat_load_str(uat_t* uat_in, char* entry, char** err);
291
292 /** Given a uat name or filename, find its pointer.
293  *
294  * @param name The name or filename of the uat
295  *
296  * @return A pointer to the uat on success, NULL on failure.
297  */
298 uat_t *uat_find(gchar *name);
299
300 /*
301  * uat_dup()
302  * uat_se_dup()
303  * make a reliable copy of an uat for internal use,
304  * so that pointers to records can be kept through calls.
305  * return NULL on zero len.
306  */
307 void* uat_dup(uat_t*, guint* len_p); /* to be freed */
308 void* uat_se_dup(uat_t*, guint* len_p);
309 WS_DLL_PUBLIC
310 uat_t* uat_get_table_by_name(const char* name);
311
312 /*
313  * Some common uat_fld_chk_cbs
314  */
315 WS_DLL_PUBLIC
316 gboolean uat_fld_chk_str(void*, const char*, unsigned, const void*, const void*, const char** err);
317 gboolean uat_fld_chk_oid(void*, const char*, unsigned, const void*, const void*, const char** err);
318 WS_DLL_PUBLIC
319 gboolean uat_fld_chk_proto(void*, const char*, unsigned, const void*, const void*, const char** err);
320 WS_DLL_PUBLIC
321 gboolean uat_fld_chk_num_dec(void*, const char*, unsigned, const void*, const void*, const char** err);
322 WS_DLL_PUBLIC
323 gboolean uat_fld_chk_num_hex(void*, const char*, unsigned, const void*, const void*, const char** err);
324 WS_DLL_PUBLIC
325 gboolean uat_fld_chk_enum(void*, const char*, unsigned, const void*, const void*, const char**);
326 WS_DLL_PUBLIC
327 gboolean uat_fld_chk_range(void*, const char*, unsigned, const void*, const void*, const char**);
328
329 #define CHK_STR_IS_DECL(what) \
330 gboolean uat_fld_chk_str_ ## what (void*, const char*, unsigned, const void*, const void*, const char**)
331
332 typedef void (*uat_cb_t)(void* uat,void* user_data);
333 WS_DLL_PUBLIC
334 void uat_foreach_table(uat_cb_t cb,void* user_data);
335 void uat_unload_all(void);
336
337 char* uat_undquote(const char* si, guint in_len, guint* len_p);
338 char* uat_unbinstring(const char* si, guint in_len, guint* len_p);
339 char* uat_unesc(const char* si, guint in_len, guint* len_p);
340 char* uat_esc(const char* buf, guint len);
341
342 /* Some strings entirely made of ... already declared */
343 WS_DLL_PUBLIC
344 CHK_STR_IS_DECL(isprint);
345 WS_DLL_PUBLIC
346 CHK_STR_IS_DECL(isalpha);
347 WS_DLL_PUBLIC
348 CHK_STR_IS_DECL(isalnum);
349 WS_DLL_PUBLIC
350 CHK_STR_IS_DECL(isdigit);
351 WS_DLL_PUBLIC
352 CHK_STR_IS_DECL(isxdigit);
353
354 #define CHK_STR_IS_DEF(what) \
355 gboolean uat_fld_chk_str_ ## what (void* u1 _U_, const char* strptr, guint len, const void* u2 _U_, const void* u3 _U_, const char** err) { \
356         guint i; for (i=0;i<len;i++) { \
357                 char c = strptr[i]; \
358                         if (! what((int)c)) { \
359                                 *err = ep_strdup_printf("invalid char pos=%d value=%.2x",i,c); return FALSE;  } } \
360                 *err = NULL; return TRUE; }
361
362
363 /*
364  * Macros
365  *   to define basic uat_fld_set_cbs, uat_fld_tostr_cbs
366  *   for those elements in uat_field_t array
367  */
368
369 /*
370  * CSTRING macros,
371  *    a simple c-string contained in (((rec_t*)rec)->(field_name))
372  */
373 #define UAT_CSTRING_CB_DEF(basename,field_name,rec_t) \
374 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* u1 _U_, const void* u2 _U_) {\
375     char* new_buf = g_strndup(buf,len); \
376         g_free((((rec_t*)rec)->field_name)); \
377         (((rec_t*)rec)->field_name) = new_buf; } \
378 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* u1 _U_, const void* u2 _U_) {\
379                 if (((rec_t*)rec)->field_name ) { \
380                         *out_ptr = (((rec_t*)rec)->field_name); \
381                         *out_len = (unsigned)strlen((((rec_t*)rec)->field_name)); \
382                 } else { \
383                         *out_ptr = ""; *out_len = 0; } }
384
385 #define UAT_FLD_CSTRING(basename,field_name,title,desc) \
386         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_str,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
387
388 #define UAT_FLD_CSTRING_ISPRINT(basename,field_name,title,desc) \
389         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_str_isprint,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
390
391 #define UAT_FLD_CSTRING_OTHER(basename,field_name,title,chk,desc) \
392         {#field_name, title, PT_TXTMOD_STRING,{ chk ,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
393
394 /*
395  * FILENAME and DIRECTORYNAME,
396  *    a simple c-string contained in (((rec_t*)rec)->(field_name))
397  */
398 #define UAT_FILENAME_CB_DEF(basename,field_name,rec_t) UAT_CSTRING_CB_DEF(basename,field_name,rec_t)
399
400 #define UAT_FLD_FILENAME(basename,field_name,title,desc) \
401         {#field_name, title, PT_TXTMOD_FILENAME,{uat_fld_chk_str,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
402
403 #define UAT_FLD_FILENAME_OTHER(basename,field_name,title,chk,desc) \
404         {#field_name, title, PT_TXTMOD_FILENAME,{chk,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
405
406 #define UAT_DIRECTORYNAME_CB_DEF(basename,field_name,rec_t) UAT_CSTRING_CB_DEF(basename,field_name,rec_t)
407
408 #define UAT_FLD_DIRECTORYNAME(basename,field_name,title,desc) \
409         {#field_name, title, PT_TXTMOD_DIRECTORYNAME,{uat_fld_chk_str,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
410
411 /*
412  * OID - just a CSTRING with a specific check routine
413  */
414 #define UAT_FLD_OID(basename,field_name,title,desc) \
415         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_oid,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
416
417
418 /*
419  * LSTRING MACROS
420  */
421 #define UAT_LSTRING_CB_DEF(basename,field_name,rec_t,ptr_element,len_element) \
422 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* u1 _U_, const void* u2 _U_) {\
423         char* new_val = uat_unesc(buf,len,&(((rec_t*)rec)->len_element)); \
424         g_free((((rec_t*)rec)->ptr_element)); \
425         (((rec_t*)rec)->ptr_element) = new_val; }\
426 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* u1 _U_, const void* u2 _U_) {\
427         if (((rec_t*)rec)->ptr_element ) { \
428                 *out_ptr = uat_esc(((rec_t*)rec)->ptr_element, (((rec_t*)rec)->len_element)); \
429                 *out_len = (unsigned)strlen(*out_ptr); \
430         } else { \
431                 *out_ptr = ""; *out_len = 0; } }
432
433 #define UAT_FLD_LSTRING(basename,field_name,title, desc) \
434 {#field_name, title, PT_TXTMOD_STRING,{0,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
435
436
437 /*
438  * BUFFER macros,
439  *    a buffer_ptr contained in (((rec_t*)rec)->(field_name))
440  *    and its len in (((rec_t*)rec)->(len_name))
441  *  XXX: UNTESTED and probably BROKEN
442  */
443 #define UAT_BUFFER_CB_DEF(basename,field_name,rec_t,ptr_element,len_element) \
444 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* u1 _U_, const void* u2 _U_) {\
445         char* new_buf = len ? (char *)g_memdup(buf,len) : NULL; \
446         g_free((((rec_t*)rec)->ptr_element)); \
447         (((rec_t*)rec)->ptr_element) = new_buf; \
448         (((rec_t*)rec)->len_element) = len; } \
449 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* u1 _U_, const void* u2 _U_) {\
450         *out_ptr = ((rec_t*)rec)->ptr_element ? (const char*)ep_memdup(((rec_t*)rec)->ptr_element,((rec_t*)rec)->len_element) : ""; \
451         *out_len = ((rec_t*)rec)->len_element; }
452
453 #define UAT_FLD_BUFFER(basename,field_name,title,desc) \
454         {#field_name, title, PT_TXTMOD_HEXBYTES,{0,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
455
456
457 /*
458  * DEC Macros,
459  *   a decimal number contained in
460  */
461 #define UAT_DEC_CB_DEF(basename,field_name,rec_t) \
462 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* u1 _U_, const void* u2 _U_) {\
463         ((rec_t*)rec)->field_name = (guint)strtol(ep_strndup(buf,len),NULL,10); } \
464 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* u1 _U_, const void* u2 _U_) {\
465         *out_ptr = ep_strdup_printf("%d",((rec_t*)rec)->field_name); \
466         *out_len = (unsigned)strlen(*out_ptr); }
467
468 #define UAT_FLD_DEC(basename,field_name,title,desc) \
469         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_num_dec,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
470
471
472 /*
473  * HEX Macros,
474  *   an hexadecimal number contained in
475  */
476 #define UAT_HEX_CB_DEF(basename,field_name,rec_t) \
477 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* u1 _U_, const void* u2 _U_) {\
478         ((rec_t*)rec)->field_name = (guint)strtol(ep_strndup(buf,len),NULL,16); } \
479 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* u1 _U_, const void* u2 _U_) {\
480         *out_ptr = ep_strdup_printf("%x",((rec_t*)rec)->field_name); \
481         *out_len = (unsigned)strlen(*out_ptr); }
482
483 #define UAT_FLD_HEX(basename,field_name,title,desc) \
484 {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_num_hex,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
485
486
487 /*
488  * ENUM macros
489  *  enum_t: name = ((enum_t*)ptr)->strptr
490  *          value = ((enum_t*)ptr)->value
491  *  rec_t:
492  *        value
493  */
494 #define UAT_VS_DEF(basename,field_name,rec_t,default_t,default_val,default_str) \
495 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* vs, const void* u2 _U_) {\
496         guint i; \
497         char* str = ep_strndup(buf,len); \
498         const char* cstr; ((rec_t*)rec)->field_name = default_val; \
499         for(i=0; ( cstr = ((value_string*)vs)[i].strptr ) ;i++) { \
500                 if (g_str_equal(cstr,str)) { \
501                         ((rec_t*)rec)->field_name = (default_t)((value_string*)vs)[i].value; return; } } } \
502 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* vs, const void* u2 _U_) {\
503         guint i; \
504         *out_ptr = ep_strdup(default_str); \
505         *out_len = (unsigned)strlen(default_str);\
506         for(i=0;((value_string*)vs)[i].strptr;i++) { \
507                 if ( ((value_string*)vs)[i].value == ((rec_t*)rec)->field_name ) { \
508                         *out_ptr = ep_strdup(((value_string*)vs)[i].strptr); \
509                         *out_len = (unsigned)strlen(*out_ptr); return; } } }
510
511 #define UAT_VS_CSTRING_DEF(basename,field_name,rec_t,default_val,default_str) \
512 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* vs, const void* u2 _U_) {\
513         guint i; \
514         char* str = ep_strndup(buf,len); \
515         const char* cstr; ((rec_t*)rec)->field_name = default_val; \
516         for(i=0; ( cstr = ((value_string*)vs)[i].strptr ) ;i++) { \
517                 if (g_str_equal(cstr,str)) { \
518                   ((rec_t*)rec)->field_name = g_strdup(((value_string*)vs)[i].strptr); return; } } } \
519 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* vs _U_, const void* u2 _U_) {\
520                 if (((rec_t*)rec)->field_name ) { \
521                         *out_ptr = (((rec_t*)rec)->field_name); \
522                         *out_len = (unsigned)strlen((((rec_t*)rec)->field_name)); \
523                 } else { \
524                         *out_ptr = ""; *out_len = 0; } }
525
526 #define UAT_FLD_VS(basename,field_name,title,enum,desc) \
527         {#field_name, title, PT_TXTMOD_ENUM,{uat_fld_chk_enum,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{&(enum),&(enum),&(enum)},&(enum),desc,FLDFILL}
528
529
530 /*
531  * PROTO macros
532  */
533
534 #define UAT_PROTO_DEF(basename, field_name, dissector_field, name_field, rec_t) \
535 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* u1 _U_, const void* u2 _U_) {\
536         if (len) { \
537                 gchar *tmp = g_strndup(buf,len); \
538                 ((rec_t*)rec)->name_field = g_ascii_strdown(tmp, -1); \
539                 g_free(tmp); \
540                 g_strchug(((rec_t*)rec)->name_field); \
541                 ((rec_t*)rec)->dissector_field = find_dissector(((rec_t*)rec)->name_field); \
542         } else { \
543                 ((rec_t*)rec)->dissector_field = find_dissector("data"); \
544                 ((rec_t*)rec)->name_field = NULL; \
545                 } } \
546 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* u1 _U_, const void* u2 _U_) {\
547         if ( ((rec_t*)rec)->name_field ) { \
548                 *out_ptr = (((rec_t*)rec)->name_field); \
549                 *out_len = (unsigned)strlen(*out_ptr); \
550         } else { \
551                 *out_ptr = ""; *out_len = 0; } }
552
553
554 #define UAT_FLD_PROTO(basename,field_name,title,desc) \
555         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_proto,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
556
557 /*
558  * RANGE macros
559  */
560
561 #define UAT_RANGE_CB_DEF(basename,field_name,rec_t) \
562 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* u1 _U_, const void* u2) {\
563         char* rng = ep_strndup(buf,len);\
564                 range_convert_str(&(((rec_t*)rec)->field_name), rng,GPOINTER_TO_UINT(u2)); \
565         } \
566 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* u1 _U_, const void* u2 _U_) {\
567         if ( ((rec_t*)rec)->field_name ) { \
568                 *out_ptr = range_convert_range(((rec_t*)rec)->field_name); \
569                 *out_len = (unsigned)strlen(*out_ptr); \
570         } else { \
571                 *out_ptr = ""; *out_len = 0; } }
572
573
574 #define UAT_FLD_RANGE(basename,field_name,title,max,desc) \
575         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_range,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},\
576           {0,0,0},GUINT_TO_POINTER(max),desc,FLDFILL}
577
578 #ifdef __cplusplus
579 }
580 #endif /* __cplusplus */
581
582 #endif /* __UAT_H__ */