Add yet more source code documentation.
[adu.git] / adu.h
1 /*
2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file adu.h \brief Global definitions. */
8
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <time.h> /* time(), localtime() */
15 #include <unistd.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <stdarg.h>
19 #include <inttypes.h>
20 #include <string.h>
21 #include <assert.h>
22 #include <osl.h>
23 #include "gcc-compat.h"
24 #include "portable_io.h"
25
26 /** debug loglevel, gets really noisy */
27 #define DEBUG 1
28 /** still noisy, but won't fill your disk */
29 #define INFO 2
30 /** normal, but significant event */
31 #define NOTICE 3
32 /** unexpected event that can be handled */
33 #define WARNING 4
34 /** unhandled error condition */
35 #define ERROR 5
36 /** system might be unreliable */
37 #define CRIT 6
38 /** last message before exit */
39 #define EMERG 7
40
41 /** Log messages with lower priority than that will not be compiled in. */
42 #define COMPILE_TIME_LOGLEVEL 0
43
44 /**
45 * A variant of static inline that requires the object being documented.
46 *
47 * If doxygen finds the \p static keyword in any context, that part will not be
48 * included in the documentation. However, we want static inline functions in
49 * header files to be documented while static functions in C files and
50 * statically declared variables should be left out. As a workaround for this
51 * flaw we use \p _static_inline_ for static inline functions declared in
52 * header files.
53 */
54 #define _static_inline_ static inline
55
56 /** \cond */
57 #if DEBUG > COMPILE_TIME_LOGLEVEL
58 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
59 #else
60 #define DEBUG_LOG(...) do {;} while (0)
61 #endif
62
63 #if INFO > COMPILE_TIME_LOGLEVEL
64 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
65 #else
66 #define INFO_LOG(...) do {;} while (0)
67 #endif
68
69 #if NOTICE > COMPILE_TIME_LOGLEVEL
70 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
71 #else
72 #define NOTICE_LOG(...) do {;} while (0)
73 #endif
74
75 #if WARNING > COMPILE_TIME_LOGLEVEL
76 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
77 #else
78 #define WARNING_LOG(...) do {;} while (0)
79 #endif
80
81 #if ERROR > COMPILE_TIME_LOGLEVEL
82 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
83 #else
84 #define ERROR_LOG(...) do {;} while (0)
85 #endif
86
87 #if CRIT > COMPILE_TIME_LOGLEVEL
88 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
89 #else
90 #define CRIT_LOG(...) do {;} while (0)
91 #endif
92
93 #if EMERG > COMPILE_TIME_LOGLEVEL
94 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
95 #else
96 #define EMERG_LOG(...)
97 #endif
98 /** \endcond */
99
100 /**
101 * Write a log message to a dynamically allocated string.
102 *
103 * \param fmt Usual format string.
104 * \param p Result pointer.
105 *
106 * \sa printf(3). */
107 #define VSPRINTF(fmt, p) \
108 { \
109 int n; \
110 size_t size = 100; \
111 p = adu_malloc(size); \
112 while (1) { \
113 va_list ap; \
114 /* Try to print in the allocated space. */ \
115 va_start(ap, fmt); \
116 n = vsnprintf(p, size, fmt, ap); \
117 va_end(ap); \
118 /* If that worked, return the string. */ \
119 if (n > -1 && n < size) \
120 break; \
121 /* Else try again with more space. */ \
122 if (n > -1) /* glibc 2.1 */ \
123 size = n + 1; /* precisely what is needed */ \
124 else /* glibc 2.0 */ \
125 size *= 2; /* twice the old size */ \
126 p = adu_realloc(p, size); \
127 } \
128 }
129
130 /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
131 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
132
133 /** The columns of the directory table. */
134 enum dir_table_columns {
135 /** The name of the directory. */
136 DT_NAME,
137 /** The dir count number. */
138 DT_NUM,
139 /** The number of the parent directory. */
140 DT_PARENT_NUM,
141 /** The number of bytes of all regular files. */
142 DT_BYTES,
143 /** The number of all regular files. */
144 DT_FILES,
145 /** Number of columns in this table. */
146 NUM_DT_COLUMNS
147 };
148
149 extern struct osl_table *dir_table;
150
151 /** The adu command line options. */
152 extern struct gengetopt_args_info conf;
153
154 /**
155 * The select command line options.
156 *
157 * Either given at the command line, or via the \a set command
158 * in interactive mode.
159 */
160 extern struct select_args_info select_conf;
161
162 /**
163 * Compare two osl objects pointing to unsigned integers of 64 bit size.
164 *
165 * \param obj1 Pointer to the first integer.
166 * \param obj2 Pointer to the second integer.
167 *
168 * \return The values required for an osl compare function.
169 *
170 * \sa osl_compare_func, osl_hash_compare().
171 */
172 _static_inline_ int uint64_compare(const struct osl_object *obj1,
173 const struct osl_object *obj2)
174 {
175 uint64_t d1 = read_u64((const char *)obj1->data);
176 uint64_t d2 = read_u64((const char *)obj2->data);
177
178 if (d1 < d2)
179 return 1;
180 if (d1 > d2)
181 return -1;
182 return 0;
183 }
184
185 /**
186 * Compare the size of two directories
187 *
188 * \param obj1 Pointer to the first object.
189 * \param obj2 Pointer to the second object.
190 *
191 * This function first compares the size values as usual integers. If they compare as
192 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
193 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
194 */
195 _static_inline_ int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
196 {
197 uint64_t d1 = *(uint64_t *)obj1->data;
198 uint64_t d2 = *(uint64_t *)obj2->data;
199 int ret = NUM_COMPARE(d2, d1);
200
201 if (ret)
202 return ret;
203 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
204 return NUM_COMPARE(obj2->data, obj1->data);
205 }
206
207 /* adu.c */
208 __printf_2_3 void __log(int, const char*, ...);
209 int open_dir_table(int create);
210 void check_signals(void);
211 /* create.c */
212 int com_create(void);
213
214 /* interactive.c */
215 void print_interactive_help(void);
216 int com_interactive(void);