42sh/src/io_backend/io_backend.h

72 lines
1.7 KiB
C
Raw Normal View History

2026-01-08 14:40:37 +01:00
#ifndef IO_BACKEND_H
#define IO_BACKEND_H
#include <sys/types.h>
2026-01-08 16:15:29 +01:00
// Error codes
#define IOB_ERROR_GENERIC -1
#define IOB_ERROR_BAD_ARG -2
#define IOB_ERROR_MODULE_NOT_INITIALIZED -3
#define IOB_ERROR_MODULE_ALREADY_INITIALIZED -4
2026-01-08 16:15:29 +01:00
#define IOB_ERROR_CANNOT_OPEN_FILE -5
2026-01-12 21:48:11 +01:00
enum iob_mode
{
2026-01-08 14:40:37 +01:00
IOB_MODE_NULL = 0,
IOB_MODE_STDIN,
IOB_MODE_SCRIPT,
IOB_MODE_CMD
};
2026-01-13 11:08:36 +01:00
enum iob_state
{
2026-01-08 16:15:29 +01:00
IOB_STATE_NOT_INITIALIZED,
IOB_STATE_READY,
IOB_STATE_FINISHED,
IOB_STATE_ERROR
};
2026-01-08 14:40:37 +01:00
/* @struct iob_context
* @var mode
* @var args contains
2026-01-08 16:15:29 +01:00
* the script name when mode is set to IOB_MODE_SCRIPT,
* the command to execute when mode is set to IOB_MODE_CMD
2026-01-08 14:40:37 +01:00
*/
2026-01-12 21:48:11 +01:00
struct iob_context
{
2026-01-08 14:40:37 +01:00
enum iob_mode mode;
2026-01-12 21:48:11 +01:00
char *args;
2026-01-08 14:40:37 +01:00
};
/**
* @brief Converts struct arg_options to iob_context
*
* @param args The arguments options struct
* @param ctx The IO Backend context struct to populate
* @return int 0 on success, negative value on error
*/
int iob_config_from_args(struct args_options *args, struct iob_context *ctx);
2026-01-08 14:40:37 +01:00
/*
* @brief Initializes the IO Backend module
2026-01-12 21:48:11 +01:00
*
2026-01-08 14:40:37 +01:00
* @param context contains the input mode and the args
* @return 0 on success, the corresponding error code otherwise
*/
int iob_init(struct iob_context *context);
2026-01-08 16:15:29 +01:00
/* @brief Closes the opened buffers and the module gracefully
2026-01-08 14:40:37 +01:00
*/
2026-01-08 15:02:33 +01:00
void iob_close(void);
2026-01-08 14:40:37 +01:00
2026-01-08 15:02:33 +01:00
/* @brief reads at most one line of the input and stores it into *stream
2026-01-08 14:40:37 +01:00
*
2026-01-08 15:02:33 +01:00
* @param stream is a pointer that will be set to a string to parse
2026-01-08 16:15:29 +01:00
* @return the number of read characters if positive,
* zero if finished (reached EOF),
* the error code otherwise
2026-01-08 14:40:37 +01:00
*/
2026-01-12 21:48:11 +01:00
ssize_t stream_read(char **stream);
2026-01-08 14:40:37 +01:00
#endif /* ! IO_BACKEND_H */