aboutsummaryrefslogtreecommitdiff
path: root/libpkgconf/argvsplit.c
blob: 4e4032771374eef2cb66b99bac7679f7ea118838 (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
/*
 * argvsplit.c
 * argv_split() routine
 *
 * Copyright (c) 2012, 2017 pkgconf authors (see AUTHORS).
 *
 * Permission to use, copy, modify, and/or 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.
 *
 * This software is provided 'as is' and without any warranty, express or
 * implied.  In no event shall the authors be liable for any damages arising
 * from the use of this software.
 */

#include <libpkgconf/stdinc.h>
#include <libpkgconf/libpkgconf.h>

/*
 * !doc
 *
 * libpkgconf `argvsplit` module
 * =============================
 *
 * This is a lowlevel module which provides parsing of strings into argument vectors,
 * similar to what a shell would do.
 */

/*
 * !doc
 *
 * .. c:function:: void pkgconf_argv_free(char **argv)
 *
 *    Frees an argument vector.
 *
 *    :param char** argv: The argument vector to free.
 *    :return: nothing
 */
void
pkgconf_argv_free(char **argv)
{
	free(argv[0]);
	free(argv);
}

/*
 * !doc
 *
 * .. c:function:: int pkgconf_argv_split(const char *src, int *argc, char ***argv)
 *
 *    Splits a string into an argument vector.
 *
 *    :param char*   src: The string to split.
 *    :param int*    argc: A pointer to an integer to store the argument count.
 *    :param char*** argv: A pointer to a pointer for an argument vector.
 *    :return: 0 on success, -1 on error.
 *    :rtype: int
 */
int
pkgconf_argv_split(const char *src, int *argc, char ***argv)
{
	char *buf = malloc(strlen(src) + 1);
	const char *src_iter;
	char *dst_iter;
	int argc_count = 0;
	int argv_size = 5;
	char quote = 0;
	bool escaped = false;

	src_iter = src;
	dst_iter = buf;

	memset(buf, 0, strlen(src) + 1);

	*argv = calloc(sizeof (void *), argv_size);
	(*argv)[argc_count] = dst_iter;

	while (*src_iter)
	{
		if (escaped)
		{
			/* POSIX: only \CHAR is special inside a double quote if CHAR is {$, `, ", \, newline}. */
			if (quote == '\"')
			{
				if (!(*src_iter == '$' || *src_iter == '`' || *src_iter == '"' || *src_iter == '\\'))
					*dst_iter++ = '\\';

				*dst_iter++ = *src_iter;
			}
			else
			{
        /*
         * There is no reason to keep space character escaped in fragment
         * objects, especially given that other characters are unescaped (issue
         * #139 is reported).
         *
         * Update: as a part of the issue #140 fix the backslash is now
         * escaped as well.
         */
        /*
				if (isspace((unsigned int) *src_iter) || *src_iter == '\\')
					*dst_iter++ = '\\';
         */

				*dst_iter++ = *src_iter;
			}

			escaped = false;
		}
		else if (quote)
		{
			if (*src_iter == quote)
				quote = 0;
			else if (*src_iter == '\\')
				escaped = true;
			else
				*dst_iter++ = *src_iter;
		}
		else if (isspace((unsigned int)*src_iter))
		{
			if ((*argv)[argc_count] != NULL)
			{
				argc_count++, dst_iter++;

				if (argc_count == argv_size)
				{
					argv_size += 5;
					*argv = realloc(*argv, sizeof(void *) * argv_size);
				}

				(*argv)[argc_count] = dst_iter;
			}
		}
		else switch(*src_iter)
		{
			case '\\':
				escaped = true;
				break;

			case '\"':
			case '\'':
				quote = *src_iter;
				break;

			default:
				*dst_iter++ = *src_iter;
				break;
		}

		src_iter++;
	}

	if (escaped || quote)
	{
		free(*argv);
		free(buf);
		return -1;
	}

	if (strlen((*argv)[argc_count]))
	{
		argc_count++;
	}

	*argc = argc_count;
	return 0;
}