/*
 * Copyright 2026 Zygaena Project Contributors
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * BSD-compatible utility library.
 * Link with -lutil.
 */

#ifndef _LIBUTIL_H
#define	_LIBUTIL_H

#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>

struct termios;
struct winsize;

#ifdef __cplusplus
extern "C" {
#endif

/* PTY functions (also available via <pty.h>) */
int	openpty(int *, int *, char *, struct termios *, struct winsize *);
pid_t	forkpty(int *, char *, struct termios *, struct winsize *);
int	login_tty(int);

/* Atomic open + flock */
int	flopen(const char *, int, ...);
int	flopenat(int, const char *, int, ...);

/* PID file management */
struct pidfh;

struct pidfh *pidfile_open(const char *, mode_t, pid_t *);
int	pidfile_write(struct pidfh *);
int	pidfile_close(struct pidfh *);
int	pidfile_remove(struct pidfh *);
int	pidfile_fileno(const struct pidfh *);

/* Human-readable number formatting */
#define	HN_DECIMAL		0x01
#define	HN_NOSPACE		0x02
#define	HN_B			0x04
#define	HN_DIVISOR_1000		0x08
#define	HN_IEC_PREFIXES		0x10

#define	HN_GETSCALE		0x10
#define	HN_AUTOSCALE		0x20

int	humanize_number(char *, size_t, int64_t, const char *, int, int);
int	expand_number(const char *, uint64_t *);

/* Config file line parsing */
#define	FPARSELN_UNESCESC	0x01
#define	FPARSELN_UNESCCONT	0x02
#define	FPARSELN_UNESCCOMM	0x04
#define	FPARSELN_UNESCREST	0x08
#define	FPARSELN_UNESCALL	0x0f

char	*fparseln(FILE *, size_t *, size_t *, const char[3], int);

#ifdef __cplusplus
}
#endif

#endif /* _LIBUTIL_H */
