summaryrefslogtreecommitdiff
path: root/cmd/service/dumpscript.c
blob: 23966ee46e98bb3816e563f66a1e04a304371ce2 (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
/* SPDX-License-Identifier: ISC */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include "servicecmd.h"

enum {
	NEED_QUOTES = 0x01,
	NEED_ESCAPE = 0x02,
};

static int check_str(const char *str)
{
	int ret = 0;

	while (*str != '\0') {
		if (isspace(*str))
			ret |= NEED_QUOTES;

		if (!isascii(*str) || !isprint(*str))
			ret |= NEED_ESCAPE | NEED_QUOTES;

		if (*str == '\\' || *str == '"' || *str == '\n')
			ret |= NEED_ESCAPE | NEED_QUOTES;

		++str;
	}

	return ret;
}

static void print_str(const char *str)
{
	int flags = check_str(str);

	if (flags & NEED_QUOTES)
		fputc('"', stdout);

	if (flags & NEED_ESCAPE) {
		while (*str != '\0') {
			switch (*str) {
			case '\a': fputs("\\a", stdout); break;
			case '\b': fputs("\\b", stdout); break;
			case '\f': fputs("\\f", stdout); break;
			case '\r': fputs("\\r", stdout); break;
			case '\t': fputs("\\t", stdout); break;
			case '\n': fputs("\\n", stdout); break;
			case '\\':
			case '"':
				fprintf(stdout, "\\%c", *str);
				break;
			default:
				if (!isascii(*str) || !isprint(*str)) {
					fprintf(stdout, "\\x%02X", *str);
				} else {
					fputc(*str, stdout);
				}
				break;
			}
			++str;
		}
	} else {
		fputs(str, stdout);
	}

	if (flags & NEED_QUOTES)
		fputc('"', stdout);
}

static int cmd_dumpscript(int argc, char **argv)
{
	char *filename, *ptr;
	service_t *svc;
	size_t len;
	exec_t *e;
	int i;

	if (check_arguments(argv[0], argc, 2, 3))
		return EXIT_FAILURE;

	for (len = 1, i = 1; i < argc; ++i)
		len += strlen(argv[i]) + 1;

	filename = alloca(len);
	filename[0] = '\0';

	for (i = 1; i < argc; ++i) {
		if (i > 1)
			strcat(filename, "@");
		strcat(filename, argv[i]);
	}

	svc = loadsvc(SVCDIR, filename, 0);

	if (svc == NULL) {
		fprintf(stderr, "Could not load service '%s'\n", filename);
		return EXIT_FAILURE;
	}

	fprintf(stdout, "#\n# commands executed for serice '%s'\n#\n",
		filename);

	for (e = svc->exec; e != NULL; e = e->next) {
		ptr = e->args;

		for (i = 0; i < e->argc; ++i) {
			if (i)
				fputc(' ', stdout);

			print_str(ptr);
			ptr += strlen(ptr) + 1;
		}

		putchar('\n');
	}

	delsvc(svc);
	return EXIT_SUCCESS;
}

static command_t dumpscript = {
	.cmd = "dumpscript",
	.usage = "<name> [arguments]",
	.s_desc = "print commands executed for a service",
	.l_desc = "This parses a service file from " SVCDIR " and "
		  "produces a pseudo-shell-script containing the "
		  "exact commands after argument expansion that init "
		  "runs when starting the service.",
	.run_cmd = cmd_dumpscript,
};

REGISTER_COMMAND(dumpscript)