Skip to content

Commit 0f1e82a

Browse files
committed
doc update
1 parent 1b2a75a commit 0f1e82a

File tree

2 files changed

+103
-27
lines changed

2 files changed

+103
-27
lines changed

src/mmfatl.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
* -DINLINE=inline -DTEST_ENABLE -c -o mmfatl.o ../src/mmfatl.c
1818
*
1919
* This should not produce an error or a warning.
20+
*
21+
* If you have Doxygen installed the parameter -d of the build.sh script
22+
* generates a hyperlinked documentation from the comments both here and in the
23+
* header file.
2024
*/
2125

2226
#include <limits.h>
@@ -91,9 +95,11 @@ struct Buffer {
9195
static struct Buffer buffer;
9296

9397
/*!
98+
* \brief initilize and empty the message buffer.
99+
*
94100
* We do not rely on any initialization during program start. Instead we
95101
* assume the worst case, a corrupted pointer overwrote the buffer. So we
96-
* initialize it again immediately before use.
102+
* initialize it immediately before use.
97103
* \pre the ellipsis appended to the writeable portion of the buffer must
98104
* terminate with a LF, so printing a buffer in overflow state will keep
99105
* a command prompt in a new line after program exit.
@@ -111,6 +117,8 @@ static void initBuffer(struct Buffer* buffer) {
111117
}
112118

113119
/*!
120+
* \brief checking the message buffer for emptiness
121+
*
114122
* \param buffer [const, not null] the buffer to check for emptiness.
115123
* \return true, iff the \ref buffer is in its initial state.
116124
* \pre \ref initBuffer was called
@@ -120,6 +128,8 @@ inline static bool isBufferEmpty(struct Buffer const* buffer) {
120128
}
121129

122130
/*!
131+
* \brief last character in the message buffer
132+
*
123133
* Get the last stored character in the buffer. Discarded characters due to
124134
* overflow are ignored. A returned NUL indicates the buffer is empty.
125135
* \param buffer [const, not null] the buffer to investigate.
@@ -131,6 +141,8 @@ static char getLastBufferedChar(struct Buffer const* buffer) {
131141
}
132142

133143
/*!
144+
* \brief check whether the buffer is overflown
145+
*
134146
* \param buffer [const, not null] the buffer to check for overflow.
135147
* \return true, iff the current contents exceeds the capacity of the
136148
* \ref buffer, so at least the terminating \ref NUL is cut off, maybe more.
@@ -141,6 +153,8 @@ inline static bool isBufferOverflow(struct Buffer const* buffer) {
141153
}
142154

143155
/*!
156+
* \brief modes to append text to the message buffer
157+
*
144158
* used to indicate whether \ref MMFATL_PH_PREFIX is a normal character, or
145159
* an escape character in a format string.
146160
*/
@@ -150,6 +164,8 @@ enum SourceType {
150164
};
151165

152166
/*!
167+
* \brief append text to the current contents of the message buffer
168+
*
153169
* append characters to the current end of the buffer from a string until a
154170
* terminating \ref NUL, or optionally a placeholder is encountered, or the
155171
* buffer overflows.
@@ -170,6 +186,8 @@ static unsigned appendText(char const* source, enum SourceType type,
170186
}
171187

172188
/*!
189+
* \brief state of the parser scanning a formatted message in printf style
190+
*
173191
* A simple grammar scheme allows inserting data in a prepared general message.
174192
* The scheme is a downgrade of the C format string. Allowed are only
175193
* placeholders %s and %u that are replaced with given data. The percent sign
@@ -211,6 +229,8 @@ struct ParserState {
211229
static struct ParserState state;
212230

213231
/*!
232+
* \brief initializes the parser state (but not the associated message buffer!)
233+
*
214234
* initializes \ref state.
215235
* \post establish the invariant in state
216236
* \param state [not null] the struct \ref ParserState to initialize.
@@ -224,6 +244,8 @@ static void initState(struct ParserState* state, struct Buffer* buffer) {
224244
}
225245

226246
/*!
247+
* \brief converting an unsigned to a string of decimal numbers
248+
*
227249
* converts an unsigned to a sequence of decimal digits representing its value.
228250
* The value range is known to be at least 2**32 on contemporary hardware, but
229251
* C99 guarantees just 2**16. We support unsigned in formatted error output
@@ -260,7 +282,10 @@ static char const* unsignedToString(unsigned value) {
260282
return digits + ofs;
261283
}
262284

263-
/*! reflect a possible buffer overflow in the parser state
285+
/*!
286+
* \brief update the parser state in case of message buffer overflow
287+
*
288+
* reflect a possible buffer overflow in the parser state
264289
* \param state [not null] ParserState object being updated in case of
265290
* overflow
266291
* \return false in case of overflow
@@ -275,6 +300,8 @@ static bool checkOverflow(struct ParserState* state) {
275300
}
276301

277302
/*!
303+
* \brief copy a portion of text verbatim to the message buffer
304+
*
278305
* copy text verbatim from a format string to the message buffer, until either
279306
* the format ends, or a placeholder is encountered.
280307
* \param state struct ParserState* parser state going to be handled and updated
@@ -290,6 +317,8 @@ static void handleText(struct ParserState* state) {
290317
}
291318

292319
/*!
320+
* \brief handle a placeholder in a formatted message
321+
*
293322
* A format specifier is a two character combination, where a placeholder
294323
* character \ref MMFATL_PH_PREFIX is followed by an alphabetic character
295324
* designating a type. A placeholder is substituted by the next argument in
@@ -342,6 +371,8 @@ static void handleSubstitution(struct ParserState* state) {
342371
}
343372

344373
/*!
374+
* \brief convert a formatted message to human readable text
375+
*
345376
* parses the submitted format string, replacing each placeholder with one of
346377
* the values in member args of \ref state, and appends the result to the
347378
* current contents of \ref buffer.
@@ -386,6 +417,8 @@ char const* getFatalErrorPlaceholderToken(
386417
}
387418

388419
/*!
420+
* \brief get the message buffer instance
421+
*
389422
* gets the instance of Buffer to use with this interface (currently a global
390423
* singleton). The returned instance is not guaranteed to be initialized.
391424
* \return [not null] a pointer to the Buffer instance
@@ -395,6 +428,8 @@ inline static struct Buffer* getBufferInstance(void) {
395428
}
396429

397430
/*!
431+
* \brief get the parser state instance
432+
*
398433
* gets the instance of ParserState to use with this interface (currently a
399434
* global singleton). The returned instance is not guaranteed to be
400435
* initialized.

src/mmfatl.h

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
* \file mmfatl.h
1515
* \brief supports generating of fatal error messages
1616
*
17-
* part of the application's infrastructure
17+
* In a sort of 3-tier architecture consisting of resource/configuration/data
18+
* management, operation logic and algorithms, and presentation layer (or user
19+
* interface), this code is interfacing the operating system on the
20+
* lowest infrastructure (resource) layer.
1821
*
1922
* Rationale
2023
* =========
2124
*
2225
* When a fatal error occurs, the internal structures of a program may be
2326
* corrupted to the point that recovery is impossible. The program exits
24-
* immediately, but hopefully still displays a diagnostic message.
27+
* immediately with a failure code, but hopefully still displays a diagnostic
28+
* message.
2529
*
2630
* To display this final message, we restrict its code to very basic,
2731
* self-contained routines, independent of the rest of the program to the
@@ -31,7 +35,7 @@
3135
* in a corrupted or memory-tight environment is minimized. This is to the
3236
* detriment of flexibility, in particular, support for dynamic behavior is
3337
* limited. Many Standard C library functions like printf MUST NOT be called
34-
* when heap problems arise, since they use it internally. GNU tags such
38+
* when heap problems arise, since they rely on it internally. GNU tags such
3539
* functions as 'AS-Unsafe heap' in their documentation (libc.pdf).
3640
*
3741
* Often it is sensible to embed details in a diagnosis message. Placeholders
@@ -50,22 +54,30 @@
5054
/*** Export basic features of the fatal error message processing ***/
5155

5256

53-
/*! the size a fatal error message including the terminating NUL character can
57+
/*!
58+
* \brief size of a text buffer used to construct a message
59+
*
60+
* the size a fatal error message including the terminating NUL character can
5461
* assume without truncation. Must be in the range of an int.
5562
*/
5663
enum {
5764
MMFATL_MAX_MSG_SIZE = 1024,
5865
};
5966

60-
/*! the character sequence appended to a truncated fatal error message due to
61-
* a buffer overflow, so its reader is aware a displayed text is incomplete.
62-
* The ellipsis is followed by a line feed to ensure even an overflown message
63-
* ends with one. This is to separate a message from the command prompt
64-
* following an exit.
67+
/*!
68+
* \brief ASCII text sequence indicating truncated text
69+
*
70+
* the character sequence appended to a truncated fatal error message due to a
71+
* buffer overflow, so its reader is aware a displayed text is incomplete. The
72+
* ellipsis is followed by a line feed to ensure even an overflown message ends
73+
* with one. This is to separate a message from the command prompt following
74+
* an exit.
6575
*/
6676
#define MMFATL_ELLIPSIS "...\n"
6777

6878
/*!
79+
* \brief ASCII characters used for placeholder tokens, printf style
80+
*
6981
* supported value types of a two character placeholder token in a format
7082
* string. The first character of a placeholder is always an escape
7183
* character \ref MMFATL_PH_PREFIX, followed by one of the type characters
@@ -79,7 +91,7 @@ enum {
7991
* in this particular case.
8092
*/
8193
enum fatalErrorPlaceholderType {
82-
//! escape character marking a placeholder
94+
//! escape character marking a placeholder, followed by a type character
8395
MMFATL_PH_PREFIX = '%',
8496
//! type character marking a placeholder for a string
8597
MMFATL_PH_STRING = 's',
@@ -152,13 +164,13 @@ extern void fatalErrorInit(void);
152164
* correct placeholder token.
153165
*
154166
* NULL is equivalent to an empty format string, and supported both as a
155-
* \ref format string and as a parameter for a string placeholder, to
167+
* \p format string and as a parameter for a string placeholder, to
156168
* enhance robustness.
157169
*
158170
* It is recommended to let the message end with a LF character, so a command
159171
* prompt following it is displayed on a new line. If it is missing
160-
* \ref fatalErrorPrintAndExit will supply one, but relying on this adds to
161-
* unnecessary steps under severe conditions.
172+
* \ref fatalErrorPrintAndExit will supply one, but relying on this adds an
173+
* unnecessary correction under severe conditions.
162174
*
163175
*
164176
* The \p format is followed by a possibly empty list of paramaters substituted
@@ -190,7 +202,21 @@ extern bool fatalErrorPush(char const* format, ...);
190202
* \brief display buffer contents and exit program with code EXIT_FAILURE.
191203
*
192204
* This function does not return.
193-
*
205+
*
206+
* A NUL terminated message has been prepared in an internal buffer using a
207+
* sequence of \ref fatalErrorPush. This function writes this message to
208+
* stderr (by default tied to the terminal screen like stdout), and exits the
209+
* program afterwards, indicating a failure to the operating system.
210+
*
211+
* A line feed is appended to a prepared non-empty message if it is not its
212+
* last character. This keeps the message and a command prompt following the
213+
* exit on separare lines. It is recommended to avoid this corrective
214+
* measure and supply a terminating line feed as part of the message.
215+
*
216+
* It is possible to call this function without preparing a message. In this
217+
* case nothing is written to stderr, and the program just exits with a failure
218+
* code.
219+
*
194220
* \pre \ref fatalErrorInit has initialized the internal error message
195221
* buffer, possibly followed by a sequence of \ref fatalErrorPush
196222
* filling it with a message.
@@ -203,28 +229,43 @@ extern bool fatalErrorPush(char const* format, ...);
203229
* stderr to, say, a log file, the error message is displayed to the user on
204230
* his terminal.
205231
* \warning previous versions of Metamath returned the exit code 1. Many
206-
* implementations define EXIT_FAILURE to this very value, but that is not
207-
* mandated by the C11 standard. In fact, some systems may interpret 1 as a
208-
* success code, so EXIT_FAILURE is more appropriate.
232+
* systemss define EXIT_FAILURE to this very value, but that is not mandated
233+
* by the C11 standard. In fact, some systems may interpret 1 as a success
234+
* code, so EXIT_FAILURE is more appropriate.
209235
*/
210236
extern void fatalErrorPrintAndExit(void);
211237

212238
/*!
213-
* convenience function, covering a sequence of \ref fatalErrorInit,
239+
* \brief standard error reporting and program exit with failure code.
240+
*
241+
* Convenience function, covering a sequence of \ref fatalErrorInit,
214242
* \ref fatalErrorPush and \ref fatalErrorPrintAndExit in succession. This
215243
* function does not return.
216244
*
245+
* If an error location is given, it is printed first, followed by any
246+
* non-empty message. A line feed is padded to the right of the message if it
247+
* is missing. This is to keep a following command prompt on a new line. It
248+
* is recommended to avoid this corrective measure and supply a line feed as
249+
* part of the message.
250+
*
251+
* If all parameters are NULL or 0, no message is printed, not even the
252+
* automatically supplied line feed, and this function just exits the program
253+
* with an error code. If possible use \ref fatalErrorPrintAndExit directly
254+
* instead.
255+
*
217256
* \param file [null] filename of code responsible for calling this function,
218-
* suitable for macro __FILE__. Part of an error location.
257+
* suitable for macro __FILE__. Part of an error location. Ignored in case
258+
* of NULL.
219259
* \param line [unsigned] if greater 0, interpreted as a line number, where
220260
* a call to this function is initiated, suitable for macro __LINE__.
221261
* Part of an error location.
222-
* \param msgWithPlaceholders the error message to display. This message
223-
* may include placeholders, in which case it must be followed by more
224-
* parameters, corresponding to the values to replace the placeholders.
225-
* These values must match in type that of the placeholders, and their
226-
* number must be enough (can be more) to cover all placeholders. The
227-
* details of this process is explained in \ref fatalErrorPush.
262+
* \param msgWithPlaceholders [null] the error message to display. This
263+
* message may include placeholders in printf style, in which case it must be
264+
* followed by more parameters, corresponding to the values replacing
265+
* placeholders. These values must match in type that of the placeholders,
266+
* and their number must be enough (can be more) to cover all placeholders.
267+
* The details of this process is explained in \ref fatalErrorPush. Ignored
268+
* if NULL.
228269
* \post the program exits with EXIT_FAILURE return code, after writing the
229270
* error location and message to stderr.
230271
*/

0 commit comments

Comments
 (0)