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