summaryrefslogtreecommitdiff
path: root/util/util.c
blob: cc711ed494945966cdf034a90768ea0f5205b0a3 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "util.h"

/* Some systems don't have strndup. */
char *
strdupn(char *s, size_t len)
{
	char *ret;

	ret = xalloc(len + 1);
	memcpy(ret, s, len);
	ret[len] = '\0';
	return ret;
}

char *
xstrdup(char *s)
{
	char *p;

	p = strdup(s);
	if (!p && s)
		die("Out of memory");
	return p;
}

char *
strjoin(char *u, char *v)
{
	size_t n;
	char *s;

	n = strlen(u) + strlen(v) + 1;
	s = xalloc(n);
	bprintf(s, n + 1, "%s%s", u, v);
	return s;
}

char *
swapsuffix(char *buf, size_t sz, char *s, char *suf, char *swap)
{
	size_t slen, suflen, swaplen;

	slen = strlen(s);
	suflen = strlen(suf);
	swaplen = strlen(swap);

	if (slen < suflen)
		return NULL;
	if (slen + swaplen >= sz)
		die("swapsuffix: buf too small");

	buf[0] = '\0';
	/* if we have matching suffixes */
	if (suflen < slen && !strcmp(suf, &s[slen - suflen])) {
		strncat(buf, s, slen - suflen);
		strncat(buf, swap, swaplen);
	} else {
		bprintf(buf, sz, "%s%s", s, swap);
	}

	return buf;
}

size_t
max(size_t a, size_t b)
{
	if (a > b)
		return a;
	else
		return b;
}

size_t
min(size_t a, size_t b)
{
	if (a < b)
		return a;
	else
		return b;
}

size_t
align(size_t sz, size_t a)
{
	/* align to 0 just returns sz */
	if (a == 0)
		return sz;
	return (sz + a - 1) & ~(a - 1);
}

void
indentf(int depth, char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vfindentf(stdout, depth, fmt, ap);
	va_end(ap);
}

void
findentf(FILE *fd, int depth, char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vfindentf(fd, depth, fmt, ap);
	va_end(ap);
}

void
vfindentf(FILE *fd, int depth, char *fmt, va_list ap)
{
	ssize_t i;

	for (i = 0; i < depth; i++)
		fprintf(fd, "\t");
	vfprintf(fd, fmt, ap);
}

static int
optinfo(Optctx *ctx, char arg, int *take, int *mand)
{
	char *s;

	for (s = ctx->optstr; *s != '\0'; s++) {
		if (*s == arg) {
			s++;
			if (*s == ':') {
				*take = 1;
				*mand = 1;
				return 1;
			} else if (*s == '?') {
				*take = 1;
				*mand = 0;
				return 1;
			} else {
				*take = 0;
				*mand = 0;
				return 1;
			}
		}
	}
	return 0;
}

static int
findnextopt(Optctx *ctx)
{
	size_t i;

	for (i = ctx->argidx + 1; i < ctx->noptargs; i++) {
		if (ctx->optargs[i][0] == '-')
			goto foundopt;
		else
			lappend(&ctx->args, &ctx->nargs, ctx->optargs[i]);
	}
	ctx->finished = 1;
	return 0;
foundopt
:
	ctx->argidx = i;
	ctx->curarg = ctx->optargs[i] + 1; /* skip initial '-' */
	return 1;
}

void
optinit(Optctx *ctx, char *optstr, char **optargs, size_t noptargs)
{
	ctx->args = NULL;
	ctx->nargs = 0;

	ctx->optstr = optstr;
	ctx->optargs = optargs;
	ctx->noptargs = noptargs;
	ctx->optdone = 0;
	ctx->finished = 0;
	ctx->argidx = 0;
	ctx->curarg = "";
	findnextopt(ctx);
}

int
optnext(Optctx *ctx)
{
	int take, mand;
	int c;

	c = *ctx->curarg;
	ctx->curarg++;
	if (!optinfo(ctx, c, &take, &mand)) {
		printf("Unexpected argument %c\n", c);
		exit(1);
	}

	ctx->optarg = NULL;
	if (take) {
		if (*ctx->curarg) {
			ctx->optarg = ctx->curarg;
			ctx->curarg += strlen(ctx->optarg);
		} else if (ctx->argidx < ctx->noptargs - 1) {
			ctx->optarg = ctx->optargs[ctx->argidx + 1];
			ctx->argidx++;
		} else if (mand) {
			fprintf(stderr, "expected argument for %c\n", *ctx->curarg);
		}
		findnextopt(ctx);
	} else {
		if (*ctx->curarg == '\0')
			findnextopt(ctx);
	}
	return c;
}

int
optdone(Optctx *ctx) { return *ctx->curarg == '\0' && ctx->finished; }