1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
|
/**
* util - Utility function library
*
* Print records in different output formats
*
* Copyright IBM Corp. 2016, 2017
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <argz.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib/util_base.h"
#include "lib/util_libc.h"
#include "lib/util_list.h"
#include "lib/util_panic.h"
#include "lib/util_prg.h"
#include "lib/util_rec.h"
/*
* Field structure containing the string value and secondary information
*/
struct util_rec_fld {
char *key; /* Name of the filed */
char *hdr; /* Content of the header */
size_t len; /* Length of string argz array */
char *val; /* The value of the field */
enum util_rec_align align; /* Alignment of the field */
int width; /* Field width */
struct util_list_node node; /* Pointers to previous and next field */
};
/*
* Print formats
*/
struct rec_fmt {
enum {
REC_FMT_WIDE,
REC_FMT_LONG,
REC_FMT_CSV,
} type;
union {
struct wide_p {
char *hdr_sep;
char *col_sep;
int argz_sep;
} wide_p;
struct long_p {
char *hdr_sep;
char *col_sep;
int argz_sep;
char *key;
int key_size;
int val_size;
} long_p;
struct csv_p {
char *col_sep;
int argz_sep;
} csv_p;
} d;
int indent;
};
/*
* Record structure (internal representation)
*/
/// @cond
struct util_rec {
struct util_list *list; /* List of the fields */
struct rec_fmt fmt; /* Output format */
};
/// @endcond
struct util_list *__util_rec_get_list(struct util_rec *rec)
{
return rec->list;
}
/*
* Get the field according to a distinct key
*/
static struct util_rec_fld *rec_get_fld(struct util_rec *rec, const char *key)
{
struct util_rec_fld *fld;
util_list_iterate(rec->list, fld) {
if (!strcmp(fld->key, key))
return fld;
}
return NULL;
}
/**
* Return the key name of a field
*
* @param[in] fld Field for query
*
* @returns Pointer to key string
*/
const char *util_rec_fld_get_key(struct util_rec_fld *fld)
{
return fld->key;
}
/**
* Create a new record with "wide" output format
*
* @param[in] hdr_sep Header separator
*
* @returns Pointer to the created record
*/
struct util_rec *util_rec_new_wide(const char *hdr_sep)
{
struct util_rec *rec = util_malloc(sizeof(struct util_rec));
rec->list = util_list_new(struct util_rec_fld, node);
rec->fmt.type = REC_FMT_WIDE;
rec->fmt.d.wide_p.hdr_sep = util_strdup(hdr_sep);
rec->fmt.d.wide_p.argz_sep = ',';
rec->fmt.indent = 0;
return rec;
}
/*
* Print the indentation characters
*/
static inline void rec_print_indention(int indent)
{
if (indent <= 0)
return;
printf("%*s", indent, "");
}
/*
* Print record separator in "wide" output format
*/
static void rec_print_wide_separator(struct util_rec *rec)
{
const char *hdr_sep = rec->fmt.d.wide_p.hdr_sep;
int size = 0, field_count = 0;
struct util_rec_fld *fld;
char *buf;
if (!hdr_sep)
return;
util_list_iterate(rec->list, fld) {
if (fld->hdr) {
size += fld->width;
field_count++;
}
}
size += field_count - 1;
buf = util_malloc(size + 1);
memset(buf, (int)hdr_sep[0], size);
buf[size] = 0;
rec_print_indention(rec->fmt.indent);
printf("%s\n", buf);
free(buf);
}
/*
* Print record header in "wide" output format
*/
static void rec_print_wide_hdr(struct util_rec *rec)
{
const char *hdr_sep = rec->fmt.d.wide_p.hdr_sep;
int col_nr = 0, size = 0, field_count = 0;
struct util_rec_fld *fld;
char *buf;
rec_print_indention(rec->fmt.indent);
util_list_iterate(rec->list, fld) {
if (col_nr)
printf(" ");
if (fld->hdr) {
if (fld->align == UTIL_REC_ALIGN_LEFT)
printf("%-*s", fld->width, fld->hdr);
else
printf("%*s", fld->width, fld->hdr);
size += fld->width;
field_count++;
}
col_nr++;
}
printf("\n");
if (!hdr_sep)
return;
size += field_count - 1;
if (hdr_sep) {
buf = util_malloc(size + 1);
memset(buf, (int)hdr_sep[0], size);
buf[size] = 0;
rec_print_indention(rec->fmt.indent);
printf("%s\n", buf);
free(buf);
}
}
/*
* Print record field values in "wide" output format
*/
void rec_print_wide(struct util_rec *rec)
{
const char argz_sep = rec->fmt.d.wide_p.argz_sep;
struct util_rec_fld *fld;
char argz_str[PAGE_SIZE];
int fld_count = 0;
char *entry;
rec_print_indention(rec->fmt.indent);
util_list_iterate(rec->list, fld) {
if (!fld->hdr)
continue;
if (fld_count)
printf(" ");
entry = fld->val;
if (argz_count(fld->val, fld->len) > 1) {
strcpy(argz_str, entry);
while ((entry = argz_next(fld->val, fld->len, entry)))
strcat(strncat(argz_str, &argz_sep, 1), entry);
entry = argz_str;
}
if (fld->align == UTIL_REC_ALIGN_LEFT)
printf("%-*s", fld->width, entry);
else
printf("%*s", fld->width, entry);
fld_count++;
}
printf("\n");
}
/*
* Free private memory of record
*/
static void rec_free_wide(struct util_rec *rec)
{
free(rec->fmt.d.wide_p.hdr_sep);
}
/**
* Create a new record with "long" output format
*
* @param[in] hdr_sep Header separator
* @param[in] col_sep Column separator
* @param[in] key Primary key of record
* @param[in] key_size Width of left column i.e. keys
* @param[in] val_size Width of right column i.e. values
*
* @returns Pointer to the created record
*/
struct util_rec *util_rec_new_long(const char *hdr_sep, const char *col_sep,
const char *key, int key_size, int val_size)
{
struct util_rec *rec = util_malloc(sizeof(struct util_rec));
rec->list = util_list_new(struct util_rec_fld, node);
rec->fmt.type = REC_FMT_LONG;
rec->fmt.d.long_p.hdr_sep = util_strdup(hdr_sep);
rec->fmt.d.long_p.col_sep = util_strdup(col_sep);
rec->fmt.d.long_p.key = util_strdup(key);
rec->fmt.d.long_p.key_size = key_size;
rec->fmt.d.long_p.val_size = val_size;
rec->fmt.d.long_p.argz_sep = ' ';
rec->fmt.indent = 0;
return rec;
}
/*
* Print field header in "long" output format
*/
static void rec_print_long_hdr(struct util_rec *rec)
{
struct long_p *p = &rec->fmt.d.long_p;
struct util_rec_fld *fld;
int len = 0;
char *buf;
fld = rec_get_fld(rec, p->key);
util_assert(fld != NULL, "Record not found\n");
util_assert(fld->hdr != NULL, "Header for field not found\n");
rec_print_indention(rec->fmt.indent);
if (p->col_sep) {
printf("%-*s %s %-*s\n", p->key_size, fld->hdr,
p->col_sep, fld->width, fld->val);
len = p->key_size + p->val_size + 3;
} else {
printf("%-*s %-*s\n", p->key_size, fld->hdr, fld->width,
fld->val);
len = p->key_size + p->val_size + 1;
}
if (!p->hdr_sep)
return;
buf = util_malloc(len + 1);
memset(buf, p->hdr_sep[0], len);
buf[len] = 0;
rec_print_indention(rec->fmt.indent);
printf("%s\n", buf);
free(buf);
}
/*
* Print record field values in "long" output format
*/
static void rec_print_long(struct util_rec *rec)
{
struct long_p *p = &rec->fmt.d.long_p;
struct util_rec_fld *fld;
char *item = NULL;
rec_print_long_hdr(rec);
util_list_iterate(rec->list, fld) {
if (!fld->hdr)
continue;
if (!strcmp(p->key, fld->key))
continue;
if (!fld->val)
continue;
rec_print_indention(rec->fmt.indent);
item = argz_next(fld->val, fld->len, item);
if (p->col_sep) {
printf(" %-*s %s %s\n",
p->key_size - 8, fld->hdr, p->col_sep, item);
while ((item = argz_next(fld->val, fld->len, item))) {
rec_print_indention(rec->fmt.indent);
printf(" %-*s %c %s\n",
p->key_size - 8, "", p->argz_sep, item);
}
} else {
printf(" %-*s %s\n",
p->key_size - 8, fld->hdr, fld->val);
while ((item = argz_next(fld->val, fld->len, item))) {
rec_print_indention(rec->fmt.indent);
printf(" %-*s %s\n",
p->key_size - 8, "", item);
}
}
}
printf("\n");
}
/*
* Free private memory of record
*/
static void rec_free_long(struct util_rec *rec)
{
free(rec->fmt.d.long_p.hdr_sep);
free(rec->fmt.d.long_p.col_sep);
free(rec->fmt.d.long_p.key);
}
/**
* Create a new record with "csv" output format
*
* @param[in] col_sep Column separator
*
* @returns Pointer to the created record
*/
struct util_rec *util_rec_new_csv(const char *col_sep)
{
struct util_rec *rec = util_malloc(sizeof(struct util_rec));
rec->list = util_list_new(struct util_rec_fld, node);
rec->fmt.type = REC_FMT_CSV;
rec->fmt.d.csv_p.col_sep = util_strdup(col_sep);
rec->fmt.d.csv_p.argz_sep = ' ';
rec->fmt.indent = 0;
return rec;
}
/*
* Print record header in "csv" output format
*/
void rec_print_csv_hdr(struct util_rec *rec)
{
const char *col_sep = rec->fmt.d.csv_p.col_sep;
struct util_rec_fld *fld;
int fld_count = 0;
rec_print_indention(rec->fmt.indent);
util_list_iterate(rec->list, fld) {
if (fld_count)
printf("%c", *col_sep);
if (fld->hdr) {
printf("%s", fld->hdr);
fld_count++;
}
}
printf("\n");
}
/*
* Print record field values in "csv" output format
*/
void rec_print_csv(struct util_rec *rec)
{
const char argz_sep = rec->fmt.d.csv_p.argz_sep;
const char *col_sep = rec->fmt.d.csv_p.col_sep;
struct util_rec_fld *fld;
int fld_count = 0;
char *item = NULL;
rec_print_indention(rec->fmt.indent);
util_list_iterate(rec->list, fld) {
item = argz_next(fld->val, fld->len, item);
if (fld_count)
printf("%c", *col_sep);
if (fld->hdr) {
printf("%s", item);
while ((item = argz_next(fld->val, fld->len, item)))
printf("%c%s", argz_sep, item);
fld_count++;
}
}
printf("\n");
}
/*
* Free private memory of record
*/
static void rec_free_csv(struct util_rec *rec)
{
free(rec->fmt.d.csv_p.col_sep);
}
/**
* Define a new field for the record
*
* @param[in] rec Pointer of the record
* @param[in] key Key of the filed is to be created. It should be unique
* within the record.
* @param[in] align Alignment of field
* @param[in] width Width of field
* @param[in] hdr This information is printed in record headers. If it is
* NULL, the field is prohibited form printing completely.
*/
void util_rec_def(struct util_rec *rec, const char *key,
enum util_rec_align align, int width, const char *hdr)
{
struct util_rec_fld *fld = util_malloc(sizeof(struct util_rec_fld));
fld->key = util_strdup(key);
fld->hdr = util_strdup(hdr);
fld->val = NULL;
fld->align = align;
fld->width = width;
util_list_add_tail(rec->list, fld);
}
/**
* Free record and associated fields
*
* @param[in] rec Record pointer
*/
void util_rec_free(struct util_rec *rec)
{
struct util_rec_fld *fld, *tmp;
util_list_iterate_safe(rec->list, fld, tmp) {
util_list_remove(rec->list, fld);
free(fld->key);
free(fld->hdr);
free(fld->val);
free(fld);
}
util_list_free(rec->list);
switch (rec->fmt.type) {
case REC_FMT_WIDE:
rec_free_wide(rec);
break;
case REC_FMT_LONG:
rec_free_long(rec);
break;
case REC_FMT_CSV:
rec_free_csv(rec);
break;
}
free(rec);
}
/**
* Print record field values according to output format
*
* @param[in] rec Record pointer
*/
void util_rec_print(struct util_rec *rec)
{
switch (rec->fmt.type) {
case REC_FMT_WIDE:
rec_print_wide(rec);
break;
case REC_FMT_LONG:
rec_print_long(rec);
break;
case REC_FMT_CSV:
rec_print_csv(rec);
break;
}
}
/**
* Print record header according to output format
*
* @param[in] rec Record pointer
*/
void util_rec_print_hdr(struct util_rec *rec)
{
switch (rec->fmt.type) {
case REC_FMT_WIDE:
rec_print_wide_hdr(rec);
break;
case REC_FMT_LONG:
break;
case REC_FMT_CSV:
rec_print_csv_hdr(rec);
break;
}
}
/**
* Print record separator according to output format
*
* @param[in] rec Record pointer
*/
void util_rec_print_separator(struct util_rec *rec)
{
switch (rec->fmt.type) {
case REC_FMT_WIDE:
rec_print_wide_separator(rec);
break;
case REC_FMT_LONG:
break;
case REC_FMT_CSV:
break;
}
}
/**
* Set a field value to an argz vector
*
* @param[in] rec Record pointer
* @param[in] key Key of the desired field
* @param[in] argz Pointer to the series of strings
* @param[in] len Length of the argz buffer
*/
void util_rec_set_argz(struct util_rec *rec, const char *key, const char *argz,
size_t len)
{
struct util_rec_fld *fld;
char *val;
fld = rec_get_fld(rec, key);
if (!fld)
return;
val = util_malloc(len);
val = memcpy(val, argz, len);
free(fld->val);
fld->val = val;
fld->len = len;
}
/**
* Set a field value to a formatted string
*
* @param[in] rec Record pointer
* @param[in] key Key of the desired field
* @param[in] fmt Format string for generation of value string
* @param[in] ... Parameters for format string
*
* @returns Pointer to the field which was modified or NULL in the case of
* any error.
*/
void util_rec_set(struct util_rec *rec, const char *key, const char *fmt, ...)
{
struct util_rec_fld *fld;
va_list ap;
char *str;
util_assert(fmt != NULL, "Parameter 'fmt' pointer must not be NULL\n");
fld = rec_get_fld(rec, key);
if (!fld)
return;
UTIL_VASPRINTF(&str, fmt, ap);
free(fld->val);
fld->val = str;
fld->len = strlen(str) + 1;
}
/**
* Return the string value of a desired field. If the field value stored in argz
* format, pointer to the first argz element is returned.
*
* @param[in] rec Record pointer
* @param[in] key Key of the field
*
* @returns If the desired field was found, the pointer to its value.
* NULL in the case of any error or if the field is empty.
*/
const char *util_rec_get(struct util_rec *rec, const char *key)
{
struct util_rec_fld *fld = rec_get_fld(rec, key);
return (fld != NULL) ? fld->val : NULL;
}
/**
* Sets the indentation of the record
*
* @param[in] rec Record pointer
* @param[in] indent Number of characters to indent
*/
void util_rec_set_indent(struct util_rec *rec, int indent)
{
rec->fmt.indent = indent;
}
|