1#!/usr/bin/python3
2#
3# Copyright 2014, NICTA
4#
5# This software may be distributed and modified according to the terms of
6# the BSD 2-Clause license. Note that NO WARRANTY is provided.
7# See "LICENSE_BSD2.txt" for details.
8#
9# @TAG(DATA61_BSD)
10#
11
12import re, os;
13
14helper_lines = open("helper.h", "r").readlines();
15helper_out = open("../../src/helper.c", "w");
16
17# Generate header.
18print ( "// WARNING: This file is generated. DO NOT EDIT.", file=helper_out );
19print ( "// Look in include/pci/helper_gen.y instead.\n", file=helper_out );
20print ( "#include <pci/helper.h>\n", file=helper_out );
21
22# Generate vendor IDs.
23print ( "char* libpci_vendorID_str(int vid) {", file=helper_out );
24
25for line in helper_lines:
26    line = line.strip();
27    r = re.search(r'^#define PCI_VENDOR_ID_([A-Z0-9_]+)\s+(\w+)', line);
28    if not r: continue;
29
30    (vendor, val) = (r.group(1).lower(), r.group(2));
31    print ( "    if (vid == %s) return \"%s\";" % (val, vendor), file=helper_out );
32
33print ( "    return \"Unknown vendor ID.\"; ", file=helper_out );
34print ( "}\n", file=helper_out );
35
36# Generate device IDs.
37print ( "char* libpci_deviceID_str(int vid, int did) {", file=helper_out );
38vendor = ""; vval = "";
39for line in helper_lines:
40    line = line.strip();
41
42    rv = re.search(r'^#define PCI_VENDOR_ID_([A-Z0-9_]+)\s+(\w+)', line);
43    if rv:
44        (vendor, vval) = (rv.group(1).lower(), rv.group(2));
45
46    r = re.search(r'^#define PCI_DEVICE_ID_([A-Z0-9]+_[A-Z0-9_]+)\s+(\w+)', line);
47    if not r: continue;
48
49    (device, val) = (r.group(1).lower(), r.group(2));
50    print ( "    if (vid == %s && did == %s) return \"%s\";" % (vval, val, device), file=helper_out );
51
52print ( "    return \"Unknown device ID.\"; ", file=helper_out );
53print ( "}\n", file=helper_out );
54