Skip to content

Commit ad0a2b1

Browse files
author
Stephen Mathieson
committed
Add C89 support
Closes #19.
1 parent 57c397c commit ad0a2b1

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
test: test.c src/commander.c
3-
$(CC) $^ -o $@ -Wall -Wextra
3+
$(CC) $^ -o $@ -std=c89 -Wall -Wextra
44

55
clean:
66
rm -f test

src/commander.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
//
3-
// commander.c
4-
//
5-
// Copyright (c) 2012 TJ Holowaychuk <[email protected]>
6-
//
2+
/*
3+
* commander.h
4+
*
5+
* Copyright (c) 2012 TJ Holowaychuk <[email protected]>
6+
*/
77

88
#include <stdio.h>
99
#include <stdlib.h>
@@ -141,19 +141,20 @@ normalize_args(int *argc, char **argv) {
141141
const char *arg = argv[i];
142142
size_t len = strlen(arg);
143143

144-
// short flag
144+
/* short flag */
145145
if (len > 2 && '-' == arg[0] && !strchr(arg + 1, '-')) {
146+
size_t j;
146147
alloc += len - 2;
147148
nargv = realloc(nargv, alloc * sizeof(char *));
148-
for (size_t j = 1; j < len; ++j) {
149+
for (j = 1; j < len; ++j) {
149150
nargv[size] = malloc(3);
150151
sprintf(nargv[size], "-%c", arg[j]);
151152
size++;
152153
}
153154
continue;
154155
}
155156

156-
// regular arg
157+
/* regular arg */
157158
nargv[size] = malloc(len + 1);
158159
strcpy(nargv[size], arg);
159160
size++;
@@ -206,11 +207,11 @@ command_parse_args(command_t *self, int argc, char **argv) {
206207
for (j = 0; j < self->option_count; ++j) {
207208
command_option_t *option = &self->options[j];
208209

209-
// match flag
210+
/* match flag */
210211
if (!strcmp(arg, option->small) || !strcmp(arg, option->large)) {
211212
self->arg = NULL;
212213

213-
// required
214+
/* required */
214215
if (option->required_arg) {
215216
arg = argv[++i];
216217
if (!arg || '-' == arg[0]) {
@@ -221,26 +222,26 @@ command_parse_args(command_t *self, int argc, char **argv) {
221222
self->arg = arg;
222223
}
223224

224-
// optional
225+
/* optional */
225226
if (option->optional_arg) {
226227
if (argv[i + 1] && '-' != argv[i + 1][0]) {
227228
self->arg = argv[++i];
228229
}
229230
}
230231

231-
// invoke callback
232+
/* invoke callback */
232233
option->cb(self);
233234
goto match;
234235
}
235236
}
236237

237-
// --
238+
/* -- */
238239
if ('-' == arg[0] && '-' == arg[1] && 0 == arg[2]) {
239240
literal = 1;
240241
goto match;
241242
}
242243

243-
// unrecognized
244+
/* unrecognized */
244245
if ('-' == arg[0] && !literal) {
245246
fprintf(stderr, "unrecognized flag %s\n", arg);
246247
command_free(self);

src/commander.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
//
3-
// commander.h
4-
//
5-
// Copyright (c) 2012 TJ Holowaychuk <[email protected]>
6-
//
2+
/*
3+
* commander.h
4+
*
5+
* Copyright (c) 2012 TJ Holowaychuk <[email protected]>
6+
*/
77

88
#ifndef COMMANDER_H
99
#define COMMANDER_H
@@ -68,7 +68,7 @@ typedef struct command {
6868
char **nargv;
6969
} command_t;
7070

71-
// prototypes
71+
/* prototypes */
7272

7373
void
7474
command_init(command_t *self, const char *name, const char *version);

test.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ main(int argc, char **argv){
2626
command_option(&cmd, "-o", "--optional [arg]", "optional arg", optional);
2727
command_parse(&cmd, argc, argv);
2828
printf("additional args:\n");
29-
for (int i = 0; i < cmd.argc; ++i) {
29+
int i;
30+
for (i = 0; i < cmd.argc; ++i) {
3031
printf(" - '%s'\n", cmd.argv[i]);
3132
}
3233
command_free(&cmd);

0 commit comments

Comments
 (0)