2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "dynamic-string.h"
23 #include "ovsdb-data.h"
24 #include "ovsdb-error.h"
33 cell_to_text(struct cell *cell, const struct table_style *style)
37 if (style->cell_format == CF_JSON || !cell->type) {
38 cell->text = json_to_string(cell->json, JSSF_SORT);
40 struct ovsdb_datum datum;
41 struct ovsdb_error *error;
44 error = ovsdb_datum_from_json(&datum, cell->type, cell->json,
48 if (style->cell_format == CF_STRING) {
49 ovsdb_datum_to_string(&datum, cell->type, &s);
51 ovsdb_datum_to_bare(&datum, cell->type, &s);
53 ovsdb_datum_destroy(&datum, cell->type);
54 cell->text = ds_steal_cstr(&s);
56 cell->text = json_to_string(cell->json, JSSF_SORT);
57 ovsdb_error_destroy(error);
61 cell->text = xstrdup("");
69 cell_destroy(struct cell *cell)
72 json_destroy(cell->json);
75 /* Initializes 'table' as an empty table.
77 * The caller should then:
79 * 1. Call table_add_column() once for each column.
81 * 2a. Call table_add_row().
82 * 2b. For each column in the cell, call table_add_cell() and fill in
84 * 3. Call table_print() to print the final table.
85 * 4. Free the table with table_destroy().
88 table_init(struct table *table)
90 memset(table, 0, sizeof *table);
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.) */
96 table_destroy(struct table *table)
101 for (i = 0; i < table->n_columns; i++) {
102 free(table->columns[i].heading);
104 free(table->columns);
106 for (i = 0; i < table->n_columns * table->n_rows; i++) {
107 cell_destroy(&table->cells[i]);
111 free(table->caption);
115 /* Sets 'caption' as the caption for 'table'.
117 * 'table' takes ownership of 'caption'. */
119 table_set_caption(struct table *table, char *caption)
121 free(table->caption);
122 table->caption = caption;
125 /* Turns printing a timestamp along with 'table' on or off, according to
128 table_set_timestamp(struct table *table, bool timestamp)
130 table->timestamp = timestamp;
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()
137 * Columns must be added before any data is put into 'table'. */
139 table_add_column(struct table *table, const char *heading, ...)
141 struct column *column;
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);
149 column = &table->columns[table->n_columns++];
151 va_start(args, heading);
152 column->heading = xvasprintf(heading, args);
157 table_cell__(const struct table *table, size_t row, size_t column)
159 return &table->cells[column + row * table->n_columns];
162 /* Adds a new row to 'table'. The table's columns must already have been added
163 * with table_add_column().
165 * The row is initially empty; use table_add_cell() to start filling it in. */
167 table_add_row(struct table *table)
171 if (table->n_rows >= table->allocated_rows) {
172 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
173 table->n_columns * sizeof *table->cells);
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);
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().
188 * The caller is responsible for filling in the returned cell, in one of two
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.
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.
201 table_add_cell(struct table *table)
205 ovs_assert(table->n_rows > 0);
206 ovs_assert(table->current_column < table->n_columns);
208 x = table->current_column++;
209 y = table->n_rows - 1;
211 return table_cell__(table, y, x);
215 table_print_table_line__(struct ds *line)
222 table_format_timestamp__(void)
224 return xastrftime("%Y-%m-%d %H:%M:%S", time_wall(), true);
228 table_print_timestamp__(const struct table *table)
230 if (table->timestamp) {
231 char *s = table_format_timestamp__();
238 table_print_table__(const struct table *table, const struct table_style *style)
241 struct ds line = DS_EMPTY_INITIALIZER;
249 table_print_timestamp__(table);
251 if (table->caption) {
252 puts(table->caption);
255 widths = xmalloc(table->n_columns * sizeof *widths);
256 for (x = 0; x < table->n_columns; x++) {
257 const struct column *column = &table->columns[x];
259 widths[x] = strlen(column->heading);
260 for (y = 0; y < table->n_rows; y++) {
261 const char *text = cell_to_text(table_cell__(table, y, x), style);
262 size_t length = strlen(text);
264 if (length > widths[x]) {
270 if (style->headings) {
271 for (x = 0; x < table->n_columns; x++) {
272 const struct column *column = &table->columns[x];
274 ds_put_char(&line, ' ');
276 ds_put_format(&line, "%-*s", widths[x], column->heading);
278 table_print_table_line__(&line);
280 for (x = 0; x < table->n_columns; x++) {
282 ds_put_char(&line, ' ');
284 ds_put_char_multiple(&line, '-', widths[x]);
286 table_print_table_line__(&line);
289 for (y = 0; y < table->n_rows; y++) {
290 for (x = 0; x < table->n_columns; x++) {
291 const char *text = cell_to_text(table_cell__(table, y, x), style);
293 ds_put_char(&line, ' ');
295 ds_put_format(&line, "%-*s", widths[x], text);
297 table_print_table_line__(&line);
305 table_print_list__(const struct table *table, const struct table_style *style)
314 table_print_timestamp__(table);
316 if (table->caption) {
317 puts(table->caption);
320 for (y = 0; y < table->n_rows; y++) {
324 for (x = 0; x < table->n_columns; x++) {
325 const char *text = cell_to_text(table_cell__(table, y, x), style);
326 if (style->headings) {
327 printf("%-20s: ", table->columns[x].heading);
335 table_escape_html_text__(const char *s, size_t n)
339 for (i = 0; i < n; i++) {
344 fputs("&", stdout);
347 fputs("<", stdout);
350 fputs(">", stdout);
353 fputs(""", stdout);
363 table_print_html_cell__(const char *element, const char *content)
367 printf(" <%s>", element);
368 for (p = content; *p; ) {
371 if (uuid_from_string_prefix(&uuid, p)) {
372 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
375 table_escape_html_text__(p, 1);
379 printf("</%s>\n", element);
383 table_print_html__(const struct table *table, const struct table_style *style)
387 table_print_timestamp__(table);
389 fputs("<table border=1>\n", stdout);
391 if (table->caption) {
392 table_print_html_cell__("caption", table->caption);
395 if (style->headings) {
396 fputs(" <tr>\n", stdout);
397 for (x = 0; x < table->n_columns; x++) {
398 const struct column *column = &table->columns[x];
399 table_print_html_cell__("th", column->heading);
401 fputs(" </tr>\n", stdout);
404 for (y = 0; y < table->n_rows; y++) {
405 fputs(" <tr>\n", stdout);
406 for (x = 0; x < table->n_columns; x++) {
409 content = cell_to_text(table_cell__(table, y, x), style);
410 if (!strcmp(table->columns[x].heading, "_uuid")) {
411 fputs(" <td><a name=\"", stdout);
412 table_escape_html_text__(content, strlen(content));
413 fputs("\">", stdout);
414 table_escape_html_text__(content, 8);
415 fputs("</a></td>\n", stdout);
417 table_print_html_cell__("td", content);
420 fputs(" </tr>\n", stdout);
423 fputs("</table>\n", stdout);
427 table_print_csv_cell__(const char *content)
431 if (!strpbrk(content, "\n\",")) {
432 fputs(content, stdout);
435 for (p = content; *p != '\0'; p++) {
438 fputs("\"\"", stdout);
450 table_print_csv__(const struct table *table, const struct table_style *style)
459 table_print_timestamp__(table);
461 if (table->caption) {
462 puts(table->caption);
465 if (style->headings) {
466 for (x = 0; x < table->n_columns; x++) {
467 const struct column *column = &table->columns[x];
471 table_print_csv_cell__(column->heading);
476 for (y = 0; y < table->n_rows; y++) {
477 for (x = 0; x < table->n_columns; x++) {
481 table_print_csv_cell__(cell_to_text(table_cell__(table, y, x),
489 table_print_json__(const struct table *table, const struct table_style *style)
491 struct json *json, *headings, *data;
495 json = json_object_create();
496 if (table->caption) {
497 json_object_put_string(json, "caption", table->caption);
499 if (table->timestamp) {
500 char *s = table_format_timestamp__();
501 json_object_put_string(json, "time", s);
505 headings = json_array_create_empty();
506 for (x = 0; x < table->n_columns; x++) {
507 const struct column *column = &table->columns[x];
508 json_array_add(headings, json_string_create(column->heading));
510 json_object_put(json, "headings", headings);
512 data = json_array_create_empty();
513 for (y = 0; y < table->n_rows; y++) {
514 struct json *row = json_array_create_empty();
515 for (x = 0; x < table->n_columns; x++) {
516 const struct cell *cell = table_cell__(table, y, x);
518 json_array_add(row, json_string_create(cell->text));
519 } else if (cell->json) {
520 json_array_add(row, json_clone(cell->json));
522 json_array_add(row, json_null_create());
525 json_array_add(data, row);
527 json_object_put(json, "data", data);
529 s = json_to_string(json, style->json_flags);
535 /* Parses 'format' as the argument to a --format command line option, updating
536 * 'style->format'. */
538 table_parse_format(struct table_style *style, const char *format)
540 if (!strcmp(format, "table")) {
541 style->format = TF_TABLE;
542 } else if (!strcmp(format, "list")) {
543 style->format = TF_LIST;
544 } else if (!strcmp(format, "html")) {
545 style->format = TF_HTML;
546 } else if (!strcmp(format, "csv")) {
547 style->format = TF_CSV;
548 } else if (!strcmp(format, "json")) {
549 style->format = TF_JSON;
551 ovs_fatal(0, "unknown output format \"%s\"", format);
555 /* Parses 'format' as the argument to a --data command line option, updating
556 * 'style->cell_format'. */
558 table_parse_cell_format(struct table_style *style, const char *format)
560 if (!strcmp(format, "string")) {
561 style->cell_format = CF_STRING;
562 } else if (!strcmp(format, "bare")) {
563 style->cell_format = CF_BARE;
564 } else if (!strcmp(format, "json")) {
565 style->cell_format = CF_JSON;
567 ovs_fatal(0, "unknown data format \"%s\"", format);
571 /* Outputs 'table' on stdout in the specified 'style'. */
573 table_print(const struct table *table, const struct table_style *style)
575 switch (style->format) {
577 table_print_table__(table, style);
581 table_print_list__(table, style);
585 table_print_html__(table, style);
589 table_print_csv__(table, style);
593 table_print_json__(table, style);