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"
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);
60 cell->text = xstrdup("");
68 cell_destroy(struct cell *cell)
71 json_destroy(cell->json);
74 /* Initializes 'table' as an empty table.
76 * The caller should then:
78 * 1. Call table_add_column() once for each column.
80 * 2a. Call table_add_row().
81 * 2b. For each column in the cell, call table_add_cell() and fill in
83 * 3. Call table_print() to print the final table.
84 * 4. Free the table with table_destroy().
87 table_init(struct table *table)
89 memset(table, 0, sizeof *table);
92 /* Destroys 'table' and frees all associated storage. (However, the client
93 * owns the 'type' members pointed to by cells, so these are not destroyed.) */
95 table_destroy(struct table *table)
100 for (i = 0; i < table->n_columns; i++) {
101 free(table->columns[i].heading);
103 free(table->columns);
105 for (i = 0; i < table->n_columns * table->n_rows; i++) {
106 cell_destroy(&table->cells[i]);
110 free(table->caption);
114 /* Sets 'caption' as the caption for 'table'.
116 * 'table' takes ownership of 'caption'. */
118 table_set_caption(struct table *table, char *caption)
120 free(table->caption);
121 table->caption = caption;
124 /* Adds a new column to 'table' just to the right of any existing column, with
125 * 'heading' as a title for the column. 'heading' must be a valid printf()
128 * Columns must be added before any data is put into 'table'. */
130 table_add_column(struct table *table, const char *heading, ...)
132 struct column *column;
135 assert(!table->n_rows);
136 if (table->n_columns >= table->allocated_columns) {
137 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
138 sizeof *table->columns);
140 column = &table->columns[table->n_columns++];
142 va_start(args, heading);
143 column->heading = xvasprintf(heading, args);
148 table_cell__(const struct table *table, size_t row, size_t column)
150 return &table->cells[column + row * table->n_columns];
153 /* Adds a new row to 'table'. The table's columns must already have been added
154 * with table_add_column().
156 * The row is initially empty; use table_add_cell() to start filling it in. */
158 table_add_row(struct table *table)
162 if (table->n_rows >= table->allocated_rows) {
163 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
164 table->n_columns * sizeof *table->cells);
168 table->current_column = 0;
169 for (x = 0; x < table->n_columns; x++) {
170 struct cell *cell = table_cell__(table, y, x);
171 memset(cell, 0, sizeof *cell);
175 /* Adds a new cell in the current row of 'table', which must have been added
176 * with table_add_row(). Cells are filled in the same order that the columns
177 * were added with table_add_column().
179 * The caller is responsible for filling in the returned cell, in one of two
182 * - If the cell should contain an ovsdb_datum, formatted according to the
183 * table style, then fill in the 'json' member with the JSON representation
184 * of the datum and 'type' with its type.
186 * - If the cell should contain a fixed text string, then the caller should
187 * assign that string to the 'text' member. This is undesirable if the
188 * cell actually contains OVSDB data because 'text' cannot be formatted
189 * according to the table style; it is always output verbatim.
192 table_add_cell(struct table *table)
196 assert(table->n_rows > 0);
197 assert(table->current_column < table->n_columns);
199 x = table->current_column++;
200 y = table->n_rows - 1;
202 return table_cell__(table, y, x);
206 table_print_table_line__(struct ds *line)
213 table_print_table__(const struct table *table, const struct table_style *style)
216 struct ds line = DS_EMPTY_INITIALIZER;
224 if (table->caption) {
225 puts(table->caption);
228 widths = xmalloc(table->n_columns * sizeof *widths);
229 for (x = 0; x < table->n_columns; x++) {
230 const struct column *column = &table->columns[x];
232 widths[x] = strlen(column->heading);
233 for (y = 0; y < table->n_rows; y++) {
234 const char *text = cell_to_text(table_cell__(table, y, x), style);
235 size_t length = strlen(text);
237 if (length > widths[x]) {
243 if (style->headings) {
244 for (x = 0; x < table->n_columns; x++) {
245 const struct column *column = &table->columns[x];
247 ds_put_char(&line, ' ');
249 ds_put_format(&line, "%-*s", widths[x], column->heading);
251 table_print_table_line__(&line);
253 for (x = 0; x < table->n_columns; x++) {
255 ds_put_char(&line, ' ');
257 ds_put_char_multiple(&line, '-', widths[x]);
259 table_print_table_line__(&line);
262 for (y = 0; y < table->n_rows; y++) {
263 for (x = 0; x < table->n_columns; x++) {
264 const char *text = cell_to_text(table_cell__(table, y, x), style);
266 ds_put_char(&line, ' ');
268 ds_put_format(&line, "%-*s", widths[x], text);
270 table_print_table_line__(&line);
278 table_print_list__(const struct table *table, const struct table_style *style)
287 if (table->caption) {
288 puts(table->caption);
291 for (y = 0; y < table->n_rows; y++) {
295 for (x = 0; x < table->n_columns; x++) {
296 const char *text = cell_to_text(table_cell__(table, y, x), style);
297 if (style->headings) {
298 printf("%-20s: ", table->columns[x].heading);
306 table_escape_html_text__(const char *s, size_t n)
310 for (i = 0; i < n; i++) {
315 fputs("&", stdout);
318 fputs("<", stdout);
321 fputs(">", stdout);
324 fputs(""", stdout);
334 table_print_html_cell__(const char *element, const char *content)
338 printf(" <%s>", element);
339 for (p = content; *p; ) {
342 if (uuid_from_string_prefix(&uuid, p)) {
343 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
346 table_escape_html_text__(p, 1);
350 printf("</%s>\n", element);
354 table_print_html__(const struct table *table, const struct table_style *style)
358 fputs("<table border=1>\n", stdout);
360 if (table->caption) {
361 table_print_html_cell__("caption", table->caption);
364 if (style->headings) {
365 fputs(" <tr>\n", stdout);
366 for (x = 0; x < table->n_columns; x++) {
367 const struct column *column = &table->columns[x];
368 table_print_html_cell__("th", column->heading);
370 fputs(" </tr>\n", stdout);
373 for (y = 0; y < table->n_rows; y++) {
374 fputs(" <tr>\n", stdout);
375 for (x = 0; x < table->n_columns; x++) {
378 content = cell_to_text(table_cell__(table, y, x), style);
379 if (!strcmp(table->columns[x].heading, "_uuid")) {
380 fputs(" <td><a name=\"", stdout);
381 table_escape_html_text__(content, strlen(content));
382 fputs("\">", stdout);
383 table_escape_html_text__(content, 8);
384 fputs("</a></td>\n", stdout);
386 table_print_html_cell__("td", content);
389 fputs(" </tr>\n", stdout);
392 fputs("</table>\n", stdout);
396 table_print_csv_cell__(const char *content)
400 if (!strpbrk(content, "\n\",")) {
401 fputs(content, stdout);
404 for (p = content; *p != '\0'; p++) {
407 fputs("\"\"", stdout);
419 table_print_csv__(const struct table *table, const struct table_style *style)
428 if (table->caption) {
429 puts(table->caption);
432 if (style->headings) {
433 for (x = 0; x < table->n_columns; x++) {
434 const struct column *column = &table->columns[x];
438 table_print_csv_cell__(column->heading);
443 for (y = 0; y < table->n_rows; y++) {
444 for (x = 0; x < table->n_columns; x++) {
448 table_print_csv_cell__(cell_to_text(table_cell__(table, y, x),
456 table_print_json__(const struct table *table, const struct table_style *style)
458 struct json *json, *headings, *data;
462 json = json_object_create();
463 if (table->caption) {
464 json_object_put_string(json, "caption", table->caption);
467 headings = json_array_create_empty();
468 for (x = 0; x < table->n_columns; x++) {
469 const struct column *column = &table->columns[x];
470 json_array_add(headings, json_string_create(column->heading));
472 json_object_put(json, "headings", headings);
474 data = json_array_create_empty();
475 for (y = 0; y < table->n_rows; y++) {
476 struct json *row = json_array_create_empty();
477 for (x = 0; x < table->n_columns; x++) {
478 const struct cell *cell = table_cell__(table, y, x);
480 json_array_add(row, json_string_create(cell->text));
482 json_array_add(row, json_clone(cell->json));
485 json_array_add(data, row);
487 json_object_put(json, "data", data);
489 s = json_to_string(json, style->json_flags);
495 /* Parses 'format' as the argument to a --format command line option, updating
496 * 'style->format'. */
498 table_parse_format(struct table_style *style, const char *format)
500 if (!strcmp(format, "table")) {
501 style->format = TF_TABLE;
502 } else if (!strcmp(format, "list")) {
503 style->format = TF_LIST;
504 } else if (!strcmp(format, "html")) {
505 style->format = TF_HTML;
506 } else if (!strcmp(format, "csv")) {
507 style->format = TF_CSV;
508 } else if (!strcmp(format, "json")) {
509 style->format = TF_JSON;
511 ovs_fatal(0, "unknown output format \"%s\"", format);
515 /* Parses 'format' as the argument to a --data command line option, updating
516 * 'style->cell_format'. */
518 table_parse_cell_format(struct table_style *style, const char *format)
520 if (!strcmp(format, "string")) {
521 style->cell_format = CF_STRING;
522 } else if (!strcmp(format, "bare")) {
523 style->cell_format = CF_BARE;
524 } else if (!strcmp(format, "json")) {
525 style->cell_format = CF_JSON;
527 ovs_fatal(0, "unknown data format \"%s\"", format);
531 /* Outputs 'table' on stdout in the specified 'style'. */
533 table_print(const struct table *table, const struct table_style *style)
535 switch (style->format) {
537 table_print_table__(table, style);
541 table_print_list__(table, style);
545 table_print_html__(table, style);
549 table_print_csv__(table, style);
553 table_print_json__(table, style);