Commit Graph

40 Commits

Author SHA1 Message Date
Alex Elder
8fc49e139b parser: print program name with error messages
In yyerror(), prefix the line reporting the error with the program
name.  This makes the output consistent with the output of errx()
which reports memory allocation errors.

Note that this uses a GNU-only feature; if that's not desirable it's
easy to do this more portably.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-18-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:24:04 -05:00
Alex Elder
32c0632957 parser: introduce memalloc() macro
Create a new macro that allocates and zeroes a block of memory,
which guarantees that the allocation will succeed.  Use this in two
spots where calloc() is already assumed not to fail, and use it in
other places where memory is dynamically allocated.

If the malloc() call in the macro fails, memalloc() will call errx()
to print an error message to stderr and exit with status 1.

In addition, check for a null pointer returned by strdup() in
yylex(), and report a similar error if that occurs.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-17-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:23:10 -05:00
Alex Elder
dcc2397921 parser: use stderr for error messages
Send error text output by yyerror() to stderr instead of stdout.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-16-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:22:33 -05:00
Alex Elder
dcec18722a parser: use token_expect() instead of token_accept()
If parse_package() finds anything other than an identifier as the
next token, it calls yyerror().  This is exactly what token_expect()
does, so use that instead.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-15-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:22:21 -05:00
Alex Elder
f80136eb08 parser: use standard I/O for input buffering
Rather than maintaining an input buffer and details about its
current state--as well as a lookahead character--just let the
standard I/O library take care of those details.  A single character
of "lookahead" is guaranteed by the library, so this will be no
worse than the hand-done buffering, and will certainly be more
robust.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-14-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:21:48 -05:00
Alex Elder
c7c8e47f74 parser: only one lookahead byte is required
The scratch_buf[] array is used to hold characters supplied to
unput().

The only time unput() is called is in yylex(), when a character
returned by input() isn't in the character set appropriate for the
symbol being parsed.  And in that case unput() is called only once.

We will not call unput() again until input() has been called at
least once, and that will consume the only character in the scratch
buffer.

Therefore, for our purposes, only one character is required for the
lookahead buffer.

We never accept a NUL byte on input, so it will never be used as a
lookahead character.  So we can use 0 as a special lookahead value
that indicates "no lookahead present."

So replace the scratch_buf[] and scratch_pos with a single character
lookahead, which is considered invalid if its value is 0.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-13-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:21:00 -05:00
Alex Elder
fad43b91d2 parser: use CHAR_MAX to define non-ASCII token id values
The first token id is given the value 256.  This ensures it is
outside the range of any valid character.

But we don't allow non-ASCII characters on input, so we can use 128
as the first non-character token id value.  Represent this as
(CHAR_MAX + 1), to make explicit the reason this value is chosen.

Give the enumerated type representing token ids a name.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-12-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:20:00 -05:00
Alex Elder
b246e6d86f parser: introduce TOK_EOF
Define an explicit end-of-file token, TOK_EOF.  When EOF is reached,
input() returns 0.  Use a symbolic TOK_EOF value rather than 0 to
represent that condition.

Note that also implies '\0' is not a valid input character; add an
explicit check to disallow a NUL byte on input.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-11-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:19:24 -05:00
Alex Elder
e8fd1bafda parser: permit only ASCII characters in input
Report an error if any input byte is not an ASCII character.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-10-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:18:42 -05:00
Alex Elder
6b15855aad parser: make input() return char
Now that read errors are dealt with immediately, input() will never
return anything but a character.  Change its return type to char,
and change the type of the variable in yylex() that holds its return
value to char as well.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-9-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:18:31 -05:00
Alex Elder
28c48a5e18 parser: report input read errors
In qmi_parse(), the main loop accepts tokens until it sees one with
id 0.  If an unrecognized (or unexpected) token is encountered, it
reports an error and exits using yyerror().

Normally, input() returns the next character to be processed while
parsing.  If the read() call in input() encounters an error, input()
will return -1 instead (which happens to be EOF).

The only caller of input() is yylex().  It recognizes tokens
comprised of alphanumeric characters and '_'.  If input() returns
any other value, it uses that value as the token identifier.  And in
particular, if input() returns -1, that will be the value stored in
the id field of the token structure yylex() returns.

So, back to the top, qmi_parse() will not consider -1 a valid token
id, and will report the read error as an "unexpected symbol".

Instead, report a read error immediately when it occurs and exit.
Move the definition of yyerror() earlier in the source file so it
can be called from yylex() (and all other functions) without needing
a declaration.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-8-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:17:46 -05:00
Alex Elder
53dc3278af parser: free unused message token strings
In qmi_parse(), when a message token is recognized its token
structure will contain a dynamically-allocated string continaing the
message name.  We only use the message identifier (number) though,
and as a result the message name gets leaked.  Fix this by freeing
the type token string if no-null.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-7-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:16:23 -05:00
Alex Elder
61da0f8fda parser: free unused type token strings
In qmi_message_parse(), the type token for each field parsed will
contain a dynamically-allocated string continaing the field type.
We only use the type identifier (number) though, and as a result
the type name gets leaked.  Fix this by freeing the type token
string if non-null.

Essentially the same thing occurs in qmi_struct_parse() while
parsing the fields of a message.  Fix the problem there as well.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-6-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:15:25 -05:00
Alex Elder
405e4e2b01 parser: free unused token strings in token_accept()
If token_accept() is provided a null token pointer argument, the
previous "current" token is discarded.  If the token matched is a
symbol type, the token string (which will have been dynamically
allocated in yylex()) will be leaked.

Fix this by freeing the current token string if token pointer passed
is null.

The compiler warns when we attempt to free a pointer to constant
data, so change the type of the string pointer in the symbol
structure to be pointer to non-constant.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-5-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:13:43 -05:00
Alex Elder
23710a79b3 parser: don't bother with a buffer in yyerror()
Just print the message passed to yyerror(); don't bother putting it
into a buffer first.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-4-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:11:56 -05:00
Alex Elder
8dcad17a55 parser: use unsigned for array indexes
We have no use for negative array indexes, so change the types of
the static variables scratch_pos, input_pos, and input_len to be
unsigned.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-3-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:11:26 -05:00
Alex Elder
0d92483f4a parser: get rid of a pointless loop in yylex()
A loop at the top of yylex() traverses the symbols list without
doing anything.  It serves no purpose, so get rid of it.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-2-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2021-10-04 12:11:10 -05:00
Bjorn Andersson
815dd495eb kernel: Support having strings in structs
Add handling for strings in structs in the kernel-style code generator.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-06-29 14:12:50 -07:00
Khem Raj
4ad63502c5 Makefile: Allow compiler/linker flags to be overridden
This helps with cross compilation where toolchains specify
certain flags via environment e.g. CFLAGS/LDFLAGS

Signed-off-by: Khem Raj <raj.khem@gmail.com>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-03-05 21:54:21 -08:00
Bjorn Andersson
3f62f9ba1e kernel: Support static arrays
Statically sized arrays should not have a len property and should be
marked as STATIC_ARRAY in the elem_info list.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-02-15 12:46:19 -08:00
Bjorn Andersson
8e5daab4a6 kernel: Introduce kernel-style generator
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-01-31 13:00:02 -08:00
Bjorn Andersson
06a2c5a19b accessor: Move accessor generators to one file
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-01-30 16:54:13 -08:00
Bjorn Andersson
a8c34af751 qmic: Extract all generators into functions
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-01-30 16:42:22 -08:00
Bjorn Andersson
6f19b29065 parser: Tidy up parser after move
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-01-30 16:18:13 -08:00
Bjorn Andersson
7e7d2a2a17 parser: Move struct parser to parser.c
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-01-30 16:13:06 -08:00
Bjorn Andersson
63f0bedbda parser: Move message parsing to parser.c
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-01-30 16:10:54 -08:00
Bjorn Andersson
a3aa10545b qmic: Move simple type array to common file
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-01-30 16:06:33 -08:00
Bjorn Andersson
eaa0f3b8a5 parser: Move parser code from qmic.c to parser.c
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-01-30 16:03:50 -08:00
Bjorn Andersson
4c693b7551 qmic: Adopt common list implementation
Use the list implementation from other projects instead of rolling
custom list operations throughout the codebase.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-01-30 15:55:49 -08:00
Bjorn Andersson
b4e899fffc qmi: Annotate yyerror noreturn
The yyerror() doesn't return, so let the compiler know this, in order to
silence warnings about potentially uninitialized variables.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2018-01-30 12:09:42 -08:00
Bjorn Andersson
1c036374ea Merge pull request #1 from ndechesne/gnu
Makefile: implement GNU Coding Standard for Makefiles
2016-06-06 08:40:23 -07:00
Nicolas Dechesne
d42cf61564 Makefile: implement GNU Coding Standard for Makefiles
GNU coding standards notably specifies:
* install files with the $(DESTDIR) to the target system image
* install files with the $(prefix), not $(PREFIX)
* the default value of $(prefix) should be /usr/local

as per https://www.gnu.org/prep/standards/html_node/Directory-Variables.html.

Signed-off-by: Nicolas Dechesne <nicolas.dechesne@linaro.org>
2016-06-05 22:00:13 +02:00
Bjorn Andersson
62f4c6a799 qmic: Update makefile for packaging
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2016-06-03 15:14:13 -07:00
Bjorn Andersson
08d9f3694b qmic: Add .gitignore
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2016-06-02 10:27:42 -07:00
Bjorn Andersson
7b9c51d078 qmic: Add the missing LICENSE file
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2016-06-02 10:26:43 -07:00
Bjorn Andersson
1ef360e6ae qmic: Add missing string type in structs
Structs can contain strings, we still don't encode and decode these but
this change makes the generated header file compilable.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2016-06-02 10:25:01 -07:00
Bjorn Andersson
0b09df395a qmic: Correct qmi_message string accessor
The accessor function generator for strings in qmi messages output code
that is incorrect, fix this.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2016-05-10 13:25:51 -07:00
Bjorn Andersson
9d4a317d4f qmic: Support specifying request, response or indication type
Allow specifying message type for the tlv support functions to verify
and create the right type of qmi header.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2016-02-07 21:24:02 -08:00
Bjorn Andersson
241fff829e qmic: Initial basic implementation
This initial implementation is capable of generating encoder and decoder
accessors for messages with basic integers, strings, arrays and structs.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
2016-02-07 09:27:50 -08:00
Bjorn Andersson
739fbdc423 Initial commit for qmic 2016-02-07 09:25:31 -08:00