Commit 0b3ced06 0b3ced0625d37c283632e93f0d49360c668cb459 by Alain Magloire

Adding more comments, and in message_destroy make sure we

destroy all the objects attached to it.
1 parent 33e314b0
...@@ -53,6 +53,12 @@ ...@@ -53,6 +53,12 @@
53 #define ATTRIBUTE_IS_DELETED(flag) (flag & MU_ATTRIBUTE_DELETED) 53 #define ATTRIBUTE_IS_DELETED(flag) (flag & MU_ATTRIBUTE_DELETED)
54 #define ATTRIBUTE_IS_EQUAL(flag1, flag2) (flag1 == flag2) 54 #define ATTRIBUTE_IS_EQUAL(flag1, flag2) (flag1 == flag2)
55 55
56 struct _unix_message;
57 struct _unix_data;
58
59 typedef struct _unix_data* unix_data_t;
60 typedef struct _unix_message* unix_message_t;
61
56 static int unix_create (mailbox_t *pmbox, const char *name); 62 static int unix_create (mailbox_t *pmbox, const char *name);
57 static void unix_destroy (mailbox_t *pmbox); 63 static void unix_destroy (mailbox_t *pmbox);
58 64
...@@ -64,7 +70,7 @@ struct mailbox_registrar _mailbox_unix_registrar = ...@@ -64,7 +70,7 @@ struct mailbox_registrar _mailbox_unix_registrar =
64 70
65 /* Keep the position of where the header and body starts and ends. 71 /* Keep the position of where the header and body starts and ends.
66 old_flags is the "Status:" message. */ 72 old_flags is the "Status:" message. */
67 typedef struct _unix_message 73 struct _unix_message
68 { 74 {
69 /* Offset of the parts of the messages in the mailbox. */ 75 /* Offset of the parts of the messages in the mailbox. */
70 off_t header_from; 76 off_t header_from;
...@@ -90,7 +96,7 @@ typedef struct _unix_message ...@@ -90,7 +96,7 @@ typedef struct _unix_message
90 /* A message attach to it. */ 96 /* A message attach to it. */
91 message_t message; 97 message_t message;
92 98
93 } *unix_message_t; 99 };
94 100
95 /* The umessages is an array of pointers that contains umessages_count of 101 /* The umessages is an array of pointers that contains umessages_count of
96 unix_message_t*; umessages[umessages_count]. We do it this because 102 unix_message_t*; umessages[umessages_count]. We do it this because
...@@ -98,7 +104,7 @@ typedef struct _unix_message ...@@ -98,7 +104,7 @@ typedef struct _unix_message
98 the pointers someone has on the messages. Thanks to <Dave Inglis> for 104 the pointers someone has on the messages. Thanks to <Dave Inglis> for
99 pointing this out. The messages_count is the count number of messages 105 pointing this out. The messages_count is the count number of messages
100 parsed so far. */ 106 parsed so far. */
101 typedef struct _unix_data 107 struct _unix_data
102 { 108 {
103 unix_message_t *umessages; 109 unix_message_t *umessages;
104 size_t umessages_count; 110 size_t umessages_count;
...@@ -118,8 +124,9 @@ typedef struct _unix_data ...@@ -118,8 +124,9 @@ typedef struct _unix_data
118 char *date; 124 char *date;
119 off_t off; 125 off_t off;
120 126
121 } *unix_data_t; 127 };
122 128
129 /* Mailbox implementation. */
123 static int unix_open (mailbox_t mbox, int flag); 130 static int unix_open (mailbox_t mbox, int flag);
124 static int unix_close (mailbox_t mbox); 131 static int unix_close (mailbox_t mbox);
125 static int unix_get_message (mailbox_t, size_t msgno, message_t *msg); 132 static int unix_get_message (mailbox_t, size_t msgno, message_t *msg);
...@@ -249,6 +256,7 @@ unix_create (mailbox_t *pmbox, const char *name) ...@@ -249,6 +256,7 @@ unix_create (mailbox_t *pmbox, const char *name)
249 return 0; /* okdoke */ 256 return 0; /* okdoke */
250 } 257 }
251 258
259 /* Free all ressources associated with Unix mailbox. */
252 static void 260 static void
253 unix_destroy (mailbox_t *pmbox) 261 unix_destroy (mailbox_t *pmbox)
254 { 262 {
......
...@@ -22,8 +22,7 @@ ...@@ -22,8 +22,7 @@
22 /* Parsing. 22 /* Parsing.
23 * The approach is to detect the "From " as start of a 23 * The approach is to detect the "From " as start of a
24 * new message, give the position of the header and scan 24 * new message, give the position of the header and scan
25 * until "\n" then set header_end, set body position, if we have 25 * until "\n" then set header_end, set body position,
26 * a Content-Length field jump to the point if not
27 * scan until we it another "From " and set body_end. 26 * scan until we it another "From " and set body_end.
28 * 27 *
29 ************************************ 28 ************************************
...@@ -187,7 +186,7 @@ do \ ...@@ -187,7 +186,7 @@ do \
187 (buf[4] == 'U' || buf[4] == 'u') && \ 186 (buf[4] == 'U' || buf[4] == 'u') && \
188 (buf[5] == 'S' || buf[5] == 's') && (buf[6] == ':')) 187 (buf[5] == 'S' || buf[5] == 's') && (buf[6] == ':'))
189 188
190 /* notification */ 189 /* Notification. */
191 #define MAILBOX_NOTIFICATION(mbox,which,bailing) \ 190 #define MAILBOX_NOTIFICATION(mbox,which,bailing) \
192 do \ 191 do \
193 { \ 192 { \
...@@ -201,7 +200,7 @@ do \ ...@@ -201,7 +200,7 @@ do \
201 } \ 200 } \
202 } while (0) 201 } while (0)
203 202
204 /* notifications ADD_MESG */ 203 /* Notifications ADD_MESG. */
205 #define DISPATCH_ADD_MSG(mbox,mud) \ 204 #define DISPATCH_ADD_MSG(mbox,mud) \
206 do \ 205 do \
207 { \ 206 { \
...@@ -218,17 +217,12 @@ do \ ...@@ -218,17 +217,12 @@ do \
218 unix_ilock (mbox, MU_LOCKER_WRLOCK); \ 217 unix_ilock (mbox, MU_LOCKER_WRLOCK); \
219 } while (0); 218 } while (0);
220 219
221 /* notification MBX_PROGRESS 220 /* Notification MBX_PROGRESS
222 * We do not want to fire up the progress notification 221 We do not want to fire up the progress notification every line, it will be
223 * every line, it will be too expensive, so we do it 222 too expensive, so we do it arbitrarely every 10 000 Lines.
224 * arbitrarely every 10 000 Lines. 223 FIXME: maybe this should be configurable. */
225 * FIXME: maybe this should be configurable. 224 /* This is more tricky we can not leave the mum struct incomplete. So we
226 */ 225 only tell them about the complete messages. */
227 /*
228 * This is more tricky we can not leave the mum
229 * struct incomplete. So we only tell them about
230 * the complete messages.
231 */
232 #define DISPATCH_PROGRESS(mbox,mud) \ 226 #define DISPATCH_PROGRESS(mbox,mud) \
233 do \ 227 do \
234 { \ 228 { \
...@@ -249,25 +243,7 @@ do \ ...@@ -249,25 +243,7 @@ do \
249 } \ 243 } \
250 } while (0) 244 } while (0)
251 245
252 #if 0 246 /* Allocate slots for the new messages. */
253 /* skip a function call, ?? do we gain that much */
254 #define ATTRIBUTE_CREATE(attr, m, mbox) \
255 do \
256 { \
257 attr = calloc (1, sizeof(*(attr))); \
258 attr->owner = m; \
259 if ((attr) == NULL) \
260 { \
261 unix_iunlock (mbox); \
262 unix_unlock (mbox); \
263 return ENOMEM; \
264 } \
265 } while (0)
266 #else
267 # define ATTRIBUTE_CREATE
268 #endif
269
270 /* allocate slots for the new messages */
271 /* size_t num = 2 * ((mud)->messages_count) + 10; */ 247 /* size_t num = 2 * ((mud)->messages_count) + 10; */
272 #define ALLOCATE_MSGS(mbox,mud) \ 248 #define ALLOCATE_MSGS(mbox,mud) \
273 do \ 249 do \
...@@ -303,7 +279,7 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif) ...@@ -303,7 +279,7 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif)
303 int inheader; 279 int inheader;
304 int inbody; 280 int inbody;
305 off_t total = 0; 281 off_t total = 0;
306 unix_data_t mud; 282 unix_data_t mud = mbox->data;
307 unix_message_t mum = NULL; 283 unix_message_t mum = NULL;
308 int status = 0; 284 int status = 0;
309 size_t lines; 285 size_t lines;
...@@ -313,21 +289,20 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif) ...@@ -313,21 +289,20 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif)
313 int zn, isfrom = 0; 289 int zn, isfrom = 0;
314 char *temp; 290 char *temp;
315 291
316 /* sanity */ 292 /* Sanity. */
317 if (mbox == NULL || 293 if (mud == NULL)
318 (mud = (unix_data_t)mbox->data) == NULL)
319 return EINVAL; 294 return EINVAL;
320 295
321 /* save the timestamp and size */ 296 /* Save the timestamp and size. */
322 status = stream_size (mbox->stream, &(mud->size)); 297 status = stream_size (mbox->stream, &(mud->size));
323 if (status != 0) 298 if (status != 0)
324 return status; 299 return status;
325 300
326 /* grab the locks */ 301 /* Grab the locks. */
327 unix_ilock (mbox, MU_LOCKER_WRLOCK); 302 unix_ilock (mbox, MU_LOCKER_WRLOCK);
328 unix_lock (mbox, MU_LOCKER_RDLOCK); 303 unix_lock (mbox, MU_LOCKER_RDLOCK);
329 304
330 /* seek to the starting point */ 305 /* Seek to the starting point. */
331 if (mud->umessages && msgno > 0 && mud->messages_count > 0 306 if (mud->umessages && msgno > 0 && mud->messages_count > 0
332 && msgno <= mud->messages_count) 307 && msgno <= mud->messages_count)
333 { 308 {
...@@ -352,7 +327,7 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif) ...@@ -352,7 +327,7 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif)
352 VALID(buf, temp, isfrom, zn); 327 VALID(buf, temp, isfrom, zn);
353 isfrom = (isfrom) ? 1 : 0; 328 isfrom = (isfrom) ? 1 : 0;
354 329
355 /* which part of the message are we in ? */ 330 /* Which part of the message are we in ? */
356 inheader = isfrom | ((!nl) & inheader); 331 inheader = isfrom | ((!nl) & inheader);
357 inbody = (!isfrom) & (!inheader); 332 inbody = (!isfrom) & (!inheader);
358 333
...@@ -360,10 +335,10 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif) ...@@ -360,10 +335,10 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif)
360 335
361 if (inheader) 336 if (inheader)
362 { 337 {
363 /* new message */ 338 /* New message. */
364 if (isfrom) 339 if (isfrom)
365 { 340 {
366 /* signal the end of the body */ 341 /* Signal the end of the body. */
367 if (mum && !mum->body_end) 342 if (mum && !mum->body_end)
368 { 343 {
369 mum->body_end = total - n - newline; 344 mum->body_end = total - n - newline;
...@@ -371,7 +346,7 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif) ...@@ -371,7 +346,7 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif)
371 if (do_notif) 346 if (do_notif)
372 DISPATCH_ADD_MSG(mbox, mud); 347 DISPATCH_ADD_MSG(mbox, mud);
373 } 348 }
374 /* allocate_msgs will initialize mum */ 349 /* Allocate_msgs will initialize mum. */
375 ALLOCATE_MSGS(mbox, mud); 350 ALLOCATE_MSGS(mbox, mud);
376 mud->messages_count++; 351 mud->messages_count++;
377 mum = mud->umessages[mud->messages_count - 1]; 352 mum = mud->umessages[mud->messages_count - 1];
...@@ -391,10 +366,10 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif) ...@@ -391,10 +366,10 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif)
391 } 366 }
392 } 367 }
393 368
394 /* body */ 369 /* Body. */
395 if (inbody) 370 if (inbody)
396 { 371 {
397 /* set the body position */ 372 /* Set the body position. */
398 if (mum && !mum->body) 373 if (mum && !mum->body)
399 { 374 {
400 mum->body = total - n + nl; 375 mum->body = total - n + nl;
...@@ -405,11 +380,11 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif) ...@@ -405,11 +380,11 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif)
405 380
406 newline = nl; 381 newline = nl;
407 382
408 /* every 50 mesgs update the lock, it should be every minute */ 383 /* Every 50 mesgs update the lock, it should be every minute. */
409 if ((mud->messages_count % 50) == 0) 384 if ((mud->messages_count % 50) == 0)
410 unix_touchlock (mbox); 385 unix_touchlock (mbox);
411 386
412 /* ping them every 1000 lines */ 387 /* Ping them every 1000 lines. */
413 if (do_notif) 388 if (do_notif)
414 if (((lines +1) % 1000) == 0) 389 if (((lines +1) % 1000) == 0)
415 DISPATCH_PROGRESS(mbox, mud); 390 DISPATCH_PROGRESS(mbox, mud);
...@@ -429,4 +404,3 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif) ...@@ -429,4 +404,3 @@ unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif)
429 *pcount = mud->messages_count; 404 *pcount = mud->messages_count;
430 return status; 405 return status;
431 } 406 }
432
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
27 #include <time.h> 27 #include <time.h>
28 #include <string.h> 28 #include <string.h>
29 29
30 /* FIXME: This should be part of the address_t object when implemented. */
30 static int extract_addr(const char *s, size_t n, char **presult, 31 static int extract_addr(const char *s, size_t n, char **presult,
31 size_t *pnwrite); 32 size_t *pnwrite);
32 static int message_read (stream_t is, char *buf, size_t buflen, 33 static int message_read (stream_t is, char *buf, size_t buflen,
...@@ -35,6 +36,7 @@ static int message_write (stream_t os, const char *buf, size_t buflen, ...@@ -35,6 +36,7 @@ static int message_write (stream_t os, const char *buf, size_t buflen,
35 off_t off, size_t *pnwrite); 36 off_t off, size_t *pnwrite);
36 static int message_get_fd (stream_t stream, int *pfd); 37 static int message_get_fd (stream_t stream, int *pfd);
37 38
39 /* Allocate ressources for the message_t. */
38 int 40 int
39 message_create (message_t *pmsg, void *owner) 41 message_create (message_t *pmsg, void *owner)
40 { 42 {
...@@ -59,31 +61,45 @@ message_destroy (message_t *pmsg, void *owner) ...@@ -59,31 +61,45 @@ message_destroy (message_t *pmsg, void *owner)
59 61
60 if (msg->owner == owner) 62 if (msg->owner == owner)
61 { 63 {
62 /* notify the listeners */ 64 /* Notify the listeners. */
65 /* FIXME: to be removed since we do not supoort this event. */
63 if (msg->event_num) 66 if (msg->event_num)
64 { 67 {
65 message_notification (msg, MU_EVT_MSG_DESTROY); 68 message_notification (msg, MU_EVT_MSG_DESTROY);
66 free (msg->event); 69 free (msg->event);
67 } 70 }
68 /* header */ 71
72 /* Header. */
73 if (msg->header)
69 header_destroy (&(msg->header), owner); 74 header_destroy (&(msg->header), owner);
70 /* attribute */ 75 if (msg->header)
76 header_destroy (&(msg->header), msg);
77
78 /* Attribute. */
79 if (msg->attribute)
71 attribute_destroy (&(msg->attribute), owner); 80 attribute_destroy (&(msg->attribute), owner);
72 /* stream */ 81 if (msg->attribute)
82 attribute_destroy (&(msg->attribute), msg);
83
84 /* Stream. */
85 if (msg->stream)
73 stream_destroy (&(msg->stream), owner); 86 stream_destroy (&(msg->stream), owner);
87 if (msg->stream)
88 stream_destroy (&(msg->stream), msg);
74 89
75 /* if sometype of floating/temporary message */ 90 /* Body. */
91 if (msg->body)
76 body_destroy (&(msg->body), owner); 92 body_destroy (&(msg->body), owner);
77 /* notifications are done */ 93 if (msg->body)
94 body_destroy (&(msg->body), msg);
95
96 /* Mime. */
97 if (msg->mime)
98 mime_destroy (&(msg->mime));
78 99
79 /* check again for resurrection before free()ing
80 * the memory maybe it was clone, if yes we can not
81 * free the pointer.
82 *
83 */
84 free (msg); 100 free (msg);
85 } 101 }
86 /* loose the link */ 102 /* Loose the link */
87 *pmsg = NULL; 103 *pmsg = NULL;
88 } 104 }
89 } 105 }
...@@ -94,8 +110,8 @@ message_get_header (message_t msg, header_t *phdr) ...@@ -94,8 +110,8 @@ message_get_header (message_t msg, header_t *phdr)
94 if (msg == NULL || phdr == NULL) 110 if (msg == NULL || phdr == NULL)
95 return EINVAL; 111 return EINVAL;
96 112
97 /* is it a floating mesg */ 113 /* Is it a floating mesg */
98 if (msg->header == NULL && msg->owner == NULL) 114 if (msg->header == NULL)
99 { 115 {
100 header_t header; 116 header_t header;
101 int status = header_create (&header, NULL, 0, msg); 117 int status = header_create (&header, NULL, 0, msg);
...@@ -114,8 +130,9 @@ message_set_header (message_t msg, header_t hdr, void *owner) ...@@ -114,8 +130,9 @@ message_set_header (message_t msg, header_t hdr, void *owner)
114 return EINVAL; 130 return EINVAL;
115 if (msg->owner != owner) 131 if (msg->owner != owner)
116 return EACCES; 132 return EACCES;
117 /* make sure we destoy the old if it was own by the mesg */ 133 /* Make sure we destoy the old if it was own by the mesg */
118 header_destroy (&(msg->header), msg); 134 /* FIXME: I do not know if somebody has already a ref on this ? */
135 /* header_destroy (&(msg->header), msg); */
119 msg->header = hdr; 136 msg->header = hdr;
120 return 0; 137 return 0;
121 } 138 }
...@@ -126,7 +143,7 @@ message_get_body (message_t msg, body_t *pbody) ...@@ -126,7 +143,7 @@ message_get_body (message_t msg, body_t *pbody)
126 if (msg == NULL || pbody == NULL) 143 if (msg == NULL || pbody == NULL)
127 return EINVAL; 144 return EINVAL;
128 145
129 /* is it a floating mesg */ 146 /* Is it a floating mesg. */
130 if (msg->body == NULL) 147 if (msg->body == NULL)
131 { 148 {
132 body_t body; 149 body_t body;
...@@ -146,8 +163,9 @@ message_set_body (message_t msg, body_t body, void *owner) ...@@ -146,8 +163,9 @@ message_set_body (message_t msg, body_t body, void *owner)
146 return EINVAL; 163 return EINVAL;
147 if (msg->owner != owner) 164 if (msg->owner != owner)
148 return EACCES; 165 return EACCES;
149 /* make sure we destoy the old if it was own by the mesg */ 166 /* Make sure we destoy the old if it was own by the mesg. */
150 body_destroy (&(msg->body), msg); 167 /* FIXME: I do not know if somebody has already a ref on this ? */
168 /* body_destroy (&(msg->body), msg); */
151 msg->body = body; 169 msg->body = body;
152 return 0; 170 return 0;
153 } 171 }
...@@ -205,7 +223,7 @@ message_lines (message_t msg, size_t *plines) ...@@ -205,7 +223,7 @@ message_lines (message_t msg, size_t *plines)
205 size_t hlines, blines; 223 size_t hlines, blines;
206 if (msg == NULL) 224 if (msg == NULL)
207 return EINVAL; 225 return EINVAL;
208 /* Overload */ 226 /* Overload. */
209 if (msg->_lines) 227 if (msg->_lines)
210 return msg->_lines (msg, plines); 228 return msg->_lines (msg, plines);
211 if (plines) 229 if (plines)
...@@ -272,11 +290,11 @@ message_from (message_t msg, char *buf, size_t len, size_t *pnwrite) ...@@ -272,11 +290,11 @@ message_from (message_t msg, char *buf, size_t len, size_t *pnwrite)
272 if (msg == NULL) 290 if (msg == NULL)
273 return EINVAL; 291 return EINVAL;
274 292
275 /* did they provide a way to get it */ 293 /* Did they provide a way to get it ? */
276 if (msg->_from) 294 if (msg->_from)
277 return msg->_from (msg, buf, len, pnwrite); 295 return msg->_from (msg, buf, len, pnwrite);
278 296
279 /* can it be extracted from the From: */ 297 /* Can it be extracted from the From: */
280 message_get_header (msg, &header); 298 message_get_header (msg, &header);
281 status = header_get_value (header, MU_HEADER_FROM, NULL, 0, &n); 299 status = header_get_value (header, MU_HEADER_FROM, NULL, 0, &n);
282 if (status == 0 && n != 0) 300 if (status == 0 && n != 0)
...@@ -335,14 +353,14 @@ message_received (message_t msg, char *buf, size_t len, size_t *pnwrite) ...@@ -335,14 +353,14 @@ message_received (message_t msg, char *buf, size_t len, size_t *pnwrite)
335 size_t n; 353 size_t n;
336 if (msg == NULL) 354 if (msg == NULL)
337 return EINVAL; 355 return EINVAL;
338 /* is it provided */ 356 /* Is it provided ? */
339 if (msg->_received) 357 if (msg->_received)
340 return msg->_received (msg, buf, len, pnwrite); 358 return msg->_received (msg, buf, len, pnwrite);
341 359
342 /* FIXME: extract the time from "Date:" */ 360 /* FIXME: extract the time from "Date:". */
343 361
344 /* catch all */ 362 /* Catch all. */
345 /* FIXME: ctime() is not thread safe use strftime() */ 363 /* FIXME: ctime() is not thread safe use strftime(). */
346 t = time (NULL); 364 t = time (NULL);
347 n = strlen (ctime (&t)); 365 n = strlen (ctime (&t));
348 366
...@@ -406,7 +424,7 @@ message_get_uidl (message_t msg, char *buffer, size_t buflen, size_t *pwritten) ...@@ -406,7 +424,7 @@ message_get_uidl (message_t msg, char *buffer, size_t buflen, size_t *pwritten)
406 424
407 int 425 int
408 message_set_uidl (message_t msg, int (* _get_uidl) 426 message_set_uidl (message_t msg, int (* _get_uidl)
409 __P ((message_t msg, char *buffer, size_t buflen, size_t *pwritten)), void *owner) 427 __P ((message_t, char *, size_t, size_t *)), void *owner)
410 { 428 {
411 if (msg == NULL) 429 if (msg == NULL)
412 return EINVAL; 430 return EINVAL;
...@@ -527,7 +545,7 @@ message_register (message_t msg, size_t type, ...@@ -527,7 +545,7 @@ message_register (message_t msg, size_t type,
527 if (msg == NULL || action == NULL || type == 0) 545 if (msg == NULL || action == NULL || type == 0)
528 return EINVAL; 546 return EINVAL;
529 547
530 /* find a free spot */ 548 /* Find a free spot. */
531 for (i = 0; i < msg->event_num; i++) 549 for (i = 0; i < msg->event_num; i++)
532 { 550 {
533 event = &(msg->event[i]); 551 event = &(msg->event[i]);
...@@ -633,7 +651,7 @@ message_write (stream_t os, const char *buf, size_t buflen, ...@@ -633,7 +651,7 @@ message_write (stream_t os, const char *buf, size_t buflen,
633 if (os == NULL || (msg = os->owner) == NULL) 651 if (os == NULL || (msg = os->owner) == NULL)
634 return EINVAL; 652 return EINVAL;
635 653
636 /* skip the obvious */ 654 /* Skip the obvious. */
637 if (buf == NULL || *buf == '\0' || buflen == 0) 655 if (buf == NULL || *buf == '\0' || buflen == 0)
638 { 656 {
639 if (pnwrite) 657 if (pnwrite)
...@@ -649,7 +667,7 @@ message_write (stream_t os, const char *buf, size_t buflen, ...@@ -649,7 +667,7 @@ message_write (stream_t os, const char *buf, size_t buflen,
649 while (!msg->hdr_done && (nl = memchr (buf, '\n', buflen)) != NULL) 667 while (!msg->hdr_done && (nl = memchr (buf, '\n', buflen)) != NULL)
650 { 668 {
651 len = nl - buf + 1; 669 len = nl - buf + 1;
652 /* allocate more buffer to hold the header */ 670 /* Allocate more buffer to hold the header. */
653 thdr = realloc (msg->hdr_buf, msg->hdr_buflen + len); 671 thdr = realloc (msg->hdr_buf, msg->hdr_buflen + len);
654 if (thdr == NULL) 672 if (thdr == NULL)
655 { 673 {
...@@ -662,8 +680,8 @@ message_write (stream_t os, const char *buf, size_t buflen, ...@@ -662,8 +680,8 @@ message_write (stream_t os, const char *buf, size_t buflen,
662 msg->hdr_buf = thdr; 680 msg->hdr_buf = thdr;
663 memcpy (msg->hdr_buf + msg->hdr_buflen, buf, len); 681 memcpy (msg->hdr_buf + msg->hdr_buflen, buf, len);
664 msg->hdr_buflen += len; 682 msg->hdr_buflen += len;
665 /* we detect an empty line .i.e "^\n$" this signal the end 683 /* We detect an empty line .i.e "^\n$" this signal the end of the
666 * of the header */ 684 header. */
667 if (buf == nl) 685 if (buf == nl)
668 { 686 {
669 header_destroy (&(msg->header), msg); 687 header_destroy (&(msg->header), msg);
...@@ -683,7 +701,7 @@ message_write (stream_t os, const char *buf, size_t buflen, ...@@ -683,7 +701,7 @@ message_write (stream_t os, const char *buf, size_t buflen,
683 } 701 }
684 } 702 }
685 703
686 /* message header is not complete but was not a full line */ 704 /* Message header is not complete but was not a full line. */
687 if (!msg->hdr_done && buflen > 0) 705 if (!msg->hdr_done && buflen > 0)
688 { 706 {
689 char *thdr = realloc (msg->hdr_buf, msg->hdr_buflen + buflen); 707 char *thdr = realloc (msg->hdr_buf, msg->hdr_buflen + buflen);
...@@ -700,7 +718,7 @@ message_write (stream_t os, const char *buf, size_t buflen, ...@@ -700,7 +718,7 @@ message_write (stream_t os, const char *buf, size_t buflen,
700 msg->hdr_buflen += buflen; 718 msg->hdr_buflen += buflen;
701 buflen = 0; 719 buflen = 0;
702 } 720 }
703 else if (buflen > 0) /* in the body */ 721 else if (buflen > 0) /* In the body. */
704 { 722 {
705 stream_t bs; 723 stream_t bs;
706 body_t body; 724 body_t body;
...@@ -735,11 +753,12 @@ message_get_fd (stream_t stream, int *pfd) ...@@ -735,11 +753,12 @@ message_get_fd (stream_t stream, int *pfd)
735 if (stream == NULL || (msg = stream->owner) == NULL) 753 if (stream == NULL || (msg = stream->owner) == NULL)
736 return EINVAL; 754 return EINVAL;
737 755
738 /* Probably being lazy, then create a body for the stream */ 756 /* Probably being lazy, then create a body for the stream. */
739 if (msg->body == NULL) 757 if (msg->body == NULL)
740 { 758 {
741 int status = body_create (&body, msg); 759 int status = body_create (&body, msg);
742 if (status != 0 ) 760 if (status != 0 )
761
743 return status; 762 return status;
744 msg->body = body; 763 msg->body = body;
745 } 764 }
...@@ -758,7 +777,7 @@ extract_addr (const char *s, size_t n, char **presult, size_t *pnwrite) ...@@ -758,7 +777,7 @@ extract_addr (const char *s, size_t n, char **presult, size_t *pnwrite)
758 if (s == NULL || n == 0 || presult == NULL) 777 if (s == NULL || n == 0 || presult == NULL)
759 return EINVAL; 778 return EINVAL;
760 779
761 /* skip the double quotes */ 780 /* Skip the double quotes. */
762 p = memchr (s, '\"', n); 781 p = memchr (s, '\"', n);
763 if (p != NULL) 782 if (p != NULL)
764 { 783 {
...@@ -785,7 +804,7 @@ extract_addr (const char *s, size_t n, char **presult, size_t *pnwrite) ...@@ -785,7 +804,7 @@ extract_addr (const char *s, size_t n, char **presult, size_t *pnwrite)
785 p2 = memchr (p, ' ', p1 - p); 804 p2 = memchr (p, ' ', p1 - p);
786 if (p2 == NULL) 805 if (p2 == NULL)
787 { 806 {
788 /* the NULL is already accounted for */ 807 /* The NULL is already accounted for. */
789 *presult = calloc (1, p1 - p); 808 *presult = calloc (1, p1 - p);
790 if (*presult == NULL) 809 if (*presult == NULL)
791 return ENOMEM; 810 return ENOMEM;
......