/crypto/heimdal/lib/roken/rtbl.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 487 lines · 383 code · 68 blank · 36 comment · 98 complexity · 2bc4b4d2aa2c4723a6c7d384f8005451 MD5 · raw file

  1. /*
  2. * Copyright (c) 2000, 2002, 2004 Kungliga Tekniska Hรถgskolan
  3. * (Royal Institute of Technology, Stockholm, Sweden).
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * 3. Neither the name of the Institute nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include "roken.h"
  35. #include "rtbl.h"
  36. struct column_entry {
  37. char *data;
  38. };
  39. struct column_data {
  40. char *header;
  41. char *prefix;
  42. int width;
  43. unsigned flags;
  44. size_t num_rows;
  45. struct column_entry *rows;
  46. unsigned int column_id;
  47. char *suffix;
  48. };
  49. struct rtbl_data {
  50. char *column_prefix;
  51. size_t num_columns;
  52. struct column_data **columns;
  53. unsigned int flags;
  54. char *column_separator;
  55. };
  56. ROKEN_LIB_FUNCTION rtbl_t ROKEN_LIB_CALL
  57. rtbl_create (void)
  58. {
  59. return calloc (1, sizeof (struct rtbl_data));
  60. }
  61. ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
  62. rtbl_set_flags (rtbl_t table, unsigned int flags)
  63. {
  64. table->flags = flags;
  65. }
  66. ROKEN_LIB_FUNCTION unsigned int ROKEN_LIB_CALL
  67. rtbl_get_flags (rtbl_t table)
  68. {
  69. return table->flags;
  70. }
  71. static struct column_data *
  72. rtbl_get_column_by_id (rtbl_t table, unsigned int id)
  73. {
  74. size_t i;
  75. for(i = 0; i < table->num_columns; i++)
  76. if(table->columns[i]->column_id == id)
  77. return table->columns[i];
  78. return NULL;
  79. }
  80. static struct column_data *
  81. rtbl_get_column (rtbl_t table, const char *column)
  82. {
  83. size_t i;
  84. for(i = 0; i < table->num_columns; i++)
  85. if(strcmp(table->columns[i]->header, column) == 0)
  86. return table->columns[i];
  87. return NULL;
  88. }
  89. ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
  90. rtbl_destroy (rtbl_t table)
  91. {
  92. size_t i, j;
  93. for (i = 0; i < table->num_columns; i++) {
  94. struct column_data *c = table->columns[i];
  95. for (j = 0; j < c->num_rows; j++)
  96. free (c->rows[j].data);
  97. free (c->rows);
  98. free (c->header);
  99. free (c->prefix);
  100. free (c->suffix);
  101. free (c);
  102. }
  103. free (table->column_prefix);
  104. free (table->column_separator);
  105. free (table->columns);
  106. free (table);
  107. }
  108. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  109. rtbl_add_column_by_id (rtbl_t table, unsigned int id,
  110. const char *header, unsigned int flags)
  111. {
  112. struct column_data *col, **tmp;
  113. tmp = realloc (table->columns, (table->num_columns + 1) * sizeof (*tmp));
  114. if (tmp == NULL)
  115. return ENOMEM;
  116. table->columns = tmp;
  117. col = malloc (sizeof (*col));
  118. if (col == NULL)
  119. return ENOMEM;
  120. col->header = strdup (header);
  121. if (col->header == NULL) {
  122. free (col);
  123. return ENOMEM;
  124. }
  125. col->prefix = NULL;
  126. col->width = 0;
  127. col->flags = flags;
  128. col->num_rows = 0;
  129. col->rows = NULL;
  130. col->column_id = id;
  131. col->suffix = NULL;
  132. table->columns[table->num_columns++] = col;
  133. return 0;
  134. }
  135. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  136. rtbl_add_column (rtbl_t table, const char *header, unsigned int flags)
  137. {
  138. return rtbl_add_column_by_id(table, 0, header, flags);
  139. }
  140. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  141. rtbl_new_row(rtbl_t table)
  142. {
  143. size_t max_rows = 0;
  144. size_t c;
  145. for (c = 0; c < table->num_columns; c++)
  146. if(table->columns[c]->num_rows > max_rows)
  147. max_rows = table->columns[c]->num_rows;
  148. for (c = 0; c < table->num_columns; c++) {
  149. struct column_entry *tmp;
  150. if(table->columns[c]->num_rows == max_rows)
  151. continue;
  152. tmp = realloc(table->columns[c]->rows,
  153. max_rows * sizeof(table->columns[c]->rows));
  154. if(tmp == NULL)
  155. return ENOMEM;
  156. table->columns[c]->rows = tmp;
  157. while(table->columns[c]->num_rows < max_rows) {
  158. if((tmp[table->columns[c]->num_rows++].data = strdup("")) == NULL)
  159. return ENOMEM;
  160. }
  161. }
  162. return 0;
  163. }
  164. static void
  165. column_compute_width (rtbl_t table, struct column_data *column)
  166. {
  167. size_t i;
  168. if(table->flags & RTBL_HEADER_STYLE_NONE)
  169. column->width = 0;
  170. else
  171. column->width = strlen (column->header);
  172. for (i = 0; i < column->num_rows; i++)
  173. column->width = max (column->width, (int) strlen (column->rows[i].data));
  174. }
  175. /* DEPRECATED */
  176. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  177. rtbl_set_prefix (rtbl_t table, const char *prefix)
  178. {
  179. if (table->column_prefix)
  180. free (table->column_prefix);
  181. table->column_prefix = strdup (prefix);
  182. if (table->column_prefix == NULL)
  183. return ENOMEM;
  184. return 0;
  185. }
  186. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  187. rtbl_set_separator (rtbl_t table, const char *separator)
  188. {
  189. if (table->column_separator)
  190. free (table->column_separator);
  191. table->column_separator = strdup (separator);
  192. if (table->column_separator == NULL)
  193. return ENOMEM;
  194. return 0;
  195. }
  196. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  197. rtbl_set_column_prefix (rtbl_t table, const char *column,
  198. const char *prefix)
  199. {
  200. struct column_data *c = rtbl_get_column (table, column);
  201. if (c == NULL)
  202. return -1;
  203. if (c->prefix)
  204. free (c->prefix);
  205. c->prefix = strdup (prefix);
  206. if (c->prefix == NULL)
  207. return ENOMEM;
  208. return 0;
  209. }
  210. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  211. rtbl_set_column_affix_by_id(rtbl_t table, unsigned int id,
  212. const char *prefix, const char *suffix)
  213. {
  214. struct column_data *c = rtbl_get_column_by_id (table, id);
  215. if (c == NULL)
  216. return -1;
  217. if (c->prefix)
  218. free (c->prefix);
  219. if(prefix == NULL)
  220. c->prefix = NULL;
  221. else {
  222. c->prefix = strdup (prefix);
  223. if (c->prefix == NULL)
  224. return ENOMEM;
  225. }
  226. if (c->suffix)
  227. free (c->suffix);
  228. if(suffix == NULL)
  229. c->suffix = NULL;
  230. else {
  231. c->suffix = strdup (suffix);
  232. if (c->suffix == NULL)
  233. return ENOMEM;
  234. }
  235. return 0;
  236. }
  237. static const char *
  238. get_column_prefix (rtbl_t table, struct column_data *c)
  239. {
  240. if (c == NULL)
  241. return "";
  242. if (c->prefix)
  243. return c->prefix;
  244. if (table->column_prefix)
  245. return table->column_prefix;
  246. return "";
  247. }
  248. static const char *
  249. get_column_suffix (rtbl_t table, struct column_data *c)
  250. {
  251. if (c && c->suffix)
  252. return c->suffix;
  253. return "";
  254. }
  255. static int
  256. add_column_entry (struct column_data *c, const char *data)
  257. {
  258. struct column_entry row, *tmp;
  259. row.data = strdup (data);
  260. if (row.data == NULL)
  261. return ENOMEM;
  262. tmp = realloc (c->rows, (c->num_rows + 1) * sizeof (*tmp));
  263. if (tmp == NULL) {
  264. free (row.data);
  265. return ENOMEM;
  266. }
  267. c->rows = tmp;
  268. c->rows[c->num_rows++] = row;
  269. return 0;
  270. }
  271. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  272. rtbl_add_column_entry_by_id (rtbl_t table, unsigned int id, const char *data)
  273. {
  274. struct column_data *c = rtbl_get_column_by_id (table, id);
  275. if (c == NULL)
  276. return -1;
  277. return add_column_entry(c, data);
  278. }
  279. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  280. rtbl_add_column_entryv_by_id (rtbl_t table, unsigned int id,
  281. const char *fmt, ...)
  282. {
  283. va_list ap;
  284. char *str;
  285. int ret;
  286. va_start(ap, fmt);
  287. ret = vasprintf(&str, fmt, ap);
  288. va_end(ap);
  289. if (ret == -1)
  290. return -1;
  291. ret = rtbl_add_column_entry_by_id(table, id, str);
  292. free(str);
  293. return ret;
  294. }
  295. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  296. rtbl_add_column_entry (rtbl_t table, const char *column, const char *data)
  297. {
  298. struct column_data *c = rtbl_get_column (table, column);
  299. if (c == NULL)
  300. return -1;
  301. return add_column_entry(c, data);
  302. }
  303. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  304. rtbl_add_column_entryv (rtbl_t table, const char *column, const char *fmt, ...)
  305. {
  306. va_list ap;
  307. char *str;
  308. int ret;
  309. va_start(ap, fmt);
  310. ret = vasprintf(&str, fmt, ap);
  311. va_end(ap);
  312. if (ret == -1)
  313. return -1;
  314. ret = rtbl_add_column_entry(table, column, str);
  315. free(str);
  316. return ret;
  317. }
  318. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  319. rtbl_format (rtbl_t table, FILE * f)
  320. {
  321. size_t i, j;
  322. for (i = 0; i < table->num_columns; i++)
  323. column_compute_width (table, table->columns[i]);
  324. if((table->flags & RTBL_HEADER_STYLE_NONE) == 0) {
  325. for (i = 0; i < table->num_columns; i++) {
  326. struct column_data *c = table->columns[i];
  327. if(table->column_separator != NULL && i > 0)
  328. fprintf (f, "%s", table->column_separator);
  329. fprintf (f, "%s", get_column_prefix (table, c));
  330. if(i == table->num_columns - 1 && c->suffix == NULL)
  331. /* last column, so no need to pad with spaces */
  332. fprintf (f, "%-*s", 0, c->header);
  333. else
  334. fprintf (f, "%-*s", (int)c->width, c->header);
  335. fprintf (f, "%s", get_column_suffix (table, c));
  336. }
  337. fprintf (f, "\n");
  338. }
  339. for (j = 0;; j++) {
  340. int flag = 0;
  341. /* are there any more rows left? */
  342. for (i = 0; flag == 0 && i < table->num_columns; ++i) {
  343. struct column_data *c = table->columns[i];
  344. if (c->num_rows > j) {
  345. ++flag;
  346. break;
  347. }
  348. }
  349. if (flag == 0)
  350. break;
  351. for (i = 0; i < table->num_columns; i++) {
  352. int w;
  353. struct column_data *c = table->columns[i];
  354. if(table->column_separator != NULL && i > 0)
  355. fprintf (f, "%s", table->column_separator);
  356. w = c->width;
  357. if ((c->flags & RTBL_ALIGN_RIGHT) == 0) {
  358. if(i == table->num_columns - 1 && c->suffix == NULL)
  359. /* last column, so no need to pad with spaces */
  360. w = 0;
  361. else
  362. w = -w;
  363. }
  364. fprintf (f, "%s", get_column_prefix (table, c));
  365. if (c->num_rows <= j)
  366. fprintf (f, "%*s", w, "");
  367. else
  368. fprintf (f, "%*s", w, c->rows[j].data);
  369. fprintf (f, "%s", get_column_suffix (table, c));
  370. }
  371. fprintf (f, "\n");
  372. }
  373. return 0;
  374. }
  375. #ifdef TEST
  376. int
  377. main (int argc, char **argv)
  378. {
  379. rtbl_t table;
  380. table = rtbl_create ();
  381. rtbl_add_column_by_id (table, 0, "Issued", 0);
  382. rtbl_add_column_by_id (table, 1, "Expires", 0);
  383. rtbl_add_column_by_id (table, 2, "Foo", RTBL_ALIGN_RIGHT);
  384. rtbl_add_column_by_id (table, 3, "Principal", 0);
  385. rtbl_add_column_entry_by_id (table, 0, "Jul 7 21:19:29");
  386. rtbl_add_column_entry_by_id (table, 1, "Jul 8 07:19:29");
  387. rtbl_add_column_entry_by_id (table, 2, "73");
  388. rtbl_add_column_entry_by_id (table, 2, "0");
  389. rtbl_add_column_entry_by_id (table, 2, "-2000");
  390. rtbl_add_column_entry_by_id (table, 3, "krbtgt/NADA.KTH.SE@NADA.KTH.SE");
  391. rtbl_add_column_entry_by_id (table, 0, "Jul 7 21:19:29");
  392. rtbl_add_column_entry_by_id (table, 1, "Jul 8 07:19:29");
  393. rtbl_add_column_entry_by_id (table, 3, "afs/pdc.kth.se@NADA.KTH.SE");
  394. rtbl_add_column_entry_by_id (table, 0, "Jul 7 21:19:29");
  395. rtbl_add_column_entry_by_id (table, 1, "Jul 8 07:19:29");
  396. rtbl_add_column_entry_by_id (table, 3, "afs@NADA.KTH.SE");
  397. rtbl_set_separator (table, " ");
  398. rtbl_format (table, stdout);
  399. rtbl_destroy (table);
  400. printf("\n");
  401. table = rtbl_create ();
  402. rtbl_add_column_by_id (table, 0, "Column A", 0);
  403. rtbl_set_column_affix_by_id (table, 0, "<", ">");
  404. rtbl_add_column_by_id (table, 1, "Column B", 0);
  405. rtbl_set_column_affix_by_id (table, 1, "[", "]");
  406. rtbl_add_column_by_id (table, 2, "Column C", 0);
  407. rtbl_set_column_affix_by_id (table, 2, "(", ")");
  408. rtbl_add_column_entry_by_id (table, 0, "1");
  409. rtbl_new_row(table);
  410. rtbl_add_column_entry_by_id (table, 1, "2");
  411. rtbl_new_row(table);
  412. rtbl_add_column_entry_by_id (table, 2, "3");
  413. rtbl_new_row(table);
  414. rtbl_set_separator (table, " ");
  415. rtbl_format (table, stdout);
  416. rtbl_destroy (table);
  417. return 0;
  418. }
  419. #endif