NeoMutt  2025-01-09-117-gace867
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 ConfigDef *cdef, intptr_t value, struct Buffer *err)
46{
47 if (value == 0)
48 return CSR_SUCCESS;
49
50 const char *str = (const char *) value;
51
52 if ((cdef->type & D_CHARSET_SINGLE) && strchr(str, ':'))
53 {
54 buf_printf(err, _("'charset' must contain exactly one character set name"));
55 return CSR_ERR_INVALID;
56 }
57
58 int rc = CSR_SUCCESS;
59 bool strict = (cdef->type & D_CHARSET_STRICT);
60 char *q = NULL;
61 char *s = mutt_str_dup(str);
62
63 for (char *p = strtok_r(s, ":", &q); p; p = strtok_r(NULL, ":", &q))
64 {
65 if (*p == '\0')
66 continue; // LCOV_EXCL_LINE
67 if (!mutt_ch_check_charset(p, strict))
68 {
69 rc = CSR_ERR_INVALID;
70 buf_printf(err, _("Invalid value for option %s: %s"), cdef->name, p);
71 break;
72 }
73 }
74
75 FREE(&s);
76 return rc;
77}
78
84int charset_slist_validator(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
85{
86 if (value == 0)
87 return CSR_SUCCESS;
88
89 const struct Slist *list = (const struct Slist *) value;
90
91 int rc = CSR_SUCCESS;
92 bool strict = (cdef->type & D_CHARSET_STRICT);
93
94 const struct ListNode *np = NULL;
95 STAILQ_FOREACH(np, &list->head, entries)
96 {
97 char const *charset = np->data;
98 if (!mutt_ch_check_charset(charset, strict))
99 {
100 rc = CSR_ERR_INVALID;
101 buf_printf(err, _("Invalid value for option %s: %s"), cdef->name, charset);
102 break;
103 }
104 }
105
106 return rc;
107}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:36
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:33
int charset_validator(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate the "charset" config variables - Implements ConfigDef::validator() -.
Definition: charset.c:45
int charset_slist_validator(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate the multiple "charset" config variables - Implements ConfigDef::validator() -.
Definition: charset.c:84
#define FREE(x)
Definition: memory.h:55
bool mutt_ch_check_charset(const char *cs, bool strict)
Does iconv understand a character set?
Definition: charset.c:894
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:254
Parse the 'set' command.
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:390
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:62
const char * name
User-visible name.
Definition: set.h:63
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:64
A List node for strings.
Definition: list.h:37
char * data
String.
Definition: list.h:38
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:83
#define D_CHARSET_STRICT
Flag for charset_validator to use strict char check.
Definition: types.h:84