Blame view

stringex.c 3.11 KB
redmine authored
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
/*
    clsync - file tree sync utility based on inotify/kqueue/bsm

    Copyright (C) 2014  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>	// free()
#include <string.h>	// strtok_r()
#include <errno.h>	// errno

#include "malloc.h"
#include "error.h"

static int _str_splitargs(
		char *ptr,
		char **arg_start_p,
		int quotes,
		int (*handler)(char *, size_t, void *),
		char *additional_arg
) {
	char  *arg_start, *arg;
	size_t arg_len;
	int rc;

	 arg_start       = *arg_start_p;
	*arg_start_p     = &ptr[1];

	arg_len = ptr - arg_start;

	if (arg_len == 0)  // Skipping nearby spaces
		return 0;

	arg = xmalloc(arg_len+1);
	if (quotes) {
		int s, d;
		s = d = 0;
		while (s < arg_len) {
			if (arg_start[s])
				arg[d++] = arg_start[s];
			s++;
		}
		arg_len = d;
	} else
		memcpy(arg, arg_start, arg_len);

#ifdef _DEBUG
	debug(15, "%p %p %i: <%s>", arg_start, ptr, arg_len, arg);
#endif

	arg[arg_len] = 0;

	if ((rc = handler(arg, arg_len, additional_arg))) {
		free(arg);
		return rc;
	}
	return 0;
}

int str_splitargs(
redmine authored
73
		char *_instr,
redmine authored
74 75 76 77
		int (*handler)(char *, size_t, void *),
		void *arg
) {
	debug(9, "");
redmine authored
78
	char *arg_start, *ptr, *instr;
redmine authored
79 80
	int quotes = 0;

redmine authored
81
	instr     = strdup(_instr);
redmine authored
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
	ptr       = instr;
	arg_start = instr;
	while (1) {
		ptr = strpbrk(ptr, " \t\"\'");
#ifdef _DEBUG
		debug(10, "ptr == %p", ptr);
#endif
		if (ptr == NULL)
			break;

#ifdef _DEBUG
		debug(10, "*ptr == \"%c\" (%i)", *ptr, *ptr);
#endif
		switch (*(ptr++)) {
			case ' ':
			case '\t': {
				int rc;

				if ((rc = _str_splitargs(&ptr[-1], &arg_start, quotes, handler, arg)))
					return rc;
				quotes = 0;
				break;
			}
			case '"':
				ptr[-1] = 0;
				quotes++;
				while ((ptr = strchr(ptr, '"')) != NULL) {
					// Checking for escaping
					char *p;

					p = &ptr[-1];
					while (*p == '\\') {
						p--;
#ifdef PARANOID
						if (p < instr)
							critical("Dangerous internal error");
#endif
					}

					if ((ptr-p)%2)
						break;
				}
				if (ptr == NULL) {
					errno = EINVAL;
					error("Unterminated quote <\"> in string: <%s>", instr);
					return errno;
				}
				*ptr = 0;
				quotes++;
				ptr++;
				break;
			case '\'':
				ptr[-1] = 0;
				quotes++;
				ptr = strchr(ptr, '\'');
				if (ptr == NULL) {
					errno = EINVAL;
					error("Unterminated quote <'> in string: <%s>", instr);
					return errno;
				}
				*ptr = 0;
				quotes++;
				ptr++;
				break;
		}
	}

redmine authored
149 150 151
	int rc = _str_splitargs(strchr(arg_start, 0), &arg_start, quotes, handler, arg);
	free(instr);
	return rc;
redmine authored
152
}