Commit Graph

327 Commits

Author SHA1 Message Date
Karel Zak aac716f503 libsmartcols: make buffers append function more robust
Fixes: https://github.com/karelzak/util-linux/issues/1279
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-04-14 16:25:30 +02:00
Karel Zak 529b51706e libsmartcols: introduce default sort column
* add default sort column, set by scols_sort_table()

* sort tree according to default sort column also in scols_sort_table_by_tree()

The function scols_sort_table() does not sort tree branches if tree
is not enabled. The function scols_sort_table_by_tree() does not care
if tree is enabled and it always follows parent->child relations. For
scols_sort_table_by_tree() we need to follow order in branches if
previously scols_sort_table() has been called.

For example lsblk calls

	scols_sort_table(tb, cl);
	scols_sort_table_by_tree(tb);

for list-like output (for example lsblk -P) and users assume the
same order as for tree (lsblk --tree).

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1940607
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-03-24 12:43:17 +01:00
Karel Zak c59d442809 libsmartcols: add sort sunction to the sample
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-03-24 12:42:58 +01:00
Karel Zak 308a097968 meson: update sources and dependencies
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-03-17 15:07:27 +01:00
Zbigniew Jędrzejewski-Szmek 69939195bd meson: implement building of static programs
The whole thing is complicated by the fact that we have two layers
of libraries: e.g. libmount also needs libblkid. If we just tell meson
to make libmount static, this is not enough, because we also need it
to link to a static libblkid. Hence in the case of libs that link to
other libs internally, we need to create a different object with a
a different set of link_with items.

To avoid building the libraries twice, libfdisk and libmount are first
built into an internal "convenience" library, which is then linked into
the static and shared versions as appropriate.
2021-03-17 15:07:27 +01:00
Zbigniew Jędrzejewski-Szmek d4c880d5a4 meson: add second build system
To build: meson build && ninja -C build
To run tests: ninja -C build check
To install for packaging: DESTDIR=/var/tmp/inst ninja -C build install
To install for realz: sudo ninja -C build install

v2:
- Optional items are now based on the 'feature' feature in meson.
  Built libraries which are disabled turn into disabler() objects
  and also poison any executables which link to them.

What is there:
- building of the binaries and libs and the python module
- installation of binaries, libs, python module, localization files,
  man pages, pkgconfig files
- running of tests
- most options to configure build equivalently to the
  ./configure settings

Partially implemented:
- disabling of stuff when things missing. In the C code, the defines
  are all used, so that should be fine. In the build system, some
  files should be skipped, but that is probably not always done properly.
  Getting this right might require some testing of various build option
  combinations to get the details right.

Not implemented:
- static builds of fdisk and other binaries
- things marked with XXX or FIXME
- ???

Differences:
- .la files are not created. They are useless and everybody hates them.
- Requires.private in pkgconfig files are not present in the
  autogenerated .pc file. Not sure if they should be there or not. If
  necessary, they can be added by hand.
- man pages and systemd units are installed by the install target. Not
  sure why 'make install' doesn't do that.
- the split between / and /usr is probably wrong. But it's all pointless
  anyway, so maybe we could simplify things but not implementing it at
  all under meson?
2021-03-17 15:07:27 +01:00
Karel Zak ef7e258b3d libsmratcols: print title color only when wanted
Reference: 676fb001d6.
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-03-15 16:19:23 +01:00
Karel Zak 676fb001d6 libsmartcols: fix colors use
* use color for all cell
* always switch back to line color

Signed-off-by: Karel Zak <kzak@redhat.com>
2021-03-12 16:15:16 +01:00
Karel Zak c173ba11ec libsmartcols: add comments to private header file
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-03-10 11:30:53 +01:00
Karel Zak f458782f14 libsmartcols: remove unnecessary code
Fixes: https://github.com/karelzak/util-linux/pull/1243
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-02-08 14:47:38 +01:00
Karel Zak 3c7355dd63 libsmartcols: support arrays for JSON output
This patch add support to format multi-line cells (columns with
SCOLS_FL_WRAP) to arrays in JSON output.

For example mountpoints[] in lsblk output:

Normal output:
	 $ lsblk -oNAME,FSTYPE,TYPE,MOUNTPOINTS /dev/sdc1
	 NAME FSTYPE TYPE MOUNTPOINTS
	 sdc1 btrfs  part /mnt/A
			  /mnt/test
			  /mnt/B

JSON output:
	$ lsblk -J -oNAME,FSTYPE,TYPE,MOUNTPOINTS /dev/sdc1
	{
	   "blockdevices": [
	      {
		 "name": "sdc1",
		 "fstype": "btrfs",
		 "type": "part",
		 "mountpoints": [
		     "/mnt/A",
		     "/mnt/test",
		     "/mnt/B"
		 ]
	      }
	   ]
	}

Signed-off-by: Karel Zak <kzak@redhat.com>
2021-01-08 13:12:57 +01:00
Karel Zak 58b510e580 libsmartcols: sanitize variable names on export output
The shells are very restrictive about variable names, only [:alnum:]
chars are allowed (and alphabetic chars as the first char).  The
library will replace "bad" chars with "_". The char '%' at the end is
replaced by _PCT.

Addresses: https://github.com/karelzak/util-linux/issues/1201
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-12-03 12:14:10 +01:00
Karel Zak 618a6ccbfd libsmartcols: use lib/jsonwrt.c for JSON
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-12 11:21:50 +01:00
Karel Zak f29a2cfbe1 libsmartcols: don't print empty output on empty table in JSON
old version:
 $ findmnt --json --type foo
 $

new version:
 $ findmnt --json --type foo
 {
    "filesystems": [
    ]
 }

Addresses: https://github.com/karelzak/util-linux/issues/1136
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-09-07 11:52:13 +02:00
Karel Zak 1ac70bf5a5 libsmartcols: (docs) fix reference
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-22 11:22:51 +02:00
Karel Zak 40a4845693 libsmartcols: (docs) add missing references
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-22 10:34:14 +02:00
Karel Zak 29e204d117 docs: reword others "allow to"
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-14 14:31:29 +02:00
Karel Zak 045e7efabd libsmartcols: (sample) remove unused variable
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-14 12:32:56 +02:00
Karel Zak 04e6ab9c1b libsmartcols: (sample) remove unnecessary check [coverity scan]
@str cannot be NULL ...

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-14 12:21:22 +02:00
Karel Zak f3a55c1fde libsmartcols: (sample) check scols_line_refer_data() return code [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-14 12:18:01 +02:00
Karel Zak 59ba65df21 libsmartcols: (smaple) check scols_line_set_data() return code [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-14 12:03:11 +02:00
Karel Zak 8aedaf592e libsmartcols: (sample) check scols_line_set_data() return code [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-14 11:36:23 +02:00
Yuri Chornoivan 311e33afef Fix minor typos 2020-07-09 20:14:32 +03:00
Karel Zak 2292d0baf9 libsmartcols: don't calculate with encoding on scols_table_enable_noencoding()
Old:
	$ echo -e "$(tput smul)A$(tput rmul) B\nC D" | column --table
	A  B
	C                 D

Fixed:
	$ echo -e "$(tput smul)A$(tput rmul) B\nC D" | ./column --table
	A  B
	C  D

Addresses: https://github.com/karelzak/util-linux/issues/1011
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-04-22 12:24:33 +02:00
Rosen Penev 042f62dfc5
[clang-tidy] do not use else after return
Found with readability-else-after-return

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-20 13:20:59 -07:00
Sami Kerola f6b6beaf6a
various: fix more lgtm scan warnings
The logger and rtwake time function changes continue the same fixes as
previous commit - use thread safe functions.  The libsmartcols condition
removal is possible because width must be greater than tb->termwidth that is
size_t and cannot be smaller than zero.  And remove couple FIXME's that are
old and unlikely ever to get fixed.

Reference: 3160589d86
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-02-20 20:18:46 +00:00
Karel Zak 24cee7157a docs: update year in libs docs
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-06 12:59:45 +01:00
Sami Kerola 218b1dd6f9
misc: fix typos [codespell]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-09-12 19:41:46 +01:00
Karel Zak 94dbb32273 libsmartcols: cleanup and extend padding functionality
LIBSMARTCOLS_DEBUG_PADDING=on in the next examples forces libsmartcols
print '.' as a padding char. See line "ffff" in the exmaples.

* default output is to fill all except last cell

	$ LIBSMARTCOLS_DEBUG_PADDING=on ./sample-scols-fromfile --nlines 10  --width 80 --column tests/ts/libsmartcols/files/col-name --column tests/ts/libsmartcols/files/col-number --column tests/ts/libsmartcols/files/col-string --column tests/ts/libsmartcols/files/col-string tests/ts/libsmartcols/files/data-string tests/ts/libsmartcols/files/data-number tests/ts/libsmartcols/files/data-string-empty tests/ts/libsmartcols/files/data-string-empty 2> /dev/null
	NAME.. ......NUM STRINGS STRINGS
	aaaa.. ........0 aaaa... aaaa
	bbb... ......100 bbb.... bbb
	ccccc. .......21 ccccc.. ccccc
	dddddd ........3 dddddd. dddddd
	ee.... ......411 ee..... ee
	ffff.. .....5111 .......
	gggggg 678993321 gggggg. gggggg
	hhh... ..7666666 hhh.... hhh

* scols_table_enable_minout() minimizes output for tailing empty cells, example:

	$ LIBSMARTCOLS_DEBUG_PADDING=on ./sample-scols-fromfile --nlines 10 --minout  --width 80 --column tests/ts/libsmartcols/files/col-name --column tests/ts/libsmartcols/files/col-number --column tests/ts/libsmartcols/files/col-string --column tests/ts/libsmartcols/files/col-string tests/ts/libsmartcols/files/data-string tests/ts/libsmartcols/files/data-number tests/ts/libsmartcols/files/data-string-empty tests/ts/libsmartcols/files/data-string-empty 2> /dev/null
	NAME.. ......NUM STRINGS STRINGS
	aaaa.. ........0 aaaa... aaaa
	bbb... ......100 bbb.... bbb
	ccccc. .......21 ccccc.. ccccc
	dddddd ........3 dddddd. dddddd
	ee.... ......411 ee..... ee
	ffff.. .....5111
	gggggg 678993321 gggggg. gggggg
	hhh... ..7666666 hhh.... hhh

* cleanup up scols_table_enable_maxout() use, example:

	$ LIBSMARTCOLS_DEBUG_PADDING=on ./sample-scols-fromfile --nlines 10 --maxout  --width 80 --column tests/ts/libsmartcols/files/col-name --column tests/ts/libsmartcols/files/col-number --column tests/ts/libsmartcols/files/col-string --column tests/ts/libsmartcols/files/col-string tests/ts/libsmartcols/files/data-string tests/ts/libsmartcols/files/data-number tests/ts/libsmartcols/files/data-string-empty tests/ts/libsmartcols/files/data-string-empty 2> /dev/null
	NAME.............. ..................NUM STRINGS............ STRINGS............
	aaaa.............. ....................0 aaaa............... aaaa...............
	bbb............... ..................100 bbb................ bbb................
	ccccc............. ...................21 ccccc.............. ccccc..............
	dddddd............ ....................3 dddddd............. dddddd.............
	ee................ ..................411 ee................. ee.................
	ffff.............. .................5111 ................... ...................
	gggggg............ ............678993321 gggggg............. gggggg.............
	hhh............... ..............7666666 hhh................ hhh................

Note that we cannot make scols_table_enable_minout() default because
for example "column --table" is pretty commonly used with non-blank
columns separator and in this case all cells has to be filled.

	$ echo -e "aa,b,ccc\na,,\naaa,bbb,ccc" |  column --table --separator ',' --output-separator '|'
	aa |b  |ccc
	a  |   |
	aaa|bbb|ccc

Addresses: https://github.com/karelzak/util-linux/issues/826
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-07-23 16:24:42 +02:00
Karel Zak ed16c910be libsmartcols: fix docs
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-13 13:45:46 +02:00
Karel Zak 36ad92c23e libsmartcols: (docs) add reference to v2.33 and v2.34
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-07 12:32:45 +02:00
Karel Zak e2a0f26341 libsmartcols: (groups) improve scols_table_group_lines() args check [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15 17:46:31 +02:00
Karel Zak 47b6eccc97 libsmartcols: use scols_walk_* for calculations and printing
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-07 11:46:11 +02:00
Karel Zak 19c2e73116 libsmartcols: add generic function to walk on tree
Now we implement the same thing on more places. Let's add one set of
functions to walk the tree and use it everywhere.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-07 11:44:24 +02:00
Karel Zak 281a2f3212 libsmartcols: cell width calulation cleanup
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-03 17:27:17 +02:00
Karel Zak ea7fb72eb2 libsmartcols: (groups) use print functions tp calculate grpset
Now we have extra code to calculate grpset. It seems better to use
only one way how we wall the tree.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-03 17:12:18 +02:00
Karel Zak b94932c063 libsmartcols: (groups) print group childrent after regualr tree
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-03 14:53:23 +02:00
Karel Zak 46634fc90d libsmartcols: (groups) improve debug messages
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-03 13:25:09 +02:00
Karel Zak 883246a104 libsmartcols: fix groups reset, add debugs
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-02 15:51:29 +02:00
Karel Zak 4525b6cf16 libsmartcols: (groups) remove hardcoded const numbers
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-02 13:44:12 +02:00
Karel Zak ebb6368990 libsmartcols: print tree also for empty cells
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-02-20 12:47:24 +01:00
Sami Kerola 110260830d docs: fix typos [codespell]
Reference: https://github.com/codespell-project/codespell
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-02-18 13:22:43 +01:00
Karel Zak ebc2397ec4 libsmartcols: use list_add_tail() in more robust way
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-02-18 12:33:12 +01:00
Sami Kerola 5837f08a50 libsmartcols: remove extra ';' outside of a function [-Wextra-semi]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-01-02 14:40:30 +01:00
Sami Kerola 539e96b141
libsmartcols: fix variable shadowing
libsmartcols/src/grouping.c:115:26: warning: declaration of ‘ln’ shadows a
				    previous local [-Wshadow]
libsmartcols/src/grouping.c:108:24: note: shadowed declaration is here

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-12-10 20:31:48 +00:00
Karel Zak 4f4c2b3cdd libsmartcols: add grouping API docs
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-07 12:33:34 +01:00
Karel Zak 309c76e4ee libsmartcols: add grouping samples
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-07 12:33:34 +01:00
Karel Zak d52f5542b0 libsmartcols: add lines grouping support
For some use-case we need to describe M:N relation between output
lines. The nice examples are RAIDs or multi-path devices in lsblk
output.

    NAME                 MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
    loop0                  7:0    0 955.7M  0 loop
┌┈▶ ├─test-thin-metadata 253:0    0     2M  0 dm
└┬▶ └─test-thin-data     253:1    0 953.7M  0 dm
 └┈┈test-thin-pool       253:2    0 953.7M  0 dm

In this example two line (test-thin-metadata and test-thin-data) are
parents for another line (test-thin-pool). The new API uses term "group"
for parental line -- the number of group members is unlimited and every
group has at least one child.

It's possible that group's child is member of another group:

       NAME                 MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
       loop0                  7:0    0 955.7M  0 loop
   ┌┈▶ ├─test-thin-metadata 253:0    0     2M  0 dm
   └┬▶ └─test-thin-data     253:1    0 953.7M  0 dm
┌┈▶ └┈┈test-thin-pool       253:2    0 953.7M  0 dm
┆      └─test-thin          253:3    0 190.8M  0 dm
└┬▶    loop1                  7:1    0 190.8M  0 loop
 └┈┈┈┈┈test-thin-extsnap    253:4    0 190.8M  0 dm

For now multi-group relation is unsupported and one line can be member
of one group only. The library API and printing code is ready to
support this feature, but not sure if we really need it. All what is
necessary is to create array of groups in the line struct.

Note that grouping is independent on standard parent->child relations
between lines and grouping can connect arbitrary lines. The
restriction is only that group child cannot be child of another line
or child of another group. These cross reference are (and probably
will be) impossible.

The patch is relative large, but easy to review. Changes:

* add new UTF symbols
* add scols_symbols_set_group_* public API to modify new symbols
* add struct libscols_group, used only internally

* add "grpset" array to table struct -- the array is used to keep
position of the group in the output. Every active group uses three
items in the grpset. If there is more overlapping groups than bigger
grpset is allocated.

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-07 12:33:34 +01:00
Karel Zak 3bb882be44 libsmartcols: split print.c into print.c, put.c and print-api.c
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-07 12:33:34 +01:00
Karel Zak 06a8decd72 libsmartcols: move width calculation to separate file
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-07 12:33:34 +01:00