chiark / gitweb /
dump control command
[inn-innduct.git] / innfeed / misc.h
1 /*  $Id: misc.h 6648 2004-01-25 20:07:11Z rra $
2 **
3 **  Miscellaneous utility functions for innfeed.
4 **
5 **  Written by James Brister <brister@vix.com>
6 */
7
8 #if ! defined ( misc_h__ )
9 #define misc_h__
10
11 #include "config.h"
12
13 #include <stdarg.h>
14 #include <sys/types.h>
15
16 /* These typedefs are all here because C is too stupid to let me multiply
17    define typedefs to the same things (as C++ will). Hence I can't redeclare
18    the typedefs to get around recursive header file includes (like host.h and
19    connection.h would need if they contained their own typedefs). */
20
21 typedef struct article_s *Article ;             /* see article.h */
22 typedef struct buffer_s *Buffer ;               /* see buffer.h */
23 typedef struct commander_s *Commander ;         /* see commander.h */
24 typedef struct config_s *Config ;               /* see config.h */
25 typedef struct connection_s *Connection ;       /* see connection.h */
26 typedef struct endpoint_s *EndPoint ;           /* see endpoint.h */
27 typedef struct host_s *Host ;                   /* see host.h */
28 typedef struct innlistener_s *InnListener ;     /* see innlistener.h */
29 typedef struct tape_s *Tape ;                   /* see tape.h */
30
31 typedef int TimeoutId ;                         /* see endpoint.h */
32 typedef enum {                                  /* see endpoint.h */
33   IoDone, IoIncomplete, IoFailed, IoEOF, IoProgress
34 } IoStatus ; 
35
36 typedef void (*EndpRWCB) (EndPoint e,           /* see endpoint.h */
37                           IoStatus i, Buffer *b, void *d) ;
38 typedef void (*EndpTCB) (TimeoutId tid, void *d) ; /* see endpoint.h */
39 typedef void (*EndpWorkCbk) (EndPoint ep, void *data) ;
40
41
42 /* debugging information */
43 extern unsigned int loggingLevel ;     /* if 0 then d_printf is a no-op */
44
45 /* the current count of file desccriptors */
46 extern unsigned int openfds ;
47
48 /* if level <= loggingLevel then print */
49 void d_printf (unsigned int level, const char *fmt, ...) __attribute__ ((__format__ (printf, 2, 3)));
50
51 /* for the gethostbyname() error code */
52 const char *host_err_str (void) ;
53
54 /* parse a reponse line into it's code and body. *rest will end up pointing
55    into the middle of p */
56 bool getNntpResponse (char *p, int *code, char **rest) ;
57
58 /* parse out the first field of a nntp response code as a message-id. Caller
59    must free the return value when done. */
60 char *getMsgId (const char *p) ;
61
62 /* pick out the next non-blank string inside PTR. TAIL is set to point at
63    the first blank (or NULL) after the string. Returns a pointer into PTR */
64 char *findNonBlankString (char *ptr, char **tail) ;
65
66 /* if fp is not NULL then print to it, otherwise syslog at the level. */
67 void logOrPrint (int level, FILE *fp, const char *fmt, ...)
68   __attribute__ ((__format__ (printf, 3, 4)));
69
70 /* Error handling functions for use with warn and die. */
71 void error_log_stderr_date(int len, const char *fmt, va_list args, int err);
72
73 /* Do cleanup and then abort, for use with die. */
74 int dump_core(void);
75
76 /* Alternate die that doesn't invoke an error handler. */
77 void logAndExit (int exitVal, const char *fmt, ...)
78   __attribute__ ((__format__ (printf, 2, 3)));
79
80 /* return true of the file exists and is a regular file */
81 bool fileExistsP (const char *filename) ;
82
83 /* return true if file exists and is a directory */
84 bool isDirectory (const char *filename) ;
85
86 char *mystrtok (char *string, const char *sep) ;
87
88 /* remove trailing whitespace */
89 void trim_ws (char *string) ;
90
91 /* locks the peer and returns true or returns false */
92 bool lockPeer (const char *peerName) ;
93
94 /* return true if the end of string matches tail. */
95 bool endsIn (const char *string, const char *tail) ;
96
97 /* scribble over then free up the null-terminated string */
98 void freeCharP (char *charp) ;
99
100 /* append the contents of src to dest. src is removed if append if
101    successful */
102 bool appendFile (const char *dest, const char *src) ;
103
104 /* return the length of the file reference by the given file descriptor */
105 long fileLength (int fd) ;
106
107 bool lockFile (const char *fileName) ;
108 void unlockFile (const char *lockfile) ;
109
110
111 /* return true if file1 is older than file2 */
112 bool isOlder (const char *file1, const char *file2) ;
113
114 /* converts val into a printable string */
115 const char *boolToString (bool val) ;
116
117 /* memory leak checker helper. */
118 void addPointerFreedOnExit (char *pointerToFree) ;
119
120 /* splice direcotory and fname together and return free'able string */
121 char *buildFilename (const char *directory, const char *fname) ;
122
123 /* string the file opened by FP to the give SIZE. The NEWNAME is the name
124    of the file to have after truncation. FP will be reopened for writing on
125    the new file and will be positioned at the end */
126 bool shrinkfile (FILE *fp, long size, char *name, const char *mode) ;
127
128 /* max length of pathname */
129 long pathMax (const char *pathname) ;
130
131 #define ASSERT(x) do{if (!(x))die("assertion -- %s -- failed in file %s line %d",#x,__FILE__,__LINE__);}while(0)
132
133 #define INDENT_INCR 5
134 #define INDENT_BUFFER_SIZE 80
135 #if ! defined (MIN)
136 #define MIN(A,B) ((A) < (B) ? (A) : (B))
137 #endif
138
139 #endif /* misc_h__ */