]> git.tuebingen.mpg.de Git - adu.git/blob - format.c
3d1daa2aca20fde5f7b993bb01d6b178818f8d3b
[adu.git] / format.c
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 format.c Functions for pretty-printing numbers and strings. */
8
9 #include <dirent.h> /* readdir() */
10 #include "adu.h"
11 #include "gcc-compat.h"
12 #include "fd.h"
13 #include "string.h"
14 #include "error.h"
15 #include "format.h"
16 enum alignment {ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER};
17
18 struct num_format {
19         enum alignment align;
20         char unit;
21         int supress_unit;
22 };
23
24 struct string_format {
25         enum alignment align;
26 };
27
28 struct const_string {
29         char *string;
30 };
31
32 union atom_format {
33         struct string_format sf;
34         struct num_format nf;
35         struct const_string cs;
36 };
37
38 struct format_item {
39         struct atom *atom_ptr;
40         unsigned int width;
41         union atom_format af;
42 };
43
44 struct format_info {
45         struct atom *atoms;
46         struct format_item **items;
47 };
48
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <inttypes.h>
53
54
55 static const char units[] = "bkmgth KMGTH";
56 /*
57  * In a format string, find the next occurrence of %(atom).
58  */
59 static char *find_next(char *cp)
60 {
61         while (*cp) {
62                 if (*cp == '%') {
63                         /* %( is the start of an atom;
64                          * %% is a quoted per-cent.
65                          */
66                         if (cp[1] == '(')
67                                 return cp;
68                         else if (cp[1] == '%')
69                                 cp++; /* skip over two % */
70                         /* otherwise this is a singleton, literal % */
71                 }
72                 cp++;
73         }
74         return NULL;
75 }
76
77 static struct format_item *make_const_string(char *cp, char *ep)
78 {
79         struct format_item *fi = malloc(sizeof *fi);
80         char *op;
81
82         fi->atom_ptr = NULL;
83         fi->width = ep - cp;
84         op = fi->af.cs.string = malloc(fi->width + 1);
85         while (*cp && (!ep || cp < ep)) {
86                 if (*cp == '%' && cp[1] == '%') {
87                         cp++;
88                         fi->width--;
89                 }
90                 *op = *cp;
91                 cp++;
92                 op++;
93         }
94         *op = '\0';
95         return fi;
96 }
97
98 static struct format_item *parse_atom(char *ap, struct atom *atoms)
99 {
100         struct format_item *fi = NULL;
101         int i, n, len;
102         char *col, *ep = strchr(ap, ')');
103         char *err_msg = "malformed format string";
104
105         if (!ep)
106                 goto err;
107         col = memchr(ap, ':', ep - ap);
108         if (col)
109                 len = col - ap;
110         else
111                 len = ep - ap;
112         for (i = 0; atoms[i].name; i++) {
113                 int j;
114                 struct atom *a = atoms + i;
115                 if (strlen(a->name) != len)
116                         continue;
117                 if (strncmp(a->name, ap, len))
118                         continue;
119                 fi = malloc(sizeof(*fi));
120                 fi->atom_ptr = a;
121                 /* set defaults */
122                 fi->width = 0;
123                 switch (a->type) {
124                 case AT_STRING:
125                         fi->af.sf.align = ALIGN_LEFT;
126                         break;
127                 case AT_ID:
128                         fi->af.nf.align = ALIGN_RIGHT;
129                         fi->af.nf.unit = ' ';
130                         fi->af.nf.supress_unit = 0;
131                         break;
132                 case AT_COUNT:
133                         fi->af.nf.align = ALIGN_RIGHT;
134                         fi->af.nf.unit = 'H';
135                         fi->af.nf.supress_unit = 0;
136                         break;
137                 case AT_SIZE:
138                         fi->af.nf.align = ALIGN_RIGHT;
139                         fi->af.nf.unit = 'h';
140                         fi->af.nf.supress_unit = 0;
141                         break;
142                 }
143                 if (!col)
144                         goto success;
145                 /* read alignment */
146                 switch (col[1]) {
147                 case 'l':
148                         fi->af.sf.align = ALIGN_LEFT;
149                         break;
150                 case 'r':
151                         fi->af.nf.align = ALIGN_RIGHT;
152                         break;
153                 case 'c':
154                         fi->af.nf.align = ALIGN_CENTER;
155                         break;
156                 case ':': /* no alignment spec is OK */
157                         col--;
158                         break;
159                 default:
160                         err_msg = "bad alignment spec";
161                         goto err;
162                 }
163                 switch (col[2]) {
164                 case ')':
165                         goto success;
166                 case ':':
167                         col += 2;
168                         break;
169                 default:
170                         err_msg = "trailing garbage after alignment spec";
171                         goto err;
172                 }
173                 /* read width */
174                 n = 0;
175                 sscanf(col + 1, "%u%n", &fi->width, &n);
176                 /* read unit */
177                 switch (col[1 + n]) {
178                 case ')':
179                         goto success;
180                 case ':':
181                         col += 1 + n;
182                         break;
183                 default:
184                         err_msg = "trailing garbage after width spec";
185                         goto err;
186                 }
187                 if (a->type != AT_SIZE && a->type != AT_COUNT) {
188                         err_msg = "no unit allowed here";
189                         goto err;
190                 }
191                 if (col[1] == '*') {
192                         fi->af.nf.supress_unit = 1;
193                         col++;
194                 }
195                 for (j = 0; units[j]; j++) {
196                         if (units[j] != col[1])
197                                 continue;
198                         fi->af.nf.unit = units[j];
199                         break;
200                 }
201                 if (!units[j]) {
202                         err_msg = "bad unit spec";
203                         goto err;
204                 }
205                 if (col + 2 != ep) {
206                         err_msg = "trailing garbage after unit spec";
207                         goto err;
208                 }
209                 goto success;
210         }
211         err_msg = "invalid atom";
212 err:
213         ERROR_LOG("%s\n", err_msg);
214         free(fi);
215         return NULL;
216 success:
217         return fi;
218 }
219
220 struct format_info *parse_format_string(char *fmt, struct atom *atoms)
221 {
222         char *cp, *ap, *ep;
223         int num_items;
224         struct format_info *info = malloc(sizeof(*info));
225
226         info->atoms = atoms;
227         info->items = NULL;
228         for (cp = fmt, num_items = 0; *cp && (ap = find_next(cp));
229                         cp = ep + 1, num_items++) {
230                 if (cp < ap) {
231                         info->items = realloc(info->items,
232                                 (num_items + 1) * sizeof(struct format_info *));
233                         info->items[num_items] = make_const_string(cp, ap);
234                         num_items++;
235                 }
236                 info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
237                 info->items[num_items] = parse_atom(ap + 2, atoms);
238                 if (!info->items[num_items]) {
239                         num_items--;
240                         goto err;
241                 }
242                 ep = strchr(ap, ')');
243         }
244         if (*cp) {
245                 ep = cp + strlen(cp);
246                 info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
247                 info->items[num_items] = make_const_string(cp, ep);
248                 num_items++;
249         }
250         info->items = realloc(info->items, (num_items + 1) * sizeof(struct format_info *));
251         info->items[num_items] = NULL;
252         return info;
253 err:
254         for (; num_items >= 0; num_items--) {
255                 if (!info->items[num_items]->atom_ptr)
256                         free(info->items[num_items]->af.cs.string);
257                 free(info->items[num_items]);
258         }
259         free(info->items);
260         free(info);
261         return NULL;
262 }
263
264 void free_format_info(struct format_info *info)
265 {
266         int i;
267         struct format_item *item;
268
269         for (i = 0; (item = info->items[i]); i++) {
270                 if (!item->atom_ptr)
271                         free(item->af.cs.string);
272                 free(info->items[i]);
273         }
274         free(info->items);
275         free(info);
276 }
277
278 static char *align_string(const char *src, unsigned int width, enum alignment align)
279 {
280         int len;
281
282         if (!width)
283                 return adu_strdup(src);
284         if (align == ALIGN_LEFT)
285                 return make_message("%-*s", width, src);
286         if (align == ALIGN_RIGHT)
287                 return make_message("%*s", width, src);
288         len = strlen(src);
289         return make_message("%*s%*s", (width + len) / 2, src,
290                 width - (width + len) / 2, "");
291 }
292
293 /** Compute the number of (decimal) digits of a number. */
294 #define GET_NUM_DIGITS(x, num) { \
295         typeof((x)) _tmp = x; \
296         *num = 1; \
297         if ((x)) \
298                 while ((_tmp) > 9) { \
299                         (_tmp) /= 10; \
300                         (*num)++; \
301                 } \
302 } \
303
304 static long long unsigned normalize_number(long long unsigned num, char unit,
305         char *effective_unit)
306 {
307         long long unsigned normalized_num;
308
309         if (unit == 'h') {
310                 if (num < 1024ULL)
311                         *effective_unit = ' ';
312                 else if (num < 1024ULL * 1024ULL)
313                         *effective_unit = 'k';
314                 else if (num < 1024ULL * 1024ULL * 1024ULL)
315                         *effective_unit = 'm';
316                 else if (num < 1024ULL * 1024ULL * 1024ULL * 1024ULL)
317                         *effective_unit = 'g';
318                 else
319                         *effective_unit = 't';
320         } else if (unit == 'H') {
321                 if (num < 1000ULL)
322                         *effective_unit = ' ';
323                 else if (num < 1000ULL * 1000ULL)
324                         *effective_unit = 'K';
325                 else if (num < 1000ULL * 1000ULL * 1000ULL)
326                         *effective_unit = 'M';
327                 else if (num < 1000ULL * 1000ULL * 1000ULL * 1000ULL)
328                         *effective_unit = 'G';
329                 else
330                         *effective_unit = 'T';
331         } else
332                 *effective_unit = unit;
333         switch (*effective_unit) {
334         case ' ':
335         case 'b': normalized_num = num; break;
336         case 'k': normalized_num = num / 1024; break;
337         case 'm': normalized_num = num / 1024 / 1024; break;
338         case 'g': normalized_num = num / 1024 / 1024 / 1024; break;
339         case 't': normalized_num = num / 1024 / 1024 / 1024 / 1024; break;
340         case 'K': normalized_num = num / 1000; break;
341         case 'M': normalized_num = num / 1000 / 1000; break;
342         case 'G': normalized_num = num / 1000 / 1000 / 1000; break;
343         case 'T': normalized_num = num / 1000 / 1000 / 1000 / 1000; break;
344         default:
345                 EMERG_LOG("BUG: invalid unit %c\n", *effective_unit);
346                 exit(1);
347         }
348         return normalized_num;
349 }
350
351 static void get_unit_postfix(struct num_format *nf, char eu, enum atom_type type,
352                 char postfix[2])
353 {
354         if (nf->supress_unit) {
355                 *postfix = '\0';
356                 return;
357         }
358         postfix[0] = eu;
359         postfix[1] = '\0';
360         if (eu != ' ')
361                 return;
362         if (nf->unit != 'h' && nf->unit != 'H')
363                 return;
364         if (type != AT_SIZE)
365                 return;
366         postfix[0] = 'b'; /* bytes */
367 }
368
369 static char *align_unsigned_int(long long unsigned num, unsigned int width,
370                 struct num_format *nf, enum atom_type type)
371 {
372         char eu; /* effective unit */
373         long long unsigned nnum = normalize_number(num, nf->unit, &eu);
374         char postfix[2] = "\0\0";
375         int len;
376
377         get_unit_postfix(nf, eu, type, postfix);
378         if (!width)
379                 return make_message("%llu%s", nnum, postfix);
380         GET_NUM_DIGITS(nnum, &len);
381         len += strlen(postfix);
382         if (len > width)
383                 width = len;
384         if (nf->align == ALIGN_LEFT)
385                 return make_message("%llu%s%*s", nnum, postfix, width - len, "");
386         if (nf->align == ALIGN_RIGHT)
387                 return make_message("%*s%llu%s", width - len, "", nnum, postfix);
388         return make_message("%*llu%s%*s", (width + len) / 2,
389                 nnum, postfix, width - (width + len) / 2, "");
390 }
391
392 char *format_items(struct format_info *info, union atom_value *values)
393 {
394         int i;
395         char *buf = NULL;
396
397         for (i = 0; info->items[i]; i++) {
398                 struct atom *a;
399                 struct format_item *fi = info->items[i];
400                 union atom_format *af = &fi->af;
401                 enum alignment align;
402                 enum atom_type type;
403                 char *val;
404                 int idx;
405
406                 if (!fi->atom_ptr) { /* const string */
407                         buf = adu_strcat(buf, af->cs.string);
408                         continue;
409                 }
410                 a = fi->atom_ptr;
411                 type = a->type;
412                 idx = a - info->atoms;
413
414                 if (type == AT_STRING) {
415                         align = af->sf.align;
416                         val = align_string(values[idx].string_value, fi->width, align);
417                 } else {
418                         char unit;
419                         align = af->nf.align;
420                         unit = af->nf.unit;
421                         val = align_unsigned_int(values[idx].num_value,
422                                 fi->width, &af->nf, type);
423                 }
424                 buf = adu_strcat(buf, val);
425         }
426         return buf;
427 }