Implement CLOCK_MONOTONIC using getnanouptime(), which in DragonFly is
[dragonfly.git] / contrib / cvs-1.12.9 / src / buffer.c
1 /* Code for the buffer data structure.  */
2
3 #include "cvs.h"
4 #include "buffer.h"
5
6 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
7
8 # ifdef HAVE_WINSOCK_H
9 #   include <winsock.h>
10 # else
11 #  include <sys/socket.h>
12 # endif
13
14 /* OS/2 doesn't have EIO.  FIXME: this whole notion of turning
15    a different error into EIO strikes me as pretty dubious.  */
16 # if !defined( EIO )
17 #   define EIO EBADPOS
18 # endif
19
20 /* Linked list of available buffer_data structures.  */
21 static struct buffer_data *free_buffer_data;
22
23 /* Local functions.  */
24 static void buf_default_memory_error (struct buffer *);
25 static void allocate_buffer_datas (void);
26 static struct buffer_data *get_buffer_data (void);
27
28 /* Initialize a buffer structure.  */
29
30 struct buffer *
31 buf_initialize (int (*input) (void *, char *, int, int, int *),
32                 int (*output) (void *, const char *, int, int *),
33                 int (*flush) (void *),
34                 int (*block) (void *, int),
35                 int (*shutdown) (struct buffer *),
36                 void (*memory) (struct buffer *),
37                 void *closure)
38 {
39     struct buffer *buf;
40
41     buf = (struct buffer *) xmalloc (sizeof (struct buffer));
42     buf->data = NULL;
43     buf->last = NULL;
44     buf->nonblocking = 0;
45     buf->input = input;
46     buf->output = output;
47     buf->flush = flush;
48     buf->block = block;
49     buf->shutdown = shutdown;
50     buf->memory_error = memory ? memory : buf_default_memory_error;
51     buf->closure = closure;
52     return buf;
53 }
54
55 /* Free a buffer structure.  */
56
57 void
58 buf_free (struct buffer *buf)
59 {
60     if (buf->closure != NULL)
61     {
62         free (buf->closure);
63         buf->closure = NULL;
64     }
65     if (buf->data != NULL)
66     {
67         buf->last->next = free_buffer_data;
68         free_buffer_data = buf->data;
69     }
70     free (buf);
71 }
72
73 /* Initialize a buffer structure which is not to be used for I/O.  */
74
75 struct buffer *
76 buf_nonio_initialize( void (*memory) (struct buffer *) )
77 {
78     return (buf_initialize
79             ((int (*) (void *, char *, int, int, int *)) NULL,
80              (int (*) (void *, const char *, int, int *)) NULL,
81              (int (*) (void *)) NULL,
82              (int (*) (void *, int)) NULL,
83              (int (*) (struct buffer *)) NULL,
84              memory,
85              (void *) NULL));
86 }
87
88 /* Default memory error handler.  */
89
90 static void
91 buf_default_memory_error (struct buffer *buf)
92 {
93     error (1, 0, "out of memory");
94 }
95
96 /* Allocate more buffer_data structures.  */
97
98 static void
99 allocate_buffer_datas (void)
100 {
101     struct buffer_data *alc;
102     char *space;
103     int i;
104
105     /* Allocate buffer_data structures in blocks of 16.  */
106 # define ALLOC_COUNT (16)
107
108     alc = ((struct buffer_data *)
109            xmalloc (ALLOC_COUNT * sizeof (struct buffer_data)));
110     space = (char *) valloc (ALLOC_COUNT * BUFFER_DATA_SIZE);
111     if (alc == NULL || space == NULL)
112         return;
113     for (i = 0; i < ALLOC_COUNT; i++, alc++, space += BUFFER_DATA_SIZE)
114     {
115         alc->next = free_buffer_data;
116         free_buffer_data = alc;
117         alc->text = space;
118     }     
119 }
120
121 /* Get a new buffer_data structure.  */
122
123 static struct buffer_data *
124 get_buffer_data (void)
125 {
126     struct buffer_data *ret;
127
128     if (free_buffer_data == NULL)
129     {
130         allocate_buffer_datas ();
131         if (free_buffer_data == NULL)
132             return NULL;
133     }
134
135     ret = free_buffer_data;
136     free_buffer_data = ret->next;
137     return ret;
138 }
139
140
141
142 /* See whether a buffer and its file descriptor is empty.  */
143 int
144 buf_empty (buf)
145     struct buffer *buf;
146 {
147         /* Try and read any data on the file descriptor first.
148          * We already know the descriptor is non-blocking.
149          */
150         buf_input_data (buf, NULL);
151         return buf_empty_p (buf);
152 }
153
154
155
156 /* See whether a buffer is empty.  */
157 int
158 buf_empty_p (struct buffer *buf)
159 {
160     struct buffer_data *data;
161
162     for (data = buf->data; data != NULL; data = data->next)
163         if (data->size > 0)
164             return 0;
165     return 1;
166 }
167
168
169
170 # ifdef SERVER_FLOWCONTROL
171 /*
172  * Count how much data is stored in the buffer..
173  * Note that each buffer is a xmalloc'ed chunk BUFFER_DATA_SIZE.
174  */
175
176 int
177 buf_count_mem (struct buffer *buf)
178 {
179     struct buffer_data *data;
180     int mem = 0;
181
182     for (data = buf->data; data != NULL; data = data->next)
183         mem += BUFFER_DATA_SIZE;
184
185     return mem;
186 }
187 # endif /* SERVER_FLOWCONTROL */
188
189 /* Add data DATA of length LEN to BUF.  */
190
191 void
192 buf_output (struct buffer *buf, const char *data, int len)
193 {
194     if (buf->data != NULL
195         && (((buf->last->text + BUFFER_DATA_SIZE)
196              - (buf->last->bufp + buf->last->size))
197             >= len))
198     {
199         memcpy (buf->last->bufp + buf->last->size, data, len);
200         buf->last->size += len;
201         return;
202     }
203
204     while (1)
205     {
206         struct buffer_data *newdata;
207
208         newdata = get_buffer_data ();
209         if (newdata == NULL)
210         {
211             (*buf->memory_error) (buf);
212             return;
213         }
214
215         if (buf->data == NULL)
216             buf->data = newdata;
217         else
218             buf->last->next = newdata;
219         newdata->next = NULL;
220         buf->last = newdata;
221
222         newdata->bufp = newdata->text;
223
224         if (len <= BUFFER_DATA_SIZE)
225         {
226             newdata->size = len;
227             memcpy (newdata->text, data, len);
228             return;
229         }
230
231         newdata->size = BUFFER_DATA_SIZE;
232         memcpy (newdata->text, data, BUFFER_DATA_SIZE);
233
234         data += BUFFER_DATA_SIZE;
235         len -= BUFFER_DATA_SIZE;
236     }
237
238     /*NOTREACHED*/
239 }
240
241 /* Add a '\0' terminated string to BUF.  */
242
243 void
244 buf_output0 (struct buffer *buf, const char *string)
245 {
246     buf_output (buf, string, strlen (string));
247 }
248
249 /* Add a single character to BUF.  */
250
251 void
252 buf_append_char (struct buffer *buf, int ch)
253 {
254     if (buf->data != NULL
255         && (buf->last->text + BUFFER_DATA_SIZE
256             != buf->last->bufp + buf->last->size))
257     {
258         *(buf->last->bufp + buf->last->size) = ch;
259         ++buf->last->size;
260     }
261     else
262     {
263         char b;
264
265         b = ch;
266         buf_output (buf, &b, 1);
267     }
268 }
269
270 /*
271  * Send all the output we've been saving up.  Returns 0 for success or
272  * errno code.  If the buffer has been set to be nonblocking, this
273  * will just write until the write would block.
274  */
275
276 int
277 buf_send_output (struct buffer *buf)
278 {
279     if (buf->output == NULL)
280         abort ();
281
282     while (buf->data != NULL)
283     {
284         struct buffer_data *data;
285
286         data = buf->data;
287
288         if (data->size > 0)
289         {
290             int status, nbytes;
291
292             status = (*buf->output) (buf->closure, data->bufp, data->size,
293                                      &nbytes);
294             if (status != 0)
295             {
296                 /* Some sort of error.  Discard the data, and return.  */
297
298                 buf->last->next = free_buffer_data;
299                 free_buffer_data = buf->data;
300                 buf->data = NULL;
301                 buf->last = NULL;
302
303                 return status;
304             }
305
306             if (nbytes != data->size)
307             {
308                 /* Not all the data was written out.  This is only
309                    permitted in nonblocking mode.  Adjust the buffer,
310                    and return.  */
311
312                 assert (buf->nonblocking);
313
314                 data->size -= nbytes;
315                 data->bufp += nbytes;
316
317                 return 0;
318             }
319         }
320
321         buf->data = data->next;
322         data->next = free_buffer_data;
323         free_buffer_data = data;
324     }
325
326     buf->last = NULL;
327
328     return 0;
329 }
330
331 /*
332  * Flush any data queued up in the buffer.  If BLOCK is nonzero, then
333  * if the buffer is in nonblocking mode, put it into blocking mode for
334  * the duration of the flush.  This returns 0 on success, or an error
335  * code.
336  */
337
338 int
339 buf_flush (struct buffer *buf, int block)
340 {
341     int nonblocking;
342     int status;
343
344     if (buf->flush == NULL)
345         abort ();
346
347     nonblocking = buf->nonblocking;
348     if (nonblocking && block)
349     {
350         status = set_block (buf);
351         if (status != 0)
352             return status;
353     }
354
355     status = buf_send_output (buf);
356     if (status == 0)
357         status = (*buf->flush) (buf->closure);
358
359     if (nonblocking && block)
360     {
361         int blockstat;
362
363         blockstat = set_nonblock (buf);
364         if (status == 0)
365             status = blockstat;
366     }
367
368     return status;
369 }
370
371 /*
372  * Set buffer BUF to nonblocking I/O.  Returns 0 for success or errno
373  * code.
374  */
375
376 int
377 set_nonblock (struct buffer *buf)
378 {
379     int status;
380
381     if (buf->nonblocking)
382         return 0;
383     if (buf->block == NULL)
384         abort ();
385     status = (*buf->block) (buf->closure, 0);
386     if (status != 0)
387         return status;
388     buf->nonblocking = 1;
389     return 0;
390 }
391
392 /*
393  * Set buffer BUF to blocking I/O.  Returns 0 for success or errno
394  * code.
395  */
396
397 int
398 set_block (struct buffer *buf)
399 {
400     int status;
401
402     if (! buf->nonblocking)
403         return 0;
404     if (buf->block == NULL)
405         abort ();
406     status = (*buf->block) (buf->closure, 1);
407     if (status != 0)
408         return status;
409     buf->nonblocking = 0;
410     return 0;
411 }
412
413 /*
414  * Send a character count and some output.  Returns errno code or 0 for
415  * success.
416  *
417  * Sending the count in binary is OK since this is only used on a pipe
418  * within the same system.
419  */
420
421 int
422 buf_send_counted (struct buffer *buf)
423 {
424     int size;
425     struct buffer_data *data;
426
427     size = 0;
428     for (data = buf->data; data != NULL; data = data->next)
429         size += data->size;
430
431     data = get_buffer_data ();
432     if (data == NULL)
433     {
434         (*buf->memory_error) (buf);
435         return ENOMEM;
436     }
437
438     data->next = buf->data;
439     buf->data = data;
440     if (buf->last == NULL)
441         buf->last = data;
442
443     data->bufp = data->text;
444     data->size = sizeof (int);
445
446     *((int *) data->text) = size;
447
448     return buf_send_output (buf);
449 }
450
451 /*
452  * Send a special count.  COUNT should be negative.  It will be
453  * handled speciallyi by buf_copy_counted.  This function returns 0 or
454  * an errno code.
455  *
456  * Sending the count in binary is OK since this is only used on a pipe
457  * within the same system.
458  */
459
460 int
461 buf_send_special_count (struct buffer *buf, int count)
462 {
463     struct buffer_data *data;
464
465     data = get_buffer_data ();
466     if (data == NULL)
467     {
468         (*buf->memory_error) (buf);
469         return ENOMEM;
470     }
471
472     data->next = buf->data;
473     buf->data = data;
474     if (buf->last == NULL)
475         buf->last = data;
476
477     data->bufp = data->text;
478     data->size = sizeof (int);
479
480     *((int *) data->text) = count;
481
482     return buf_send_output (buf);
483 }
484
485 /* Append a list of buffer_data structures to an buffer.  */
486
487 void
488 buf_append_data (struct buffer *buf, struct buffer_data *data,
489                  struct buffer_data *last)
490 {
491     if (data != NULL)
492     {
493         if (buf->data == NULL)
494             buf->data = data;
495         else
496             buf->last->next = data;
497         buf->last = last;
498     }
499 }
500
501 /* Append the data on one buffer to another.  This removes the data
502    from the source buffer.  */
503
504 void
505 buf_append_buffer (struct buffer *to, struct buffer *from)
506 {
507     buf_append_data (to, from->data, from->last);
508     from->data = NULL;
509     from->last = NULL;
510 }
511
512 /*
513  * Copy the contents of file F into buffer_data structures.  We can't
514  * copy directly into an buffer, because we want to handle failure and
515  * succeess differently.  Returns 0 on success, or -2 if out of
516  * memory, or a status code on error.  Since the caller happens to
517  * know the size of the file, it is passed in as SIZE.  On success,
518  * this function sets *RETP and *LASTP, which may be passed to
519  * buf_append_data.
520  */
521
522 int
523 buf_read_file (FILE *f, long int size, struct buffer_data **retp,
524                struct buffer_data **lastp)
525 {
526     int status;
527
528     *retp = NULL;
529     *lastp = NULL;
530
531     while (size > 0)
532     {
533         struct buffer_data *data;
534         int get;
535
536         data = get_buffer_data ();
537         if (data == NULL)
538         {
539             status = -2;
540             goto error_return;
541         }
542
543         if (*retp == NULL)
544             *retp = data;
545         else
546             (*lastp)->next = data;
547         data->next = NULL;
548         *lastp = data;
549
550         data->bufp = data->text;
551         data->size = 0;
552
553         if (size > BUFFER_DATA_SIZE)
554             get = BUFFER_DATA_SIZE;
555         else
556             get = size;
557
558         errno = EIO;
559         if (fread (data->text, get, 1, f) != 1)
560         {
561             status = errno;
562             goto error_return;
563         }
564
565         data->size += get;
566         size -= get;
567     }
568
569     return 0;
570
571   error_return:
572     if (*retp != NULL)
573     {
574         (*lastp)->next = free_buffer_data;
575         free_buffer_data = *retp;
576     }
577     return status;
578 }
579
580 /*
581  * Copy the contents of file F into buffer_data structures.  We can't
582  * copy directly into an buffer, because we want to handle failure and
583  * succeess differently.  Returns 0 on success, or -2 if out of
584  * memory, or a status code on error.  On success, this function sets
585  * *RETP and *LASTP, which may be passed to buf_append_data.
586  */
587
588 int
589 buf_read_file_to_eof (FILE *f, struct buffer_data **retp,
590                       struct buffer_data **lastp)
591 {
592     int status;
593
594     *retp = NULL;
595     *lastp = NULL;
596
597     while (!feof (f))
598     {
599         struct buffer_data *data;
600         int get, nread;
601
602         data = get_buffer_data ();
603         if (data == NULL)
604         {
605             status = -2;
606             goto error_return;
607         }
608
609         if (*retp == NULL)
610             *retp = data;
611         else
612             (*lastp)->next = data;
613         data->next = NULL;
614         *lastp = data;
615
616         data->bufp = data->text;
617         data->size = 0;
618
619         get = BUFFER_DATA_SIZE;
620
621         errno = EIO;
622         nread = fread (data->text, 1, get, f);
623         if (nread == 0 && !feof (f))
624         {
625             status = errno;
626             goto error_return;
627         }
628
629         data->size = nread;
630     }
631
632     return 0;
633
634   error_return:
635     if (*retp != NULL)
636     {
637         (*lastp)->next = free_buffer_data;
638         free_buffer_data = *retp;
639     }
640     return status;
641 }
642
643 /* Return the number of bytes in a chain of buffer_data structures.  */
644
645 int
646 buf_chain_length (struct buffer_data *buf)
647 {
648     int size = 0;
649     while (buf)
650     {
651         size += buf->size;
652         buf = buf->next;
653     }
654     return size;
655 }
656
657 /* Return the number of bytes in a buffer.  */
658
659 int
660 buf_length (struct buffer *buf)
661 {
662     return buf_chain_length (buf->data);
663 }
664
665 /*
666  * Read an arbitrary amount of data into an input buffer.  The buffer
667  * will be in nonblocking mode, and we just grab what we can.  Return
668  * 0 on success, or -1 on end of file, or -2 if out of memory, or an
669  * error code.  If COUNTP is not NULL, *COUNTP is set to the number of
670  * bytes read.
671  */
672
673 int
674 buf_input_data (struct buffer *buf, int *countp)
675 {
676     if (buf->input == NULL)
677         abort ();
678
679     if (countp != NULL)
680         *countp = 0;
681
682     while (1)
683     {
684         int get;
685         int status, nbytes;
686
687         if (buf->data == NULL
688             || (buf->last->bufp + buf->last->size
689                 == buf->last->text + BUFFER_DATA_SIZE))
690         {
691             struct buffer_data *data;
692
693             data = get_buffer_data ();
694             if (data == NULL)
695             {
696                 (*buf->memory_error) (buf);
697                 return -2;
698             }
699
700             if (buf->data == NULL)
701                 buf->data = data;
702             else
703                 buf->last->next = data;
704             data->next = NULL;
705             buf->last = data;
706
707             data->bufp = data->text;
708             data->size = 0;
709         }
710
711         get = ((buf->last->text + BUFFER_DATA_SIZE)
712                - (buf->last->bufp + buf->last->size));
713
714         status = (*buf->input) (buf->closure,
715                                 buf->last->bufp + buf->last->size,
716                                 0, get, &nbytes);
717         if (status != 0)
718             return status;
719
720         buf->last->size += nbytes;
721         if (countp != NULL)
722             *countp += nbytes;
723
724         if (nbytes < get)
725         {
726             /* If we did not fill the buffer, then presumably we read
727                all the available data.  */
728             return 0;
729         }
730     }
731
732     /*NOTREACHED*/
733 }
734
735 /*
736  * Read a line (characters up to a \012) from an input buffer.  (We
737  * use \012 rather than \n for the benefit of non Unix clients for
738  * which \n means something else).  This returns 0 on success, or -1
739  * on end of file, or -2 if out of memory, or an error code.  If it
740  * succeeds, it sets *LINE to an allocated buffer holding the contents
741  * of the line.  The trailing \012 is not included in the buffer.  If
742  * LENP is not NULL, then *LENP is set to the number of bytes read;
743  * strlen may not work, because there may be embedded null bytes.
744  */
745
746 int
747 buf_read_line (struct buffer *buf, char **line, int *lenp)
748 {
749     if (buf->input == NULL)
750         abort ();
751
752     *line = NULL;
753
754     while (1)
755     {
756         int len, finallen = 0;
757         struct buffer_data *data;
758         char *nl;
759
760         /* See if there is a newline in BUF.  */
761         len = 0;
762         for (data = buf->data; data != NULL; data = data->next)
763         {
764             nl = memchr (data->bufp, '\012', data->size);
765             if (nl != NULL)
766             {
767                 finallen = nl - data->bufp;
768                 len += finallen;
769                 break;
770             }
771             len += data->size;
772         }
773
774         /* If we found a newline, copy the line into a memory buffer,
775            and remove it from BUF.  */
776         if (data != NULL)
777         {
778             char *p;
779             struct buffer_data *nldata;
780
781             p = xmalloc (len + 1);
782             if (p == NULL)
783                 return -2;
784             *line = p;
785
786             nldata = data;
787             data = buf->data;
788             while (data != nldata)
789             {
790                 struct buffer_data *next;
791
792                 memcpy (p, data->bufp, data->size);
793                 p += data->size;
794                 next = data->next;
795                 data->next = free_buffer_data;
796                 free_buffer_data = data;
797                 data = next;
798             }
799
800             memcpy (p, data->bufp, finallen);
801             p[finallen] = '\0';
802
803             data->size -= finallen + 1;
804             data->bufp = nl + 1;
805             buf->data = data;
806
807             if (lenp != NULL)
808                 *lenp = len;
809
810             return 0;
811         }
812
813         /* Read more data until we get a newline.  */
814         while (1)
815         {
816             int size, status, nbytes;
817             char *mem;
818
819             if (buf->data == NULL
820                 || (buf->last->bufp + buf->last->size
821                     == buf->last->text + BUFFER_DATA_SIZE))
822             {
823                 data = get_buffer_data ();
824                 if (data == NULL)
825                 {
826                     (*buf->memory_error) (buf);
827                     return -2;
828                 }
829
830                 if (buf->data == NULL)
831                     buf->data = data;
832                 else
833                     buf->last->next = data;
834                 data->next = NULL;
835                 buf->last = data;
836
837                 data->bufp = data->text;
838                 data->size = 0;
839             }
840
841             mem = buf->last->bufp + buf->last->size;
842             size = (buf->last->text + BUFFER_DATA_SIZE) - mem;
843
844             /* We need to read at least 1 byte.  We can handle up to
845                SIZE bytes.  This will only be efficient if the
846                underlying communication stream does its own buffering,
847                or is clever about getting more than 1 byte at a time.  */
848             status = (*buf->input) (buf->closure, mem, 1, size, &nbytes);
849             if (status != 0)
850                 return status;
851
852             buf->last->size += nbytes;
853
854             /* Optimize slightly to avoid an unnecessary call to
855                memchr.  */
856             if (nbytes == 1)
857             {
858                 if (*mem == '\012')
859                     break;
860             }
861             else
862             {
863                 if (memchr (mem, '\012', nbytes) != NULL)
864                     break;
865             }
866         }
867     }
868 }
869
870 /*
871  * Extract data from the input buffer BUF.  This will read up to WANT
872  * bytes from the buffer.  It will set *RETDATA to point at the bytes,
873  * and set *GOT to the number of bytes to be found there.  Any buffer
874  * call which uses BUF may change the contents of the buffer at *DATA,
875  * so the data should be fully processed before any further calls are
876  * made.  This returns 0 on success, or -1 on end of file, or -2 if
877  * out of memory, or an error code.
878  */
879
880 int
881 buf_read_data (struct buffer *buf, int want, char **retdata, int *got)
882 {
883     if (buf->input == NULL)
884         abort ();
885
886     while (buf->data != NULL && buf->data->size == 0)
887     {
888         struct buffer_data *next;
889
890         next = buf->data->next;
891         buf->data->next = free_buffer_data;
892         free_buffer_data = buf->data;
893         buf->data = next;
894         if (next == NULL)
895             buf->last = NULL;
896     }
897
898     if (buf->data == NULL)
899     {
900         struct buffer_data *data;
901         int get, status, nbytes;
902
903         data = get_buffer_data ();
904         if (data == NULL)
905         {
906             (*buf->memory_error) (buf);
907             return -2;
908         }
909
910         buf->data = data;
911         buf->last = data;
912         data->next = NULL;
913         data->bufp = data->text;
914         data->size = 0;
915
916         if (want < BUFFER_DATA_SIZE)
917             get = want;
918         else
919             get = BUFFER_DATA_SIZE;
920         status = (*buf->input) (buf->closure, data->bufp, get,
921                                 BUFFER_DATA_SIZE, &nbytes);
922         if (status != 0)
923             return status;
924
925         data->size = nbytes;
926     }
927
928     *retdata = buf->data->bufp;
929     if (want < buf->data->size)
930     {
931         *got = want;
932         buf->data->size -= want;
933         buf->data->bufp += want;
934     }
935     else
936     {
937         *got = buf->data->size;
938         buf->data->size = 0;
939     }
940
941     return 0;
942 }
943
944 /*
945  * Copy lines from an input buffer to an output buffer.  This copies
946  * all complete lines (characters up to a newline) from INBUF to
947  * OUTBUF.  Each line in OUTBUF is preceded by the character COMMAND
948  * and a space.
949  */
950
951 void
952 buf_copy_lines (struct buffer *outbuf, struct buffer *inbuf, int command)
953 {
954     while (1)
955     {
956         struct buffer_data *data;
957         struct buffer_data *nldata;
958         char *nl;
959         int len;
960
961         /* See if there is a newline in INBUF.  */
962         nldata = NULL;
963         nl = NULL;
964         for (data = inbuf->data; data != NULL; data = data->next)
965         {
966             nl = memchr (data->bufp, '\n', data->size);
967             if (nl != NULL)
968             {
969                 nldata = data;
970                 break;
971             }
972         }
973
974         if (nldata == NULL)
975         {
976             /* There are no more lines in INBUF.  */
977             return;
978         }
979
980         /* Put in the command.  */
981         buf_append_char (outbuf, command);
982         buf_append_char (outbuf, ' ');
983
984         if (inbuf->data != nldata)
985         {
986             /*
987              * Simply move over all the buffers up to the one containing
988              * the newline.
989              */
990             for (data = inbuf->data; data->next != nldata; data = data->next)
991                 ;
992             data->next = NULL;
993             buf_append_data (outbuf, inbuf->data, data);
994             inbuf->data = nldata;
995         }
996
997         /*
998          * If the newline is at the very end of the buffer, just move
999          * the buffer onto OUTBUF.  Otherwise we must copy the data.
1000          */
1001         len = nl + 1 - nldata->bufp;
1002         if (len == nldata->size)
1003         {
1004             inbuf->data = nldata->next;
1005             if (inbuf->data == NULL)
1006                 inbuf->last = NULL;
1007
1008             nldata->next = NULL;
1009             buf_append_data (outbuf, nldata, nldata);
1010         }
1011         else
1012         {
1013             buf_output (outbuf, nldata->bufp, len);
1014             nldata->bufp += len;
1015             nldata->size -= len;
1016         }
1017     }
1018 }
1019
1020 /*
1021  * Copy counted data from one buffer to another.  The count is an
1022  * integer, host size, host byte order (it is only used across a
1023  * pipe).  If there is enough data, it should be moved over.  If there
1024  * is not enough data, it should remain on the original buffer.  A
1025  * negative count is a special case.  if one is seen, *SPECIAL is set
1026  * to the (negative) count value and no additional data is gathered
1027  * from the buffer; normally *SPECIAL is set to 0.  This function
1028  * returns the number of bytes it needs to see in order to actually
1029  * copy something over.
1030  */
1031
1032 int
1033 buf_copy_counted (struct buffer *outbuf, struct buffer *inbuf, int *special)
1034 {
1035     *special = 0;
1036
1037     while (1)
1038     {
1039         struct buffer_data *data;
1040         int need;
1041         union
1042         {
1043             char intbuf[sizeof (int)];
1044             int i;
1045         } u;
1046         char *intp;
1047         int count;
1048         struct buffer_data *start;
1049         int startoff;
1050         struct buffer_data *stop;
1051         int stopwant;
1052
1053         /* See if we have enough bytes to figure out the count.  */
1054         need = sizeof (int);
1055         intp = u.intbuf;
1056         for (data = inbuf->data; data != NULL; data = data->next)
1057         {
1058             if (data->size >= need)
1059             {
1060                 memcpy (intp, data->bufp, need);
1061                 break;
1062             }
1063             memcpy (intp, data->bufp, data->size);
1064             intp += data->size;
1065             need -= data->size;
1066         }
1067         if (data == NULL)
1068         {
1069             /* We don't have enough bytes to form an integer.  */
1070             return need;
1071         }
1072
1073         count = u.i;
1074         start = data;
1075         startoff = need;
1076
1077         if (count < 0)
1078         {
1079             /* A negative COUNT is a special case meaning that we
1080                don't need any further information.  */
1081             stop = start;
1082             stopwant = 0;
1083         }
1084         else
1085         {
1086             /*
1087              * We have an integer in COUNT.  We have gotten all the
1088              * data from INBUF in all buffers before START, and we
1089              * have gotten STARTOFF bytes from START.  See if we have
1090              * enough bytes remaining in INBUF.
1091              */
1092             need = count - (start->size - startoff);
1093             if (need <= 0)
1094             {
1095                 stop = start;
1096                 stopwant = count;
1097             }
1098             else
1099             {
1100                 for (data = start->next; data != NULL; data = data->next)
1101                 {
1102                     if (need <= data->size)
1103                         break;
1104                     need -= data->size;
1105                 }
1106                 if (data == NULL)
1107                 {
1108                     /* We don't have enough bytes.  */
1109                     return need;
1110                 }
1111                 stop = data;
1112                 stopwant = need;
1113             }
1114         }
1115
1116         /*
1117          * We have enough bytes.  Free any buffers in INBUF before
1118          * START, and remove STARTOFF bytes from START, so that we can
1119          * forget about STARTOFF.
1120          */
1121         start->bufp += startoff;
1122         start->size -= startoff;
1123
1124         if (start->size == 0)
1125             start = start->next;
1126
1127         if (stop->size == stopwant)
1128         {
1129             stop = stop->next;
1130             stopwant = 0;
1131         }
1132
1133         while (inbuf->data != start)
1134         {
1135             data = inbuf->data;
1136             inbuf->data = data->next;
1137             data->next = free_buffer_data;
1138             free_buffer_data = data;
1139         }
1140
1141         /* If COUNT is negative, set *SPECIAL and get out now.  */
1142         if (count < 0)
1143         {
1144             *special = count;
1145             return 0;
1146         }
1147
1148         /*
1149          * We want to copy over the bytes from START through STOP.  We
1150          * only want STOPWANT bytes from STOP.
1151          */
1152
1153         if (start != stop)
1154         {
1155             /* Attach the buffers from START through STOP to OUTBUF.  */
1156             for (data = start; data->next != stop; data = data->next)
1157                 ;
1158             inbuf->data = stop;
1159             data->next = NULL;
1160             buf_append_data (outbuf, start, data);
1161         }
1162
1163         if (stopwant > 0)
1164         {
1165             buf_output (outbuf, stop->bufp, stopwant);
1166             stop->bufp += stopwant;
1167             stop->size -= stopwant;
1168         }
1169     }
1170
1171     /*NOTREACHED*/
1172 }
1173
1174 /* Shut down a buffer.  This returns 0 on success, or an errno code.  */
1175
1176 int
1177 buf_shutdown (struct buffer *buf)
1178 {
1179     if (buf->shutdown)
1180         return (*buf->shutdown) (buf);
1181     return 0;
1182 }
1183
1184
1185
1186 /* The simplest type of buffer is one built on top of a stdio FILE.
1187    For simplicity, and because it is all that is required, we do not
1188    implement setting this type of buffer into nonblocking mode.  The
1189    closure field is just a FILE *.  */
1190
1191 static int stdio_buffer_input (void *, char *, int, int, int *);
1192 static int stdio_buffer_output (void *, const char *, int, int *);
1193 static int stdio_buffer_flush (void *);
1194 static int stdio_buffer_shutdown (struct buffer *buf);
1195
1196
1197
1198 /* Initialize a buffer built on a stdio FILE.  */
1199 struct stdio_buffer_closure
1200 {
1201     FILE *fp;
1202     int child_pid;
1203 };
1204
1205
1206
1207 struct buffer *
1208 stdio_buffer_initialize (FILE *fp, int child_pid, int input,
1209                          void (*memory) (struct buffer *))
1210 {
1211     struct stdio_buffer_closure *bc = xmalloc (sizeof (*bc));
1212
1213     bc->fp = fp;
1214     bc->child_pid = child_pid;
1215
1216     return buf_initialize (input ? stdio_buffer_input : NULL,
1217                            input ? NULL : stdio_buffer_output,
1218                            input ? NULL : stdio_buffer_flush,
1219                            NULL, stdio_buffer_shutdown, memory,
1220                            bc);
1221 }
1222
1223
1224
1225 /* Return the file associated with a stdio buffer. */
1226 FILE *
1227 stdio_buffer_get_file (struct buffer *buf)
1228 {
1229     struct stdio_buffer_closure *bc;
1230
1231     assert (buf->shutdown == stdio_buffer_shutdown);
1232
1233     bc = buf->closure;
1234
1235     return bc->fp;
1236 }
1237
1238
1239
1240 /* The buffer input function for a buffer built on a stdio FILE.  */
1241 static int
1242 stdio_buffer_input (void *closure, char *data, int need, int size, int *got)
1243 {
1244     struct stdio_buffer_closure *bc = closure;
1245     int nbytes;
1246
1247     /* Since stdio does its own buffering, we don't worry about
1248        getting more bytes than we need.  */
1249
1250     if (need == 0 || need == 1)
1251     {
1252         int ch;
1253
1254         ch = getc (bc->fp);
1255
1256         if (ch == EOF)
1257         {
1258             if (feof (bc->fp))
1259                 return -1;
1260             else if (errno == 0)
1261                 return EIO;
1262             else
1263                 return errno;
1264         }
1265
1266         *data = ch;
1267         *got = 1;
1268         return 0;
1269     }
1270
1271     nbytes = fread (data, 1, need, bc->fp);
1272
1273     if (nbytes == 0)
1274     {
1275         *got = 0;
1276         if (feof (bc->fp))
1277             return -1;
1278         else if (errno == 0)
1279             return EIO;
1280         else
1281             return errno;
1282     }
1283
1284     *got = nbytes;
1285
1286     return 0;
1287 }
1288
1289
1290
1291 /* The buffer output function for a buffer built on a stdio FILE.  */
1292 static int
1293 stdio_buffer_output (void *closure, const char *data, int have, int *wrote)
1294 {
1295     struct stdio_buffer_closure *bc = closure;
1296
1297     *wrote = 0;
1298
1299     while (have > 0)
1300     {
1301         int nbytes;
1302
1303         nbytes = fwrite (data, 1, have, bc->fp);
1304
1305         if (nbytes != have)
1306         {
1307             if (errno == 0)
1308                 return EIO;
1309             else
1310                 return errno;
1311         }
1312
1313         *wrote += nbytes;
1314         have -= nbytes;
1315         data += nbytes;
1316     }
1317
1318     return 0;
1319 }
1320
1321
1322
1323 /* The buffer flush function for a buffer built on a stdio FILE.  */
1324 static int
1325 stdio_buffer_flush (void *closure)
1326 {
1327     struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure;
1328
1329     if (fflush (bc->fp) != 0)
1330     {
1331         if (errno == 0)
1332             return EIO;
1333         else
1334             return errno;
1335     }
1336
1337     return 0;
1338 }
1339
1340
1341
1342 static int
1343 stdio_buffer_shutdown (struct buffer *buf)
1344 {
1345     struct stdio_buffer_closure *bc = buf->closure;
1346     struct stat s;
1347     int closefp = 1;
1348
1349     /* Must be a pipe or a socket.  What could go wrong? */
1350     assert (fstat (fileno (bc->fp), &s) != -1);
1351
1352     /* Flush the buffer if we can */
1353     if (buf->flush)
1354     {
1355         buf_flush (buf, 1);
1356         buf->flush = NULL;
1357     }
1358
1359     if (buf->input)
1360     {
1361         /* There used to be a check here for unread data in the buffer of on
1362          * the pipe, but it was deemed unnecessary and possibly dangerous.  In
1363          * some sense it could be second-guessing the caller who requested it
1364          * closed, as well.
1365          */
1366
1367 # ifdef SHUTDOWN_SERVER
1368         if (current_parsed_root->method != server_method)
1369 # endif
1370 # ifndef NO_SOCKET_TO_FD
1371         {
1372             /* shutdown() sockets */
1373             if (S_ISSOCK (s.st_mode))
1374                 shutdown (fileno (bc->fp), 0);
1375         }
1376 # endif /* NO_SOCKET_TO_FD */
1377 # ifdef START_RSH_WITH_POPEN_RW
1378         /* Can't be set with SHUTDOWN_SERVER defined */
1379         else if (pclose (bc->fp) == EOF)
1380         {
1381             error (1, errno, "closing connection to %s",
1382                    current_parsed_root->hostname);
1383             closefp = 0;
1384         }
1385 # endif /* START_RSH_WITH_POPEN_RW */
1386
1387         buf->input = NULL;
1388     }
1389     else if (buf->output)
1390     {
1391 # ifdef SHUTDOWN_SERVER
1392         /* FIXME:  Should have a SHUTDOWN_SERVER_INPUT &
1393          * SHUTDOWN_SERVER_OUTPUT
1394          */
1395         if (current_parsed_root->method == server_method)
1396             SHUTDOWN_SERVER (fileno (bc->fp));
1397         else
1398 # endif
1399 # ifndef NO_SOCKET_TO_FD
1400         /* shutdown() sockets */
1401         if (S_ISSOCK (s.st_mode))
1402             shutdown (fileno (bc->fp), 1);
1403 # else
1404         {
1405         /* I'm not sure I like this empty block, but the alternative
1406          * is a another nested NO_SOCKET_TO_FD switch above.
1407          */
1408         }
1409 # endif /* NO_SOCKET_TO_FD */
1410
1411         buf->output = NULL;
1412     }
1413
1414     if (closefp && fclose (bc->fp) == EOF)
1415     {
1416         if (0
1417 # ifdef SERVER_SUPPORT
1418             || server_active
1419 # endif /* SERVER_SUPPORT */
1420            )
1421         {
1422             /* Syslog this? */
1423         }
1424 # ifdef CLIENT_SUPPORT
1425         else
1426             error (1, errno,
1427                    "closing down connection to %s",
1428                    current_parsed_root->hostname);
1429 # endif /* CLIENT_SUPPORT */
1430     }
1431
1432     /* If we were talking to a process, make sure it exited */
1433     if (bc->child_pid)
1434     {
1435         int w;
1436
1437         do
1438             w = waitpid (bc->child_pid, (int *) 0, 0);
1439         while (w == -1 && errno == EINTR);
1440         if (w == -1)
1441             error (1, errno, "waiting for process %d", bc->child_pid);
1442     }
1443     return 0;
1444 }
1445
1446
1447
1448 /* Certain types of communication input and output data in packets,
1449    where each packet is translated in some fashion.  The packetizing
1450    buffer type supports that, given a buffer which handles lower level
1451    I/O and a routine to translate the data in a packet.
1452
1453    This code uses two bytes for the size of a packet, so packets are
1454    restricted to 65536 bytes in total.
1455
1456    The translation functions should just translate; they may not
1457    significantly increase or decrease the amount of data.  The actual
1458    size of the initial data is part of the translated data.  The
1459    output translation routine may add up to PACKET_SLOP additional
1460    bytes, and the input translation routine should shrink the data
1461    correspondingly.  */
1462
1463 # define PACKET_SLOP (100)
1464
1465 /* This structure is the closure field of a packetizing buffer.  */
1466
1467 struct packetizing_buffer
1468 {
1469     /* The underlying buffer.  */
1470     struct buffer *buf;
1471     /* The input translation function.  Exactly one of inpfn and outfn
1472        will be NULL.  The input translation function should
1473        untranslate the data in INPUT, storing the result in OUTPUT.
1474        SIZE is the amount of data in INPUT, and is also the size of
1475        OUTPUT.  This should return 0 on success, or an errno code.  */
1476     int (*inpfn) (void *fnclosure, const char *input, char *output,
1477                         int size);
1478     /* The output translation function.  This should translate the
1479        data in INPUT, storing the result in OUTPUT.  The first two
1480        bytes in INPUT will be the size of the data, and so will SIZE.
1481        This should set *TRANSLATED to the amount of translated data in
1482        OUTPUT.  OUTPUT is large enough to hold SIZE + PACKET_SLOP
1483        bytes.  This should return 0 on success, or an errno code.  */
1484     int (*outfn) (void *fnclosure, const char *input, char *output,
1485                         int size, int *translated);
1486     /* A closure for the translation function.  */
1487     void *fnclosure;
1488     /* For an input buffer, we may have to buffer up data here.  */
1489     /* This is non-zero if the buffered data has been translated.
1490        Otherwise, the buffered data has not been translated, and starts
1491        with the two byte packet size.  */
1492     int translated;
1493     /* The amount of buffered data.  */
1494     int holdsize;
1495     /* The buffer allocated to hold the data.  */
1496     char *holdbuf;
1497     /* The size of holdbuf.  */
1498     int holdbufsize;
1499     /* If translated is set, we need another data pointer to track
1500        where we are in holdbuf.  If translated is clear, then this
1501        pointer is not used.  */
1502     char *holddata;
1503 };
1504
1505
1506
1507 static int packetizing_buffer_input (void *, char *, int, int, int *);
1508 static int packetizing_buffer_output (void *, const char *, int, int *);
1509 static int packetizing_buffer_flush (void *);
1510 static int packetizing_buffer_block (void *, int);
1511 static int packetizing_buffer_shutdown (struct buffer *);
1512
1513
1514
1515 /* Create a packetizing buffer.  */
1516 struct buffer *
1517 packetizing_buffer_initialize (struct buffer *buf,
1518                                int (*inpfn) (void *, const char *, char *,
1519                                              int),
1520                                int (*outfn) (void *, const char *, char *,
1521                                              int, int *),
1522                                void *fnclosure,
1523                                void (*memory) (struct buffer *))
1524 {
1525     struct packetizing_buffer *pb;
1526
1527     pb = (struct packetizing_buffer *) xmalloc (sizeof *pb);
1528     memset (pb, 0, sizeof *pb);
1529
1530     pb->buf = buf;
1531     pb->inpfn = inpfn;
1532     pb->outfn = outfn;
1533     pb->fnclosure = fnclosure;
1534
1535     if (inpfn != NULL)
1536     {
1537         /* Add PACKET_SLOP to handle larger translated packets, and
1538            add 2 for the count.  This buffer is increased if
1539            necessary.  */
1540         pb->holdbufsize = BUFFER_DATA_SIZE + PACKET_SLOP + 2;
1541         pb->holdbuf = xmalloc (pb->holdbufsize);
1542     }
1543
1544     return buf_initialize (inpfn != NULL ? packetizing_buffer_input : NULL,
1545                            inpfn != NULL ? NULL : packetizing_buffer_output,
1546                            inpfn != NULL ? NULL : packetizing_buffer_flush,
1547                            packetizing_buffer_block,
1548                            packetizing_buffer_shutdown,
1549                            memory,
1550                            pb);
1551 }
1552
1553
1554
1555 /* Input data from a packetizing buffer.  */
1556 static int
1557 packetizing_buffer_input (void *closure, char *data, int need, int size,
1558                           int *got)
1559 {
1560     struct packetizing_buffer *pb = closure;
1561
1562     *got = 0;
1563
1564     if (pb->holdsize > 0 && pb->translated)
1565     {
1566         int copy;
1567
1568         copy = pb->holdsize;
1569
1570         if (copy > size)
1571         {
1572             memcpy (data, pb->holddata, size);
1573             pb->holdsize -= size;
1574             pb->holddata += size;
1575             *got = size;
1576             return 0;
1577         }
1578
1579         memcpy (data, pb->holddata, copy);
1580         pb->holdsize = 0;
1581         pb->translated = 0;
1582
1583         data += copy;
1584         need -= copy;
1585         size -= copy;
1586         *got = copy;
1587     }
1588
1589     while (need > 0 || *got == 0)
1590     {
1591         int get, status, nread, count, tcount;
1592         char *bytes;
1593         char stackoutbuf[BUFFER_DATA_SIZE + PACKET_SLOP];
1594         char *inbuf, *outbuf;
1595
1596         /* If we don't already have the two byte count, get it.  */
1597         if (pb->holdsize < 2)
1598         {
1599             get = 2 - pb->holdsize;
1600             status = buf_read_data (pb->buf, get, &bytes, &nread);
1601             if (status != 0)
1602             {
1603                 /* buf_read_data can return -2, but a buffer input
1604                    function is only supposed to return -1, 0, or an
1605                    error code.  */
1606                 if (status == -2)
1607                     status = ENOMEM;
1608                 return status;
1609             }
1610
1611             if (nread == 0)
1612             {
1613                 /* The buffer is in nonblocking mode, and we didn't
1614                    manage to read anything.  */
1615                 return 0;
1616             }
1617
1618             if (get == 1)
1619                 pb->holdbuf[1] = bytes[0];
1620             else
1621             {
1622                 pb->holdbuf[0] = bytes[0];
1623                 if (nread < 2)
1624                 {
1625                     /* We only got one byte, but we needed two.  Stash
1626                        the byte we got, and try again.  */
1627                     pb->holdsize = 1;
1628                     continue;
1629                 }
1630                 pb->holdbuf[1] = bytes[1];
1631             }
1632             pb->holdsize = 2;
1633         }
1634
1635         /* Read the packet.  */
1636
1637         count = (((pb->holdbuf[0] & 0xff) << 8)
1638                  + (pb->holdbuf[1] & 0xff));
1639
1640         if (count + 2 > pb->holdbufsize)
1641         {
1642             char *n;
1643
1644             /* We didn't allocate enough space in the initialize
1645                function.  */
1646
1647             n = xrealloc (pb->holdbuf, count + 2);
1648             if (n == NULL)
1649             {
1650                 (*pb->buf->memory_error) (pb->buf);
1651                 return ENOMEM;
1652             }
1653             pb->holdbuf = n;
1654             pb->holdbufsize = count + 2;
1655         }
1656
1657         get = count - (pb->holdsize - 2);
1658
1659         status = buf_read_data (pb->buf, get, &bytes, &nread);
1660         if (status != 0)
1661         {
1662             /* buf_read_data can return -2, but a buffer input
1663                function is only supposed to return -1, 0, or an error
1664                code.  */
1665             if (status == -2)
1666                 status = ENOMEM;
1667             return status;
1668         }
1669
1670         if (nread == 0)
1671         {
1672             /* We did not get any data.  Presumably the buffer is in
1673                nonblocking mode.  */
1674             return 0;
1675         }
1676
1677         if (nread < get)
1678         {
1679             /* We did not get all the data we need to fill the packet.
1680                buf_read_data does not promise to return all the bytes
1681                requested, so we must try again.  */
1682             memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1683             pb->holdsize += nread;
1684             continue;
1685         }
1686
1687         /* We have a complete untranslated packet of COUNT bytes.  */
1688
1689         if (pb->holdsize == 2)
1690         {
1691             /* We just read the entire packet (the 2 bytes in
1692                PB->HOLDBUF are the size).  Save a memcpy by
1693                translating directly from BYTES.  */
1694             inbuf = bytes;
1695         }
1696         else
1697         {
1698             /* We already had a partial packet in PB->HOLDBUF.  We
1699                need to copy the new data over to make the input
1700                contiguous.  */
1701             memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1702             inbuf = pb->holdbuf + 2;
1703         }
1704
1705         if (count <= sizeof stackoutbuf)
1706             outbuf = stackoutbuf;
1707         else
1708         {
1709             outbuf = xmalloc (count);
1710             if (outbuf == NULL)
1711             {
1712                 (*pb->buf->memory_error) (pb->buf);
1713                 return ENOMEM;
1714             }
1715         }
1716
1717         status = (*pb->inpfn) (pb->fnclosure, inbuf, outbuf, count);
1718         if (status != 0)
1719             return status;
1720
1721         /* The first two bytes in the translated buffer are the real
1722            length of the translated data.  */
1723         tcount = ((outbuf[0] & 0xff) << 8) + (outbuf[1] & 0xff);
1724
1725         if (tcount > count)
1726             error (1, 0, "Input translation failure");
1727
1728         if (tcount > size)
1729         {
1730             /* We have more data than the caller has provided space
1731                for.  We need to save some of it for the next call.  */
1732
1733             memcpy (data, outbuf + 2, size);
1734             *got += size;
1735
1736             pb->holdsize = tcount - size;
1737             memcpy (pb->holdbuf, outbuf + 2 + size, tcount - size);
1738             pb->holddata = pb->holdbuf;
1739             pb->translated = 1;
1740
1741             if (outbuf != stackoutbuf)
1742                 free (outbuf);
1743
1744             return 0;
1745         }
1746
1747         memcpy (data, outbuf + 2, tcount);
1748
1749         if (outbuf != stackoutbuf)
1750             free (outbuf);
1751
1752         pb->holdsize = 0;
1753
1754         data += tcount;
1755         need -= tcount;
1756         size -= tcount;
1757         *got += tcount;
1758     }
1759
1760     return 0;
1761 }
1762
1763
1764
1765 /* Output data to a packetizing buffer.  */
1766 static int
1767 packetizing_buffer_output (void *closure, const char *data, int have,
1768                            int *wrote)
1769 {
1770     struct packetizing_buffer *pb = closure;
1771     char inbuf[BUFFER_DATA_SIZE + 2];
1772     char stack_outbuf[BUFFER_DATA_SIZE + PACKET_SLOP + 4];
1773     struct buffer_data *outdata;
1774     char *outbuf;
1775     int size, status, translated;
1776
1777     if (have > BUFFER_DATA_SIZE)
1778     {
1779         /* It would be easy to xmalloc a buffer, but I don't think this
1780            case can ever arise.  */
1781         abort ();
1782     }
1783
1784     inbuf[0] = (have >> 8) & 0xff;
1785     inbuf[1] = have & 0xff;
1786     memcpy (inbuf + 2, data, have);
1787
1788     size = have + 2;
1789
1790     /* The output function is permitted to add up to PACKET_SLOP
1791        bytes, and we need 2 bytes for the size of the translated data.
1792        If we can guarantee that the result will fit in a buffer_data,
1793        we translate directly into one to avoid a memcpy in buf_output.  */
1794     if (size + PACKET_SLOP + 2 > BUFFER_DATA_SIZE)
1795         outbuf = stack_outbuf;
1796     else
1797     {
1798         outdata = get_buffer_data ();
1799         if (outdata == NULL)
1800         {
1801             (*pb->buf->memory_error) (pb->buf);
1802             return ENOMEM;
1803         }
1804
1805         outdata->next = NULL;
1806         outdata->bufp = outdata->text;
1807
1808         outbuf = outdata->text;
1809     }
1810
1811     status = (*pb->outfn) (pb->fnclosure, inbuf, outbuf + 2, size,
1812                            &translated);
1813     if (status != 0)
1814         return status;
1815
1816     /* The output function is permitted to add up to PACKET_SLOP
1817        bytes.  */
1818     if (translated > size + PACKET_SLOP)
1819         abort ();
1820
1821     outbuf[0] = (translated >> 8) & 0xff;
1822     outbuf[1] = translated & 0xff;
1823
1824     if (outbuf == stack_outbuf)
1825         buf_output (pb->buf, outbuf, translated + 2);
1826     else
1827     {
1828         outdata->size = translated + 2;
1829         buf_append_data (pb->buf, outdata, outdata);
1830     }
1831
1832     *wrote = have;
1833
1834     /* We will only be here because buf_send_output was called on the
1835        packetizing buffer.  That means that we should now call
1836        buf_send_output on the underlying buffer.  */
1837     return buf_send_output (pb->buf);
1838 }
1839
1840
1841
1842 /* Flush data to a packetizing buffer.  */
1843 static int
1844 packetizing_buffer_flush (void *closure)
1845 {
1846     struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1847
1848     /* Flush the underlying buffer.  Note that if the original call to
1849        buf_flush passed 1 for the BLOCK argument, then the buffer will
1850        already have been set into blocking mode, so we should always
1851        pass 0 here.  */
1852     return buf_flush (pb->buf, 0);
1853 }
1854
1855
1856
1857 /* The block routine for a packetizing buffer.  */
1858 static int
1859 packetizing_buffer_block (void *closure, int block)
1860 {
1861     struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1862
1863     if (block)
1864         return set_block (pb->buf);
1865     else
1866         return set_nonblock (pb->buf);
1867 }
1868
1869
1870
1871 /* Shut down a packetizing buffer.  */
1872 static int
1873 packetizing_buffer_shutdown (struct buffer *buf)
1874 {
1875     struct packetizing_buffer *pb = buf->closure;
1876
1877     return buf_shutdown (pb->buf);
1878 }
1879
1880 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */