summaryrefslogtreecommitdiff
path: root/libmu_scm/mu_body.c
blob: ff88759ecd2d75c64c3dc742c6846eaade23a075 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2005, 2006, 2007, 2010 Free Software
   Foundation, Inc.

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

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA */

#include "mu_scm.h"

static scm_t_bits body_tag;

struct mu_body
{
  mu_body_t body;             /* Message body */
  mu_stream_t stream;         /* Associated stream */
  int offset;              /* Current offset in the stream */
  char *buffer;            /* I/O buffer */
  int bufsize;             /* Size of allocated buffer */
  SCM msg;                 /* Message the body belongs to */		
};

/* Initial buffer size */
#define BUF_SIZE 64

/* SMOB functions: */
static SCM
mu_scm_body_mark (SCM body_smob)
{
  struct mu_body *mbp = (struct mu_body *) SCM_CDR (body_smob);
  return mbp->msg;
}

static scm_sizet
mu_scm_body_free (SCM body_smob)
{
  struct mu_body *mbp = (struct mu_body *) SCM_CDR (body_smob);
  if (mbp->buffer)
    free (mbp->buffer);
  free (mbp);
  return sizeof (struct mu_body);
}

static int
mu_scm_body_print (SCM body_smob, SCM port, scm_print_state * pstate)
{
  struct mu_body *mbp = (struct mu_body *) SCM_CDR (body_smob);
  size_t b_size = 0, b_lines = 0, len = 0;
  char buffer[512];

  mu_body_size (mbp->body, &b_size);
  mu_body_lines (mbp->body, &b_lines);
  buffer[0] = 0;
  mu_body_get_filename (mbp->body, buffer, sizeof (buffer), &len);

  scm_puts ("#<body \"", port);
  scm_puts (buffer, port);
  scm_puts ("\" ", port);

  snprintf (buffer, sizeof (buffer), "%3lu %-5lu",
	    (unsigned long) b_lines, (unsigned long) b_size);
  scm_puts (buffer, port);

  scm_puts (">", port);
  return 1;
}

/* Internal functions: */

int
mu_scm_is_body (SCM scm)
{
  return SCM_NIMP (scm) && (long) SCM_CAR (scm) == body_tag;
}

SCM
mu_scm_body_create (SCM msg, mu_body_t body)
{
  struct mu_body *mbp;

  mbp = scm_gc_malloc (sizeof (struct mu_body), "body");
  mbp->msg = msg;
  mbp->body = body;
  mbp->stream = NULL;
  mbp->offset = 0;
  mbp->buffer = NULL;
  mbp->bufsize = 0;
  SCM_RETURN_NEWSMOB (body_tag, mbp);
}

/* ************************************************************************* */
/* Guile primitives */

SCM_DEFINE_PUBLIC (scm_mu_body_read_line, "mu-body-read-line", 1, 0, 0,
	    (SCM body), 
	    "Read next line from the @var{body}.")
#define FUNC_NAME s_scm_mu_body_read_line
{
  struct mu_body *mbp;
  size_t n, nread;
  int status;
  
  SCM_ASSERT (mu_scm_is_body (body), body, SCM_ARG1, FUNC_NAME);
  mbp = (struct mu_body *) SCM_CDR (body);

  if (!mbp->stream)
    {
      status = mu_body_get_stream (mbp->body, &mbp->stream);
      if (status)
	mu_scm_error (FUNC_NAME, status,
		      "Cannot get body stream",
		      SCM_BOOL_F);
    }

  if (!mbp->buffer)
    {
      mbp->bufsize = BUF_SIZE;
      mbp->buffer = malloc (mbp->bufsize);
      if (!mbp->buffer)
	mu_scm_error (FUNC_NAME, ENOMEM, "Cannot allocate memory", SCM_BOOL_F);
    }

  nread = 0;
  while (1)
    {
      status = mu_stream_readline (mbp->stream, mbp->buffer + nread,
				   mbp->bufsize - nread,
				   mbp->offset, &n);
      if (status)
	mu_scm_error (FUNC_NAME, status,
		      "Error reading from stream", SCM_BOOL_F);
      if (n == 0)
	break;
      nread += n;
      mbp->offset += n;
      if (mbp->buffer[n - 1] != '\n' && n == mbp->bufsize)
	{
	  char *p = realloc (mbp->buffer, mbp->bufsize + BUF_SIZE);
	  if (!p)
	    break;
	  mbp->buffer = p;
	  mbp->bufsize += BUF_SIZE;
	}
      else
	break;
    }

  if (nread == 0)
    return SCM_EOF_VAL;

  return scm_from_locale_string (mbp->buffer);
}
#undef FUNC_NAME

SCM_DEFINE_PUBLIC (scm_mu_body_write, "mu-body-write", 2, 0, 0,
	    (SCM body, SCM text),
	    "Append @var{text} to message @var{body}.")
#define FUNC_NAME s_scm_mu_body_write
{
  char *ptr;
  size_t len, n;
  struct mu_body *mbp;
  int status;
  
  SCM_ASSERT (mu_scm_is_body (body), body, SCM_ARG1, FUNC_NAME);
  mbp = (struct mu_body *) SCM_CDR (body);
  SCM_ASSERT (scm_is_string (text), text, SCM_ARG2, FUNC_NAME);
  
  if (!mbp->stream)
    {
      status = mu_body_get_stream (mbp->body, &mbp->stream);
      if (status)
	mu_scm_error (FUNC_NAME, status,
		      "Cannot get body stream", SCM_BOOL_F);
    }

  ptr = scm_to_locale_string (text);
  len = strlen (ptr);
  status = mu_stream_write (mbp->stream, ptr, len, mbp->offset, &n);
  free (ptr);
  mu_scm_error (FUNC_NAME, status,
		"Error writing to stream", SCM_BOOL_F);
  mbp->offset += n;
  return SCM_BOOL_T;
}
#undef FUNC_NAME

/* Initialize the module */
void
mu_scm_body_init ()
{
  body_tag = scm_make_smob_type ("body", sizeof (struct mu_body));
  scm_set_smob_mark (body_tag, mu_scm_body_mark);
  scm_set_smob_free (body_tag, mu_scm_body_free);
  scm_set_smob_print (body_tag, mu_scm_body_print);

#include "mu_body.x"

}

Return to:

Send suggestions and report system problems to the System administrator.