{ "alternative_registry.h": "#ifndef ALTERNATIVE_REGISTRY_H\n#define ALTERNATIVE_REGISTRY_H\n#include \n#include \n#include \n#include \nnamespace alternatives {\nenum class Error { none = 0, invalid = 1 };\nenum class Mode { automatic = 0, manual = 1 };\nenum class Operation { install = 0, remove = 1, select = 2, automatic = 3, enable = 4 };\nstruct Slot {\n std::string name;\n std::string link;\n};\nstruct Attachment {\n std::string slot;\n std::string target;\n};\nstruct Candidate {\n std::string path;\n std::int32_t priority = 0;\n bool enabled = true;\n std::vector attachments;\n};\nstruct Group {\n std::string name;\n std::string master_link;\n std::vector slots;\n std::vector candidates;\n Mode mode = Mode::automatic;\n std::string pin;\n};\nstruct World {\n std::vector groups;\n};\nstruct Selection {\n // A manual selection can have an empty current while its installed pin sleeps.\n std::string group;\n Mode mode = Mode::automatic;\n std::string pin;\n std::string current;\n};\nstruct Binding {\n // Every declared entry appears, including entries whose target is empty.\n std::string link;\n std::string target;\n};\nstruct Projection {\n std::vector selections;\n std::vector bindings;\n};\nstruct Event {\n Operation op = Operation::automatic;\n std::string group;\n std::string path;\n std::string priority;\n bool enabled = false;\n std::vector attachments;\n};\nstruct Change {\n // Empty before/after values denote an absent logical target, not inheritance.\n std::string link;\n std::string before;\n std::string after;\n};\nstruct Frame {\n // Each frame owns the complete post-event state and the changes from its predecessor.\n World world;\n Projection projection;\n std::vector changes;\n};\nstruct Transcript {\n World final_world;\n std::vector frames;\n};\n// Shared syntax predicates are candidate code, never host filesystem probes.\nbool valid_id(std::string_view value);\nbool valid_path(std::string_view value);\nError parse_priority(std::string_view text, std::int32_t& output);\nError compile_world(const std::vector& groups, World& output);\nProjection project(const World& world);\nError replay(const World& initial, const std::vector& events, Transcript& output);\n} // namespace alternatives\n#endif\n", "registry_observe.cpp": "#include \"alternative_registry.h\"\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace alternatives;\nnamespace {\nconst char* hex = \"0123456789abcdef\";\nstd::string quoted(const std::string& value) {\n std::string result = \"\\\"\";\n for (unsigned char c : value) {\n result += \"\\\\u00\";\n result += hex[c >> 4];\n result += hex[c & 15];\n }\n return result + \"\\\"\";\n}\nint nibble(char c) {\n if (c >= '0' && c <= '9') {\n return c - '0';\n }\n if (c >= 'a' && c <= 'f') {\n return c - 'a' + 10;\n }\n throw std::runtime_error(\"hex\");\n}\nstd::string unhex(const std::string& value) {\n if (value == \"-\") {\n return {};\n }\n if (value.empty() || value.size() % 2 != 0) {\n throw std::runtime_error(\"hex length\");\n }\n std::string result;\n for (std::size_t i = 0; i < value.size(); i += 2) {\n result += static_cast((nibble(value[i]) << 4) | nibble(value[i + 1]));\n }\n return result;\n}\nstd::string boolean(bool value) {\n return value ? \"true\" : \"false\";\n}\ntemplate\nstd::string array(const std::vector& values, Encode encode) {\n std::string result = \"[\";\n bool first = true;\n for (const auto& value : values) {\n if (!first) {\n result += ',';\n }\n first = false;\n result += encode(value);\n }\n return result + ']';\n}\nstd::string slot_json(const Slot& slot) {\n return \"[\" + quoted(slot.name) + ',' + quoted(slot.link) + ']';\n}\nstd::string attachment_json(const Attachment& attachment) {\n return \"[\" + quoted(attachment.slot) + ',' + quoted(attachment.target) + ']';\n}\nstd::string candidate_json(const Candidate& candidate) {\n return \"[\" + quoted(candidate.path) + ',' + std::to_string(candidate.priority) + ',' +\n boolean(candidate.enabled) + ',' + array(candidate.attachments, attachment_json) + ']';\n}\nstd::string group_json(const Group& group) {\n return \"[\" + quoted(group.name) + ',' + quoted(group.master_link) + ',' +\n std::to_string(static_cast(group.mode)) + ',' + quoted(group.pin) + ',' +\n array(group.slots, slot_json) + ',' + array(group.candidates, candidate_json) + ']';\n}\nstd::string world_json(const World& world) {\n return array(world.groups, group_json);\n}\nstd::string selection_json(const Selection& selection) {\n return \"[\" + quoted(selection.group) + ',' + std::to_string(static_cast(selection.mode)) +\n ',' + quoted(selection.pin) + ',' + quoted(selection.current) + ']';\n}\nstd::string binding_json(const Binding& binding) {\n return \"[\" + quoted(binding.link) + ',' + quoted(binding.target) + ']';\n}\nstd::string projection_json(const Projection& projection) {\n return \"[\" + array(projection.selections, selection_json) + ',' +\n array(projection.bindings, binding_json) + ']';\n}\nstd::string change_json(const Change& change) {\n return \"[\" + quoted(change.link) + ',' + quoted(change.before) + ',' + quoted(change.after) + ']';\n}\nstd::string frame_json(const Frame& frame) {\n return \"[\" + world_json(frame.world) + ',' + projection_json(frame.projection) + ',' +\n array(frame.changes, change_json) + ']';\n}\nstd::string transcript_json(const Transcript& transcript) {\n return \"[\" + world_json(transcript.final_world) + ',' + array(transcript.frames, frame_json) + ']';\n}\nstd::string event_json(const Event& event) {\n return \"[\" + std::to_string(static_cast(event.op)) + ',' + quoted(event.group) + ',' +\n quoted(event.path) + ',' + quoted(event.priority) + ',' + boolean(event.enabled) + ',' +\n array(event.attachments, attachment_json) + ']';\n}\nWorld dirty_world() {\n World result;\n Group group;\n group.name = \"dirty\";\n group.master_link = \"/dirty/master\";\n group.slots.push_back({\"dirty\", \"/dirty/slave\"});\n group.candidates.push_back({\"/dirty/candidate\", 123, true, {{\"dirty\", \"/dirty/target\"}}});\n result.groups.push_back(group);\n return result;\n}\nTranscript dirty_transcript() {\n Transcript result;\n result.final_world = dirty_world();\n Frame frame;\n frame.world = dirty_world();\n frame.projection.bindings.push_back({\"/dirty/link\", \"/dirty/target\"});\n frame.changes.push_back({\"/dirty/link\", \"old\", \"new\"});\n result.frames.push_back(frame);\n return result;\n}\nvoid mutate_copy(World& world) {\n if (world.groups.empty()) {\n world.groups.push_back({});\n }\n auto& group = world.groups.front();\n group.name = \"copy_changed\";\n group.pin = \"/copy/pin\";\n if (group.slots.empty()) {\n group.slots.push_back({});\n }\n group.slots.front().name = \"copy_slot\";\n group.slots.front().link = \"/copy/link\";\n if (group.candidates.empty()) {\n group.candidates.push_back({});\n }\n auto& candidate = group.candidates.front();\n candidate.path = \"/copy/candidate\";\n candidate.priority = -123;\n candidate.enabled = !candidate.enabled;\n if (candidate.attachments.empty()) {\n candidate.attachments.push_back({});\n }\n candidate.attachments.front() = {\"copy_slot\", \"/copy/target\"};\n}\nvoid mutate_copy(Transcript& transcript) {\n mutate_copy(transcript.final_world);\n if (transcript.frames.empty()) {\n transcript.frames.push_back({});\n }\n auto& frame = transcript.frames.front();\n mutate_copy(frame.world);\n frame.projection.selections.push_back({\"copy\", Mode::manual, \"/copy\", \"/copy\"});\n if (frame.projection.bindings.empty()) {\n frame.projection.bindings.push_back({});\n }\n frame.projection.bindings.front() = {\"/copy/link\", \"/copy/target\"};\n if (frame.changes.empty()) {\n frame.changes.push_back({});\n }\n frame.changes.front() = {\"/copy/link\", \"before_copy\", \"after_copy\"};\n}\nvoid end_of_command(std::istringstream& input) {\n std::string extra;\n if (input >> extra) {\n throw std::runtime_error(\"extra token\");\n }\n}\nstd::string token(std::istringstream& input) {\n std::string result;\n if (!(input >> result)) {\n throw std::runtime_error(\"missing token\");\n }\n return result;\n}\nstd::size_t index(std::istringstream& input) {\n return static_cast(std::stoull(token(input)));\n}\nint number(std::istringstream& input) {\n return std::stoi(token(input));\n}\n} // namespace\nint main() {\n std::vector raw;\n World world;\n std::vector events;\n Transcript transcript;\n bool world_ok = false;\n bool transcript_ok = false;\n std::string line;\n try {\n while (std::getline(std::cin, line)) {\n std::istringstream input(line);\n const std::string command = token(input);\n std::string result;\n if (command == \"R\") {\n raw.clear();\n result = \"0\";\n } else if (command == \"G\") {\n Group group;\n group.name = unhex(token(input));\n group.master_link = unhex(token(input));\n group.mode = static_cast(number(input));\n group.pin = unhex(token(input));\n raw.push_back(std::move(group));\n result = \"0\";\n } else if (command == \"S\") {\n const auto gi = index(input);\n Slot slot;\n slot.name = unhex(token(input));\n slot.link = unhex(token(input));\n raw.at(gi).slots.push_back(std::move(slot));\n result = \"0\";\n } else if (command == \"C\") {\n const auto gi = index(input);\n Candidate candidate;\n candidate.path = unhex(token(input));\n candidate.priority = static_cast(number(input));\n candidate.enabled = number(input) != 0;\n raw.at(gi).candidates.push_back(std::move(candidate));\n result = \"0\";\n } else if (command == \"T\") {\n const auto gi = index(input);\n const auto ci = index(input);\n Attachment attachment;\n attachment.slot = unhex(token(input));\n attachment.target = unhex(token(input));\n raw.at(gi).candidates.at(ci).attachments.push_back(std::move(attachment));\n result = \"0\";\n } else if (command == \"W\") {\n const std::string before = array(raw, group_json);\n world = dirty_world();\n const Error error = compile_world(raw, world);\n world_ok = error == Error::none;\n const bool unchanged = before == array(raw, group_json);\n result = \"[\" + std::to_string(static_cast(error)) + ',' +\n world_json(world) + ',' + boolean(unchanged) + ']';\n } else if (command == \"K\") {\n const std::string text = unhex(token(input));\n const std::string before = text;\n std::int32_t value = 987654321;\n const Error error = parse_priority(text, value);\n result = \"[\" + std::to_string(static_cast(error)) + ',' +\n std::to_string(value) + ',' + boolean(text == before) + ']';\n } else if (command == \"I\") {\n const std::string text = unhex(token(input));\n const std::string before = text;\n const bool identifier = valid_id(text);\n const bool path = valid_path(text);\n const bool unchanged = before == text;\n result = \"[\" + boolean(identifier) + ',' + boolean(path) + ',' + boolean(unchanged) + ']';\n } else if (command == \"P\") {\n if (!world_ok) {\n throw std::runtime_error(\"world unavailable\");\n }\n const std::string before = world_json(world);\n const Projection projected = project(world);\n result = \"[\" + projection_json(projected) + ',' + boolean(before == world_json(world)) + ']';\n } else if (command == \"X\") {\n events.clear();\n result = \"0\";\n } else if (command == \"E\") {\n Event event;\n event.op = static_cast(number(input));\n event.group = unhex(token(input));\n event.path = unhex(token(input));\n event.priority = unhex(token(input));\n event.enabled = number(input) != 0;\n const auto count = index(input);\n for (std::size_t i = 0; i < count; ++i) {\n Attachment attachment;\n attachment.slot = unhex(token(input));\n attachment.target = unhex(token(input));\n event.attachments.push_back(std::move(attachment));\n }\n events.push_back(std::move(event));\n result = \"0\";\n } else if (command == \"F\") {\n if (!world_ok) {\n throw std::runtime_error(\"world unavailable\");\n }\n const std::string before = world_json(world) + array(events, event_json);\n transcript = dirty_transcript();\n const Error error = replay(world, events, transcript);\n transcript_ok = error == Error::none;\n const bool unchanged = before == world_json(world) + array(events, event_json);\n result = \"[\" + std::to_string(static_cast(error)) + ',' +\n transcript_json(transcript) + ',' + boolean(unchanged) + ']';\n } else if (command == \"U\") {\n if (!transcript_ok) {\n throw std::runtime_error(\"transcript unavailable\");\n }\n world = transcript.final_world;\n world_ok = true;\n result = \"0\";\n } else if (command == \"J\") {\n const std::string before_world = world_json(world);\n const std::string before_transcript = transcript_json(transcript);\n World world_copy = world;\n Transcript transcript_copy = transcript;\n mutate_copy(world_copy);\n mutate_copy(transcript_copy);\n result = \"[\" + boolean(before_world == world_json(world)) + ',' +\n boolean(before_transcript == transcript_json(transcript)) + ']';\n } else {\n throw std::runtime_error(\"unknown command\");\n }\n end_of_command(input);\n std::cout << result << '\\n';\n }\n } catch (const std::exception& error) {\n std::cerr << error.what() << '\\n';\n return 2;\n }\n return 0;\n}\n" }