File: debconfclient.c

package info (click to toggle)
cdebconf 0.138lenny3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,184 kB
  • ctags: 1,371
  • sloc: ansic: 14,158; sh: 469; makefile: 429; sql: 51; perl: 13
file content (121 lines) | stat: -rw-r--r-- 2,604 bytes parent folder | download | duplicates (4)
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
#include "debconfclient.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <stdarg.h>

#define DBC_DELETE(x) do { if (x) free(x); x = NULL; } while (0)

static int debconfclient_handle_rsp(struct debconfclient *client)
{
	char buf[2048];
	char *v;

	fgets(buf, sizeof(buf), stdin);
	if (strlen(buf) > 0)
	{
		if (buf[strlen(buf)-1] == '\n')
			buf[strlen(buf)-1] = '\0';
		/* strip off the return code */
		strtok(buf, " \t\n");
		DBC_DELETE(client->value);
		v = strtok(NULL, "\n");
		if (v == NULL)
			client->value = strdup("");
		else
			client->value = strdup(v);
		return atoi(buf);
	}
	else
	{
		/*
		 * Nothing was entered; never really happens except during
		 * debugging.
		 */
		DBC_DELETE(client->value);
		client->value = strdup("");
		return 0;
	}
}

static int debconfclient_command(struct debconfclient *client,
	const char *command, ...)
{
	va_list ap;
	char *c;

	fputs(command, client->out);
	va_start(ap, command);
	while ((c = va_arg(ap, char *)) != NULL)
	{
		fputs(" ", client->out);
		fputs(c, client->out);
	}
	va_end(ap);
	fputs("\n", client->out);
	fflush(client->out); /* make sure debconf sees it to prevent deadlock */

	return debconfclient_handle_rsp(client);
}

static int debconfclient_commandf(struct debconfclient *client,
	const char *cmd, ...)
{
	va_list ap;

	va_start(ap, cmd);
	vfprintf(client->out, cmd, ap);
	va_end(ap);
	fprintf(client->out, "\n");
	fflush(client->out);

	return debconfclient_handle_rsp(client);
}

static char *debconfclient_ret(struct debconfclient *client)
{
	return client->value;
}

void debconfclient_init(void) __attribute__ ((constructor));
void debconfclient_init(void)
{
}

struct debconfclient *debconfclient_new(void)
{
	struct debconfclient *client = (struct debconfclient *)
		malloc(sizeof(struct debconfclient));
	memset(client, 0, sizeof(struct debconfclient));

	if (getenv("DEBCONF_REDIR") == NULL)
	{
		dup2(DEBCONF_OLD_STDOUT_FD, 1);
		setenv("DEBCONF_REDIR", "1", 1);
	}

	client->command = debconfclient_command;
	client->commandf = debconfclient_commandf;
	client->ret = debconfclient_ret;
	client->out = fdopen(3, "a");
	/* If we aren't connected to cdebconf, try talking to stdout. It
	 * might be (Perl) debconf, or a developer might just want to talk
	 * debconf protocol to us by hand. Either way, it's better than
	 * segfaulting.
	 */
	if (!client->out)
		client->out = stdout;

	return client;
}

void debconfclient_delete(struct debconfclient *client)
{
	DBC_DELETE(client->value);
	DBC_DELETE(client);
}