2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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"
34 cell_to_text(struct cell *cell, const struct table_style *style)
38 if (style->cell_format == CF_JSON || !cell->type) {
39 cell->text = json_to_string(cell->json, JSSF_SORT);
41 struct ovsdb_datum datum;
42 struct ovsdb_error *error;
45 error = ovsdb_datum_from_json(&datum, cell->type, cell->json,
49 if (style->cell_format == CF_STRING) {
50 ovsdb_datum_to_string(&datum, cell->type, &s);
52 ovsdb_datum_to_bare(&datum, cell->type, &s);
54 ovsdb_datum_destroy(&datum, cell->type);
55 cell->text = ds_steal_cstr(&s);
57 cell->text = json_to_string(cell->json, JSSF_SORT);
58 ovsdb_error_destroy(error);
62 cell->text = xstrdup("");
70 cell_destroy(struct cell *cell)
73 json_destroy(cell->json);
76 /* Initializes 'table' as an empty table.
78 * The caller should then:
80 * 1. Call table_add_column() once for each column.
82 * 2a. Call table_add_row().
83 * 2b. For each column in the cell, call table_add_cell() and fill in
85 * 3. Call table_print() to print the final table.
86 * 4. Free the table with table_destroy().
89 table_init(struct table *table)
91 memset(table, 0, sizeof *table);
94 /* Destroys 'table' and frees all associated storage. (However, the client
95 * owns the 'type' members pointed to by cells, so these are not destroyed.) */
97 table_destroy(struct table *table)
102 for (i = 0; i < table->n_columns; i++) {
103 free(table->columns[i].heading);
105 free(table->columns);
107 for (i = 0; i < table->n_columns * table->n_rows; i++) {
108 cell_destroy(&table->cells[i]);
112 free(table->caption);
116 /* Sets 'caption' as the caption for 'table'.
118 * 'table' takes ownership of 'caption'. */
120 table_set_caption(struct table *table, char *caption)
122 free(table->caption);
123 table->caption = caption;
126 /* Adds a new column to 'table' just to the right of any existing column, with
127 * 'heading' as a title for the column. 'heading' must be a valid printf()
130 * Columns must be added before any data is put into 'table'. */
132 table_add_column(struct table *table, const char *heading, ...)
134 struct column *column;
137 assert(!table->n_rows);
138 if (table->n_columns >= table->allocated_columns) {
139 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
140 sizeof *table->columns);
142 column = &table->columns[table->n_columns++];
144 va_start(args, heading);
145 column->heading = xvasprintf(heading, args);
150 table_cell__(const struct table *table, size_t row, size_t column)
152 return &table->cells[column + row * table->n_columns];
155 /* Adds a new row to 'table'. The table's columns must already have been added
156 * with table_add_column().
158 * The row is initially empty; use table_add_cell() to start filling it in. */
160 table_add_row(struct table *table)
164 if (table->n_rows >= table->allocated_rows) {
165 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
166 table->n_columns * sizeof *table->cells);
170 table->current_column = 0;
171 for (x = 0; x < table->n_columns; x++) {
172 struct cell *cell = table_cell__(table, y, x);
173 memset(cell, 0, sizeof *cell);
177 /* Adds a new cell in the current row of 'table', which must have been added
178 * with table_add_row(). Cells are filled in the same order that the columns
179 * were added with table_add_column().
181 * The caller is responsible for filling in the returned cell, in one of two
184 * - If the cell should contain an ovsdb_datum, formatted according to the
185 * table style, then fill in the 'json' member with the JSON representation
186 * of the datum and 'type' with its type.
188 * - If the cell should contain a fixed text string, then the caller should
189 * assign that string to the 'text' member. This is undesirable if the
190 * cell actually contains OVSDB data because 'text' cannot be formatted
191 * according to the table style; it is always output verbatim.
194 table_add_cell(struct table *table)
198 assert(table->n_rows > 0);
199 assert(table->current_column < table->n_columns);
201 x = table->current_column++;
202 y = table->n_rows - 1;
204 return table_cell__(table, y, x);
208 table_print_table_line__(struct ds *line)
215 table_print_table__(const struct table *table, const struct table_style *style)
218 struct ds line = DS_EMPTY_INITIALIZER;
226 if (table->caption) {
227 puts(table->caption);
230 widths = xmalloc(table->n_columns * sizeof *widths);
231 for (x = 0; x < table->n_columns; x++) {
232 const struct column *column = &table->columns[x];
234 widths[x] = strlen(column->heading);
235 for (y = 0; y < table->n_rows; y++) {
236 const char *text = cell_to_text(table_cell__(table, y, x), style);
237 size_t length = strlen(text);
239 if (length > widths[x]) {
245 if (style->headings) {
246 for (x = 0; x < table->n_columns; x++) {
247 const struct column *column = &table->columns[x];
249 ds_put_char(&line, ' ');
251 ds_put_format(&line, "%-*s", widths[x], column->heading);
253 table_print_table_line__(&line);
255 for (x = 0; x < table->n_columns; x++) {
257 ds_put_char(&line, ' ');
259 ds_put_char_multiple(&line, '-', widths[x]);
261 table_print_table_line__(&line);
264 for (y = 0; y < table->n_rows; y++) {
265 for (x = 0; x < table->n_columns; x++) {
266 const char *text = cell_to_text(table_cell__(table, y, x), style);
268 ds_put_char(&line, ' ');
270 ds_put_format(&line, "%-*s", widths[x], text);
272 table_print_table_line__(&line);
280 table_print_list__(const struct table *table, const struct table_style *style)
289 if (table->caption) {
290 puts(table->caption);
293 for (y = 0; y < table->n_rows; y++) {
297 for (x = 0; x < table->n_columns; x++) {
298 const char *text = cell_to_text(table_cell__(table, y, x), style);
299 if (style->headings) {
300 printf("%-20s: ", table->columns[x].heading);
308 table_escape_html_text__(const char *s, size_t n)
312 for (i = 0; i < n; i++) {
317 fputs("&", stdout);
320 fputs("<", stdout);
323 fputs(">", stdout);
326 fputs(""", stdout);
336 table_print_html_cell__(const char *element, const char *content)
340 printf(" <%s>", element);
341 for (p = content; *p; ) {
344 if (uuid_from_string_prefix(&uuid, p)) {
345 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
348 table_escape_html_text__(p, 1);
352 printf("</%s>\n", element);
356 table_print_html__(const struct table *table, const struct table_style *style)
360 fputs("<table border=1>\n", stdout);
362 if (table->caption) {
363 table_print_html_cell__("caption", table->caption);
366 if (style->headings) {
367 fputs(" <tr>\n", stdout);
368 for (x = 0; x < table->n_columns; x++) {
369 const struct column *column = &table->columns[x];
370 table_print_html_cell__("th", column->heading);
372 fputs(" </tr>\n", stdout);
375 for (y = 0; y < table->n_rows; y++) {
376 fputs(" <tr>\n", stdout);
377 for (x = 0; x < table->n_columns; x++) {
380 content = cell_to_text(table_cell__(table, y, x), style);
381 if (!strcmp(table->columns[x].heading, "_uuid")) {
382 fputs(" <td><a name=\"", stdout);
383 table_escape_html_text__(content, strlen(content));
384 fputs("\">", stdout);
385 table_escape_html_text__(content, 8);
386 fputs("</a></td>\n", stdout);
388 table_print_html_cell__("td", content);
391 fputs(" </tr>\n", stdout);
394 fputs("</table>\n", stdout);
398 table_print_csv_cell__(const char *content)
402 if (!strpbrk(content, "\n\",")) {
403 fputs(content, stdout);
406 for (p = content; *p != '\0'; p++) {
409 fputs("\"\"", stdout);
421 table_print_csv__(const struct table *table, const struct table_style *style)
430 if (table->caption) {
431 puts(table->caption);
434 if (style->headings) {
435 for (x = 0; x < table->n_columns; x++) {
436 const struct column *column = &table->columns[x];
440 table_print_csv_cell__(column->heading);
445 for (y = 0; y < table->n_rows; y++) {
446 for (x = 0; x < table->n_columns; x++) {
450 table_print_csv_cell__(cell_to_text(table_cell__(table, y, x),
458 table_print_json__(const struct table *table, const struct table_style *style)
460 struct json *json, *headings, *data;
464 json = json_object_create();
465 if (table->caption) {
466 json_object_put_string(json, "caption", table->caption);
469 headings = json_array_create_empty();
470 for (x = 0; x < table->n_columns; x++) {
471 const struct column *column = &table->columns[x];
472 json_array_add(headings, json_string_create(column->heading));
474 json_object_put(json, "headings", headings);
476 data = json_array_create_empty();
477 for (y = 0; y < table->n_rows; y++) {
478 struct json *row = json_array_create_empty();
479 for (x = 0; x < table->n_columns; x++) {
480 const struct cell *cell = table_cell__(table, y, x);
482 json_array_add(row, json_string_create(cell->text));
484 json_array_add(row, json_clone(cell->json));
487 json_array_add(data, row);
489 json_object_put(json, "data", data);
491 s = json_to_string(json, style->json_flags);
497 /* Parses 'format' as the argument to a --format command line option, updating
498 * 'style->format'. */
500 table_parse_format(struct table_style *style, const char *format)
502 if (!strcmp(format, "table")) {
503 style->format = TF_TABLE;
504 } else if (!strcmp(format, "list")) {
505 style->format = TF_LIST;
506 } else if (!strcmp(format, "html")) {
507 style->format = TF_HTML;
508 } else if (!strcmp(format, "csv")) {
509 style->format = TF_CSV;
510 } else if (!strcmp(format, "json")) {
511 style->format = TF_JSON;
513 ovs_fatal(0, "unknown output format \"%s\"", format);
517 /* Parses 'format' as the argument to a --data command line option, updating
518 * 'style->cell_format'. */
520 table_parse_cell_format(struct table_style *style, const char *format)
522 if (!strcmp(format, "string")) {
523 style->cell_format = CF_STRING;
524 } else if (!strcmp(format, "bare")) {
525 style->cell_format = CF_BARE;
526 } else if (!strcmp(format, "json")) {
527 style->cell_format = CF_JSON;
529 ovs_fatal(0, "unknown data format \"%s\"", format);
533 /* Outputs 'table' on stdout in the specified 'style'. */
535 table_print(const struct table *table, const struct table_style *style)
537 switch (style->format) {
539 table_print_table__(table, style);
543 table_print_list__(table, style);
547 table_print_html__(table, style);
551 table_print_csv__(table, style);
555 table_print_json__(table, style);