common.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #define as(ar) (sizeof(ar)/sizeof(ar[0]))
  33. #define __stringify(x) #x
  34. #define stringify(x) __stringify(x)
  35. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
  36. # define ATTR_UNUSED __attribute__((unused))
  37. # define ATTR_NORETURN __attribute__((noreturn))
  38. # define ATTR_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
  39. # define ATTR_PACKED(ref) __attribute__((packed,aligned(sizeof(ref))))
  40. #else
  41. # define ATTR_UNUSED
  42. # define ATTR_NORETURN
  43. # define ATTR_PRINTFLIKE(fmt,var)
  44. # define ATTR_PACKED(ref)
  45. #endif
  46. #ifdef __GNUC__
  47. # define INLINE __inline__
  48. #else
  49. # define INLINE
  50. #endif
  51. #define EXE "mbsync"
  52. /* main.c */
  53. #define DEBUG_CRASH 0x01
  54. #define DEBUG_MAILDIR 0x02
  55. #define DEBUG_NET 0x04
  56. #define DEBUG_NET_ALL 0x08
  57. #define DEBUG_SYNC 0x10
  58. #define DEBUG_MAIN 0x20
  59. #define DEBUG_ALL (0xFF & ~DEBUG_NET_ALL)
  60. #define QUIET 0x100
  61. #define VERYQUIET 0x200
  62. #define PROGRESS 0x400
  63. #define VERBOSE 0x800
  64. #define KEEPJOURNAL 0x1000
  65. #define ZERODELAY 0x2000
  66. extern int DFlags;
  67. extern int UseFSync;
  68. extern char FieldDelimiter;
  69. extern int Pid;
  70. extern char Hostname[256];
  71. extern const char *Home;
  72. extern int BufferLimit;
  73. extern int new_total[2], new_done[2];
  74. extern int flags_total[2], flags_done[2];
  75. extern int trash_total[2], trash_done[2];
  76. void stats( void );
  77. /* util.c */
  78. void vdebug( int, const char *, va_list va );
  79. void vdebugn( int, const char *, va_list va );
  80. void ATTR_PRINTFLIKE(1, 2) info( const char *, ... );
  81. void ATTR_PRINTFLIKE(1, 2) infon( const char *, ... );
  82. void ATTR_PRINTFLIKE(1, 2) progress( const char *, ... );
  83. void ATTR_PRINTFLIKE(1, 2) notice( const char *, ... );
  84. void ATTR_PRINTFLIKE(1, 2) warn( const char *, ... );
  85. void ATTR_PRINTFLIKE(1, 2) error( const char *, ... );
  86. void ATTR_PRINTFLIKE(1, 2) sys_error( const char *, ... );
  87. void flushn( void );
  88. typedef struct string_list {
  89. struct string_list *next;
  90. char string[1];
  91. } string_list_t;
  92. void add_string_list_n( string_list_t **list, const char *str, int len );
  93. void add_string_list( string_list_t **list, const char *str );
  94. void free_string_list( string_list_t *list );
  95. #ifndef HAVE_MEMRCHR
  96. void *memrchr( const void *s, int c, size_t n );
  97. #endif
  98. int starts_with( const char *str, int strl, const char *cmp, int cmpl );
  99. int starts_with_upper( const char *str, int strl, const char *cmp, int cmpl );
  100. int equals( const char *str, int strl, const char *cmp, int cmpl );
  101. #ifndef HAVE_TIMEGM
  102. time_t timegm( struct tm *tm );
  103. #endif
  104. void *nfmalloc( size_t sz );
  105. void *nfcalloc( size_t sz );
  106. void *nfrealloc( void *mem, size_t sz );
  107. char *nfstrndup( const char *str, size_t nchars );
  108. char *nfstrdup( const char *str );
  109. int nfvasprintf( char **str, const char *fmt, va_list va );
  110. int ATTR_PRINTFLIKE(2, 3) nfasprintf( char **str, const char *fmt, ... );
  111. int ATTR_PRINTFLIKE(3, 4) nfsnprintf( char *buf, int blen, const char *fmt, ... );
  112. void ATTR_NORETURN oob( void );
  113. char *expand_strdup( const char *s );
  114. int map_name( const char *arg, char **result, int reserve, const char *in, const char *out );
  115. #define DEFINE_ARRAY_TYPE(T) \
  116. typedef struct T##_array { \
  117. T *data; \
  118. int size; \
  119. } ATTR_PACKED(T *) T##_array_t; \
  120. typedef struct T##_array_alloc { \
  121. T##_array_t array; \
  122. int alloc; \
  123. } ATTR_PACKED(T *) T##_array_alloc_t; \
  124. static INLINE T *T##_array_append( T##_array_alloc_t *arr ) \
  125. { \
  126. if (arr->array.size == arr->alloc) { \
  127. arr->alloc = arr->alloc * 2 + 100; \
  128. arr->array.data = nfrealloc( arr->array.data, arr->alloc * sizeof(T) ); \
  129. } \
  130. return &arr->array.data[arr->array.size++]; \
  131. }
  132. #define ARRAY_INIT(arr) \
  133. do { (arr)->array.data = 0; (arr)->array.size = (arr)->alloc = 0; } while (0)
  134. #define ARRAY_SQUEEZE(arr) \
  135. do { \
  136. (arr)->data = nfrealloc( (arr)->data, (arr)->size * sizeof((arr)->data[0]) ); \
  137. } while (0)
  138. DEFINE_ARRAY_TYPE(int)
  139. void sort_int_array( int_array_t array );
  140. void arc4_init( void );
  141. uchar arc4_getbyte( void );
  142. int bucketsForSize( int size );
  143. typedef struct list_head {
  144. struct list_head *next, *prev;
  145. } list_head_t;
  146. typedef struct notifier {
  147. struct notifier *next;
  148. void (*cb)( int what, void *aux );
  149. void *aux;
  150. #ifdef HAVE_SYS_POLL_H
  151. int index;
  152. #else
  153. int fd, events;
  154. #endif
  155. } notifier_t;
  156. #ifdef HAVE_SYS_POLL_H
  157. # include <sys/poll.h>
  158. #else
  159. # define POLLIN 1
  160. # define POLLOUT 4
  161. # define POLLERR 8
  162. #endif
  163. void init_notifier( notifier_t *sn, int fd, void (*cb)( int, void * ), void *aux );
  164. void conf_notifier( notifier_t *sn, int and_events, int or_events );
  165. void wipe_notifier( notifier_t *sn );
  166. typedef struct {
  167. list_head_t links;
  168. void (*cb)( void *aux );
  169. void *aux;
  170. time_t timeout;
  171. } wakeup_t;
  172. void init_wakeup( wakeup_t *tmr, void (*cb)( void * ), void *aux );
  173. void conf_wakeup( wakeup_t *tmr, int timeout );
  174. void wipe_wakeup( wakeup_t *tmr );
  175. static INLINE int pending_wakeup( wakeup_t *tmr ) { return tmr->links.next != 0; }
  176. void main_loop( void );
  177. #endif