NeoMutt  2023-03-22
Teaching an old dog new tricks
DOXYGEN
state.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <inttypes.h>
31#include <limits.h>
32#include <stdarg.h>
33#include <stdio.h>
34#include <wchar.h>
35#include "config/lib.h"
36#include "core/lib.h"
37#include "state.h"
38#include "date.h"
39#include "random.h"
40
45const char *state_attachment_marker(void)
46{
47 static char marker[256] = { 0 };
48 if (!marker[0])
49 {
50 snprintf(marker, sizeof(marker), "\033]9;%" PRIu64 "\a", mutt_rand64());
51 }
52 return marker;
53}
54
60{
61 static char marker[256] = { 0 };
62 if (!marker[0])
63 {
64 snprintf(marker, sizeof(marker), "\033]8;%lld\a", (long long) mutt_date_now());
65 }
66 return marker;
67}
68
73void state_mark_attach(struct State *state)
74{
75 if (!state || !state->fp_out)
76 return;
77 const char *const c_pager = cs_subset_string(NeoMutt->sub, "pager");
78 if ((state->flags & STATE_DISPLAY) && (!c_pager || mutt_str_equal(c_pager, "builtin")))
79 {
81 }
82}
83
89{
90 const char *const c_pager = cs_subset_string(NeoMutt->sub, "pager");
91 if ((state->flags & STATE_DISPLAY) && (!c_pager || mutt_str_equal(c_pager, "builtin")))
92 {
94 }
95}
96
102void state_attach_puts(struct State *state, const char *t)
103{
104 if (!state || !state->fp_out || !t)
105 return;
106
107 if (*t != '\n')
108 state_mark_attach(state);
109 while (*t)
110 {
111 state_putc(state, *t);
112 if ((*t++ == '\n') && *t)
113 if (*t != '\n')
114 state_mark_attach(state);
115 }
116}
117
125static int state_putwc(struct State *state, wchar_t wc)
126{
127 char mb[MB_LEN_MAX] = { 0 };
128 int rc;
129
130 rc = wcrtomb(mb, wc, NULL);
131 if (rc < 0)
132 return rc;
133 if (fputs(mb, state->fp_out) == EOF)
134 return -1;
135 return 0;
136}
137
145int state_putws(struct State *state, const wchar_t *ws)
146{
147 const wchar_t *p = ws;
148
149 while (p && (*p != L'\0'))
150 {
151 if (state_putwc(state, *p) < 0)
152 return -1;
153 p++;
154 }
155 return 0;
156}
157
163void state_prefix_putc(struct State *state, char c)
164{
165 if (state->flags & STATE_PENDINGPREFIX)
166 {
167 state_reset_prefix(state);
168 if (state->prefix)
169 state_puts(state, state->prefix);
170 }
171
172 state_putc(state, c);
173
174 if (c == '\n')
175 state_set_prefix(state);
176}
177
185int state_printf(struct State *state, const char *fmt, ...)
186{
187 int rc;
188 va_list ap;
189
190 va_start(ap, fmt);
191 rc = vfprintf(state->fp_out, fmt, ap);
192 va_end(ap);
193
194 return rc;
195}
196
203void state_prefix_put(struct State *state, const char *buf, size_t buflen)
204{
205 if (state->prefix)
206 {
207 while (buflen--)
208 state_prefix_putc(state, *buf++);
209 }
210 else
211 fwrite(buf, buflen, 1, state->fp_out);
212}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:428
Time and date handling routines.
void state_attach_puts(struct State *state, const char *t)
Write a string to the state.
Definition: state.c:102
void state_mark_attach(struct State *state)
Write a unique marker around content.
Definition: state.c:73
int state_printf(struct State *state, const char *fmt,...)
Write a formatted string to the State.
Definition: state.c:185
static int state_putwc(struct State *state, wchar_t wc)
Write a wide character to the state.
Definition: state.c:125
const char * state_attachment_marker(void)
Get a unique (per-run) ANSI string to mark PGP messages in an email.
Definition: state.c:45
void state_prefix_put(struct State *state, const char *buf, size_t buflen)
Write a prefixed fixed-string to the State.
Definition: state.c:203
void state_mark_protected_header(struct State *state)
Write a unique marker around protected headers.
Definition: state.c:88
int state_putws(struct State *state, const wchar_t *ws)
Write a wide string to the state.
Definition: state.c:145
const char * state_protected_header_marker(void)
Get a unique (per-run) ANSI string to mark protected headers in an email.
Definition: state.c:59
void state_prefix_putc(struct State *state, char c)
Write a prefixed character to the state.
Definition: state.c:163
Keep track when processing files.
#define state_puts(STATE, STR)
Definition: state.h:56
#define state_set_prefix(state)
Definition: state.h:54
#define STATE_DISPLAY
Output is displayed to the user.
Definition: state.h:32
#define STATE_PENDINGPREFIX
Prefix to write, but character must follow.
Definition: state.h:34
#define state_reset_prefix(state)
Definition: state.h:55
#define state_putc(STATE, STR)
Definition: state.h:57
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
uint64_t mutt_rand64(void)
Create a 64-bit random number.
Definition: random.c:129
Random number/string functions.
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Keep track when processing files.
Definition: state.h:46
StateFlags flags
Flags, e.g. STATE_DISPLAY.
Definition: state.h:50
FILE * fp_out
File to write to.
Definition: state.h:48
char * prefix
String to add to the beginning of each output line.
Definition: state.h:49