Welcome to little lamb

Code » limb » release » tree

[release] / src / liblimb / autoopt.h / parse_buffer_dest.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <errno.h>
#include <limb/bytestr.h>
#include <limb/djbunix.h>
#include <limb/obuffer.h>
#include "autoopt.h"

int
parse_buffer_dest(u8 *lvl, const char ** const arg, size_t len)
{
    /* is there a level specified via @[level]: */
    if (**arg == '@') {
        ++*arg;
        --len;
        /* another '@' would mean file name starts with a '@' */
        if (**arg != '@') {
            /* until the ':' we have the level */
            size_t end = byte_chr(*arg, len, ':');
            if (end == len)
                return (errno = EINVAL, *lvl = (u8) -1, -1);
            if (end == 0) {
                /* @: w/out level means bumping it from its default value */
                if (*lvl < OLVL_MAXIMUM)
                    *lvl += 50;
            } else {
                *lvl = obuffer_parse_level(*arg, end);
                if (*lvl == (u8) -1)
                    return (errno = EINVAL, -1);
            }
            *arg += end + 1;
        }
    }

    return open_parsed_name(*arg, open_append);
}