scols_column_get_flags
scols_column_get_header
scols_column_get_json_type
+scols_column_get_name
+scols_column_get_name_as_shellvar
scols_column_get_safechars
scols_column_get_table
scols_column_get_whint
scols_column_set_color
scols_column_set_flags
scols_column_set_json_type
+scols_column_set_name
scols_column_set_safechars
scols_column_set_whint
scols_column_set_wrapfunc
scols_table_colors_wanted
scols_table_enable_ascii
scols_table_enable_colors
-scols_table_enable_noencoding
scols_table_enable_export
scols_table_enable_header_repeat
scols_table_enable_json
scols_table_enable_maxout
scols_table_enable_minout
+scols_table_enable_noencoding
scols_table_enable_noheadings
scols_table_enable_nolinesep
scols_table_enable_nowrap
scols_table_enable_raw
+scols_table_enable_shellvar
scols_table_get_column
scols_table_get_column_separator
scols_table_get_line
scols_table_is_json
scols_table_is_maxout
scols_table_is_minout
-scols_table_is_noheadings
scols_table_is_noencoding
+scols_table_is_noheadings
scols_table_is_nolinesep
scols_table_is_nowrap
scols_table_is_raw
+scols_table_is_shellvar
scols_table_is_tree
scols_table_move_column
scols_table_new_column
scols_table_new_line
scols_table_next_column
-scols_table_set_columns_iter
scols_table_next_line
scols_table_reduce_termwidth
scols_table_remove_column
scols_table_remove_line
scols_table_remove_lines
scols_table_set_column_separator
+scols_table_set_columns_iter
scols_table_set_default_symbols
scols_table_set_line_separator
scols_table_set_name
switch (nlines) {
case 0: /* NAME */
- {
- struct libscols_cell *hr;
-
cl = scols_new_column();
if (!cl)
goto fail;
- hr = scols_column_get_header(cl);
- if (!hr || scols_cell_set_data(hr, line))
+ if (scols_column_set_name(cl, line) != 0)
goto fail;
break;
- }
+
case 1: /* WIDTH-HINT */
{
double whint = strtod_or_err(line, "failed to parse column whint");
free(cl->color);
free(cl->safechars);
free(cl->pending_data_buf);
+ free(cl->shellvar);
free(cl);
}
}
return &cl->header;
}
+/**
+ * scols_column_set_name:
+ * @cl: a pointer to a struct libscols_column instance
+ * @name: column name
+ *
+ * Returns: 0, a negative value in case of an error.
+ *
+ * Since: 2.38
+ */
+int scols_column_set_name(struct libscols_column *cl, const char *name)
+{
+ struct libscols_cell *hr = scols_column_get_header(cl);
+
+ if (!hr)
+ return -EINVAL;
+
+ free(cl->shellvar);
+ cl->shellvar = NULL;
+
+ return scols_cell_set_data(hr, name);
+}
+
+/**
+ * scols_column_get_name:
+ * @cl: a pointer to a struct libscols_column instance
+ *
+ * Returns: A pointer to a column name, which is stored in column header
+ *
+ * Since: 2.38
+ */
+const char *scols_column_get_name(struct libscols_column *cl)
+{
+ return scols_cell_get_data(&cl->header);
+}
+
+/**
+ * scols_column_get_name_as_shellvar
+ * @cl: a pointer to a struct libscols_column instance
+ *
+ * Like scols_column_get_name(), but column name is modified to be compatible with shells
+ * requirements for variable names.
+ *
+ * Since: 2.38
+ */
+const char *scols_column_get_name_as_shellvar(struct libscols_column *cl)
+{
+ if (!cl->shellvar) {
+ const char *s, *name = scols_column_get_name(cl);
+ char *p;
+ size_t sz;
+
+ if (!name || !*name)
+ return NULL;
+
+ /* "1FOO%" --> "_1FOO_PCT */
+ sz = strlen(name) + 1 + 3;
+ p = cl->shellvar = calloc(1, sz + 1);
+ if (!cl->shellvar)
+ return NULL;
+
+ /* convert "1FOO" to "_1FOO" */
+ if (!isalpha(*name))
+ *p++ = '_';
+
+ /* replace all "bad" chars with "_" */
+ for (s = name; *s; s++)
+ *p++ = !isalnum(*s) ? '_' : *s;
+
+ if (!*s && *(s - 1) == '%') {
+ *p++ = 'P';
+ *p++ = 'C';
+ *p++ = 'T';
+ }
+ }
+ return cl->shellvar;
+}
+
+
/**
* scols_column_set_color:
* @cl: a pointer to a struct libscols_column instance
extern const char *scols_column_get_color(const struct libscols_column *cl);
extern struct libscols_table *scols_column_get_table(const struct libscols_column *cl);
+extern int scols_column_set_name(struct libscols_column *cl, const char *name);
+extern const char *scols_column_get_name(struct libscols_column *cl);
+extern const char *scols_column_get_name_as_shellvar(struct libscols_column *cl);
+
extern int scols_column_set_cmpfunc(struct libscols_column *cl,
int (*cmp)(struct libscols_cell *a,
struct libscols_cell *b, void *),
extern int scols_table_is_header_repeat(const struct libscols_table *tb);
extern int scols_table_is_empty(const struct libscols_table *tb);
extern int scols_table_is_export(const struct libscols_table *tb);
+extern int scols_table_is_shellvar(const struct libscols_table *tb);
extern int scols_table_is_maxout(const struct libscols_table *tb);
extern int scols_table_is_minout(const struct libscols_table *tb);
extern int scols_table_is_nowrap(const struct libscols_table *tb);
extern int scols_table_enable_noheadings(struct libscols_table *tb, int enable);
extern int scols_table_enable_header_repeat(struct libscols_table *tb, int enable);
extern int scols_table_enable_export(struct libscols_table *tb, int enable);
+extern int scols_table_enable_shellvar(struct libscols_table *tb, int enable);
extern int scols_table_enable_maxout(struct libscols_table *tb, int enable);
extern int scols_table_enable_minout(struct libscols_table *tb, int enable);
extern int scols_table_enable_nowrap(struct libscols_table *tb, int enable);
SMARTCOLS_2.38 {
scols_line_get_column_data;
+ scols_column_set_name;
+ scols_column_get_name;
+ scols_column_get_name_as_shellvar;
+ scols_table_is_shellvar;
+ scols_table_enable_shellvar;
} SMARTCOLS_2.35;
if (!data)
data = "";
- if (tb->format != SCOLS_FMT_HUMAN)
- name = scols_cell_get_data(&cl->header);
+ if (tb->format != SCOLS_FMT_HUMAN) {
+ name = scols_table_is_shellvar(tb) ?
+ scols_column_get_name_as_shellvar(cl) :
+ scols_column_get_name(cl);
+ }
is_last = is_last_column(cl);
return 0;
case SCOLS_FMT_EXPORT:
- fputs_shell_ident(name, tb->out);
- if (endswith(name, "%"))
- fputs("PCT", tb->out);
+ fputs(name, tb->out);
fputc('=', tb->out);
fputs_quoted(data, tb->out);
if (!is_last)
}
}
if (!rc)
- rc = ul_buffer_append_string(buf, scols_cell_get_data(&cl->header));
+ rc = ul_buffer_append_string(buf,
+ scols_table_is_shellvar(tb) ?
+ scols_column_get_name_as_shellvar(cl) :
+ scols_column_get_name(cl));
if (!rc)
rc = print_data(tb, cl, NULL, &cl->header, buf);
}
while (scols_table_next_column(tb, &itr, &cl) == 0) {
if (scols_column_is_hidden(cl))
continue;
- extra_bufsz += strlen(scols_cell_get_data(&cl->header)); /* data */
+
+ if (scols_column_get_name(cl))
+ extra_bufsz += strlen(scols_column_get_name(cl)); /* data */
extra_bufsz += 2; /* separators */
}
break;
void *wrapfunc_data;
- struct libscols_cell header;
+ struct libscols_cell header; /* column name with color etc. */
+ char *shellvar; /* raw colum name in shell compatible format */
+
struct list_head cl_columns; /* member of table->tb_columns */
struct libscols_table *table;
is_term :1, /* isatty() */
padding_debug :1, /* output visible padding chars */
is_dummy_print :1, /* printing used for width calculation only */
+ is_shellvar :1, /* shell compatible column names */
maxout :1, /* maximize output */
minout :1, /* minimize output (mutually exclusive to maxout) */
header_repeat :1, /* print header after libscols_table->termheight */
int flags)
{
struct libscols_column *cl;
- struct libscols_cell *hr;
if (!tb)
return NULL;
if (!cl)
return NULL;
- /* set column name */
- hr = scols_column_get_header(cl);
- if (!hr)
+ if (scols_column_set_name(cl, name))
goto err;
- if (scols_cell_set_data(hr, name))
- goto err;
-
scols_column_set_whint(cl, whint);
scols_column_set_flags(cl, flags);
* Enable/disable export output format (COLUMNAME="value" ...).
* The parsable output formats (export and raw) are mutually exclusive.
*
- * Note that COLUMNAME maybe be modified on output to contains only chars
- * allowed as shell variable identifiers, for example MIN-IO and FSUSE% will be
- * MIN_IO and FSUSE_PCT.
+ * See also scols_table_enable_shellvar(). Note that in version 2.37 (and only
+ * in this version) scols_table_enable_shellvar() functionality has been
+ * automatically enabled for "export" format. This behavior has been reverted
+ * in version 2.38 due to backward compatibility issues. Now it's necessary to
+ * explicitly call scols_table_enable_shellvar().
*
* Returns: 0 on success, negative number in case of an error.
*/
return 0;
}
+/**
+ * scols_table_enable_shellvar:
+ * @tb: table
+ * @enable: 1 or 0
+ *
+ * Force library to print column names to be compatible with shell requirements
+ * to variable names. For example "1FOO%" will be printed as "_1FOO_PCT".
+ *
+ * Returns: 0 on success, negative number in case of an error.
+ *
+ * Since: 2.38
+ */
+int scols_table_enable_shellvar(struct libscols_table *tb, int enable)
+{
+ if (!tb)
+ return -EINVAL;
+
+ DBG(TAB, ul_debugobj(tb, "shellvar: %s", enable ? "ENABLE" : "DISABLE"));
+ tb->is_shellvar = enable ? 1 : 0;
+ return 0;
+}
+
+
/**
* scols_table_enable_ascii:
* @tb: table
return tb->format == SCOLS_FMT_EXPORT;
}
+/**
+ * scols_table_is_shellvar:
+ * @tb: table
+ *
+ * Returns: 1 if column names has to be compatible with shell requirements
+ * to variable names
+ *
+ * Since: 2.38
+ */
+int scols_table_is_shellvar(const struct libscols_table *tb)
+{
+ return tb->is_shellvar;
+}
+
/**
* scols_table_is_raw:
* @tb: table