]>
git.proxmox.com Git - 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_print_table_line__(struct ds
*line
)
217 while (ds_last(line
) == ' ') {
225 table_format_timestamp__(void)
227 return xastrftime_msec("%Y-%m-%d %H:%M:%S.###", time_wall_msec(), true);
231 table_print_timestamp__(const struct table
*table
)
233 if (table
->timestamp
) {
234 char *s
= table_format_timestamp__();
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
= xzalloc(table
->n_columns
* sizeof *widths
);
259 for (x
= 0; x
< table
->n_columns
; x
++) {
260 const struct column
*column
= &table
->columns
[x
];
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
);
272 int max
= style
->max_column_width
;
273 if (max
> 0 && w
> max
) {
276 if (style
->headings
) {
277 int min
= strlen(column
->heading
);
285 if (style
->headings
) {
286 for (x
= 0; x
< table
->n_columns
; x
++) {
287 const struct column
*column
= &table
->columns
[x
];
289 ds_put_char(&line
, ' ');
291 ds_put_format(&line
, "%-*s", widths
[x
], column
->heading
);
293 table_print_table_line__(&line
);
295 for (x
= 0; x
< table
->n_columns
; x
++) {
297 ds_put_char(&line
, ' ');
299 ds_put_char_multiple(&line
, '-', widths
[x
]);
301 table_print_table_line__(&line
);
304 for (y
= 0; y
< table
->n_rows
; y
++) {
305 for (x
= 0; x
< table
->n_columns
; x
++) {
306 const char *text
= cell_to_text(table_cell__(table
, y
, x
), style
);
308 ds_put_char(&line
, ' ');
310 ds_put_format(&line
, "%-*.*s", widths
[x
], widths
[x
], text
);
312 table_print_table_line__(&line
);
320 table_print_list__(const struct table
*table
, const struct table_style
*style
)
329 table_print_timestamp__(table
);
331 if (table
->caption
) {
332 puts(table
->caption
);
335 for (y
= 0; y
< table
->n_rows
; y
++) {
339 for (x
= 0; x
< table
->n_columns
; x
++) {
340 const char *text
= cell_to_text(table_cell__(table
, y
, x
), style
);
341 if (style
->headings
) {
342 printf("%-20s: ", table
->columns
[x
].heading
);
350 table_escape_html_text__(const char *s
, size_t n
)
354 for (i
= 0; i
< n
; i
++) {
359 fputs("&", stdout
);
362 fputs("<", stdout
);
365 fputs(">", stdout
);
368 fputs(""", stdout
);
378 table_print_html_cell__(const char *element
, const char *content
)
382 printf(" <%s>", element
);
383 for (p
= content
; *p
; ) {
386 if (uuid_from_string_prefix(&uuid
, p
)) {
387 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN
, p
, 8, p
);
390 table_escape_html_text__(p
, 1);
394 printf("</%s>\n", element
);
398 table_print_html__(const struct table
*table
, const struct table_style
*style
)
402 table_print_timestamp__(table
);
404 fputs("<table border=1>\n", stdout
);
406 if (table
->caption
) {
407 table_print_html_cell__("caption", table
->caption
);
410 if (style
->headings
) {
411 fputs(" <tr>\n", stdout
);
412 for (x
= 0; x
< table
->n_columns
; x
++) {
413 const struct column
*column
= &table
->columns
[x
];
414 table_print_html_cell__("th", column
->heading
);
416 fputs(" </tr>\n", stdout
);
419 for (y
= 0; y
< table
->n_rows
; y
++) {
420 fputs(" <tr>\n", stdout
);
421 for (x
= 0; x
< table
->n_columns
; x
++) {
424 content
= cell_to_text(table_cell__(table
, y
, x
), style
);
425 if (!strcmp(table
->columns
[x
].heading
, "_uuid")) {
426 fputs(" <td><a name=\"", stdout
);
427 table_escape_html_text__(content
, strlen(content
));
428 fputs("\">", stdout
);
429 table_escape_html_text__(content
, 8);
430 fputs("</a></td>\n", stdout
);
432 table_print_html_cell__("td", content
);
435 fputs(" </tr>\n", stdout
);
438 fputs("</table>\n", stdout
);
442 table_print_csv_cell__(const char *content
)
446 if (!strpbrk(content
, "\n\",")) {
447 fputs(content
, stdout
);
450 for (p
= content
; *p
!= '\0'; p
++) {
453 fputs("\"\"", stdout
);
465 table_print_csv__(const struct table
*table
, const struct table_style
*style
)
474 table_print_timestamp__(table
);
476 if (table
->caption
) {
477 puts(table
->caption
);
480 if (style
->headings
) {
481 for (x
= 0; x
< table
->n_columns
; x
++) {
482 const struct column
*column
= &table
->columns
[x
];
486 table_print_csv_cell__(column
->heading
);
491 for (y
= 0; y
< table
->n_rows
; y
++) {
492 for (x
= 0; x
< table
->n_columns
; x
++) {
496 table_print_csv_cell__(cell_to_text(table_cell__(table
, y
, x
),
504 table_print_json__(const struct table
*table
, const struct table_style
*style
)
506 struct json
*json
, *headings
, *data
;
509 json
= json_object_create();
510 if (table
->caption
) {
511 json_object_put_string(json
, "caption", table
->caption
);
513 if (table
->timestamp
) {
514 char *s
= table_format_timestamp__();
515 json_object_put_string(json
, "time", s
);
519 headings
= json_array_create_empty();
520 for (x
= 0; x
< table
->n_columns
; x
++) {
521 const struct column
*column
= &table
->columns
[x
];
522 json_array_add(headings
, json_string_create(column
->heading
));
524 json_object_put(json
, "headings", headings
);
526 data
= json_array_create_empty();
527 for (y
= 0; y
< table
->n_rows
; y
++) {
528 struct json
*row
= json_array_create_empty();
529 for (x
= 0; x
< table
->n_columns
; x
++) {
530 const struct cell
*cell
= table_cell__(table
, y
, x
);
532 json_array_add(row
, json_string_create(cell
->text
));
533 } else if (cell
->json
) {
534 json_array_add(row
, json_clone(cell
->json
));
536 json_array_add(row
, json_null_create());
539 json_array_add(data
, row
);
541 json_object_put(json
, "data", data
);
543 char *s
= json_to_string(json
, style
->json_flags
);
549 /* Parses 'format' as the argument to a --format command line option, updating
550 * 'style->format'. */
552 table_parse_format(struct table_style
*style
, const char *format
)
554 if (!strcmp(format
, "table")) {
555 style
->format
= TF_TABLE
;
556 } else if (!strcmp(format
, "list")) {
557 style
->format
= TF_LIST
;
558 } else if (!strcmp(format
, "html")) {
559 style
->format
= TF_HTML
;
560 } else if (!strcmp(format
, "csv")) {
561 style
->format
= TF_CSV
;
562 } else if (!strcmp(format
, "json")) {
563 style
->format
= TF_JSON
;
565 ovs_fatal(0, "unknown output format \"%s\"", format
);
569 /* Parses 'format' as the argument to a --data command line option, updating
570 * 'style->cell_format'. */
572 table_parse_cell_format(struct table_style
*style
, const char *format
)
574 if (!strcmp(format
, "string")) {
575 style
->cell_format
= CF_STRING
;
576 } else if (!strcmp(format
, "bare")) {
577 style
->cell_format
= CF_BARE
;
578 } else if (!strcmp(format
, "json")) {
579 style
->cell_format
= CF_JSON
;
581 ovs_fatal(0, "unknown data format \"%s\"", format
);
585 /* Outputs 'table' on stdout in the specified 'style'. */
587 table_print(const struct table
*table
, const struct table_style
*style
)
589 switch (style
->format
) {
591 table_print_table__(table
, style
);
595 table_print_list__(table
, style
);
599 table_print_html__(table
, style
);
603 table_print_csv__(table
, style
);
607 table_print_json__(table
, style
);
615 printf("\nOutput formatting options:\n"
616 " -f, --format=FORMAT set output formatting to FORMAT\n"
617 " (\"table\", \"html\", \"csv\", "
619 " -d, --data=FORMAT set table cell output formatting to\n"
620 " FORMAT (\"string\", \"bare\", "
622 " --no-headings omit table heading row\n"
623 " --pretty pretty-print JSON in output\n"
624 " --bare equivalent to "
625 "\"--format=list --data=bare --no-headings\"\n");