>From 41d06302c3f279bbe8ba7a52bbbe9d608a073797 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?"Rodolfo=20Garc=C3=ADa=20Pe=C3=B1as=20(kix)"?= <[email protected]>
Date: Fri, 20 Jul 2012 10:41:30 +0200
Subject: [PATCH 2/3] menuparser_macros code clean
This patch do this changes:
1. Change (add/remove) blank lines
2. Join some if's in only one
3. Change c++ comments to c comments (// -> /* */)
4. Add / remove curly brackets if needed
5. Change spaces (add/remove) & set correct indentation
---
WINGs/menuparser_macros.c | 243 ++++++++++++++++++++++++++++-----------------
1 file changed, 153 insertions(+), 90 deletions(-)
diff --git a/WINGs/menuparser_macros.c b/WINGs/menuparser_macros.c
index bada620..b18c2ab 100644
--- a/WINGs/menuparser_macros.c
+++ b/WINGs/menuparser_macros.c
@@ -101,6 +101,7 @@ void menu_parser_free_macros(WMenuParser parser)
printf(" %s", macro->name);
if (macro->arg_count >= 0)
printf("(args=%d)", macro->arg_count);
+
printf(" = ");
if (macro->function != NULL) {
@@ -118,6 +119,7 @@ void menu_parser_free_macros(WMenuParser parser)
printf(" #%d ", (*rd++) + 1);
}
}
+
printf(", used %d times\n", macro->usage_count);
count++;
#endif
@@ -127,15 +129,15 @@ void menu_parser_free_macros(WMenuParser parser)
#ifdef DEBUG
printf(__FILE__ ": %d macros\n", count);
#endif
- parser->macros = NULL; // Security
+ parser->macros = NULL; /* Security */
}
/* Check wether the specified character is valid for a name (macro, parameter)
or not */
int isnamechr(char ch)
{
static const int table[256] = {
- [0] = 0, // In case we'd fall on buggy compiler, to avoid crash
- // C99: 6.7.8.21 -> non specified values are initialised to 0
+ [0] = 0, /* In case we'd fall on buggy compiler, to avoid crash
*/
+ /* C99: 6.7.8.21 -> non specified values are initialised to 0 */
['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
['A'] = 1, ['B'] = 1, ['C'] = 1, ['D'] = 1, ['E'] = 1, ['F'] =
1,
@@ -149,8 +151,9 @@ int isnamechr(char ch)
['s'] = 1, ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, ['x'] =
1,
['y'] = 1, ['z'] = 1,
['_'] = 1
- // We refuse any UTF-8 coded character, or accents in ISO-xxx
codepages
+ /* We refuse any UTF-8 coded character, or accents in ISO-xxx
codepages */
};
+
return table[0x00FF & (unsigned)ch ];
}
@@ -166,6 +169,7 @@ void menu_parser_define_macro(WMenuParser parser)
WMenuParserError(parser, _("no macro name found for #define") );
return;
}
+
macro = wmalloc(sizeof(*macro));
/* Isolate name of macro */
@@ -173,9 +177,10 @@ void menu_parser_define_macro(WMenuParser parser)
while (isnamechr(*parser->rd)) {
if (idx < sizeof(macro->name) - 1)
macro->name[idx++] = *parser->rd;
+
parser->rd++;
}
- // macro->name[idx] = '\0'; -> Already present because wmalloc filled
struct with 0s
+ /* macro->name[idx] = '\0'; -> Already present because wmalloc filled
struct with 0s */
/* Build list of expected arguments */
if (*parser->rd == '(') {
@@ -183,57 +188,67 @@ void menu_parser_define_macro(WMenuParser parser)
idx = 0;
for (;;) {
if (!menu_parser_skip_spaces_and_comments(parser)) {
- arglist_error_premature_eol:
+arglist_error_premature_eol:
WMenuParserError(parser, _("premature end of
file while reading arg-list for macro \"%s\""), macro->name);
wfree(macro);
return;
}
- if (*parser->rd == ')') break;
+
+ if (*parser->rd == ')')
+ break;
if (macro->arg_count >= sizeof(arg_name) /
sizeof(arg_name[0])) {
WMenuParserError(parser, _("too many parameters
for macro \"%s\" definition"), macro->name);
wfree(macro);
- *parser->rd = '\0'; // fake end-of-line to
avoid warnings from remaining line content
+ *parser->rd = '\0'; /* fake end-of-line to
avoid warnings from remaining line content */
return;
}
+
if (isnamechr(*parser->rd)) {
arg_name[macro->arg_count++] = arg_names_buf +
idx;
do {
if (idx < sizeof(arg_names_buf) - 1)
arg_names_buf[idx++] =
*parser->rd;
+
parser->rd++;
} while (isnamechr(*parser->rd));
+
arg_names_buf[idx] = '\0';
- if (idx < sizeof(arg_names_buf) - 1) idx++;
+ if (idx < sizeof(arg_names_buf) - 1)
+ idx++;
} else {
WMenuParserError(parser, _("invalid characted
'%c' in arg-list for macro \"%s\" while expecting parameter name"),
-
*parser->rd, macro->name);
+ *parser->rd, macro->name);
wfree(macro);
*parser->rd = '\0'; // fake end-of-line to
avoid warnings from remaining line content
return;
}
+
if (!menu_parser_skip_spaces_and_comments(parser))
goto arglist_error_premature_eol;
- if (*parser->rd == ')') break;
+
+ if (*parser->rd == ')')
+ break;
if (*parser->rd != ',') {
WMenuParserError(parser, _("invalid characted
'%c' in arg-list for macro \"%s\" while expecting ',' or ')'"),
-
*parser->rd, macro->name);
+ *parser->rd, macro->name);
wfree(macro);
- *parser->rd = '\0'; // fake end-of-line to
avoid warnings from remaining line content
+ *parser->rd = '\0'; /* fake end-of-line to
avoid warnings from remaining line content */
return;
}
parser->rd++;
}
- parser->rd++; // skip the closing ')'
- } else
- macro->arg_count = -1; // Means no parenthesis at all to expect
+ parser->rd++; /* skip the closing ')' */
+ } else {
+ macro->arg_count = -1; /* Means no parenthesis at all to
expect */
+ }
/* If we're inside a #if sequence, we abort now, but not sooner in
- order to keep the syntax check */
+ * order to keep the syntax check */
if (parser->cond.stack[0].skip) {
wfree(macro);
- *parser->rd = '\0'; // Ignore macro content
+ *parser->rd = '\0'; /* Ignore macro content */
return;
}
@@ -251,7 +266,7 @@ void menu_parser_define_macro(WMenuParser parser)
/* Check that the macro was not already defined */
if (menu_parser_find_macro(parser, macro->name) != NULL) {
WMenuParserError(parser, _("macro \"%s\" already defined,
ignoring redefinition"),
- macro->name);
+ macro->name);
wfree(macro);
return;
}
@@ -269,45 +284,49 @@ WParserMacro *menu_parser_find_macro(WMenuParser parser,
const char *name)
while (parser->parent_file != NULL)
parser = parser->parent_file;
+
for (macro = parser->macros; macro != NULL; macro = macro->next) {
ref = macro->name;
cmp = name;
while (*ref != '\0')
if (*ref++ != *cmp++)
goto check_next_macro;
+
if (isnamechr(*cmp))
continue;
return macro;
- check_next_macro: ;
+check_next_macro:
+ ;
}
return NULL;
}
/* look to see if the next word matches the name of one of the parameter
- names for a macro definition
- This function is internal to the macro definition function as this is
- where the analysis is done */
-static inline char *mp_is_parameter(char *parse, const char *param) {
+ * names for a macro definition
+ * This function is internal to the macro definition function as this is
+ * where the analysis is done */
+static inline char *mp_is_parameter(char *parse, const char *param)
+{
while (*param)
if (*parse++ != *param++)
return NULL;
+
if (isnamechr(*parse))
return NULL;
+
return parse;
}
/* Read the content definition part of a #define construct (the part after the
optional
- argument list) and store it in the prepared format for quick expansion
-
- There is no need to keep track of the names of the parameters, so they
are stored in
- a temporary storage for the time of the macro parsing. */
+ * argument list) and store it in the prepared format for quick expansion
+ *
+ * There is no need to keep track of the names of the parameters, so they are
stored in
+ * a temporary storage for the time of the macro parsing. */
static Bool menu_parser_read_macro_def(WMenuParser parser, WParserMacro
*macro, char **arg)
{
- unsigned char *wr_size;
- unsigned char *wr;
- unsigned int size_data;
- unsigned int size_max;
+ unsigned char *wr_size, *wr;
+ unsigned int size_data, size_max;
int i;
wr_size = macro->value;
@@ -324,6 +343,7 @@ static Bool menu_parser_read_macro_def(WMenuParser parser,
WParserMacro *macro,
if (next_rd != NULL) {
if (wr + 4 >= macro->value +
sizeof(macro->value))
goto error_too_much_data;
+
wr_size[0] = (size_data >> 8) & 0xFF;
wr_size[1] = size_data & 0xFF;
*wr++ = i;
@@ -333,8 +353,8 @@ static Bool menu_parser_read_macro_def(WMenuParser parser,
WParserMacro *macro,
*wr++ = ' ';
size_data = 1;
size_max = sizeof(macro->value) - (wr -
macro->value) - 3;
- goto next_loop; // Because we can't
'break' this loop and 'continue'
- // the outer one in a
clean and easy way
+ goto next_loop; /* Because we can't
'break' this loop and 'continue'
+ * the outer one in a
clean and easy way */
}
}
@@ -342,12 +362,13 @@ static Bool menu_parser_read_macro_def(WMenuParser
parser, WParserMacro *macro,
do {
*wr++ = *parser->rd++;
if (++size_data >= size_max) {
- error_too_much_data:
+error_too_much_data:
WMenuParserError(parser, _("more
content than supported for the macro \"%s\""),
-
macro->name);
+ macro->name);
return False;
}
} while (isnamechr(*parser->rd));
+
if (isspace(*parser->rd)) {
*wr++ = ' ';
if (++size_data >= size_max)
@@ -356,20 +377,24 @@ static Bool menu_parser_read_macro_def(WMenuParser
parser, WParserMacro *macro,
} else {
/* Some uninterresting characters, copy as-is */
while (*parser->rd != '\0') {
- if (isnamechr(*parser->rd)) break; // handle in
next loop
- if (parser->rd[0] == '/')
- if ((parser->rd[1] == '*') ||
(parser->rd[1] == '/'))
- break; // Comments are handled
by std function
+ if (isnamechr(*parser->rd))
+ break; /* handle in next loop
*/
+
+ if ((parser->rd[0] == '/') &&
+ ((parser->rd[1] == '*') || (parser->rd[1]
== '/')))
+ break; /* Comments are handled
by std function */
+
if ((parser->rd[0] == '\\') &&
- (parser->rd[1] == '\n') &&
- (parser->rd[2] == '\0'))
- break; // Long-lines are handled by std
function
+ (parser->rd[1] == '\n') &&
+ (parser->rd[2] == '\0'))
+ break; /* Long-lines are
handled by std function */
+
*wr++ = *parser->rd++;
if (++size_data >= size_max)
goto error_too_much_data;
}
}
- next_loop:
+next_loop:
;
}
wr_size[0] = (size_data >> 8) & 0xFF;
@@ -379,10 +404,10 @@ static Bool menu_parser_read_macro_def(WMenuParser
parser, WParserMacro *macro,
}
/* When a macro is being used in the file, this function will generate the
- expanded value for the macro in the parser's work line.
- It blindly supposes that the data generated in macro->value is valid */
+ * expanded value for the macro in the parser's work line.
+ * It blindly supposes that the data generated in macro->value is valid */
void menu_parser_expand_macro(WMenuParser parser, WParserMacro *macro,
-
char *write_buf, int write_buf_size)
+ char *write_buf, int write_buf_size)
{
char save_buf[sizeof(parser->line_buffer)];
char arg_values_buf[MAXLINE];
@@ -403,25 +428,26 @@ void menu_parser_expand_macro(WMenuParser parser,
WParserMacro *macro,
#endif
/* Save the remaining data from current line as we will overwrite the
- current line's workspace with the expanded macro, so we can
re-append
- it afterwards */
+ * current line's workspace with the expanded macro, so we can re-append
+ * it afterwards */
dst = save_buf;
- while ((*dst++ = *parser->rd++) != '\0') ;
+ while ((*dst++ = *parser->rd++) != '\0');
/* Generate expanded macro */
dst = write_buf;
space_left = write_buf_size - 1;
if (macro->function != NULL) {
/* Parser's pre-defined macros actually proposes a function call to
- generate dynamic value for the expansion of the
macro. In this case
- it is generated as a C string in the macro->value and
used directly */
+ * generate dynamic value for the expansion of the macro. In this case
+ * it is generated as a C string in the macro->value and used directly
*/
macro->function(macro, parser);
rd = macro->value;
- while (--space_left > 0)
+ while (--space_left > 0) {
if ((*dst = *rd++) == '\0')
break;
else
dst++;
+ }
} else {
rd = macro->value;
for (;;) {
@@ -429,13 +455,18 @@ void menu_parser_expand_macro(WMenuParser parser,
WParserMacro *macro,
size |= *rd++;
while (size-- > 0) {
*dst = *rd++;
- if (--space_left > 0) dst++;
+ if (--space_left > 0)
+ dst++;
}
- if (*rd == 0xFF) break;
+
+ if (*rd == 0xFF)
+ break;
+
src = arg_value[*rd++];
while (*src) {
*dst = *src++;
- if (--space_left > 0) dst++;
+ if (--space_left > 0)
+ dst++;
}
}
}
@@ -445,28 +476,29 @@ void menu_parser_expand_macro(WMenuParser parser,
WParserMacro *macro,
while (--space_left > 0)
if ((*dst++ = *src++) == '\0')
break;
+
*dst = '\0';
if (space_left <= 0)
WMenuParserError(parser, _("expansion for macro \"%s\" too big,
line truncated"),
- macro->name);
+ macro->name);
}
/* When reading a macro to be expanded (not being defined), that takes
arguments,
- this function parses the arguments being provided */
+ * this function parses the arguments being provided */
static Bool menu_parser_read_macro_args(WMenuParser parser, WParserMacro
*macro,
-
char *array[], char *buffer, ssize_t buffer_size)
+ char *array[], char *buffer, ssize_t
buffer_size)
{
int arg;
if (*parser->rd != '(') {
WMenuParserError(parser, _("macro \"%s\" needs parenthesis for
arguments"),
- macro->name);
+ macro->name);
return False;
}
parser->rd++;
- buffer_size--; // Room for final '\0'
+ buffer_size--; /* Room for final '\0' */
menu_parser_skip_spaces_and_comments(parser);
arg = 0;
for (;;) {
@@ -479,35 +511,45 @@ static Bool menu_parser_read_macro_args(WMenuParser
parser, WParserMacro *macro,
if (*parser->rd == '(')
paren_count++;
- if (paren_count <= 0)
- if ((*parser->rd == ',') ||
- (*parser->rd == ')') ) break;
+ if ((paren_count <= 0) &&
+ ((*parser->rd == ',') || (*parser->rd == ')')))
+ break;
if ((*parser->rd == '"') || (*parser->rd == '\'')) {
char eot = *parser->rd++;
- if (buffer_size-- > 0) *buffer++ = eot;
+ if (buffer_size-- > 0)
+ *buffer++ = eot;
+
while (*parser->rd) {
if ((*buffer = *parser->rd++) == eot)
goto found_end_of_string;
- if (buffer_size-- > 0) buffer++;
+
+ if (buffer_size-- > 0)
+ buffer++;
}
+
WMenuParserError(parser, _("missing closing
quote or double-quote before end-of-line") );
return False;
- found_end_of_string:
+found_end_of_string:
continue;
}
if (isspace(*parser->rd)) {
- if (buffer_size-- > 0) *buffer++ = ' ';
+ if (buffer_size-- > 0)
+ *buffer++ = ' ';
+
menu_parser_skip_spaces_and_comments(parser);
continue;
}
*buffer = *parser->rd++;
- if (buffer_size-- > 0) buffer++;
+ if (buffer_size-- > 0)
+ buffer++;
}
+
*buffer = '\0';
- if (buffer_size-- > 0) buffer++;
+ if (buffer_size-- > 0)
+ buffer++;
arg++;
@@ -515,27 +557,31 @@ static Bool menu_parser_read_macro_args(WMenuParser
parser, WParserMacro *macro,
parser->rd++;
if (arg >= macro->arg_count) {
WMenuParserError(parser, _("too many arguments
for macro \"%s\", expected only %d"),
-
macro->name, macro->arg_count);
+ macro->name, macro->arg_count);
return False;
}
continue;
}
break;
}
+
if (*parser->rd != ')') {
WMenuParserError(parser, _("premature end of line while
searching for arguments to macro \"%s\""),
- macro->name);
+ macro->name);
return False;
}
+
parser->rd++;
if (arg < macro->arg_count) {
WMenuParserError(parser, _("not enough arguments for macro
\"%s\", expected %d but got only %d"),
- macro->name,
macro->arg_count, arg);
+ macro->name, macro->arg_count, arg);
return False;
}
+
if (buffer_size < 0)
WMenuParserError(parser, _("too much data in parameter list of
macro \"%s\", truncated"),
- macro->name);
+ macro->name);
+
return True;
}
@@ -550,18 +596,20 @@ void WMenuParserRegisterSimpleMacro(WMenuParser parser,
const char *name, const
unsigned char *wr;
macro = wmalloc(sizeof(*macro));
- strncpy(macro->name, name, sizeof(macro->name)-1);
+ strncpy(macro->name, name, sizeof(macro->name) - 1);
macro->arg_count = -1;
len = strlen(value);
if (len > sizeof(macro->value) - 3) {
wwarning(_("size of value for macro '%s' is too big,
truncated"), name);
len = sizeof(macro->value) - 3;
}
+
macro->value[0] = (len >> 8) & 0xFF;
macro->value[1] = len & 0xFF;
wr = ¯o->value[2];
while (len-- > 0)
*wr++ = *value++;
+
*wr = 0xFF;
macro->next = parser->macros;
parser->macros = macro;
@@ -572,7 +620,8 @@ static void mpm_base_file(WParserMacro *this, WMenuParser
parser)
{
unsigned char *src, *dst;
- if (this->value[0] != '\0') return; // Value already evaluated, re-use
previous
+ if (this->value[0] != '\0')
+ return; /* Value already evaluated, re-use previous */
while (parser->parent_file != NULL)
parser = parser->parent_file;
@@ -580,11 +629,13 @@ static void mpm_base_file(WParserMacro *this, WMenuParser
parser)
dst = this->value;
src = (unsigned char *) parser->file_name;
*dst++ = '\"';
- while (*src != '\0')
+ while (*src != '\0') {
if (dst < this->value + sizeof(this->value) - 2)
*dst++ = *src++;
else
break;
+ }
+
*dst++ = '\"';
*dst = '\0';
}
@@ -593,10 +644,12 @@ static void mpm_base_file(WParserMacro *this, WMenuParser
parser)
static void mpm_include_level(WParserMacro *this, WMenuParser parser)
{
int level = 0;
+
while (parser->parent_file != NULL) {
parser = parser->parent_file;
level++;
}
+
snprintf((char *) this->value, sizeof(this->value), "%d", level);
}
@@ -608,11 +661,13 @@ static void mpm_current_file(WParserMacro *this,
WMenuParser parser)
dst = this->value;
src = (unsigned char *) parser->file_name;
*dst++ = '\"';
- while (*src != '\0')
+ while (*src != '\0') {
if (dst < this->value + sizeof(this->value) - 2)
*dst++ = *src++;
else
break;
+ }
+
*dst++ = '\"';
*dst = '\0';
}
@@ -628,7 +683,8 @@ static void mpm_get_hostname(WParserMacro *this,
WMenuParser parser)
{
char *h;
- if (this->value[0] != '\0') return; // Value already evaluated, re-use
previous
+ if (this->value[0] != '\0')
+ return; /* Value already evaluated, re-use previous */
h = getenv("HOSTNAME");
if (h == NULL) {
@@ -644,6 +700,7 @@ static void mpm_get_hostname(WParserMacro *this,
WMenuParser parser)
return;
}
}
+
wstrlcpy((char *) this->value, h, sizeof(this->value) );
}
@@ -652,7 +709,8 @@ static void mpm_get_user_name(WParserMacro *this,
WMenuParser parser)
{
char *user;
- if (this->value[0] != '\0') return; // Value already evaluated, re-use
previous
+ if (this->value[0] != '\0')
+ return; /* Value already evaluated, re-use previous */
user = getlogin();
if (user == NULL) {
@@ -660,14 +718,17 @@ static void mpm_get_user_name(WParserMacro *this,
WMenuParser parser)
pw_user = getpwuid(getuid());
if (pw_user == NULL) {
- error_no_username:
+error_no_username:
WMenuParserError(parser, _("could not determine %s"),
"USER" );
+
/* Fall back on numeric id - better than nothing */
- snprintf((char *) this->value, sizeof(this->value),
"%d", getuid() );
+ snprintf((char *) this->value, sizeof(this->value),
"%d", getuid());
return;
}
+
user = pw_user->pw_name;
- if (user == NULL) goto error_no_username;
+ if (user == NULL)
+ goto error_no_username;
}
wstrlcpy((char *) this->value, user, sizeof(this->value) );
}
@@ -675,24 +736,26 @@ static void mpm_get_user_name(WParserMacro *this,
WMenuParser parser)
/* Number id of the user under which we are running */
static void mpm_get_user_id(WParserMacro *this, WMenuParser parser)
{
- if (this->value[0] != '\0') return; // Already evaluated, re-use
previous
- snprintf((char *) this->value, sizeof(this->value), "%d", getuid() );
+ if (this->value[0] != '\0')
+ return; /* Already evaluated, re-use previous */
+
+ snprintf((char *) this->value, sizeof(this->value), "%d", getuid());
}
/* Small helper to automate creation of one pre-defined macro in the parser */
static void w_create_macro(WMenuParser parser, const char *name,
WParserMacroFunction *handler)
{
- WParserMacro *macro;
+ WParserMacro *macro = wmalloc(sizeof(macro));
- macro = wmalloc(sizeof(*macro));
strcpy(macro->name, name);
macro->function = handler;
macro->arg_count = -1;
macro->next = parser->macros;
+
parser->macros = macro;
}
-/***** Register all the pre-defined macros in the parser *****/
+/* Register all the pre-defined macros in the parser */
void menu_parser_register_preset_macros(WMenuParser parser)
{
/* Defined by CPP: common predefined macros (GNU C extension) */
@@ -702,8 +765,8 @@ void menu_parser_register_preset_macros(WMenuParser parser)
/* Defined by CPP: standard predefined macros */
w_create_macro(parser, "__FILE__", mpm_current_file);
w_create_macro(parser, "__LINE__", mpm_current_line);
- // w_create_macro(parser, "__DATE__", NULL); [will be implemented only
per user request]
- // w_create_macro(parser, "__TIME__", NULL); [will be implemented only
per user request]
+ /* w_create_macro(parser, "__DATE__", NULL); [will be implemented only
per user request] */
+ /* w_create_macro(parser, "__TIME__", NULL); [will be implemented only
per user request] */
/* Historically defined by WindowMaker */
w_create_macro(parser, "HOST", mpm_get_hostname);
--
1.7.10.4
--
To unsubscribe, send mail to [email protected].