]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add scols_table_enable_noencoding()
authorKarel Zak <kzak@redhat.com>
Mon, 31 Jul 2017 09:45:23 +0000 (11:45 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 31 Jul 2017 09:45:23 +0000 (11:45 +0200)
Now the library encode all output. It seems too strict and difficult
to use the library for some use-cases -- for example if you want to
use the library for already colorized output (by esc.sequences).

Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/smartcolsP.h
libsmartcols/src/table.c
libsmartcols/src/table_print.c

index e72637addf59fc9603a95f6ee717fbc8ffd3ad95..974123540fd7cc74ee412eca13149afd893a6b9c 100644 (file)
@@ -112,6 +112,7 @@ scols_table_add_line
 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
@@ -140,6 +141,7 @@ scols_table_is_header_repeat
 scols_table_is_json
 scols_table_is_maxout
 scols_table_is_noheadings
+scols_table_is_noencoding
 scols_table_is_nolinesep
 scols_table_is_nowrap
 scols_table_is_raw
index bce2cc725e1485c14b6f283f1f913e447ba466e2..d99d52ba0c23d962fb8b503691812a9c6688e66c 100644 (file)
@@ -226,6 +226,7 @@ extern int scols_table_is_maxout(const struct libscols_table *tb);
 extern int scols_table_is_nowrap(const struct libscols_table *tb);
 extern int scols_table_is_nolinesep(const struct libscols_table *tb);
 extern int scols_table_is_tree(const struct libscols_table *tb);
+extern int scols_table_is_noencoding(const struct libscols_table *tb);
 
 extern int scols_table_enable_colors(struct libscols_table *tb, int enable);
 extern int scols_table_enable_raw(struct libscols_table *tb, int enable);
@@ -237,6 +238,7 @@ extern int scols_table_enable_export(struct libscols_table *tb, int enable);
 extern int scols_table_enable_maxout(struct libscols_table *tb, int enable);
 extern int scols_table_enable_nowrap(struct libscols_table *tb, int enable);
 extern int scols_table_enable_nolinesep(struct libscols_table *tb, int enable);
+extern int scols_table_enable_noencoding(struct libscols_table *tb, int enable);
 
 extern int scols_table_set_column_separator(struct libscols_table *tb, const char *sep);
 extern int scols_table_set_line_separator(struct libscols_table *tb, const char *sep);
index fec33cbfdd78600d77a17cc9274352d056c4a912..a4ac39cc9391dc811557b34c41a52db7c65dfe69 100644 (file)
@@ -173,4 +173,6 @@ SMARTCOLS_2.31 {
        scols_table_get_termheight;
        scols_table_is_header_repeat;
        scols_table_enable_header_repeat;
+       scols_table_enable_noencoding;
+       scols_table_is_noencoding;
 } SMARTCOLS_2.30;
index 65009779aea2f573ae0f7a7867f4248d569200b5..b3b54cddd16f335e4f901bc40e03a989f4e7dd6b 100644 (file)
@@ -177,6 +177,7 @@ struct libscols_table {
                        header_printed  :1,     /* header already printed */
                        priv_symbols    :1,     /* default private symbols */
                        no_headings     :1,     /* don't print header */
+                       no_encode       :1,     /* don't care about control and non-printable chars */
                        no_linesep      :1,     /* don't print line separator */
                        no_wrap         :1;     /* never wrap lines */
 };
index 3306ff57c70eee6a0fac2e67520d4687f2d81037..81f4b8f2b27b36db879d86ec91a93332a7d61018 100644 (file)
@@ -1106,6 +1106,39 @@ int scols_table_is_nowrap(const struct libscols_table *tb)
        return tb->no_wrap;
 }
 
+/**
+ * scols_table_enable_noencoding:
+ * @tb: table
+ * @enable: 1 or 0
+ *
+ * The library encode non-printable and control chars by \x<hex> by default.
+ *
+ * Returns: 0 on success, negative number in case of an error.
+ *
+ * Since: 2.31
+ */
+int scols_table_enable_noencoding(struct libscols_table *tb, int enable)
+{
+       if (!tb)
+               return -EINVAL;
+       DBG(TAB, ul_debugobj(tb, "encoding: %s", enable ? "ENABLE" : "DISABLE"));
+       tb->no_encode = enable ? 1 : 0;
+       return 0;
+}
+
+/**
+ * scols_table_is_noencoding:
+ * @tb: a pointer to a struct libscols_table instance
+ *
+ * Returns: 1 if encoding is disabled.
+ *
+ * Since: 2.31
+ */
+int scols_table_is_noencoding(const struct libscols_table *tb)
+{
+       return tb->no_encode;
+}
+
 /**
  * scols_table_colors_wanted:
  * @tb: table
index 1152121015e98f1ef9c15b483a8f8588c281ae5f..472c3616c6f41ffd994feddbaa2076865d56c1a9 100644 (file)
@@ -132,7 +132,9 @@ static char *buffer_get_data(struct libscols_buffer *buf)
 }
 
 /* encode data by mbs_safe_encode() to avoid control and non-printable chars */
-static char *buffer_get_safe_data(struct libscols_buffer *buf, size_t *cells,
+static char *buffer_get_safe_data(struct libscols_table *tb,
+                                 struct libscols_buffer *buf,
+                                 size_t *cells,
                                  const char *safechars)
 {
        char *data = buffer_get_data(buf);
@@ -147,7 +149,14 @@ static char *buffer_get_safe_data(struct libscols_buffer *buf, size_t *cells,
                        goto nothing;
        }
 
-       res = mbs_safe_encode_to_buffer(data, cells, buf->encdata, safechars);
+       if (tb->no_encode) {
+               *cells = mbs_safe_width(data);
+               strcpy(buf->encdata, data);
+               res = buf->encdata;
+       } else {
+               res = mbs_safe_encode_to_buffer(data, cells, buf->encdata, safechars);
+       }
+
        if (!res || !*cells || *cells == (size_t) -1)
                goto nothing;
        return res;
@@ -251,7 +260,7 @@ static void print_empty_cell(struct libscols_table *tb,
                                line_ascii_art_to_buffer(tb, ln, art);
                                if (!list_empty(&ln->ln_branch) && has_pending_data(tb))
                                        buffer_append_data(art, vertical_symbol(tb));
-                               data = buffer_get_safe_data(art, &len_pad, NULL);
+                               data = buffer_get_safe_data(tb, art, &len_pad, NULL);
                                if (data && len_pad)
                                        fputs(data, tb->out);
                                free_buffer(art);
@@ -475,7 +484,7 @@ static int print_data(struct libscols_table *tb,
 
        /* Encode. Note that 'len' and 'width' are number of cells, not bytes.
         */
-       data = buffer_get_safe_data(buf, &len, scols_column_get_safechars(cl));
+       data = buffer_get_safe_data(tb, buf, &len, scols_column_get_safechars(cl));
        if (!data)
                data = "";
        bytes = strlen(data);
@@ -763,21 +772,30 @@ static int print_title(struct libscols_table *tb)
        DBG(TAB, ul_debugobj(tb, "printing title"));
 
        /* encode data */
-       bufsz = mbs_safe_encode_size(strlen(tb->title.data)) + 1;
-       if (bufsz == 1) {
-               DBG(TAB, ul_debugobj(tb, "title is empty string -- ignore"));
-               return 0;
-       }
-       buf = malloc(bufsz);
-       if (!buf) {
-               rc = -ENOMEM;
-               goto done;
-       }
+       if (tb->no_encode) {
+               bufsz = strlen(tb->title.data) + 1;
+               buf = strdup(tb->title.data);
+               if (!buf) {
+                       rc = -ENOMEM;
+                       goto done;
+               }
+       } else {
+               bufsz = mbs_safe_encode_size(strlen(tb->title.data)) + 1;
+               if (bufsz == 1) {
+                       DBG(TAB, ul_debugobj(tb, "title is empty string -- ignore"));
+                       return 0;
+               }
+               buf = malloc(bufsz);
+               if (!buf) {
+                       rc = -ENOMEM;
+                       goto done;
+               }
 
-       if (!mbs_safe_encode_to_buffer(tb->title.data, &bufsz, buf, NULL) ||
-           !bufsz || bufsz == (size_t) -1) {
-               rc = -EINVAL;
-               goto done;
+               if (!mbs_safe_encode_to_buffer(tb->title.data, &bufsz, buf, NULL) ||
+                   !bufsz || bufsz == (size_t) -1) {
+                       rc = -EINVAL;
+                       goto done;
+               }
        }
 
        /* truncate and align */