]> git.proxmox.com Git - mirror_ovs.git/blob - lib/table.c
cirrus: Use FreeBSD 12.2.
[mirror_ovs.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 "openvswitch/dynamic-string.h"
22 #include "openvswitch/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_finish_line(struct ds *s)
216 {
217 while (ds_last(s) == ' ') {
218 s->length--;
219 }
220 ds_put_char(s, '\n');
221 }
222
223 static char *
224 table_format_timestamp__(void)
225 {
226 return xastrftime_msec("%Y-%m-%d %H:%M:%S.###", time_wall_msec(), true);
227 }
228
229 static void
230 table_print_timestamp__(const struct table *table, struct ds *s)
231 {
232 if (table->timestamp) {
233 char *timestamp = table_format_timestamp__();
234 ds_put_format(s, "%s\n", timestamp);
235 free(timestamp);
236 }
237 }
238
239 static bool first_table = true;
240
241 static void
242 table_print_table__(const struct table *table, const struct table_style *style,
243 struct ds *s)
244 {
245 int *widths;
246 size_t x, y;
247
248 if (first_table) {
249 first_table = false;
250 } else {
251 ds_put_char(s, '\n');
252 }
253
254 table_print_timestamp__(table, s);
255
256 if (table->caption) {
257 ds_put_format(s, "%s\n", table->caption);
258 }
259
260 widths = xzalloc(table->n_columns * sizeof *widths);
261 for (x = 0; x < table->n_columns; x++) {
262 const struct column *column = &table->columns[x];
263
264 int w = 0;
265 for (y = 0; y < table->n_rows; y++) {
266 const char *text = cell_to_text(table_cell__(table, y, x), style);
267 size_t length = strlen(text);
268
269 if (length > w) {
270 w = length;
271 }
272 }
273
274 int max = style->max_column_width;
275 if (max > 0 && w > max) {
276 w = max;
277 }
278 if (style->headings) {
279 int min = strlen(column->heading);
280 if (w < min) {
281 w = min;
282 }
283 }
284 widths[x] = w;
285 }
286
287 if (style->headings) {
288 for (x = 0; x < table->n_columns; x++) {
289 const struct column *column = &table->columns[x];
290 if (x) {
291 ds_put_char(s, ' ');
292 }
293 ds_put_format(s, "%-*s", widths[x], column->heading);
294 }
295 table_finish_line(s);
296
297 for (x = 0; x < table->n_columns; x++) {
298 if (x) {
299 ds_put_char(s, ' ');
300 }
301 ds_put_char_multiple(s, '-', widths[x]);
302 }
303 table_finish_line(s);
304 }
305
306 for (y = 0; y < table->n_rows; y++) {
307 for (x = 0; x < table->n_columns; x++) {
308 const char *text = cell_to_text(table_cell__(table, y, x), style);
309 if (x) {
310 ds_put_char(s, ' ');
311 }
312 ds_put_format(s, "%-*.*s", widths[x], widths[x], text);
313 }
314 table_finish_line(s);
315 }
316
317 free(widths);
318 }
319
320 static void
321 table_print_list__(const struct table *table, const struct table_style *style,
322 struct ds *s)
323 {
324 size_t x, y;
325
326 if (first_table) {
327 first_table = false;
328 } else {
329 ds_put_char(s, '\n');
330 }
331
332 table_print_timestamp__(table, s);
333
334 if (table->caption) {
335 ds_put_format(s, "%s\n", table->caption);
336 }
337
338 for (y = 0; y < table->n_rows; y++) {
339 if (y > 0) {
340 ds_put_char(s, '\n');
341 }
342 for (x = 0; x < table->n_columns; x++) {
343 const char *text = cell_to_text(table_cell__(table, y, x), style);
344 if (style->headings) {
345 ds_put_format(s, "%-20s: ", table->columns[x].heading);
346 }
347 ds_put_format(s, "%s\n", text);
348 }
349 }
350 }
351
352 static void
353 table_escape_html_text__(const char *content, size_t n, struct ds *s)
354 {
355 if (!strpbrk(content, "&<>\"")) {
356 ds_put_buffer(s, content, n);
357 } else {
358 size_t i;
359
360 for (i = 0; i < n; i++) {
361 char c = content[i];
362
363 switch (c) {
364 case '&':
365 ds_put_cstr(s, "&amp;");
366 break;
367 case '<':
368 ds_put_cstr(s, "&lt;");
369 break;
370 case '>':
371 ds_put_cstr(s, "&gt;");
372 break;
373 case '"':
374 ds_put_cstr(s, "&quot;");
375 break;
376 default:
377 ds_put_char(s, c);
378 break;
379 }
380 }
381 }
382 }
383
384 static void
385 table_print_html_cell__(const char *element, const char *content, struct ds *s)
386 {
387 const char *p;
388
389 ds_put_format(s, " <%s>", element);
390 for (p = content; *p; ) {
391 struct uuid uuid;
392
393 if (uuid_from_string_prefix(&uuid, p)) {
394 ds_put_format(s, "<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
395 p += UUID_LEN;
396 } else {
397 table_escape_html_text__(p, 1, s);
398 p++;
399 }
400 }
401 ds_put_format(s, "</%s>\n", element);
402 }
403
404 static void
405 table_print_html__(const struct table *table, const struct table_style *style,
406 struct ds *s)
407 {
408 size_t x, y;
409
410 table_print_timestamp__(table, s);
411
412 ds_put_cstr(s, "<table border=1>\n");
413
414 if (table->caption) {
415 table_print_html_cell__("caption", table->caption, s);
416 }
417
418 if (style->headings) {
419 ds_put_cstr(s, " <tr>\n");
420 for (x = 0; x < table->n_columns; x++) {
421 const struct column *column = &table->columns[x];
422 table_print_html_cell__("th", column->heading, s);
423 }
424 ds_put_cstr(s, " </tr>\n");
425 }
426
427 for (y = 0; y < table->n_rows; y++) {
428 ds_put_cstr(s, " <tr>\n");
429 for (x = 0; x < table->n_columns; x++) {
430 const char *content;
431
432 content = cell_to_text(table_cell__(table, y, x), style);
433 if (!strcmp(table->columns[x].heading, "_uuid")) {
434 ds_put_cstr(s, " <td><a name=\"");
435 table_escape_html_text__(content, strlen(content), s);
436 ds_put_cstr(s, "\">");
437 table_escape_html_text__(content, 8, s);
438 ds_put_cstr(s, "</a></td>\n");
439 } else {
440 table_print_html_cell__("td", content, s);
441 }
442 }
443 ds_put_cstr(s, " </tr>\n");
444 }
445
446 ds_put_cstr(s, "</table>\n");
447 }
448
449 static void
450 table_print_csv_cell__(const char *content, struct ds *s)
451 {
452 const char *p;
453
454 if (!strpbrk(content, "\n\",")) {
455 ds_put_cstr(s, content);
456 } else {
457 ds_put_char(s, '"');
458 for (p = content; *p != '\0'; p++) {
459 switch (*p) {
460 case '"':
461 ds_put_cstr(s, "\"\"");
462 break;
463 default:
464 ds_put_char(s, *p);
465 break;
466 }
467 }
468 ds_put_char(s, '"');
469 }
470 }
471
472 static void
473 table_print_csv__(const struct table *table, const struct table_style *style,
474 struct ds *s)
475 {
476 size_t x, y;
477
478 if (first_table) {
479 first_table = false;
480 } else {
481 ds_put_char(s, '\n');
482 }
483
484 table_print_timestamp__(table, s);
485
486 if (table->caption) {
487 ds_put_format(s, "%s\n", table->caption);
488 }
489
490 if (style->headings) {
491 for (x = 0; x < table->n_columns; x++) {
492 const struct column *column = &table->columns[x];
493 if (x) {
494 ds_put_char(s, ',');
495 }
496 table_print_csv_cell__(column->heading, s);
497 }
498 ds_put_char(s, '\n');
499 }
500
501 for (y = 0; y < table->n_rows; y++) {
502 for (x = 0; x < table->n_columns; x++) {
503 if (x) {
504 ds_put_char(s, ',');
505 }
506 table_print_csv_cell__(cell_to_text(table_cell__(table, y, x),
507 style), s);
508 }
509 ds_put_char(s, '\n');
510 }
511 }
512
513 static void
514 table_print_json__(const struct table *table, const struct table_style *style,
515 struct ds *s)
516 {
517 struct json *json, *headings, *data;
518 size_t x, y;
519
520 json = json_object_create();
521 if (table->caption) {
522 json_object_put_string(json, "caption", table->caption);
523 }
524 if (table->timestamp) {
525 json_object_put_nocopy(
526 json, "time",
527 json_string_create_nocopy(table_format_timestamp__()));
528 }
529
530 headings = json_array_create_empty();
531 for (x = 0; x < table->n_columns; x++) {
532 const struct column *column = &table->columns[x];
533 json_array_add(headings, json_string_create(column->heading));
534 }
535 json_object_put(json, "headings", headings);
536
537 data = json_array_create_empty();
538 for (y = 0; y < table->n_rows; y++) {
539 struct json *row = json_array_create_empty();
540 for (x = 0; x < table->n_columns; x++) {
541 const struct cell *cell = table_cell__(table, y, x);
542 if (cell->text) {
543 json_array_add(row, json_string_create(cell->text));
544 } else if (cell->json) {
545 json_array_add(row, json_clone(cell->json));
546 } else {
547 json_array_add(row, json_null_create());
548 }
549 }
550 json_array_add(data, row);
551 }
552 json_object_put(json, "data", data);
553
554 json_to_ds(json, style->json_flags, s);
555 ds_put_char(s, '\n');
556 json_destroy(json);
557 }
558 \f
559 /* Parses 'format' as the argument to a --format command line option, updating
560 * 'style->format'. */
561 void
562 table_parse_format(struct table_style *style, const char *format)
563 {
564 if (!strcmp(format, "table")) {
565 style->format = TF_TABLE;
566 } else if (!strcmp(format, "list")) {
567 style->format = TF_LIST;
568 } else if (!strcmp(format, "html")) {
569 style->format = TF_HTML;
570 } else if (!strcmp(format, "csv")) {
571 style->format = TF_CSV;
572 } else if (!strcmp(format, "json")) {
573 style->format = TF_JSON;
574 } else {
575 ovs_fatal(0, "unknown output format \"%s\"", format);
576 }
577 }
578
579 /* Parses 'format' as the argument to a --data command line option, updating
580 * 'style->cell_format'. */
581 void
582 table_parse_cell_format(struct table_style *style, const char *format)
583 {
584 if (!strcmp(format, "string")) {
585 style->cell_format = CF_STRING;
586 } else if (!strcmp(format, "bare")) {
587 style->cell_format = CF_BARE;
588 } else if (!strcmp(format, "json")) {
589 style->cell_format = CF_JSON;
590 } else {
591 ovs_fatal(0, "unknown data format \"%s\"", format);
592 }
593 }
594
595 void
596 table_format(const struct table *table, const struct table_style *style,
597 struct ds *s)
598 {
599 switch (style->format) {
600 case TF_TABLE:
601 table_print_table__(table, style, s);
602 break;
603
604 case TF_LIST:
605 table_print_list__(table, style, s);
606 break;
607
608 case TF_HTML:
609 table_print_html__(table, style, s);
610 break;
611
612 case TF_CSV:
613 table_print_csv__(table, style, s);
614 break;
615
616 case TF_JSON:
617 table_print_json__(table, style, s);
618 break;
619 }
620 }
621
622 void
623 table_format_reset(void)
624 {
625 first_table = true;
626 }
627
628 /* Outputs 'table' on stdout in the specified 'style'. */
629 void
630 table_print(const struct table *table, const struct table_style *style)
631 {
632 struct ds s = DS_EMPTY_INITIALIZER;
633 table_format(table, style, &s);
634 fputs(ds_cstr(&s), stdout);
635 ds_destroy(&s);
636 }
637
638 void
639 table_usage(void)
640 {
641 printf("\nOutput formatting options:\n"
642 " -f, --format=FORMAT set output formatting to FORMAT\n"
643 " (\"table\", \"html\", \"csv\", "
644 "or \"json\")\n"
645 " -d, --data=FORMAT set table cell output formatting to\n"
646 " FORMAT (\"string\", \"bare\", "
647 "or \"json\")\n"
648 " --no-headings omit table heading row\n"
649 " --pretty pretty-print JSON in output\n"
650 " --bare equivalent to "
651 "\"--format=list --data=bare --no-headings\"\n");
652 }