mutt stable branch with some hacks
1/*
2 * Copyright (C) 1996-2000,2007,2010,2012 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2005,2007 Thomas Roessler <roessler@does-not-exist.org>
4 *
5 * This program is free software; you can redistribute it
6 * and/or modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later
9 * version.
10 *
11 * This program is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23/* mutt functions which are generally useful. */
24
25#ifndef _LIB_H
26# define _LIB_H
27
28# include <stdio.h>
29# include <string.h>
30# ifdef HAVE_UNISTD_H
31# include <unistd.h> /* needed for SEEK_SET */
32# endif
33# include <sys/types.h>
34# include <sys/stat.h>
35# include <time.h>
36# include <limits.h>
37# include <stdarg.h>
38# include <signal.h>
39
40# ifndef _POSIX_PATH_MAX
41# include <limits.h>
42# endif
43
44# ifdef ENABLE_NLS
45# include <libintl.h>
46# define _(a) (gettext (a))
47# ifdef gettext_noop
48# define N_(a) gettext_noop (a)
49# else
50# define N_(a) (a)
51# endif
52# else
53# define _(a) (a)
54# define N_(a) a
55# endif
56
57# define TRUE 1
58# define FALSE 0
59
60# define HUGE_STRING 8192
61# define LONG_STRING 1024
62# define STRING 256
63# define SHORT_STRING 128
64
65/*
66 * Create a format string to be used with scanf.
67 * To use it, write, for instance, MUTT_FORMAT(HUGE_STRING).
68 *
69 * See K&R 2nd ed, p. 231 for an explanation.
70 */
71# define _MUTT_FORMAT_2(a,b) "%" a b
72# define _MUTT_FORMAT_1(a, b) _MUTT_FORMAT_2(#a, b)
73# define MUTT_FORMAT(a) _MUTT_FORMAT_1(a, "s")
74# define MUTT_FORMAT2(a,b) _MUTT_FORMAT_1(a, b)
75
76
77# define FREE(x) safe_free(x)
78# define NONULL(x) x?x:""
79# define ISSPACE(c) isspace((unsigned char)c)
80# define strfcpy(A,B,C) strncpy(A,B,C), *(A+(C)-1)=0
81
82# undef MAX
83# undef MIN
84# define MAX(a,b) ((a) < (b) ? (b) : (a))
85# define MIN(a,b) ((a) < (b) ? (a) : (b))
86
87/* Use this with care. If the compiler can't see the array
88 * definition, it obviously won't produce a correct result. */
89#define mutt_array_size(x) (sizeof (x) / sizeof ((x)[0]))
90
91/* For mutt_format_string() justifications */
92/* Making left 0 and center -1 is of course completely nonsensical, but
93 * it retains compatibility for any patches that call mutt_format_string.
94 * Once patches are updated to use FMT_*, these can be made sane. */
95#define FMT_LEFT 0
96#define FMT_RIGHT 1
97#define FMT_CENTER -1
98
99#define FOREVER while (1)
100
101/* this macro must check for *c == 0 since isspace(0) has unreliable behavior
102 on some systems */
103# define SKIPWS(c) while (*(c) && isspace ((unsigned char) *(c))) c++;
104
105#define EMAIL_WSP " \t\r\n"
106
107/* skip over WSP as defined by RFC5322. This is used primarily for parsing
108 * header fields. */
109
110static inline char *skip_email_wsp(const char *s)
111{
112 if (s)
113 return (char *)(s + strspn(s, EMAIL_WSP));
114 return (char *)s;
115}
116
117static inline int is_email_wsp(char c)
118{
119 return c && (strchr(EMAIL_WSP, c) != NULL);
120}
121
122
123/*
124 * These functions aren't defined in lib.c, but
125 * they are used there.
126 *
127 * A non-mutt "implementation" (ahem) can be found in extlib.c.
128 */
129
130
131# ifndef _EXTLIB_C
132extern void (*mutt_error) (const char *, ...);
133# endif
134
135# ifdef _LIB_C
136# define MUTT_LIB_WHERE
137# define MUTT_LIB_INITVAL(x) = x
138# else
139# define MUTT_LIB_WHERE extern
140# define MUTT_LIB_INITVAL(x)
141# endif
142
143void mutt_exit (int);
144
145
146# ifdef DEBUG
147
148MUTT_LIB_WHERE FILE *debugfile;
149MUTT_LIB_WHERE int debuglevel;
150
151void mutt_debug (FILE *, const char *, ...);
152
153# define dprint(N,X) do { if (debuglevel>=N && debugfile) mutt_debug X; } while (0)
154
155# else
156
157# define dprint(N,X) do { } while (0)
158
159# endif
160
161
162/* Exit values used in send_msg() */
163#define S_ERR 127
164#define S_BKG 126
165
166/* Flags for mutt_read_line() */
167#define MUTT_CONT (1<<0) /* \-continuation */
168#define MUTT_EOL (1<<1) /* don't strip \n/\r\n */
169
170/* The actual library functions. */
171
172char *mutt_concat_path (char *, const char *, const char *, size_t);
173char *mutt_read_line (char *, size_t *, FILE *, int *, int);
174char *mutt_skip_whitespace (char *);
175char *mutt_strlower (char *);
176char *mutt_substrcpy (char *, const char *, const char *, size_t);
177char *mutt_substrdup (const char *, const char *);
178char *safe_strcat (char *, size_t, const char *);
179char *safe_strncat (char *, size_t, const char *, size_t);
180char *safe_strdup (const char *);
181
182/* strtol() wrappers with range checking; they return
183 * 0 success
184 * -1 format error
185 * -2 overflow (for int and short)
186 * the int pointer may be NULL to test only without conversion
187 */
188int mutt_atos (const char *, short *);
189int mutt_atoi (const char *, int *);
190int mutt_atol (const char *, long *);
191int mutt_atoui (const char *, unsigned int *);
192int mutt_atoul (const char *, unsigned long *);
193int mutt_atoull (const char *, unsigned long long *);
194
195const char *mutt_stristr (const char *, const char *);
196const char *mutt_basename (const char *);
197
198int compare_stat (struct stat *, struct stat *);
199int mutt_copy_stream (FILE *, FILE *);
200int mutt_copy_bytes (FILE *, FILE *, size_t);
201int mutt_strcasecmp (const char *, const char *);
202int mutt_strcmp (const char *, const char *);
203int mutt_strncasecmp (const char *, const char *, size_t);
204int mutt_strncmp (const char *, const char *, size_t);
205int mutt_strcoll (const char *, const char *);
206int safe_asprintf (char **, const char *, ...);
207int safe_rename (const char *, const char *);
208int safe_fclose (FILE **);
209int safe_fsync_close (FILE **);
210
211size_t mutt_quote_filename (char *, size_t, const char *);
212size_t mutt_strlen (const char *);
213
214void *safe_calloc (size_t, size_t);
215void *safe_malloc (size_t);
216void mutt_nocurses_error (const char *, ...);
217void mutt_remove_trailing_ws (char *);
218void mutt_sanitize_filename (char *, short);
219void mutt_str_replace (char **p, const char *s);
220int mutt_mkdir (char *path, mode_t mode);
221void mutt_str_adjust (char **p);
222void mutt_unlink (const char *);
223void safe_free (void *);
224void safe_realloc (void *, size_t);
225
226const char *mutt_strsysexit(int e);
227#endif