2 * Copyright (c) 2009, 2010, 2011, 2012 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.
23 #include "dynamic-string.h"
25 #include "ovsdb-data.h"
26 #include "ovsdb-error.h"
35 cell_to_text(struct cell *cell, const struct table_style *style)
39 if (style->cell_format == CF_JSON || !cell->type) {
40 cell->text = json_to_string(cell->json, JSSF_SORT);
42 struct ovsdb_datum datum;
43 struct ovsdb_error *error;
46 error = ovsdb_datum_from_json(&datum, cell->type, cell->json,
50 if (style->cell_format == CF_STRING) {
51 ovsdb_datum_to_string(&datum, cell->type, &s);
53 ovsdb_datum_to_bare(&datum, cell->type, &s);
55 ovsdb_datum_destroy(&datum, cell->type);
56 cell->text = ds_steal_cstr(&s);
58 cell->text = json_to_string(cell->json, JSSF_SORT);
59 ovsdb_error_destroy(error);
63 cell->text = xstrdup("");
71 cell_destroy(struct cell *cell)
74 json_destroy(cell->json);
77 /* Initializes 'table' as an empty table.
79 * The caller should then:
81 * 1. Call table_add_column() once for each column.
83 * 2a. Call table_add_row().
84 * 2b. For each column in the cell, call table_add_cell() and fill in
86 * 3. Call table_print() to print the final table.
87 * 4. Free the table with table_destroy().
90 table_init(struct table *table)
92 memset(table, 0, sizeof *table);
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.) */
98 table_destroy(struct table *table)
103 for (i = 0; i < table->n_columns; i++) {
104 free(table->columns[i].heading);
106 free(table->columns);
108 for (i = 0; i < table->n_columns * table->n_rows; i++) {
109 cell_destroy(&table->cells[i]);
113 free(table->caption);
117 /* Sets 'caption' as the caption for 'table'.
119 * 'table' takes ownership of 'caption'. */
121 table_set_caption(struct table *table, char *caption)
123 free(table->caption);
124 table->caption = caption;
127 /* Turns printing a timestamp along with 'table' on or off, according to
130 table_set_timestamp(struct table *table, bool timestamp)
132 table->timestamp = timestamp;
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()
139 * Columns must be added before any data is put into 'table'. */
141 table_add_column(struct table *table, const char *heading, ...)
143 struct column *column;
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);
151 column = &table->columns[table->n_columns++];
153 va_start(args, heading);
154 column->heading = xvasprintf(heading, args);
159 table_cell__(const struct table *table, size_t row, size_t column)
161 return &table->cells[column + row * table->n_columns];
164 /* Adds a new row to 'table'. The table's columns must already have been added
165 * with table_add_column().
167 * The row is initially empty; use table_add_cell() to start filling it in. */
169 table_add_row(struct table *table)
173 if (table->n_rows >= table->allocated_rows) {
174 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
175 table->n_columns * sizeof *table->cells);
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);
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().
190 * The caller is responsible for filling in the returned cell, in one of two
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.
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.
203 table_add_cell(struct table *table)
207 assert(table->n_rows > 0);
208 assert(table->current_column < table->n_columns);
210 x = table->current_column++;
211 y = table->n_rows - 1;
213 return table_cell__(table, y, x);
217 table_print_table_line__(struct ds *line)
224 table_format_timestamp__(char *s, size_t size)
226 time_t now = time_wall();
227 strftime(s, size, "%Y-%m-%d %H:%M:%S", gmtime(&now));
231 table_print_timestamp__(const struct table *table)
233 if (table->timestamp) {
236 table_format_timestamp__(s, sizeof s);
242 table_print_table__(const struct table *table, const struct table_style *style)
245 struct ds line = DS_EMPTY_INITIALIZER;
253 table_print_timestamp__(table);
255 if (table->caption) {
256 puts(table->caption);
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];
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);
268 if (length > widths[x]) {
274 if (style->headings) {
275 for (x = 0; x < table->n_columns; x++) {
276 const struct column *column = &table->columns[x];
278 ds_put_char(&line, ' ');
280 ds_put_format(&line, "%-*s", widths[x], column->heading);
282 table_print_table_line__(&line);
284 for (x = 0; x < table->n_columns; x++) {
286 ds_put_char(&line, ' ');
288 ds_put_char_multiple(&line, '-', widths[x]);
290 table_print_table_line__(&line);
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);
297 ds_put_char(&line, ' ');
299 ds_put_format(&line, "%-*s", widths[x], text);
301 table_print_table_line__(&line);
309 table_print_list__(const struct table *table, const struct table_style *style)
318 table_print_timestamp__(table);
320 if (table->caption) {
321 puts(table->caption);
324 for (y = 0; y < table->n_rows; y++) {
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);
339 table_escape_html_text__(const char *s, size_t n)
343 for (i = 0; i < n; i++) {
348 fputs("&", stdout);
351 fputs("<", stdout);
354 fputs(">", stdout);
357 fputs(""", stdout);
367 table_print_html_cell__(const char *element, const char *content)
371 printf(" <%s>", element);
372 for (p = content; *p; ) {
375 if (uuid_from_string_prefix(&uuid, p)) {
376 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
379 table_escape_html_text__(p, 1);
383 printf("</%s>\n", element);
387 table_print_html__(const struct table *table, const struct table_style *style)
391 table_print_timestamp__(table);
393 fputs("<table border=1>\n", stdout);
395 if (table->caption) {
396 table_print_html_cell__("caption", table->caption);
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);
405 fputs(" </tr>\n", stdout);
408 for (y = 0; y < table->n_rows; y++) {
409 fputs(" <tr>\n", stdout);
410 for (x = 0; x < table->n_columns; x++) {
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);
421 table_print_html_cell__("td", content);
424 fputs(" </tr>\n", stdout);
427 fputs("</table>\n", stdout);
431 table_print_csv_cell__(const char *content)
435 if (!strpbrk(content, "\n\",")) {
436 fputs(content, stdout);
439 for (p = content; *p != '\0'; p++) {
442 fputs("\"\"", stdout);
454 table_print_csv__(const struct table *table, const struct table_style *style)
463 table_print_timestamp__(table);
465 if (table->caption) {
466 puts(table->caption);
469 if (style->headings) {
470 for (x = 0; x < table->n_columns; x++) {
471 const struct column *column = &table->columns[x];
475 table_print_csv_cell__(column->heading);
480 for (y = 0; y < table->n_rows; y++) {
481 for (x = 0; x < table->n_columns; x++) {
485 table_print_csv_cell__(cell_to_text(table_cell__(table, y, x),
493 table_print_json__(const struct table *table, const struct table_style *style)
495 struct json *json, *headings, *data;
499 json = json_object_create();
500 if (table->caption) {
501 json_object_put_string(json, "caption", table->caption);
503 if (table->timestamp) {
506 table_format_timestamp__(s, sizeof s);
507 json_object_put_string(json, "time", s);
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));
515 json_object_put(json, "headings", headings);
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);
523 json_array_add(row, json_string_create(cell->text));
524 } else if (cell->json) {
525 json_array_add(row, json_clone(cell->json));
527 json_array_add(row, json_null_create());
530 json_array_add(data, row);
532 json_object_put(json, "data", data);
534 s = json_to_string(json, style->json_flags);
540 /* Parses 'format' as the argument to a --format command line option, updating
541 * 'style->format'. */
543 table_parse_format(struct table_style *style, const char *format)
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;
556 ovs_fatal(0, "unknown output format \"%s\"", format);
560 /* Parses 'format' as the argument to a --data command line option, updating
561 * 'style->cell_format'. */
563 table_parse_cell_format(struct table_style *style, const char *format)
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;
572 ovs_fatal(0, "unknown data format \"%s\"", format);
576 /* Outputs 'table' on stdout in the specified 'style'. */
578 table_print(const struct table *table, const struct table_style *style)
580 switch (style->format) {
582 table_print_table__(table, style);
586 table_print_list__(table, style);
590 table_print_html__(table, style);
594 table_print_csv__(table, style);
598 table_print_json__(table, style);