commands_parser: use safewrapper functions

This commit is contained in:
Michael Stapelberg 2012-01-28 10:35:18 +00:00
parent 144be37517
commit 29aa28b623
2 changed files with 7 additions and 8 deletions

View File

@ -67,7 +67,7 @@ include/GENERATED_tokens.h: include/GENERATED_call.h
# and once as an object file for i3. # and once as an object file for i3.
src/commands_parser.o: src/commands_parser.c ${HEADERS} ${CMDPARSE_HEADERS} src/commands_parser.o: src/commands_parser.c ${HEADERS} ${CMDPARSE_HEADERS}
echo "[i3] CC $<" echo "[i3] CC $<"
$(CC) $(CPPFLAGS) $(CFLAGS) -DTEST_PARSER -DLOGLEVEL="((uint64_t)1 << $(shell awk '/$(shell basename $< .c)/ { print NR; exit 0; }' loglevels.tmp))" -o test.commands_parser $< $(CC) $(CPPFLAGS) $(CFLAGS) -DTEST_PARSER -DLOGLEVEL="((uint64_t)1 << $(shell awk '/$(shell basename $< .c)/ { print NR; exit 0; }' loglevels.tmp))" -o test.commands_parser $< $(LIBS)
$(CC) $(CPPFLAGS) $(CFLAGS) -DLOGLEVEL="((uint64_t)1 << $(shell awk '/$(shell basename $< .c)/ { print NR; exit 0; }' loglevels.tmp))" -c -o $@ $< $(CC) $(CPPFLAGS) $(CFLAGS) -DLOGLEVEL="((uint64_t)1 << $(shell awk '/$(shell basename $< .c)/ { print NR; exit 0; }' loglevels.tmp))" -c -o $@ $<
src/cfgparse.yy.o: src/cfgparse.l src/cfgparse.y.o ${HEADERS} src/cfgparse.yy.o: src/cfgparse.l src/cfgparse.y.o ${HEADERS}

View File

@ -31,7 +31,6 @@
#include <stdint.h> #include <stdint.h>
#include "all.h" #include "all.h"
#include "queue.h"
/******************************************************************************* /*******************************************************************************
* The data structures used for parsing. Essentially the current state and a * The data structures used for parsing. Essentially the current state and a
@ -233,7 +232,7 @@ char *parse_command(const char *input) {
if (strncasecmp(walk, token->name + 1, strlen(token->name) - 1) == 0) { if (strncasecmp(walk, token->name + 1, strlen(token->name) - 1) == 0) {
DLOG("found literal, moving to next state\n"); DLOG("found literal, moving to next state\n");
if (token->identifier != NULL) if (token->identifier != NULL)
push_string(token->identifier, strdup(token->name + 1)); push_string(token->identifier, sstrdup(token->name + 1));
walk += strlen(token->name) - 1; walk += strlen(token->name) - 1;
next_state(token); next_state(token);
token_handled = true; token_handled = true;
@ -274,7 +273,7 @@ char *parse_command(const char *input) {
} }
} }
if (walk != beginning) { if (walk != beginning) {
char *str = calloc(walk-beginning + 1, 1); char *str = scalloc(walk-beginning + 1);
strncpy(str, beginning, walk-beginning); strncpy(str, beginning, walk-beginning);
if (token->identifier) if (token->identifier)
push_string(token->identifier, str); push_string(token->identifier, str);
@ -321,7 +320,7 @@ char *parse_command(const char *input) {
* full input, and underline the position where the parser * full input, and underline the position where the parser
* currently is. */ * currently is. */
char *errormessage; char *errormessage;
char *possible_tokens = malloc(tokenlen + 1); char *possible_tokens = smalloc(tokenlen + 1);
char *tokenwalk = possible_tokens; char *tokenwalk = possible_tokens;
for (c = 0; c < ptr->n; c++) { for (c = 0; c < ptr->n; c++) {
token = &(ptr->array[c]); token = &(ptr->array[c]);
@ -346,13 +345,13 @@ char *parse_command(const char *input) {
} }
} }
*tokenwalk = '\0'; *tokenwalk = '\0';
asprintf(&errormessage, "Expected one of these tokens: %s", sasprintf(&errormessage, "Expected one of these tokens: %s",
possible_tokens); possible_tokens);
free(possible_tokens); free(possible_tokens);
/* Contains the same amount of characters as 'input' has, but with /* Contains the same amount of characters as 'input' has, but with
* the unparseable part highlighted using ^ characters. */ * the unparseable part highlighted using ^ characters. */
char *position = malloc(len + 1); char *position = smalloc(len + 1);
for (const char *copywalk = input; *copywalk != '\0'; copywalk++) for (const char *copywalk = input; *copywalk != '\0'; copywalk++)
position[(copywalk - input)] = (copywalk >= walk ? '^' : ' '); position[(copywalk - input)] = (copywalk >= walk ? '^' : ' ');
position[len] = '\0'; position[len] = '\0';