1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
ls.add_snippets('c', {
s('trip', {
-- cond ? then : else statment
i(1, 'cond'), t(' ? '), i(2, 'then'), t(' : '), i(3, 'else')
}),
s('stdlibs', {
-- the normal stuff
t('#include <stdio.h>'),
t({'', '#include <stdlib.h>'})
}),
s('die', {
-- message provieder when program is exiting
t({
'void die(const char *fmt, ...) {', '',
'va_list ap;',
'',
'va_start(ap, fmt);',
'vfprintf(stderr, fmt, ap);',
'va_end(ap);',
'',
[[if (fmt[0] && fmt[strlen(fmt)-1] == ':') {]],
[[ fputc(' ', stderr);]],
' perror(NULL);',
'} else',
[[ fputc('\n', stderr);]],
' exit(1);',
'}',
})
}),
s({
name = 'get them opts!',
trig = 'getopt',
dscr = 'standard argument parser',
},
fmta(
[[
int c;
while ((c = getopt(argc, argv, "<OPTS>")) != -1) {
switch (c) {
case '<OPT1>': <OPT1RUN> break;
default: <DEFRUN> break;
}
}
]],
{
OPTS = i(1, 'h'),
OPT1 = i(2, 'h'),
OPT1RUN = i(3, 'printf("help text\\n");'),
DEFRUN = i(4, 'printf("run %s -h for help\\n", argv[0]); return 1;'),
}
)
),
s({
name = 'Variadic function parser',
trig = 'infinite vars',
dscr = 'Parse an infinite number of arguments passed to a function',
},
{
t({
"/*",
" * NOTE: the function must have a int before the ... argument",
" * and you need to include <stdarg.h> for this to work",
" */",
"va_list ptr;",
"va_start(ptr, ", i(1, "n"),
");",
"for (int i = 0; i < ", ri(1),
"; i++) {",
i(2),
"}",
"va_end(ptr);",
}),
}
)
})
|