Skip to content

Commit

Permalink
Merge pull request vmg#137 from hoedown/ansi
Browse files Browse the repository at this point in the history
Use a stricter subset of C
  • Loading branch information
Devin Torres committed Oct 21, 2014
2 parents 4e1b16c + 684357d commit a88343c
Show file tree
Hide file tree
Showing 9 changed files with 606 additions and 494 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -g -O3 -Wall -Wextra -Wno-unused-parameter -Isrc
CFLAGS = -g -O3 -ansi -pedantic -Wall -Wextra -Wno-unused-parameter -Isrc

ifneq ($(OS),Windows_NT)
CFLAGS += -fPIC
Expand Down Expand Up @@ -63,3 +63,6 @@ clean:

%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

src/html_blocks.o: src/html_blocks.c
$(CC) $(CFLAGS) -Wno-static-in-inline -c -o $@ $<
77 changes: 70 additions & 7 deletions bin/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
#define count_of(arr) (sizeof(arr)/sizeof(0[arr]))

int
parseint(const char *string, long *result) {
parseint(const char *string, long *result)
{
char *end;
errno = 0;
*result = strtol(string, &end, 10);
return !(*end || errno);
}

const char *
strprefix(const char *str, const char *prefix) {
strprefix(const char *str, const char *prefix)
{
while (*prefix) {
if (!(*str && *str == *prefix)) return 0;
prefix++; str++;
Expand All @@ -28,7 +30,8 @@ strprefix(const char *str, const char *prefix) {
}

void
print_option(char short_opt, const char *long_opt, const char *description) {
print_option(char short_opt, const char *long_opt, const char *description)
{
if (short_opt)
printf(" -%c, ", short_opt);
else
Expand All @@ -38,8 +41,68 @@ print_option(char short_opt, const char *long_opt, const char *description) {
}

void
print_version() {
int major, minor, revision;
hoedown_version(&major, &minor, &revision);
printf("Built with Hoedown v%d.%d.%d.\n", major, minor, revision);
print_version()
{
printf("Built with Hoedown " HOEDOWN_VERSION ".\n");
}

int
parse_options(
int argc, char **argv,
int(*parse_short_option)(char opt, char *next, void *opaque),
int(*parse_long_option)(char *opt, char *next, void *opaque),
int(*parse_argument)(int argn, char *arg, int is_forced, void *opaque),
void *opaque)
{
int result;
int i = 1, regular_args = 0;

/* Parse options mixed with arguments */
while (i < argc) {
char *arg = argv[i];

if (arg[0] == '-' && arg[1]) {
char *next_arg = (i+1 < argc) ? argv[i+1] : NULL;

if (arg[1] == '-' && !arg[2]) {
/* '--' signals end of options */
i++;
break;
}

if (arg[1] == '-') {
/* Long option */
result = parse_long_option(arg + 2, next_arg, opaque);
if (!result) return 0;
i += result;
} else {
/* Sequence of short options */
size_t pos;
for (pos = 1; arg[pos]; pos++) {
char *next = (arg[pos+1]) ? arg + pos+1 : next_arg;
result = parse_short_option(arg[pos], next, opaque);
if (!result) return 0;
if (result == 2) {
i++;
break;
}
}
i++;
}
} else {
/* Argument */
result = parse_argument(regular_args++, arg, 0, opaque);
if (!result) return 0;
i++;
}
}

/* Parse rest as forced arguments */
while (i < argc) {
result = parse_argument(regular_args++, argv[i], 1, opaque);
if (!result) return 0;
i++;
}

return 1;
}
Loading

0 comments on commit a88343c

Please sign in to comment.