Skip to content
suit Documentation
Login

Documentation

#ifndef SUIT_NEED_H
#define SUIT_NEED_H

#include <stdbool.h>
#include <stddef.h>

// The test a need carries in its `done` text. Two forms and nothing else:
//   PATH exists
//   PATH contains "TEXT"
// PATH is one word; a leading ~ is the home folder; it must then be absolute.
// TEXT is everything between the first quote after "contains " and the last
// quote on the line, and is not empty.

constexpr size_t DONE_PATH_MAX = 4096;
constexpr size_t DONE_FILE_MAX = 4 * 1048576;  // a file over this is not searched
constexpr size_t DONE_WHY_MAX = 256;

typedef enum { DONE_EXISTS, DONE_CONTAINS } DoneKind;

// TEXT points into the string that was parsed, which must outlive the test.
typedef struct {
    DoneKind kind;
    char path[DONE_PATH_MAX];
    const char *text;
    size_t text_len;
} DoneTest;

typedef enum { NEED_DONE, NEED_NOT_DONE, NEED_UNKNOWN } NeedState;

// Read the test in S, with HOME the home folder. False if it is not one of
// the two forms, with WHY (CAP bytes) saying what is wrong.
[[nodiscard]] bool done_parse(DoneTest *t, const char *home, const char *s, char *why, size_t cap);

// Whether the test holds. A missing file is not done. A file that cannot be
// read, or is not a regular file, or is over DONE_FILE_MAX, is NEED_UNKNOWN,
// and WHY says why.
NeedState done_eval(const DoneTest *t, char *why, size_t cap);

// Whether P is a path inside the store: not empty, relative, with no ".." part.
bool need_prompt_ok(const char *p);

#endif