- n_columns = table->class->n_columns + 1;
- columns = xmalloc(n_columns * sizeof *columns);
- columns[0] = NULL;
- for (i = 0; i < table->class->n_columns; i++) {
- columns[i + 1] = &table->class->columns[i];
- }
- } else {
- char *s = xstrdup(column_names);
- size_t allocated_columns;
- char *save_ptr = NULL;
- char *column_name;
-
- columns = NULL;
- allocated_columns = n_columns = 0;
- for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
- column_name = strtok_r(NULL, ", ", &save_ptr)) {
- const struct ovsdb_idl_column *column;
-
- if (!strcasecmp(column_name, "_uuid")) {
- column = NULL;
- } else {
- die_if_error(get_column(table, column_name, &column));
- }
- if (n_columns >= allocated_columns) {
- columns = x2nrealloc(columns, &allocated_columns,
- sizeof *columns);
- }
- columns[n_columns++] = column;
- }
- free(s);
-
- if (!n_columns) {
- vsctl_fatal("must specify at least one column name");
- }
- }
- *columnsp = columns;
- *n_columnsp = n_columns;
-}
-
-
-static void
-pre_list_columns(struct vsctl_context *ctx,
- const struct vsctl_table_class *table,
- const char *column_names)
-{
- const struct ovsdb_idl_column **columns;
- size_t n_columns;
- size_t i;
-
- parse_column_names(column_names, table, &columns, &n_columns);
- for (i = 0; i < n_columns; i++) {
- if (columns[i]) {
- ovsdb_idl_add_column(ctx->idl, columns[i]);
- }
- }
- free(columns);
-}
-
-static void
-pre_cmd_list(struct vsctl_context *ctx)
-{
- const char *column_names = shash_find_data(&ctx->options, "--columns");
- const char *table_name = ctx->argv[1];
- const struct vsctl_table_class *table;
-
- table = pre_get_table(ctx, table_name);
- pre_list_columns(ctx, table, column_names);
-}
-
-static struct table *
-list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
-{
- struct table *out;
- size_t i;
-
- out = xmalloc(sizeof *out);
- table_init(out);
-
- for (i = 0; i < n_columns; i++) {
- const struct ovsdb_idl_column *column = columns[i];
- const char *column_name = column ? column->name : "_uuid";
-
- table_add_column(out, "%s", column_name);
- }
-
- return out;
-}
-
-static void
-list_record(const struct ovsdb_idl_row *row,
- const struct ovsdb_idl_column **columns, size_t n_columns,
- struct table *out)
-{
- size_t i;
-
- table_add_row(out);
- for (i = 0; i < n_columns; i++) {
- const struct ovsdb_idl_column *column = columns[i];
- struct cell *cell = table_add_cell(out);
-
- if (!column) {
- struct ovsdb_datum datum;
- union ovsdb_atom atom;
-
- atom.uuid = row->uuid;
-
- datum.keys = &atom;
- datum.values = NULL;
- datum.n = 1;
-
- cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
- cell->type = &ovsdb_type_uuid;
- } else {
- const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
-
- cell->json = ovsdb_datum_to_json(datum, &column->type);
- cell->type = &column->type;
- }
- }
-}
-
-static void
-cmd_list(struct vsctl_context *ctx)
-{
- const char *column_names = shash_find_data(&ctx->options, "--columns");
- const struct ovsdb_idl_column **columns;
- const char *table_name = ctx->argv[1];
- const struct vsctl_table_class *table;
- struct table *out;
- size_t n_columns;
- int i;
-
- table = get_table(table_name);
- parse_column_names(column_names, table, &columns, &n_columns);
- out = ctx->table = list_make_table(columns, n_columns);
- if (ctx->argc > 2) {
- for (i = 2; i < ctx->argc; i++) {
- list_record(must_get_row(ctx, table, ctx->argv[i]),
- columns, n_columns, out);
- }
- } else {
- const struct ovsdb_idl_row *row;
-
- for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
- row = ovsdb_idl_next_row(row)) {
- list_record(row, columns, n_columns, out);
- }
- }
- free(columns);
-}
-
-static void
-pre_cmd_find(struct vsctl_context *ctx)
-{
- const char *column_names = shash_find_data(&ctx->options, "--columns");
- const char *table_name = ctx->argv[1];
- const struct vsctl_table_class *table;
- int i;
-
- table = pre_get_table(ctx, table_name);
- pre_list_columns(ctx, table, column_names);
- for (i = 2; i < ctx->argc; i++) {
- pre_parse_column_key_value(ctx, ctx->argv[i], table);
- }
-}
-
-static void
-cmd_find(struct vsctl_context *ctx)
-{
- const char *column_names = shash_find_data(&ctx->options, "--columns");
- const struct ovsdb_idl_column **columns;
- const char *table_name = ctx->argv[1];
- const struct vsctl_table_class *table;
- const struct ovsdb_idl_row *row;
- struct table *out;
- size_t n_columns;
-
- table = get_table(table_name);
- parse_column_names(column_names, table, &columns, &n_columns);
- out = ctx->table = list_make_table(columns, n_columns);
- for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
- row = ovsdb_idl_next_row(row)) {
- int i;
-
- for (i = 2; i < ctx->argc; i++) {
- if (!is_condition_satisfied(table, row, ctx->argv[i],
- ctx->symtab)) {
- goto next_row;
- }
- }
- list_record(row, columns, n_columns, out);
-
- next_row: ;
- }
- free(columns);
-}
-
-static void
-pre_cmd_set(struct vsctl_context *ctx)
-{
- const char *table_name = ctx->argv[1];
- const struct vsctl_table_class *table;
- int i;
-
- table = pre_get_table(ctx, table_name);
- for (i = 3; i < ctx->argc; i++) {
- pre_parse_column_key_value(ctx, ctx->argv[i], table);
- }
-}
-
-static void
-set_column(const struct vsctl_table_class *table,
- const struct ovsdb_idl_row *row, const char *arg,
- struct ovsdb_symbol_table *symtab)
-{
- const struct ovsdb_idl_column *column;
- char *key_string, *value_string;
- char *error;
-
- error = parse_column_key_value(arg, table, &column, &key_string,
- NULL, NULL, 0, &value_string);
- die_if_error(error);
- if (!value_string) {
- vsctl_fatal("%s: missing value", arg);
- }
-
- if (key_string) {
- union ovsdb_atom key, value;
- struct ovsdb_datum datum;
-
- if (column->type.value.type == OVSDB_TYPE_VOID) {
- vsctl_fatal("cannot specify key to set for non-map column %s",
- column->name);