Skip to content

Commit

Permalink
Merge pull request #62 from masozzi/fix-format
Browse files Browse the repository at this point in the history
add support for [\n] character format parameter inside config file
  • Loading branch information
emersion authored Sep 23, 2018
2 parents 924f5a2 + 09cee1a commit e228571
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ static bool apply_style_option(struct mako_style *style, const char *name,
return spec->markup = parse_boolean(value, &style->markup);
} else if (strcmp(name, "format") == 0) {
free(style->format);
return spec->format = !!(style->format = strdup(value));
return spec->format = parse_format(value, &style->format);
} else if (strcmp(name, "default-timeout") == 0) {
return spec->default_timeout =
parse_int(value, &style->default_timeout);
Expand Down
1 change: 1 addition & 0 deletions criteria.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string.h>
#include <wayland-client.h>

#include "enum.h"
#include "mako.h"
#include "config.h"
#include "criteria.h"
Expand Down
9 changes: 0 additions & 9 deletions include/criteria.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@

struct mako_config;

// State is intended to work as a bitmask, so if more need to be added in the
// future, this should be taken into account.
enum mako_parse_state {
MAKO_PARSE_STATE_NORMAL = 0,
MAKO_PARSE_STATE_ESCAPE = 1,
MAKO_PARSE_STATE_QUOTE = 2,
MAKO_PARSE_STATE_QUOTE_ESCAPE = 3,
};

// Stores whether or not each field was part of the criteria specification, so
// that, for example, "not actionable" can be distinguished from "don't care".
// This is unnecessary for string fields, but it's best to just keep it
Expand Down
13 changes: 13 additions & 0 deletions include/enum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _ENUM_H_
#define _ENUM_H_

// State is intended to work as a bitmask, so if more need to be added in the
// future, this should be taken into account.
enum mako_parse_state {
MAKO_PARSE_STATE_NORMAL = 0,
MAKO_PARSE_STATE_ESCAPE = 1,
MAKO_PARSE_STATE_QUOTE = 2,
MAKO_PARSE_STATE_QUOTE_ESCAPE = 3,
};

#endif
1 change: 1 addition & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ struct mako_directional {
};

bool parse_directional(const char *string, struct mako_directional *out);
bool parse_format(const char *string, char **out);

#endif
4 changes: 4 additions & 0 deletions mako.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ specifiers.

*%%* Literal "%"

*\\\\* Literal "\\"

*\\n* New Line

## For notifications

*%a* Application name
Expand Down
58 changes: 58 additions & 0 deletions types.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <wordexp.h>
#include <unistd.h>

#include "enum.h"
#include "types.h"

bool parse_boolean(const char *string, bool *out) {
Expand Down Expand Up @@ -123,3 +124,60 @@ bool parse_directional(const char *string, struct mako_directional *out) {
free(components);
return true;
}

bool parse_format(const char *string, char **out) {
size_t token_max_length = strlen(string) + 1;
char token[token_max_length];
memset(token, 0, token_max_length);
size_t token_location = 0;

enum mako_parse_state state = MAKO_PARSE_STATE_NORMAL;
for (size_t i = 0; i < token_max_length; ++i) {
char ch = string[i];

switch (state) {
case MAKO_PARSE_STATE_ESCAPE:
switch (ch) {
case 'n':
token[token_location] = '\n';
++token_location;
break;

case '\\':
token[token_location] = '\\';
++token_location;
break;

default:
++token_location;
token[token_location] = ch;
++token_location;
break;
}

state = MAKO_PARSE_STATE_NORMAL;
break;

case MAKO_PARSE_STATE_NORMAL:
switch (ch) {
case '\\':
token[token_location] = ch;
state = MAKO_PARSE_STATE_ESCAPE;
break;

default:
token[token_location] = ch;
++token_location;
break;
}
break;

default:
*out = NULL;
return false;
}
}

*out = strdup(token);
return true;
}

0 comments on commit e228571

Please sign in to comment.