NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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 "state.h"
36#include "date.h"
37#include "random.h"
38
43const char *state_attachment_marker(void)
44{
45 static char marker[256] = { 0 };
46 if (!marker[0])
47 {
48 snprintf(marker, sizeof(marker), "\033]9;%" PRIu64 "\a", mutt_rand64());
49 }
50 return marker;
51}
52
58{
59 static char marker[256] = { 0 };
60 if (!marker[0])
61 {
62 snprintf(marker, sizeof(marker), "\033]8;%lld\a", (long long) mutt_date_now());
63 }
64 return marker;
65}
66
71void state_mark_attach(struct State *state)
72{
73 if (!state || !state->fp_out)
74 return;
75
77 {
79 }
80}
81
87{
88 if (!state || !state->fp_out)
89 return;
90
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 {
212 fwrite(buf, buflen, 1, state->fp_out);
213 }
214}
Time and date handling routines.
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:446
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:71
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:43
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:86
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:57
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_PAGER
Output will be displayed in the Pager.
Definition: state.h:41
#define state_puts(STATE, STR)
Definition: state.h:57
#define state_set_prefix(state)
Definition: state.h:55
#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:56
#define state_putc(STATE, STR)
Definition: state.h:58
uint64_t mutt_rand64(void)
Create a 64-bit random number.
Definition: random.c:134
Random number/string functions.
Keep track when processing files.
Definition: state.h:47
StateFlags flags
Flags, e.g. STATE_DISPLAY.
Definition: state.h:51
FILE * fp_out
File to write to.
Definition: state.h:49
char * prefix
String to add to the beginning of each output line.
Definition: state.h:50