NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
charset.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <stdbool.h>
33#include <stdint.h>
34#include <string.h>
35#include "mutt/lib.h"
36#include "charset.h"
37#include "set.h"
38#include "types.h"
39
45int charset_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef,
46 intptr_t value, struct Buffer *err)
47{
48 if (value == 0)
49 return CSR_SUCCESS;
50
51 const char *str = (const char *) value;
52
53 if ((cdef->type & D_CHARSET_SINGLE) && strchr(str, ':'))
54 {
55 buf_printf(err, _("'charset' must contain exactly one character set name"));
56 return CSR_ERR_INVALID;
57 }
58
59 int rc = CSR_SUCCESS;
60 bool strict = (cdef->type & D_CHARSET_STRICT);
61 char *q = NULL;
62 char *s = mutt_str_dup(str);
63
64 for (char *p = strtok_r(s, ":", &q); p; p = strtok_r(NULL, ":", &q))
65 {
66 if (*p == '\0')
67 continue; // LCOV_EXCL_LINE
68 if (!mutt_ch_check_charset(p, strict))
69 {
70 rc = CSR_ERR_INVALID;
71 buf_printf(err, _("Invalid value for option %s: %s"), cdef->name, p);
72 break;
73 }
74 }
75
76 FREE(&s);
77 return rc;
78}
79
85int charset_slist_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef,
86 intptr_t value, struct Buffer *err)
87{
88 if (value == 0)
89 return CSR_SUCCESS;
90
91 const struct Slist *list = (const struct Slist *) value;
92
93 int rc = CSR_SUCCESS;
94 bool strict = (cdef->type & D_CHARSET_STRICT);
95
96 const struct ListNode *np = NULL;
97 STAILQ_FOREACH(np, &list->head, entries)
98 {
99 char const *charset = np->data;
100 if (!mutt_ch_check_charset(charset, strict))
101 {
102 rc = CSR_ERR_INVALID;
103 buf_printf(err, _("Invalid value for option %s: %s"), cdef->name, charset);
104 break;
105 }
106 }
107
108 return rc;
109}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:38
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
int charset_slist_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate the multiple "charset" config variables - Implements ConfigDef::validator() -.
Definition: charset.c:85
int charset_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate the "charset" config variables - Implements ConfigDef::validator() -.
Definition: charset.c:45
#define FREE(x)
Definition: memory.h:45
bool mutt_ch_check_charset(const char *cs, bool strict)
Does iconv understand a character set?
Definition: charset.c:893
Conversion between different character encodings.
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
Parse the 'set' command.
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:64
const char * name
User-visible name.
Definition: set.h:65
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
Container for lots of config items.
Definition: set.h:252
A List node for strings.
Definition: list.h:35
char * data
String.
Definition: list.h:36
String list.
Definition: slist.h:37
struct ListHead head
List containing values.
Definition: slist.h:38
Constants for all the config types.
#define D_CHARSET_SINGLE
Flag for charset_validator to allow only one charset.
Definition: types.h:84
#define D_CHARSET_STRICT
Flag for charset_validator to use strict char check.
Definition: types.h:85