]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/table.c
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 "openvswitch/dynamic-string.h"
22 #include "openvswitch/json.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_finish_line(struct ds
*s
)
217 while (ds_last(s
) == ' ') {
220 ds_put_char(s
, '\n');
224 table_format_timestamp__(void)
226 return xastrftime_msec("%Y-%m-%d %H:%M:%S.###", time_wall_msec(), true);
230 table_print_timestamp__(const struct table
*table
, struct ds
*s
)
232 if (table
->timestamp
) {
233 char *timestamp
= table_format_timestamp__();
234 ds_put_format(s
, "%s\n", timestamp
);
239 static bool first_table
= true;
242 table_print_table__(const struct table
*table
, const struct table_style
*style
,
251 ds_put_char(s
, '\n');
254 table_print_timestamp__(table
, s
);
256 if (table
->caption
) {
257 ds_put_format(s
, "%s\n", table
->caption
);
260 widths
= xzalloc(table
->n_columns
* sizeof *widths
);
261 for (x
= 0; x
< table
->n_columns
; x
++) {
262 const struct column
*column
= &table
->columns
[x
];
265 for (y
= 0; y
< table
->n_rows
; y
++) {
266 const char *text
= cell_to_text(table_cell__(table
, y
, x
), style
);
267 size_t length
= strlen(text
);
274 int max
= style
->max_column_width
;
275 if (max
> 0 && w
> max
) {
278 if (style
->headings
) {
279 int min
= strlen(column
->heading
);
287 if (style
->headings
) {
288 for (x
= 0; x
< table
->n_columns
; x
++) {
289 const struct column
*column
= &table
->columns
[x
];
293 ds_put_format(s
, "%-*s", widths
[x
], column
->heading
);
295 table_finish_line(s
);
297 for (x
= 0; x
< table
->n_columns
; x
++) {
301 ds_put_char_multiple(s
, '-', widths
[x
]);
303 table_finish_line(s
);
306 for (y
= 0; y
< table
->n_rows
; y
++) {
307 for (x
= 0; x
< table
->n_columns
; x
++) {
308 const char *text
= cell_to_text(table_cell__(table
, y
, x
), style
);
312 ds_put_format(s
, "%-*.*s", widths
[x
], widths
[x
], text
);
314 table_finish_line(s
);
321 table_print_list__(const struct table
*table
, const struct table_style
*style
,
329 ds_put_char(s
, '\n');
332 table_print_timestamp__(table
, s
);
334 if (table
->caption
) {
335 ds_put_format(s
, "%s\n", table
->caption
);
338 for (y
= 0; y
< table
->n_rows
; y
++) {
340 ds_put_char(s
, '\n');
342 for (x
= 0; x
< table
->n_columns
; x
++) {
343 const char *text
= cell_to_text(table_cell__(table
, y
, x
), style
);
344 if (style
->headings
) {
345 ds_put_format(s
, "%-20s: ", table
->columns
[x
].heading
);
347 ds_put_format(s
, "%s\n", text
);
353 table_escape_html_text__(const char *content
, size_t n
, struct ds
*s
)
355 if (!strpbrk(content
, "&<>\"")) {
356 ds_put_buffer(s
, content
, n
);
360 for (i
= 0; i
< n
; i
++) {
365 ds_put_cstr(s
, "&");
368 ds_put_cstr(s
, "<");
371 ds_put_cstr(s
, ">");
374 ds_put_cstr(s
, """);
385 table_print_html_cell__(const char *element
, const char *content
, struct ds
*s
)
389 ds_put_format(s
, " <%s>", element
);
390 for (p
= content
; *p
; ) {
393 if (uuid_from_string_prefix(&uuid
, p
)) {
394 ds_put_format(s
, "<a href=\"#%.*s\">%.*s</a>", UUID_LEN
, p
, 8, p
);
397 table_escape_html_text__(p
, 1, s
);
401 ds_put_format(s
, "</%s>\n", element
);
405 table_print_html__(const struct table
*table
, const struct table_style
*style
,
410 table_print_timestamp__(table
, s
);
412 ds_put_cstr(s
, "<table border=1>\n");
414 if (table
->caption
) {
415 table_print_html_cell__("caption", table
->caption
, s
);
418 if (style
->headings
) {
419 ds_put_cstr(s
, " <tr>\n");
420 for (x
= 0; x
< table
->n_columns
; x
++) {
421 const struct column
*column
= &table
->columns
[x
];
422 table_print_html_cell__("th", column
->heading
, s
);
424 ds_put_cstr(s
, " </tr>\n");
427 for (y
= 0; y
< table
->n_rows
; y
++) {
428 ds_put_cstr(s
, " <tr>\n");
429 for (x
= 0; x
< table
->n_columns
; x
++) {
432 content
= cell_to_text(table_cell__(table
, y
, x
), style
);
433 if (!strcmp(table
->columns
[x
].heading
, "_uuid")) {
434 ds_put_cstr(s
, " <td><a name=\"");
435 table_escape_html_text__(content
, strlen(content
), s
);
436 ds_put_cstr(s
, "\">");
437 table_escape_html_text__(content
, 8, s
);
438 ds_put_cstr(s
, "</a></td>\n");
440 table_print_html_cell__("td", content
, s
);
443 ds_put_cstr(s
, " </tr>\n");
446 ds_put_cstr(s
, "</table>\n");
450 table_print_csv_cell__(const char *content
, struct ds
*s
)
454 if (!strpbrk(content
, "\n\",")) {
455 ds_put_cstr(s
, content
);
458 for (p
= content
; *p
!= '\0'; p
++) {
461 ds_put_cstr(s
, "\"\"");
473 table_print_csv__(const struct table
*table
, const struct table_style
*style
,
481 ds_put_char(s
, '\n');
484 table_print_timestamp__(table
, s
);
486 if (table
->caption
) {
487 ds_put_format(s
, "%s\n", table
->caption
);
490 if (style
->headings
) {
491 for (x
= 0; x
< table
->n_columns
; x
++) {
492 const struct column
*column
= &table
->columns
[x
];
496 table_print_csv_cell__(column
->heading
, s
);
498 ds_put_char(s
, '\n');
501 for (y
= 0; y
< table
->n_rows
; y
++) {
502 for (x
= 0; x
< table
->n_columns
; x
++) {
506 table_print_csv_cell__(cell_to_text(table_cell__(table
, y
, x
),
509 ds_put_char(s
, '\n');
514 table_print_json__(const struct table
*table
, const struct table_style
*style
,
517 struct json
*json
, *headings
, *data
;
520 json
= json_object_create();
521 if (table
->caption
) {
522 json_object_put_string(json
, "caption", table
->caption
);
524 if (table
->timestamp
) {
525 json_object_put_nocopy(
527 json_string_create_nocopy(table_format_timestamp__()));
530 headings
= json_array_create_empty();
531 for (x
= 0; x
< table
->n_columns
; x
++) {
532 const struct column
*column
= &table
->columns
[x
];
533 json_array_add(headings
, json_string_create(column
->heading
));
535 json_object_put(json
, "headings", headings
);
537 data
= json_array_create_empty();
538 for (y
= 0; y
< table
->n_rows
; y
++) {
539 struct json
*row
= json_array_create_empty();
540 for (x
= 0; x
< table
->n_columns
; x
++) {
541 const struct cell
*cell
= table_cell__(table
, y
, x
);
543 json_array_add(row
, json_string_create(cell
->text
));
544 } else if (cell
->json
) {
545 json_array_add(row
, json_clone(cell
->json
));
547 json_array_add(row
, json_null_create());
550 json_array_add(data
, row
);
552 json_object_put(json
, "data", data
);
554 json_to_ds(json
, style
->json_flags
, s
);
555 ds_put_char(s
, '\n');
559 /* Parses 'format' as the argument to a --format command line option, updating
560 * 'style->format'. */
562 table_parse_format(struct table_style
*style
, const char *format
)
564 if (!strcmp(format
, "table")) {
565 style
->format
= TF_TABLE
;
566 } else if (!strcmp(format
, "list")) {
567 style
->format
= TF_LIST
;
568 } else if (!strcmp(format
, "html")) {
569 style
->format
= TF_HTML
;
570 } else if (!strcmp(format
, "csv")) {
571 style
->format
= TF_CSV
;
572 } else if (!strcmp(format
, "json")) {
573 style
->format
= TF_JSON
;
575 ovs_fatal(0, "unknown output format \"%s\"", format
);
579 /* Parses 'format' as the argument to a --data command line option, updating
580 * 'style->cell_format'. */
582 table_parse_cell_format(struct table_style
*style
, const char *format
)
584 if (!strcmp(format
, "string")) {
585 style
->cell_format
= CF_STRING
;
586 } else if (!strcmp(format
, "bare")) {
587 style
->cell_format
= CF_BARE
;
588 } else if (!strcmp(format
, "json")) {
589 style
->cell_format
= CF_JSON
;
591 ovs_fatal(0, "unknown data format \"%s\"", format
);
596 table_format(const struct table
*table
, const struct table_style
*style
,
599 switch (style
->format
) {
601 table_print_table__(table
, style
, s
);
605 table_print_list__(table
, style
, s
);
609 table_print_html__(table
, style
, s
);
613 table_print_csv__(table
, style
, s
);
617 table_print_json__(table
, style
, s
);
623 table_format_reset(void)
628 /* Outputs 'table' on stdout in the specified 'style'. */
630 table_print(const struct table
*table
, const struct table_style
*style
)
632 struct ds s
= DS_EMPTY_INITIALIZER
;
633 table_format(table
, style
, &s
);
634 fputs(ds_cstr(&s
), stdout
);
641 printf("\nOutput formatting options:\n"
642 " -f, --format=FORMAT set output formatting to FORMAT\n"
643 " (\"table\", \"html\", \"csv\", "
645 " -d, --data=FORMAT set table cell output formatting to\n"
646 " FORMAT (\"string\", \"bare\", "
648 " --no-headings omit table heading row\n"
649 " --pretty pretty-print JSON in output\n"
650 " --bare equivalent to "
651 "\"--format=list --data=bare --no-headings\"\n");