#!/usr/bin/env python3

import sys

def generate_pci_data():
    vendors = []
    devices = []
    classes = []
    
    current_vendor = None
    in_class_section = False

    for line in sys.stdin:
        if not line.strip() or line.startswith('#'):
            continue

        if line.startswith('C '):
            in_class_section = True
            parts = line.split()
            class_id = parts[1]
            name = " ".join(parts[2:]).replace('"', '\\"')
            classes.append((class_id, "00", name))
            current_class = class_id
            continue

        if not in_class_section:
            if not line.startswith('\t'):
                parts = line.split()
                if len(parts) < 2: continue
                current_vendor = parts[0]
                name = " ".join(parts[1:]).replace('"', '\\"')
                vendors.append((current_vendor, name))
            elif line.startswith('\t') and not line.startswith('\t\t'):
                parts = line.strip().split()
                if len(parts) < 2: continue
                dev_id = parts[0]
                name = " ".join(parts[1:]).replace('"', '\\"')
                devices.append((current_vendor, dev_id, name))
        else:
            if line.startswith('\t') and not line.startswith('\t\t'):
                parts = line.strip().split()
                sub_id = parts[0]
                name = " ".join(parts[1:]).replace('"', '\\"')
                classes.append((current_class, sub_id, name))

    print("#include <libk/std.h>\n")
    print("#include <device/pci.h>\n")
    
    print("const struct pci_vendor pci_vendors[] = {")
    for v_id, name in vendors:
        print(f"    {{0x{v_id}, \"{name}\"}},")
    print("    {0xFFFF, 0}\n};\n")

    print("const struct pci_device_id pci_device_names[] = {")
    for v_id, d_id, name in devices:
        print(f"    {{0x{v_id}, 0x{d_id}, \"{name}\"}},")
    print("    {0xFFFF, 0xFFFF, 0}\n};\n")

    print("const struct pci_class pci_classes[] = {")
    for c_id, s_id, name in classes:
        print(f"    {{0x{c_id}, 0x{s_id}, \"{name}\"}},")
    print("    {0xFF, 0xFF, 0}\n};")

if __name__ == "__main__":
    generate_pci_data()
