site  contact  subhomenews

Fix convert pup_event_ipc.bac to C

November 18, 2026 — BarryK

Yesterday I posted about using Claude AI to convert some utilities in the pup-tools package written in BaCon and nim to C:

The BaCon utilities: welcome1stboot.bac, find_cat.bac, pup_event_ipc.bac

The nim utility: debdb2pupdb.nim

Don't actually use welcome1stboot anymore, nor, I think, find_cat; however, pup_event_ipc is an important part of the pup_event mechanism.

I compiled these C programs, and unfortunately pup_event_ipc does not work. Could probably work through it and find where it is going wrong; however, decided on a different approach. The BaCon compiler is not an actual compiler; it generates C code then calls gcc. So, I told the 'bacon' executable to create the C code only, then I made it into a tarball and provided it to Claude. I asked Claude:

Could you simplify this C code

What followed is quite remarkable. The C code created by bacon is very human-unreadable, so I was hoping that Claude could reduce it to something simpler and human-readable. It did. This is Claude's analysis:

I've rewritten the BaCon-generated code as one standalone file, `pup_event_ipc.c`, about 186 lines. It builds with `cc -o pup_event_ipc pup_event_ipc.c`. The original was roughly 1,850 lines spread over nine files, plus the BaCon runtime.

I compiled both versions and ran them side by side. They gave the same output and exit codes for every case I tried:
- mailbox send, read and empty
- the `waitmail` timeout
- a bad format
- a request/reply exchange
- the keyword waits

**What changed**
- The `#line` markers, the BaCon string runtime, the `select`/`CONCAT` machinery and the `exitsub`/`waitsub` wrappers are gone.
- The help text is one string literal.
- The keyword handling is a small table: `block`, `dbus`, `network`, `x`, `bluetoothhw`, `timeout1`, `timeout4`, `timeout60` and `netchg` all wait on `<keyword>_<client>`.
- The "Not implemented" branch is gone. It could never run, because the file had just been created a few lines earlier.
- Cleanup is left to `exit`, which closes the file descriptors. There's no explicit `inotify_rm_watch`.
- The timeout wait now uses `poll` in both cases, with an infinite timeout when `-t` is 0, instead of a separate blocking `read`.

**Behaviour that differs from the original**
- **`dbus`:** In the original it was missing from the keyword list, so `dbus:x` wrote to file descriptor 0 (stdin). Now it behaves like the other keywords. This is a guess at what the author intended.
- **Error codes 3 and 4:** A failed `open` used to hit BaCon's runtime error first, so the documented exit codes 3 and 4 never happened. They now work as the help text says.
- **`-t` as the last argument:** It used to read past the end of `argv`. Now it's ignored.

The client names go straight into `/tmp/pup_event_ipc/<name>` with no check for `/`, same as the original. That would allow path traversal if untrusted input ever reaches it. I left it alone to keep behaviour identical, but it's a one-line check if you want it.

The result is a single small C file, completely redesigned, using some C functions that must be in libc, but I know nothing about them. Anyway, it compiled, and it works. Haven't studied how it works; for now, just happy that it does.  

Tags: easy