common.h 5.1 KB

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