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__(char *s, size_t size)
224 time_t now = time_wall();
226 strftime(s, size, "%Y-%m-%d %H:%M:%S", gmtime_r(&now, &tm));
230 table_print_timestamp__(const struct table *table)
232 if (table->timestamp) {
235 table_format_timestamp__(s, sizeof s);
241 table_print_table__(const struct table *table, const struct table_style *style)
244 struct ds line = DS_EMPTY_INITIALIZER;
252 table_print_timestamp__(table);
254 if (table->caption) {
255 puts(table->caption);
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];
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);
267 if (length > widths[x]) {
273 if (style->headings) {
274 for (x = 0; x < table->n_columns; x++) {
275 const struct column *column = &table->columns[x];
277 ds_put_char(&line, ' ');
279 ds_put_format(&line, "%-*s", widths[x], column->heading);
281 table_print_table_line__(&line);
283 for (x = 0; x < table->n_columns; x++) {
285 ds_put_char(&line, ' ');
287 ds_put_char_multiple(&line, '-', widths[x]);
289 table_print_table_line__(&line);
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);
296 ds_put_char(&line, ' ');
298 ds_put_format(&line, "%-*s", widths[x], text);
300 table_print_table_line__(&line);
308 table_print_list__(const struct table *table, const struct table_style *style)
317 table_print_timestamp__(table);
319 if (table->caption) {
320 puts(table->caption);
323 for (y = 0; y < table->n_rows; y++) {
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);
338 table_escape_html_text__(const char *s, size_t n)
342 for (i = 0; i < n; i++) {
347 fputs("&", stdout);
350 fputs("<", stdout);
353 fputs(">", stdout);
356 fputs(""", stdout);
366 table_print_html_cell__(const char *element, const char *content)
370 printf(" <%s>", element);
371 for (p = content; *p; ) {
374 if (uuid_from_string_prefix(&uuid, p)) {
375 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
378 table_escape_html_text__(p, 1);
382 printf("</%s>\n", element);
386 table_print_html__(const struct table *table, const struct table_style *style)
390 table_print_timestamp__(table);
392 fputs("<table border=1>\n", stdout);
394 if (table->caption) {
395 table_print_html_cell__("caption", table->caption);
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);
404 fputs(" </tr>\n", stdout);
407 for (y = 0; y < table->n_rows; y++) {
408 fputs(" <tr>\n", stdout);
409 for (x = 0; x < table->n_columns; x++) {
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);
420 table_print_html_cell__("td", content);
423 fputs(" </tr>\n", stdout);
426 fputs("</table>\n", stdout);
430 table_print_csv_cell__(const char *content)
434 if (!strpbrk(content, "\n\",")) {
435 fputs(content, stdout);
438 for (p = content; *p != '\0'; p++) {
441 fputs("\"\"", stdout);
453 table_print_csv__(const struct table *table, const struct table_style *style)
462 table_print_timestamp__(table);
464 if (table->caption) {
465 puts(table->caption);
468 if (style->headings) {
469 for (x = 0; x < table->n_columns; x++) {
470 const struct column *column = &table->columns[x];
474 table_print_csv_cell__(column->heading);
479 for (y = 0; y < table->n_rows; y++) {
480 for (x = 0; x < table->n_columns; x++) {
484 table_print_csv_cell__(cell_to_text(table_cell__(table, y, x),
492 table_print_json__(const struct table *table, const struct table_style *style)
494 struct json *json, *headings, *data;
498 json = json_object_create();
499 if (table->caption) {
500 json_object_put_string(json, "caption", table->caption);
502 if (table->timestamp) {
505 table_format_timestamp__(s, sizeof s);
506 json_object_put_string(json, "time", s);
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));
514 json_object_put(json, "headings", headings);
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);
522 json_array_add(row, json_string_create(cell->text));
523 } else if (cell->json) {
524 json_array_add(row, json_clone(cell->json));
526 json_array_add(row, json_null_create());
529 json_array_add(data, row);
531 json_object_put(json, "data", data);
533 s = json_to_string(json, style->json_flags);
539 /* Parses 'format' as the argument to a --format command line option, updating
540 * 'style->format'. */
542 table_parse_format(struct table_style *style, const char *format)
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;
555 ovs_fatal(0, "unknown output format \"%s\"", format);
559 /* Parses 'format' as the argument to a --data command line option, updating
560 * 'style->cell_format'. */
562 table_parse_cell_format(struct table_style *style, const char *format)
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;
571 ovs_fatal(0, "unknown data format \"%s\"", format);
575 /* Outputs 'table' on stdout in the specified 'style'. */
577 table_print(const struct table *table, const struct table_style *style)
579 switch (style->format) {
581 table_print_table__(table, style);
585 table_print_list__(table, style);
589 table_print_html__(table, style);
593 table_print_csv__(table, style);
597 table_print_json__(table, style);