forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.c
263 lines (226 loc) · 6.95 KB
/
dump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* @file
* Dump all the config
*
* @authors
* Copyright (C) 2017-2018 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page config_dump Dump all the config
*
* Dump all the config items in various formats.
*/
#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "dump.h"
#include "set.h"
#include "subset.h"
#include "types.h"
void mutt_pretty_mailbox(char *buf, size_t buflen);
/**
* escape_string - Write a string to a buffer, escaping special characters
* @param buf Buffer to write to
* @param src String to write
* @retval num Bytes written to buffer
*/
size_t escape_string(struct Buffer *buf, const char *src)
{
if (!buf || !src)
return 0;
size_t len = 0;
for (; *src; src++)
{
switch (*src)
{
case '\007':
len += mutt_buffer_addstr(buf, "\\g");
break;
case '\n':
len += mutt_buffer_addstr(buf, "\\n");
break;
case '\r':
len += mutt_buffer_addstr(buf, "\\r");
break;
case '\t':
len += mutt_buffer_addstr(buf, "\\t");
break;
default:
if ((*src == '\\') || (*src == '"'))
len += mutt_buffer_addch(buf, '\\');
len += mutt_buffer_addch(buf, src[0]);
}
}
return len;
}
/**
* pretty_var - Escape and stringify a config item value
* @param str String to escape
* @param buf Buffer to write to
* @retval num Number of bytes written to buffer
*/
size_t pretty_var(const char *str, struct Buffer *buf)
{
if (!buf || !str)
return 0;
int len = 0;
len += mutt_buffer_addch(buf, '"');
len += escape_string(buf, str);
len += mutt_buffer_addch(buf, '"');
return len;
}
/**
* dump_config_neo - Dump the config in the style of NeoMutt
* @param cs Config items
* @param he HashElem representing config item
* @param value Current value of the config item
* @param initial Initial value of the config item
* @param flags Flags, see #ConfigDumpFlags
* @param fp File pointer to write to
*/
void dump_config_neo(struct ConfigSet *cs, struct HashElem *he, struct Buffer *value,
struct Buffer *initial, ConfigDumpFlags flags, FILE *fp)
{
if (!he || !value || !fp)
return;
const char *name = he->key.strkey;
if ((flags & CS_DUMP_ONLY_CHANGED) &&
(!initial || mutt_str_equal(value->data, initial->data)))
{
return;
}
if (he->type == DT_SYNONYM)
{
const struct ConfigDef *cdef = he->data;
const char *syn = (const char *) cdef->initial;
fprintf(fp, "# synonym: %s -> %s\n", name, syn);
return;
}
if (flags & CS_DUMP_SHOW_DOCS)
{
const struct ConfigDef *cdef = he->data;
fprintf(fp, "# %s\n", cdef->docs);
}
bool show_name = !(flags & CS_DUMP_HIDE_NAME);
bool show_value = !(flags & CS_DUMP_HIDE_VALUE);
if (show_name && show_value)
fprintf(fp, "set ");
if (show_name)
fprintf(fp, "%s", name);
if (show_name && show_value)
fprintf(fp, " = ");
if (show_value)
fprintf(fp, "%s", value->data);
if (show_name || show_value)
fprintf(fp, "\n");
if (flags & CS_DUMP_SHOW_DEFAULTS)
{
const struct ConfigSetType *cst = cs_get_type_def(cs, he->type);
if (cst)
fprintf(fp, "# %s %s %s\n", cst->name, name, value->data);
}
if (flags & CS_DUMP_SHOW_DOCS)
fprintf(fp, "\n");
}
/**
* dump_config - Write all the config to a file
* @param cs ConfigSet to dump
* @param flags Flags, see #ConfigDumpFlags
* @param fp File to write config to
*/
bool dump_config(struct ConfigSet *cs, ConfigDumpFlags flags, FILE *fp)
{
if (!cs)
return false;
struct HashElem *he = NULL;
struct HashElem **list = get_elem_list(cs);
if (!list)
return false; /* LCOV_EXCL_LINE */
bool result = true;
struct Buffer value = mutt_buffer_make(256);
struct Buffer initial = mutt_buffer_make(256);
struct Buffer tmp = mutt_buffer_make(256);
for (size_t i = 0; list[i]; i++)
{
mutt_buffer_reset(&value);
mutt_buffer_reset(&initial);
he = list[i];
const int type = DTYPE(he->type);
if ((type == DT_SYNONYM) && !(flags & CS_DUMP_SHOW_SYNONYMS))
continue;
if ((he->type & DT_DEPRECATED) && !(flags & CS_DUMP_SHOW_DEPRECATED))
continue;
// if ((type == DT_DISABLED) && !(flags & CS_DUMP_SHOW_DISABLED))
// continue;
if (type != DT_SYNONYM)
{
/* If necessary, get the current value */
if ((flags & CS_DUMP_ONLY_CHANGED) || !(flags & CS_DUMP_HIDE_VALUE) ||
(flags & CS_DUMP_SHOW_DEFAULTS))
{
int rc = cs_he_string_get(cs, he, &value);
if (CSR_RESULT(rc) != CSR_SUCCESS)
{
result = false; /* LCOV_EXCL_LINE */
break; /* LCOV_EXCL_LINE */
}
const struct ConfigDef *cdef = he->data;
if ((type == DT_STRING) && IS_SENSITIVE(*cdef) &&
(flags & CS_DUMP_HIDE_SENSITIVE) && !mutt_buffer_is_empty(&value))
{
mutt_buffer_reset(&value);
mutt_buffer_addstr(&value, "***");
}
if (((type == DT_PATH) || IS_MAILBOX(he)) && (value.data[0] == '/'))
mutt_pretty_mailbox(value.data, value.dsize);
if ((type != DT_BOOL) && (type != DT_NUMBER) && (type != DT_LONG) &&
(type != DT_QUAD) && !(flags & CS_DUMP_NO_ESCAPING))
{
mutt_buffer_reset(&tmp);
pretty_var(value.data, &tmp);
mutt_buffer_strcpy(&value, tmp.data);
}
}
/* If necessary, get the default value */
if (flags & (CS_DUMP_ONLY_CHANGED | CS_DUMP_SHOW_DEFAULTS))
{
int rc = cs_he_initial_get(cs, he, &initial);
if (CSR_RESULT(rc) != CSR_SUCCESS)
{
result = false; /* LCOV_EXCL_LINE */
break; /* LCOV_EXCL_LINE */
}
if (((type == DT_PATH) || IS_MAILBOX(he)) && !(he->type & DT_MAILBOX))
mutt_pretty_mailbox(initial.data, initial.dsize);
if ((type != DT_BOOL) && (type != DT_NUMBER) && (type != DT_LONG) &&
(type != DT_QUAD) && !(flags & CS_DUMP_NO_ESCAPING))
{
mutt_buffer_reset(&tmp);
pretty_var(initial.data, &tmp);
mutt_buffer_strcpy(&initial, tmp.data);
}
}
}
dump_config_neo(cs, he, &value, &initial, flags, fp);
}
FREE(&list);
mutt_buffer_dealloc(&value);
mutt_buffer_dealloc(&initial);
mutt_buffer_dealloc(&tmp);
return result;
}