PageRenderTime 64ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/epan/prefs.c

https://github.com/labx-technologies-llc/wireshark
C | 4657 lines | 3812 code | 369 blank | 476 comment | 223 complexity | 9b7d8c0299a094ae5c3b50a131d948b7 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /* prefs.c
  2. * Routines for handling preferences
  3. *
  4. * $Id$
  5. *
  6. * Wireshark - Network traffic analyzer
  7. * By Gerald Combs <gerald@wireshark.org>
  8. * Copyright 1998 Gerald Combs
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. #include "config.h"
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #include <glib.h>
  33. #include <stdio.h>
  34. #include <epan/filesystem.h>
  35. #include <epan/address.h>
  36. #include <epan/addr_resolv.h>
  37. #include <epan/oids.h>
  38. #ifdef HAVE_GEOIP
  39. #include <epan/geoip_db.h>
  40. #endif
  41. #include <epan/packet.h>
  42. #include <epan/prefs.h>
  43. #include <epan/proto.h>
  44. #include <epan/strutil.h>
  45. #include <epan/column.h>
  46. #include "print.h"
  47. #include <wsutil/file_util.h>
  48. #include <epan/prefs-int.h>
  49. #include <epan/uat-int.h>
  50. #include "epan/filter_expressions.h"
  51. #include "epan/wmem/wmem.h"
  52. /* Internal functions */
  53. static module_t *find_subtree(module_t *parent, const char *tilte);
  54. static module_t *prefs_register_module_or_subtree(module_t *parent,
  55. const char *name, const char *title, const char *description, gboolean is_subtree,
  56. void (*apply_cb)(void), gboolean use_gui);
  57. static prefs_set_pref_e set_pref(gchar*, const gchar*, void *, gboolean);
  58. static char * join_string_list(GList *);
  59. static void free_col_info(GList *);
  60. static void pre_init_prefs(void);
  61. static gboolean prefs_is_column_visible(const gchar *cols_hidden, fmt_data *cfmt);
  62. static gboolean parse_column_format(fmt_data *cfmt, const char *fmt);
  63. static void try_convert_to_custom_column(gpointer *el_data);
  64. #define PF_NAME "preferences"
  65. #define OLD_GPF_NAME "wireshark.conf" /* old name for global preferences file */
  66. static gboolean prefs_initialized = FALSE;
  67. static gboolean prefs_pre_initialized = FALSE;
  68. static gchar *gpf_path = NULL;
  69. static gchar *cols_hidden_list = NULL;
  70. /*
  71. * XXX - variables to allow us to attempt to interpret the first
  72. * "mgcp.{tcp,udp}.port" in a preferences file as
  73. * "mgcp.{tcp,udp}.gateway_port" and the second as
  74. * "mgcp.{tcp,udp}.callagent_port".
  75. */
  76. static int mgcp_tcp_port_count;
  77. static int mgcp_udp_port_count;
  78. e_prefs prefs;
  79. static const enum_val_t gui_ptree_line_style[] = {
  80. {"NONE", "NONE", 0},
  81. {"SOLID", "SOLID", 1},
  82. {"DOTTED", "DOTTED", 2},
  83. {"TABBED", "TABBED", 3},
  84. {NULL, NULL, -1}
  85. };
  86. static const enum_val_t gui_ptree_expander_style[] = {
  87. {"NONE", "NONE", 0},
  88. {"SQUARE", "SQUARE", 1},
  89. {"TRIANGLE", "TRIANGLE", 2},
  90. {"CIRCULAR", "CIRCULAR", 3},
  91. {NULL, NULL, -1}
  92. };
  93. static const enum_val_t gui_hex_dump_highlight_style[] = {
  94. {"BOLD", "BOLD", 0},
  95. {"INVERSE", "INVERSE", 1},
  96. {NULL, NULL, -1}
  97. };
  98. static const enum_val_t gui_console_open_type[] = {
  99. {"NEVER", "NEVER", console_open_never},
  100. {"AUTOMATIC", "AUTOMATIC", console_open_auto},
  101. {"ALWAYS", "ALWAYS", console_open_always},
  102. {NULL, NULL, -1}
  103. };
  104. static const enum_val_t gui_version_placement_type[] = {
  105. {"WELCOME", "WELCOME", version_welcome_only},
  106. {"TITLE", "TITLE", version_title_only},
  107. {"BOTH", "BOTH", version_both},
  108. {"NEITHER", "NEITHER", version_neither},
  109. {NULL, NULL, -1}
  110. };
  111. static const enum_val_t gui_fileopen_style[] = {
  112. {"LAST_OPENED", "LAST_OPENED", 0},
  113. {"SPECIFIED", "SPECIFIED", 1},
  114. {NULL, NULL, -1}
  115. };
  116. /* GTK knows of two ways representing "both", vertical and horizontal aligned.
  117. * as this may not work on other guis, we use only "both" in general here */
  118. static const enum_val_t gui_toolbar_style[] = {
  119. {"ICONS", "ICONS", 0},
  120. {"TEXT", "TEXT", 1},
  121. {"BOTH", "BOTH", 2},
  122. {NULL, NULL, -1}
  123. };
  124. static const enum_val_t gui_layout_content[] = {
  125. {"NONE", "NONE", 0},
  126. {"PLIST", "PLIST", 1},
  127. {"PDETAILS", "PDETAILS", 2},
  128. {"PBYTES", "PBYTES", 3},
  129. {NULL, NULL, -1}
  130. };
  131. static const enum_val_t gui_update_channel[] = {
  132. {"DEVELOPMENT", "DEVELOPMENT", UPDATE_CHANNEL_DEVELOPMENT},
  133. {"STABLE", "STABLE", UPDATE_CHANNEL_STABLE},
  134. {NULL, NULL, -1}
  135. };
  136. #if defined(HAVE_PCAP_CREATE)
  137. /* Can set monitor mode and buffer size. */
  138. static gint num_capture_cols = 7;
  139. static const gchar *capture_cols[7] = {
  140. "INTERFACE",
  141. "LINK",
  142. "PMODE",
  143. "SNAPLEN",
  144. "MONITOR",
  145. "BUFFER",
  146. "FILTER"
  147. };
  148. #define CAPTURE_COL_TYPE_DESCRIPTION \
  149. "Possible values: INTERFACE, LINK, PMODE, SNAPLEN, MONITOR, BUFFER, FILTER\n"
  150. #elif defined(_WIN32) && !defined (HAVE_PCAP_CREATE)
  151. /* Can set buffer size but not monitor mode. */
  152. static gint num_capture_cols = 6;
  153. static const gchar *capture_cols[6] = {
  154. "INTERFACE",
  155. "LINK",
  156. "PMODE",
  157. "SNAPLEN",
  158. "BUFFER",
  159. "FILTER"
  160. };
  161. #define CAPTURE_COL_TYPE_DESCRIPTION \
  162. "Possible values: INTERFACE, LINK, PMODE, SNAPLEN, BUFFER, FILTER\n"
  163. #else
  164. /* Can neither set buffer size nor monitor mode. */
  165. static gint num_capture_cols = 5;
  166. static const gchar *capture_cols[5] = {
  167. "INTERFACE",
  168. "LINK",
  169. "PMODE",
  170. "SNAPLEN",
  171. "FILTER"
  172. };
  173. #define CAPTURE_COL_TYPE_DESCRIPTION \
  174. "Possible values: INTERFACE, LINK, PMODE, SNAPLEN, FILTER\n"
  175. #endif
  176. /*
  177. * List of all modules with preference settings.
  178. */
  179. static wmem_tree_t *prefs_modules = NULL;
  180. /*
  181. * List of all modules that should show up at the top level of the
  182. * tree in the preference dialog box.
  183. */
  184. static wmem_tree_t *prefs_top_level_modules = NULL;
  185. /** Sets up memory used by proto routines. Called at program startup */
  186. void
  187. prefs_init(void)
  188. {
  189. prefs_modules = wmem_tree_new(wmem_epan_scope());
  190. prefs_top_level_modules = wmem_tree_new(wmem_epan_scope());
  191. }
  192. static void
  193. free_pref(gpointer data, gpointer user_data _U_)
  194. {
  195. pref_t *pref = (pref_t *)data;
  196. switch (pref->type) {
  197. case PREF_OBSOLETE:
  198. case PREF_BOOL:
  199. case PREF_ENUM:
  200. case PREF_UINT:
  201. case PREF_STATIC_TEXT:
  202. case PREF_UAT:
  203. case PREF_COLOR:
  204. break;
  205. case PREF_STRING:
  206. case PREF_FILENAME:
  207. case PREF_DIRNAME:
  208. g_free((char *)*pref->varp.string);
  209. *pref->varp.string = NULL;
  210. g_free(pref->default_val.string);
  211. pref->default_val.string = NULL;
  212. break;
  213. case PREF_RANGE:
  214. g_free(*pref->varp.range);
  215. *pref->varp.range = NULL;
  216. g_free(pref->default_val.range);
  217. pref->default_val.range = NULL;
  218. break;
  219. case PREF_CUSTOM:
  220. if (strcmp(pref->name, "columns") == 0)
  221. pref->stashed_val.boolval = TRUE;
  222. pref->custom_cbs.free_cb(pref);
  223. break;
  224. }
  225. g_free(pref);
  226. }
  227. static guint
  228. free_module_prefs(module_t *module, gpointer data _U_)
  229. {
  230. if (module->prefs) {
  231. g_list_foreach(module->prefs, free_pref, NULL);
  232. g_list_free(module->prefs);
  233. }
  234. module->prefs = NULL;
  235. module->numprefs = 0;
  236. if (module->submodules) {
  237. prefs_modules_foreach_submodules(module, free_module_prefs, NULL);
  238. }
  239. /* We don't free the actual module: its submodules pointer points to
  240. a wmem_tree and the module itself is stored in a wmem_tree
  241. */
  242. return 0;
  243. }
  244. /** Frees memory used by proto routines. Called at program shutdown */
  245. void
  246. prefs_cleanup(void)
  247. {
  248. /* This isn't strictly necessary since we're exiting anyway, but let's
  249. * do what clean up we can.
  250. */
  251. prefs_modules_foreach(free_module_prefs, NULL);
  252. }
  253. /*
  254. * Register a module that will have preferences.
  255. * Specify the module under which to register it or NULL to register it
  256. * at the top level, the name used for the module in the preferences file,
  257. * the title used in the tab for it in a preferences dialog box, and a
  258. * routine to call back when we apply the preferences.
  259. */
  260. module_t *
  261. prefs_register_module(module_t *parent, const char *name, const char *title,
  262. const char *description, void (*apply_cb)(void),
  263. const gboolean use_gui)
  264. {
  265. return prefs_register_module_or_subtree(parent, name, title, description,
  266. FALSE, apply_cb, use_gui);
  267. }
  268. /*
  269. * Register a subtree that will have modules under it.
  270. * Specify the module under which to register it or NULL to register it
  271. * at the top level and the title used in the tab for it in a preferences
  272. * dialog box.
  273. */
  274. module_t *
  275. prefs_register_subtree(module_t *parent, const char *title, const char *description,
  276. void (*apply_cb)(void))
  277. {
  278. return prefs_register_module_or_subtree(parent, NULL, title, description,
  279. TRUE, apply_cb,
  280. parent ? parent->use_gui : FALSE);
  281. }
  282. static module_t *
  283. prefs_register_module_or_subtree(module_t *parent, const char *name,
  284. const char *title, const char *description,
  285. gboolean is_subtree, void (*apply_cb)(void),
  286. gboolean use_gui)
  287. {
  288. module_t *module;
  289. const char *p;
  290. guchar c;
  291. /* this module may have been created as a subtree item previously */
  292. if ((module = find_subtree(parent, title))) {
  293. /* the module is currently a subtree */
  294. module->name = name;
  295. module->apply_cb = apply_cb;
  296. module->description = description;
  297. if (prefs_find_module(name) == NULL) {
  298. wmem_tree_insert_string(prefs_modules, name, module,
  299. WMEM_TREE_STRING_NOCASE);
  300. }
  301. return module;
  302. }
  303. module = wmem_new(wmem_epan_scope(), module_t);
  304. module->name = name;
  305. module->title = title;
  306. module->description = description;
  307. module->apply_cb = apply_cb;
  308. module->prefs = NULL; /* no preferences, to start */
  309. module->parent = parent;
  310. module->submodules = NULL; /* no submodules, to start */
  311. module->numprefs = 0;
  312. module->prefs_changed = FALSE;
  313. module->obsolete = FALSE;
  314. module->use_gui = use_gui;
  315. /*
  316. * Do we have a module name?
  317. */
  318. if (name != NULL) {
  319. /*
  320. * Yes.
  321. * Make sure that only lower-case ASCII letters, numbers,
  322. * underscores, hyphens, and dots appear in the name.
  323. *
  324. * Crash if there is, as that's an error in the code;
  325. * you can make the title a nice string with capitalization,
  326. * white space, punctuation, etc., but the name can be used
  327. * on the command line, and shouldn't require quoting,
  328. * shifting, etc.
  329. */
  330. for (p = name; (c = *p) != '\0'; p++)
  331. g_assert(isascii(c) &&
  332. (islower(c) || isdigit(c) || c == '_' ||
  333. c == '-' || c == '.'));
  334. /*
  335. * Make sure there's not already a module with that
  336. * name. Crash if there is, as that's an error in the
  337. * code, and the code has to be fixed not to register
  338. * more than one module with the same name.
  339. *
  340. * We search the list of all modules; the subtree stuff
  341. * doesn't require preferences in subtrees to have names
  342. * that reflect the subtree they're in (that would require
  343. * protocol preferences to have a bogus "protocol.", or
  344. * something such as that, to be added to all their names).
  345. */
  346. g_assert(prefs_find_module(name) == NULL);
  347. /*
  348. * Insert this module in the list of all modules.
  349. */
  350. wmem_tree_insert_string(prefs_modules, name, module, WMEM_TREE_STRING_NOCASE);
  351. } else {
  352. /*
  353. * This has no name, just a title; check to make sure it's a
  354. * subtree, and crash if it's not.
  355. */
  356. g_assert(is_subtree);
  357. }
  358. /*
  359. * Insert this module into the appropriate place in the display
  360. * tree.
  361. */
  362. if (parent == NULL) {
  363. /*
  364. * It goes at the top.
  365. */
  366. wmem_tree_insert_string(prefs_top_level_modules, title, module, WMEM_TREE_STRING_NOCASE);
  367. } else {
  368. /*
  369. * It goes into the list for this module.
  370. */
  371. if (parent->submodules == NULL)
  372. parent->submodules = wmem_tree_new(wmem_epan_scope());
  373. wmem_tree_insert_string(parent->submodules, title, module, WMEM_TREE_STRING_NOCASE);
  374. }
  375. return module;
  376. }
  377. /*
  378. * Register that a protocol has preferences.
  379. */
  380. module_t *protocols_module = NULL;
  381. module_t *
  382. prefs_register_protocol(int id, void (*apply_cb)(void))
  383. {
  384. protocol_t *protocol;
  385. /*
  386. * Have we yet created the "Protocols" subtree?
  387. */
  388. if (protocols_module == NULL) {
  389. /*
  390. * No. Register Protocols subtree as well as any preferences
  391. * for non-dissector modules.
  392. */
  393. prefs_register_modules();
  394. }
  395. protocol = find_protocol_by_id(id);
  396. return prefs_register_module(protocols_module,
  397. proto_get_protocol_filter_name(id),
  398. proto_get_protocol_short_name(protocol),
  399. proto_get_protocol_name(id), apply_cb, TRUE);
  400. }
  401. module_t *
  402. prefs_register_protocol_subtree(const char *subtree, int id, void (*apply_cb)(void))
  403. {
  404. protocol_t *protocol;
  405. module_t *subtree_module;
  406. module_t *new_module;
  407. char *sep = NULL, *ptr = NULL, *orig = NULL;
  408. /*
  409. * Have we yet created the "Protocols" subtree?
  410. * XXX - can we just do this by registering Protocols/{subtree}?
  411. * If not, why not?
  412. */
  413. if (protocols_module == NULL) {
  414. /*
  415. * No. Register Protocols subtree as well as any preferences
  416. * for non-dissector modules.
  417. */
  418. prefs_register_modules();
  419. }
  420. subtree_module = protocols_module;
  421. if (subtree) {
  422. /* take a copy of the buffer, orig keeps a base pointer while ptr
  423. * walks through the string */
  424. orig = ptr = g_strdup(subtree);
  425. while (ptr && *ptr) {
  426. if ((sep = strchr(ptr, '/')))
  427. *sep++ = '\0';
  428. if (!(new_module = find_subtree(subtree_module, ptr))) {
  429. /*
  430. * There's no such module; create it, with the description
  431. * being the name (if it's later registered explicitly
  432. * with a description, that will override it).
  433. */
  434. ptr = wmem_strdup(wmem_epan_scope(), ptr),
  435. new_module = prefs_register_subtree(subtree_module, ptr, ptr, NULL);
  436. }
  437. subtree_module = new_module;
  438. ptr = sep;
  439. }
  440. g_free(orig);
  441. }
  442. protocol = find_protocol_by_id(id);
  443. return prefs_register_module(subtree_module,
  444. proto_get_protocol_filter_name(id),
  445. proto_get_protocol_short_name(protocol),
  446. proto_get_protocol_name(id), apply_cb, TRUE);
  447. }
  448. /*
  449. * Register that a protocol used to have preferences but no longer does,
  450. * by creating an "obsolete" module for it.
  451. */
  452. module_t *
  453. prefs_register_protocol_obsolete(int id)
  454. {
  455. module_t *module;
  456. protocol_t *protocol;
  457. /*
  458. * Have we yet created the "Protocols" subtree?
  459. */
  460. if (protocols_module == NULL) {
  461. /*
  462. * No. Register Protocols subtree as well as any preferences
  463. * for non-dissector modules.
  464. */
  465. prefs_register_modules();
  466. }
  467. protocol = find_protocol_by_id(id);
  468. module = prefs_register_module(protocols_module,
  469. proto_get_protocol_filter_name(id),
  470. proto_get_protocol_short_name(protocol),
  471. proto_get_protocol_name(id), NULL, TRUE);
  472. module->obsolete = TRUE;
  473. return module;
  474. }
  475. /*
  476. * Register that a statistical tap has preferences.
  477. *
  478. * "name" is a name for the tap to use on the command line with "-o"
  479. * and in preference files.
  480. *
  481. * "title" is a short human-readable name for the tap.
  482. *
  483. * "description" is a longer human-readable description of the tap.
  484. */
  485. module_t *stats_module = NULL;
  486. module_t *
  487. prefs_register_stat(const char *name, const char *title,
  488. const char *description, void (*apply_cb)(void))
  489. {
  490. /*
  491. * Have we yet created the "Statistics" subtree?
  492. */
  493. if (stats_module == NULL) {
  494. /*
  495. * No. Register Statistics subtree as well as any preferences
  496. * for non-dissector modules.
  497. */
  498. prefs_register_modules();
  499. }
  500. return prefs_register_module(stats_module, name, title, description,
  501. apply_cb, TRUE);
  502. }
  503. module_t *
  504. prefs_find_module(const char *name)
  505. {
  506. return (module_t *)wmem_tree_lookup_string(prefs_modules, name, WMEM_TREE_STRING_NOCASE);
  507. }
  508. static module_t *
  509. find_subtree(module_t *parent, const char *name)
  510. {
  511. return (module_t *)wmem_tree_lookup_string(parent ? parent->submodules : prefs_top_level_modules, name, WMEM_TREE_STRING_NOCASE);
  512. }
  513. /*
  514. * Call a callback function, with a specified argument, for each module
  515. * in a list of modules. If the list is NULL, searches the top-level
  516. * list in the display tree of modules. If any callback returns a
  517. * non-zero value, we stop and return that value, otherwise we
  518. * return 0.
  519. *
  520. * Ignores "obsolete" modules; their sole purpose is to allow old
  521. * preferences for dissectors that no longer have preferences to be
  522. * silently ignored in preference files. Does not ignore subtrees,
  523. * as this can be used when walking the display tree of modules.
  524. */
  525. typedef struct {
  526. module_cb callback;
  527. gpointer user_data;
  528. guint ret;
  529. } call_foreach_t;
  530. static gboolean
  531. call_foreach_cb(void *value, void *data)
  532. {
  533. module_t *module = (module_t*)value;
  534. call_foreach_t *call_data = (call_foreach_t*)data;
  535. if (!module->obsolete)
  536. call_data->ret = (*call_data->callback)(module, call_data->user_data);
  537. return (call_data->ret != 0);
  538. }
  539. static guint
  540. prefs_module_list_foreach(wmem_tree_t *module_list, module_cb callback,
  541. gpointer user_data)
  542. {
  543. call_foreach_t call_data;
  544. if (module_list == NULL)
  545. module_list = prefs_top_level_modules;
  546. call_data.callback = callback;
  547. call_data.user_data = user_data;
  548. call_data.ret = 0;
  549. wmem_tree_foreach(module_list, call_foreach_cb, &call_data);
  550. return call_data.ret;
  551. }
  552. /*
  553. * Returns TRUE if module has any submodules
  554. */
  555. gboolean
  556. prefs_module_has_submodules(module_t *module)
  557. {
  558. if (module->submodules == NULL) {
  559. return FALSE;
  560. }
  561. if (wmem_tree_is_empty(module->submodules)) {
  562. return FALSE;
  563. }
  564. return TRUE;
  565. }
  566. /*
  567. * Call a callback function, with a specified argument, for each module
  568. * in the list of all modules. (This list does not include subtrees.)
  569. *
  570. * Ignores "obsolete" modules; their sole purpose is to allow old
  571. * preferences for dissectors that no longer have preferences to be
  572. * silently ignored in preference files.
  573. */
  574. guint
  575. prefs_modules_foreach(module_cb callback, gpointer user_data)
  576. {
  577. return prefs_module_list_foreach(prefs_modules, callback, user_data);
  578. }
  579. /*
  580. * Call a callback function, with a specified argument, for each submodule
  581. * of specified modules. If the module is NULL, goes through the top-level
  582. * list in the display tree of modules.
  583. *
  584. * Ignores "obsolete" modules; their sole purpose is to allow old
  585. * preferences for dissectors that no longer have preferences to be
  586. * silently ignored in preference files. Does not ignore subtrees,
  587. * as this can be used when walking the display tree of modules.
  588. */
  589. guint
  590. prefs_modules_foreach_submodules(module_t *module, module_cb callback,
  591. gpointer user_data)
  592. {
  593. return prefs_module_list_foreach((module)?module->submodules:prefs_top_level_modules, callback, user_data);
  594. }
  595. static gboolean
  596. call_apply_cb(void *value, void *data _U_)
  597. {
  598. module_t *module = (module_t *)value;
  599. if (module->obsolete)
  600. return FALSE;
  601. if (module->prefs_changed) {
  602. if (module->apply_cb != NULL)
  603. (*module->apply_cb)();
  604. module->prefs_changed = FALSE;
  605. }
  606. return FALSE;
  607. }
  608. /*
  609. * Call the "apply" callback function for each module if any of its
  610. * preferences have changed, and then clear the flag saying its
  611. * preferences have changed, as the module has been notified of that
  612. * fact.
  613. */
  614. void
  615. prefs_apply_all(void)
  616. {
  617. wmem_tree_foreach(prefs_modules, call_apply_cb, NULL);
  618. }
  619. /*
  620. * Call the "apply" callback function for a specific module if any of
  621. * its preferences have changed, and then clear the flag saying its
  622. * preferences have changed, as the module has been notified of that
  623. * fact.
  624. */
  625. void
  626. prefs_apply(module_t *module)
  627. {
  628. if (module && module->prefs_changed)
  629. call_apply_cb(module, NULL);
  630. }
  631. /*
  632. * Register a preference in a module's list of preferences.
  633. * If it has a title, give it an ordinal number; otherwise, it's a
  634. * preference that won't show up in the UI, so it shouldn't get an
  635. * ordinal number (the ordinal should be the ordinal in the set of
  636. * *visible* preferences).
  637. */
  638. static pref_t *
  639. register_preference(module_t *module, const char *name, const char *title,
  640. const char *description, pref_type_t type)
  641. {
  642. pref_t *preference;
  643. const gchar *p;
  644. preference = g_new(pref_t,1);
  645. preference->name = name;
  646. preference->title = title;
  647. preference->description = description;
  648. preference->type = type;
  649. if (title != NULL)
  650. preference->ordinal = module->numprefs;
  651. else
  652. preference->ordinal = -1; /* no ordinal for you */
  653. /*
  654. * Make sure that only lower-case ASCII letters, numbers,
  655. * underscores, and dots appear in the preference name.
  656. *
  657. * Crash if there is, as that's an error in the code;
  658. * you can make the title and description nice strings
  659. * with capitalization, white space, punctuation, etc.,
  660. * but the name can be used on the command line,
  661. * and shouldn't require quoting, shifting, etc.
  662. */
  663. for (p = name; *p != '\0'; p++)
  664. if (!(isascii((guchar)*p) &&
  665. (islower((guchar)*p) || isdigit((guchar)*p) || *p == '_' || *p == '.')))
  666. g_error("Preference %s.%s contains invalid characters", module->name, name);
  667. /*
  668. * Make sure there's not already a preference with that
  669. * name. Crash if there is, as that's an error in the
  670. * code, and the code has to be fixed not to register
  671. * more than one preference with the same name.
  672. */
  673. if (prefs_find_preference(module, name) != NULL)
  674. g_error("Preference %s has already been registered", name);
  675. if ((type != PREF_OBSOLETE) &&
  676. /* Don't compare if it's a subtree */
  677. (module->name != NULL)) {
  678. /*
  679. * Make sure the preference name doesn't begin with the
  680. * module name, as that's redundant and Just Silly.
  681. */
  682. if (!((strncmp(name, module->name, strlen(module->name)) != 0) ||
  683. (((name[strlen(module->name)]) != '.') && ((name[strlen(module->name)]) != '_'))))
  684. g_error("Preference %s begins with the module name", name);
  685. }
  686. /*
  687. * There isn't already one with that name, so add the
  688. * preference.
  689. */
  690. module->prefs = g_list_append(module->prefs, preference);
  691. if (title != NULL)
  692. module->numprefs++;
  693. return preference;
  694. }
  695. /*
  696. * Find a preference in a module's list of preferences, given the module
  697. * and the preference's name.
  698. */
  699. typedef struct {
  700. GList *list_entry;
  701. const char *name;
  702. } find_pref_arg_t;
  703. static gint
  704. preference_match(gconstpointer a, gconstpointer b)
  705. {
  706. const pref_t *pref = (const pref_t *)a;
  707. const char *name = (const char *)b;
  708. return strcmp(name, pref->name);
  709. }
  710. static gboolean module_find_pref_cb(void *value, void *data)
  711. {
  712. find_pref_arg_t* arg = (find_pref_arg_t*)data;
  713. GList *list_entry;
  714. module_t *module = (module_t *)value;
  715. if (module == NULL)
  716. return FALSE;
  717. list_entry = g_list_find_custom(module->prefs, arg->name,
  718. preference_match);
  719. if (list_entry == NULL)
  720. return FALSE;
  721. arg->list_entry = list_entry;
  722. return TRUE;
  723. }
  724. struct preference *
  725. prefs_find_preference(module_t *module, const char *name)
  726. {
  727. find_pref_arg_t arg;
  728. GList *list_entry;
  729. if (module == NULL)
  730. return NULL; /* invalid parameters */
  731. list_entry = g_list_find_custom(module->prefs, name,
  732. preference_match);
  733. if (list_entry == NULL)
  734. {
  735. arg.list_entry = NULL;
  736. if (module->submodules != NULL)
  737. {
  738. arg.name = name;
  739. wmem_tree_foreach(module->submodules, module_find_pref_cb, &arg);
  740. }
  741. list_entry = arg.list_entry;
  742. }
  743. if (list_entry == NULL)
  744. return NULL; /* no such preference */
  745. return (struct preference *) list_entry->data;
  746. }
  747. /*
  748. * Returns TRUE if the given protocol has registered preferences
  749. */
  750. gboolean
  751. prefs_is_registered_protocol(const char *name)
  752. {
  753. module_t *m = prefs_find_module(name);
  754. return (m != NULL && !m->obsolete);
  755. }
  756. /*
  757. * Returns the module title of a registered protocol
  758. */
  759. const char *
  760. prefs_get_title_by_name(const char *name)
  761. {
  762. module_t *m = prefs_find_module(name);
  763. return (m != NULL && !m->obsolete) ? m->title : NULL;
  764. }
  765. /*
  766. * Register a preference with an unsigned integral value.
  767. */
  768. void
  769. prefs_register_uint_preference(module_t *module, const char *name,
  770. const char *title, const char *description,
  771. guint base, guint *var)
  772. {
  773. pref_t *preference;
  774. preference = register_preference(module, name, title, description,
  775. PREF_UINT);
  776. preference->varp.uint = var;
  777. preference->default_val.uint = *var;
  778. g_assert(base > 0 && base != 1 && base < 37);
  779. preference->info.base = base;
  780. }
  781. /*
  782. * XXX Add a prefs_register_{uint16|port}_preference which sets max_value?
  783. */
  784. /*
  785. * Register a "custom" preference with a unsigned integral value.
  786. * XXX - This should be temporary until we can find a better way
  787. * to do "custom" preferences
  788. */
  789. static void
  790. prefs_register_uint_custom_preference(module_t *module, const char *name,
  791. const char *title, const char *description,
  792. struct pref_custom_cbs* custom_cbs, guint *var)
  793. {
  794. pref_t *preference;
  795. preference = register_preference(module, name, title, description,
  796. PREF_CUSTOM);
  797. preference->custom_cbs = *custom_cbs;
  798. preference->varp.uint = var;
  799. preference->default_val.uint = *var;
  800. }
  801. /*
  802. * Register a preference with an Boolean value.
  803. */
  804. void
  805. prefs_register_bool_preference(module_t *module, const char *name,
  806. const char *title, const char *description,
  807. gboolean *var)
  808. {
  809. pref_t *preference;
  810. preference = register_preference(module, name, title, description,
  811. PREF_BOOL);
  812. preference->varp.boolp = var;
  813. preference->default_val.boolval = *var;
  814. }
  815. /*
  816. * Register a preference with an enumerated value.
  817. */
  818. void
  819. prefs_register_enum_preference(module_t *module, const char *name,
  820. const char *title, const char *description,
  821. gint *var, const enum_val_t *enumvals,
  822. gboolean radio_buttons)
  823. {
  824. pref_t *preference;
  825. preference = register_preference(module, name, title, description,
  826. PREF_ENUM);
  827. preference->varp.enump = var;
  828. preference->default_val.enumval = *var;
  829. preference->info.enum_info.enumvals = enumvals;
  830. preference->info.enum_info.radio_buttons = radio_buttons;
  831. }
  832. static pref_t*
  833. register_string_like_preference(module_t *module, const char *name,
  834. const char *title, const char *description,
  835. const char **var, pref_type_t type)
  836. {
  837. pref_t *preference;
  838. char *varcopy;
  839. preference = register_preference(module, name, title, description,
  840. type);
  841. /*
  842. * String preference values should be non-null (as you can't
  843. * keep them null after using the preferences GUI, you can at best
  844. * have them be null strings) and freeable (as we free them
  845. * if we change them).
  846. *
  847. * If the value is a null pointer, make it a copy of a null
  848. * string, otherwise make it a copy of the value.
  849. */
  850. if (*var == NULL) {
  851. *var = g_strdup("");
  852. varcopy = g_strdup("");
  853. } else {
  854. *var = g_strdup(*var);
  855. varcopy = g_strdup(*var);
  856. }
  857. preference->varp.string = var;
  858. preference->default_val.string = varcopy;
  859. preference->stashed_val.string = NULL;
  860. return preference;
  861. }
  862. /*
  863. * Register a preference with a character-string value.
  864. */
  865. void
  866. prefs_register_string_preference(module_t *module, const char *name,
  867. const char *title, const char *description,
  868. const char **var)
  869. {
  870. register_string_like_preference(module, name, title, description, var,
  871. PREF_STRING);
  872. }
  873. /*
  874. * Register a "custom" preference with a character-string value.
  875. * XXX - This should be temporary until we can find a better way
  876. * to do "custom" preferences
  877. */
  878. static
  879. void prefs_register_string_custom_preference(module_t *module, const char *name,
  880. const char *title, const char *description,
  881. struct pref_custom_cbs* custom_cbs, const char **var)
  882. {
  883. pref_t *preference;
  884. preference = register_string_like_preference(module, name, title, description, var,
  885. PREF_CUSTOM);
  886. preference->custom_cbs = *custom_cbs;
  887. }
  888. /*
  889. * Register a preference with a file name (string) value.
  890. */
  891. void
  892. prefs_register_filename_preference(module_t *module, const char *name,
  893. const char *title, const char *description,
  894. const char **var)
  895. {
  896. register_string_like_preference(module, name, title, description, var,
  897. PREF_FILENAME);
  898. }
  899. /*
  900. * Register a preference with a directory name (string) value.
  901. */
  902. void
  903. prefs_register_directory_preference(module_t *module, const char *name,
  904. const char *title, const char *description,
  905. const char **var)
  906. {
  907. register_string_like_preference(module, name, title, description, var,
  908. PREF_DIRNAME);
  909. }
  910. /*
  911. * Register a preference with a ranged value.
  912. */
  913. void
  914. prefs_register_range_preference(module_t *module, const char *name,
  915. const char *title, const char *description,
  916. range_t **var, guint32 max_value)
  917. {
  918. pref_t *preference;
  919. preference = register_preference(module, name, title, description,
  920. PREF_RANGE);
  921. preference->info.max_value = max_value;
  922. /*
  923. * Range preference values should be non-null (as you can't
  924. * keep them null after using the preferences GUI, you can at best
  925. * have them be empty ranges) and freeable (as we free them
  926. * if we change them).
  927. *
  928. * If the value is a null pointer, make it an empty range.
  929. */
  930. if (*var == NULL)
  931. *var = range_empty();
  932. preference->varp.range = var;
  933. preference->default_val.range = range_copy(*var);
  934. preference->stashed_val.range = NULL;
  935. }
  936. /*
  937. * Register a static text 'preference'. It can be used to add explanatory
  938. * text inline with other preferences in the GUI.
  939. * Note: Static preferences are not saved to the preferences file.
  940. */
  941. void
  942. prefs_register_static_text_preference(module_t *module, const char *name,
  943. const char *title,
  944. const char *description)
  945. {
  946. register_preference(module, name, title, description, PREF_STATIC_TEXT);
  947. }
  948. /*
  949. * Register a uat 'preference'. It adds a button that opens the uat's window in the
  950. * preferences tab of the module.
  951. */
  952. extern void
  953. prefs_register_uat_preference(module_t *module, const char *name,
  954. const char *title, const char *description,
  955. uat_t* uat)
  956. {
  957. pref_t* preference = register_preference(module, name, title, description, PREF_UAT);
  958. preference->varp.uat = uat;
  959. }
  960. /*
  961. * Register a color preference.
  962. */
  963. void prefs_register_color_preference(module_t *module, const char *name,
  964. const char *title, const char *description, color_t *color)
  965. {
  966. pref_t* preference = register_preference(module, name, title, description, PREF_COLOR);
  967. preference->varp.colorp = color;
  968. preference->default_val.color = *color;
  969. }
  970. /*
  971. * Register a "custom" preference with a list.
  972. * XXX - This should be temporary until we can find a better way
  973. * to do "custom" preferences
  974. */
  975. typedef void (*pref_custom_list_init_cb) (pref_t* pref, GList** value);
  976. static
  977. void prefs_register_list_custom_preference(module_t *module, const char *name,
  978. const char *title, const char *description, struct pref_custom_cbs* custom_cbs,
  979. pref_custom_list_init_cb init_cb, GList** list)
  980. {
  981. pref_t* preference = register_preference(module, name, title, description, PREF_CUSTOM);
  982. preference->custom_cbs = *custom_cbs;
  983. init_cb(preference, list);
  984. }
  985. /*
  986. * Register a custom preference.
  987. */
  988. void prefs_register_custom_preference(module_t *module, const char *name,
  989. const char *title, const char *description, struct pref_custom_cbs* custom_cbs,
  990. void** custom_data _U_)
  991. {
  992. pref_t* preference = register_preference(module, name, title, description, PREF_CUSTOM);
  993. preference->custom_cbs = *custom_cbs;
  994. /* XXX - wait until we can handle void** pointers
  995. preference->custom_cbs.init_cb(preference, custom_data);
  996. */
  997. }
  998. /*
  999. * Register a preference that used to be supported but no longer is.
  1000. */
  1001. void
  1002. prefs_register_obsolete_preference(module_t *module, const char *name)
  1003. {
  1004. register_preference(module, name, NULL, NULL, PREF_OBSOLETE);
  1005. }
  1006. /*
  1007. * Check to see if a preference is obsolete.
  1008. */
  1009. extern gboolean
  1010. prefs_get_preference_obsolete(pref_t *pref)
  1011. {
  1012. if (pref)
  1013. return pref->type == PREF_OBSOLETE ? TRUE : FALSE;
  1014. return TRUE;
  1015. }
  1016. /*
  1017. * Make a preference obsolete.
  1018. */
  1019. extern prefs_set_pref_e
  1020. prefs_set_preference_obsolete(pref_t *pref)
  1021. {
  1022. if (pref) {
  1023. pref->type = PREF_OBSOLETE;
  1024. return PREFS_SET_OK;
  1025. }
  1026. return PREFS_SET_NO_SUCH_PREF;
  1027. }
  1028. /* Return the value assigned to the given uint preference. */
  1029. guint prefs_get_uint_preference(pref_t *pref)
  1030. {
  1031. if (pref && pref->type == PREF_UINT)
  1032. return *pref->varp.uint;
  1033. return 0;
  1034. }
  1035. /*
  1036. * Call a callback function, with a specified argument, for each preference
  1037. * in a given module.
  1038. *
  1039. * If any of the callbacks return a non-zero value, stop and return that
  1040. * value, otherwise return 0.
  1041. */
  1042. guint
  1043. prefs_pref_foreach(module_t *module, pref_cb callback, gpointer user_data)
  1044. {
  1045. GList *elem;
  1046. pref_t *pref;
  1047. guint ret;
  1048. for (elem = g_list_first(module->prefs); elem != NULL; elem = g_list_next(elem)) {
  1049. pref = (pref_t *)elem->data;
  1050. if (pref->type == PREF_OBSOLETE) {
  1051. /*
  1052. * This preference is no longer supported; it's
  1053. * not a real preference, so we don't call the
  1054. * callback for it (i.e., we treat it as if it
  1055. * weren't found in the list of preferences,
  1056. * and we weren't called in the first place).
  1057. */
  1058. continue;
  1059. }
  1060. ret = (*callback)(pref, user_data);
  1061. if (ret != 0)
  1062. return ret;
  1063. }
  1064. return 0;
  1065. }
  1066. static const enum_val_t print_format_vals[] = {
  1067. { "text", "Plain Text", PR_FMT_TEXT },
  1068. { "postscript", "Postscript", PR_FMT_PS },
  1069. { NULL, NULL, 0 }
  1070. };
  1071. static const enum_val_t print_dest_vals[] = {
  1072. #ifdef _WIN32
  1073. /* "PR_DEST_CMD" means "to printer" on Windows */
  1074. { "command", "Printer", PR_DEST_CMD },
  1075. #else
  1076. { "command", "Command", PR_DEST_CMD },
  1077. #endif
  1078. { "file", "File", PR_DEST_FILE },
  1079. { NULL, NULL, 0 }
  1080. };
  1081. static void stats_callback(void)
  1082. {
  1083. /* Test for a sane tap update interval */
  1084. if (prefs.tap_update_interval < 100 || prefs.tap_update_interval > 10000)
  1085. prefs.tap_update_interval = TAP_UPDATE_DEFAULT_INTERVAL;
  1086. #ifdef HAVE_LIBPORTAUDIO
  1087. /* Test for a sane max channels entry */
  1088. if (prefs.rtp_player_max_visible < 1 || prefs.rtp_player_max_visible > 10)
  1089. prefs.rtp_player_max_visible = RTP_PLAYER_DEFAULT_VISIBLE;
  1090. #endif
  1091. }
  1092. static void gui_callback(void)
  1093. {
  1094. /* Ensure there is at least one file count */
  1095. if (prefs.gui_recent_files_count_max == 0)
  1096. prefs.gui_recent_files_count_max = 10;
  1097. /* Ensure there is at least one display filter entry */
  1098. if (prefs.gui_recent_df_entries_max == 0)
  1099. prefs.gui_recent_df_entries_max = 10;
  1100. }
  1101. static void gui_layout_callback(void)
  1102. {
  1103. if (prefs.gui_layout_type == layout_unused ||
  1104. prefs.gui_layout_type >= layout_type_max) {
  1105. /* XXX - report an error? It's not a syntax error - we'd need to
  1106. add a way of reporting a *semantic* error. */
  1107. prefs.gui_layout_type = layout_type_5;
  1108. }
  1109. }
  1110. /******************************************************
  1111. * All custom preference function callbacks
  1112. ******************************************************/
  1113. static void custom_pref_no_cb(pref_t* pref _U_) {}
  1114. /*
  1115. * Console log level custom preference functions
  1116. */
  1117. static void console_log_level_reset_cb(pref_t* pref)
  1118. {
  1119. *pref->varp.uint = pref->default_val.uint;
  1120. }
  1121. static prefs_set_pref_e console_log_level_set_cb(pref_t* pref, const gchar* value, gboolean* changed)
  1122. {
  1123. guint uval;
  1124. uval = (guint)strtoul(value, NULL, 10);
  1125. if (*pref->varp.uint != uval) {
  1126. *changed = TRUE;
  1127. *pref->varp.uint = uval;
  1128. }
  1129. if (*pref->varp.uint & (G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG)) {
  1130. /*
  1131. * GLib >= 2.32 drops INFO and DEBUG messages by default. Tell
  1132. * it not to do that.
  1133. */
  1134. g_setenv("G_MESSAGES_DEBUG", "all", TRUE);
  1135. }
  1136. return PREFS_SET_OK;
  1137. }
  1138. static const char * console_log_level_type_name_cb(void) {
  1139. return "Log level";
  1140. }
  1141. static char * console_log_level_type_description_cb(void) {
  1142. return g_strdup_printf(
  1143. "Console log level (for debugging)\n"
  1144. "A bitmask of log levels:\n"
  1145. "ERROR = 4\n"
  1146. "CRITICAL = 8\n"
  1147. "WARNING = 16\n"
  1148. "MESSAGE = 32\n"
  1149. "INFO = 64\n"
  1150. "DEBUG = 128");
  1151. }
  1152. static gboolean console_log_level_is_default_cb(pref_t* pref) {
  1153. return *pref->varp.uint == pref->default_val.uint;
  1154. }
  1155. static char * console_log_level_to_str_cb(pref_t* pref, gboolean default_val) {
  1156. return g_strdup_printf("%u", default_val ? pref->default_val.uint : *pref->varp.uint);
  1157. }
  1158. /*
  1159. * Column preference functions
  1160. */
  1161. #define PRS_COL_HIDDEN "column.hidden"
  1162. #define PRS_COL_FMT "column.format"
  1163. #define PRS_COL_NUM "column.number"
  1164. static module_t *gui_column_module = NULL;
  1165. static void column_hidden_free_cb(pref_t* pref)
  1166. {
  1167. g_free((char *)*pref->varp.string);
  1168. *pref->varp.string = NULL;
  1169. g_free(pref->default_val.string);
  1170. pref->default_val.string = NULL;
  1171. }
  1172. static void column_hidden_reset_cb(pref_t* pref)
  1173. {
  1174. g_free((void *)*pref->varp.string);
  1175. *pref->varp.string = g_strdup(pref->default_val.string);
  1176. }
  1177. static prefs_set_pref_e column_hidden_set_cb(pref_t* pref, const gchar* value, gboolean* changed)
  1178. {
  1179. GList *clp;
  1180. fmt_data *cfmt;
  1181. pref_t *format_pref;
  1182. if (*pref->varp.string) {
  1183. if (strcmp(*pref->varp.string, value) != 0) {
  1184. *changed = TRUE;
  1185. g_free((void *)*pref->varp.string);
  1186. *pref->varp.string = g_strdup(value);
  1187. }
  1188. } else if (value) {
  1189. *pref->varp.string = g_strdup(value);
  1190. }
  1191. /*
  1192. * Set the "visible" flag for the existing columns; we need to
  1193. * do this if we set PRS_COL_HIDDEN but don't set PRS_COL_FMT
  1194. * after setting it (which might be the case if, for example, we
  1195. * set PRS_COL_HIDDEN on the command line).
  1196. */
  1197. format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
  1198. for (clp = *format_pref->varp.list; clp != NULL; clp = clp->next) {
  1199. cfmt = (fmt_data *)clp->data;
  1200. cfmt->visible = prefs_is_column_visible(*pref->varp.string, cfmt);
  1201. }
  1202. return PREFS_SET_OK;
  1203. }
  1204. static const char * column_hidden_type_name_cb(void) {
  1205. return "Packet list hidden columns";
  1206. }
  1207. static char * column_hidden_type_description_cb(void) {
  1208. return g_strdup("List all columns to hide in the packet list.");
  1209. }
  1210. static char * column_hidden_to_str_cb(pref_t* pref, gboolean default_val) {
  1211. GString *cols_hidden = g_string_new ("");
  1212. GList *clp;
  1213. fmt_data *cfmt;
  1214. pref_t *format_pref;
  1215. if (default_val)
  1216. return g_strdup(pref->default_val.string);
  1217. format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
  1218. clp = (format_pref) ? *format_pref->varp.list : NULL;
  1219. while (clp) {
  1220. gchar *prefs_fmt;
  1221. cfmt = (fmt_data *) clp->data;
  1222. if ((cfmt->fmt == COL_CUSTOM) && (cfmt->custom_field)) {
  1223. prefs_fmt = g_strdup_printf("%s:%s:%d:%c",
  1224. col_format_to_string(cfmt->fmt),
  1225. cfmt->custom_field,
  1226. cfmt->custom_occurrence,
  1227. cfmt->resolved ? 'R' : 'U');
  1228. } else {
  1229. prefs_fmt = g_strdup(col_format_to_string(cfmt->fmt));
  1230. }
  1231. if (!cfmt->visible) {
  1232. if (cols_hidden->len)
  1233. g_string_append (cols_hidden, ",");
  1234. g_string_append (cols_hidden, prefs_fmt);
  1235. }
  1236. clp = clp->next;
  1237. }
  1238. return g_string_free (cols_hidden, FALSE);
  1239. }
  1240. static gboolean column_hidden_is_default_cb(pref_t* pref) {
  1241. char *cur_hidden_str = column_hidden_to_str_cb(pref, FALSE);
  1242. gboolean is_default = g_strcmp0(cur_hidden_str, pref->default_val.string) == 0;
  1243. g_free(cur_hidden_str);
  1244. return is_default;
  1245. }
  1246. /* Number of columns "preference". This is only used internally and is not written to the
  1247. * preference file
  1248. */
  1249. static void column_num_reset_cb(pref_t* pref)
  1250. {
  1251. *pref->varp.uint = pref->default_val.uint;
  1252. }
  1253. static prefs_set_pref_e column_num_set_cb(pref_t* pref _U_, const gchar* value _U_, gboolean* changed _U_)
  1254. {
  1255. /* Don't write this to the preferences file */
  1256. return PREFS_SET_OK;
  1257. }
  1258. static const char * column_num_type_name_cb(void) {
  1259. return NULL;
  1260. }
  1261. static char * column_num_type_description_cb(void) {
  1262. return g_strdup("");
  1263. }
  1264. static gboolean column_num_is_default_cb(pref_t* pref _U_) {
  1265. return TRUE;
  1266. }
  1267. static char * column_num_to_str_cb(pref_t* pref _U_, gboolean default_val _U_) {
  1268. return g_strdup("");
  1269. }
  1270. /*
  1271. * Column format custom preference functions
  1272. */
  1273. static void column_format_init_cb(pref_t* pref, GList** value)
  1274. {
  1275. fmt_data *src_cfmt, *dest_cfmt;
  1276. GList *entry;
  1277. pref->varp.list = value;
  1278. pref->default_val.list = NULL;
  1279. for (entry = *pref->varp.list; entry != NULL; entry = g_list_next(entry)) {
  1280. src_cfmt = (fmt_data *)entry->data;
  1281. dest_cfmt = g_new(fmt_data,1);
  1282. dest_cfmt->title = g_strdup(src_cfmt->title);
  1283. dest_cfmt->fmt = src_cfmt->fmt;
  1284. if (src_cfmt->custom_field) {
  1285. dest_cfmt->custom_field = g_strdup(src_cfmt->custom_field);
  1286. dest_cfmt->custom_occurrence = src_cfmt->custom_occurrence;
  1287. } else {
  1288. dest_cfmt->custom_field = NULL;
  1289. dest_cfmt->custom_occurrence = 0;
  1290. }
  1291. dest_cfmt->visible = src_cfmt->visible;
  1292. dest_cfmt->resolved = src_cfmt->resolved;
  1293. pref->default_val.list = g_list_append(pref->default_val.list, dest_cfmt);
  1294. }
  1295. }
  1296. static void column_format_free_cb(pref_t* pref)
  1297. {
  1298. free_col_info(*pref->varp.list);
  1299. free_col_info(pref->default_val.list);
  1300. }
  1301. static void column_format_reset_cb(pref_t* pref)
  1302. {
  1303. fmt_data *src_cfmt, *dest_cfmt;
  1304. GList *entry;
  1305. pref_t *col_num_pref;
  1306. free_col_info(*pref->varp.list);
  1307. *pref->varp.list = NULL;
  1308. for (entry = pref->default_val.list; entry != NULL; entry = g_list_next(entry)) {
  1309. src_cfmt = (fmt_data *)entry->data;
  1310. dest_cfmt = g_new(fmt_data,1);
  1311. dest_cfmt->title = g_strdup(src_cfmt->title);
  1312. dest_cfmt->fmt = src_cfmt->fmt;
  1313. if (src_cfmt->custom_field) {
  1314. dest_cfmt->custom_field = g_strdup(src_cfmt->custom_field);
  1315. dest_cfmt->custom_occurrence = src_cfmt->custom_occurrence;
  1316. } else {
  1317. dest_cfmt->custom_field = NULL;
  1318. dest_cfmt->custom_occurrence = 0;
  1319. }
  1320. dest_cfmt->visible = src_cfmt->visible;
  1321. dest_cfmt->resolved = src_cfmt->resolved;
  1322. *pref->varp.list = g_list_append(*pref->varp.list, dest_cfmt);
  1323. }
  1324. col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
  1325. column_num_reset_cb(col_num_pref);
  1326. }
  1327. static prefs_set_pref_e column_format_set_cb(pref_t* pref, const gchar* value, gboolean* changed _U_)
  1328. {
  1329. GList *col_l, *col_l_elt;
  1330. fmt_data *cfmt;
  1331. gint llen;
  1332. pref_t *hidden_pref, *col_num_pref;
  1333. col_l = prefs_get_string_list(value);
  1334. if (col_l == NULL)
  1335. return PREFS_SET_SYNTAX_ERR;
  1336. if ((g_list_length(col_l) % 2) != 0) {
  1337. /* A title didn't have a matching format. */
  1338. prefs_clear_string_list(col_l);
  1339. return PREFS_SET_SYNTAX_ERR;
  1340. }
  1341. /* Check to make sure all column formats are valid. */
  1342. col_l_elt = g_list_first(col_l);
  1343. while (col_l_elt) {
  1344. fmt_data cfmt_check;
  1345. /* Go past the title. */
  1346. col_l_elt = col_l_elt->next;
  1347. /* Parse the format to see if it's valid. */
  1348. if (!parse_column_format(&cfmt_check, (char *)col_l_elt->data)) {
  1349. /* It's not a valid column format. */
  1350. prefs_clear_string_list(col_l);
  1351. return PREFS_SET_SYNTAX_ERR;
  1352. }
  1353. if (cfmt_check.fmt != COL_CUSTOM) {
  1354. /* Some predefined columns have been migrated to use custom columns.
  1355. * We'll convert these silently here */
  1356. try_convert_to_custom_column(&col_l_elt->data);
  1357. } else {
  1358. /* We don't need the custom column field on this pass. */
  1359. g_free(cfmt_check.custom_field);
  1360. }
  1361. /* Go past the format. */
  1362. col_l_elt = col_l_elt->next;
  1363. }
  1364. /* They're all valid; process them. */
  1365. free_col_info(*pref->varp.list);
  1366. *pref->varp.list = NULL;
  1367. hidden_pref = prefs_find_preference(gui_column_module, PRS_COL_HIDDEN);
  1368. col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
  1369. llen = g_list_length(col_l);
  1370. *col_num_pref->varp.uint = llen / 2;
  1371. col_l_elt = g_list_first(col_l);
  1372. while (col_l_elt) {
  1373. cfmt = g_new(fmt_data,1);
  1374. cfmt->title = g_strdup((gchar *)col_l_elt->data);
  1375. col_l_elt = col_l_elt->next;
  1376. parse_column_format(cfmt, (char *)col_l_elt->data);
  1377. cfmt->visible = prefs_is_column_visible((gchar*)(*hidden_pref->varp.string), cfmt);
  1378. col_l_elt = col_l_elt->next;
  1379. *pref->varp.list = g_list_append(*pref->varp.list, cfmt);
  1380. }
  1381. prefs_clear_string_list(col_l);
  1382. column_hidden_free_cb(hidden_pref);
  1383. return PREFS_SET_OK;
  1384. }
  1385. static const char * column_format_type_name_cb(void) {
  1386. return "Packet list column format";
  1387. }
  1388. static char * column_format_type_description_cb(void) {
  1389. return g_strdup("Each pair of strings consists of a column title and its format");
  1390. }
  1391. static gboolean column_format_is_default_cb(pref_t* pref) {
  1392. GList *clp = *pref->varp.list,
  1393. *pref_col = g_list_first(clp),
  1394. *def_col = g_list_first(pref->default_val.list);
  1395. fmt_data *cfmt, *def_cfmt;
  1396. gboolean is_default = TRUE;
  1397. pref_t *col_num_pref;
  1398. /* See if the column data has changed from the default */
  1399. col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
  1400. if (col_num_pref && *col_num_pref->varp.uint != col_num_pref->default_val.uint) {
  1401. is_default = FALSE;
  1402. } else {
  1403. while (pref_col && def_col) {
  1404. cfmt = (fmt_data *) pref_col->data;
  1405. def_cfmt = (fmt_data *) def_col->data;
  1406. if ((g_strcmp0(cfmt->title, def_cfmt->title) != 0) ||
  1407. (cfmt->fmt != def_cfmt->fmt) ||
  1408. (((cfmt->fmt == COL_CUSTOM) && (cfmt->custom_field)) &&
  1409. ((g_strcmp0(cfmt->custom_field, def_cfmt->custom_field) != 0) ||
  1410. (cfmt->resolved != def_cfmt->resolved)))) {
  1411. is_default = FALSE;
  1412. break;
  1413. }
  1414. pref_col = pref_col->next;
  1415. def_col = def_col->next;
  1416. }
  1417. }
  1418. return is_default;
  1419. }
  1420. static char * column_format_to_str_cb(pref_t* pref, gboolean default_val) {
  1421. GList *pref_l = default_val ? pref->default_val.list : *pref->varp.list;
  1422. GList *clp = g_list_first(pref_l);
  1423. GList *col_l;
  1424. fmt_data *cfmt;
  1425. gchar *prefs_fmt;
  1426. char *column_format_str;
  1427. col_l = NULL;
  1428. while (clp) {
  1429. cfmt = (fmt_data *) clp->data;
  1430. col_l = g_list_append(col_l, g_strdup(cfmt->title));
  1431. if ((cfmt->fmt == COL_CUSTOM) && (cfmt->custom_field)) {
  1432. prefs_fmt = g_strdup_printf("%s:%s:%d:%c",
  1433. col_format_to_string(cfmt->fmt),
  1434. cfmt->custom_field,
  1435. cfmt->custom_occurrence,
  1436. cfmt->resolved ? 'R' : 'U');
  1437. } else {
  1438. prefs_fmt = g_strdup(col_format_to_string(cfmt->fmt));
  1439. }
  1440. col_l = g_list_append(col_l, prefs_fmt);
  1441. clp = clp->next;
  1442. }
  1443. column_format_str = join_string_list(col_l);
  1444. /* This frees the list of strings, but not the strings to which it
  1445. refers; they are free'ed in join_string_list(). */
  1446. g_list_free(col_l);
  1447. return column_format_str;
  1448. }
  1449. /****** Capture column custom preference functions ******/
  1450. /* This routine is only called when Wireshark is started, NOT when another profile is selected.
  1451. Copy the pref->capture_columns list (just loaded with the capture_cols[] struct values)
  1452. to prefs->default_val.list.
  1453. */
  1454. static void capture_column_init_cb(pref_t* pref, GList** capture_cols_values)
  1455. {
  1456. GList *ccv_list = *capture_cols_values,
  1457. *dlist = NULL;
  1458. /* */
  1459. while (ccv_list) {
  1460. dlist = g_list_append(dlist, g_strdup((gchar *)ccv_list->data));
  1461. ccv_list = ccv_list->next;
  1462. }
  1463. pref->default_val.list = dlist;
  1464. pref->varp.list = &prefs.capture_columns;
  1465. pref->stashed_val.boolval = FALSE;
  1466. }
  1467. /* Free the prefs->capture_columns list strings and remove the list entries.
  1468. Note that since pref->varp.list points to &prefs.capture_columns, it is
  1469. also freed.
  1470. */
  1471. static void capture_column_free_cb(pref_t* pref)
  1472. {
  1473. GList *clist = prefs.capture_columns;
  1474. gchar *col_name;
  1475. while (clist) {
  1476. col_name = (gchar *)clist->data;
  1477. g_free(col_name);
  1478. clist = g_list_remove_link(clist, clist);
  1479. }
  1480. g_list_free(clist);
  1481. prefs.capture_columns = NULL;
  1482. if (pref->stashed_val.boolval == TRUE) {
  1483. GList *dlist;
  1484. gchar *col;
  1485. dlist = pref->default_val.list;
  1486. while (dlist != NULL) {
  1487. col = (gchar *)dlist->data;
  1488. g_free(col);
  1489. dlist = g_list_remove_link(dlist, dlist);
  1490. }
  1491. g_list_free(dlist);
  1492. dlist = NULL;
  1493. }
  1494. }
  1495. /* Copy pref->default_val.list to *pref->varp.list.
  1496. */
  1497. static void capture_column_reset_cb(pref_t* pref)
  1498. {
  1499. GList *vlist, *dlist;
  1500. gchar *vcol;
  1501. /* Free the column name strings and remove the links from *pref->varp.list */
  1502. vlist = *pref->varp.list;
  1503. while (vlist != NULL) {
  1504. vcol = (gchar *)vlist->data;
  1505. g_free(vcol);
  1506. vlist = g_list_remove_link(vlist, vlist);
  1507. }
  1508. g_list_free(vlist);
  1509. vlist = NULL;
  1510. for (dlist = pref->default_val.list; dlist != NULL; dlist = g_list_next(dlist)) {
  1511. vlist = g_list_append(vlist, g_strdup((gchar *)dlist->data));
  1512. }
  1513. *pref->varp.list = vlist;
  1514. }
  1515. static prefs_set_pref_e capture_column_set_cb(pref_t* pref, const gchar* value, gboolean* changed _U_)
  1516. {
  1517. GList *col_l = prefs_get_string_list(value);
  1518. GList *col_l_elt;
  1519. gchar *col_name;
  1520. int i;
  1521. if (col_l == NULL)
  1522. return PREFS_SET_SYNTAX_ERR;
  1523. capture_column_free_cb(pref);
  1524. /* If value (the list of capture.columns read from preferences) is empty, set capture.columns
  1525. to the full list of valid capture column names. */
  1526. col_l_elt = g_list_first(col_l);
  1527. if (!(*(gchar *)col_l_elt->data)) {
  1528. for (i = 0; i < num_capture_cols; i++) {
  1529. col_name = g_strdup(capture_cols[i]);
  1530. prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
  1531. }
  1532. }
  1533. /* Verify that all the column names are valid. If not, use the entire list of valid columns.
  1534. */
  1535. while (col_l_elt) {
  1536. gboolean found_match = FALSE;
  1537. col_name = (gchar *)col_l_elt->data;
  1538. for (i = 0; i < num_capture_cols; i++) {
  1539. if (strcmp(col_name, capture_cols[i])==0) {
  1540. found_match = TRUE;
  1541. break;
  1542. }
  1543. }
  1544. if (!found_match) {
  1545. /* One or more cols are invalid so use the entire list of valid cols. */
  1546. for (i = 0; i < num_capture_cols; i++) {
  1547. col_name = g_strdup(capture_cols[i]);
  1548. prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
  1549. }
  1550. pref->varp.list = &prefs.capture_columns;
  1551. return PREFS_SET_SYNTAX_ERR;
  1552. }
  1553. col_l_elt = col_l_elt->next;
  1554. }
  1555. col_l_elt = g_list_first(col_l);
  1556. while (col_l_elt) {
  1557. col_name = (gchar *)col_l_elt->data;
  1558. prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
  1559. col_l_elt = col_l_elt->next;
  1560. }
  1561. pref->varp.list = &prefs.capture_columns;
  1562. return PREFS_SET_OK;
  1563. }
  1564. static const char * capture_column_type_name_cb(void) {
  1565. return "Column list";
  1566. }
  1567. static char * capture_column_type_description_cb(void) {
  1568. return g_strdup(
  1569. "List of columns to be displayed in the capture options dialog.\n"
  1570. CAPTURE_COL_TYPE_DESCRIPTION);
  1571. }
  1572. static gboolean capture_column_is_default_cb(pref_t* pref) {
  1573. GList *pref_col = g_list_first(prefs.capture_columns),
  1574. *def_col = g_list_first(pref->default_val.list);
  1575. gboolean is_default = TRUE;
  1576. /* See if the column data has changed from the default */
  1577. while (pref_col && def_col) {
  1578. if (strcmp((gchar *)pref_col->data, (gchar *)def_col->data) != 0) {
  1579. is_default = FALSE;
  1580. break;
  1581. }
  1582. pref_col = pref_col->next;
  1583. def_col = def_col->next;
  1584. }
  1585. /* Ensure the same column count */
  1586. if (((pref_col == NULL) && (def_col != NULL)) ||
  1587. ((pref_col != NULL) && (def_col == NULL)))
  1588. is_default = FALSE;
  1589. return is_default;
  1590. }
  1591. static char * capture_column_to_str_cb(pref_t* pref, gboolean default_val) {
  1592. GList *pref_l = default_val ? pref->default_val.list : prefs.capture_columns;
  1593. GList *clp = g_list_first(pref_l);
  1594. GList *col_l = NULL;
  1595. gchar *col, *capture_column_str;
  1596. while (clp) {
  1597. col = (gchar *) clp->data;
  1598. col_l = g_list_append(col_l, g_strdup(col));
  1599. clp = clp->next;
  1600. }
  1601. capture_column_str = join_string_list(col_l);
  1602. /* This frees the list of strings, but not the strings to which it
  1603. refers; they are free'ed in write_string_list(). */
  1604. g_list_free(col_l);
  1605. return capture_column_str;
  1606. }
  1607. static void colorized_frame_free_cb(pref_t* pref)
  1608. {
  1609. g_free((char *)*pref->varp.string);
  1610. *pref->varp.string = NULL;
  1611. g_free(pref->default_val.string);
  1612. pref->default_val.string = NULL;
  1613. }
  1614. static void colorized_frame_reset_cb(pref_t* pref)
  1615. {
  1616. g_free((void *)*pref->varp.string);
  1617. *pref->varp.string = g_strdup(pref->default_val.string);
  1618. }
  1619. static prefs_set_pref_e colorized_frame_set_cb(pref_t* pref, const gchar* value, gboolean* changed)
  1620. {
  1621. if (strcmp(*pref->varp.string, value) != 0) {
  1622. *changed = TRUE;
  1623. g_free((void *)*pref->varp.string);
  1624. *pref->varp.string = g_strdup(value);
  1625. }
  1626. return PREFS_SET_OK;
  1627. }
  1628. static const char * colorized_frame_type_name_cb(void) {
  1629. /* Don't write the colors of the 10 easy-access-colorfilters to the preferences
  1630. * file until the colors can be changed in the GUI. Currently this is not really
  1631. * possible since the STOCK-icons for these colors are hardcoded.
  1632. *
  1633. * XXX Find a way to change the colors of the STOCK-icons on the fly and then
  1634. * add these 10 colors to the list of colors that can be changed through
  1635. * the preferences.
  1636. *
  1637. */
  1638. return NULL;
  1639. }
  1640. static char * colorized_frame_type_description_cb(void) {
  1641. return g_strdup("");
  1642. }
  1643. static gboolean colorized_frame_is_default_cb(pref_t* pref _U_) {
  1644. return TRUE;
  1645. }
  1646. static char * colorized_frame_to_str_cb(pref_t* pref _U_, gboolean default_val _U_) {
  1647. return g_strdup("");
  1648. }
  1649. /*
  1650. * Register all non-dissector modules' preferences.
  1651. */
  1652. static module_t *gui_module = NULL;
  1653. static module_t *gui_color_module = NULL;
  1654. static module_t *nameres_module = NULL;
  1655. void
  1656. prefs_register_modules(void)
  1657. {
  1658. module_t *printing, *capture_module, *console_module,
  1659. *gui_layout_module, *gui_font_module;
  1660. struct pref_custom_cbs custom_cbs;
  1661. if (protocols_module != NULL) {
  1662. /* Already setup preferences */
  1663. return;
  1664. }
  1665. /* Ensure the "global" preferences have been initialized so the
  1666. * preference API has the proper default values to work from
  1667. */
  1668. pre_init_prefs();
  1669. /* GUI
  1670. * These are "simple" GUI preferences that can be read/written using the
  1671. * preference module API. These preferences still use their own
  1672. * configuration screens for access, but this cuts down on the
  1673. * preference "string compare list" in set_pref()
  1674. */
  1675. gui_module = prefs_register_module(NULL, "gui", "User Interface",
  1676. "User Interface", &gui_callback, FALSE);
  1677. /* gui.console_open is placed first in the list so that any problems encountered
  1678. * in the following prefs can be displayed in the console window.
  1679. */
  1680. prefs_register_enum_preference(gui_module, "console_open",
  1681. "Open a console window",
  1682. "Open a console window (WIN32 only)",
  1683. (gint*)(void*)(&prefs.gui_console_open), gui_console_open_type, FALSE);
  1684. prefs_register_obsolete_preference(gui_module, "scrollbar_on_right");
  1685. prefs_register_obsolete_preference(gui_module, "packet_list_sel_browse");
  1686. prefs_register_obsolete_preference(gui_module, "protocol_tree_sel_browse");
  1687. prefs_register_bool_preference(gui_module, "tree_view_altern_colors",
  1688. "Alternating colors in TreeViews",
  1689. "Alternating colors in TreeViews?",
  1690. &prefs.gui_altern_colors);
  1691. prefs_register_bool_preference(gui_module, "expert_composite_eyecandy",
  1692. "Display Icons on Expert Composite Dialog Tabs",
  1693. "Display Icons on Expert Composite Dialog Tabs?",
  1694. &prefs.gui_expert_composite_eyecandy);
  1695. prefs_register_bool_preference(gui_module, "filter_toolbar_show_in_statusbar",
  1696. "Place filter toolbar inside the statusbar",
  1697. "Place filter toolbar inside the statusbar?",
  1698. &prefs.filter_toolbar_show_in_statusbar);
  1699. prefs_register_enum_preference(gui_module, "protocol_tree_line_style",
  1700. "Protocol-tree line style",
  1701. "Protocol-tree line style",
  1702. &prefs.gui_ptree_line_style, gui_ptree_line_style, FALSE);
  1703. prefs_register_enum_preference(gui_module, "protocol_tree_expander_style",
  1704. "Protocol-tree expander style",
  1705. "Protocol-tree expander style",
  1706. &prefs.gui_ptree_expander_style, gui_ptree_expander_style, FALSE);
  1707. prefs_register_enum_preference(gui_module, "hex_dump_highlight_style",
  1708. "Hex dump highlight style",
  1709. "Hex dump highlight style",
  1710. &prefs.gui_hex_dump_highlight_style, gui_hex_dump_highlight_style, FALSE);
  1711. gui_column_module = prefs_register_subtree(gui_module, "Columns", "Columns", NULL);
  1712. custom_cbs.free_cb = column_hidden_free_cb;
  1713. custom_cbs.reset_cb = column_hidden_reset_cb;
  1714. custom_cbs.set_cb = column_hidden_set_cb;
  1715. custom_cbs.type_name_cb = column_hidden_type_name_cb;
  1716. custom_cbs.type_description_cb = column_hidden_type_description_cb;
  1717. custom_cbs.is_default_cb = column_hidden_is_default_cb;
  1718. custom_cbs.to_str_cb = column_hidden_to_str_cb;
  1719. prefs_register_string_custom_preference(gui_column_module, PRS_COL_HIDDEN, "Packet list hidden columns",
  1720. "List all columns to hide in the packet list", &custom_cbs, (const char **)&cols_hidden_list);
  1721. custom_cbs.free_cb = column_format_free_cb;
  1722. custom_cbs.reset_cb = column_format_reset_cb;
  1723. custom_cbs.set_cb = column_format_set_cb;
  1724. custom_cbs.type_name_cb = column_format_type_name_cb;
  1725. custom_cbs.type_description_cb = column_format_type_description_cb;
  1726. custom_cbs.is_default_cb = column_format_is_default_cb;
  1727. custom_cbs.to_str_cb = column_format_to_str_cb;
  1728. prefs_register_list_custom_preference(gui_column_module, PRS_COL_FMT, "Packet list column format",
  1729. "Each pair of strings consists of a column title and its format", &custom_cbs,
  1730. column_format_init_cb, &prefs.col_list);
  1731. /* Number of columns. This is only used internally and is not written to the
  1732. * preference file
  1733. */
  1734. custom_cbs.free_cb = custom_pref_no_cb;
  1735. custom_cbs.reset_cb = column_num_reset_cb;
  1736. custom_cbs.set_cb = column_num_set_cb;
  1737. custom_cbs.type_name_cb = column_num_type_name_cb;
  1738. custom_cbs.type_description_cb = column_num_type_description_cb;
  1739. custom_cbs.is_default_cb = column_num_is_default_cb;
  1740. custom_cbs.to_str_cb = column_num_to_str_cb;
  1741. prefs_register_uint_custom_preference(gui_column_module, PRS_COL_NUM, "Number of columns",
  1742. "Number of columns in col_list", &custom_cbs, &prefs.num_cols);
  1743. /* User Interface : Font */
  1744. gui_font_module = prefs_register_subtree(gui_module, "Font", "Font", NULL);
  1745. prefs_register_obsolete_preference(gui_font_module, "font_name");
  1746. prefs_register_string_preference(gui_font_module, "gtk2.font_name", "Font name",
  1747. "Font name for packet list, protocol tree, and hex dump panes. (GTK+)", (const char **)&prefs.gui_gtk2_font_name);
  1748. prefs_register_string_preference(gui_font_module, "qt.font_name", "Font name",
  1749. "Font name for packet list, protocol tree, and hex dump panes. (Qt)", (const char **)&prefs.gui_qt_font_name);
  1750. /* User Interface : Colors */
  1751. gui_color_module = prefs_register_subtree(gui_module, "Colors", "Colors", NULL);
  1752. prefs_register_color_preference(gui_color_module, "marked_frame.fg", "Color preferences for a marked frame",
  1753. "Color preferences for a marked frame", &prefs.gui_marked_fg);
  1754. prefs_register_color_preference(gui_color_module, "marked_frame.bg", "Color preferences for a marked frame",
  1755. "Color preferences for a marked frame", &prefs.gui_marked_bg);
  1756. prefs_register_color_preference(gui_color_module, "ignored_frame.fg", "Color preferences for a ignored frame",
  1757. "Color preferences for a ignored frame", &prefs.gui_ignored_fg);
  1758. prefs_register_color_preference(gui_color_module, "ignored_frame.bg", "Color preferences for a ignored frame",
  1759. "Color preferences for a ignored frame", &prefs.gui_ignored_bg);
  1760. prefs_register_color_preference(gui_color_module, "stream.client.fg", "TCP stream window color preference",
  1761. "TCP stream window color preference", &prefs.st_client_fg);
  1762. prefs_register_color_preference(gui_color_module, "stream.client.bg", "TCP stream window color preference",
  1763. "TCP stream window color preference", &prefs.st_client_bg);
  1764. prefs_register_color_preference(gui_color_module, "stream.server.fg", "TCP stream window color preference",
  1765. "TCP stream window color preference", &prefs.st_server_fg);
  1766. prefs_register_color_preference(gui_color_module, "stream.server.bg", "TCP stream window color preference",
  1767. "TCP stream window color preference", &prefs.st_server_bg);
  1768. custom_cbs.free_cb = colorized_frame_free_cb;
  1769. custom_cbs.reset_cb = colorized_frame_reset_cb;
  1770. custom_cbs.set_cb = colorized_frame_set_cb;
  1771. custom_cbs.type_name_cb = colorized_frame_type_name_cb;
  1772. custom_cbs.type_description_cb = colorized_frame_type_description_cb;
  1773. custom_cbs.is_default_cb = colorized_frame_is_default_cb;
  1774. custom_cbs.to_str_cb = colorized_frame_to_str_cb;
  1775. prefs_register_string_custom_preference(gui_column_module, "colorized_frame.fg", "Colorized Foreground",
  1776. "Filter Colorized Foreground", &custom_cbs, (const char **)&prefs.gui_colorized_fg);
  1777. custom_cbs.free_cb = colorized_frame_free_cb;
  1778. custom_cbs.reset_cb = colorized_frame_reset_cb;
  1779. custom_cbs.set_cb = colorized_frame_set_cb;
  1780. custom_cbs.type_name_cb = colorized_frame_type_name_cb;
  1781. custom_cbs.type_description_cb = colorized_frame_type_description_cb;
  1782. custom_cbs.is_default_cb = colorized_frame_is_default_cb;
  1783. custom_cbs.to_str_cb = colorized_frame_to_str_cb;
  1784. prefs_register_string_custom_preference(gui_column_module, "colorized_frame.bg", "Colorized Background",
  1785. "Filter Colorized Background", &custom_cbs, (const char **)&prefs.gui_colorized_bg);
  1786. prefs_register_color_preference(gui_color_module, "color_filter_bg.valid", "Valid color filter background",
  1787. "Valid color filter background", &prefs.gui_text_valid);
  1788. prefs_register_color_preference(gui_color_module, "color_filter_bg.invalid", "Invalid color filter background",
  1789. "Invalid color filter background", &prefs.gui_text_invalid);
  1790. prefs_register_color_preference(gui_color_module, "color_filter_bg.deprecated", "Deprecated color filter background",
  1791. "Deprecated color filter background", &prefs.gui_text_deprecated);
  1792. prefs_register_enum_preference(gui_module, "fileopen.style",
  1793. "Where to start the File Open dialog box",
  1794. "Where to start the File Open dialog box",
  1795. &prefs.gui_fileopen_style, gui_fileopen_style, FALSE);
  1796. prefs_register_uint_preference(gui_module, "recent_files_count.max",
  1797. "The max. number of items in the open recent files list",
  1798. "The max. number of items in the open recent files list",
  1799. 10,
  1800. &prefs.gui_recent_files_count_max);
  1801. prefs_register_uint_preference(gui_module, "recent_display_filter_entries.max",
  1802. "The max. number of entries in the display filter list",
  1803. "The max. number of entries in the display filter list",
  1804. 10,
  1805. &prefs.gui_recent_df_entries_max);
  1806. prefs_register_directory_preference(gui_module, "fileopen.dir", "Start Directory",
  1807. "Directory to start in when opening File Open dialog.", (const char **)&prefs.gui_fileopen_dir);
  1808. prefs_register_obsolete_preference(gui_module, "fileopen.remembered_dir");
  1809. prefs_register_uint_preference(gui_module, "fileopen.preview",
  1810. "The preview timeout in the File Open dialog",
  1811. "The preview timeout in the File Open dialog",
  1812. 10,
  1813. &prefs.gui_fileopen_preview);
  1814. prefs_register_bool_preference(gui_module, "ask_unsaved",
  1815. "Ask to save unsaved capture files",
  1816. "Ask to save unsaved capture files?",
  1817. &prefs.gui_ask_unsaved);
  1818. prefs_register_bool_preference(gui_module, "find_wrap",
  1819. "Wrap to beginning/end of file during search",
  1820. "Wrap to beginning/end of file during search?",
  1821. &prefs.gui_find_wrap);
  1822. prefs_register_bool_preference(gui_module, "use_pref_save",
  1823. "Settings dialogs use a save button",
  1824. "Settings dialogs use a save button?",
  1825. &prefs.gui_use_pref_save);
  1826. prefs_register_bool_preference(gui_module, "geometry.save.position",
  1827. "Save window position at exit",
  1828. "Save window position at exit?",
  1829. &prefs.gui_geometry_save_position);
  1830. prefs_register_bool_preference(gui_module, "geometry.save.size",
  1831. "Save window size at exit",
  1832. "Save window size at exit?",
  1833. &prefs.gui_geometry_save_size);
  1834. prefs_register_bool_preference(gui_module, "geometry.save.maximized",
  1835. "Save window maximized state at exit",
  1836. "Save window maximized state at exit?",
  1837. &prefs.gui_geometry_save_maximized);
  1838. prefs_register_bool_preference(gui_module, "macosx_style",
  1839. "Use Mac OS X style",
  1840. "Use Mac OS X style (Mac OS X with native GTK only)?",
  1841. &prefs.gui_macosx_style);
  1842. prefs_register_obsolete_preference(gui_module, "geometry.main.x");
  1843. prefs_register_obsolete_preference(gui_module, "geometry.main.y");
  1844. prefs_register_obsolete_preference(gui_module, "geometry.main.width");
  1845. prefs_register_obsolete_preference(gui_module, "geometry.main.height");
  1846. prefs_register_obsolete_preference(gui_module, "toolbar_main_show");
  1847. prefs_register_enum_preference(gui_module, "toolbar_main_style",
  1848. "Main Toolbar style",
  1849. "Main Toolbar style",
  1850. &prefs.gui_toolbar_main_style, gui_toolbar_style, FALSE);
  1851. prefs_register_enum_preference(gui_module, "toolbar_filter_style",
  1852. "Filter Toolbar style",
  1853. "Filter Toolbar style",
  1854. &prefs.gui_toolbar_filter_style, gui_toolbar_style, FALSE);
  1855. prefs_register_string_preference(gui_module, "webbrowser", "The path to the webbrowser",
  1856. "The path to the webbrowser (Ex: mozilla)", (const char **)&prefs.gui_webbrowser);
  1857. prefs_register_bool_preference(gui_module, "update.enabled",
  1858. "Check for updates",
  1859. "Check for updates (Windows only)",
  1860. &prefs.gui_update_enabled);
  1861. prefs_register_enum_preference(gui_module, "update.channel",
  1862. "Update channel",
  1863. "The type of update to fetch. You should probably leave this set to UPDATE_CHANNEL_STABLE.",
  1864. (gint*)(void*)(&prefs.gui_update_channel), gui_update_channel, FALSE);
  1865. prefs_register_uint_preference(gui_module, "update.interval",
  1866. "How often to check for software updates",
  1867. "How often to check for software updates in seconds",
  1868. 10,
  1869. &prefs.gui_update_interval);
  1870. prefs_register_string_preference(gui_module, "window_title", "Custom window title",
  1871. "Custom window title. (Appended to existing titles.)", (const char **)&prefs.gui_window_title);
  1872. prefs_register_string_preference(gui_module, "start_title", "Custom start page title",
  1873. "Custom start page title", (const char**)(&prefs.gui_start_title));
  1874. prefs_register_enum_preference(gui_module, "version_placement",
  1875. "Show version in the start page and/or main screen's title bar",
  1876. "Show version in the start page and/or main screen's title bar",
  1877. (gint*)(void*)(&prefs.gui_version_placement), gui_version_placement_type, FALSE);
  1878. prefs_register_bool_preference(gui_module, "auto_scroll_on_expand",
  1879. "Automatically scroll the recently expanded item",
  1880. "Automatically scroll the recently expanded item",
  1881. &prefs.gui_auto_scroll_on_expand);
  1882. prefs_register_uint_preference(gui_module, "auto_scroll_percentage",
  1883. "The percentage down the view the recently expanded item should be scrolled",
  1884. "The percentage down the view the recently expanded item should be scrolled",
  1885. 10,
  1886. &prefs.gui_auto_scroll_percentage);
  1887. /* User Interface : Layout */
  1888. gui_layout_module = prefs_register_subtree(gui_module, "Layout", "Layout", gui_layout_callback);
  1889. prefs_register_uint_preference(gui_layout_module, "layout_type",
  1890. "Layout type",
  1891. "Layout type (1-6)",
  1892. 10,
  1893. (guint*)(void*)(&prefs.gui_layout_type));
  1894. prefs_register_enum_preference(gui_layout_module, "layout_content_1",
  1895. "Layout content of the pane 1",
  1896. "Layout content of the pane 1",
  1897. (gint*)(void*)(&prefs.gui_layout_content_1), gui_layout_content, FALSE);
  1898. prefs_register_enum_preference(gui_layout_module, "layout_content_2",
  1899. "Layout content of the pane 2",
  1900. "Layout content of the pane 2",
  1901. (gint*)(void*)(&prefs.gui_layout_content_2), gui_layout_content, FALSE);
  1902. prefs_register_enum_preference(gui_layout_module, "layout_content_3",
  1903. "Layout content of the pane 3",
  1904. "Layout content of the pane 3",
  1905. (gint*)(void*)(&prefs.gui_layout_content_3), gui_layout_content, FALSE);
  1906. /* Console
  1907. * These are preferences that can be read/written using the
  1908. * preference module API. These preferences still use their own
  1909. * configuration screens for access, but this cuts down on the
  1910. * preference "string compare list" in set_pref()
  1911. */
  1912. console_module = prefs_register_module(NULL, "console", "Console",
  1913. "CONSOLE", NULL, FALSE);
  1914. custom_cbs.free_cb = custom_pref_no_cb;
  1915. custom_cbs.reset_cb = console_log_level_reset_cb;
  1916. custom_cbs.set_cb = console_log_level_set_cb;
  1917. custom_cbs.type_name_cb = console_log_level_type_name_cb;
  1918. custom_cbs.type_description_cb = console_log_level_type_description_cb;
  1919. custom_cbs.is_default_cb = console_log_level_is_default_cb;
  1920. custom_cbs.to_str_cb = console_log_level_to_str_cb;
  1921. prefs_register_uint_custom_preference(console_module, "log.level", "logging level",
  1922. "A bitmask of glib log levels", &custom_cbs, &prefs.console_log_level);
  1923. /* Capture
  1924. * These are preferences that can be read/written using the
  1925. * preference module API. These preferences still use their own
  1926. * configuration screens for access, but this cuts down on the
  1927. * preference "string compare list" in set_pref()
  1928. */
  1929. capture_module = prefs_register_module(NULL, "capture", "Capture",
  1930. "CAPTURE", NULL, FALSE);
  1931. prefs_register_string_preference(capture_module, "device", "Default capture device",
  1932. "Default capture device", (const char **)&prefs.capture_device);
  1933. prefs_register_string_preference(capture_module, "devices_linktypes", "Interface link-layer header type",
  1934. "Interface link-layer header types (Ex: en0(1),en1(143),...)",
  1935. (const char **)&prefs.capture_devices_linktypes);
  1936. prefs_register_string_preference(capture_module, "devices_descr", "Interface descriptions",
  1937. "Interface descriptions (Ex: eth0(eth0 descr),eth1(eth1 descr),...)",
  1938. (const char **)&prefs.capture_devices_descr);
  1939. prefs_register_string_preference(capture_module, "devices_hide", "Hide interface",
  1940. "Hide interface? (Ex: eth0,eth3,...)", (const char **)&prefs.capture_devices_hide);
  1941. prefs_register_string_preference(capture_module, "devices_monitor_mode", "Capture in monitor mode",
  1942. "By default, capture in monitor mode on interface? (Ex: eth0,eth3,...)",
  1943. (const char **)&prefs.capture_devices_monitor_mode);
  1944. #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
  1945. prefs_register_string_preference(capture_module, "devices_buffersize", "Interface buffer size",
  1946. "Interface buffer size (Ex: en0(1),en1(143),...)",
  1947. ((const char **)&prefs.capture_devices_buffersize));
  1948. #endif
  1949. prefs_register_string_preference(capture_module, "devices_snaplen", "Interface snap length",
  1950. "Interface snap length (Ex: en0(65535),en1(1430),...)",
  1951. (const char **)&prefs.capture_devices_snaplen);
  1952. prefs_register_string_preference(capture_module, "devices_pmode", "Interface promiscuous mode",
  1953. "Interface promiscuous mode (Ex: en0(0),en1(1),...)",
  1954. (const char **)&prefs.capture_devices_pmode);
  1955. prefs_register_bool_preference(capture_module, "prom_mode", "Capture in promiscuous mode",
  1956. "Capture in promiscuous mode?", &prefs.capture_prom_mode);
  1957. prefs_register_bool_preference(capture_module, "pcap_ng", "Capture in Pcap-NG format",
  1958. "Capture in Pcap-NG format?", &prefs.capture_pcap_ng);
  1959. prefs_register_bool_preference(capture_module, "real_time_update", "Update packet list in real time during capture",
  1960. "Update packet list in real time during capture?", &prefs.capture_real_time);
  1961. prefs_register_bool_preference(capture_module, "auto_scroll", "Scroll packet list during capture",
  1962. "Scroll packet list during capture?", &prefs.capture_auto_scroll);
  1963. prefs_register_bool_preference(capture_module, "show_info", "Show capture info dialog while capturing",
  1964. "Show capture info dialog while capturing?", &prefs.capture_show_info);
  1965. prefs_register_obsolete_preference(capture_module, "syntax_check_filter");
  1966. custom_cbs.free_cb = capture_column_free_cb;
  1967. custom_cbs.reset_cb = capture_column_reset_cb;
  1968. custom_cbs.set_cb = capture_column_set_cb;
  1969. custom_cbs.type_name_cb = capture_column_type_name_cb;
  1970. custom_cbs.type_description_cb = capture_column_type_description_cb;
  1971. custom_cbs.is_default_cb = capture_column_is_default_cb;
  1972. custom_cbs.to_str_cb = capture_column_to_str_cb;
  1973. prefs_register_list_custom_preference(capture_module, "columns", "Capture options dialog column list",
  1974. "List of columns to be displayed", &custom_cbs, capture_column_init_cb, &prefs.capture_columns);
  1975. /* Name Resolution */
  1976. nameres_module = prefs_register_module(NULL, "nameres", "Name Resolution",
  1977. "Name Resolution", NULL, TRUE);
  1978. addr_resolve_pref_init(nameres_module);
  1979. oid_pref_init(nameres_module);
  1980. #ifdef HAVE_GEOIP
  1981. geoip_db_pref_init(nameres_module);
  1982. #endif
  1983. /* Printing */
  1984. printing = prefs_register_module(NULL, "print", "Printing",
  1985. "Printing", NULL, TRUE);
  1986. prefs_register_enum_preference(printing, "format",
  1987. "Format", "Can be one of \"text\" or \"postscript\"",
  1988. &prefs.pr_format, print_format_vals, TRUE);
  1989. prefs_register_enum_preference(printing, "destination",
  1990. "Print to", "Can be one of \"command\" or \"file\"",
  1991. &prefs.pr_dest, print_dest_vals, TRUE);
  1992. #ifndef _WIN32
  1993. prefs_register_string_preference(printing, "command", "Command",
  1994. "Output gets piped to this command when the destination is set to \"command\"", (const char**)(&prefs.pr_cmd));
  1995. #endif
  1996. prefs_register_filename_preference(printing, "file", "File",
  1997. "This is the file that gets written to when the destination is set to \"file\"", (const char**)(&prefs.pr_file));
  1998. /* Statistics */
  1999. stats_module = prefs_register_module(NULL, "statistics", "Statistics",
  2000. "Statistics", &stats_callback, TRUE);
  2001. prefs_register_uint_preference(stats_module, "update_interval",
  2002. "Tap update interval in ms",
  2003. "Determines time between tap updates",
  2004. 10,
  2005. &prefs.tap_update_interval);
  2006. #ifdef HAVE_LIBPORTAUDIO
  2007. prefs_register_uint_preference(stats_module, "rtp_player_max_visible",
  2008. "Max visible channels in RTP Player",
  2009. "Determines maximum height of RTP Player window",
  2010. 10,
  2011. &prefs.rtp_player_max_visible);
  2012. #endif
  2013. /* Protocols */
  2014. protocols_module = prefs_register_module(NULL, "protocols", "Protocols",
  2015. "Protocols", NULL, TRUE);
  2016. prefs_register_bool_preference(protocols_module, "display_hidden_proto_items",
  2017. "Display hidden protocol items",
  2018. "Display all hidden protocol items in the packet list.",
  2019. &prefs.display_hidden_proto_items);
  2020. /* Obsolete preferences
  2021. * These "modules" were reorganized/renamed to correspond to their GUI
  2022. * configuration screen within the preferences dialog
  2023. */
  2024. /* taps is now part of the stats module */
  2025. prefs_register_module(NULL, "taps", "TAPS", "TAPS", NULL, FALSE);
  2026. /* packet_list is now part of the protocol (parent) module */
  2027. prefs_register_module(NULL, "packet_list", "PACKET_LIST", "PACKET_LIST", NULL, FALSE);
  2028. /* stream is now part of the gui module */
  2029. prefs_register_module(NULL, "stream", "STREAM", "STREAM", NULL, FALSE);
  2030. }
  2031. /* Parse through a list of comma-separated, possibly quoted strings.
  2032. Return a list of the string data. */
  2033. GList *
  2034. prefs_get_string_list(const gchar *str)
  2035. {
  2036. enum { PRE_STRING, IN_QUOT, NOT_IN_QUOT };
  2037. gint state = PRE_STRING, i = 0, j = 0;
  2038. gboolean backslash = FALSE;
  2039. guchar cur_c;
  2040. gchar *slstr = NULL;
  2041. GList *sl = NULL;
  2042. /* Allocate a buffer for the first string. */
  2043. slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
  2044. j = 0;
  2045. for (;;) {
  2046. cur_c = str[i];
  2047. if (cur_c == '\0') {
  2048. /* It's the end of the input, so it's the end of the string we
  2049. were working on, and there's no more input. */
  2050. if (state == IN_QUOT || backslash) {
  2051. /* We were in the middle of a quoted string or backslash escape,
  2052. and ran out of characters; that's an error. */
  2053. g_free(slstr);
  2054. prefs_clear_string_list(sl);
  2055. return NULL;
  2056. }
  2057. slstr[j] = '\0';
  2058. sl = g_list_append(sl, slstr);
  2059. break;
  2060. }
  2061. if (cur_c == '"' && ! backslash) {
  2062. switch (state) {
  2063. case PRE_STRING:
  2064. /* We hadn't yet started processing a string; this starts the
  2065. string, and we're now quoting. */
  2066. state = IN_QUOT;
  2067. break;
  2068. case IN_QUOT:
  2069. /* We're in the middle of a quoted string, and we saw a quotation
  2070. mark; we're no longer quoting. */
  2071. state = NOT_IN_QUOT;
  2072. break;
  2073. case NOT_IN_QUOT:
  2074. /* We're working on a string, but haven't seen a quote; we're
  2075. now quoting. */
  2076. state = IN_QUOT;
  2077. break;
  2078. default:
  2079. break;
  2080. }
  2081. } else if (cur_c == '\\' && ! backslash) {
  2082. /* We saw a backslash, and the previous character wasn't a
  2083. backslash; escape the next character.
  2084. This also means we've started a new string. */
  2085. backslash = TRUE;
  2086. if (state == PRE_STRING)
  2087. state = NOT_IN_QUOT;
  2088. } else if (cur_c == ',' && state != IN_QUOT && ! backslash) {
  2089. /* We saw a comma, and we're not in the middle of a quoted string
  2090. and it wasn't preceded by a backslash; it's the end of
  2091. the string we were working on... */
  2092. slstr[j] = '\0';
  2093. sl = g_list_append(sl, slstr);
  2094. /* ...and the beginning of a new string. */
  2095. state = PRE_STRING;
  2096. slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
  2097. j = 0;
  2098. } else if (!isspace(cur_c) || state != PRE_STRING) {
  2099. /* Either this isn't a white-space character, or we've started a
  2100. string (i.e., already seen a non-white-space character for that
  2101. string and put it into the string).
  2102. The character is to be put into the string; do so if there's
  2103. room. */
  2104. if (j < COL_MAX_LEN) {
  2105. slstr[j] = cur_c;
  2106. j++;
  2107. }
  2108. /* If it was backslash-escaped, we're done with the backslash escape. */
  2109. backslash = FALSE;
  2110. }
  2111. i++;
  2112. }
  2113. return(sl);
  2114. }
  2115. static char *
  2116. join_string_list(GList *sl)
  2117. {
  2118. GString *joined_str = g_string_new("");
  2119. GList *cur, *first;
  2120. gchar *str;
  2121. gchar *quoted_str;
  2122. guint item_count = 0;
  2123. cur = first = g_list_first(sl);
  2124. while (cur) {
  2125. item_count++;
  2126. str = (gchar *)cur->data;
  2127. if (cur != first)
  2128. g_string_append_c(joined_str, ',');
  2129. if (item_count % 2) {
  2130. /* Wrap the line. */
  2131. g_string_append(joined_str, "\n\t");
  2132. } else
  2133. g_string_append_c(joined_str, ' ');
  2134. quoted_str = g_strescape(str, "");
  2135. g_string_append_printf(joined_str, "\"%s\"", quoted_str);
  2136. g_free(quoted_str);
  2137. cur = cur->next;
  2138. }
  2139. return g_string_free(joined_str, FALSE);
  2140. }
  2141. void
  2142. prefs_clear_string_list(GList *sl)
  2143. {
  2144. GList *l = sl;
  2145. while (l) {
  2146. g_free(l->data);
  2147. l = g_list_remove_link(l, l);
  2148. }
  2149. }
  2150. /*
  2151. * Takes a string, a pointer to an array of "enum_val_t"s, and a default gint
  2152. * value.
  2153. * The array must be terminated by an entry with a null "name" string.
  2154. *
  2155. * If the string matches a "name" string in an entry, the value from that
  2156. * entry is returned.
  2157. *
  2158. * Otherwise, if a string matches a "desctiption" string in an entry, the
  2159. * value from that entry is returned; we do that for backwards compatibility,
  2160. * as we used to have only a "name" string that was used both for command-line
  2161. * and configuration-file values and in the GUI (which meant either that
  2162. * the GUI had what might be somewhat cryptic values to select from or that
  2163. * the "-o" flag took long strings, often with spaces in them).
  2164. *
  2165. * Otherwise, the default value that was passed as the third argument is
  2166. * returned.
  2167. */
  2168. static gint
  2169. find_val_for_string(const char *needle, const enum_val_t *haystack,
  2170. gint default_value)
  2171. {
  2172. int i;
  2173. for (i = 0; haystack[i].name != NULL; i++) {
  2174. if (g_ascii_strcasecmp(needle, haystack[i].name) == 0) {
  2175. return haystack[i].value;
  2176. }
  2177. }
  2178. for (i = 0; haystack[i].name != NULL; i++) {
  2179. if (g_ascii_strcasecmp(needle, haystack[i].description) == 0) {
  2180. return haystack[i].value;
  2181. }
  2182. }
  2183. return default_value;
  2184. }
  2185. /* Preferences file format:
  2186. * - Configuration directives start at the beginning of the line, and
  2187. * are terminated with a colon.
  2188. * - Directives can be continued on the next line by preceding them with
  2189. * whitespace.
  2190. *
  2191. * Example:
  2192. # This is a comment line
  2193. print.command: lpr
  2194. print.file: /a/very/long/path/
  2195. to/wireshark-out.ps
  2196. *
  2197. */
  2198. #define DEF_NUM_COLS 7
  2199. /*
  2200. * Parse a column format, filling in the relevant fields of a fmt_data.
  2201. */
  2202. static gboolean
  2203. parse_column_format(fmt_data *cfmt, const char *fmt)
  2204. {
  2205. const gchar *cust_format = col_format_to_string(COL_CUSTOM);
  2206. size_t cust_format_len = strlen(cust_format);
  2207. gchar **cust_format_info;
  2208. char *p;
  2209. int col_fmt;
  2210. gchar *col_custom_field;
  2211. long col_custom_occurrence;
  2212. gboolean col_resolved;
  2213. /*
  2214. * Is this a custom column?
  2215. */
  2216. if ((strlen(fmt) > cust_format_len) && (fmt[cust_format_len] == ':') &&
  2217. strncmp(fmt, cust_format, cust_format_len) == 0) {
  2218. /* Yes. */
  2219. col_fmt = COL_CUSTOM;
  2220. cust_format_info = g_strsplit(&fmt[cust_format_len+1],":",3); /* add 1 for ':' */
  2221. col_custom_field = g_strdup(cust_format_info[0]);
  2222. if (col_custom_field && cust_format_info[1]) {
  2223. col_custom_occurrence = strtol(cust_format_info[1], &p, 10);
  2224. if (p == cust_format_info[1] || *p != '\0') {
  2225. /* Not a valid number. */
  2226. g_free(col_custom_field);
  2227. g_strfreev(cust_format_info);
  2228. return FALSE;
  2229. }
  2230. } else {
  2231. col_custom_occurrence = 0;
  2232. }
  2233. if (col_custom_field && cust_format_info[1] && cust_format_info[2]) {
  2234. col_resolved = (cust_format_info[2][0] == 'U') ? FALSE : TRUE;
  2235. } else {
  2236. col_resolved = TRUE;
  2237. }
  2238. g_strfreev(cust_format_info);
  2239. } else {
  2240. col_fmt = get_column_format_from_str(fmt);
  2241. if (col_fmt == -1)
  2242. return FALSE;
  2243. col_custom_field = NULL;
  2244. col_custom_occurrence = 0;
  2245. col_resolved = TRUE;
  2246. }
  2247. cfmt->fmt = col_fmt;
  2248. cfmt->custom_field = col_custom_field;
  2249. cfmt->custom_occurrence = (int)col_custom_occurrence;
  2250. cfmt->resolved = col_resolved;
  2251. return TRUE;
  2252. }
  2253. /* Initialize non-dissector preferences to wired-in default values.
  2254. * (The dissector preferences are assumed to be set to those values
  2255. * by the dissectors.)
  2256. * They may be overridden by the global preferences file or the
  2257. * user's preferences file.
  2258. */
  2259. static void
  2260. init_prefs(void)
  2261. {
  2262. if (prefs_initialized)
  2263. return;
  2264. uat_load_all();
  2265. prefs_register_modules();
  2266. filter_expression_init(TRUE);
  2267. prefs_initialized = TRUE;
  2268. }
  2269. /* Initialize non-dissector preferences used by the "register preference" API
  2270. * to default values so the default values can be used when registered
  2271. */
  2272. static void
  2273. pre_init_prefs(void)
  2274. {
  2275. int i;
  2276. gchar *col_name;
  2277. fmt_data *cfmt;
  2278. static const gchar *col_fmt[DEF_NUM_COLS*2] = {
  2279. "No.", "%m", "Time", "%t",
  2280. "Source", "%s", "Destination", "%d",
  2281. "Protocol", "%p", "Length", "%L",
  2282. "Info", "%i"};
  2283. if (prefs_pre_initialized)
  2284. return;
  2285. prefs.pr_format = PR_FMT_TEXT;
  2286. prefs.pr_dest = PR_DEST_CMD;
  2287. prefs.pr_file = "wireshark.out";
  2288. prefs.pr_cmd = "lpr";
  2289. prefs.gui_altern_colors = FALSE;
  2290. prefs.gui_expert_composite_eyecandy = FALSE;
  2291. prefs.gui_ptree_line_style = 0;
  2292. prefs.gui_ptree_expander_style = 1;
  2293. prefs.gui_hex_dump_highlight_style = 1;
  2294. prefs.filter_toolbar_show_in_statusbar = FALSE;
  2295. prefs.gui_toolbar_main_style = TB_STYLE_ICONS;
  2296. prefs.gui_toolbar_filter_style = TB_STYLE_TEXT;
  2297. /* These string prefs will be strduped shortly, so we can safely cast away
  2298. * their constness in these assignments */
  2299. #ifdef _WIN32
  2300. prefs.gui_gtk2_font_name = (char *) "Lucida Console 10";
  2301. #else
  2302. prefs.gui_gtk2_font_name = (char *) "Monospace 10";
  2303. #endif
  2304. /* We try to find the best font in the Qt code */
  2305. prefs.gui_qt_font_name = (char *) "";
  2306. prefs.gui_marked_fg.pixel = 65535;
  2307. prefs.gui_marked_fg.red = 65535;
  2308. prefs.gui_marked_fg.green = 65535;
  2309. prefs.gui_marked_fg.blue = 65535;
  2310. prefs.gui_marked_bg.pixel = 0;
  2311. prefs.gui_marked_bg.red = 0;
  2312. prefs.gui_marked_bg.green = 8224;
  2313. prefs.gui_marked_bg.blue = 10794;
  2314. prefs.gui_ignored_fg.pixel = 32767;
  2315. prefs.gui_ignored_fg.red = 32767;
  2316. prefs.gui_ignored_fg.green = 32767;
  2317. prefs.gui_ignored_fg.blue = 32767;
  2318. prefs.gui_ignored_bg.pixel = 65535;
  2319. prefs.gui_ignored_bg.red = 65535;
  2320. prefs.gui_ignored_bg.green = 65535;
  2321. prefs.gui_ignored_bg.blue = 65535;
  2322. prefs.gui_colorized_fg = "000000,000000,000000,000000,000000,000000,000000,000000,000000,000000";
  2323. prefs.gui_colorized_bg = "ffc0c0,ffc0ff,e0c0e0,c0c0ff,c0e0e0,c0ffff,c0ffc0,ffffc0,e0e0c0,e0e0e0";
  2324. prefs.st_client_fg.pixel = 0;
  2325. prefs.st_client_fg.red = 32767;
  2326. prefs.st_client_fg.green = 0;
  2327. prefs.st_client_fg.blue = 0;
  2328. prefs.st_client_bg.pixel = 0;
  2329. prefs.st_client_bg.red = 64507;
  2330. prefs.st_client_bg.green = 60909;
  2331. prefs.st_client_bg.blue = 60909;
  2332. prefs.st_server_fg.pixel = 0;
  2333. prefs.st_server_fg.red = 0;
  2334. prefs.st_server_fg.green = 0;
  2335. prefs.st_server_fg.blue = 32767;
  2336. prefs.st_server_bg.pixel = 0;
  2337. prefs.st_server_bg.red = 60909;
  2338. prefs.st_server_bg.green = 60909;
  2339. prefs.st_server_bg.blue = 64507;
  2340. prefs.gui_text_valid.pixel = 0; /* light green */
  2341. prefs.gui_text_valid.red = 0xAFFF;
  2342. prefs.gui_text_valid.green = 0xFFFF;
  2343. prefs.gui_text_valid.blue = 0xAFFF;
  2344. prefs.gui_text_invalid.pixel = 0; /* light red */
  2345. prefs.gui_text_invalid.red = 0xFFFF;
  2346. prefs.gui_text_invalid.green = 0xAFFF;
  2347. prefs.gui_text_invalid.blue = 0xAFFF;
  2348. prefs.gui_text_deprecated.pixel = 0; /* light yellow */
  2349. prefs.gui_text_deprecated.red = 0xFFFF;
  2350. prefs.gui_text_deprecated.green = 0xFFFF;
  2351. prefs.gui_text_deprecated.blue = 0xAFFF;
  2352. prefs.gui_geometry_save_position = TRUE;
  2353. prefs.gui_geometry_save_size = TRUE;
  2354. prefs.gui_geometry_save_maximized= TRUE;
  2355. prefs.gui_macosx_style = TRUE;
  2356. prefs.gui_console_open = console_open_never;
  2357. prefs.gui_fileopen_style = FO_STYLE_LAST_OPENED;
  2358. prefs.gui_recent_df_entries_max = 10;
  2359. prefs.gui_recent_files_count_max = 10;
  2360. prefs.gui_fileopen_dir = (char *) get_persdatafile_dir();
  2361. prefs.gui_fileopen_preview = 3;
  2362. prefs.gui_ask_unsaved = TRUE;
  2363. prefs.gui_find_wrap = TRUE;
  2364. prefs.gui_use_pref_save = FALSE;
  2365. prefs.gui_update_enabled = TRUE;
  2366. prefs.gui_update_channel = UPDATE_CHANNEL_STABLE;
  2367. prefs.gui_update_interval = 60*60*24; /* Seconds */
  2368. prefs.gui_webbrowser = (char *) HTML_VIEWER " %s";
  2369. prefs.gui_window_title = (char *) "";
  2370. prefs.gui_start_title = "The World's Most Popular Network Protocol Analyzer";
  2371. prefs.gui_version_placement = version_both;
  2372. prefs.gui_auto_scroll_on_expand = FALSE;
  2373. prefs.gui_auto_scroll_percentage = 0;
  2374. prefs.gui_layout_type = layout_type_5;
  2375. prefs.gui_layout_content_1 = layout_pane_content_plist;
  2376. prefs.gui_layout_content_2 = layout_pane_content_pdetails;
  2377. prefs.gui_layout_content_3 = layout_pane_content_pbytes;
  2378. prefs.col_list = NULL;
  2379. for (i = 0; i < DEF_NUM_COLS; i++) {
  2380. cfmt = g_new(fmt_data,1);
  2381. cfmt->title = g_strdup(col_fmt[i * 2]);
  2382. parse_column_format(cfmt, col_fmt[(i * 2) + 1]);
  2383. cfmt->visible = TRUE;
  2384. cfmt->resolved = TRUE;
  2385. cfmt->custom_field = NULL;
  2386. cfmt->custom_occurrence = 0;
  2387. prefs.col_list = g_list_append(prefs.col_list, cfmt);
  2388. }
  2389. prefs.num_cols = DEF_NUM_COLS;
  2390. /* set the default values for the capture dialog box */
  2391. prefs.capture_prom_mode = TRUE;
  2392. #ifdef PCAP_NG_DEFAULT
  2393. prefs.capture_pcap_ng = TRUE;
  2394. #else
  2395. prefs.capture_pcap_ng = FALSE;
  2396. #endif
  2397. prefs.capture_real_time = TRUE;
  2398. prefs.capture_auto_scroll = TRUE;
  2399. prefs.capture_show_info = FALSE;
  2400. prefs.capture_columns = NULL;
  2401. for (i = 0; i < num_capture_cols; i++) {
  2402. col_name = g_strdup(capture_cols[i]);
  2403. prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
  2404. }
  2405. prefs.console_log_level =
  2406. G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR;
  2407. /* set the default values for the tap/statistics dialog box */
  2408. prefs.tap_update_interval = TAP_UPDATE_DEFAULT_INTERVAL;
  2409. prefs.rtp_player_max_visible = RTP_PLAYER_DEFAULT_VISIBLE;
  2410. prefs.display_hidden_proto_items = FALSE;
  2411. prefs_pre_initialized = TRUE;
  2412. }
  2413. /*
  2414. * Reset a single dissector preference.
  2415. */
  2416. static void
  2417. reset_pref(pref_t *pref)
  2418. {
  2419. if (!pref) return;
  2420. switch (pref->type) {
  2421. case PREF_UINT:
  2422. *pref->varp.uint = pref->default_val.uint;
  2423. break;
  2424. case PREF_BOOL:
  2425. *pref->varp.boolp = pref->default_val.boolval;
  2426. break;
  2427. case PREF_ENUM:
  2428. /*
  2429. * For now, we save the "description" value, so that if we
  2430. * save the preferences older versions of Wireshark can at
  2431. * least read preferences that they supported; we support
  2432. * either the short name or the description when reading
  2433. * the preferences file or a "-o" option.
  2434. */
  2435. *pref->varp.enump = pref->default_val.enumval;
  2436. break;
  2437. case PREF_STRING:
  2438. case PREF_FILENAME:
  2439. case PREF_DIRNAME:
  2440. g_free((void *)*pref->varp.string);
  2441. *pref->varp.string = g_strdup(pref->default_val.string);
  2442. break;
  2443. case PREF_RANGE:
  2444. g_free(*pref->varp.range);
  2445. *pref->varp.range = range_copy(pref->default_val.range);
  2446. break;
  2447. case PREF_STATIC_TEXT:
  2448. case PREF_UAT:
  2449. /* Nothing to do */
  2450. break;
  2451. case PREF_COLOR:
  2452. *pref->varp.colorp = pref->default_val.color;
  2453. break;
  2454. case PREF_CUSTOM:
  2455. pref->custom_cbs.reset_cb(pref);
  2456. break;
  2457. case PREF_OBSOLETE:
  2458. /*
  2459. * This preference is no longer supported; it's not a
  2460. * real preference, so we don't reset it (i.e., we
  2461. * treat it as if it weren't found in the list of
  2462. * preferences, and we weren't called in the first place).
  2463. */
  2464. break;
  2465. }
  2466. }
  2467. static void
  2468. reset_pref_cb(gpointer data, gpointer user_data _U_)
  2469. {
  2470. pref_t *pref = (pref_t *) data;
  2471. reset_pref(pref);
  2472. }
  2473. typedef struct {
  2474. module_t *module;
  2475. } reset_pref_arg_t;
  2476. /*
  2477. * Reset all preferences for a module.
  2478. */
  2479. static gboolean
  2480. reset_module_prefs(void *value, void *data _U_)
  2481. {
  2482. reset_pref_arg_t arg;
  2483. arg.module = (module_t *)value;
  2484. g_list_foreach(arg.module->prefs, reset_pref_cb, &arg);
  2485. return FALSE;
  2486. }
  2487. /* Reset preferences */
  2488. void
  2489. prefs_reset(void)
  2490. {
  2491. prefs_initialized = FALSE;
  2492. g_free(prefs.saved_at_version);
  2493. /*
  2494. * Unload all UAT preferences.
  2495. */
  2496. uat_unload_all();
  2497. /*
  2498. * Unload any loaded MIBs.
  2499. */
  2500. oids_cleanup();
  2501. /*
  2502. * Reset the non-dissector preferences.
  2503. */
  2504. init_prefs();
  2505. /*
  2506. * Reset the non-UAT dissector preferences.
  2507. */
  2508. wmem_tree_foreach(prefs_modules, reset_module_prefs, NULL);
  2509. }
  2510. /* Read the preferences file, fill in "prefs", and return a pointer to it.
  2511. If we got an error (other than "it doesn't exist") trying to read
  2512. the global preferences file, stuff the errno into "*gpf_errno_return"
  2513. and a pointer to the path of the file into "*gpf_path_return", and
  2514. return NULL.
  2515. If we got an error (other than "it doesn't exist") trying to read
  2516. the user's preferences file, stuff the errno into "*pf_errno_return"
  2517. and a pointer to the path of the file into "*pf_path_return", and
  2518. return NULL. */
  2519. e_prefs *
  2520. read_prefs(int *gpf_errno_return, int *gpf_read_errno_return,
  2521. char **gpf_path_return, int *pf_errno_return,
  2522. int *pf_read_errno_return, char **pf_path_return)
  2523. {
  2524. int err;
  2525. char *pf_path;
  2526. FILE *pf;
  2527. /* clean up libsmi structures before reading prefs */
  2528. oids_cleanup();
  2529. init_prefs();
  2530. /*
  2531. * If we don't already have the pathname of the global preferences
  2532. * file, construct it. Then, in either case, try to open the file.
  2533. */
  2534. if (gpf_path == NULL) {
  2535. /*
  2536. * We don't have the path; try the new path first, and, if that
  2537. * file doesn't exist, try the old path.
  2538. */
  2539. gpf_path = get_datafile_path(PF_NAME);
  2540. if ((pf = ws_fopen(gpf_path, "r")) == NULL && errno == ENOENT) {
  2541. /*
  2542. * It doesn't exist by the new name; try the old name.
  2543. */
  2544. g_free(gpf_path);
  2545. gpf_path = get_datafile_path(OLD_GPF_NAME);
  2546. pf = ws_fopen(gpf_path, "r");
  2547. }
  2548. } else {
  2549. /*
  2550. * We have the path; try it.
  2551. */
  2552. pf = ws_fopen(gpf_path, "r");
  2553. }
  2554. /*
  2555. * If we were able to open the file, read it.
  2556. * XXX - if it failed for a reason other than "it doesn't exist",
  2557. * report the error.
  2558. */
  2559. *gpf_path_return = NULL;
  2560. if (pf != NULL) {
  2561. /*
  2562. * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
  2563. * seen.
  2564. */
  2565. mgcp_tcp_port_count = 0;
  2566. mgcp_udp_port_count = 0;
  2567. /* We succeeded in opening it; read it. */
  2568. err = read_prefs_file(gpf_path, pf, set_pref, NULL);
  2569. if (err != 0) {
  2570. /* We had an error reading the file; return the errno and the
  2571. pathname, so our caller can report the error. */
  2572. *gpf_errno_return = 0;
  2573. *gpf_read_errno_return = err;
  2574. *gpf_path_return = gpf_path;
  2575. }
  2576. fclose(pf);
  2577. } else {
  2578. /* We failed to open it. If we failed for some reason other than
  2579. "it doesn't exist", return the errno and the pathname, so our
  2580. caller can report the error. */
  2581. if (errno != ENOENT) {
  2582. *gpf_errno_return = errno;
  2583. *gpf_read_errno_return = 0;
  2584. *gpf_path_return = gpf_path;
  2585. }
  2586. }
  2587. /* Construct the pathname of the user's preferences file. */
  2588. pf_path = get_persconffile_path(PF_NAME, TRUE);
  2589. /* Read the user's preferences file, if it exists. */
  2590. *pf_path_return = NULL;
  2591. if ((pf = ws_fopen(pf_path, "r")) != NULL) {
  2592. /*
  2593. * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
  2594. * seen.
  2595. */
  2596. mgcp_tcp_port_count = 0;
  2597. mgcp_udp_port_count = 0;
  2598. /* We succeeded in opening it; read it. */
  2599. err = read_prefs_file(pf_path, pf, set_pref, NULL);
  2600. if (err != 0) {
  2601. /* We had an error reading the file; return the errno and the
  2602. pathname, so our caller can report the error. */
  2603. *pf_errno_return = 0;
  2604. *pf_read_errno_return = err;
  2605. *pf_path_return = pf_path;
  2606. } else
  2607. g_free(pf_path);
  2608. fclose(pf);
  2609. } else {
  2610. /* We failed to open it. If we failed for some reason other than
  2611. "it doesn't exist", return the errno and the pathname, so our
  2612. caller can report the error. */
  2613. if (errno != ENOENT) {
  2614. *pf_errno_return = errno;
  2615. *pf_read_errno_return = 0;
  2616. *pf_path_return = pf_path;
  2617. } else
  2618. g_free(pf_path);
  2619. }
  2620. /* load SMI modules if needed */
  2621. oids_init();
  2622. return &prefs;
  2623. }
  2624. /* read the preferences file (or similiar) and call the callback
  2625. * function to set each key/value pair found */
  2626. int
  2627. read_prefs_file(const char *pf_path, FILE *pf,
  2628. pref_set_pair_cb pref_set_pair_fct, void *private_data)
  2629. {
  2630. enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
  2631. int got_c, state = START;
  2632. GString *cur_val;
  2633. GString *cur_var;
  2634. gboolean got_val = FALSE;
  2635. gint fline = 1, pline = 1;
  2636. gchar hint[] = "(save preferences to remove this warning)";
  2637. cur_val = g_string_new("");
  2638. cur_var = g_string_new("");
  2639. /* Try to read in the profile name in the first line of the preferences file. */
  2640. got_c = getc(pf);
  2641. if (got_c) {
  2642. char firstl[100];
  2643. if (fgets(firstl, 100, pf) != NULL) {
  2644. if (strncmp((const char *)firstl, " Configuration file for ", 24) == 0) {
  2645. const gchar *ver = (gchar *)&firstl[24];
  2646. /* Eliminate the period and LF the end of the string */
  2647. prefs.saved_at_version = g_strndup(ver, strlen(ver) - 2);
  2648. }
  2649. }
  2650. }
  2651. rewind(pf);
  2652. while ((got_c = getc(pf)) != EOF) {
  2653. if (got_c == '\n') {
  2654. state = START;
  2655. fline++;
  2656. continue;
  2657. }
  2658. switch (state) {
  2659. case START:
  2660. if (isalnum(got_c)) {
  2661. if (cur_var->len > 0) {
  2662. if (got_val) {
  2663. if (cur_val->len > 0) {
  2664. if (cur_val->str[cur_val->len-1] == ',') {
  2665. /*
  2666. * If the pref has a trailing comma, eliminate it.
  2667. */
  2668. cur_val->str[cur_val->len-1] = '\0';
  2669. g_warning ("%s line %d: trailing comma in \"%s\" %s", pf_path, pline, cur_var->str, hint);
  2670. }
  2671. }
  2672. /* Call the routine to set the preference; it will parse
  2673. the value as appropriate. */
  2674. switch (pref_set_pair_fct(cur_var->str, cur_val->str, private_data, FALSE)) {
  2675. case PREFS_SET_OK:
  2676. break;
  2677. case PREFS_SET_SYNTAX_ERR:
  2678. g_warning ("Syntax error in preference \"%s\" at line %d of\n%s %s",
  2679. cur_var->str, pline, pf_path, hint);
  2680. break;
  2681. case PREFS_SET_NO_SUCH_PREF:
  2682. /*
  2683. * If "print.command" silently ignore it because it's valid
  2684. * on non-Win32 platforms.
  2685. */
  2686. if (strcmp(cur_var->str, "print.command") != 0)
  2687. g_warning ("No such preference \"%s\" at line %d of\n%s %s",
  2688. cur_var->str, pline, pf_path, hint);
  2689. prefs.unknown_prefs = TRUE;
  2690. break;
  2691. case PREFS_SET_OBSOLETE:
  2692. if (strcmp(cur_var->str, "print.command") != 0)
  2693. /* If an attempt is made to save the preferences, a popup warning will be
  2694. displayed stating that obsolete prefs have been detected and the user will
  2695. be given the opportunity to save these prefs under a different profile name.
  2696. The prefs in question need to be listed in the console window so that the
  2697. user can make an informed choice.
  2698. */
  2699. g_warning ("Obsolete preference \"%s\" at line %d of\n%s %s",
  2700. cur_var->str, pline, pf_path, hint);
  2701. prefs.unknown_prefs = TRUE;
  2702. break;
  2703. }
  2704. } else {
  2705. g_warning ("Incomplete preference at line %d: of\n%s %s", pline, pf_path, hint);
  2706. }
  2707. }
  2708. state = IN_VAR;
  2709. got_val = FALSE;
  2710. g_string_truncate(cur_var, 0);
  2711. g_string_append_c(cur_var, (gchar) got_c);
  2712. pline = fline;
  2713. } else if (isspace(got_c) && cur_var->len > 0 && got_val) {
  2714. state = PRE_VAL;
  2715. } else if (got_c == '#') {
  2716. state = IN_SKIP;
  2717. } else {
  2718. g_warning ("Malformed preference at line %d of\n%s %s", fline, pf_path, hint);
  2719. }
  2720. break;
  2721. case IN_VAR:
  2722. if (got_c != ':') {
  2723. g_string_append_c(cur_var, (gchar) got_c);
  2724. } else {
  2725. /* This is a colon (':') */
  2726. state = PRE_VAL;
  2727. g_string_truncate(cur_val, 0);
  2728. /*
  2729. * Set got_val to TRUE to accommodate prefs such as
  2730. * "gui.fileopen.dir" that do not require a value.
  2731. */
  2732. got_val = TRUE;
  2733. }
  2734. break;
  2735. case PRE_VAL:
  2736. if (!isspace(got_c)) {
  2737. state = IN_VAL;
  2738. g_string_append_c(cur_val, (gchar) got_c);
  2739. }
  2740. break;
  2741. case IN_VAL:
  2742. g_string_append_c(cur_val, (gchar) got_c);
  2743. break;
  2744. }
  2745. }
  2746. if (cur_var->len > 0) {
  2747. if (got_val) {
  2748. /* Convert the string to a range. Since we're reading the
  2749. * preferences file, silently lower values in excess of the
  2750. * range's maximum.
  2751. */
  2752. switch (pref_set_pair_fct(cur_var->str, cur_val->str, private_data, FALSE)) {
  2753. case PREFS_SET_OK:
  2754. break;
  2755. case PREFS_SET_SYNTAX_ERR:
  2756. g_warning ("Syntax error in preference %s at line %d of\n%s %s",
  2757. cur_var->str, pline, pf_path, hint);
  2758. break;
  2759. case PREFS_SET_NO_SUCH_PREF:
  2760. g_warning ("No such preference \"%s\" at line %d of\n%s %s",
  2761. cur_var->str, pline, pf_path, hint);
  2762. prefs.unknown_prefs = TRUE;
  2763. break;
  2764. case PREFS_SET_OBSOLETE:
  2765. prefs.unknown_prefs = TRUE;
  2766. break;
  2767. }
  2768. } else {
  2769. g_warning ("Incomplete preference at line %d of\n%s %s",
  2770. pline, pf_path, hint);
  2771. }
  2772. }
  2773. g_string_free(cur_val, TRUE);
  2774. g_string_free(cur_var, TRUE);
  2775. if (ferror(pf))
  2776. return errno;
  2777. else
  2778. return 0;
  2779. }
  2780. /*
  2781. * If we were handed a preference starting with "uat:", try to turn it into
  2782. * a valid uat entry.
  2783. */
  2784. static gboolean
  2785. prefs_set_uat_pref(char *uat_entry) {
  2786. gchar *p, *colonp;
  2787. uat_t *uat;
  2788. gchar *err;
  2789. colonp = strchr(uat_entry, ':');
  2790. if (colonp == NULL)
  2791. return FALSE;
  2792. p = colonp;
  2793. *p++ = '\0';
  2794. /*
  2795. * Skip over any white space (there probably won't be any, but
  2796. * as we allow it in the preferences file, we might as well
  2797. * allow it here).
  2798. */
  2799. while (isspace((guchar)*p))
  2800. p++;
  2801. if (*p == '\0') {
  2802. /*
  2803. * Put the colon back, so if our caller uses, in an
  2804. * error message, the string they passed us, the message
  2805. * looks correct.
  2806. */
  2807. *colonp = ':';
  2808. return FALSE;
  2809. }
  2810. uat = uat_find(uat_entry);
  2811. *colonp = ':';
  2812. if (uat == NULL) {
  2813. return FALSE;
  2814. }
  2815. if (uat_load_str(uat, p, &err)) {
  2816. return TRUE;
  2817. }
  2818. return FALSE;
  2819. }
  2820. /*
  2821. * Given a string of the form "<pref name>:<pref value>", as might appear
  2822. * as an argument to a "-o" option, parse it and set the preference in
  2823. * question. Return an indication of whether it succeeded or failed
  2824. * in some fashion.
  2825. */
  2826. prefs_set_pref_e
  2827. prefs_set_pref(char *prefarg)
  2828. {
  2829. gchar *p, *colonp;
  2830. prefs_set_pref_e ret;
  2831. /*
  2832. * Set the counters of "mgcp.{tcp,udp}.port" entries we've
  2833. * seen to values that keep us from trying to interpret tham
  2834. * as "mgcp.{tcp,udp}.gateway_port" or "mgcp.{tcp,udp}.callagent_port",
  2835. * as, from the command line, we have no way of guessing which
  2836. * the user had in mind.
  2837. */
  2838. mgcp_tcp_port_count = -1;
  2839. mgcp_udp_port_count = -1;
  2840. colonp = strchr(prefarg, ':');
  2841. if (colonp == NULL)
  2842. return PREFS_SET_SYNTAX_ERR;
  2843. p = colonp;
  2844. *p++ = '\0';
  2845. /*
  2846. * Skip over any white space (there probably won't be any, but
  2847. * as we allow it in the preferences file, we might as well
  2848. * allow it here).
  2849. */
  2850. while (isspace((guchar)*p))
  2851. p++;
  2852. if (*p == '\0') {
  2853. /*
  2854. * Put the colon back, so if our caller uses, in an
  2855. * error message, the string they passed us, the message
  2856. * looks correct.
  2857. */
  2858. *colonp = ':';
  2859. return PREFS_SET_SYNTAX_ERR;
  2860. }
  2861. if (strcmp(prefarg, "uat")) {
  2862. ret = set_pref(prefarg, p, NULL, TRUE);
  2863. } else {
  2864. ret = prefs_set_uat_pref(p) ? PREFS_SET_OK : PREFS_SET_SYNTAX_ERR;
  2865. }
  2866. *colonp = ':'; /* put the colon back */
  2867. return ret;
  2868. }
  2869. /*
  2870. * Returns TRUE if the given device is hidden
  2871. */
  2872. gboolean
  2873. prefs_is_capture_device_hidden(const char *name)
  2874. {
  2875. gchar *tok, *devices;
  2876. size_t len;
  2877. if (prefs.capture_devices_hide && name) {
  2878. devices = g_strdup (prefs.capture_devices_hide);
  2879. len = strlen (name);
  2880. for (tok = strtok (devices, ","); tok; tok = strtok(NULL, ",")) {
  2881. if (strlen (tok) == len && strcmp (name, tok) == 0) {
  2882. g_free (devices);
  2883. return TRUE;
  2884. }
  2885. }
  2886. g_free (devices);
  2887. }
  2888. return FALSE;
  2889. }
  2890. /*
  2891. * Returns TRUE if the given column is visible (not hidden)
  2892. */
  2893. static gboolean
  2894. prefs_is_column_visible(const gchar *cols_hidden, fmt_data *cfmt)
  2895. {
  2896. gchar *tok, *cols;
  2897. fmt_data cfmt_hidden;
  2898. /*
  2899. * Do we have a list of hidden columns?
  2900. */
  2901. if (cols_hidden) {
  2902. /*
  2903. * Yes - check the column against each of the ones in the
  2904. * list.
  2905. */
  2906. cols = g_strdup(cols_hidden);
  2907. for (tok = strtok(cols, ","); tok; tok = strtok(NULL, ",")) {
  2908. tok = g_strstrip(tok);
  2909. /*
  2910. * Parse this column format.
  2911. */
  2912. if (!parse_column_format(&cfmt_hidden, tok)) {
  2913. /*
  2914. * It's not valid; ignore it.
  2915. */
  2916. continue;
  2917. }
  2918. /*
  2919. * Does it match the column?
  2920. */
  2921. if (cfmt->fmt != cfmt_hidden.fmt) {
  2922. /* No. */
  2923. g_free(cfmt_hidden.custom_field);
  2924. continue;
  2925. }
  2926. if (cfmt->fmt == COL_CUSTOM) {
  2927. /*
  2928. * A custom column has to have the
  2929. * same custom field and occurrence.
  2930. */
  2931. if (strcmp(cfmt->custom_field,
  2932. cfmt_hidden.custom_field) != 0) {
  2933. /* Different fields. */
  2934. g_free(cfmt_hidden.custom_field);
  2935. continue;
  2936. }
  2937. if (cfmt->custom_occurrence != cfmt_hidden.custom_occurrence) {
  2938. /* Different occurrences. */
  2939. g_free(cfmt_hidden.custom_field);
  2940. continue;
  2941. }
  2942. }
  2943. /*
  2944. * OK, they match, so it's one of the hidden fields,
  2945. * hence not visible.
  2946. */
  2947. g_free(cfmt_hidden.custom_field);
  2948. g_free(cols);
  2949. return FALSE;
  2950. }
  2951. g_free(cols);
  2952. }
  2953. /*
  2954. * No - either there are no hidden columns or this isn't one
  2955. * of them - so it is visible.
  2956. */
  2957. return TRUE;
  2958. }
  2959. /*
  2960. * Returns TRUE if the given device should capture in monitor mode by default
  2961. */
  2962. gboolean
  2963. prefs_capture_device_monitor_mode(const char *name)
  2964. {
  2965. gchar *tok, *devices;
  2966. size_t len;
  2967. if (prefs.capture_devices_monitor_mode && name) {
  2968. devices = g_strdup (prefs.capture_devices_monitor_mode);
  2969. len = strlen (name);
  2970. for (tok = strtok (devices, ","); tok; tok = strtok(NULL, ",")) {
  2971. if (strlen (tok) == len && strcmp (name, tok) == 0) {
  2972. g_free (devices);
  2973. return TRUE;
  2974. }
  2975. }
  2976. g_free (devices);
  2977. }
  2978. return FALSE;
  2979. }
  2980. /*
  2981. * Returns TRUE if the user has marked this column as visible
  2982. */
  2983. gboolean
  2984. prefs_capture_options_dialog_column_is_visible(const gchar *column)
  2985. {
  2986. GList *curr;
  2987. gchar *col;
  2988. for (curr = g_list_first(prefs.capture_columns); curr; curr = g_list_next(curr)) {
  2989. col = (gchar *)curr->data;
  2990. if (col && (g_ascii_strcasecmp(col, column) == 0)) {
  2991. return TRUE;
  2992. }
  2993. }
  2994. return FALSE;
  2995. }
  2996. #define PRS_GUI_FILTER_LABEL "gui.filter_expressions.label"
  2997. #define PRS_GUI_FILTER_EXPR "gui.filter_expressions.expr"
  2998. #define PRS_GUI_FILTER_ENABLED "gui.filter_expressions.enabled"
  2999. #define RED_COMPONENT(x) (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
  3000. #define GREEN_COMPONENT(x) (guint16) (((((x) >> 8) & 0xff) * 65535 / 255))
  3001. #define BLUE_COMPONENT(x) (guint16) ( (((x) & 0xff) * 65535 / 255))
  3002. char
  3003. string_to_name_resolve(const char *string, e_addr_resolve *name_resolve)
  3004. {
  3005. char c;
  3006. memset(name_resolve, 0, sizeof(e_addr_resolve));
  3007. while ((c = *string++) != '\0') {
  3008. switch (c) {
  3009. case 'm':
  3010. name_resolve->mac_name = TRUE;
  3011. break;
  3012. case 'n':
  3013. name_resolve->network_name = TRUE;
  3014. break;
  3015. case 'N':
  3016. name_resolve->use_external_net_name_resolver = TRUE;
  3017. break;
  3018. case 't':
  3019. name_resolve->transport_name = TRUE;
  3020. break;
  3021. case 'C':
  3022. name_resolve->concurrent_dns = TRUE;
  3023. break;
  3024. default:
  3025. /*
  3026. * Unrecognized letter.
  3027. */
  3028. return c;
  3029. }
  3030. }
  3031. return '\0';
  3032. }
  3033. static void
  3034. try_convert_to_custom_column(gpointer *el_data)
  3035. {
  3036. /* Array of columns that have been migrated to custom columns */
  3037. struct {
  3038. gint el;
  3039. const gchar *col_expr;
  3040. } migrated_columns[] = {
  3041. { COL_COS_VALUE, "vlan.priority" },
  3042. { COL_CIRCUIT_ID, "iax2.call" },
  3043. { COL_BSSGP_TLLI, "bssgp.tlli" },
  3044. { COL_HPUX_SUBSYS, "nettl.subsys" },
  3045. { COL_HPUX_DEVID, "nettl.devid" },
  3046. { COL_FR_DLCI, "fr.dlci" },
  3047. { COL_REL_CONV_TIME, "tcp.time_relative" },
  3048. { COL_DELTA_CONV_TIME, "tcp.time_delta" },
  3049. { COL_OXID, "fc.ox_id" },
  3050. { COL_RXID, "fc.rx_id" },
  3051. { COL_SRCIDX, "mdshdr.srcidx" },
  3052. { COL_DSTIDX, "mdshdr.dstidx" },
  3053. { COL_DCE_CTX, "dcerpc.cn_ctx_id" }
  3054. };
  3055. guint haystack_idx;
  3056. const gchar *haystack_fmt;
  3057. gchar **fmt = (gchar **) el_data;
  3058. for (haystack_idx = 0;
  3059. haystack_idx < G_N_ELEMENTS(migrated_columns);
  3060. ++haystack_idx) {
  3061. haystack_fmt = col_format_to_string(migrated_columns[haystack_idx].el);
  3062. if (strcmp(haystack_fmt, *fmt) == 0) {
  3063. gchar *cust_col = g_strdup_printf("%%Cus:%s:0",
  3064. migrated_columns[haystack_idx].col_expr);
  3065. g_free(*fmt);
  3066. *fmt = cust_col;
  3067. }
  3068. }
  3069. }
  3070. static prefs_set_pref_e
  3071. set_pref(gchar *pref_name, const gchar *value, void *private_data _U_,
  3072. gboolean return_range_errors)
  3073. {
  3074. unsigned long int cval;
  3075. guint uval;
  3076. gboolean bval;
  3077. gint enum_val;
  3078. char *p;
  3079. gchar *dotp, *last_dotp;
  3080. static gchar *filter_label = NULL;
  3081. static gboolean filter_enabled = FALSE;
  3082. gchar *filter_expr = NULL;
  3083. module_t *module;
  3084. pref_t *pref;
  3085. if (strcmp(pref_name, PRS_GUI_FILTER_LABEL) == 0) {
  3086. filter_label = g_strdup(value);
  3087. } else if (strcmp(pref_name, PRS_GUI_FILTER_ENABLED) == 0) {
  3088. filter_enabled = (strcmp(value, "TRUE") == 0) ? TRUE : FALSE;
  3089. } else if (strcmp(pref_name, PRS_GUI_FILTER_EXPR) == 0) {
  3090. filter_expr = g_strdup(value);
  3091. filter_expression_new(filter_label, filter_expr, filter_enabled);
  3092. g_free(filter_label);
  3093. g_free(filter_expr);
  3094. } else if (strcmp(pref_name, "gui.version_in_start_page") == 0) {
  3095. /* Convert deprecated value to closest current equivalent */
  3096. if (g_ascii_strcasecmp(value, "true") == 0) {
  3097. prefs.gui_version_placement = version_both;
  3098. } else {
  3099. prefs.gui_version_placement = version_neither;
  3100. }
  3101. } else if (strcmp(pref_name, "name_resolve") == 0 ||
  3102. strcmp(pref_name, "capture.name_resolve") == 0) {
  3103. /*
  3104. * Handle the deprecated name resolution options.
  3105. *
  3106. * "TRUE" and "FALSE", for backwards compatibility, are synonyms for
  3107. * RESOLV_ALL and RESOLV_NONE.
  3108. *
  3109. * Otherwise, we treat it as a list of name types we want to resolve.
  3110. */
  3111. if (g_ascii_strcasecmp(value, "true") == 0) {
  3112. gbl_resolv_flags.mac_name = TRUE;
  3113. gbl_resolv_flags.network_name = TRUE;
  3114. gbl_resolv_flags.transport_name = TRUE;
  3115. gbl_resolv_flags.concurrent_dns = TRUE;
  3116. }
  3117. else if (g_ascii_strcasecmp(value, "false") == 0) {
  3118. gbl_resolv_flags.mac_name = FALSE;
  3119. gbl_resolv_flags.network_name = FALSE;
  3120. gbl_resolv_flags.transport_name = FALSE;
  3121. gbl_resolv_flags.concurrent_dns = FALSE;
  3122. }
  3123. else {
  3124. /* start out with none set */
  3125. gbl_resolv_flags.mac_name = FALSE;
  3126. gbl_resolv_flags.network_name = FALSE;
  3127. gbl_resolv_flags.transport_name = FALSE;
  3128. gbl_resolv_flags.concurrent_dns = FALSE;
  3129. if (string_to_name_resolve(value, &gbl_resolv_flags) != '\0')
  3130. return PREFS_SET_SYNTAX_ERR;
  3131. }
  3132. } else {
  3133. /* Handle deprecated "global" options that don't have a module
  3134. * associated with them
  3135. */
  3136. if ((strcmp(pref_name, "name_resolve_concurrency") == 0) ||
  3137. (strcmp(pref_name, "name_resolve_load_smi_modules") == 0) ||
  3138. (strcmp(pref_name, "name_resolve_suppress_smi_errors") == 0)) {
  3139. module = nameres_module;
  3140. dotp = pref_name;
  3141. } else {
  3142. /* To which module does this preference belong? */
  3143. module = NULL;
  3144. last_dotp = pref_name;
  3145. while (!module) {
  3146. dotp = strchr(last_dotp, '.');
  3147. if (dotp == NULL) {
  3148. /* Either there's no such module, or no module was specified.
  3149. In either case, that means there's no such preference. */
  3150. return PREFS_SET_NO_SUCH_PREF;
  3151. }
  3152. *dotp = '\0'; /* separate module and preference name */
  3153. module = prefs_find_module(pref_name);
  3154. /*
  3155. * XXX - "Diameter" rather than "diameter" was used in earlier
  3156. * versions of Wireshark; if we didn't find the module, and its name
  3157. * was "Diameter", look for "diameter" instead.
  3158. *
  3159. * In addition, the BEEP protocol used to be the BXXP protocol,
  3160. * so if we didn't find the module, and its name was "bxxp",
  3161. * look for "beep" instead.
  3162. *
  3163. * Also, the preferences for GTP v0 and v1 were combined under
  3164. * a single "gtp" heading, and the preferences for SMPP were
  3165. * moved to "smpp-gsm-sms" and then moved to "gsm-sms-ud".
  3166. * However, SMPP now has its own preferences, so we just map
  3167. * "smpp-gsm-sms" to "gsm-sms-ud", and then handle SMPP below.
  3168. *
  3169. * We also renamed "dcp" to "dccp", "x.25" to "x25", "x411" to "p1"
  3170. * and "nsip" to "gprs_ns".
  3171. *
  3172. * The SynOptics Network Management Protocol (SONMP) is now known by
  3173. * its modern name, the Nortel Discovery Protocol (NDP).
  3174. */
  3175. if (module == NULL) {
  3176. if (strcmp(pref_name, "column") == 0)
  3177. module = gui_column_module;
  3178. else if (strcmp(pref_name, "Diameter") == 0)
  3179. module = prefs_find_module("diameter");
  3180. else if (strcmp(pref_name, "bxxp") == 0)
  3181. module = prefs_find_module("beep");
  3182. else if (strcmp(pref_name, "gtpv0") == 0 ||
  3183. strcmp(pref_name, "gtpv1") == 0)
  3184. module = prefs_find_module("gtp");
  3185. else if (strcmp(pref_name, "smpp-gsm-sms") == 0)
  3186. module = prefs_find_module("gsm-sms-ud");
  3187. else if (strcmp(pref_name, "dcp") == 0)
  3188. module = prefs_find_module("dccp");
  3189. else if (strcmp(pref_name, "x.25") == 0)
  3190. module = prefs_find_module("x25");
  3191. else if (strcmp(pref_name, "x411") == 0)
  3192. module = prefs_find_module("p1");
  3193. else if (strcmp(pref_name, "nsip") == 0)
  3194. module = prefs_find_module("gprs-ns");
  3195. else if (strcmp(pref_name, "sonmp") == 0)
  3196. module = prefs_find_module("ndp");
  3197. else if (strcmp(pref_name, "etheric") == 0 ||
  3198. strcmp(pref_name, "isup_thin") == 0) {
  3199. /* This protocol was removed 7. July 2009 */
  3200. return PREFS_SET_OBSOLETE;
  3201. }
  3202. if (module) {
  3203. g_warning ("Preference \"%s.%s\" has been converted to \"%s.%s.%s\"\n"
  3204. "Save your preferences to make this change permanent.",
  3205. pref_name, dotp+1, module->parent->name, pref_name, dotp+1);
  3206. prefs.unknown_prefs = TRUE;
  3207. }
  3208. }
  3209. *dotp = '.'; /* put the preference string back */
  3210. dotp++; /* skip past separator to preference name */
  3211. last_dotp = dotp;
  3212. }
  3213. }
  3214. pref = prefs_find_preference(module, dotp);
  3215. if (pref == NULL) {
  3216. prefs.unknown_prefs = TRUE;
  3217. /* "gui" prefix was added to column preferences for better organization
  3218. * within the preferences file
  3219. */
  3220. if ((strcmp(pref_name, PRS_COL_HIDDEN) == 0) ||
  3221. (strcmp(pref_name, PRS_COL_FMT) == 0)) {
  3222. pref = prefs_find_preference(module, pref_name);
  3223. }
  3224. else if (strcmp(module->name, "mgcp") == 0) {
  3225. /*
  3226. * XXX - "mgcp.display raw text toggle" and "mgcp.display dissect tree"
  3227. * rather than "mgcp.display_raw_text" and "mgcp.display_dissect_tree"
  3228. * were used in earlier versions of Wireshark; if we didn't find the
  3229. * preference, it was an MGCP preference, and its name was
  3230. * "display raw text toggle" or "display dissect tree", look for
  3231. * "display_raw_text" or "display_dissect_tree" instead.
  3232. *
  3233. * "mgcp.tcp.port" and "mgcp.udp.port" are harder to handle, as both
  3234. * the gateway and callagent ports were given those names; we interpret
  3235. * the first as "mgcp.{tcp,udp}.gateway_port" and the second as
  3236. * "mgcp.{tcp,udp}.callagent_port", as that's the order in which
  3237. * they were registered by the MCCP dissector and thus that's the
  3238. * order in which they were written to the preferences file. (If
  3239. * we're not reading the preferences file, but are handling stuff
  3240. * from a "-o" command-line option, we have no clue which the user
  3241. * had in mind - they should have used "mgcp.{tcp,udp}.gateway_port"
  3242. * or "mgcp.{tcp,udp}.callagent_port" instead.)
  3243. */
  3244. if (strcmp(dotp, "display raw text toggle") == 0)
  3245. pref = prefs_find_preference(module, "display_raw_text");
  3246. else if (strcmp(dotp, "display dissect tree") == 0)
  3247. pref = prefs_find_preference(module, "display_dissect_tree");
  3248. else if (strcmp(dotp, "tcp.port") == 0) {
  3249. mgcp_tcp_port_count++;
  3250. if (mgcp_tcp_port_count == 1) {
  3251. /* It's the first one */
  3252. pref = prefs_find_preference(module, "tcp.gateway_port");
  3253. } else if (mgcp_tcp_port_count == 2) {
  3254. /* It's the second one */
  3255. pref = prefs_find_preference(module, "tcp.callagent_port");
  3256. }
  3257. /* Otherwise it's from the command line, and we don't bother
  3258. mapping it. */
  3259. } else if (strcmp(dotp, "udp.port") == 0) {
  3260. mgcp_udp_port_count++;
  3261. if (mgcp_udp_port_count == 1) {
  3262. /* It's the first one */
  3263. pref = prefs_find_preference(module, "udp.gateway_port");
  3264. } else if (mgcp_udp_port_count == 2) {
  3265. /* It's the second one */
  3266. pref = prefs_find_preference(module, "udp.callagent_port");
  3267. }
  3268. /* Otherwise it's from the command line, and we don't bother
  3269. mapping it. */
  3270. }
  3271. } else if (strcmp(module->name, "smb") == 0) {
  3272. /* Handle old names for SMB preferences. */
  3273. if (strcmp(dotp, "smb.trans.reassembly") == 0)
  3274. pref = prefs_find_preference(module, "trans_reassembly");
  3275. else if (strcmp(dotp, "smb.dcerpc.reassembly") == 0)
  3276. pref = prefs_find_preference(module, "dcerpc_reassembly");
  3277. } else if (strcmp(module->name, "ndmp") == 0) {
  3278. /* Handle old names for NDMP preferences. */
  3279. if (strcmp(dotp, "ndmp.desegment") == 0)
  3280. pref = prefs_find_preference(module, "desegment");
  3281. } else if (strcmp(module->name, "diameter") == 0) {
  3282. /* Handle old names for Diameter preferences. */
  3283. if (strcmp(dotp, "diameter.desegment") == 0)
  3284. pref = prefs_find_preference(module, "desegment");
  3285. } else if (strcmp(module->name, "pcli") == 0) {
  3286. /* Handle old names for PCLI preferences. */
  3287. if (strcmp(dotp, "pcli.udp_port") == 0)
  3288. pref = prefs_find_preference(module, "udp_port");
  3289. } else if (strcmp(module->name, "artnet") == 0) {
  3290. /* Handle old names for ARTNET preferences. */
  3291. if (strcmp(dotp, "artnet.udp_port") == 0)
  3292. pref = prefs_find_preference(module, "udp_port");
  3293. } else if (strcmp(module->name, "mapi") == 0) {
  3294. /* Handle old names for MAPI preferences. */
  3295. if (strcmp(dotp, "mapi_decrypt") == 0)
  3296. pref = prefs_find_preference(module, "decrypt");
  3297. } else if (strcmp(module->name, "fc") == 0) {
  3298. /* Handle old names for Fibre Channel preferences. */
  3299. if (strcmp(dotp, "reassemble_fc") == 0)
  3300. pref = prefs_find_preference(module, "reassemble");
  3301. else if (strcmp(dotp, "fc_max_frame_size") == 0)
  3302. pref = prefs_find_preference(module, "max_frame_size");
  3303. } else if (strcmp(module->name, "fcip") == 0) {
  3304. /* Handle old names for Fibre Channel-over-IP preferences. */
  3305. if (strcmp(dotp, "desegment_fcip_messages") == 0)
  3306. pref = prefs_find_preference(module, "desegment");
  3307. else if (strcmp(dotp, "fcip_port") == 0)
  3308. pref = prefs_find_preference(module, "target_port");
  3309. } else if (strcmp(module->name, "gtp") == 0) {
  3310. /* Handle old names for GTP preferences. */
  3311. if (strcmp(dotp, "gtpv0_port") == 0)
  3312. pref = prefs_find_preference(module, "v0_port");
  3313. else if (strcmp(dotp, "gtpv1c_port") == 0)
  3314. pref = prefs_find_preference(module, "v1c_port");
  3315. else if (strcmp(dotp, "gtpv1u_port") == 0)
  3316. pref = prefs_find_preference(module, "v1u_port");
  3317. else if (strcmp(dotp, "gtp_dissect_tpdu") == 0)
  3318. pref = prefs_find_preference(module, "dissect_tpdu");
  3319. else if (strcmp(dotp, "gtpv0_dissect_cdr_as") == 0)
  3320. pref = prefs_find_preference(module, "v0_dissect_cdr_as");
  3321. else if (strcmp(dotp, "gtpv0_check_etsi") == 0)
  3322. pref = prefs_find_preference(module, "v0_check_etsi");
  3323. else if (strcmp(dotp, "gtpv1_check_etsi") == 0)
  3324. pref = prefs_find_preference(module, "v1_check_etsi");
  3325. } else if (strcmp(module->name, "ip") == 0) {
  3326. /* Handle old names for IP preferences. */
  3327. if (strcmp(dotp, "ip_summary_in_tree") == 0)
  3328. pref = prefs_find_preference(module, "summary_in_tree");
  3329. } else if (strcmp(module->name, "iscsi") == 0) {
  3330. /* Handle old names for iSCSI preferences. */
  3331. if (strcmp(dotp, "iscsi_port") == 0)
  3332. pref = prefs_find_preference(module, "target_port");
  3333. } else if (strcmp(module->name, "lmp") == 0) {
  3334. /* Handle old names for LMP preferences. */
  3335. if (strcmp(dotp, "lmp_version") == 0)
  3336. pref = prefs_find_preference(module, "version");
  3337. } else if (strcmp(module->name, "mtp3") == 0) {
  3338. /* Handle old names for MTP3 preferences. */
  3339. if (strcmp(dotp, "mtp3_standard") == 0)
  3340. pref = prefs_find_preference(module, "standard");
  3341. else if (strcmp(dotp, "net_addr_format") == 0)
  3342. pref = prefs_find_preference(module, "addr_format");
  3343. } else if (strcmp(module->name, "nlm") == 0) {
  3344. /* Handle old names for NLM preferences. */
  3345. if (strcmp(dotp, "nlm_msg_res_matching") == 0)
  3346. pref = prefs_find_preference(module, "msg_res_matching");
  3347. } else if (strcmp(module->name, "ppp") == 0) {
  3348. /* Handle old names for PPP preferences. */
  3349. if (strcmp(dotp, "ppp_fcs") == 0)
  3350. pref = prefs_find_preference(module, "fcs_type");
  3351. else if (strcmp(dotp, "ppp_vj") == 0)
  3352. pref = prefs_find_preference(module, "decompress_vj");
  3353. } else if (strcmp(module->name, "rsvp") == 0) {
  3354. /* Handle old names for RSVP preferences. */
  3355. if (strcmp(dotp, "rsvp_process_bundle") == 0)
  3356. pref = prefs_find_preference(module, "process_bundle");
  3357. } else if (strcmp(module->name, "tcp") == 0) {
  3358. /* Handle old names for TCP preferences. */
  3359. if (strcmp(dotp, "tcp_summary_in_tree") == 0)
  3360. pref = prefs_find_preference(module, "summary_in_tree");
  3361. else if (strcmp(dotp, "tcp_analyze_sequence_numbers") == 0)
  3362. pref = prefs_find_preference(module, "analyze_sequence_numbers");
  3363. else if (strcmp(dotp, "tcp_relative_sequence_numbers") == 0)
  3364. pref = prefs_find_preference(module, "relative_sequence_numbers");
  3365. } else if (strcmp(module->name, "udp") == 0) {
  3366. /* Handle old names for UDP preferences. */
  3367. if (strcmp(dotp, "udp_summary_in_tree") == 0)
  3368. pref = prefs_find_preference(module, "summary_in_tree");
  3369. } else if (strcmp(module->name, "ndps") == 0) {
  3370. /* Handle old names for NDPS preferences. */
  3371. if (strcmp(dotp, "desegment_ndps") == 0)
  3372. pref = prefs_find_preference(module, "desegment_tcp");
  3373. } else if (strcmp(module->name, "http") == 0) {
  3374. /* Handle old names for HTTP preferences. */
  3375. if (strcmp(dotp, "desegment_http_headers") == 0)
  3376. pref = prefs_find_preference(module, "desegment_headers");
  3377. else if (strcmp(dotp, "desegment_http_body") == 0)
  3378. pref = prefs_find_preference(module, "desegment_body");
  3379. } else if (strcmp(module->name, "smpp") == 0) {
  3380. /* Handle preferences that moved from SMPP. */
  3381. module_t *new_module = prefs_find_module("gsm-sms-ud");
  3382. if (new_module){
  3383. if (strcmp(dotp, "port_number_udh_means_wsp") == 0)
  3384. pref = prefs_find_preference(new_module, "port_number_udh_means_wsp");
  3385. else if (strcmp(dotp, "try_dissect_1st_fragment") == 0)
  3386. pref = prefs_find_preference(new_module, "try_dissect_1st_fragment");
  3387. }
  3388. } else if (strcmp(module->name, "asn1") == 0) {
  3389. /* Handle old generic ASN.1 preferences (it's not really a
  3390. rename, as the new preferences support multiple ports,
  3391. but we might as well copy them over). */
  3392. if (strcmp(dotp, "tcp_port") == 0)
  3393. pref = prefs_find_preference(module, "tcp_ports");
  3394. else if (strcmp(dotp, "udp_port") == 0)
  3395. pref = prefs_find_preference(module, "udp_ports");
  3396. else if (strcmp(dotp, "sctp_port") == 0)
  3397. pref = prefs_find_preference(module, "sctp_ports");
  3398. } else if (strcmp(module->name, "llcgprs") == 0) {
  3399. if (strcmp(dotp, "ignore_cipher_bit") == 0)
  3400. pref = prefs_find_preference(module, "autodetect_cipher_bit");
  3401. } else if (strcmp(module->name, "erf") == 0) {
  3402. if (strcmp(dotp, "erfeth") == 0) {
  3403. /* Handle the old "erfeth" preference; map it to the new
  3404. "ethfcs" preference, and map the values to those for
  3405. the new preference. */
  3406. pref = prefs_find_preference(module, "ethfcs");
  3407. if (strcmp(value, "ethfcs") == 0 || strcmp(value, "Ethernet with FCS") == 0)
  3408. value = "TRUE";
  3409. else if (strcmp(value, "eth") == 0 || strcmp(value, "Ethernet") == 0)
  3410. value = "FALSE";
  3411. else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
  3412. value = "TRUE";
  3413. } else if (strcmp(dotp, "erfatm") == 0) {
  3414. /* Handle the old "erfatm" preference; map it to the new
  3415. "aal5_type" preference, and map the values to those for
  3416. the new preference. */
  3417. pref = prefs_find_preference(module, "aal5_type");
  3418. if (strcmp(value, "atm") == 0 || strcmp(value, "ATM") == 0)
  3419. value = "guess";
  3420. else if (strcmp(value, "llc") == 0 || strcmp(value, "LLC") == 0)
  3421. value = "llc";
  3422. else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
  3423. value = "guess";
  3424. } else if (strcmp(dotp, "erfhdlc") == 0) {
  3425. /* Handle the old "erfhdlc" preference; map it to the new
  3426. "hdlc_type" preference, and map the values to those for
  3427. the new preference. */
  3428. pref = prefs_find_preference(module, "hdlc_type");
  3429. if (strcmp(value, "chdlc") == 0 || strcmp(value, "Cisco HDLC") == 0)
  3430. value = "chdlc";
  3431. else if (strcmp(value, "ppp") == 0 || strcmp(value, "PPP serial") == 0)
  3432. value = "ppp";
  3433. else if (strcmp(value, "fr") == 0 || strcmp(value, "Frame Relay") == 0)
  3434. value = "frelay";
  3435. else if (strcmp(value, "mtp2") == 0 || strcmp(value, "SS7 MTP2") == 0)
  3436. value = "mtp2";
  3437. else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
  3438. value = "guess";
  3439. }
  3440. } else if (strcmp(module->name, "eth") == 0) {
  3441. /* "eth.qinq_ethertype" has been changed(restored) to "vlan.qinq.ethertype" */
  3442. if (strcmp(dotp, "qinq_ethertype") == 0) {
  3443. module_t *new_module = prefs_find_module("vlan");
  3444. if (new_module) {
  3445. pref = prefs_find_preference(new_module, "qinq_ethertype");
  3446. module = new_module;
  3447. }
  3448. }
  3449. } else if (strcmp(module->name, "taps") == 0) {
  3450. /* taps preferences moved to "statistics" module */
  3451. if (strcmp(dotp, "update_interval") == 0 ||
  3452. strcmp(dotp, "rtp_player_max_visible") == 0)
  3453. pref = prefs_find_preference(stats_module, dotp);
  3454. } else if (strcmp(module->name, "packet_list") == 0) {
  3455. /* packet_list preferences moved to protocol module */
  3456. if (strcmp(dotp, "display_hidden_proto_items") == 0)
  3457. pref = prefs_find_preference(protocols_module, dotp);
  3458. } else if (strcmp(module->name, "stream") == 0) {
  3459. /* stream preferences moved to gui color module */
  3460. if ((strcmp(dotp, "client.fg") == 0) ||
  3461. (strcmp(dotp, "client.bg") == 0) ||
  3462. (strcmp(dotp, "server.fg") == 0) ||
  3463. (strcmp(dotp, "server.bg") == 0))
  3464. pref = prefs_find_preference(gui_color_module, pref_name);
  3465. } else if (strcmp(module->name, "nameres") == 0) {
  3466. if (strcmp(pref_name, "name_resolve_concurrency") == 0) {
  3467. pref = prefs_find_preference(nameres_module, pref_name);
  3468. } else if (strcmp(pref_name, "name_resolve_load_smi_modules") == 0) {
  3469. pref = prefs_find_preference(nameres_module, "load_smi_modules");
  3470. } else if (strcmp(pref_name, "name_resolve_suppress_smi_errors") == 0) {
  3471. pref = prefs_find_preference(nameres_module, "suppress_smi_errors");
  3472. }
  3473. }
  3474. }
  3475. if (pref == NULL)
  3476. return PREFS_SET_NO_SUCH_PREF; /* no such preference */
  3477. switch (pref->type) {
  3478. case PREF_UINT:
  3479. /* XXX - give an error if it doesn't fit in a guint? */
  3480. uval = (guint)strtoul(value, &p, pref->info.base);
  3481. if (p == value || *p != '\0')
  3482. return PREFS_SET_SYNTAX_ERR; /* number was bad */
  3483. if (*pref->varp.uint != uval) {
  3484. module->prefs_changed = TRUE;
  3485. *pref->varp.uint = uval;
  3486. }
  3487. break;
  3488. case PREF_BOOL:
  3489. /* XXX - give an error if it's neither "true" nor "false"? */
  3490. if (g_ascii_strcasecmp(value, "true") == 0)
  3491. bval = TRUE;
  3492. else
  3493. bval = FALSE;
  3494. if (*pref->varp.boolp != bval) {
  3495. module->prefs_changed = TRUE;
  3496. *pref->varp.boolp = bval;
  3497. }
  3498. break;
  3499. case PREF_ENUM:
  3500. /* XXX - give an error if it doesn't match? */
  3501. enum_val = find_val_for_string(value, pref->info.enum_info.enumvals,
  3502. *pref->varp.enump);
  3503. if (*pref->varp.enump != enum_val) {
  3504. module->prefs_changed = TRUE;
  3505. *pref->varp.enump = enum_val;
  3506. }
  3507. break;
  3508. case PREF_STRING:
  3509. case PREF_FILENAME:
  3510. case PREF_DIRNAME:
  3511. if (strcmp(*pref->varp.string, value) != 0) {
  3512. module->prefs_changed = TRUE;
  3513. g_free((void *)*pref->varp.string);
  3514. *pref->varp.string = g_strdup(value);
  3515. }
  3516. break;
  3517. case PREF_RANGE:
  3518. {
  3519. range_t *newrange;
  3520. if (range_convert_str_work(&newrange, value, pref->info.max_value,
  3521. return_range_errors) != CVT_NO_ERROR) {
  3522. return PREFS_SET_SYNTAX_ERR; /* number was bad */
  3523. }
  3524. if (!ranges_are_equal(*pref->varp.range, newrange)) {
  3525. module->prefs_changed = TRUE;
  3526. g_free(*pref->varp.range);
  3527. *pref->varp.range = newrange;
  3528. } else {
  3529. g_free (newrange);
  3530. }
  3531. break;
  3532. }
  3533. case PREF_COLOR:
  3534. {
  3535. cval = strtoul(value, NULL, 16);
  3536. pref->varp.colorp->pixel = 0;
  3537. if ((pref->varp.colorp->red != RED_COMPONENT(cval)) ||
  3538. (pref->varp.colorp->green != GREEN_COMPONENT(cval)) ||
  3539. (pref->varp.colorp->blue != BLUE_COMPONENT(cval))) {
  3540. module->prefs_changed = TRUE;
  3541. pref->varp.colorp->red = RED_COMPONENT(cval);
  3542. pref->varp.colorp->green = GREEN_COMPONENT(cval);
  3543. pref->varp.colorp->blue = BLUE_COMPONENT(cval);
  3544. }
  3545. break;
  3546. }
  3547. case PREF_CUSTOM:
  3548. return pref->custom_cbs.set_cb(pref, value, &module->prefs_changed);
  3549. case PREF_STATIC_TEXT:
  3550. case PREF_UAT:
  3551. {
  3552. break;
  3553. }
  3554. case PREF_OBSOLETE:
  3555. return PREFS_SET_OBSOLETE; /* no such preference any more */
  3556. }
  3557. }
  3558. return PREFS_SET_OK;
  3559. }
  3560. typedef struct {
  3561. FILE *pf;
  3562. gboolean is_gui_module;
  3563. } write_gui_pref_arg_t;
  3564. const char *
  3565. prefs_pref_type_name(pref_t *pref)
  3566. {
  3567. const char *type_name = "[Unknown]";
  3568. if (!pref) {
  3569. return type_name; /* ...or maybe assert? */
  3570. }
  3571. switch (pref->type) {
  3572. case PREF_UINT:
  3573. switch (pref->info.base) {
  3574. case 10:
  3575. type_name = "Decimal";
  3576. break;
  3577. case 8:
  3578. type_name = "Octal";
  3579. break;
  3580. case 16:
  3581. type_name = "Hexadecimal";
  3582. break;
  3583. }
  3584. break;
  3585. case PREF_BOOL:
  3586. type_name = "Boolean";
  3587. break;
  3588. case PREF_ENUM:
  3589. type_name = "Choice";
  3590. break;
  3591. case PREF_STRING:
  3592. type_name = "String";
  3593. break;
  3594. case PREF_FILENAME:
  3595. type_name = "Filename";
  3596. break;
  3597. case PREF_DIRNAME:
  3598. type_name = "Directory";
  3599. break;
  3600. case PREF_RANGE:
  3601. type_name = "Range";
  3602. break;
  3603. case PREF_COLOR:
  3604. type_name = "Color";
  3605. break;
  3606. case PREF_CUSTOM:
  3607. if (pref->custom_cbs.type_name_cb)
  3608. return pref->custom_cbs.type_name_cb();
  3609. type_name = "Custom";
  3610. break;
  3611. case PREF_OBSOLETE:
  3612. type_name = "Obsolete";
  3613. break;
  3614. case PREF_STATIC_TEXT:
  3615. type_name = "Static text";
  3616. break;
  3617. case PREF_UAT:
  3618. type_name = "UAT";
  3619. break;
  3620. }
  3621. return type_name;
  3622. }
  3623. char *
  3624. prefs_pref_type_description(pref_t *pref)
  3625. {
  3626. const char *type_desc = "An unkown preference type";
  3627. if (!pref) {
  3628. return g_strdup_printf("%s.", type_desc); /* ...or maybe assert? */
  3629. }
  3630. switch (pref->type) {
  3631. case PREF_UINT:
  3632. switch (pref->info.base) {
  3633. case 10:
  3634. type_desc = "A decimal number";
  3635. break;
  3636. case 8:
  3637. type_desc = "An octal number";
  3638. break;
  3639. case 16:
  3640. type_desc = "A hexadecimal number";
  3641. break;
  3642. }
  3643. break;
  3644. case PREF_BOOL:
  3645. type_desc = "TRUE or FALSE (case-insensitive)";
  3646. break;
  3647. case PREF_ENUM:
  3648. {
  3649. const enum_val_t *enum_valp = pref->info.enum_info.enumvals;
  3650. GString *enum_str = g_string_new("One of: ");
  3651. while (enum_valp->name != NULL) {
  3652. g_string_append(enum_str, enum_valp->description);
  3653. enum_valp++;
  3654. if (enum_valp->name != NULL)
  3655. g_string_append(enum_str, ", ");
  3656. }
  3657. g_string_append(enum_str, "\n(case-insensitive).");
  3658. return g_string_free(enum_str, FALSE);
  3659. break;
  3660. }
  3661. case PREF_STRING:
  3662. type_desc = "A string";
  3663. break;
  3664. case PREF_FILENAME:
  3665. type_desc = "A path to a file";
  3666. break;
  3667. case PREF_DIRNAME:
  3668. type_desc = "A path to a directory";
  3669. break;
  3670. case PREF_RANGE:
  3671. {
  3672. type_desc = "A string denoting an positive integer range (e.g., \"1-20,30-40\")";
  3673. break;
  3674. }
  3675. case PREF_COLOR:
  3676. {
  3677. type_desc = "A six-digit hexadecimal RGB color triplet (e.g. fce94f)";
  3678. break;
  3679. }
  3680. case PREF_CUSTOM:
  3681. if (pref->custom_cbs.type_description_cb)
  3682. return pref->custom_cbs.type_description_cb();
  3683. type_desc = "A custom value";
  3684. break;
  3685. case PREF_OBSOLETE:
  3686. type_desc = "An obsolete preference";
  3687. break;
  3688. case PREF_STATIC_TEXT:
  3689. type_desc = "[Static text]";
  3690. break;
  3691. case PREF_UAT:
  3692. type_desc = "Configuration data stored in its own file";
  3693. break;
  3694. default:
  3695. break;
  3696. }
  3697. return g_strdup(type_desc);
  3698. }
  3699. static gboolean
  3700. prefs_pref_is_default(pref_t *pref) {
  3701. if (!pref) return FALSE;
  3702. switch (pref->type) {
  3703. case PREF_UINT:
  3704. if (pref->default_val.uint == *pref->varp.uint)
  3705. return TRUE;
  3706. break;
  3707. case PREF_BOOL:
  3708. if (pref->default_val.boolval == *pref->varp.boolp)
  3709. return TRUE;
  3710. break;
  3711. case PREF_ENUM:
  3712. if (pref->default_val.enumval == *pref->varp.enump)
  3713. return TRUE;
  3714. break;
  3715. case PREF_STRING:
  3716. case PREF_FILENAME:
  3717. case PREF_DIRNAME:
  3718. if (!(g_strcmp0(pref->default_val.string, *pref->varp.string)))
  3719. return TRUE;
  3720. break;
  3721. case PREF_RANGE:
  3722. {
  3723. if ((ranges_are_equal(pref->default_val.range, *pref->varp.range)))
  3724. return TRUE;
  3725. break;
  3726. }
  3727. case PREF_COLOR:
  3728. {
  3729. if ((pref->default_val.color.red == pref->varp.colorp->red) &&
  3730. (pref->default_val.color.green == pref->varp.colorp->green) &&
  3731. (pref->default_val.color.blue == pref->varp.colorp->blue))
  3732. return TRUE;
  3733. break;
  3734. }
  3735. case PREF_CUSTOM:
  3736. return pref->custom_cbs.is_default_cb(pref);
  3737. case PREF_OBSOLETE:
  3738. case PREF_STATIC_TEXT:
  3739. case PREF_UAT:
  3740. return FALSE;
  3741. /* g_assert_not_reached(); */
  3742. break;
  3743. }
  3744. return FALSE;
  3745. }
  3746. char *
  3747. prefs_pref_to_str(pref_t *pref, pref_source_t source) {
  3748. const char *pref_text = "[Unknown]";
  3749. guint pref_uint;
  3750. gboolean pref_boolval;
  3751. gint pref_enumval;
  3752. const char *pref_string;
  3753. range_t *pref_range;
  3754. color_t *pref_color;
  3755. if (!pref) {
  3756. return g_strdup(pref_text);
  3757. }
  3758. switch (source) {
  3759. case pref_default:
  3760. pref_uint = pref->default_val.uint;
  3761. pref_boolval = pref->default_val.boolval;
  3762. pref_enumval = pref->default_val.enumval;
  3763. pref_string = pref->default_val.string;
  3764. pref_range = pref->default_val.range;
  3765. pref_color = &pref->default_val.color;
  3766. break;
  3767. case pref_stashed:
  3768. pref_uint = pref->stashed_val.uint;
  3769. pref_boolval = pref->stashed_val.boolval;
  3770. pref_enumval = pref->stashed_val.enumval;
  3771. pref_string = pref->stashed_val.string;
  3772. pref_range = pref->stashed_val.range;
  3773. pref_color = &pref->stashed_val.color;
  3774. break;
  3775. case pref_current:
  3776. pref_uint = *pref->varp.uint;
  3777. pref_boolval = *pref->varp.boolp;
  3778. pref_enumval = *pref->varp.enump;
  3779. pref_string = *pref->varp.string;
  3780. pref_range = *pref->varp.range;
  3781. pref_color = pref->varp.colorp;
  3782. break;
  3783. default:
  3784. return g_strdup(pref_text);
  3785. }
  3786. switch (pref->type) {
  3787. case PREF_UINT:
  3788. switch (pref->info.base) {
  3789. case 10:
  3790. return g_strdup_printf("%u", pref_uint);
  3791. break;
  3792. case 8:
  3793. return g_strdup_printf("%#o", pref_uint);
  3794. break;
  3795. case 16:
  3796. return g_strdup_printf("%#x", pref_uint);
  3797. break;
  3798. }
  3799. break;
  3800. case PREF_BOOL:
  3801. return g_strdup_printf("%s", pref_boolval ? "TRUE" : "FALSE");
  3802. break;
  3803. case PREF_ENUM:
  3804. {
  3805. /*
  3806. * For now, we return the "description" value, so that if we
  3807. * save the preferences older versions of Wireshark can at
  3808. * least read preferences that they supported; we support
  3809. * either the short name or the description when reading
  3810. * the preferences file or a "-o" option.
  3811. */
  3812. const enum_val_t *enum_valp = pref->info.enum_info.enumvals;
  3813. while (enum_valp->name != NULL) {
  3814. if (enum_valp->value == pref_enumval)
  3815. return g_strdup(enum_valp->description);
  3816. enum_valp++;
  3817. }
  3818. break;
  3819. }
  3820. case PREF_STRING:
  3821. case PREF_FILENAME:
  3822. case PREF_DIRNAME:
  3823. return g_strdup(pref_string);
  3824. break;
  3825. case PREF_RANGE:
  3826. pref_text = range_convert_range(pref_range);
  3827. break;
  3828. case PREF_COLOR:
  3829. return g_strdup_printf("%02x%02x%02x",
  3830. (pref_color->red * 255 / 65535),
  3831. (pref_color->green * 255 / 65535),
  3832. (pref_color->blue * 255 / 65535));
  3833. break;
  3834. case PREF_CUSTOM:
  3835. if (pref->custom_cbs.to_str_cb)
  3836. return pref->custom_cbs.to_str_cb(pref, source == pref_default ? TRUE : FALSE);
  3837. pref_text = "[Custom]";
  3838. break;
  3839. case PREF_OBSOLETE:
  3840. pref_text = "[Obsolete]";
  3841. break;
  3842. case PREF_STATIC_TEXT:
  3843. pref_text = "[Static text]";
  3844. break;
  3845. case PREF_UAT:
  3846. {
  3847. uat_t *uat = pref->varp.uat;
  3848. if (uat && uat->filename)
  3849. return g_strdup_printf("[Managed in the file \"%s\"]", uat->filename);
  3850. else
  3851. pref_text = "[Managed in an unknown file]";
  3852. break;
  3853. }
  3854. default:
  3855. break;
  3856. }
  3857. return g_strdup(pref_text);
  3858. }
  3859. /*
  3860. * Write out a single dissector preference.
  3861. */
  3862. static void
  3863. write_pref(gpointer data, gpointer user_data)
  3864. {
  3865. pref_t *pref = (pref_t *)data;
  3866. write_pref_arg_t *arg = (write_pref_arg_t *)user_data;
  3867. gchar **desc_lines;
  3868. int i;
  3869. switch (pref->type) {
  3870. case PREF_OBSOLETE:
  3871. /*
  3872. * This preference is no longer supported; it's not a
  3873. * real preference, so we don't write it out (i.e., we
  3874. * treat it as if it weren't found in the list of
  3875. * preferences, and we weren't called in the first place).
  3876. */
  3877. return;
  3878. case PREF_STATIC_TEXT:
  3879. case PREF_UAT:
  3880. /* Nothing to do; don't bother printing the description */
  3881. return;
  3882. default:
  3883. break;
  3884. }
  3885. if (pref->type != PREF_CUSTOM || pref->custom_cbs.type_name_cb() != NULL) {
  3886. /*
  3887. * The prefix will either be the module name or the parent
  3888. * name if it's a subtree
  3889. */
  3890. const char *name_prefix = (arg->module->name != NULL) ? arg->module->name : arg->module->parent->name;
  3891. char *type_desc, *pref_text;
  3892. const char * def_prefix = prefs_pref_is_default(pref) ? "#" : "";
  3893. if (pref->type == PREF_CUSTOM) fprintf(arg->pf, "\n# %s", pref->custom_cbs.type_name_cb());
  3894. fprintf(arg->pf, "\n");
  3895. if (pref->description &&
  3896. (g_ascii_strncasecmp(pref->description,"", 2) != 0)) {
  3897. if (pref->type != PREF_CUSTOM) {
  3898. /* We get duplicate lines otherwise. */
  3899. desc_lines = g_strsplit(pref->description,"\n",0);
  3900. for (i = 0; desc_lines[i] != NULL; ++i) {
  3901. fprintf(arg->pf, "# %s\n", desc_lines[i]);
  3902. }
  3903. g_strfreev(desc_lines);
  3904. }
  3905. } else {
  3906. fprintf(arg->pf, "# No description\n");
  3907. }
  3908. type_desc = prefs_pref_type_description(pref);
  3909. desc_lines = g_strsplit(type_desc,"\n",0);
  3910. for (i = 0; desc_lines[i] != NULL; ++i) {
  3911. fprintf(arg->pf, "# %s\n", desc_lines[i]);
  3912. }
  3913. g_strfreev(desc_lines);
  3914. g_free(type_desc);
  3915. pref_text = prefs_pref_to_str(pref, pref_current);
  3916. fprintf(arg->pf, "%s%s.%s: ", def_prefix, name_prefix, pref->name);
  3917. desc_lines = g_strsplit(pref_text,"\n",0);
  3918. for (i = 0; desc_lines[i] != NULL; ++i) {
  3919. fprintf(arg->pf, "%s%s\n", i == 0 ? "" : def_prefix, desc_lines[i]);
  3920. }
  3921. if (i == 0) fprintf(arg->pf, "\n");
  3922. g_strfreev(desc_lines);
  3923. g_free(pref_text);
  3924. }
  3925. }
  3926. /*
  3927. * Write out all preferences for a module.
  3928. */
  3929. static guint
  3930. write_module_prefs(module_t *module, gpointer user_data)
  3931. {
  3932. write_gui_pref_arg_t *gui_pref_arg = (write_gui_pref_arg_t*)user_data;
  3933. write_pref_arg_t arg;
  3934. /* The GUI module needs to be explicitly called out so it
  3935. can be written out of order */
  3936. if ((module == gui_module) && (gui_pref_arg->is_gui_module != TRUE))
  3937. return 0;
  3938. /* Write a header for the main modules and GUI sub-modules */
  3939. if (((module->parent == NULL) || (module->parent == gui_module)) &&
  3940. ((prefs_module_has_submodules(module)) ||
  3941. (module->numprefs > 0) ||
  3942. (module->name == NULL))) {
  3943. if ((module->name == NULL) && (module->parent != NULL)) {
  3944. fprintf(gui_pref_arg->pf, "\n####### %s: %s ########\n", module->parent->title, module->title);
  3945. } else {
  3946. fprintf(gui_pref_arg->pf, "\n####### %s ########\n", module->title);
  3947. }
  3948. }
  3949. arg.module = module;
  3950. arg.pf = gui_pref_arg->pf;
  3951. g_list_foreach(arg.module->prefs, write_pref, &arg);
  3952. if (prefs_module_has_submodules(module))
  3953. return prefs_modules_foreach_submodules(module, write_module_prefs, user_data);
  3954. return 0;
  3955. }
  3956. /* Write out "prefs" to the user's preferences file, and return 0.
  3957. If the preferences file path is NULL, write to stdout.
  3958. If we got an error, stuff a pointer to the path of the preferences file
  3959. into "*pf_path_return", and return the errno. */
  3960. int
  3961. write_prefs(char **pf_path_return)
  3962. {
  3963. char *pf_path;
  3964. FILE *pf;
  3965. write_gui_pref_arg_t write_gui_pref_info;
  3966. /* Needed for "-G defaultprefs" */
  3967. init_prefs();
  3968. /* To do:
  3969. * - Split output lines longer than MAX_VAL_LEN
  3970. * - Create a function for the preference directory check/creation
  3971. * so that duplication can be avoided with filter.c
  3972. */
  3973. if (pf_path_return != NULL) {
  3974. pf_path = get_persconffile_path(PF_NAME, TRUE);
  3975. if ((pf = ws_fopen(pf_path, "w")) == NULL) {
  3976. *pf_path_return = pf_path;
  3977. return errno;
  3978. }
  3979. } else {
  3980. pf = stdout;
  3981. }
  3982. fputs("# Configuration file for Wireshark " VERSION ".\n"
  3983. "#\n"
  3984. "# This file is regenerated each time preferences are saved within\n"
  3985. "# Wireshark. Making manual changes should be safe, however.\n"
  3986. "# Preferences that have been commented out have not been\n"
  3987. "# changed from their default value.\n", pf);
  3988. /*
  3989. * For "backwards compatibility" the GUI module is written first as it's
  3990. * at the top of the file. This is followed by all modules that can't
  3991. * fit into the preferences read/write API. Finally the remaining modules
  3992. * are written in alphabetical order (including of course the protocol preferences)
  3993. */
  3994. write_gui_pref_info.pf = pf;
  3995. write_gui_pref_info.is_gui_module = TRUE;
  3996. write_module_prefs(gui_module, &write_gui_pref_info);
  3997. {
  3998. struct filter_expression *fe = *(struct filter_expression **)prefs.filter_expressions;
  3999. if (fe != NULL)
  4000. fprintf(pf, "\n####### Filter Expressions ########\n");
  4001. while (fe != NULL) {
  4002. if (fe->deleted == FALSE) {
  4003. fprintf(pf, "%s: %s\n", PRS_GUI_FILTER_LABEL, fe->label);
  4004. fprintf(pf, "%s: %s\n", PRS_GUI_FILTER_ENABLED,
  4005. fe->enabled == TRUE ? "TRUE" : "FALSE");
  4006. fprintf(pf, "%s: %s\n", PRS_GUI_FILTER_EXPR, fe->expression);
  4007. }
  4008. fe = fe->next;
  4009. }
  4010. }
  4011. write_gui_pref_info.is_gui_module = FALSE;
  4012. prefs_modules_foreach_submodules(NULL, write_module_prefs, &write_gui_pref_info);
  4013. fclose(pf);
  4014. /* XXX - catch I/O errors (e.g. "ran out of disk space") and return
  4015. an error indication, or maybe write to a new preferences file and
  4016. rename that file on top of the old one only if there are not I/O
  4017. errors. */
  4018. return 0;
  4019. }
  4020. /** The col_list is only partly managed by the custom preference API
  4021. * because its data is shared between multiple preferences, so
  4022. * it's freed here
  4023. */
  4024. static void
  4025. free_col_info(GList * list)
  4026. {
  4027. fmt_data *cfmt;
  4028. while (list != NULL) {
  4029. cfmt = (fmt_data *)list->data;
  4030. g_free(cfmt->title);
  4031. g_free(cfmt->custom_field);
  4032. g_free(cfmt);
  4033. list = g_list_remove_link(list, list);
  4034. }
  4035. g_list_free(list);
  4036. list = NULL;
  4037. }
  4038. /*
  4039. * Editor modelines
  4040. *
  4041. * Local Variables:
  4042. * c-basic-offset: 4
  4043. * tab-width: 8
  4044. * indent-tabs-mode: nil
  4045. * End:
  4046. *
  4047. * ex: set shiftwidth=4 tabstop=8 expandtab:
  4048. * :indentSize=4:tabSize=8:noTabs=true:
  4049. */