Index: Makefile =================================================================== --- Makefile +++ Makefile @@ -4,6 +4,8 @@ PROG= pciconf SRCS= pciconf.c cap.c err.c MAN= pciconf.8 +DPADD= ${LIBXO} +LDADD= -lxo WARNS?= 3 Index: pciconf.c =================================================================== --- pciconf.c +++ pciconf.c @@ -45,6 +45,7 @@ #include #include #include +#include #include @@ -105,6 +106,10 @@ listmode = readmode = writemode = attachedmode = 0; bars = caps = errors = verbose = vpd = byte = isshort = 0; + argc = xo_parse_args(argc, argv); + if (argc < 0) + usage(); + while ((c = getopt(argc, argv, "abcehlrwvV")) != -1) { switch(c) { case 'a': @@ -208,6 +213,7 @@ pc.patterns = patterns; } + xo_create(XO_STYLE_TEXT, XOF_CLOSE_FP); do { if (ioctl(fd, PCIOCGETCONF, &pc) == -1) err(1, "ioctl(PCIOCGETCONF)"); @@ -232,8 +238,9 @@ return; } for (p = conf; p < &conf[pc.num_matches]; p++) { - printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x " - "chip=0x%08x rev=0x%02x hdr=0x%02x\n", + xo_open_container("device"); + xo_emit("{:device-id/%s%d}@{:bus-id/pci%d:%d:%d:%d}:\tclass={:class-id/0x%06x} card={:card-id/0x%08x} " + "chip={:chip-id/0x%08x} rev={:rev/0x%02x} hdr={:hdr/0x%02x}\n", (p->pd_name && *p->pd_name) ? p->pd_name : "none", (p->pd_name && *p->pd_name) ? (int)p->pd_unit : @@ -254,9 +261,12 @@ list_errors(fd, p); if (vpd) list_vpd(fd, p); + xo_close_container("device"); } } while (pc.status == PCI_GETCONF_MORE_DEVS); + xo_finish(); + xo_destroy(NULL); close(fd); } @@ -282,6 +292,7 @@ return; } +// xo_open_container("bars"); for (i = 0; i <= max; i++) { bar.pbi_sel = p->pc_sel; bar.pbi_reg = PCIR_BAR(i); @@ -311,11 +322,14 @@ } base = bar.pbi_base & ~((uint64_t)0xf); } - printf(" bar [%02x] = type %s, range %2d, base %#jx, ", + xo_open_container("bar"); + xo_emit(" bar [{:addr/%02x}] = type {:type/%s}, range {:range/%2d}, base {:base/%#jx}, ", PCIR_BAR(i), type, range, (uintmax_t)base); - printf("size %ju, %s\n", (uintmax_t)bar.pbi_length, + xo_emit("size {:size/%ju}, {:state/%s}\n", (uintmax_t)bar.pbi_length, bar.pbi_enabled ? "enabled" : "disabled"); + xo_close_container("bar"); } +// xo_close_container("bars"); } static void @@ -325,9 +339,10 @@ struct pci_device_info *di; const char *dp; + xo_open_container("verbose"); TAILQ_FOREACH(vi, &pci_vendors, link) { if (vi->id == p->pc_vendor) { - printf(" vendor = '%s'\n", vi->desc); + xo_emit(" vendor = '{:vendor-name/%s}'\n", vi->desc); break; } } @@ -336,15 +351,16 @@ } else { TAILQ_FOREACH(di, &vi->devs, link) { if (di->id == p->pc_device) { - printf(" device = '%s'\n", di->desc); + xo_emit(" device = '{:device-name/%s}'\n", di->desc); break; } } } if ((dp = guess_class(p)) != NULL) - printf(" class = %s\n", dp); + xo_emit(" class = {:class-name/%s}\n", dp); if ((dp = guess_subclass(p)) != NULL) - printf(" subclass = %s\n", dp); + xo_emit(" subclass = {:subclass-name/%s}\n", dp); + xo_close_container("verbose"); } static void @@ -352,6 +368,7 @@ { struct pci_list_vpd_io list; struct pci_vpd_element *vpd, *end; + char *pve_str; list.plvi_sel = p->pc_sel; list.plvi_len = 0; @@ -367,10 +384,12 @@ vpd = list.plvi_data; end = (struct pci_vpd_element *)((char *)vpd + list.plvi_len); + xo_open_container("vpd"); for (; vpd < end; vpd = PVE_NEXT(vpd)) { if (vpd->pve_flags == PVE_FLAG_IDENT) { - printf(" VPD ident = '%.*s'\n", - (int)vpd->pve_datalen, vpd->pve_data); + strncpy(pve_str, (void*)&vpd->pve_data, vpd->pve_datalen); + xo_emit(" VPD ident = '{:ident/%s}'\n", pve_str); + free(pve_str); continue; } @@ -387,20 +406,27 @@ /* Handle extended capability keyword. */ if (!(vpd->pve_flags & PVE_FLAG_RW) && memcmp(vpd->pve_keyword, "CP", 2) == 0) { - printf(" VPD ro CP = ID %02x in map 0x%x[0x%x]\n", + xo_open_container("capability"); + xo_emit(" VPD {:mode/ro} {:keyword/CP} = ID {:id/%02x} in map {:addr/0x%x}[{:size/0x%x}]\n", (unsigned int)vpd->pve_data[0], PCIR_BAR((unsigned int)vpd->pve_data[1]), (unsigned int)vpd->pve_data[3] << 8 | (unsigned int)vpd->pve_data[2]); + xo_close_container("capability"); continue; } /* Remaining keywords should all have ASCII values. */ - printf(" VPD %s %c%c = '%.*s'\n", + strncpy(pve_str, (void*)&vpd->pve_data, vpd->pve_datalen); + xo_open_container("item"); + xo_emit(" VPD {:mode/%s} {:keyword/%c%c} = '{:value/%s}'\n", vpd->pve_flags & PVE_FLAG_RW ? "rw" : "ro", vpd->pve_keyword[0], vpd->pve_keyword[1], - (int)vpd->pve_datalen, vpd->pve_data); + pve_str); + xo_close_container("item"); + free(pve_str); } + xo_close_container("vpd"); free(list.plvi_data); } @@ -748,7 +774,9 @@ readone(int fd, struct pcisel *sel, long reg, int width) { - printf("%0*x", width*2, read_config(fd, sel, reg, width)); + char buf[11]; + snprintf(buf, sizeof(buf), "%0*x", width*2, read_config(fd, sel, reg, width)); + xo_emit("{:cfgdata/%s}", buf); } static void @@ -817,6 +845,6 @@ if (ioctl(fd, PCIOCATTACHED, &pi) < 0) err(1, "ioctl(PCIOCATTACHED)"); + xo_emit("{:name/%s}: {:status/%s%s}\n", name, pi.pi_data == 0 ? "not " : "", "attached"); exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */ - printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached"); }