Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / table.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "table.h"
20
21 #include <assert.h>
22
23 #include "dynamic-string.h"
24 #include "json.h"
25 #include "ovsdb-data.h"
26 #include "ovsdb-error.h"
27 #include "timeval.h"
28 #include "util.h"
29
30 struct column {
31     char *heading;
32 };
33
34 static char *
35 cell_to_text(struct cell *cell, const struct table_style *style)
36 {
37     if (!cell->text) {
38         if (cell->json) {
39             if (style->cell_format == CF_JSON || !cell->type) {
40                 cell->text = json_to_string(cell->json, JSSF_SORT);
41             } else {
42                 struct ovsdb_datum datum;
43                 struct ovsdb_error *error;
44                 struct ds s;
45
46                 error = ovsdb_datum_from_json(&datum, cell->type, cell->json,
47                                               NULL);
48                 if (!error) {
49                     ds_init(&s);
50                     if (style->cell_format == CF_STRING) {
51                         ovsdb_datum_to_string(&datum, cell->type, &s);
52                     } else {
53                         ovsdb_datum_to_bare(&datum, cell->type, &s);
54                     }
55                     ovsdb_datum_destroy(&datum, cell->type);
56                     cell->text = ds_steal_cstr(&s);
57                 } else {
58                     cell->text = json_to_string(cell->json, JSSF_SORT);
59                     ovsdb_error_destroy(error);
60                 }
61             }
62         } else {
63             cell->text = xstrdup("");
64         }
65     }
66
67     return cell->text;
68 }
69
70 static void
71 cell_destroy(struct cell *cell)
72 {
73     free(cell->text);
74     json_destroy(cell->json);
75 }
76
77 /* Initializes 'table' as an empty table.
78  *
79  * The caller should then:
80  *
81  * 1. Call table_add_column() once for each column.
82  * 2. For each row:
83  *    2a. Call table_add_row().
84  *    2b. For each column in the cell, call table_add_cell() and fill in
85  *        the returned cell.
86  * 3. Call table_print() to print the final table.
87  * 4. Free the table with table_destroy().
88  */
89 void
90 table_init(struct table *table)
91 {
92     memset(table, 0, sizeof *table);
93 }
94
95 /* Destroys 'table' and frees all associated storage.  (However, the client
96  * owns the 'type' members pointed to by cells, so these are not destroyed.) */
97 void
98 table_destroy(struct table *table)
99 {
100     if (table) {
101         size_t i;
102
103         for (i = 0; i < table->n_columns; i++) {
104             free(table->columns[i].heading);
105         }
106         free(table->columns);
107
108         for (i = 0; i < table->n_columns * table->n_rows; i++) {
109             cell_destroy(&table->cells[i]);
110         }
111         free(table->cells);
112
113         free(table->caption);
114     }
115 }
116
117 /* Sets 'caption' as the caption for 'table'.
118  *
119  * 'table' takes ownership of 'caption'. */
120 void
121 table_set_caption(struct table *table, char *caption)
122 {
123     free(table->caption);
124     table->caption = caption;
125 }
126
127 /* Turns printing a timestamp along with 'table' on or off, according to
128  * 'timestamp'.  */
129 void
130 table_set_timestamp(struct table *table, bool timestamp)
131 {
132     table->timestamp = timestamp;
133 }
134
135 /* Adds a new column to 'table' just to the right of any existing column, with
136  * 'heading' as a title for the column.  'heading' must be a valid printf()
137  * format specifier.
138  *
139  * Columns must be added before any data is put into 'table'. */
140 void
141 table_add_column(struct table *table, const char *heading, ...)
142 {
143     struct column *column;
144     va_list args;
145
146     assert(!table->n_rows);
147     if (table->n_columns >= table->allocated_columns) {
148         table->columns = x2nrealloc(table->columns, &table->allocated_columns,
149                                     sizeof *table->columns);
150     }
151     column = &table->columns[table->n_columns++];
152
153     va_start(args, heading);
154     column->heading = xvasprintf(heading, args);
155     va_end(args);
156 }
157
158 static struct cell *
159 table_cell__(const struct table *table, size_t row, size_t column)
160 {
161     return &table->cells[column + row * table->n_columns];
162 }
163
164 /* Adds a new row to 'table'.  The table's columns must already have been added
165  * with table_add_column().
166  *
167  * The row is initially empty; use table_add_cell() to start filling it in. */
168 void
169 table_add_row(struct table *table)
170 {
171     size_t x, y;
172
173     if (table->n_rows >= table->allocated_rows) {
174         table->cells = x2nrealloc(table->cells, &table->allocated_rows,
175                                   table->n_columns * sizeof *table->cells);
176     }
177
178     y = table->n_rows++;
179     table->current_column = 0;
180     for (x = 0; x < table->n_columns; x++) {
181         struct cell *cell = table_cell__(table, y, x);
182         memset(cell, 0, sizeof *cell);
183     }
184 }
185
186 /* Adds a new cell in the current row of 'table', which must have been added
187  * with table_add_row().  Cells are filled in the same order that the columns
188  * were added with table_add_column().
189  *
190  * The caller is responsible for filling in the returned cell, in one of two
191  * fashions:
192  *
193  *   - If the cell should contain an ovsdb_datum, formatted according to the
194  *     table style, then fill in the 'json' member with the JSON representation
195  *     of the datum and 'type' with its type.
196  *
197  *   - If the cell should contain a fixed text string, then the caller should
198  *     assign that string to the 'text' member.  This is undesirable if the
199  *     cell actually contains OVSDB data because 'text' cannot be formatted
200  *     according to the table style; it is always output verbatim.
201  */
202 struct cell *
203 table_add_cell(struct table *table)
204 {
205     size_t x, y;
206
207     assert(table->n_rows > 0);
208     assert(table->current_column < table->n_columns);
209
210     x = table->current_column++;
211     y = table->n_rows - 1;
212
213     return table_cell__(table, y, x);
214 }
215
216 static void
217 table_print_table_line__(struct ds *line)
218 {
219     puts(ds_cstr(line));
220     ds_clear(line);
221 }
222
223 static void
224 table_format_timestamp__(char *s, size_t size)
225 {
226     time_t now = time_wall();
227     strftime(s, size, "%Y-%m-%d %H:%M:%S", localtime(&now));
228 }
229
230 static void
231 table_print_timestamp__(const struct table *table)
232 {
233     if (table->timestamp) {
234         char s[32];
235
236         table_format_timestamp__(s, sizeof s);
237         puts(s);
238     }
239 }
240
241 static void
242 table_print_table__(const struct table *table, const struct table_style *style)
243 {
244     static int n = 0;
245     struct ds line = DS_EMPTY_INITIALIZER;
246     int *widths;
247     size_t x, y;
248
249     if (n++ > 0) {
250         putchar('\n');
251     }
252
253     table_print_timestamp__(table);
254
255     if (table->caption) {
256         puts(table->caption);
257     }
258
259     widths = xmalloc(table->n_columns * sizeof *widths);
260     for (x = 0; x < table->n_columns; x++) {
261         const struct column *column = &table->columns[x];
262
263         widths[x] = strlen(column->heading);
264         for (y = 0; y < table->n_rows; y++) {
265             const char *text = cell_to_text(table_cell__(table, y, x), style);
266             size_t length = strlen(text);
267
268             if (length > widths[x]) {
269                 widths[x] = length;
270             }
271         }
272     }
273
274     if (style->headings) {
275         for (x = 0; x < table->n_columns; x++) {
276             const struct column *column = &table->columns[x];
277             if (x) {
278                 ds_put_char(&line, ' ');
279             }
280             ds_put_format(&line, "%-*s", widths[x], column->heading);
281         }
282         table_print_table_line__(&line);
283
284         for (x = 0; x < table->n_columns; x++) {
285             if (x) {
286                 ds_put_char(&line, ' ');
287             }
288             ds_put_char_multiple(&line, '-', widths[x]);
289         }
290         table_print_table_line__(&line);
291     }
292
293     for (y = 0; y < table->n_rows; y++) {
294         for (x = 0; x < table->n_columns; x++) {
295             const char *text = cell_to_text(table_cell__(table, y, x), style);
296             if (x) {
297                 ds_put_char(&line, ' ');
298             }
299             ds_put_format(&line, "%-*s", widths[x], text);
300         }
301         table_print_table_line__(&line);
302     }
303
304     ds_destroy(&line);
305     free(widths);
306 }
307
308 static void
309 table_print_list__(const struct table *table, const struct table_style *style)
310 {
311     static int n = 0;
312     size_t x, y;
313
314     if (n++ > 0) {
315         putchar('\n');
316     }
317
318     table_print_timestamp__(table);
319
320     if (table->caption) {
321         puts(table->caption);
322     }
323
324     for (y = 0; y < table->n_rows; y++) {
325         if (y > 0) {
326             putchar('\n');
327         }
328         for (x = 0; x < table->n_columns; x++) {
329             const char *text = cell_to_text(table_cell__(table, y, x), style);
330             if (style->headings) {
331                 printf("%-20s: ", table->columns[x].heading);
332             }
333             puts(text);
334         }
335     }
336 }
337
338 static void
339 table_escape_html_text__(const char *s, size_t n)
340 {
341     size_t i;
342
343     for (i = 0; i < n; i++) {
344         char c = s[i];
345
346         switch (c) {
347         case '&':
348             fputs("&amp;", stdout);
349             break;
350         case '<':
351             fputs("&lt;", stdout);
352             break;
353         case '>':
354             fputs("&gt;", stdout);
355             break;
356         case '"':
357             fputs("&quot;", stdout);
358             break;
359         default:
360             putchar(c);
361             break;
362         }
363     }
364 }
365
366 static void
367 table_print_html_cell__(const char *element, const char *content)
368 {
369     const char *p;
370
371     printf("    <%s>", element);
372     for (p = content; *p; ) {
373         struct uuid uuid;
374
375         if (uuid_from_string_prefix(&uuid, p)) {
376             printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
377             p += UUID_LEN;
378         } else {
379             table_escape_html_text__(p, 1);
380             p++;
381         }
382     }
383     printf("</%s>\n", element);
384 }
385
386 static void
387 table_print_html__(const struct table *table, const struct table_style *style)
388 {
389     size_t x, y;
390
391     table_print_timestamp__(table);
392
393     fputs("<table border=1>\n", stdout);
394
395     if (table->caption) {
396         table_print_html_cell__("caption", table->caption);
397     }
398
399     if (style->headings) {
400         fputs("  <tr>\n", stdout);
401         for (x = 0; x < table->n_columns; x++) {
402             const struct column *column = &table->columns[x];
403             table_print_html_cell__("th", column->heading);
404         }
405         fputs("  </tr>\n", stdout);
406     }
407
408     for (y = 0; y < table->n_rows; y++) {
409         fputs("  <tr>\n", stdout);
410         for (x = 0; x < table->n_columns; x++) {
411             const char *content;
412
413             content = cell_to_text(table_cell__(table, y, x), style);
414             if (!strcmp(table->columns[x].heading, "_uuid")) {
415                 fputs("    <td><a name=\"", stdout);
416                 table_escape_html_text__(content, strlen(content));
417                 fputs("\">", stdout);
418                 table_escape_html_text__(content, 8);
419                 fputs("</a></td>\n", stdout);
420             } else {
421                 table_print_html_cell__("td", content);
422             }
423         }
424         fputs("  </tr>\n", stdout);
425     }
426
427     fputs("</table>\n", stdout);
428 }
429
430 static void
431 table_print_csv_cell__(const char *content)
432 {
433     const char *p;
434
435     if (!strpbrk(content, "\n\",")) {
436         fputs(content, stdout);
437     } else {
438         putchar('"');
439         for (p = content; *p != '\0'; p++) {
440             switch (*p) {
441             case '"':
442                 fputs("\"\"", stdout);
443                 break;
444             default:
445                 putchar(*p);
446                 break;
447             }
448         }
449         putchar('"');
450     }
451 }
452
453 static void
454 table_print_csv__(const struct table *table, const struct table_style *style)
455 {
456     static int n = 0;
457     size_t x, y;
458
459     if (n++ > 0) {
460         putchar('\n');
461     }
462
463     table_print_timestamp__(table);
464
465     if (table->caption) {
466         puts(table->caption);
467     }
468
469     if (style->headings) {
470         for (x = 0; x < table->n_columns; x++) {
471             const struct column *column = &table->columns[x];
472             if (x) {
473                 putchar(',');
474             }
475             table_print_csv_cell__(column->heading);
476         }
477         putchar('\n');
478     }
479
480     for (y = 0; y < table->n_rows; y++) {
481         for (x = 0; x < table->n_columns; x++) {
482             if (x) {
483                 putchar(',');
484             }
485             table_print_csv_cell__(cell_to_text(table_cell__(table, y, x),
486                                                 style));
487         }
488         putchar('\n');
489     }
490 }
491
492 static void
493 table_print_json__(const struct table *table, const struct table_style *style)
494 {
495     struct json *json, *headings, *data;
496     size_t x, y;
497     char *s;
498
499     json = json_object_create();
500     if (table->caption) {
501         json_object_put_string(json, "caption", table->caption);
502     }
503     if (table->timestamp) {
504         char s[32];
505
506         table_format_timestamp__(s, sizeof s);
507         json_object_put_string(json, "time", s);
508     }
509
510     headings = json_array_create_empty();
511     for (x = 0; x < table->n_columns; x++) {
512         const struct column *column = &table->columns[x];
513         json_array_add(headings, json_string_create(column->heading));
514     }
515     json_object_put(json, "headings", headings);
516
517     data = json_array_create_empty();
518     for (y = 0; y < table->n_rows; y++) {
519         struct json *row = json_array_create_empty();
520         for (x = 0; x < table->n_columns; x++) {
521             const struct cell *cell = table_cell__(table, y, x);
522             if (cell->text) {
523                 json_array_add(row, json_string_create(cell->text));
524             } else if (cell->json) {
525                 json_array_add(row, json_clone(cell->json));
526             } else {
527                 json_array_add(row, json_null_create());
528             }
529         }
530         json_array_add(data, row);
531     }
532     json_object_put(json, "data", data);
533
534     s = json_to_string(json, style->json_flags);
535     json_destroy(json);
536     puts(s);
537     free(s);
538 }
539 \f
540 /* Parses 'format' as the argument to a --format command line option, updating
541  * 'style->format'. */
542 void
543 table_parse_format(struct table_style *style, const char *format)
544 {
545     if (!strcmp(format, "table")) {
546         style->format = TF_TABLE;
547     } else if (!strcmp(format, "list")) {
548         style->format = TF_LIST;
549     } else if (!strcmp(format, "html")) {
550         style->format = TF_HTML;
551     } else if (!strcmp(format, "csv")) {
552         style->format = TF_CSV;
553     } else if (!strcmp(format, "json")) {
554         style->format = TF_JSON;
555     } else {
556         ovs_fatal(0, "unknown output format \"%s\"", format);
557     }
558 }
559
560 /* Parses 'format' as the argument to a --data command line option, updating
561  * 'style->cell_format'. */
562 void
563 table_parse_cell_format(struct table_style *style, const char *format)
564 {
565     if (!strcmp(format, "string")) {
566         style->cell_format = CF_STRING;
567     } else if (!strcmp(format, "bare")) {
568         style->cell_format = CF_BARE;
569     } else if (!strcmp(format, "json")) {
570         style->cell_format = CF_JSON;
571     } else {
572         ovs_fatal(0, "unknown data format \"%s\"", format);
573     }
574 }
575
576 /* Outputs 'table' on stdout in the specified 'style'. */
577 void
578 table_print(const struct table *table, const struct table_style *style)
579 {
580     switch (style->format) {
581     case TF_TABLE:
582         table_print_table__(table, style);
583         break;
584
585     case TF_LIST:
586         table_print_list__(table, style);
587         break;
588
589     case TF_HTML:
590         table_print_html__(table, style);
591         break;
592
593     case TF_CSV:
594         table_print_csv__(table, style);
595         break;
596
597     case TF_JSON:
598         table_print_json__(table, style);
599         break;
600     }
601 }