void block_update(struct block *block) { struct properties *props = &block->updated_props; char buf[2048] = { 0 }; int nr; /* Read a single line for persistent block, everything otherwise */ if (block->interval == INTER_PERSIST) { nr = io_readline(block->out, buf, sizeof(buf)); if (nr < 0) { berror(block, "failed to read a line"); return mark_as_failed(block, "failed to read"); } else if (nr == 0) { berror(block, "pipe closed"); return mark_as_failed(block, "pipe closed"); } } else { /* Note: read(2) returns 0 for end-of-pipe */ if (read(block->out, buf, sizeof(buf) - 1) == -1) { berrorx(block, "read stdout"); return mark_as_failed(block, strerror(errno)); } } /* Reset the defaults and merge the output */ memcpy(props, &block->default_props, sizeof(struct properties)); if (block->format == FORMAT_JSON) block_update_json(block, buf); else block_update_plain_text(block, buf); if (*FULL_TEXT(block) && *LABEL(block)) { static const size_t size = sizeof(props->full_text); char concat[size]; snprintf(concat, size, "%s %s", LABEL(block), FULL_TEXT(block)); strcpy(props->full_text, concat); } bdebug(block, "updated successfully"); }
void json_print_bar(struct bar *bar) { fprintf(stdout, ",[{\"full_text\":\"\"}"); for (int i = 0; i < bar->num; ++i) { struct block *block = bar->blocks + i; /* full_text is the only mandatory key, skip if empty */ if (!*FULL_TEXT(block)) { bdebug(block, "no text to display, skipping"); continue; } print_block(block); } fprintf(stdout, "]\n"); fflush(stdout); }