Skip to content
suit Artifact [cc0c807ffe]
Login

Artifact [cc0c807ffe]

Artifact cc0c807ffe67feba8efde455b0719931a13e87ec4caa735fcf1ccfd2ee995061:


#ifndef SUIT_LINT_H
#define SUIT_LINT_H

#include <stddef.h>
#include <stdint.h>

// The lint: what a file in the store must not hold before it is checked in
// (docs/store.md, "The lint"). It reads text and reports; it changes nothing.

constexpr size_t LINT_FILE_MAX = 4194304;      // a larger file is refused, not scanned
constexpr size_t LINT_LINE_MAX = 65536;        // a longer line is refused
constexpr size_t LINT_BINARY_PROBE = 8192;     // a NUL in this many bytes makes a file binary

// suit.lint, at the wardrobe's top: `allow PATH` and `forbid TEXT` lines.
constexpr char LINT_RULES_NAME[] = "suit.lint";
constexpr size_t LINT_RULES_FILE_MAX = 65536;
constexpr size_t LINT_RULES_LINE_MAX = 1024;
constexpr size_t LINT_TEXT_MAX = 200;          // the longest forbidden text
constexpr uint32_t LINT_ALLOW_MAX = 256;
constexpr uint32_t LINT_FORBID_MAX = 64;

typedef struct {
    uint32_t n_allow;
    uint32_t n_forbid;
    const char *allow[LINT_ALLOW_MAX];
    const char *forbid[LINT_FORBID_MAX];  // lower case
    char buf[LINT_RULES_FILE_MAX + 1];
} LintRules;

// line is 0 when the error concerns the whole file. err is the error number
// of a file that could not be read, else 0.
typedef struct {
    int line;
    int err;
    char msg[256];
} LintError;

// Parse DATA (LEN bytes) as a suit.lint. On failure R is empty and ERR says
// what and where.
[[nodiscard]] bool lint_rules_parse(LintRules *r, const char *data, size_t len, LintError *err);

// Read PATH. A file that is not there is no rules.
[[nodiscard]] bool lint_rules_load(LintRules *r, const char *path, LintError *err);

// Whether R exempts the file PATH (relative to the wardrobe's top) from the
// token and attribution rules, and a SKILL.md from rule 6.
bool lint_allowed(const LintRules *r, const char *path);

// What is wrong with LINE (LEN bytes, no newline), as a fixed text, or nullptr.
const char *lint_line(const LintRules *r, const char *line, size_t len);

// Called for each problem: LINE is 1-based.
typedef void (*LintSink)(void *state, size_t line, const char *what);

// Scan one file's bytes: nothing if they look binary, else every line through
// lint_line (unless EXEMPT) and a refusal for a line over LINT_LINE_MAX.
// Returns the number of problems.
size_t lint_text(const LintRules *r, bool exempt, const char *data, size_t len, LintSink sink, void *state);

// Whether a SKILL.md begins with a "---" line.
bool lint_has_header(const char *data, size_t len);

// What is at a path relative to the wardrobe's top, for rule 6.
typedef enum { LINT_NONE, LINT_FILE, LINT_EXEC, LINT_DIR } LintKind;
typedef LintKind (*LintLookup)(void *state, const char *path);

// Rule 6 over the bytes of skills/SKILL/SKILL.md: each script it names in a
// code span or a shell code block, by a path that points into a skill folder,
// must be a file in that folder, and executable when a shell line starts with
// it. LOOK says what is at a path. Returns the number of problems.
size_t lint_scripts(const char *skill, const char *data, size_t len, LintLookup look, void *look_state,
    LintSink sink, void *state);

#endif