| File: | builds/wireshark/wireshark/wsutil/filter_files.c |
| Warning: | line 164, column 21 Read function called when stream is in EOF state. Function has no effect |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* filter_files.c | |||
| 2 | * Code for reading and writing the filters file. | |||
| 3 | * | |||
| 4 | * Wireshark - Network traffic analyzer | |||
| 5 | * By Gerald Combs <gerald@wireshark.org> | |||
| 6 | * Copyright 1998 Gerald Combs | |||
| 7 | * | |||
| 8 | * SPDX-License-Identifier: GPL-2.0-or-later | |||
| 9 | */ | |||
| 10 | ||||
| 11 | #include "config.h" | |||
| 12 | #define WS_LOG_DOMAIN"WSUtil" LOG_DOMAIN_WSUTIL"WSUtil" | |||
| 13 | #include "filter_files.h" | |||
| 14 | ||||
| 15 | #include <stdio.h> | |||
| 16 | #include <string.h> | |||
| 17 | #include <errno(*__errno_location ()).h> | |||
| 18 | ||||
| 19 | #include <wsutil/file_util.h> | |||
| 20 | #include <wsutil/filesystem.h> | |||
| 21 | #include <wsutil/profile_inherit.h> | |||
| 22 | #include <wsutil/report_message.h> | |||
| 23 | #include <wsutil/wslog.h> | |||
| 24 | #include <wsutil/ws_assert.h> | |||
| 25 | ||||
| 26 | /* | |||
| 27 | * Read in a list of filters. | |||
| 28 | * | |||
| 29 | * On error, report the error via the UI. | |||
| 30 | */ | |||
| 31 | ||||
| 32 | #define INIT_BUF_SIZE128 128 | |||
| 33 | ||||
| 34 | static GList * | |||
| 35 | add_filter_entry(GList *fl, const char *filt_name, const char *filt_expr) | |||
| 36 | { | |||
| 37 | filter_def *filt; | |||
| 38 | ||||
| 39 | filt = g_new(filter_def, 1)((filter_def *) g_malloc_n ((1), sizeof (filter_def))); | |||
| 40 | filt->name = g_strdup(filt_name)g_strdup_inline (filt_name); | |||
| 41 | filt->strval = g_strdup(filt_expr)g_strdup_inline (filt_expr); | |||
| 42 | filt->source_profile = NULL((void*)0); | |||
| 43 | return g_list_prepend(fl, filt); | |||
| 44 | } | |||
| 45 | ||||
| 46 | static void | |||
| 47 | free_filter_entry(void * data) | |||
| 48 | { | |||
| 49 | filter_def *filt = (filter_def*)data; | |||
| 50 | g_free(filt->name)(__builtin_object_size ((filt->name), 0) != ((size_t) - 1) ) ? g_free_sized (filt->name, __builtin_object_size ((filt ->name), 0)) : (g_free) (filt->name); | |||
| 51 | g_free(filt->strval)(__builtin_object_size ((filt->strval), 0) != ((size_t) - 1 )) ? g_free_sized (filt->strval, __builtin_object_size ((filt ->strval), 0)) : (g_free) (filt->strval); | |||
| 52 | g_free(filt->source_profile)(__builtin_object_size ((filt->source_profile), 0) != ((size_t ) - 1)) ? g_free_sized (filt->source_profile, __builtin_object_size ((filt->source_profile), 0)) : (g_free) (filt->source_profile ); | |||
| 53 | g_free(filt)(__builtin_object_size ((filt), 0) != ((size_t) - 1)) ? g_free_sized (filt, __builtin_object_size ((filt), 0)) : (g_free) (filt); | |||
| 54 | } | |||
| 55 | ||||
| 56 | void ws_filter_list_free(filter_list_t *fl) | |||
| 57 | { | |||
| 58 | g_list_free_full(fl->list, free_filter_entry); | |||
| 59 | g_free(fl)(__builtin_object_size ((fl), 0) != ((size_t) - 1)) ? g_free_sized (fl, __builtin_object_size ((fl), 0)) : (g_free) (fl); | |||
| 60 | } | |||
| 61 | ||||
| 62 | static GList * | |||
| 63 | remove_filter_entry(GList *fl, GList *fl_entry) | |||
| 64 | { | |||
| 65 | filter_def *filt; | |||
| 66 | ||||
| 67 | filt = (filter_def *) fl_entry->data; | |||
| 68 | g_free(filt->name)(__builtin_object_size ((filt->name), 0) != ((size_t) - 1) ) ? g_free_sized (filt->name, __builtin_object_size ((filt ->name), 0)) : (g_free) (filt->name); | |||
| 69 | g_free(filt->strval)(__builtin_object_size ((filt->strval), 0) != ((size_t) - 1 )) ? g_free_sized (filt->strval, __builtin_object_size ((filt ->strval), 0)) : (g_free) (filt->strval); | |||
| 70 | g_free(filt->source_profile)(__builtin_object_size ((filt->source_profile), 0) != ((size_t ) - 1)) ? g_free_sized (filt->source_profile, __builtin_object_size ((filt->source_profile), 0)) : (g_free) (filt->source_profile ); | |||
| 71 | g_free(filt)(__builtin_object_size ((filt), 0) != ((size_t) - 1)) ? g_free_sized (filt, __builtin_object_size ((filt), 0)) : (g_free) (filt); | |||
| 72 | return g_list_remove_link(fl, fl_entry); | |||
| 73 | } | |||
| 74 | ||||
| 75 | static int | |||
| 76 | skip_whitespace(FILE *ff) | |||
| 77 | { | |||
| 78 | int c; | |||
| 79 | ||||
| 80 | while ((c = getc(ff)) != EOF(-1) && c != '\n' && g_ascii_isspace(c)((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)) | |||
| 81 | ; | |||
| 82 | return c; | |||
| 83 | } | |||
| 84 | ||||
| 85 | static int | |||
| 86 | getc_crlf(FILE *ff) | |||
| 87 | { | |||
| 88 | int c; | |||
| 89 | ||||
| 90 | c = getc(ff); | |||
| 91 | if (c == '\r') { | |||
| 92 | /* Treat CR-LF at the end of a line like LF, so that if we're reading | |||
| 93 | * a Windows-format file on UN*X, we handle it the same way we'd handle | |||
| 94 | * a UN*X-format file. */ | |||
| 95 | c = getc(ff); | |||
| 96 | if (c != EOF(-1) && c != '\n') { | |||
| 97 | /* Put back the character after the CR, and process the CR normally. */ | |||
| 98 | ungetc(c, ff); | |||
| 99 | c = '\r'; | |||
| 100 | } | |||
| 101 | } | |||
| 102 | return c; | |||
| 103 | } | |||
| 104 | ||||
| 105 | static const char * | |||
| 106 | filter_list_file_name(filter_list_type_t list_type, const char **description) | |||
| 107 | { | |||
| 108 | switch (list_type) { | |||
| 109 | ||||
| 110 | case CFILTER_LIST: | |||
| 111 | *description = "capture filter"; | |||
| 112 | return CFILTER_FILE_NAME"cfilters"; | |||
| 113 | ||||
| 114 | case DFILTER_LIST: | |||
| 115 | *description = "display filter"; | |||
| 116 | return DFILTER_FILE_NAME"dfilters"; | |||
| 117 | ||||
| 118 | case DMACROS_LIST: | |||
| 119 | *description = "display filter macro"; | |||
| 120 | return DMACROS_FILE_NAME"dmacros"; | |||
| 121 | } | |||
| 122 | ||||
| 123 | ws_assert_not_reached()ws_log_fatal_full("WSUtil", LOG_LEVEL_ERROR, "wsutil/filter_files.c" , 123, __func__, "assertion \"not reached\" failed"); | |||
| 124 | } | |||
| 125 | ||||
| 126 | /* | |||
| 127 | * Read the filters of an open file, prepending them to flp. | |||
| 128 | */ | |||
| 129 | static GList * | |||
| 130 | read_filter_file(FILE *ff, const char *ff_path, const char *ff_description, GList *flp) | |||
| 131 | { | |||
| 132 | int c; | |||
| 133 | char *filt_name, *filt_expr; | |||
| 134 | size_t filt_name_len, filt_expr_len; | |||
| 135 | size_t filt_name_index, filt_expr_index; | |||
| 136 | int line = 1; | |||
| 137 | ||||
| 138 | /* Allocate the filter name buffer. */ | |||
| 139 | filt_name_len = INIT_BUF_SIZE128; | |||
| 140 | filt_name = (char *)g_malloc(filt_name_len + 1); | |||
| 141 | filt_expr_len = INIT_BUF_SIZE128; | |||
| 142 | filt_expr = (char *)g_malloc(filt_expr_len + 1); | |||
| 143 | ||||
| 144 | for (line = 1; ; line++) { | |||
| 145 | /* Lines in a filter file are of the form | |||
| 146 | ||||
| 147 | "name" expression | |||
| 148 | ||||
| 149 | where "name" is a name, in quotes - backslashes in the name | |||
| 150 | escape the next character, so quotes and backslashes can appear | |||
| 151 | in the name - and "expression" is a filter expression, not in | |||
| 152 | quotes, running to the end of the line. */ | |||
| 153 | ||||
| 154 | /* Skip over leading white space, if any. */ | |||
| 155 | c = skip_whitespace(ff); | |||
| 156 | ||||
| 157 | if (c == EOF(-1)) | |||
| 158 | break; /* Nothing more to read */ | |||
| 159 | if (c == '\n') | |||
| 160 | continue; /* Blank line. */ | |||
| 161 | if (c == '#') { | |||
| 162 | /* Comment. */ | |||
| 163 | while (c != '\n') | |||
| 164 | c = getc(ff); /* skip to the end of the line */ | |||
| ||||
| 165 | continue; | |||
| 166 | } | |||
| 167 | ||||
| 168 | /* "c" is the first non-white-space character. | |||
| 169 | If it's not a quote, it's an error. */ | |||
| 170 | if (c != '"') { | |||
| 171 | ws_warning("'%s' line %d doesn't have a quoted filter name.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 172, __func__, "'%s' line %d doesn't have a quoted filter name." , ff_path, line); } } while (0) | |||
| 172 | line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 172, __func__, "'%s' line %d doesn't have a quoted filter name." , ff_path, line); } } while (0); | |||
| 173 | while (c != '\n') | |||
| 174 | c = getc(ff); /* skip to the end of the line */ | |||
| 175 | continue; | |||
| 176 | } | |||
| 177 | ||||
| 178 | /* Get the name of the filter. */ | |||
| 179 | filt_name_index = 0; | |||
| 180 | for (;;) { | |||
| 181 | c = getc_crlf(ff); | |||
| 182 | if (c == EOF(-1) || c == '\n') | |||
| 183 | break; /* End of line - or end of file */ | |||
| 184 | if (c == '"') { | |||
| 185 | /* Closing quote. */ | |||
| 186 | if (filt_name_index >= filt_name_len) { | |||
| 187 | /* Filter name buffer isn't long enough; double its length. */ | |||
| 188 | filt_name_len *= 2; | |||
| 189 | filt_name = (char *)g_realloc(filt_name, filt_name_len + 1); | |||
| 190 | } | |||
| 191 | filt_name[filt_name_index] = '\0'; | |||
| 192 | break; | |||
| 193 | } | |||
| 194 | if (c == '\\') { | |||
| 195 | /* Next character is escaped */ | |||
| 196 | c = getc_crlf(ff); | |||
| 197 | if (c == EOF(-1) || c == '\n') | |||
| 198 | break; /* End of line - or end of file */ | |||
| 199 | } | |||
| 200 | /* Add this character to the filter name string. */ | |||
| 201 | if (filt_name_index >= filt_name_len) { | |||
| 202 | /* Filter name buffer isn't long enough; double its length. */ | |||
| 203 | filt_name_len *= 2; | |||
| 204 | filt_name = (char *)g_realloc(filt_name, filt_name_len + 1); | |||
| 205 | } | |||
| 206 | filt_name[filt_name_index] = c; | |||
| 207 | filt_name_index++; | |||
| 208 | } | |||
| 209 | ||||
| 210 | if (c == EOF(-1)) { | |||
| 211 | if (!ferror(ff)) { | |||
| 212 | /* EOF, not error; no newline seen before EOF */ | |||
| 213 | ws_warning("'%s' line %d doesn't have a newline.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 214, __func__, "'%s' line %d doesn't have a newline.", ff_path , line); } } while (0) | |||
| 214 | line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 214, __func__, "'%s' line %d doesn't have a newline.", ff_path , line); } } while (0); | |||
| 215 | } | |||
| 216 | break; /* nothing more to read */ | |||
| 217 | } | |||
| 218 | ||||
| 219 | if (c != '"') { | |||
| 220 | /* No newline seen before end-of-line */ | |||
| 221 | ws_warning("'%s' line %d doesn't have a closing quote.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 222, __func__, "'%s' line %d doesn't have a closing quote." , ff_path, line); } } while (0) | |||
| 222 | line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 222, __func__, "'%s' line %d doesn't have a closing quote." , ff_path, line); } } while (0); | |||
| 223 | continue; | |||
| 224 | } | |||
| 225 | ||||
| 226 | /* Skip over separating white space, if any. */ | |||
| 227 | c = skip_whitespace(ff); | |||
| 228 | ||||
| 229 | if (c == EOF(-1)) { | |||
| 230 | if (!ferror(ff)) { | |||
| 231 | /* EOF, not error; no newline seen before EOF */ | |||
| 232 | ws_warning("'%s' line %d doesn't have a newline.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 233, __func__, "'%s' line %d doesn't have a newline.", ff_path , line); } } while (0) | |||
| 233 | line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 233, __func__, "'%s' line %d doesn't have a newline.", ff_path , line); } } while (0); | |||
| 234 | } | |||
| 235 | break; /* nothing more to read */ | |||
| 236 | } | |||
| 237 | ||||
| 238 | if (c == '\n') { | |||
| 239 | /* No filter expression */ | |||
| 240 | ws_warning("'%s' line %d doesn't have a filter expression.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 241, __func__, "'%s' line %d doesn't have a filter expression." , ff_path, line); } } while (0) | |||
| 241 | line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 241, __func__, "'%s' line %d doesn't have a filter expression." , ff_path, line); } } while (0); | |||
| 242 | continue; | |||
| 243 | } | |||
| 244 | ||||
| 245 | /* "c" is the first non-white-space character; it's the first | |||
| 246 | character of the filter expression. */ | |||
| 247 | filt_expr_index = 0; | |||
| 248 | for (;;) { | |||
| 249 | /* Add this character to the filter expression string. */ | |||
| 250 | if (filt_expr_index >= filt_expr_len) { | |||
| 251 | /* Filter expression buffer isn't long enough; double its length. */ | |||
| 252 | filt_expr_len *= 2; | |||
| 253 | filt_expr = (char *)g_realloc(filt_expr, filt_expr_len + 1); | |||
| 254 | } | |||
| 255 | filt_expr[filt_expr_index] = c; | |||
| 256 | filt_expr_index++; | |||
| 257 | ||||
| 258 | /* Get the next character. */ | |||
| 259 | c = getc_crlf(ff); | |||
| 260 | if (c == EOF(-1) || c == '\n') | |||
| 261 | break; | |||
| 262 | } | |||
| 263 | ||||
| 264 | if (c == EOF(-1)) { | |||
| 265 | if (!ferror(ff)) { | |||
| 266 | /* EOF, not error; no newline seen before EOF */ | |||
| 267 | ws_warning("'%s' line %d doesn't have a newline.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 268, __func__, "'%s' line %d doesn't have a newline.", ff_path , line); } } while (0) | |||
| 268 | line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c" , 268, __func__, "'%s' line %d doesn't have a newline.", ff_path , line); } } while (0); | |||
| 269 | } | |||
| 270 | break; /* nothing more to read */ | |||
| 271 | } | |||
| 272 | ||||
| 273 | /* We saw the ending newline; terminate the filter expression string */ | |||
| 274 | if (filt_expr_index >= filt_expr_len) { | |||
| 275 | /* Filter expression buffer isn't long enough; double its length. */ | |||
| 276 | filt_expr_len *= 2; | |||
| 277 | filt_expr = (char *)g_realloc(filt_expr, filt_expr_len + 1); | |||
| 278 | } | |||
| 279 | filt_expr[filt_expr_index] = '\0'; | |||
| 280 | ||||
| 281 | /* Add the new filter to the list of filters */ | |||
| 282 | flp = add_filter_entry(flp, filt_name, filt_expr); | |||
| 283 | } | |||
| 284 | if (ferror(ff)) { | |||
| 285 | report_warning("Error reading your %s file\n\"%s\": %s.", | |||
| 286 | ff_description, ff_path, g_strerror(errno(*__errno_location ()))); | |||
| 287 | } | |||
| 288 | g_free(filt_name)(__builtin_object_size ((filt_name), 0) != ((size_t) - 1)) ? g_free_sized (filt_name, __builtin_object_size ((filt_name), 0)) : (g_free ) (filt_name); | |||
| 289 | g_free(filt_expr)(__builtin_object_size ((filt_expr), 0) != ((size_t) - 1)) ? g_free_sized (filt_expr, __builtin_object_size ((filt_expr), 0)) : (g_free ) (filt_expr); | |||
| 290 | return flp; | |||
| 291 | } | |||
| 292 | ||||
| 293 | /* | |||
| 294 | * Read the profile's own filters. Only a profile that does not include its parent profiles | |||
| 295 | * falls back to the global file. | |||
| 296 | */ | |||
| 297 | static filter_list_t * | |||
| 298 | filter_list_read_own(filter_list_type_t list_type, const char* app_env_var_prefix, bool_Bool include_parent) | |||
| 299 | { | |||
| 300 | const char *ff_name, *ff_description; | |||
| 301 | char *ff_path; | |||
| 302 | FILE *ff; | |||
| 303 | ||||
| 304 | filter_list_t *list = g_new(filter_list_t, 1)((filter_list_t *) g_malloc_n ((1), sizeof (filter_list_t))); | |||
| 305 | list->type = list_type; | |||
| 306 | list->list = NULL((void*)0); | |||
| 307 | ||||
| 308 | ff_name = filter_list_file_name(list_type, &ff_description); | |||
| 309 | ||||
| 310 | /* try to open personal "cfilters"/"dfilters" file */ | |||
| 311 | ff_path = get_persconffile_path(ff_name, true1, app_env_var_prefix); | |||
| 312 | if ((ff = ws_fopenfopen(ff_path, "r")) == NULL((void*)0)) { | |||
| 313 | /* | |||
| 314 | * Did that fail because the file didn't exist? | |||
| 315 | */ | |||
| 316 | if (errno(*__errno_location ()) != ENOENT2) { | |||
| 317 | /* | |||
| 318 | * No. Just give up. | |||
| 319 | */ | |||
| 320 | report_warning("Could not open your %s file\n\"%s\": %s.", | |||
| 321 | ff_description, ff_path, g_strerror(errno(*__errno_location ()))); | |||
| 322 | g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized (ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) ( ff_path); | |||
| 323 | return list; | |||
| 324 | } | |||
| 325 | ||||
| 326 | g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized (ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) ( ff_path); | |||
| 327 | if (include_parent) { | |||
| 328 | /* The global filters come from the parent profiles */ | |||
| 329 | return list; | |||
| 330 | } | |||
| 331 | ||||
| 332 | /* | |||
| 333 | * Yes. Try to open the global "cfilters/dfilters" file. | |||
| 334 | */ | |||
| 335 | ff_path = get_datafile_path(ff_name, app_env_var_prefix); | |||
| 336 | if ((ff = ws_fopenfopen(ff_path, "r")) == NULL((void*)0)) { | |||
| 337 | /* | |||
| 338 | * Well, that didn't work, either. Just give up. | |||
| 339 | * Report an error if the file existed but we couldn't open it. | |||
| 340 | */ | |||
| 341 | if (errno(*__errno_location ()) != ENOENT2) { | |||
| 342 | report_warning("Could not open your %s file\n\"%s\": %s.", | |||
| 343 | ff_description, ff_path, g_strerror(errno(*__errno_location ()))); | |||
| 344 | } | |||
| 345 | g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized (ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) ( ff_path); | |||
| 346 | return list; | |||
| 347 | } | |||
| 348 | } | |||
| 349 | ||||
| 350 | list->list = read_filter_file(ff, ff_path, ff_description, NULL((void*)0)); | |||
| 351 | g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized (ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) ( ff_path); | |||
| 352 | fclose(ff); | |||
| 353 | return list; | |||
| 354 | } | |||
| 355 | ||||
| 356 | typedef struct { | |||
| 357 | filter_list_type_t type; | |||
| 358 | const char *description; | |||
| 359 | GList *entries; /* The inherited filters, in reverse order */ | |||
| 360 | } read_layer_data_t; | |||
| 361 | ||||
| 362 | /* A macro is found by its name, a filter also by its expression */ | |||
| 363 | static const char * | |||
| 364 | filter_list_key2(filter_list_type_t list_type, const filter_def *filt) | |||
| 365 | { | |||
| 366 | return list_type == DMACROS_LIST ? NULL((void*)0) : filt->strval; | |||
| 367 | } | |||
| 368 | ||||
| 369 | /* Read the filters of one parent profile */ | |||
| 370 | static bool_Bool | |||
| 371 | read_layer_filters(const profile_inherit_layer_t *layer, const char *path, const char *filename _U___attribute__((unused)), | |||
| 372 | profile_inherit_seen_t *seen, void *user_data, char **err) | |||
| 373 | { | |||
| 374 | read_layer_data_t *data = (read_layer_data_t *)user_data; | |||
| 375 | FILE *ff; | |||
| 376 | ||||
| 377 | if ((ff = ws_fopenfopen(path, "r")) == NULL((void*)0)) { | |||
| 378 | *err = ws_strdup_printf("Could not open the %s file \"%s\": %s.",wmem_strdup_printf(((void*)0), "Could not open the %s file \"%s\": %s." , data->description, path, g_strerror((*__errno_location ( )))) | |||
| 379 | data->description, path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open the %s file \"%s\": %s." , data->description, path, g_strerror((*__errno_location ( )))); | |||
| 380 | return false0; | |||
| 381 | } | |||
| 382 | /* In the order of the file */ | |||
| 383 | GList *layer_list = g_list_reverse(read_filter_file(ff, path, data->description, NULL((void*)0))); | |||
| 384 | fclose(ff); | |||
| 385 | ||||
| 386 | for (GList *item = layer_list; item; item = g_list_next(item)((item) ? (((GList *)(item))->next) : ((void*)0))) { | |||
| 387 | filter_def *filt = (filter_def *)item->data; | |||
| 388 | ||||
| 389 | if (profile_inherit_seen_check(seen, filt->name, filter_list_key2(data->type, filt))) { | |||
| 390 | free_filter_entry(filt); | |||
| 391 | continue; | |||
| 392 | } | |||
| 393 | filt->source_profile = g_strdup(layer->source)g_strdup_inline (layer->source); | |||
| 394 | data->entries = g_list_prepend(data->entries, filt); | |||
| 395 | } | |||
| 396 | g_list_free(layer_list); | |||
| 397 | return true1; | |||
| 398 | } | |||
| 399 | ||||
| 400 | filter_list_t * | |||
| 401 | ws_filter_list_read(filter_list_type_t list_type, const char* app_env_var_prefix) | |||
| 402 | { | |||
| 403 | const char *ff_name, *ff_description; | |||
| 404 | ||||
| 405 | ff_name = filter_list_file_name(list_type, &ff_description); | |||
| 406 | bool_Bool include_parent = profile_get_include_parent(app_env_var_prefix, NULL((void*)0), ff_name); | |||
| 407 | ||||
| 408 | filter_list_t *list = filter_list_read_own(list_type, app_env_var_prefix, include_parent); | |||
| 409 | if (!include_parent) | |||
| 410 | return list; | |||
| 411 | ||||
| 412 | /* The profile's own filters come first, and win over duplicates from parents */ | |||
| 413 | profile_inherit_seen_t *seen = profile_inherit_seen_new(); | |||
| 414 | for (GList *item = list->list; item; item = g_list_next(item)((item) ? (((GList *)(item))->next) : ((void*)0))) { | |||
| 415 | const filter_def *filt = (const filter_def *)item->data; | |||
| 416 | profile_inherit_seen_check(seen, filt->name, filter_list_key2(list_type, filt)); | |||
| 417 | } | |||
| 418 | profile_inherit_seen_next(seen); | |||
| 419 | ||||
| 420 | read_layer_data_t data = { list_type, ff_description, NULL((void*)0) }; | |||
| 421 | char *err = NULL((void*)0); | |||
| 422 | if (!profile_inherit_read_layers(app_env_var_prefix, ff_name, NULL((void*)0), true1, true1, seen, | |||
| 423 | read_layer_filters, &data, &err)) { | |||
| 424 | report_warning("%s", err); | |||
| 425 | } | |||
| 426 | g_free(err)(__builtin_object_size ((err), 0) != ((size_t) - 1)) ? g_free_sized (err, __builtin_object_size ((err), 0)) : (g_free) (err); | |||
| 427 | profile_inherit_seen_free(seen); | |||
| 428 | ||||
| 429 | list->list = g_list_concat(list->list, g_list_reverse(data.entries)); | |||
| 430 | return list; | |||
| 431 | } | |||
| 432 | ||||
| 433 | filter_list_t * | |||
| 434 | ws_filter_list_read_own(filter_list_type_t list_type, const char* app_env_var_prefix) | |||
| 435 | { | |||
| 436 | return filter_list_read_own(list_type, app_env_var_prefix, false0); | |||
| ||||
| 437 | } | |||
| 438 | ||||
| 439 | /* | |||
| 440 | * Add a new filter to the end of a list. | |||
| 441 | */ | |||
| 442 | void | |||
| 443 | ws_filter_list_add(filter_list_t *fl, const char *name, | |||
| 444 | const char *expression) | |||
| 445 | { | |||
| 446 | fl->list = add_filter_entry(fl->list, name, expression); | |||
| 447 | } | |||
| 448 | ||||
| 449 | static int | |||
| 450 | compare_def(const void *def, const void *name) | |||
| 451 | { | |||
| 452 | return g_strcmp0(((filter_def *)def)->name, name); | |||
| 453 | } | |||
| 454 | ||||
| 455 | GList *ws_filter_list_find(filter_list_t *list, const char *name) | |||
| 456 | { | |||
| 457 | return g_list_find_custom(list->list, name, compare_def); | |||
| 458 | } | |||
| 459 | ||||
| 460 | /* | |||
| 461 | * Remove a filter from a list. | |||
| 462 | */ | |||
| 463 | bool_Bool | |||
| 464 | ws_filter_list_remove(filter_list_t *list, const char *name) | |||
| 465 | { | |||
| 466 | GList *p; | |||
| 467 | ||||
| 468 | p = g_list_find_custom(list->list, name, compare_def); | |||
| 469 | if (p == NULL((void*)0)) | |||
| 470 | return false0; | |||
| 471 | list->list = remove_filter_entry(list->list, p); | |||
| 472 | return true1; | |||
| 473 | } | |||
| 474 | ||||
| 475 | /* | |||
| 476 | * Write out a list of filters. | |||
| 477 | * | |||
| 478 | * On error, report the error via the UI. | |||
| 479 | */ | |||
| 480 | void | |||
| 481 | ws_filter_list_write(filter_list_t *list, const char* app_env_var_prefix) | |||
| 482 | { | |||
| 483 | char *pf_dir_path; | |||
| 484 | const char *ff_name, *ff_description; | |||
| 485 | char *ff_path, *ff_path_new; | |||
| 486 | GList *fl; | |||
| 487 | GList *flpp; | |||
| 488 | filter_def *filt; | |||
| 489 | FILE *ff; | |||
| 490 | unsigned char *p, c; | |||
| 491 | ||||
| 492 | ff_name = filter_list_file_name(list->type, &ff_description); | |||
| 493 | fl = list->list; | |||
| 494 | ||||
| 495 | /* Create the directory that holds personal configuration files, | |||
| 496 | if necessary. */ | |||
| 497 | if (create_persconffile_dir(app_env_var_prefix, &pf_dir_path) == -1) { | |||
| 498 | report_failure("Can't create directory\n\"%s\"\nfor filter files: %s.", | |||
| 499 | pf_dir_path, g_strerror(errno(*__errno_location ()))); | |||
| 500 | g_free(pf_dir_path)(__builtin_object_size ((pf_dir_path), 0) != ((size_t) - 1)) ? g_free_sized (pf_dir_path, __builtin_object_size ((pf_dir_path ), 0)) : (g_free) (pf_dir_path); | |||
| 501 | return; | |||
| 502 | } | |||
| 503 | ||||
| 504 | ff_path = get_persconffile_path(ff_name, true1, app_env_var_prefix); | |||
| 505 | ||||
| 506 | /* Write to "XXX.new", and rename if that succeeds. | |||
| 507 | That means we don't trash the file if we fail to write it out | |||
| 508 | completely. */ | |||
| 509 | ff_path_new = ws_strdup_printf("%s.new", ff_path)wmem_strdup_printf(((void*)0), "%s.new", ff_path); | |||
| 510 | ||||
| 511 | if ((ff = ws_fopenfopen(ff_path_new, "w")) == NULL((void*)0)) { | |||
| 512 | /* We had an error saving the filter. */ | |||
| 513 | report_failure("Error saving your %s file\nCouldn't open \"%s\": %s.", | |||
| 514 | ff_description, ff_path_new, g_strerror(errno(*__errno_location ()))); | |||
| 515 | g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ? g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new ), 0)) : (g_free) (ff_path_new); | |||
| 516 | g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized (ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) ( ff_path); | |||
| 517 | return; | |||
| 518 | } | |||
| 519 | flpp = g_list_first(fl); | |||
| 520 | while (flpp) { | |||
| 521 | filt = (filter_def *) flpp->data; | |||
| 522 | ||||
| 523 | /* Filters inherited from parent profiles are not written to this profile */ | |||
| 524 | if (filt->source_profile != NULL((void*)0)) { | |||
| 525 | flpp = flpp->next; | |||
| 526 | continue; | |||
| 527 | } | |||
| 528 | ||||
| 529 | /* Write out the filter name as a quoted string; escape any quotes | |||
| 530 | or backslashes. */ | |||
| 531 | putc('"', ff); | |||
| 532 | for (p = (unsigned char *)filt->name; (c = *p) != '\0'; p++) { | |||
| 533 | if (c == '"' || c == '\\') | |||
| 534 | putc('\\', ff); | |||
| 535 | putc(c, ff); | |||
| 536 | } | |||
| 537 | putc('"', ff); | |||
| 538 | ||||
| 539 | /* Separate the filter name and value with a space. */ | |||
| 540 | putc(' ', ff); | |||
| 541 | ||||
| 542 | /* Write out the filter expression and a newline. */ | |||
| 543 | fprintf(ff, "%s\n", filt->strval); | |||
| 544 | if (ferror(ff)) { | |||
| 545 | report_failure("Error saving your %s file\nWrite to \"%s\" failed: %s.", | |||
| 546 | ff_description, ff_path_new, g_strerror(errno(*__errno_location ()))); | |||
| 547 | fclose(ff); | |||
| 548 | ws_unlinkunlink(ff_path_new); | |||
| 549 | g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ? g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new ), 0)) : (g_free) (ff_path_new); | |||
| 550 | g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized (ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) ( ff_path); | |||
| 551 | return; | |||
| 552 | } | |||
| 553 | flpp = flpp->next; | |||
| 554 | } | |||
| 555 | if (fclose(ff) == EOF(-1)) { | |||
| 556 | report_failure("Error saving your %s file\nWrite to \"%s\" failed: %s.", | |||
| 557 | ff_description, ff_path_new, g_strerror(errno(*__errno_location ()))); | |||
| 558 | ws_unlinkunlink(ff_path_new); | |||
| 559 | g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ? g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new ), 0)) : (g_free) (ff_path_new); | |||
| 560 | g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized (ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) ( ff_path); | |||
| 561 | return; | |||
| 562 | } | |||
| 563 | ||||
| 564 | #ifdef _WIN32 | |||
| 565 | /* ANSI C doesn't say whether "rename()" removes the target if it | |||
| 566 | exists; the Win32 call to rename files doesn't do so, which I | |||
| 567 | infer is the reason why the MSVC++ "rename()" doesn't do so. | |||
| 568 | We must therefore remove the target file first, on Windows. | |||
| 569 | ||||
| 570 | XXX - ws_rename() is g_rename() on Windows, and g_rename() | |||
| 571 | uses MoveFileEx() with MOVEFILE_REPLACE_EXISTING, | |||
| 572 | so it should remove the target if it exists, so this stuff | |||
| 573 | shouldn't be necessary. Perhaps it dates back to when we were | |||
| 574 | calling rename(), with that being a wrapper around Microsoft's | |||
| 575 | _rename(), which didn't remove the target. */ | |||
| 576 | if (ws_removeremove(ff_path) < 0 && errno(*__errno_location ()) != ENOENT2) { | |||
| 577 | /* It failed for some reason other than "it's not there"; if | |||
| 578 | it's not there, we don't need to remove it, so we just | |||
| 579 | drive on. */ | |||
| 580 | report_failure("Error saving your %s file\nCouldn't remove \"%s\": %s.", | |||
| 581 | ff_description, ff_path, g_strerror(errno(*__errno_location ()))); | |||
| 582 | ws_unlinkunlink(ff_path_new); | |||
| 583 | g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ? g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new ), 0)) : (g_free) (ff_path_new); | |||
| 584 | g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized (ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) ( ff_path); | |||
| 585 | return; | |||
| 586 | } | |||
| 587 | #endif | |||
| 588 | ||||
| 589 | if (ws_renamerename(ff_path_new, ff_path) < 0) { | |||
| 590 | report_failure("Error saving your %s file\nCouldn't rename \"%s\" to \"%s\": %s.", | |||
| 591 | ff_description, ff_path_new, ff_path, g_strerror(errno(*__errno_location ()))); | |||
| 592 | ws_unlinkunlink(ff_path_new); | |||
| 593 | g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ? g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new ), 0)) : (g_free) (ff_path_new); | |||
| 594 | g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized (ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) ( ff_path); | |||
| 595 | return; | |||
| 596 | } | |||
| 597 | g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ? g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new ), 0)) : (g_free) (ff_path_new); | |||
| 598 | g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized (ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) ( ff_path); | |||
| 599 | } |