File: split.c

package info (click to toggle)
libsdl-console 2.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 1,284 kB
  • sloc: sh: 7,371; ansic: 1,482; makefile: 49
file content (60 lines) | stat: -rw-r--r-- 922 bytes parent folder | download | duplicates (3)
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
#include "split.h"

/*
 * splitline is a destructive argument parser, much like a very primitive
 * form of a shell parser. it supports quotes for embedded spaces and
 * literal quotes with the backslash escape.
 */


char *splitnext(char **pos) {
	char *a, *d, *s;

	s = *pos;
	while (*s == ' ' || *s == '\t')
		s++;
	a = d = s;
	//printf("tokenbegin:%s\\0\n", a);

	while (*s && *s != ' ' && *s != '\t') {
		if (*s == '"') {
			s++;
			while (*s && *s != '"') {
				if (*s == '\\')
					s++;
				if (*s)
					*(d++) = *(s++);
			}
			if (*s == '"')
				s++;
		} else {
			if (*s == '\\')
				s++;
			*(d++) = *(s++);
		}
	}
	while (*s == ' ' || *s == '\t')
		s++;
	*d = 0;
	*pos = s;
	//  printf("token:%s\\0\n", a);
	return a;
}

int splitline(char **argv, int max, char *line) {
	char *s;
	int i = 0;

	s = line;
	while(*s && i < max) {
		argv[i] = splitnext(&s);
		i++;
	}
	if(!argv[0])
		return(0);
	return i;
}