summaryrefslogtreecommitdiff
path: root/lib/libfuse/fuse_opt.c
blob: 28f5661a7235f32a86075c194094844f1b92e912 (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
/* $OpenBSD: fuse_opt.c,v 1.4 2013/06/12 22:44:42 tedu Exp $ */
/*
 * Copyright (c) 2013 Sylvestre Gallon <ccna.syl@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdlib.h>
#include <string.h>

#include "debug.h"
#include "fuse_opt.h"
#include "fuse_private.h"

int
fuse_opt_add_arg(struct fuse_args *args, const char *name)
{
	char **av;
	int i;

	/* copy argv, we cannot reallocate the orignal argv */
	if (args->allocated == 0) {
		av = malloc((args->argc + 1) * sizeof(*av));
		if (av == NULL)
			return (-1);

		for (i = 0; i < args->argc; i++) {
			av[i] = strdup(args->argv[i]);
			if (av[i] == NULL)
				return (-1);
		}

		av[args->argc] = strdup(name);
		if (av[args->argc] == NULL)
			return (-1);

		args->argc++;
		args->argv = av;
		args->allocated = 1;
	} else {
		av = realloc(args->argv, (args->argc + 1) * sizeof(*av));
		if (av == NULL)
			return (-1);

		args->argv = av;
		args->argc++;
		args->argv[args->argc - 1] = strdup(name);
		if (args->argv[args->argc - 1] == NULL)
			return (-1);
	}

	return (0);
}

int
fuse_opt_parse(struct fuse_args *args, void *data, struct fuse_opt *opt,
    fuse_opt_proc_t f)
{
	const char *arg;
	struct fuse_opt *good;
	int ret;
	int i, j;

	for (i = 0; i < args->argc; i++) {
		arg = args->argv[i];

		/* not - and not -- */
		if (arg[0] != '-') {
			ret = (f) ? f(data, arg, FUSE_OPT_KEY_NONOPT, 0) : 0;

			if (ret == -1)
				return (ret);
		} else {
			switch (arg[1]) {
			case 'o':
				DPRINTF("%s: -o X,Y not supported yet.\n",
				    __func__);
				break ;
			case '-':
				DPRINTF("%s: long option not supported yet.",
				    __func__);
				break ;
			default:
				good = NULL;
				for (j = 0; opt[j].templ != NULL; j++)
					if (strcmp(arg, opt[j].templ) == 0) {
						good = &opt[j];
						break ;
					}

				if (!good)
					break ;

				if (good->val == -1 && f) {
					ret = f(data, arg, good->val, 0);

					if (ret == -1)
						return (ret);
				}
				break;
			}
		}
	}
	return (0);
}

int
fuse_opt_insert_arg(struct fuse_args *args, int p, const char *arg)
{
	char **av;
	int i;

	if (p > args->argc + 1)
		return -1;

	if (args->allocated) {
		av = realloc(args->argv, (args->argc + 1) * sizeof(char *));
		if (av == NULL)
			return (-1);

		args->argv = av;
		args->argc++;
		for (i = args->argc - 1; i > p; i++)
			args->argv[i] = args->argv[i - 1];

		args->argv[p] = strdup(arg);
		if (args->argv[p] == NULL)
			return (-1);
	}
	return (0);
}

int
fuse_opt_free_args(struct fuse_args *args)
{
	int i;

	if (args->allocated)
		for (i = 0; i < args->argc; i++)
			free(args->argv[i]);
	free(args->argv);
	args->allocated = 0;
	args->argc = 0;

	return (0);
}