Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / ui / language.c
blob38e5e1b2b3071e0b76ff802fddc1c4f06c856028
1 /* language.c
2 * Language "preference" handling routines
3 * Copyright 2014, Michal Labedzki for Tieto Corporation
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "config.h"
14 #include <stdlib.h>
15 #include <errno.h>
17 #include <epan/prefs.h>
18 #include <epan/prefs-int.h>
20 #include <wsutil/filesystem.h>
21 #include <wsutil/file_util.h>
23 #include "ui/language.h"
24 #include "ui/simple_dialog.h"
26 #define LANGUAGE_FILE_NAME "language"
27 #define LANGUAGE_PREF_LANGUAGE "language"
29 char *language;
31 /* set one user's recent common file key/value pair */
32 static prefs_set_pref_e
33 read_language_pref(char *key, const char *value,
34 void *private_data _U_, bool return_range_errors _U_)
36 if (strcmp(key, LANGUAGE_PREF_LANGUAGE) == 0) {
37 g_free(language);
39 * For backwards compatibility, treat "auto" as meaning "use the
40 * system language".
42 * To handle the old buggy code that didn't check whether "language"
43 * was null before trying to print it, treat "(null)" - which many,
44 * but *NOT* all, system printfs print for a null pointer (some
45 * printfs, such as the one in Solaris, *crash* with %s and a null
46 * pointer) - as meaning "use the system language".
48 if (!value || !*value || strcmp(value, "auto") == 0 ||
49 strcmp(value, "(null)") == 0)
50 language = g_strdup(USE_SYSTEM_LANGUAGE);
51 else
52 language = g_strdup(value);
55 return PREFS_SET_OK;
58 void
59 read_language_prefs(void)
61 char *rf_path;
62 FILE *rf;
64 rf_path = get_persconffile_path(LANGUAGE_FILE_NAME, false);
66 if ((rf = ws_fopen(rf_path, "r")) != NULL) {
67 read_prefs_file(rf_path, rf, read_language_pref, NULL);
69 fclose(rf);
72 g_free(rf_path);
75 bool
76 write_language_prefs(void)
78 char *pf_dir_path;
79 char *rf_path;
80 FILE *rf;
82 /* To do:
83 * - Split output lines longer than MAX_VAL_LEN
84 * - Create a function for the preference directory check/creation
85 * so that duplication can be avoided with filter.c
88 /* Create the directory that holds personal configuration files, if
89 necessary. */
90 if (create_persconffile_dir(&pf_dir_path) == -1) {
91 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
92 "Can't create directory\n\"%s\"\nfor language file: %s.", pf_dir_path,
93 g_strerror(errno));
94 g_free(pf_dir_path);
95 return false;
98 rf_path = get_persconffile_path(LANGUAGE_FILE_NAME, false);
99 if ((rf = ws_fopen(rf_path, "w")) == NULL) {
100 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
101 "Can't open recent file\n\"%s\": %s.", rf_path,
102 g_strerror(errno));
103 g_free(rf_path);
104 return false;
106 g_free(rf_path);
108 fputs("# Language settings file for Wireshark " VERSION ".\n"
109 "#\n"
110 "# This file is regenerated each time Wireshark is quit.\n"
111 "# So be careful, if you want to make manual changes here.\n"
112 "\n", rf);
114 fprintf(rf, LANGUAGE_PREF_LANGUAGE ": %s\n", language ? language : USE_SYSTEM_LANGUAGE);
116 fclose(rf);
118 return true;