NeoMutt  2021-02-05-666-ge300cd
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 "mutt/lib.h"
36 #include "config/lib.h"
37 #include "core/lib.h"
38 
44 const char *state_attachment_marker(void)
45 {
46  static char marker[256] = { 0 };
47  if (!marker[0])
48  {
49  snprintf(marker, sizeof(marker), "\033]9;%" PRIu64 "\a", mutt_rand64());
50  }
51  return marker;
52 }
53 
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_epoch());
65  }
66  return marker;
67 }
68 
73 void state_mark_attach(struct State *s)
74 {
75  if (!s || !s->fp_out)
76  return;
77  const char *const c_pager = cs_subset_string(NeoMutt->sub, "pager");
78  if ((s->flags & MUTT_DISPLAY) &&
79  (!c_pager || mutt_str_equal(c_pager, "builtin")))
80  {
82  }
83 }
84 
90 {
91  const char *const c_pager = cs_subset_string(NeoMutt->sub, "pager");
92  if ((s->flags & MUTT_DISPLAY) &&
93  (!c_pager || mutt_str_equal(c_pager, "builtin")))
94  {
96  }
97 }
98 
104 void state_attach_puts(struct State *s, const char *t)
105 {
106  if (!s || !s->fp_out || !t)
107  return;
108 
109  if (*t != '\n')
111  while (*t)
112  {
113  state_putc(s, *t);
114  if ((*t++ == '\n') && *t)
115  if (*t != '\n')
117  }
118 }
119 
127 static int state_putwc(struct State *s, wchar_t wc)
128 {
129  char mb[MB_LEN_MAX] = { 0 };
130  int rc;
131 
132  rc = wcrtomb(mb, wc, NULL);
133  if (rc < 0)
134  return rc;
135  if (fputs(mb, s->fp_out) == EOF)
136  return -1;
137  return 0;
138 }
139 
147 int state_putws(struct State *s, const wchar_t *ws)
148 {
149  const wchar_t *p = ws;
150 
151  while (p && (*p != L'\0'))
152  {
153  if (state_putwc(s, *p) < 0)
154  return -1;
155  p++;
156  }
157  return 0;
158 }
159 
165 void state_prefix_putc(struct State *s, char c)
166 {
167  if (s->flags & MUTT_PENDINGPREFIX)
168  {
170  if (s->prefix)
171  state_puts(s, s->prefix);
172  }
173 
174  state_putc(s, c);
175 
176  if (c == '\n')
177  state_set_prefix(s);
178 }
179 
187 int state_printf(struct State *s, const char *fmt, ...)
188 {
189  int rc;
190  va_list ap;
191 
192  va_start(ap, fmt);
193  rc = vfprintf(s->fp_out, fmt, ap);
194  va_end(ap);
195 
196  return rc;
197 }
198 
205 void state_prefix_put(struct State *s, const char *buf, size_t buflen)
206 {
207  if (s->prefix)
208  {
209  while (buflen--)
210  state_prefix_putc(s, *buf++);
211  }
212  else
213  fwrite(buf, buflen, 1, s->fp_out);
214 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:427
int state_printf(struct State *s, const char *fmt,...)
Write a formatted string to the State.
Definition: state.c:187
#define MUTT_DISPLAY
Output is displayed to the user.
Definition: state.h:32
#define state_puts(STATE, STR)
Definition: state.h:55
#define MUTT_PENDINGPREFIX
Prefix to write, but character must follow.
Definition: state.h:34
char * prefix
String to add to the beginning of each output line.
Definition: state.h:48
#define state_reset_prefix(state)
Definition: state.h:54
const char * state_attachment_marker(void)
Get a unique (per-run) ANSI string to mark PGP messages in an email.
Definition: state.c:44
FILE * fp_out
File to write to.
Definition: state.h:47
Container for Accounts, Notifications.
Definition: neomutt.h:36
Convenience wrapper for the config headers.
StateFlags flags
Flags, e.g. MUTT_DISPLAY.
Definition: state.h:49
Convenience wrapper for the core headers.
uint64_t mutt_rand64(void)
Create a 64-bit random number.
Definition: random.c:130
int state_putws(struct State *s, const wchar_t *ws)
Write a wide string to the state.
Definition: state.c:147
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
void state_mark_protected_header(struct State *s)
Write a unique marker around protected headers.
Definition: state.c:89
void state_mark_attach(struct State *s)
Write a unique marker around content.
Definition: state.c:73
void state_attach_puts(struct State *s, const char *t)
Write a string to the state.
Definition: state.c:104
#define state_set_prefix(state)
Definition: state.h:53
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_put(struct State *s, const char *buf, size_t buflen)
Write a prefixed fixed-string to the State.
Definition: state.c:205
void state_prefix_putc(struct State *s, char c)
Write a prefixed character to the state.
Definition: state.c:165
Keep track when processing files.
Definition: state.h:44
#define state_putc(STATE, STR)
Definition: state.h:56
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Convenience wrapper for the library headers.
static int state_putwc(struct State *s, wchar_t wc)
Write a wide character to the state.
Definition: state.c:127