public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
bd091fdee5cb4c5fef206c18310ce35193e98112 blob 2673 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
 
// SPDX-License-Identifier: GPL-2.0-or-later

/* PASST - Plug A Simple Socket Transport
 *  for qemu/UNIX domain socket mode
 *
 * PASTA - Pack A Subtle Tap Abstraction
 *  for network namespace/tap device mode
 *
 * parse.c - Composable parsing helpers
 *
 * Copyright Red Hat
 * Author: David Gibson <david@gibson.dropbear.id.au>
 */

#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "parse.h"

/**
 * DOC: Theory of Operation
 *
 * These are a number of primitives which can be combined into parsers for
 * moderately complex input.  For simpler composability, they have common
 * conventions.
 *
 * - Functions return a bool indicating whether they successfully parsed.
 * - Any specific output from the parse is as output parameters
 * - First argument is always @cursor, a const char **.
 * - On entry, *@cursor has the point to start parsing.
 * - On successful exit, *@cursor is updated to the next character after the
     parsed portion of the input
 * - On failure, *@cursor and any output arguments are not modified
 *
 * For brevity the common parameters and return information are omitted from the
 * individual function documentation comments.
 */

/**
 * parse_literal() - Parse a specified literal string
 * @lit:	Keyword to accept
 */
bool parse_literal(const char **cursor, const char *lit)
{
	size_t len = strlen(lit);

	if (strlen(*cursor) < len || memcmp(*cursor, lit, len))
		return false;

	*cursor += len;
	return true;
}

/**
 * parse_eoi() - Parse end of input
 * @cursor:	Current parse pointer
 *
 * Return: true if @p is at End of Input (\0), false otherwise
 */
bool parse_eoi(const char *cursor)
{
	return !(*cursor);
}

/*
 * parse_unsigned() - Parse an unsigned integer
 * @base:	Numeric base for string as strtoul(3)
 * @valp:	On success, updated with parsed value
 */
bool parse_unsigned(const char **cursor, int base, unsigned long *valp)
{
	const char *p = *cursor;
	unsigned long val;

	errno = 0;
	val = strtoul(p, (char **)&p, base);
	if (errno || p == *cursor)
		return false;
	*valp = val;
	*cursor = p;
	return true;
}

/**
 * parse_port_range() - Parse a range of port numbers '<first>[-<last>]'
 * @range:	Update with the parsed values on success
 */
bool parse_port_range(const char **cursor, struct port_range *range)
{
	unsigned long first, last;
	const char *p = *cursor;

	if (!parse_unsigned(&p, 10, &first))
		return false;

	last = first;

	if (parse_literal(&p, "-"))
		if (!parse_unsigned(&p, 10, &last))
			return false;

	if ((last < first) || (last >= NUM_PORTS))
		return false;

	range->first = first;
	range->last = last;
	*cursor = p;
	return true;
}
debug log:

solving bd091fde ...
found bd091fde in https://archives.passt.top/passt-dev/20260626071003.3472194-8-david@gibson.dropbear.id.au/
found 8abfd4dd in https://archives.passt.top/passt-dev/20260626071003.3472194-7-david@gibson.dropbear.id.au/
found c2a92422 in https://archives.passt.top/passt-dev/20260626071003.3472194-4-david@gibson.dropbear.id.au/

applying [1/3] https://archives.passt.top/passt-dev/20260626071003.3472194-4-david@gibson.dropbear.id.au/
diff --git a/parse.c b/parse.c
new file mode 100644
index 00000000..c2a92422


applying [2/3] https://archives.passt.top/passt-dev/20260626071003.3472194-7-david@gibson.dropbear.id.au/
diff --git a/parse.c b/parse.c
index c2a92422..8abfd4dd 100644


applying [3/3] https://archives.passt.top/passt-dev/20260626071003.3472194-8-david@gibson.dropbear.id.au/
diff --git a/parse.c b/parse.c
index 8abfd4dd..bd091fde 100644

Checking patch parse.c...
Applied patch parse.c cleanly.
Checking patch parse.c...
Applied patch parse.c cleanly.
Checking patch parse.c...
Applied patch parse.c cleanly.

index at:
100644 bd091fdee5cb4c5fef206c18310ce35193e98112	parse.c

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).