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