view libutil/stdin_handler.c @ 1:e5527fc2050b

libutil: copy from ice1-trau-tester
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 29 Aug 2024 12:58:56 +0000
parents
children
line wrap: on
line source

/*
 * Stdin handler function: gets called from Osmocom select loop for stdin,
 * does line read and initial parsing into arguments, then calls
 * program-supplied handler.
 */

#include <ctype.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <osmocom/core/select.h>

#include "stdin_handler.h"

#define	MAX_ARGS	16

int stdin_select_cb(struct osmo_fd *ofd, unsigned int what)
{
	stdin_dispatch_t dispatch_func = ofd->data;
	char buf[256], *argv[MAX_ARGS+1], *cp;
	int argc;

	fgets(buf, sizeof buf, stdin);
	argc = 0;
	for (cp = buf; ; ) {
		while (isspace(*cp))
			cp++;
		if (*cp == '\0' || *cp == '#')
			break;
		if (argc >= MAX_ARGS) {
			printf("error: input command exceeds MAX_ARGS\n");
			return 0;
		}
		argv[argc] = cp;
		while (*cp && !isspace(*cp))
			cp++;
		if (*cp)
			*cp++ = '\0';
		argc++;
	}
	if (!argc)
		return 0;
	argv[argc] = NULL;
	dispatch_func(argc, argv);
	return 0;
}