Install/update the Codex SessionStart reminder hook in config.toml. */
| 1628 | |
| 1629 | /* Install/update the Codex SessionStart reminder hook in config.toml. */ |
| 1630 | int cbm_upsert_codex_hooks(const char *config_path) { |
| 1631 | if (!config_path) { |
| 1632 | return CLI_ERR; |
| 1633 | } |
| 1634 | char block[CLI_BUF_2K]; |
| 1635 | snprintf(block, sizeof(block), |
| 1636 | "\n" CODEX_HOOK_BEGIN "\n" |
| 1637 | "[[hooks.SessionStart]]\n" |
| 1638 | "matcher = \"startup|resume|clear|compact\"\n\n" |
| 1639 | "[[hooks.SessionStart.hooks]]\n" |
| 1640 | "type = \"command\"\n" |
| 1641 | "command = '%s'\n" CODEX_HOOK_END "\n", |
| 1642 | CMM_SESSION_REMINDER_CMD); |
| 1643 | |
| 1644 | size_t len = 0; |
| 1645 | char *content = read_file_str(config_path, &len); |
| 1646 | if (!content) { |
| 1647 | return write_file_str(config_path, block + CLI_SKIP_ONE); /* skip leading newline */ |
| 1648 | } |
| 1649 | char *stripped = codex_hook_strip(content); |
| 1650 | const char *base = stripped ? stripped : content; |
| 1651 | size_t base_len = strlen(base); |
| 1652 | char *result = malloc(base_len + strlen(block) + CLI_SKIP_ONE); |
| 1653 | if (!result) { |
| 1654 | free(content); |
| 1655 | free(stripped); |
| 1656 | return CLI_ERR; |
| 1657 | } |
| 1658 | memcpy(result, base, base_len); |
| 1659 | memcpy(result + base_len, block, strlen(block)); |
| 1660 | result[base_len + strlen(block)] = '\0'; |
| 1661 | int rc = write_file_str(config_path, result); |
| 1662 | free(content); |
| 1663 | free(stripped); |
| 1664 | free(result); |
| 1665 | return rc; |
| 1666 | } |
| 1667 | |
| 1668 | int cbm_remove_codex_hooks(const char *config_path) { |
| 1669 | if (!config_path) { |
no test coverage detected