Minor help text improvements.
[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 /** \cond */
45 #if DEBUG > COMPILE_TIME_LOGLEVEL
46 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
47 #else
48 #define DEBUG_LOG(...) do {;} while (0)
49 #endif
50
51 #if INFO > COMPILE_TIME_LOGLEVEL
52 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
53 #else
54 #define INFO_LOG(...) do {;} while (0)
55 #endif
56
57 #if NOTICE > COMPILE_TIME_LOGLEVEL
58 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
59 #else
60 #define NOTICE_LOG(...) do {;} while (0)
61 #endif
62
63 #if WARNING > COMPILE_TIME_LOGLEVEL
64 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ##  __VA_ARGS__)
65 #else
66 #define WARNING_LOG(...) do {;} while (0)
67 #endif
68
69 #if ERROR > COMPILE_TIME_LOGLEVEL
70 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
71 #else
72 #define ERROR_LOG(...) do {;} while (0)
73 #endif
74
75 #if CRIT > COMPILE_TIME_LOGLEVEL
76 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
77 #else
78 #define CRIT_LOG(...) do {;} while (0)
79 #endif
80
81 #if EMERG > COMPILE_TIME_LOGLEVEL
82 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
83 #else
84 #define EMERG_LOG(...)
85 #endif
86 /** \endcond */
87
88 /**
89  * Wrapper for isspace.
90  * NetBSD needs this.
91  */
92 /*
93  * The values should be cast to an unsigned char first, then to int.
94  * Why? Because the isdigit (as do all other is/to functions/macros)
95  * expect a number from 0 upto and including 255 as their (int) argument.
96  * Because char is signed on most systems, casting it to int immediately
97  * gives the functions an argument between -128 and 127 (inclusive),
98  * which they will use as an array index, and which will thus fail
99  * horribly for characters which have their most significant bit set.
100  */
101 #define adu_isspace(c) isspace((int)(unsigned char)(c))
102
103 /**
104  * Write a log message to a dynamically allocated string.
105  *
106  * \param fmt Usual format string.
107  * \param p Result pointer.
108  *
109  * \sa printf(3). */
110 #define VSPRINTF(fmt, p) \
111 { \
112         int n; \
113         size_t size = 100; \
114         p = adu_malloc(size); \
115         while (1) { \
116                 va_list ap; \
117                 /* Try to print in the allocated space. */ \
118                 va_start(ap, fmt); \
119                 n = vsnprintf(p, size, fmt, ap); \
120                 va_end(ap); \
121                 /* If that worked, return the string. */ \
122                 if (n > -1 && n < size) \
123                         break; \
124                 /* Else try again with more space. */ \
125                 if (n > -1) /* glibc 2.1 */ \
126                         size = n + 1; /* precisely what is needed */ \
127                 else /* glibc 2.0 */ \
128                         size *= 2; /* twice the old size */ \
129                 p = adu_realloc(p, size); \
130         } \
131 }
132
133 /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
134 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
135
136 /** The columns of the directory table. */
137 enum dir_table_columns {
138         /** The name of the directory. */
139         DT_NAME,
140         /** The dir count number. */
141         DT_NUM,
142         /** The number of the parent directory. */
143         DT_PARENT_NUM,
144         /** The number of bytes of all regular files. */
145         DT_BYTES,
146         /** The number of all regular files. */
147         DT_FILES,
148         /** Number of columns in this table. */
149         NUM_DT_COLUMNS
150 };
151
152 extern struct osl_table *dir_table;
153
154 /** The adu command line options. */
155 extern struct gengetopt_args_info conf;
156
157 /** Computed database dir. */
158 extern char *database_dir;
159
160 /**
161  * The select command line options.
162  *
163  * Either given at the command line, or via the \a set command
164  * in interactive mode.
165  */
166 extern struct select_args_info select_conf;
167
168 /**
169  * Compare two osl objects pointing to unsigned integers of 64 bit size.
170  *
171  * \param obj1 Pointer to the first integer.
172  * \param obj2 Pointer to the second integer.
173  *
174  * \return The values required for an osl compare function.
175  *
176  * \sa osl_compare_func, osl_hash_compare().
177  */
178 _static_inline_ int uint64_compare(const struct osl_object *obj1,
179                 const struct osl_object *obj2)
180 {
181         uint64_t d1 = read_u64((const char *)obj1->data);
182         uint64_t d2 = read_u64((const char *)obj2->data);
183
184         if (d1 < d2)
185                 return 1;
186         if (d1 > d2)
187                 return -1;
188         return 0;
189 }
190
191 /**
192  * Compare the size of two directories
193  *
194  * \param obj1 Pointer to the first object.
195  * \param obj2 Pointer to the second object.
196  *
197  * This function first compares the size values as usual integers. If they compare as
198  * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
199  * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
200  */
201 _static_inline_ int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
202 {
203         uint64_t d1 = *(uint64_t *)obj1->data;
204         uint64_t d2 = *(uint64_t *)obj2->data;
205         int ret = NUM_COMPARE(d2, d1);
206
207         if (ret)
208                 return ret;
209         //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
210         return NUM_COMPARE(obj2->data, obj1->data);
211 }
212
213 /* adu.c */
214 __printf_2_3 void __log(int, const char*, ...);
215 int open_dir_table(int create);
216 void check_signals(void);
217 /* create.c */
218 int com_create(void);
219
220 /* interactive.c */
221 void print_interactive_help(void);
222 int com_interactive(void);