summaryrefslogtreecommitdiffabout
path: root/src/syslog-port.c
blob: 4b27ef97677df58433e04413e2970aa271f95675 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* This file is part of Gamma.
   Copyright (C) 2002, 2010 Sergey Poznyakoff

   Gamma 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, or (at your option)
   any later version.

   Gamma 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 Gamma.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <libguile.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>

#ifndef HAVE_SCM_T_OFF
typedef off_t scm_t_off;
#endif
 
#define GAMMA_SYSLOG_PORT_BUFSIZE 1024
static scm_t_bits scm_tc16_syslog_port;
struct _gamma_syslog_port {
	int prio;
};

static SCM
_make_syslog_port(int prio)
{
	struct _gamma_syslog_port *dp;
	SCM port;
	scm_port *pt;

	dp = scm_gc_malloc (sizeof (struct _gamma_syslog_port), "syslog-port");
	dp->prio = prio;

	port = scm_new_port_table_entry(scm_tc16_syslog_port);
	pt = SCM_PTAB_ENTRY(port);
	pt->rw_random = 0;
	pt->write_buf = scm_gc_malloc(GAMMA_SYSLOG_PORT_BUFSIZE,
				      "syslog port buffer");
	pt->write_pos = pt->write_buf;
	pt->write_buf_size = GAMMA_SYSLOG_PORT_BUFSIZE;
	pt->write_end = pt->write_buf + pt->write_buf_size;
	SCM_SET_CELL_TYPE(port,
			  (scm_tc16_syslog_port |
			   SCM_OPN | SCM_WRTNG | SCM_BUFLINE));
	SCM_SETSTREAM(port, dp);
	return port;
}

#define SYSLOG_PORT(x) ((struct _gamma_syslog_port *) SCM_STREAM (x))

static SCM
_syslog_port_mark(SCM port)
{
	return SCM_BOOL_F;
}

static void
_syslog_port_flush(SCM port)
{
	struct _gamma_syslog_port *dp = SYSLOG_PORT(port);
	scm_port *pt = SCM_PTAB_ENTRY(port);
	size_t size = pt->write_pos - pt->write_buf;
	unsigned char *nl = memchr(pt->write_buf, '\n', size);
	int wrsize;

	if (!nl)
		return;
  
	wrsize = nl - pt->write_buf;

	*nl = 0;
	syslog (dp->prio, "%s", (char *) pt->write_buf);
	*nl = '\n';
  
	if (wrsize < size) {
		size_t write_start;

		nl++;
		write_start = pt->write_pos - nl;
		memmove(pt->write_buf, nl, write_start);
		pt->write_pos = pt->write_buf + write_start;
	} else
		pt->write_pos = pt->write_buf;
}

static int
_syslog_port_close(SCM port)
{
	struct _gamma_syslog_port *dp = SYSLOG_PORT(port);
		  
	if (dp) {
		_syslog_port_flush(port);
		SCM_SETSTREAM(port, NULL);
		scm_gc_free(dp, sizeof(struct _gamma_syslog_port),
			    "syslog-port");
	}
	return 0;
}

static scm_sizet
_syslog_port_free(SCM port)
{
	_syslog_port_close(port);
	return 0;
}

static int
_syslog_port_fill_input(SCM port)
{
	return EOF;
}

static void
_syslog_port_write(SCM port, const void *data, size_t size)
{
	scm_port *pt = SCM_PTAB_ENTRY (port);
	size_t space = pt->write_end - pt->write_pos;
	if (space < size) {
		size_t start = pt->write_pos - pt->write_buf;
		size_t new_size = pt->write_buf_size;
      
		do {
			/*FIXME*/
			new_size *= 2;
		} while (new_size - start < size);
      
		pt->write_buf = scm_gc_realloc(pt->write_buf,
					       pt->write_buf_size,
					       new_size,
					       "syslog port buffer");
		pt->write_buf_size = new_size;
		pt->write_end = pt->write_buf + pt->write_buf_size;
		pt->write_pos = pt->write_buf + start;
	}
	memcpy(pt->write_pos, data, size);
	pt->write_pos += size;

	if (memchr (data, '\n', size))
		_syslog_port_flush(port);
}

static scm_t_off
_syslog_port_seek(SCM port, scm_t_off offset, int whence)
{
	return (scm_t_off) -1;
}

static int
_syslog_port_print(SCM exp, SCM port, scm_print_state *pstate)
{
	scm_puts("#<", port);
	scm_print_port_mode(exp, port);
	scm_puts("Gamma syslog port>", port);
	return 1;
}

SCM_DEFINE_PUBLIC(scm_open_syslog_port, "open-syslog-port", 1, 0, 0,
		  (SCM prio),
"Create and open an output port connected to syslog priority "
"@code{prio}.")
#define FUNC_NAME s_scm_open_syslog_port
{
	SCM_ASSERT(scm_is_integer(prio), prio, SCM_ARG1, FUNC_NAME);
	return _make_syslog_port(scm_to_int(prio));
}
#undef FUNC_NAME	   

void
_gamma_init_syslog_port()
{
	scm_tc16_syslog_port = scm_make_port_type("syslog-port",
						  _syslog_port_fill_input,
						  _syslog_port_write);
	scm_set_port_mark(scm_tc16_syslog_port, _syslog_port_mark);
	scm_set_port_free(scm_tc16_syslog_port, _syslog_port_free);
	scm_set_port_print(scm_tc16_syslog_port, _syslog_port_print);
	scm_set_port_flush(scm_tc16_syslog_port, _syslog_port_flush);
	scm_set_port_close(scm_tc16_syslog_port, _syslog_port_close);
	scm_set_port_seek(scm_tc16_syslog_port, _syslog_port_seek);
#include <syslog-port.x>	
}

Return to:

Send suggestions and report system problems to the System administrator.