1 /* 2 * Copyright (C) 1996-2000,2007 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 5120 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 /* For mutt_format_string() justifications */ 88 /* Making left 0 and center -1 is of course completely nonsensical, but 89 * it retains compatibility for any patches that call mutt_format_string. 90 * Once patches are updated to use FMT_*, these can be made sane. */ 91 #define FMT_LEFT 0 92 #define FMT_RIGHT 1 93 #define FMT_CENTER -1 94 95 #define FOREVER while (1) 96 97 /* this macro must check for *c == 0 since isspace(0) has unreliable behavior 98 on some systems */ 99 # define SKIPWS(c) while (*(c) && isspace ((unsigned char) *(c))) c++; 100 101 /* 102 * These functions aren't defined in lib.c, but 103 * they are used there. 104 * 105 * A non-mutt "implementation" (ahem) can be found in extlib.c. 106 */ 107 108 109 # ifndef _EXTLIB_C 110 extern void (*mutt_error) (const char *, ...); 111 # endif 112 113 # ifdef _LIB_C 114 # define MUTT_LIB_WHERE 115 # define MUTT_LIB_INITVAL(x) = x 116 # else 117 # define MUTT_LIB_WHERE extern 118 # define MUTT_LIB_INITVAL(x) 119 # endif 120 121 void mutt_exit (int); 122 123 124 # ifdef DEBUG 125 126 MUTT_LIB_WHERE FILE *debugfile MUTT_LIB_INITVAL(0); 127 MUTT_LIB_WHERE int debuglevel MUTT_LIB_INITVAL(0); 128 129 void mutt_debug (FILE *, const char *, ...); 130 131 # define dprint(N,X) do { if(debuglevel>=N && debugfile) mutt_debug X; } while (0) 132 133 # else 134 135 # define dprint(N,X) do { } while (0) 136 137 # endif 138 139 140 /* Exit values used in send_msg() */ 141 #define S_ERR 127 142 #define S_BKG 126 143 144 /* Flags for mutt_read_line() */ 145 #define M_CONT (1<<0) /* \-continuation */ 146 #define M_EOL (1<<1) /* don't strip \n/\r\n */ 147 148 /* The actual library functions. */ 149 150 FILE *safe_fopen (const char *, const char *); 151 152 char *mutt_concatn_path (char *, size_t, const char *, size_t, const char *, size_t); 153 char *mutt_concat_path (char *, const char *, const char *, size_t); 154 char *mutt_read_line (char *, size_t *, FILE *, int *, int); 155 char *mutt_skip_whitespace (char *); 156 char *mutt_strlower (char *); 157 char *mutt_substrcpy (char *, const char *, const char *, size_t); 158 char *mutt_substrdup (const char *, const char *); 159 char *safe_strcat (char *, size_t, const char *); 160 char *safe_strncat (char *, size_t, const char *, size_t); 161 char *safe_strdup (const char *); 162 163 /* strtol() wrappers with range checking; they return 164 * 0 success 165 * -1 format error 166 * -2 overflow (for int and short) 167 * the int pointer may be NULL to test only without conversion 168 */ 169 int mutt_atos (const char *, short *); 170 int mutt_atoi (const char *, int *); 171 int mutt_atol (const char *, long *); 172 173 const char *mutt_stristr (const char *, const char *); 174 const char *mutt_basename (const char *); 175 176 int compare_stat (struct stat *, struct stat *); 177 int mutt_copy_stream (FILE *, FILE *); 178 int mutt_copy_bytes (FILE *, FILE *, size_t); 179 int mutt_rx_sanitize_string (char *, size_t, const char *); 180 int mutt_strcasecmp (const char *, const char *); 181 int mutt_strcmp (const char *, const char *); 182 int mutt_strncasecmp (const char *, const char *, size_t); 183 int mutt_strncmp (const char *, const char *, size_t); 184 int mutt_strcoll (const char *, const char *); 185 int safe_asprintf (char **, const char *, ...); 186 int safe_open (const char *, int); 187 int safe_rename (const char *, const char *); 188 int safe_symlink (const char *, const char *); 189 int safe_fclose (FILE **); 190 int safe_fsync_close (FILE **); 191 int mutt_rmtree (const char *); 192 193 size_t mutt_quote_filename (char *, size_t, const char *); 194 size_t mutt_strlen (const char *); 195 196 void *safe_calloc (size_t, size_t); 197 void *safe_malloc (size_t); 198 void mutt_nocurses_error (const char *, ...); 199 void mutt_remove_trailing_ws (char *); 200 void mutt_sanitize_filename (char *, short); 201 void mutt_str_replace (char **p, const char *s); 202 void mutt_str_adjust (char **p); 203 void mutt_unlink (const char *); 204 void safe_free (void *); 205 void safe_realloc (void *, size_t); 206 207 const char *mutt_strsysexit(int e); 208 #endif 209