2 * Copyright (c) 2009 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.
27 #include "command-line.h"
30 #include "dynamic-string.h"
34 #include "ovsdb-error.h"
41 #define THIS_MODULE VLM_ovsdb_client
43 /* --format: Output formatting. */
45 FMT_TABLE, /* Textual table. */
46 FMT_HTML, /* HTML table. */
47 FMT_CSV /* Comma-separated lines. */
50 /* --wide: For --format=table, the maximum output width. */
51 static int output_width;
53 /* --no-headings: Whether table output should include headings. */
54 static int output_headings = true;
56 static const struct command all_commands[];
58 static void usage(void) NO_RETURN;
59 static void parse_options(int argc, char *argv[]);
62 main(int argc, char *argv[])
64 set_program_name(argv[0]);
67 parse_options(argc, argv);
68 signal(SIGPIPE, SIG_IGN);
69 run_command(argc - optind, argv + optind, all_commands);
74 parse_options(int argc, char *argv[])
76 static struct option long_options[] = {
77 {"wide", no_argument, &output_width, INT_MAX},
78 {"format", required_argument, 0, 'f'},
79 {"no-headings", no_argument, &output_headings, 0},
80 {"verbose", optional_argument, 0, 'v'},
81 {"help", no_argument, 0, 'h'},
82 {"version", no_argument, 0, 'V'},
85 char *short_options = long_options_to_short_options(long_options);
87 output_width = isatty(fileno(stdout)) ? 79 : INT_MAX;
91 c = getopt_long(argc, argv, short_options, long_options, NULL);
98 if (!strcmp(optarg, "table")) {
99 output_format = FMT_TABLE;
100 } else if (!strcmp(optarg, "html")) {
101 output_format = FMT_HTML;
102 } else if (!strcmp(optarg, "csv")) {
103 output_format = FMT_CSV;
105 ovs_fatal(0, "unknown output format \"%s\"", optarg);
110 output_width = INT_MAX;
117 OVS_PRINT_VERSION(0, 0);
121 vlog_set_verbosity(optarg);
128 /* getopt_long() already set the value for us. */
141 printf("%s: Open vSwitch database JSON-RPC client\n"
142 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
143 "\nValid commands are:\n"
144 "\n get-schema SERVER\n"
145 " retrieve schema from SERVER\n"
146 "\n list-tables SERVER\n"
147 " list SERVER's tables\n"
148 "\n list-columns SERVER [TABLE]\n"
149 " list columns in TABLE (or all tables) on SERVER\n",
150 program_name, program_name);
151 stream_usage("SERVER", true, false);
152 printf("\nOutput formatting options:\n"
153 " -f, --format=FORMAT set output formatting to FORMAT\n"
154 " (\"table\", \"html\", or \"csv\"\n"
155 " --wide don't limit TTY lines to 79 bytes\n"
156 " --no-headings omit table heading row\n");
158 printf("\nOther options:\n"
159 " -h, --help display this help message\n"
160 " -V, --version display version information\n");
164 static struct jsonrpc *
165 open_jsonrpc(const char *server)
167 struct stream *stream;
170 error = stream_open_block(server, &stream);
172 ovs_fatal(error, "failed to connect to \"%s\"", server);
175 return jsonrpc_open(stream);
179 print_json(struct json *json)
181 char *string = json_to_string(json, JSSF_SORT);
182 fputs(string, stdout);
187 print_and_free_json(struct json *json)
194 check_ovsdb_error(struct ovsdb_error *error)
197 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
201 static struct ovsdb_schema *
202 fetch_schema(const char *server)
204 struct jsonrpc_msg *request, *reply;
205 struct ovsdb_schema *schema;
209 rpc = open_jsonrpc(server);
210 request = jsonrpc_create_request("get_schema", json_array_create_empty());
211 error = jsonrpc_transact_block(rpc, request, &reply);
213 ovs_fatal(error, "transaction failed");
215 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
216 jsonrpc_msg_destroy(reply);
229 struct column *columns;
230 size_t n_columns, allocated_columns;
231 size_t n_rows, allocated_rows;
232 size_t current_column;
236 table_init(struct table *table)
238 memset(table, 0, sizeof *table);
242 table_add_column(struct table *table, const char *heading, ...)
246 table_add_column(struct table *table, const char *heading, ...)
248 struct column *column;
251 assert(!table->n_rows);
252 if (table->n_columns >= table->allocated_columns) {
253 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
254 sizeof *table->columns);
256 column = &table->columns[table->n_columns++];
258 va_start(args, heading);
259 column->heading = xvasprintf(heading, args);
260 column->width = strlen(column->heading);
265 table_cell__(const struct table *table, size_t row, size_t column)
267 return &table->cells[column + row * table->n_columns];
271 table_add_row(struct table *table)
275 if (table->n_rows >= table->allocated_rows) {
276 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
277 table->n_columns * sizeof *table->cells);
281 table->current_column = 0;
282 for (x = 0; x < table->n_columns; x++) {
283 *table_cell__(table, y, x) = NULL;
288 table_add_cell_nocopy(struct table *table, char *s)
293 assert(table->n_rows > 0);
294 assert(table->current_column < table->n_columns);
296 x = table->current_column++;
297 y = table->n_rows - 1;
298 *table_cell__(table, y, x) = s;
301 if (length > table->columns[x].width) {
302 table->columns[x].width = length;
307 table_add_cell(struct table *table, const char *format, ...)
311 va_start(args, format);
312 table_add_cell_nocopy(table, xvasprintf(format, args));
317 table_print_table_line__(struct ds *line, size_t max_width)
319 ds_truncate(line, max_width);
325 table_print_table__(const struct table *table)
327 struct ds line = DS_EMPTY_INITIALIZER;
330 if (output_headings) {
331 for (x = 0; x < table->n_columns; x++) {
332 const struct column *column = &table->columns[x];
334 ds_put_char(&line, ' ');
336 ds_put_format(&line, "%-*s", column->width, column->heading);
338 table_print_table_line__(&line, output_width);
340 for (x = 0; x < table->n_columns; x++) {
341 const struct column *column = &table->columns[x];
345 ds_put_char(&line, ' ');
347 for (i = 0; i < column->width; i++) {
348 ds_put_char(&line, '-');
351 table_print_table_line__(&line, output_width);
354 for (y = 0; y < table->n_rows; y++) {
355 for (x = 0; x < table->n_columns; x++) {
356 const char *cell = *table_cell__(table, y, x);
358 ds_put_char(&line, ' ');
360 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
362 table_print_table_line__(&line, output_width);
369 table_print_html_cell__(const char *element, const char *content)
373 printf(" <%s>", element);
374 for (p = content; *p != '\0'; p++) {
377 fputs("&", stdout);
380 fputs("<", stdout);
383 fputs(">", stdout);
390 printf("</%s>\n", element);
394 table_print_html__(const struct table *table)
398 fputs("<table>\n", stdout);
400 if (output_headings) {
401 fputs(" <tr>\n", stdout);
402 for (x = 0; x < table->n_columns; x++) {
403 const struct column *column = &table->columns[x];
404 table_print_html_cell__("th", column->heading);
406 fputs(" </tr>\n", stdout);
409 for (y = 0; y < table->n_rows; y++) {
410 fputs(" <tr>\n", stdout);
411 for (x = 0; x < table->n_columns; x++) {
412 table_print_html_cell__("td", *table_cell__(table, y, x));
414 fputs(" </tr>\n", stdout);
417 fputs("</table>\n", stdout);
421 table_print_csv_cell__(const char *content)
425 if (!strpbrk(content, "\n\",")) {
426 fputs(content, stdout);
429 for (p = content; *p != '\0'; p++) {
432 fputs("\"\"", stdout);
444 table_print_csv__(const struct table *table)
448 if (output_headings) {
449 for (x = 0; x < table->n_columns; x++) {
450 const struct column *column = &table->columns[x];
454 table_print_csv_cell__(column->heading);
459 for (y = 0; y < table->n_rows; y++) {
460 for (x = 0; x < table->n_columns; x++) {
464 table_print_csv_cell__(*table_cell__(table, y, x));
471 table_print(const struct table *table)
473 switch (output_format) {
475 table_print_table__(table);
479 table_print_html__(table);
483 table_print_csv__(table);
489 do_get_schema(int argc UNUSED, char *argv[])
491 struct ovsdb_schema *schema = fetch_schema(argv[1]);
492 print_and_free_json(ovsdb_schema_to_json(schema));
493 ovsdb_schema_destroy(schema);
497 do_list_tables(int argc UNUSED, char *argv[])
499 struct ovsdb_schema *schema;
500 struct shash_node *node;
503 schema = fetch_schema(argv[1]);
505 table_add_column(&t, "Table");
506 table_add_column(&t, "Comment");
507 SHASH_FOR_EACH (node, &schema->tables) {
508 struct ovsdb_table_schema *ts = node->data;
511 table_add_cell(&t, ts->name);
513 table_add_cell(&t, ts->comment);
516 ovsdb_schema_destroy(schema);
521 do_list_columns(int argc UNUSED, char *argv[])
523 const char *table_name = argv[2];
524 struct ovsdb_schema *schema;
525 struct shash_node *table_node;
528 schema = fetch_schema(argv[1]);
531 table_add_column(&t, "Table");
533 table_add_column(&t, "Column");
534 table_add_column(&t, "Type");
535 table_add_column(&t, "Comment");
536 SHASH_FOR_EACH (table_node, &schema->tables) {
537 struct ovsdb_table_schema *ts = table_node->data;
539 if (!table_name || !strcmp(table_name, ts->name)) {
540 struct shash_node *column_node;
542 SHASH_FOR_EACH (column_node, &ts->columns) {
543 struct ovsdb_column *column = column_node->data;
544 struct json *type = ovsdb_type_to_json(&column->type);
548 table_add_cell(&t, ts->name);
550 table_add_cell(&t, column->name);
551 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
552 if (column->comment) {
553 table_add_cell(&t, column->comment);
560 ovsdb_schema_destroy(schema);
565 do_help(int argc UNUSED, char *argv[] UNUSED)
570 static const struct command all_commands[] = {
571 { "get-schema", 1, 1, do_get_schema },
572 { "list-tables", 1, 1, do_list_tables },
573 { "list-columns", 1, 2, do_list_columns },
574 { "help", 0, INT_MAX, do_help },
575 { NULL, 0, 0, NULL },