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, true);
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);
171 if (error == EAFNOSUPPORT) {
172 struct pstream *pstream;
174 error = pstream_open(server, &pstream);
176 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
179 VLOG_INFO("%s: waiting for connection...", server);
180 error = pstream_accept_block(pstream, &stream);
182 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
185 pstream_close(pstream);
187 ovs_fatal(error, "failed to connect to \"%s\"", server);
190 return jsonrpc_open(stream);
194 print_json(struct json *json)
196 char *string = json_to_string(json, JSSF_SORT);
197 fputs(string, stdout);
202 print_and_free_json(struct json *json)
209 check_ovsdb_error(struct ovsdb_error *error)
212 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
216 static struct ovsdb_schema *
217 fetch_schema(const char *server)
219 struct jsonrpc_msg *request, *reply;
220 struct ovsdb_schema *schema;
224 rpc = open_jsonrpc(server);
225 request = jsonrpc_create_request("get_schema", json_array_create_empty());
226 error = jsonrpc_transact_block(rpc, request, &reply);
228 ovs_fatal(error, "transaction failed");
230 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
231 jsonrpc_msg_destroy(reply);
244 struct column *columns;
245 size_t n_columns, allocated_columns;
246 size_t n_rows, allocated_rows;
247 size_t current_column;
251 table_init(struct table *table)
253 memset(table, 0, sizeof *table);
257 table_add_column(struct table *table, const char *heading, ...)
261 table_add_column(struct table *table, const char *heading, ...)
263 struct column *column;
266 assert(!table->n_rows);
267 if (table->n_columns >= table->allocated_columns) {
268 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
269 sizeof *table->columns);
271 column = &table->columns[table->n_columns++];
273 va_start(args, heading);
274 column->heading = xvasprintf(heading, args);
275 column->width = strlen(column->heading);
280 table_cell__(const struct table *table, size_t row, size_t column)
282 return &table->cells[column + row * table->n_columns];
286 table_add_row(struct table *table)
290 if (table->n_rows >= table->allocated_rows) {
291 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
292 table->n_columns * sizeof *table->cells);
296 table->current_column = 0;
297 for (x = 0; x < table->n_columns; x++) {
298 *table_cell__(table, y, x) = NULL;
303 table_add_cell_nocopy(struct table *table, char *s)
308 assert(table->n_rows > 0);
309 assert(table->current_column < table->n_columns);
311 x = table->current_column++;
312 y = table->n_rows - 1;
313 *table_cell__(table, y, x) = s;
316 if (length > table->columns[x].width) {
317 table->columns[x].width = length;
322 table_add_cell(struct table *table, const char *format, ...)
326 va_start(args, format);
327 table_add_cell_nocopy(table, xvasprintf(format, args));
332 table_print_table_line__(struct ds *line, size_t max_width)
334 ds_truncate(line, max_width);
340 table_print_table__(const struct table *table)
342 struct ds line = DS_EMPTY_INITIALIZER;
345 if (output_headings) {
346 for (x = 0; x < table->n_columns; x++) {
347 const struct column *column = &table->columns[x];
349 ds_put_char(&line, ' ');
351 ds_put_format(&line, "%-*s", column->width, column->heading);
353 table_print_table_line__(&line, output_width);
355 for (x = 0; x < table->n_columns; x++) {
356 const struct column *column = &table->columns[x];
360 ds_put_char(&line, ' ');
362 for (i = 0; i < column->width; i++) {
363 ds_put_char(&line, '-');
366 table_print_table_line__(&line, output_width);
369 for (y = 0; y < table->n_rows; y++) {
370 for (x = 0; x < table->n_columns; x++) {
371 const char *cell = *table_cell__(table, y, x);
373 ds_put_char(&line, ' ');
375 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
377 table_print_table_line__(&line, output_width);
384 table_print_html_cell__(const char *element, const char *content)
388 printf(" <%s>", element);
389 for (p = content; *p != '\0'; p++) {
392 fputs("&", stdout);
395 fputs("<", stdout);
398 fputs(">", stdout);
405 printf("</%s>\n", element);
409 table_print_html__(const struct table *table)
413 fputs("<table>\n", stdout);
415 if (output_headings) {
416 fputs(" <tr>\n", stdout);
417 for (x = 0; x < table->n_columns; x++) {
418 const struct column *column = &table->columns[x];
419 table_print_html_cell__("th", column->heading);
421 fputs(" </tr>\n", stdout);
424 for (y = 0; y < table->n_rows; y++) {
425 fputs(" <tr>\n", stdout);
426 for (x = 0; x < table->n_columns; x++) {
427 table_print_html_cell__("td", *table_cell__(table, y, x));
429 fputs(" </tr>\n", stdout);
432 fputs("</table>\n", stdout);
436 table_print_csv_cell__(const char *content)
440 if (!strpbrk(content, "\n\",")) {
441 fputs(content, stdout);
444 for (p = content; *p != '\0'; p++) {
447 fputs("\"\"", stdout);
459 table_print_csv__(const struct table *table)
463 if (output_headings) {
464 for (x = 0; x < table->n_columns; x++) {
465 const struct column *column = &table->columns[x];
469 table_print_csv_cell__(column->heading);
474 for (y = 0; y < table->n_rows; y++) {
475 for (x = 0; x < table->n_columns; x++) {
479 table_print_csv_cell__(*table_cell__(table, y, x));
486 table_print(const struct table *table)
488 switch (output_format) {
490 table_print_table__(table);
494 table_print_html__(table);
498 table_print_csv__(table);
504 do_get_schema(int argc UNUSED, char *argv[])
506 struct ovsdb_schema *schema = fetch_schema(argv[1]);
507 print_and_free_json(ovsdb_schema_to_json(schema));
508 ovsdb_schema_destroy(schema);
512 do_list_tables(int argc UNUSED, char *argv[])
514 struct ovsdb_schema *schema;
515 struct shash_node *node;
518 schema = fetch_schema(argv[1]);
520 table_add_column(&t, "Table");
521 table_add_column(&t, "Comment");
522 SHASH_FOR_EACH (node, &schema->tables) {
523 struct ovsdb_table_schema *ts = node->data;
526 table_add_cell(&t, ts->name);
528 table_add_cell(&t, ts->comment);
531 ovsdb_schema_destroy(schema);
536 do_list_columns(int argc UNUSED, char *argv[])
538 const char *table_name = argv[2];
539 struct ovsdb_schema *schema;
540 struct shash_node *table_node;
543 schema = fetch_schema(argv[1]);
546 table_add_column(&t, "Table");
548 table_add_column(&t, "Column");
549 table_add_column(&t, "Type");
550 table_add_column(&t, "Comment");
551 SHASH_FOR_EACH (table_node, &schema->tables) {
552 struct ovsdb_table_schema *ts = table_node->data;
554 if (!table_name || !strcmp(table_name, ts->name)) {
555 struct shash_node *column_node;
557 SHASH_FOR_EACH (column_node, &ts->columns) {
558 struct ovsdb_column *column = column_node->data;
559 struct json *type = ovsdb_type_to_json(&column->type);
563 table_add_cell(&t, ts->name);
565 table_add_cell(&t, column->name);
566 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
567 if (column->comment) {
568 table_add_cell(&t, column->comment);
575 ovsdb_schema_destroy(schema);
580 do_help(int argc UNUSED, char *argv[] UNUSED)
585 static const struct command all_commands[] = {
586 { "get-schema", 1, 1, do_get_schema },
587 { "list-tables", 1, 1, do_list_tables },
588 { "list-columns", 1, 2, do_list_columns },
589 { "help", 0, INT_MAX, do_help },
590 { NULL, 0, 0, NULL },