common.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <ossi@users.sf.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * As a special exception, mbsync may be linked with the OpenSSL library,
  20. * despite that library's more restrictive license.
  21. */
  22. #ifndef COMMON_H
  23. #define COMMON_H
  24. #include <autodefs.h>
  25. #include <sys/types.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <time.h>
  29. typedef unsigned char uchar;
  30. typedef unsigned short ushort;
  31. typedef unsigned int uint;
  32. typedef unsigned long ulong;
  33. #define as(ar) (sizeof(ar)/sizeof(ar[0]))
  34. #define stringify__(x) #x
  35. #define stringify(x) stringify__(x)
  36. #define shifted_bit(in, from, to) \
  37. ((int)(((uint)(in) / (from > to ? from / to : 1) * (to > from ? to / from : 1)) & to))
  38. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
  39. # define ATTR_UNUSED __attribute__((unused))
  40. # define ATTR_NORETURN __attribute__((noreturn))
  41. # define ATTR_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
  42. # define ATTR_PACKED(ref) __attribute__((packed,aligned(sizeof(ref))))
  43. #else
  44. # define ATTR_UNUSED
  45. # define ATTR_NORETURN
  46. # define ATTR_PRINTFLIKE(fmt,var)
  47. # define ATTR_PACKED(ref)
  48. #endif
  49. #if defined(__clang__)
  50. # define DO_PRAGMA__(text) _Pragma(#text)
  51. # define DIAG_PUSH DO_PRAGMA__(clang diagnostic push)
  52. # define DIAG_POP DO_PRAGMA__(clang diagnostic pop)
  53. # define DIAG_DISABLE(text) DO_PRAGMA__(clang diagnostic ignored text)
  54. #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5)
  55. # define DO_PRAGMA__(text) _Pragma(#text)
  56. # define DIAG_PUSH DO_PRAGMA__(GCC diagnostic push)
  57. # define DIAG_POP DO_PRAGMA__(GCC diagnostic pop)
  58. # define DIAG_DISABLE(text) DO_PRAGMA__(GCC diagnostic ignored text)
  59. #else
  60. # define DIAG_PUSH
  61. # define DIAG_POP
  62. # define DIAG_DISABLE(text)
  63. #endif
  64. #if __GNUC__ >= 7
  65. # define FALLTHROUGH __attribute__((fallthrough));
  66. #else
  67. # define FALLTHROUGH
  68. #endif
  69. #ifdef __GNUC__
  70. # define INLINE __inline__
  71. #else
  72. # define INLINE
  73. #endif
  74. #define EXE "mbsync"
  75. /* main.c */
  76. #define DEBUG_CRASH 0x01
  77. #define DEBUG_MAILDIR 0x02
  78. #define DEBUG_NET 0x04
  79. #define DEBUG_NET_ALL 0x08
  80. #define DEBUG_SYNC 0x10
  81. #define DEBUG_MAIN 0x20
  82. #define DEBUG_DRV 0x40
  83. #define DEBUG_DRV_ALL 0x80
  84. #define DEBUG_ALL (0xFF & ~(DEBUG_NET_ALL | DEBUG_DRV_ALL))
  85. #define QUIET 0x100
  86. #define VERYQUIET 0x200
  87. #define PROGRESS 0x400
  88. #define VERBOSE 0x800
  89. #define KEEPJOURNAL 0x1000
  90. #define ZERODELAY 0x2000
  91. extern int DFlags;
  92. extern int JLimit;
  93. extern int UseFSync;
  94. extern char FieldDelimiter;
  95. extern int Pid;
  96. extern char Hostname[256];
  97. extern const char *Home;
  98. extern uint BufferLimit;
  99. extern int new_total[2], new_done[2];
  100. extern int flags_total[2], flags_done[2];
  101. extern int trash_total[2], trash_done[2];
  102. void stats( void );
  103. /* util.c */
  104. void ATTR_PRINTFLIKE(2, 0) vdebug( int, const char *, va_list va );
  105. void ATTR_PRINTFLIKE(2, 0) vdebugn( int, const char *, va_list va );
  106. void ATTR_PRINTFLIKE(1, 2) info( const char *, ... );
  107. void ATTR_PRINTFLIKE(1, 2) infon( const char *, ... );
  108. void ATTR_PRINTFLIKE(1, 2) progress( const char *, ... );
  109. void ATTR_PRINTFLIKE(1, 2) notice( const char *, ... );
  110. void ATTR_PRINTFLIKE(1, 2) warn( const char *, ... );
  111. void ATTR_PRINTFLIKE(1, 2) error( const char *, ... );
  112. void ATTR_PRINTFLIKE(1, 0) vsys_error( const char *, va_list va );
  113. void ATTR_PRINTFLIKE(1, 2) sys_error( const char *, ... );
  114. void flushn( void );
  115. typedef struct string_list {
  116. struct string_list *next;
  117. char string[1];
  118. } ATTR_PACKED(void *) string_list_t;
  119. void add_string_list_n( string_list_t **list, const char *str, uint len );
  120. void add_string_list( string_list_t **list, const char *str );
  121. void free_string_list( string_list_t *list );
  122. #ifndef HAVE_MEMRCHR
  123. void *memrchr( const void *s, int c, size_t n );
  124. #endif
  125. #ifndef HAVE_STRNLEN
  126. size_t strnlen( const char *str, size_t maxlen );
  127. #endif
  128. int starts_with( const char *str, int strl, const char *cmp, uint cmpl );
  129. int starts_with_upper( const char *str, int strl, const char *cmp, uint cmpl );
  130. int equals( const char *str, int strl, const char *cmp, uint cmpl );
  131. #ifndef HAVE_TIMEGM
  132. time_t timegm( struct tm *tm );
  133. #endif
  134. void *nfmalloc( size_t sz );
  135. void *nfcalloc( size_t sz );
  136. void *nfrealloc( void *mem, size_t sz );
  137. char *nfstrndup( const char *str, size_t nchars );
  138. char *nfstrdup( const char *str );
  139. int ATTR_PRINTFLIKE(2, 0) nfvasprintf( char **str, const char *fmt, va_list va );
  140. int ATTR_PRINTFLIKE(2, 3) nfasprintf( char **str, const char *fmt, ... );
  141. int ATTR_PRINTFLIKE(3, 4) nfsnprintf( char *buf, int blen, const char *fmt, ... );
  142. void ATTR_NORETURN oob( void );
  143. char *expand_strdup( const char *s );
  144. int map_name( const char *arg, char **result, uint reserve, const char *in, const char *out );
  145. #define DEFINE_ARRAY_TYPE(T) \
  146. typedef struct { \
  147. T *data; \
  148. uint size; \
  149. } ATTR_PACKED(T *) T##_array_t; \
  150. typedef struct { \
  151. T##_array_t array; \
  152. uint alloc; \
  153. } ATTR_PACKED(T *) T##_array_alloc_t; \
  154. static INLINE T *T##_array_append( T##_array_alloc_t *arr ) \
  155. { \
  156. if (arr->array.size == arr->alloc) { \
  157. arr->alloc = arr->alloc * 2 + 100; \
  158. arr->array.data = nfrealloc( arr->array.data, arr->alloc * sizeof(T) ); \
  159. } \
  160. return &arr->array.data[arr->array.size++]; \
  161. }
  162. #define ARRAY_INIT(arr) \
  163. do { (arr)->array.data = 0; (arr)->array.size = (arr)->alloc = 0; } while (0)
  164. #define ARRAY_SQUEEZE(arr) \
  165. do { \
  166. (arr)->data = nfrealloc( (arr)->data, (arr)->size * sizeof((arr)->data[0]) ); \
  167. } while (0)
  168. DEFINE_ARRAY_TYPE(uint)
  169. void sort_uint_array( uint_array_t array );
  170. int find_uint_array( const uint_array_t array, uint value );
  171. void arc4_init( void );
  172. uchar arc4_getbyte( void );
  173. uint bucketsForSize( uint size );
  174. typedef struct list_head {
  175. struct list_head *next, *prev;
  176. } list_head_t;
  177. typedef struct notifier {
  178. struct notifier *next;
  179. void (*cb)( int what, void *aux );
  180. void *aux;
  181. #ifdef HAVE_SYS_POLL_H
  182. uint index;
  183. #else
  184. int fd;
  185. short events;
  186. #endif
  187. } notifier_t;
  188. #ifdef HAVE_SYS_POLL_H
  189. # include <sys/poll.h>
  190. #else
  191. # define POLLIN 1
  192. # define POLLOUT 4
  193. # define POLLERR 8
  194. #endif
  195. void init_notifier( notifier_t *sn, int fd, void (*cb)( int, void * ), void *aux );
  196. void conf_notifier( notifier_t *sn, short and_events, short or_events );
  197. void wipe_notifier( notifier_t *sn );
  198. typedef struct {
  199. list_head_t links;
  200. void (*cb)( void *aux );
  201. void *aux;
  202. time_t timeout;
  203. } wakeup_t;
  204. void init_wakeup( wakeup_t *tmr, void (*cb)( void * ), void *aux );
  205. void conf_wakeup( wakeup_t *tmr, int timeout );
  206. void wipe_wakeup( wakeup_t *tmr );
  207. static INLINE int ATTR_UNUSED pending_wakeup( wakeup_t *tmr ) { return tmr->links.next != 0; }
  208. void main_loop( void );
  209. #endif