Skip to content

Commit

Permalink
implement inline comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gyf304 committed Apr 28, 2024
1 parent 44c06a2 commit aef36ff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ MANPREFIX ?= ${PREFIX}/share/man
all: dotenv

dotenv: dotenv.c
${CC} -Os -o dotenv dotenv.c
${CC} -Os -Wall -Werror -o dotenv dotenv.c

clean:
rm -f dotenv
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ Same as above but with a custom dotenv file path:

The file format is a list of key-value pairs, one per line.
The key and value are separated by an equal sign.
Whitespaces around the key and value are ignored.
Whitespaces around keys and values are ignored.
A line is terminated by a newline character or a carriage return.
Empty lines and lines starting with a hash sign (#) are ignored.
Comments can be added using a hash sign (#) either at the start of a line
or inline. For values that contain a hash sign, the value must be quoted.
Values can be optionally quoted with single or double quotes.
Characters after the ending quote for a value are ignored.
If quoted, the following escape sequences are supported:

\n
Expand Down Expand Up @@ -96,6 +96,7 @@ env(1)
Yifan Gu <[[email protected]](mailto:[email protected])>

Unknown OS - April 28, 2024

# INSTALL

```bash
Expand Down
6 changes: 3 additions & 3 deletions dotenv.1
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ $ DOTENV_PATH=.env2 dotenv env
.Sh FILE FORMAT
The file format is a list of key-value pairs, one per line.
The key and value are separated by an equal sign.
Whitespaces around the key and value are ignored.
Whitespaces around keys and values are ignored.
A line is terminated by a newline character or a carriage return.
Empty lines and lines starting with a hash sign (#) are ignored.
Comments can be added using a hash sign (#) either at the start of a line
or inline. For values that contain a hash sign, the value must be quoted.
Values can be optionally quoted with single or double quotes.
Characters after the ending quote for a value are ignored.
If quoted, the following escape sequences are supported:
.Bl -tag -width \e'
.It \en
Expand Down
37 changes: 29 additions & 8 deletions dotenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,22 @@ static inline int read_file(const char *filename, char **buffer, unsigned long *
return rc;
}

static inline void skip_charset(char **cur, const char *end, const char *charset) {
static inline int skip_charset(char **cur, const char *end, const char *charset) {
int count = 0;
while (*cur < end && strchr(charset, **cur) != NULL) {
(*cur)++;
count++;
}
return count;
}

static inline void skip_until_charset(char **cur, const char *end, const char *charset) {
static inline int skip_until_charset(char **cur, const char *end, const char *charset) {
int count = 0;
while (*cur < end && strchr(charset, **cur) == NULL) {
(*cur)++;
count++;
}
return count;
}

int main(int argc, const char *argv[]) {
Expand Down Expand Up @@ -144,7 +150,7 @@ int main(int argc, const char *argv[]) {
}

key = cur;
skip_until_charset(&cur, end, " \t=");
skip_until_charset(&cur, end, " \t=\r\n");
if (cur >= end) {
fprintf(stderr, "Unexpected end of file after key \"%s\"\n", key);
rc = 1;
Expand All @@ -154,6 +160,7 @@ int main(int argc, const char *argv[]) {
skip_charset(&cur, end, " \t");

if (*cur != '=' && *cur != '\0') {
*cur = '\0';
fprintf(stderr, "Missing equal after key \"%s\"\n", key);
rc = 1;
goto end;
Expand Down Expand Up @@ -183,9 +190,9 @@ int main(int argc, const char *argv[]) {

while (cur < end) {
if (*cur == '\\') {
cur++; // skip escape character
cur++;
if (cur >= end) {
fprintf(stderr, "Unexpected end of file after escape character at key \"%s\"\n", key);
fprintf(stderr, "Unexpected end of file after escape character\n");
rc = 1;
goto end;
}
Expand Down Expand Up @@ -233,15 +240,29 @@ int main(int argc, const char *argv[]) {
goto end;
}

key_end = inner_cur;
value_end = inner_cur;
cur++;
skip_charset(&cur, end, " \t");
if (skip_until_charset(&cur, end, "#\r\n") > 0) {
fprintf(stderr, "Unexpected characters after closing quote\n");
rc = 1;
goto end;
}
skip_until_charset(&cur, end, "\r\n");
} else {
value_end = NULL;
value = cur;
// unquoted value
while (skip_until_charset(&cur, end, "# \t\r\n") && strchr("#\r\n", *cur) == NULL) {
value_end = cur;
cur++;
}
if (value_end == NULL) {
value_end = cur;
}
skip_until_charset(&cur, end, "\r\n");
key_end = cur;
}
*key_end = '\0';
*value_end = '\0';

rc = setenv(key, value, 1);
if (rc != 0) {
Expand Down

0 comments on commit aef36ff

Please sign in to comment.