2018-10-31 19:50:43 +03:00
|
|
|
/*
|
2021-02-12 12:01:14 +03:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2018-10-31 19:50:43 +03:00
|
|
|
*/
|
2021-02-12 12:01:14 +03:00
|
|
|
|
2020-08-25 17:45:27 +03:00
|
|
|
#include <AK/Assertions.h>
|
2021-03-12 19:29:37 +03:00
|
|
|
#include <AK/Format.h>
|
2021-02-12 12:01:14 +03:00
|
|
|
#include <AK/GenericLexer.h>
|
2019-06-07 12:49:03 +03:00
|
|
|
#include <ctype.h>
|
2018-10-31 19:50:43 +03:00
|
|
|
#include <stdarg.h>
|
2019-06-07 12:49:03 +03:00
|
|
|
#include <stdio.h>
|
2021-02-12 12:01:14 +03:00
|
|
|
#include <stdlib.h>
|
2018-10-31 19:50:43 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-07-04 19:59:29 +03:00
|
|
|
enum class LengthModifier {
|
2021-02-12 12:01:14 +03:00
|
|
|
None,
|
|
|
|
Default,
|
|
|
|
Char,
|
|
|
|
Short,
|
|
|
|
Long,
|
|
|
|
LongLong,
|
|
|
|
IntMax,
|
|
|
|
Size,
|
|
|
|
PtrDiff,
|
|
|
|
LongDouble,
|
|
|
|
};
|
|
|
|
|
2021-07-04 19:59:29 +03:00
|
|
|
enum class ConversionSpecifier {
|
2021-02-12 12:01:14 +03:00
|
|
|
Unspecified,
|
|
|
|
Decimal,
|
|
|
|
Integer,
|
|
|
|
Octal,
|
|
|
|
Unsigned,
|
|
|
|
Hex,
|
|
|
|
Floating,
|
|
|
|
String,
|
|
|
|
UseScanList,
|
|
|
|
Character,
|
|
|
|
Pointer,
|
|
|
|
OutputNumberOfBytes,
|
|
|
|
Invalid,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class ReadKind {
|
|
|
|
Normal,
|
|
|
|
Octal,
|
|
|
|
Hex,
|
|
|
|
Infer,
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename ApT, ReadKind kind = ReadKind::Normal>
|
2021-04-08 13:52:15 +03:00
|
|
|
struct ReadElementConcrete {
|
2021-02-12 12:01:14 +03:00
|
|
|
bool operator()(GenericLexer&, va_list)
|
|
|
|
{
|
|
|
|
return false;
|
2018-10-31 19:50:43 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
};
|
2018-10-31 19:50:43 +03:00
|
|
|
|
2021-02-12 12:01:14 +03:00
|
|
|
template<typename ApT, ReadKind kind>
|
2021-04-08 13:52:15 +03:00
|
|
|
struct ReadElementConcrete<int, ApT, kind> {
|
2022-02-14 08:52:14 +03:00
|
|
|
bool operator()(GenericLexer& lexer, va_list* ap, bool suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
{
|
|
|
|
long value = 0;
|
|
|
|
char* endptr = nullptr;
|
|
|
|
auto nptr = lexer.remaining().characters_without_null_termination();
|
|
|
|
if constexpr (kind == ReadKind::Normal)
|
|
|
|
value = strtol(nptr, &endptr, 10);
|
|
|
|
if constexpr (kind == ReadKind::Octal)
|
|
|
|
value = strtol(nptr, &endptr, 8);
|
|
|
|
if constexpr (kind == ReadKind::Hex)
|
|
|
|
value = strtol(nptr, &endptr, 16);
|
|
|
|
if constexpr (kind == ReadKind::Infer)
|
|
|
|
value = strtol(nptr, &endptr, 0);
|
|
|
|
|
|
|
|
if (!endptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (endptr == nptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto diff = endptr - nptr;
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(diff > 0);
|
2021-02-12 12:01:14 +03:00
|
|
|
lexer.ignore((size_t)diff);
|
|
|
|
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!suppress_assignment) {
|
|
|
|
auto* ptr = va_arg(*ap, ApT*);
|
2021-04-17 22:44:08 +03:00
|
|
|
*ptr = value;
|
2022-02-14 08:52:14 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ApT, ReadKind kind>
|
2021-04-08 13:52:15 +03:00
|
|
|
struct ReadElementConcrete<char, ApT, kind> {
|
2022-02-14 08:52:14 +03:00
|
|
|
bool operator()(GenericLexer& lexer, va_list* ap, bool suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
{
|
|
|
|
static_assert(kind == ReadKind::Normal, "Can't read a non-normal character");
|
|
|
|
|
|
|
|
if (lexer.is_eof())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto ch = lexer.consume();
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!suppress_assignment) {
|
|
|
|
auto* ptr = va_arg(*ap, ApT*);
|
2021-04-17 22:44:08 +03:00
|
|
|
*ptr = ch;
|
2022-02-14 08:52:14 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
return true;
|
2018-10-31 19:50:43 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ApT, ReadKind kind>
|
2021-04-08 13:52:15 +03:00
|
|
|
struct ReadElementConcrete<unsigned, ApT, kind> {
|
2022-02-14 08:52:14 +03:00
|
|
|
bool operator()(GenericLexer& lexer, va_list* ap, bool suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
{
|
|
|
|
unsigned long value = 0;
|
|
|
|
char* endptr = nullptr;
|
|
|
|
auto nptr = lexer.remaining().characters_without_null_termination();
|
|
|
|
if constexpr (kind == ReadKind::Normal)
|
|
|
|
value = strtoul(nptr, &endptr, 10);
|
|
|
|
if constexpr (kind == ReadKind::Octal)
|
|
|
|
value = strtoul(nptr, &endptr, 8);
|
|
|
|
if constexpr (kind == ReadKind::Hex)
|
|
|
|
value = strtoul(nptr, &endptr, 16);
|
|
|
|
if constexpr (kind == ReadKind::Infer)
|
|
|
|
value = strtoul(nptr, &endptr, 0);
|
|
|
|
|
|
|
|
if (!endptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (endptr == nptr)
|
|
|
|
return false;
|
2018-10-31 19:50:43 +03:00
|
|
|
|
2021-02-12 12:01:14 +03:00
|
|
|
auto diff = endptr - nptr;
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(diff > 0);
|
2021-02-12 12:01:14 +03:00
|
|
|
lexer.ignore((size_t)diff);
|
|
|
|
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!suppress_assignment) {
|
|
|
|
auto* ptr = va_arg(*ap, ApT*);
|
2021-04-17 22:44:08 +03:00
|
|
|
*ptr = value;
|
2022-02-14 08:52:14 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
return true;
|
2018-10-31 19:50:43 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
};
|
|
|
|
|
2021-04-04 01:31:43 +03:00
|
|
|
template<typename ApT, ReadKind kind>
|
2021-04-08 13:52:15 +03:00
|
|
|
struct ReadElementConcrete<long long, ApT, kind> {
|
2022-02-14 08:52:14 +03:00
|
|
|
bool operator()(GenericLexer& lexer, va_list* ap, bool suppress_assignment)
|
2021-04-04 01:31:43 +03:00
|
|
|
{
|
|
|
|
long long value = 0;
|
|
|
|
char* endptr = nullptr;
|
|
|
|
auto nptr = lexer.remaining().characters_without_null_termination();
|
|
|
|
if constexpr (kind == ReadKind::Normal)
|
|
|
|
value = strtoll(nptr, &endptr, 10);
|
|
|
|
if constexpr (kind == ReadKind::Octal)
|
|
|
|
value = strtoll(nptr, &endptr, 8);
|
|
|
|
if constexpr (kind == ReadKind::Hex)
|
|
|
|
value = strtoll(nptr, &endptr, 16);
|
|
|
|
if constexpr (kind == ReadKind::Infer)
|
|
|
|
value = strtoll(nptr, &endptr, 0);
|
|
|
|
|
|
|
|
if (!endptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (endptr == nptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto diff = endptr - nptr;
|
|
|
|
VERIFY(diff > 0);
|
|
|
|
lexer.ignore((size_t)diff);
|
|
|
|
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!suppress_assignment) {
|
|
|
|
auto* ptr = va_arg(*ap, ApT*);
|
2021-04-17 22:44:08 +03:00
|
|
|
*ptr = value;
|
2022-02-14 08:52:14 +03:00
|
|
|
}
|
2021-04-04 01:31:43 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-16 12:20:48 +03:00
|
|
|
template<typename ApT, ReadKind kind>
|
2021-04-08 13:52:15 +03:00
|
|
|
struct ReadElementConcrete<unsigned long long, ApT, kind> {
|
2022-02-14 08:52:14 +03:00
|
|
|
bool operator()(GenericLexer& lexer, va_list* ap, bool suppress_assignment)
|
2021-02-16 12:20:48 +03:00
|
|
|
{
|
|
|
|
unsigned long long value = 0;
|
|
|
|
char* endptr = nullptr;
|
|
|
|
auto nptr = lexer.remaining().characters_without_null_termination();
|
|
|
|
if constexpr (kind == ReadKind::Normal)
|
|
|
|
value = strtoull(nptr, &endptr, 10);
|
|
|
|
if constexpr (kind == ReadKind::Octal)
|
|
|
|
value = strtoull(nptr, &endptr, 8);
|
|
|
|
if constexpr (kind == ReadKind::Hex)
|
|
|
|
value = strtoull(nptr, &endptr, 16);
|
|
|
|
if constexpr (kind == ReadKind::Infer)
|
|
|
|
value = strtoull(nptr, &endptr, 0);
|
|
|
|
|
|
|
|
if (!endptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (endptr == nptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto diff = endptr - nptr;
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(diff > 0);
|
2021-02-16 12:20:48 +03:00
|
|
|
lexer.ignore((size_t)diff);
|
|
|
|
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!suppress_assignment) {
|
|
|
|
auto* ptr = va_arg(*ap, ApT*);
|
2021-04-17 22:44:08 +03:00
|
|
|
*ptr = value;
|
2022-02-14 08:52:14 +03:00
|
|
|
}
|
2021-02-16 12:20:48 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-12 12:01:14 +03:00
|
|
|
template<typename ApT, ReadKind kind>
|
2021-04-08 13:52:15 +03:00
|
|
|
struct ReadElementConcrete<float, ApT, kind> {
|
2022-02-14 08:52:14 +03:00
|
|
|
bool operator()(GenericLexer& lexer, va_list* ap, bool suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
{
|
|
|
|
double value = 0;
|
|
|
|
char* endptr = nullptr;
|
|
|
|
auto nptr = lexer.remaining().characters_without_null_termination();
|
|
|
|
if constexpr (kind == ReadKind::Normal)
|
|
|
|
value = strtod(nptr, &endptr);
|
2018-10-31 19:50:43 +03:00
|
|
|
else
|
2021-02-12 12:01:14 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!endptr)
|
|
|
|
return false;
|
2018-10-31 19:50:43 +03:00
|
|
|
|
2021-02-12 12:01:14 +03:00
|
|
|
if (endptr == nptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto diff = endptr - nptr;
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(diff > 0);
|
2021-02-12 12:01:14 +03:00
|
|
|
lexer.ignore((size_t)diff);
|
|
|
|
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!suppress_assignment) {
|
|
|
|
auto* ptr = va_arg(*ap, ApT*);
|
2021-04-17 22:44:08 +03:00
|
|
|
*ptr = value;
|
2022-02-14 08:52:14 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
return true;
|
2018-10-31 19:50:43 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
};
|
2018-10-31 19:50:43 +03:00
|
|
|
|
2021-02-12 12:01:14 +03:00
|
|
|
template<typename T, ReadKind kind>
|
2021-04-08 13:52:15 +03:00
|
|
|
struct ReadElement {
|
2022-02-14 08:52:14 +03:00
|
|
|
bool operator()(LengthModifier length_modifier, GenericLexer& input_lexer, va_list* ap, bool suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
{
|
|
|
|
switch (length_modifier) {
|
|
|
|
default:
|
2021-07-04 19:59:29 +03:00
|
|
|
case LengthModifier::None:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2021-07-04 19:59:29 +03:00
|
|
|
case LengthModifier::Default:
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<T, T, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-07-04 19:59:29 +03:00
|
|
|
case LengthModifier::Char:
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<T, char, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-07-04 19:59:29 +03:00
|
|
|
case LengthModifier::Short:
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<T, short, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-07-04 19:59:29 +03:00
|
|
|
case LengthModifier::Long:
|
2021-04-10 16:59:06 +03:00
|
|
|
if constexpr (IsSame<T, int>)
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<T, long, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-04-10 16:59:06 +03:00
|
|
|
if constexpr (IsSame<T, unsigned>)
|
2022-06-04 21:11:06 +03:00
|
|
|
return ReadElementConcrete<T, unsigned long, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-04-10 16:59:06 +03:00
|
|
|
if constexpr (IsSame<T, float>)
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<int, double, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-02-12 12:01:14 +03:00
|
|
|
return false;
|
2021-07-04 19:59:29 +03:00
|
|
|
case LengthModifier::LongLong:
|
2021-04-10 16:59:06 +03:00
|
|
|
if constexpr (IsSame<T, int>)
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<long long, long long, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-04-10 16:59:06 +03:00
|
|
|
if constexpr (IsSame<T, unsigned>)
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<unsigned long long, unsigned long long, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-04-10 16:59:06 +03:00
|
|
|
if constexpr (IsSame<T, float>)
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<long long, double, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-02-12 12:01:14 +03:00
|
|
|
return false;
|
2021-07-04 19:59:29 +03:00
|
|
|
case LengthModifier::IntMax:
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<T, intmax_t, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-07-04 19:59:29 +03:00
|
|
|
case LengthModifier::Size:
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<T, size_t, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-07-04 19:59:29 +03:00
|
|
|
case LengthModifier::PtrDiff:
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<T, ptrdiff_t, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-07-04 19:59:29 +03:00
|
|
|
case LengthModifier::LongDouble:
|
2022-02-14 08:52:14 +03:00
|
|
|
return ReadElementConcrete<T, long double, kind> {}(input_lexer, ap, suppress_assignment);
|
2021-02-12 12:01:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-10-31 19:50:43 +03:00
|
|
|
|
2021-02-12 12:01:14 +03:00
|
|
|
template<>
|
2021-04-08 13:52:15 +03:00
|
|
|
struct ReadElement<char*, ReadKind::Normal> {
|
|
|
|
ReadElement(StringView scan_set = {}, bool invert = false)
|
2022-07-11 20:32:29 +03:00
|
|
|
: scan_set(scan_set.is_null() ? " \t\n\f\r"sv : scan_set)
|
2021-02-12 12:01:14 +03:00
|
|
|
, invert(scan_set.is_null() ? true : invert)
|
|
|
|
{
|
2018-10-31 19:50:43 +03:00
|
|
|
}
|
|
|
|
|
2022-02-14 08:52:14 +03:00
|
|
|
bool operator()(LengthModifier length_modifier, GenericLexer& input_lexer, va_list* ap, bool suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
{
|
|
|
|
// FIXME: Implement wide strings and such.
|
|
|
|
if (length_modifier != LengthModifier::Default)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto str = input_lexer.consume_while([this](auto c) { return this->matches(c); });
|
|
|
|
if (str.is_empty())
|
|
|
|
return false;
|
|
|
|
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!suppress_assignment) {
|
|
|
|
auto* ptr = va_arg(*ap, char*);
|
|
|
|
memcpy(ptr, str.characters_without_null_termination(), str.length());
|
|
|
|
ptr[str.length()] = 0;
|
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool matches(char c) const
|
|
|
|
{
|
|
|
|
return invert ^ scan_set.contains(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
const StringView scan_set;
|
|
|
|
bool invert { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2021-04-08 13:52:15 +03:00
|
|
|
struct ReadElement<void*, ReadKind::Normal> {
|
2022-02-14 08:52:14 +03:00
|
|
|
bool operator()(LengthModifier length_modifier, GenericLexer& input_lexer, va_list* ap, bool suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
{
|
|
|
|
if (length_modifier != LengthModifier::Default)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto str = input_lexer.consume_while([this](auto c) { return this->should_consume(c); });
|
|
|
|
|
|
|
|
if (count != 8) {
|
|
|
|
fail:;
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
input_lexer.retreat();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[9] { 0 };
|
|
|
|
memcpy(buf, str.characters_without_null_termination(), 8);
|
|
|
|
buf[8] = 0;
|
|
|
|
char* endptr = nullptr;
|
|
|
|
auto value = strtoull(buf, &endptr, 16);
|
|
|
|
|
|
|
|
if (endptr != &buf[8])
|
|
|
|
goto fail;
|
|
|
|
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!suppress_assignment) {
|
|
|
|
auto* ptr = va_arg(*ap, void**);
|
|
|
|
memcpy(ptr, &value, sizeof(value));
|
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool should_consume(char c)
|
|
|
|
{
|
|
|
|
if (count == 8)
|
|
|
|
return false;
|
|
|
|
if (!isxdigit(c))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++count;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
size_t count { 0 };
|
|
|
|
};
|
2018-10-31 19:50:43 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
extern "C" int vsscanf(char const* input, char const* format, va_list ap)
|
2018-10-31 19:50:43 +03:00
|
|
|
{
|
2022-07-11 22:53:29 +03:00
|
|
|
GenericLexer format_lexer { { format, strlen(format) } };
|
|
|
|
GenericLexer input_lexer { { input, strlen(input) } };
|
2021-02-12 12:01:14 +03:00
|
|
|
|
|
|
|
int elements_matched = 0;
|
|
|
|
|
2021-07-16 23:01:35 +03:00
|
|
|
va_list copy;
|
|
|
|
__builtin_va_copy(copy, ap);
|
|
|
|
|
2021-02-12 12:01:14 +03:00
|
|
|
while (!format_lexer.is_eof()) {
|
2022-09-06 11:35:40 +03:00
|
|
|
if (format_lexer.next_is(isspace)) {
|
|
|
|
format_lexer.ignore_while(isspace);
|
|
|
|
input_lexer.ignore_while(isspace);
|
|
|
|
}
|
|
|
|
|
2021-02-12 12:01:14 +03:00
|
|
|
if (!format_lexer.next_is('%')) {
|
|
|
|
read_one_literal:;
|
|
|
|
if (format_lexer.is_eof())
|
|
|
|
break;
|
|
|
|
|
|
|
|
auto next_char = format_lexer.consume();
|
|
|
|
if (!input_lexer.consume_specific(next_char))
|
|
|
|
return elements_matched;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format_lexer.next_is("%%")) {
|
|
|
|
format_lexer.ignore();
|
|
|
|
goto read_one_literal;
|
|
|
|
}
|
|
|
|
|
|
|
|
format_lexer.ignore(); // '%'
|
|
|
|
|
2021-04-17 22:44:08 +03:00
|
|
|
bool suppress_assignment = false;
|
|
|
|
if (format_lexer.next_is('*')) {
|
|
|
|
suppress_assignment = true;
|
|
|
|
format_lexer.ignore();
|
|
|
|
}
|
|
|
|
|
2021-04-04 03:01:24 +03:00
|
|
|
// Parse width specification
|
|
|
|
[[maybe_unused]] int width_specifier = 0;
|
|
|
|
if (format_lexer.next_is(isdigit)) {
|
|
|
|
auto width_digits = format_lexer.consume_while([](char c) { return isdigit(c); });
|
2023-12-23 05:59:14 +03:00
|
|
|
width_specifier = width_digits.to_number<int>().value();
|
2021-04-04 03:01:24 +03:00
|
|
|
// FIXME: Actually use width specifier
|
|
|
|
}
|
|
|
|
|
2021-02-12 12:01:14 +03:00
|
|
|
bool invert_scanlist = false;
|
|
|
|
StringView scanlist;
|
2021-07-04 19:59:29 +03:00
|
|
|
LengthModifier length_modifier { LengthModifier::None };
|
|
|
|
ConversionSpecifier conversion_specifier { ConversionSpecifier::Unspecified };
|
2021-02-12 12:01:14 +03:00
|
|
|
reread_lookahead:;
|
|
|
|
auto format_lookahead = format_lexer.peek();
|
2021-07-04 19:59:29 +03:00
|
|
|
if (length_modifier == LengthModifier::None) {
|
2021-02-12 12:01:14 +03:00
|
|
|
switch (format_lookahead) {
|
|
|
|
case 'h':
|
|
|
|
if (format_lexer.peek(1) == 'h') {
|
|
|
|
format_lexer.consume(2);
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::Char;
|
2021-02-12 12:01:14 +03:00
|
|
|
} else {
|
|
|
|
format_lexer.consume(1);
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::Short;
|
2018-10-31 19:50:43 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (format_lexer.peek(1) == 'l') {
|
|
|
|
format_lexer.consume(2);
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::LongLong;
|
2021-02-12 12:01:14 +03:00
|
|
|
} else {
|
|
|
|
format_lexer.consume(1);
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::Long;
|
2019-03-20 17:29:04 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::IntMax;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::Size;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::PtrDiff;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::LongDouble;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
default:
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::Default;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
2018-10-31 19:50:43 +03:00
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
goto reread_lookahead;
|
|
|
|
}
|
2021-07-04 19:59:29 +03:00
|
|
|
if (conversion_specifier == ConversionSpecifier::Unspecified) {
|
2021-02-12 12:01:14 +03:00
|
|
|
switch (format_lookahead) {
|
|
|
|
case 'd':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::Decimal;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::Integer;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::Octal;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::Unsigned;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'x':
|
2022-01-01 13:11:02 +03:00
|
|
|
case 'X':
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::Hex;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
case 'e':
|
|
|
|
case 'f':
|
|
|
|
case 'g':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::Floating;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::String;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
format_lexer.consume();
|
|
|
|
scanlist = format_lexer.consume_until(']');
|
2022-01-25 00:47:22 +03:00
|
|
|
format_lexer.ignore();
|
2021-02-12 12:01:14 +03:00
|
|
|
if (scanlist.starts_with('^')) {
|
|
|
|
scanlist = scanlist.substring_view(1);
|
|
|
|
invert_scanlist = true;
|
|
|
|
}
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::UseScanList;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::Character;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::Pointer;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::OutputNumberOfBytes;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::Long;
|
|
|
|
conversion_specifier = ConversionSpecifier::Character;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
length_modifier = LengthModifier::Long;
|
|
|
|
conversion_specifier = ConversionSpecifier::String;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
format_lexer.consume();
|
2021-07-04 19:59:29 +03:00
|
|
|
conversion_specifier = ConversionSpecifier::Invalid;
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
2018-10-31 19:50:43 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
|
|
|
|
// Now try to read.
|
|
|
|
switch (conversion_specifier) {
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::Invalid:
|
|
|
|
case ConversionSpecifier::Unspecified:
|
2021-02-12 12:01:14 +03:00
|
|
|
default:
|
2021-09-07 13:56:50 +03:00
|
|
|
// "undefined behavior", let's be nice and crash.
|
2021-02-12 12:01:14 +03:00
|
|
|
dbgln("Invalid conversion specifier {} in scanf!", (int)conversion_specifier);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::Decimal:
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!ReadElement<int, ReadKind::Normal> {}(length_modifier, input_lexer, ©, suppress_assignment))
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume_all();
|
2022-02-14 09:04:01 +03:00
|
|
|
else if (!suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
++elements_matched;
|
|
|
|
break;
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::Integer:
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!ReadElement<int, ReadKind::Infer> {}(length_modifier, input_lexer, ©, suppress_assignment))
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume_all();
|
2022-02-14 09:04:01 +03:00
|
|
|
else if (!suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
++elements_matched;
|
|
|
|
break;
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::Octal:
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!ReadElement<unsigned, ReadKind::Octal> {}(length_modifier, input_lexer, ©, suppress_assignment))
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume_all();
|
2022-02-14 09:04:01 +03:00
|
|
|
else if (!suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
++elements_matched;
|
|
|
|
break;
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::Unsigned:
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!ReadElement<unsigned, ReadKind::Normal> {}(length_modifier, input_lexer, ©, suppress_assignment))
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume_all();
|
2022-02-14 09:04:01 +03:00
|
|
|
else if (!suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
++elements_matched;
|
|
|
|
break;
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::Hex:
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!ReadElement<unsigned, ReadKind::Hex> {}(length_modifier, input_lexer, ©, suppress_assignment))
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume_all();
|
2022-02-14 09:04:01 +03:00
|
|
|
else if (!suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
++elements_matched;
|
|
|
|
break;
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::Floating:
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!ReadElement<float, ReadKind::Normal> {}(length_modifier, input_lexer, ©, suppress_assignment))
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume_all();
|
2022-02-14 09:04:01 +03:00
|
|
|
else if (!suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
++elements_matched;
|
|
|
|
break;
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::String:
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!ReadElement<char*, ReadKind::Normal> {}(length_modifier, input_lexer, ©, suppress_assignment))
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume_all();
|
2022-02-14 09:04:01 +03:00
|
|
|
else if (!suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
++elements_matched;
|
|
|
|
break;
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::UseScanList:
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!ReadElement<char*, ReadKind::Normal> { scanlist, invert_scanlist }(length_modifier, input_lexer, ©, suppress_assignment))
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume_all();
|
2022-02-14 09:04:01 +03:00
|
|
|
else if (!suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
++elements_matched;
|
|
|
|
break;
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::Character:
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!ReadElement<char, ReadKind::Normal> {}(length_modifier, input_lexer, ©, suppress_assignment))
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume_all();
|
2022-02-14 09:04:01 +03:00
|
|
|
else if (!suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
++elements_matched;
|
|
|
|
break;
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::Pointer:
|
2022-02-14 08:52:14 +03:00
|
|
|
if (!ReadElement<void*, ReadKind::Normal> {}(length_modifier, input_lexer, ©, suppress_assignment))
|
2021-02-12 12:01:14 +03:00
|
|
|
format_lexer.consume_all();
|
2022-02-14 09:04:01 +03:00
|
|
|
else if (!suppress_assignment)
|
2021-02-12 12:01:14 +03:00
|
|
|
++elements_matched;
|
|
|
|
break;
|
2021-07-04 19:59:29 +03:00
|
|
|
case ConversionSpecifier::OutputNumberOfBytes: {
|
2021-04-17 22:44:08 +03:00
|
|
|
if (!suppress_assignment) {
|
2021-10-25 01:23:49 +03:00
|
|
|
auto* ptr = va_arg(copy, int*);
|
2021-04-17 22:44:08 +03:00
|
|
|
*ptr = input_lexer.tell();
|
|
|
|
}
|
2021-02-12 12:01:14 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 19:50:43 +03:00
|
|
|
}
|
2021-07-16 23:01:35 +03:00
|
|
|
va_end(copy);
|
2021-02-12 12:01:14 +03:00
|
|
|
|
|
|
|
return elements_matched;
|
2018-10-31 19:50:43 +03:00
|
|
|
}
|