1 /*
2  * smartcolsP.h - private library header file
3  *
4  * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
5  * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
6  *
7  * This file may be redistributed under the terms of the
8  * GNU Lesser General Public License.
9  */
10 
11 #ifndef _LIBSMARTCOLS_PRIVATE_H
12 #define _LIBSMARTCOLS_PRIVATE_H
13 
14 #include "c.h"
15 #include "list.h"
16 #include "colors.h"
17 #include "debug.h"
18 
19 #include "libsmartcols.h"
20 
21 /* features */
22 #define CONFIG_LIBSMARTCOLS_ASSERT
23 
24 #ifdef CONFIG_LIBSMARTCOLS_ASSERT
25 # include <assert.h>
26 #else
27 # define assert(x)
28 #endif
29 
30 /*
31  * Debug
32  */
33 #define SCOLS_DEBUG_INIT	(1 << 1)
34 #define SCOLS_DEBUG_CELL	(1 << 2)
35 #define SCOLS_DEBUG_LINE	(1 << 3)
36 #define SCOLS_DEBUG_TAB		(1 << 4)
37 #define SCOLS_DEBUG_COL		(1 << 5)
38 #define SCOLS_DEBUG_BUFF	(1 << 6)
39 #define SCOLS_DEBUG_ALL		0xFFFF
40 
41 UL_DEBUG_DECLARE_MASK(libsmartcols);
42 #define DBG(m, x)	__UL_DBG(libsmartcols, SCOLS_DEBUG_, m, x)
43 #define ON_DBG(m, x)	__UL_DBG_CALL(libsmartcols, SCOLS_DEBUG_, m, x)
44 #define DBG_FLUSH	__UL_DBG_FLUSH(libsmartcols, SCOLS_DEBUG_)
45 
46 /*
47  * Generic iterator
48  */
49 struct libscols_iter {
50 	struct list_head        *p;		/* current position */
51 	struct list_head        *head;		/* start position */
52 	int			direction;	/* SCOLS_ITER_{FOR,BACK}WARD */
53 };
54 
55 /*
56  * Tree symbols
57  */
58 struct libscols_symbols {
59 	int	refcount;
60 	char	*branch;
61 	char	*vert;
62 	char	*right;
63 };
64 
65 /*
66  * Table cells
67  */
68 struct libscols_cell {
69 	char	*data;
70 	char	*color;
71 	void    *userdata;
72 };
73 
74 
75 /*
76  * Table column
77  */
78 struct libscols_column {
79 	int	refcount;	/* reference counter */
80 	size_t	seqnum;		/* column index */
81 
82 	size_t	width;		/* real column width */
83 	size_t	width_min;	/* minimal width (usually header width) */
84 	size_t  width_max;	/* maximal width */
85 	size_t  width_avg;	/* average width, used to detect extreme fields */
86 	double	width_hint;	/* hint (N < 1 is in percent of termwidth) */
87 
88 	int	flags;
89 	int	is_extreme;
90 	char	*color;		/* default column color */
91 
92 	int (*cmpfunc)(struct libscols_cell *,
93 		       struct libscols_cell *,
94 		       void *);			/* cells comparison function */
95 	void *cmpfunc_data;
96 
97 	struct libscols_cell	header;
98 	struct list_head	cl_columns;
99 };
100 
101 /*
102  * Table line
103  */
104 struct libscols_line {
105 	int	refcount;
106 	size_t	seqnum;
107 
108 	void	*userdata;
109 	char	*color;		/* default line color */
110 
111 	struct libscols_cell	*cells;		/* array with data */
112 	size_t			ncells;		/* number of cells */
113 
114 	struct list_head	ln_lines;	/* table lines */
115 	struct list_head	ln_branch;	/* begin of branch (head of ln_children) */
116 	struct list_head	ln_children;
117 
118 	struct libscols_line	*parent;
119 };
120 
121 enum {
122 	SCOLS_FMT_HUMAN = 0,		/* default, human readable */
123 	SCOLS_FMT_RAW,			/* space separated */
124 	SCOLS_FMT_EXPORT		/* COLNAME="data" ... */
125 };
126 
127 /*
128  * The table
129  */
130 struct libscols_table {
131 	int	refcount;
132 	size_t	ncols;		/* number of columns */
133 	size_t  ntreecols;	/* number of columns with SCOLS_FL_TREE */
134 	size_t	nlines;		/* number of lines */
135 	size_t	termwidth;	/* terminal width */
136 	size_t  termreduce;	/* extra blank space */
137 	FILE	*out;		/* output stream */
138 
139 	char	*colsep;	/* column separator */
140 	char	*linesep;	/* line separator */
141 
142 	struct list_head	tb_columns;
143 	struct list_head	tb_lines;
144 	struct libscols_symbols	*symbols;
145 
146 	int	format;		/* SCOLS_FMT_* */
147 
148 	/* flags */
149 	unsigned int	ascii		:1,	/* don't use unicode */
150 			colors_wanted	:1,	/* enable colors */
151 			is_term		:1,	/* isatty() */
152 			maxout		:1,	/* maximalize output */
153 			no_headings	:1;	/* don't print header */
154 };
155 
156 #define IS_ITER_FORWARD(_i)	((_i)->direction == SCOLS_ITER_FORWARD)
157 #define IS_ITER_BACKWARD(_i)	((_i)->direction == SCOLS_ITER_BACKWARD)
158 
159 #define SCOLS_ITER_INIT(itr, list) \
160 	do { \
161 		(itr)->p = IS_ITER_FORWARD(itr) ? \
162 				(list)->next : (list)->prev; \
163 		(itr)->head = (list); \
164 	} while(0)
165 
166 #define SCOLS_ITER_ITERATE(itr, res, restype, member) \
167 	do { \
168 		res = list_entry((itr)->p, restype, member); \
169 		(itr)->p = IS_ITER_FORWARD(itr) ? \
170 				(itr)->p->next : (itr)->p->prev; \
171 	} while(0)
172 
173 #endif /* _LIBSMARTCOLS_PRIVATE_H */
174