-static struct ovsdb_schema *
-fetch_schema(const char *server, const char *database)
-{
- struct ovsdb_schema *schema;
- struct jsonrpc *rpc;
-
- rpc = open_jsonrpc(server);
- schema = fetch_schema_from_rpc(rpc, database);
- jsonrpc_close(rpc);
-
- return schema;
-}
-\f
-struct column {
- char *heading;
- int width;
-};
-
-struct table {
- char **cells;
- struct column *columns;
- size_t n_columns, allocated_columns;
- size_t n_rows, allocated_rows;
- size_t current_column;
- char *caption;
-};
-
-static void
-table_init(struct table *table)
-{
- memset(table, 0, sizeof *table);
-}
-
-static void
-table_destroy(struct table *table)
-{
- size_t i;
-
- for (i = 0; i < table->n_columns; i++) {
- free(table->columns[i].heading);
- }
- free(table->columns);
-
- for (i = 0; i < table->n_columns * table->n_rows; i++) {
- free(table->cells[i]);
- }
- free(table->cells);
-
- free(table->caption);
-}
-
-static void
-table_set_caption(struct table *table, char *caption)
-{
- free(table->caption);
- table->caption = caption;
-}
-
-static void
-table_add_column(struct table *table, const char *heading, ...)
- PRINTF_FORMAT(2, 3);
-
-static void
-table_add_column(struct table *table, const char *heading, ...)
-{
- struct column *column;
- va_list args;
-
- assert(!table->n_rows);
- if (table->n_columns >= table->allocated_columns) {
- table->columns = x2nrealloc(table->columns, &table->allocated_columns,
- sizeof *table->columns);
- }
- column = &table->columns[table->n_columns++];
-
- va_start(args, heading);
- column->heading = xvasprintf(heading, args);
- column->width = strlen(column->heading);
- va_end(args);
-}
-
-static char **
-table_cell__(const struct table *table, size_t row, size_t column)
-{
- return &table->cells[column + row * table->n_columns];
-}
-
-static void
-table_add_row(struct table *table)
-{
- size_t x, y;
-
- if (table->n_rows >= table->allocated_rows) {
- table->cells = x2nrealloc(table->cells, &table->allocated_rows,
- table->n_columns * sizeof *table->cells);
- }
-
- y = table->n_rows++;
- table->current_column = 0;
- for (x = 0; x < table->n_columns; x++) {
- *table_cell__(table, y, x) = NULL;
- }
-}
-
-static void
-table_add_cell_nocopy(struct table *table, char *s)
-{
- size_t x, y;
- int length;
-
- assert(table->n_rows > 0);
- assert(table->current_column < table->n_columns);
-
- x = table->current_column++;
- y = table->n_rows - 1;
- *table_cell__(table, y, x) = s;
-
- length = strlen(s);
- if (length > table->columns[x].width) {
- table->columns[x].width = length;
- }
-}
-
-static void
-table_add_cell(struct table *table, const char *format, ...)
-{
- va_list args;
-
- va_start(args, format);
- table_add_cell_nocopy(table, xvasprintf(format, args));
- va_end(args);
-}
-
-static void
-table_print_table_line__(struct ds *line)
-{
- puts(ds_cstr(line));
- ds_clear(line);
-}
-
-static void
-table_print_table__(const struct table *table)
-{
- static int n = 0;
- struct ds line = DS_EMPTY_INITIALIZER;
- size_t x, y;
-
- if (n++ > 0) {
- putchar('\n');
- }
-
- if (output_headings) {
- for (x = 0; x < table->n_columns; x++) {
- const struct column *column = &table->columns[x];
- if (x) {
- ds_put_char(&line, ' ');
- }
- ds_put_format(&line, "%-*s", column->width, column->heading);
- }
- table_print_table_line__(&line);
-
- for (x = 0; x < table->n_columns; x++) {
- const struct column *column = &table->columns[x];
- int i;
-
- if (x) {
- ds_put_char(&line, ' ');
- }
- for (i = 0; i < column->width; i++) {
- ds_put_char(&line, '-');
- }
- }
- table_print_table_line__(&line);
- }
-
- for (y = 0; y < table->n_rows; y++) {
- for (x = 0; x < table->n_columns; x++) {
- const char *cell = *table_cell__(table, y, x);
- if (x) {
- ds_put_char(&line, ' ');
- }
- ds_put_format(&line, "%-*s", table->columns[x].width, cell);
- }
- table_print_table_line__(&line);
- }
-
- ds_destroy(&line);
-}
-
-static void
-table_escape_html_text__(const char *s, size_t n)
-{
- size_t i;
-
- for (i = 0; i < n; i++) {
- char c = s[i];
-
- switch (c) {
- case '&':
- fputs("&", stdout);
- break;
- case '<':
- fputs("<", stdout);
- break;
- case '>':
- fputs(">", stdout);
- break;
- case '"':
- fputs(""", stdout);
- break;
- default:
- putchar(c);
- break;
- }
- }
-}
-
-static void
-table_print_html_cell__(const char *element, const char *content)
-{
- const char *p;
-
- printf(" <%s>", element);
- for (p = content; *p; ) {
- struct uuid uuid;
-
- if (uuid_from_string_prefix(&uuid, p)) {
- printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
- p += UUID_LEN;
- } else {
- table_escape_html_text__(p, 1);
- p++;
- }
- }
- printf("</%s>\n", element);
-}
-
-static void
-table_print_html__(const struct table *table)
-{
- size_t x, y;
-
- fputs("<table border=1>\n", stdout);
-
- if (table->caption) {
- table_print_html_cell__("caption", table->caption);
- }
-
- if (output_headings) {
- fputs(" <tr>\n", stdout);
- for (x = 0; x < table->n_columns; x++) {
- const struct column *column = &table->columns[x];
- table_print_html_cell__("th", column->heading);
- }
- fputs(" </tr>\n", stdout);
- }
-
- for (y = 0; y < table->n_rows; y++) {
- fputs(" <tr>\n", stdout);
- for (x = 0; x < table->n_columns; x++) {
- const char *content = *table_cell__(table, y, x);
-
- if (!strcmp(table->columns[x].heading, "_uuid")) {
- fputs(" <td><a name=\"", stdout);
- table_escape_html_text__(content, strlen(content));
- fputs("\">", stdout);
- table_escape_html_text__(content, 8);
- fputs("</a></td>\n", stdout);
- } else {
- table_print_html_cell__("td", content);
- }
- }
- fputs(" </tr>\n", stdout);
- }
-
- fputs("</table>\n", stdout);
-}
-
-static void
-table_print_csv_cell__(const char *content)
-{
- const char *p;
-
- if (!strpbrk(content, "\n\",")) {
- fputs(content, stdout);
- } else {
- putchar('"');
- for (p = content; *p != '\0'; p++) {
- switch (*p) {
- case '"':
- fputs("\"\"", stdout);
- break;
- default:
- putchar(*p);
- break;
- }
- }
- putchar('"');
- }
-}
-