Something went wrong. Try again.
A reader for SNMP MIB modules: SMIv1 and SMIv2, in Zig.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! What one module parsed to, assignment by assignment.//!//! `lexcheck` says whether a file parsed and `smitranslate` says where its//! names landed, and between them is a gap: a file that parses *successfully*//! and yet is missing most of itself. ASN.1 has no statement terminator, so a//! type assignment runs until the parser decides the next one begins, and a//! parser that decides wrong quietly absorbs whatever follows and reports no//! error at all.//!//! That is not a hypothetical. `SNMPv2-SMI` reported a clean parse while//! silently dropping everything below line 55, because `ExtUTCTime`'s type//! body ran on into the `MODULE-IDENTITY MACRO` header and the macro's own//! `END` then closed the module. The symptom, three tools away, was a single//! missing name in a 2,637-line diff. Listing what a module actually yielded//! is how that gets found in one command://!//! ```console//! $ dump mibs/SNMPv2-SMI.txt//! module SNMPv2-SMI, 21 assignments://! org (object_identifier)//! ...//! zeroDotZero (object_identity)//! ```
const std = @import("std");const Io = std.Io;const smi = @import("smi");
pub fn main(init: std.process.Init) !void { const io = init.io; const gpa = init.arena.allocator();
var ob: [64 * 1024]u8 = undefined; var out: Io.File.Writer = .init(.stdout(), io, &ob); defer out.interface.flush() catch {}; var eb: [4 * 1024]u8 = undefined; var err: Io.File.Writer = .init(.stderr(), io, &eb); defer err.interface.flush() catch {};
const args = try init.minimal.args.toSlice(gpa); if (args.len != 2) { try err.interface.writeAll( \\usage: dump <mib-file> \\ \\Prints the module's name and every assignment in it, in source \\order, with the kind of each. \\ ); try err.interface.flush(); std.process.exit(2); }
var file = try Io.Dir.cwd().openFile(io, args[1], .{}); defer file.close(io); var buffer: [4096]u8 = undefined; var reader = file.readerStreaming(io, &buffer); const source = try reader.interface.allocRemaining(gpa, .unlimited);
var diagnostic: ?smi.Parser.Diagnostic = null; const module = smi.Parser.parseWithDiagnostic(gpa, source, .{}, &diagnostic) catch |e| { if (diagnostic) |d| { try err.interface.print("{s}: line {d}: {s}\n", .{ args[1], d.line(source), d.message, }); } else { try err.interface.print("{s}: {t}\n", .{ args[1], e }); } try err.interface.flush(); std.process.exit(1); };
try out.interface.print("module {s}, {d} assignments:\n", .{ module.name, module.assignments.len, }); for (module.assignments) |a| { try out.interface.print(" {s} ({t})\n", .{ a.name, a.kind }); }}