xref: /openbsd/gnu/usr.bin/cvs/src/buffer.c (revision 2770ece5)
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 allocate_buffer_datas PROTO((void));
20 static struct buffer_data *get_buffer_data PROTO((void));
21 
22 /* Initialize a buffer structure.  */
23 
24 struct buffer *
25 buf_initialize (input, output, flush, block, shutdown, memory, closure)
26      int (*input) PROTO((void *, char *, int, int, int *));
27      int (*output) PROTO((void *, const char *, int, int *));
28      int (*flush) PROTO((void *));
29      int (*block) PROTO((void *, int));
30      int (*shutdown) PROTO((void *));
31      void (*memory) PROTO((struct buffer *));
32      void *closure;
33 {
34     struct buffer *buf;
35 
36     buf = (struct buffer *) xmalloc (sizeof (struct buffer));
37     buf->data = NULL;
38     buf->last = NULL;
39     buf->nonblocking = 0;
40     buf->input = input;
41     buf->output = output;
42     buf->flush = flush;
43     buf->block = block;
44     buf->shutdown = shutdown;
45     buf->memory_error = memory;
46     buf->closure = closure;
47     return buf;
48 }
49 
50 /* Initialize a buffer structure which is not to be used for I/O.  */
51 
52 struct buffer *
53 buf_nonio_initialize (memory)
54      void (*memory) PROTO((struct buffer *));
55 {
56     return (buf_initialize
57 	    ((int (*) PROTO((void *, char *, int, int, int *))) NULL,
58 	     (int (*) PROTO((void *, const char *, int, int *))) NULL,
59 	     (int (*) PROTO((void *))) NULL,
60 	     (int (*) PROTO((void *, int))) NULL,
61 	     (int (*) PROTO((void *))) NULL,
62 	     memory,
63 	     (void *) NULL));
64 }
65 
66 /* Allocate more buffer_data structures.  */
67 
68 static void
69 allocate_buffer_datas ()
70 {
71     struct buffer_data *alc;
72     char *space;
73     int i;
74 
75     /* Allocate buffer_data structures in blocks of 16.  */
76 #define ALLOC_COUNT (16)
77 
78     alc = ((struct buffer_data *)
79 	   malloc (ALLOC_COUNT * sizeof (struct buffer_data)));
80     space = (char *) valloc (ALLOC_COUNT * BUFFER_DATA_SIZE);
81     if (alc == NULL || space == NULL)
82 	return;
83     for (i = 0; i < ALLOC_COUNT; i++, alc++, space += BUFFER_DATA_SIZE)
84     {
85 	alc->next = free_buffer_data;
86 	free_buffer_data = alc;
87 	alc->text = space;
88     }
89 }
90 
91 /* Get a new buffer_data structure.  */
92 
93 static struct buffer_data *
94 get_buffer_data ()
95 {
96     struct buffer_data *ret;
97 
98     if (free_buffer_data == NULL)
99     {
100 	allocate_buffer_datas ();
101 	if (free_buffer_data == NULL)
102 	    return NULL;
103     }
104 
105     ret = free_buffer_data;
106     free_buffer_data = ret->next;
107     return ret;
108 }
109 
110 /* See whether a buffer is empty.  */
111 
112 int
113 buf_empty_p (buf)
114     struct buffer *buf;
115 {
116     struct buffer_data *data;
117 
118     for (data = buf->data; data != NULL; data = data->next)
119 	if (data->size > 0)
120 	    return 0;
121     return 1;
122 }
123 
124 #ifdef SERVER_FLOWCONTROL
125 /*
126  * Count how much data is stored in the buffer..
127  * Note that each buffer is a malloc'ed chunk BUFFER_DATA_SIZE.
128  */
129 
130 int
131 buf_count_mem (buf)
132     struct buffer *buf;
133 {
134     struct buffer_data *data;
135     int mem = 0;
136 
137     for (data = buf->data; data != NULL; data = data->next)
138 	mem += BUFFER_DATA_SIZE;
139 
140     return mem;
141 }
142 #endif /* SERVER_FLOWCONTROL */
143 
144 /* Add data DATA of length LEN to BUF.  */
145 
146 void
147 buf_output (buf, data, len)
148     struct buffer *buf;
149     const char *data;
150     int len;
151 {
152     if (buf->data != NULL
153 	&& (((buf->last->text + BUFFER_DATA_SIZE)
154 	     - (buf->last->bufp + buf->last->size))
155 	    >= len))
156     {
157 	memcpy (buf->last->bufp + buf->last->size, data, len);
158 	buf->last->size += len;
159 	return;
160     }
161 
162     while (1)
163     {
164 	struct buffer_data *newdata;
165 
166 	newdata = get_buffer_data ();
167 	if (newdata == NULL)
168 	{
169 	    (*buf->memory_error) (buf);
170 	    return;
171 	}
172 
173 	if (buf->data == NULL)
174 	    buf->data = newdata;
175 	else
176 	    buf->last->next = newdata;
177 	newdata->next = NULL;
178 	buf->last = newdata;
179 
180 	newdata->bufp = newdata->text;
181 
182 	if (len <= BUFFER_DATA_SIZE)
183 	{
184 	    newdata->size = len;
185 	    memcpy (newdata->text, data, len);
186 	    return;
187 	}
188 
189 	newdata->size = BUFFER_DATA_SIZE;
190 	memcpy (newdata->text, data, BUFFER_DATA_SIZE);
191 
192 	data += BUFFER_DATA_SIZE;
193 	len -= BUFFER_DATA_SIZE;
194     }
195 
196     /*NOTREACHED*/
197 }
198 
199 /* Add a '\0' terminated string to BUF.  */
200 
201 void
202 buf_output0 (buf, string)
203     struct buffer *buf;
204     const char *string;
205 {
206     buf_output (buf, string, strlen (string));
207 }
208 
209 /* Add a single character to BUF.  */
210 
211 void
212 buf_append_char (buf, ch)
213     struct buffer *buf;
214     int ch;
215 {
216     if (buf->data != NULL
217 	&& (buf->last->text + BUFFER_DATA_SIZE
218 	    != buf->last->bufp + buf->last->size))
219     {
220 	*(buf->last->bufp + buf->last->size) = ch;
221 	++buf->last->size;
222     }
223     else
224     {
225 	char b;
226 
227 	b = ch;
228 	buf_output (buf, &b, 1);
229     }
230 }
231 
232 /*
233  * Send all the output we've been saving up.  Returns 0 for success or
234  * errno code.  If the buffer has been set to be nonblocking, this
235  * will just write until the write would block.
236  */
237 
238 int
239 buf_send_output (buf)
240      struct buffer *buf;
241 {
242     if (buf->output == NULL)
243 	abort ();
244 
245     while (buf->data != NULL)
246     {
247 	struct buffer_data *data;
248 
249 	data = buf->data;
250 
251 	if (data->size > 0)
252 	{
253 	    int status, nbytes;
254 
255 	    status = (*buf->output) (buf->closure, data->bufp, data->size,
256 				     &nbytes);
257 	    if (status != 0)
258 	    {
259 		/* Some sort of error.  Discard the data, and return.  */
260 
261 		buf->last->next = free_buffer_data;
262 		free_buffer_data = buf->data;
263 		buf->data = NULL;
264 		buf->last = NULL;
265 
266 	        return status;
267 	    }
268 
269 	    if (nbytes != data->size)
270 	    {
271 		/* Not all the data was written out.  This is only
272                    permitted in nonblocking mode.  Adjust the buffer,
273                    and return.  */
274 
275 		assert (buf->nonblocking);
276 
277 		data->size -= nbytes;
278 		data->bufp += nbytes;
279 
280 		return 0;
281 	    }
282 	}
283 
284 	buf->data = data->next;
285 	data->next = free_buffer_data;
286 	free_buffer_data = data;
287     }
288 
289     buf->last = NULL;
290 
291     return 0;
292 }
293 
294 /*
295  * Flush any data queued up in the buffer.  If BLOCK is nonzero, then
296  * if the buffer is in nonblocking mode, put it into blocking mode for
297  * the duration of the flush.  This returns 0 on success, or an error
298  * code.
299  */
300 
301 int
302 buf_flush (buf, block)
303      struct buffer *buf;
304      int block;
305 {
306     int nonblocking;
307     int status;
308 
309     if (buf->flush == NULL)
310         abort ();
311 
312     nonblocking = buf->nonblocking;
313     if (nonblocking && block)
314     {
315         status = set_block (buf);
316 	if (status != 0)
317 	    return status;
318     }
319 
320     status = buf_send_output (buf);
321     if (status == 0)
322         status = (*buf->flush) (buf->closure);
323 
324     if (nonblocking && block)
325     {
326         int blockstat;
327 
328         blockstat = set_nonblock (buf);
329 	if (status == 0)
330 	    status = blockstat;
331     }
332 
333     return status;
334 }
335 
336 /*
337  * Set buffer BUF to nonblocking I/O.  Returns 0 for success or errno
338  * code.
339  */
340 
341 int
342 set_nonblock (buf)
343      struct buffer *buf;
344 {
345     int status;
346 
347     if (buf->nonblocking)
348 	return 0;
349     if (buf->block == NULL)
350         abort ();
351     status = (*buf->block) (buf->closure, 0);
352     if (status != 0)
353 	return status;
354     buf->nonblocking = 1;
355     return 0;
356 }
357 
358 /*
359  * Set buffer BUF to blocking I/O.  Returns 0 for success or errno
360  * code.
361  */
362 
363 int
364 set_block (buf)
365      struct buffer *buf;
366 {
367     int status;
368 
369     if (! buf->nonblocking)
370 	return 0;
371     if (buf->block == NULL)
372         abort ();
373     status = (*buf->block) (buf->closure, 1);
374     if (status != 0)
375 	return status;
376     buf->nonblocking = 0;
377     return 0;
378 }
379 
380 /*
381  * Send a character count and some output.  Returns errno code or 0 for
382  * success.
383  *
384  * Sending the count in binary is OK since this is only used on a pipe
385  * within the same system.
386  */
387 
388 int
389 buf_send_counted (buf)
390      struct buffer *buf;
391 {
392     int size;
393     struct buffer_data *data;
394 
395     size = 0;
396     for (data = buf->data; data != NULL; data = data->next)
397 	size += data->size;
398 
399     data = get_buffer_data ();
400     if (data == NULL)
401     {
402 	(*buf->memory_error) (buf);
403 	return ENOMEM;
404     }
405 
406     data->next = buf->data;
407     buf->data = data;
408     if (buf->last == NULL)
409 	buf->last = data;
410 
411     data->bufp = data->text;
412     data->size = sizeof (int);
413 
414     *((int *) data->text) = size;
415 
416     return buf_send_output (buf);
417 }
418 
419 /*
420  * Send a special count.  COUNT should be negative.  It will be
421  * handled speciallyi by buf_copy_counted.  This function returns 0 or
422  * an errno code.
423  *
424  * Sending the count in binary is OK since this is only used on a pipe
425  * within the same system.
426  */
427 
428 int
429 buf_send_special_count (buf, count)
430      struct buffer *buf;
431      int count;
432 {
433     struct buffer_data *data;
434 
435     data = get_buffer_data ();
436     if (data == NULL)
437     {
438 	(*buf->memory_error) (buf);
439 	return ENOMEM;
440     }
441 
442     data->next = buf->data;
443     buf->data = data;
444     if (buf->last == NULL)
445 	buf->last = data;
446 
447     data->bufp = data->text;
448     data->size = sizeof (int);
449 
450     *((int *) data->text) = count;
451 
452     return buf_send_output (buf);
453 }
454 
455 /* Append a list of buffer_data structures to an buffer.  */
456 
457 void
458 buf_append_data (buf, data, last)
459      struct buffer *buf;
460      struct buffer_data *data;
461      struct buffer_data *last;
462 {
463     if (data != NULL)
464     {
465 	if (buf->data == NULL)
466 	    buf->data = data;
467 	else
468 	    buf->last->next = data;
469 	buf->last = last;
470     }
471 }
472 
473 /*
474  * Copy the contents of file F into buffer_data structures.  We can't
475  * copy directly into an buffer, because we want to handle failure and
476  * succeess differently.  Returns 0 on success, or -2 if out of
477  * memory, or a status code on error.  Since the caller happens to
478  * know the size of the file, it is passed in as SIZE.  On success,
479  * this function sets *RETP and *LASTP, which may be passed to
480  * buf_append_data.
481  */
482 
483 int
484 buf_read_file (f, size, retp, lastp)
485     FILE *f;
486     long size;
487     struct buffer_data **retp;
488     struct buffer_data **lastp;
489 {
490     int status;
491 
492     *retp = NULL;
493     *lastp = NULL;
494 
495     while (size > 0)
496     {
497 	struct buffer_data *data;
498 	int get;
499 
500 	data = get_buffer_data ();
501 	if (data == NULL)
502 	{
503 	    status = -2;
504 	    goto error_return;
505 	}
506 
507 	if (*retp == NULL)
508 	    *retp = data;
509 	else
510 	    (*lastp)->next = data;
511 	data->next = NULL;
512 	*lastp = data;
513 
514 	data->bufp = data->text;
515 	data->size = 0;
516 
517 	if (size > BUFFER_DATA_SIZE)
518 	    get = BUFFER_DATA_SIZE;
519 	else
520 	    get = size;
521 
522 	errno = EIO;
523 	if (fread (data->text, get, 1, f) != 1)
524 	{
525 	    status = errno;
526 	    goto error_return;
527 	}
528 
529 	data->size += get;
530 	size -= get;
531     }
532 
533     return 0;
534 
535   error_return:
536     if (*retp != NULL)
537     {
538 	(*lastp)->next = free_buffer_data;
539 	free_buffer_data = *retp;
540     }
541     return status;
542 }
543 
544 /*
545  * Copy the contents of file F into buffer_data structures.  We can't
546  * copy directly into an buffer, because we want to handle failure and
547  * succeess differently.  Returns 0 on success, or -2 if out of
548  * memory, or a status code on error.  On success, this function sets
549  * *RETP and *LASTP, which may be passed to buf_append_data.
550  */
551 
552 int
553 buf_read_file_to_eof (f, retp, lastp)
554      FILE *f;
555      struct buffer_data **retp;
556      struct buffer_data **lastp;
557 {
558     int status;
559 
560     *retp = NULL;
561     *lastp = NULL;
562 
563     while (!feof (f))
564     {
565 	struct buffer_data *data;
566 	int get, nread;
567 
568 	data = get_buffer_data ();
569 	if (data == NULL)
570 	{
571 	    status = -2;
572 	    goto error_return;
573 	}
574 
575 	if (*retp == NULL)
576 	    *retp = data;
577 	else
578 	    (*lastp)->next = data;
579 	data->next = NULL;
580 	*lastp = data;
581 
582 	data->bufp = data->text;
583 	data->size = 0;
584 
585 	get = BUFFER_DATA_SIZE;
586 
587 	errno = EIO;
588 	nread = fread (data->text, 1, get, f);
589 	if (nread == 0 && !feof (f))
590 	{
591 	    status = errno;
592 	    goto error_return;
593 	}
594 
595 	data->size = nread;
596     }
597 
598     return 0;
599 
600   error_return:
601     if (*retp != NULL)
602     {
603 	(*lastp)->next = free_buffer_data;
604 	free_buffer_data = *retp;
605     }
606     return status;
607 }
608 
609 /* Return the number of bytes in a chain of buffer_data structures.  */
610 
611 int
612 buf_chain_length (buf)
613      struct buffer_data *buf;
614 {
615     int size = 0;
616     while (buf)
617     {
618 	size += buf->size;
619 	buf = buf->next;
620     }
621     return size;
622 }
623 
624 /*
625  * Read an arbitrary amount of data into an input buffer.  The buffer
626  * will be in nonblocking mode, and we just grab what we can.  Return
627  * 0 on success, or -1 on end of file, or -2 if out of memory, or an
628  * error code.  If COUNTP is not NULL, *COUNTP is set to the number of
629  * bytes read.
630  */
631 
632 int
633 buf_input_data (buf, countp)
634      struct buffer *buf;
635      int *countp;
636 {
637     if (buf->input == NULL)
638 	abort ();
639 
640     if (countp != NULL)
641 	*countp = 0;
642 
643     while (1)
644     {
645 	int get;
646 	int status, nbytes;
647 
648 	if (buf->data == NULL
649 	    || (buf->last->bufp + buf->last->size
650 		== buf->last->text + BUFFER_DATA_SIZE))
651 	{
652 	    struct buffer_data *data;
653 
654 	    data = get_buffer_data ();
655 	    if (data == NULL)
656 	    {
657 		(*buf->memory_error) (buf);
658 		return -2;
659 	    }
660 
661 	    if (buf->data == NULL)
662 		buf->data = data;
663 	    else
664 		buf->last->next = data;
665 	    data->next = NULL;
666 	    buf->last = data;
667 
668 	    data->bufp = data->text;
669 	    data->size = 0;
670 	}
671 
672 	get = ((buf->last->text + BUFFER_DATA_SIZE)
673 	       - (buf->last->bufp + buf->last->size));
674 
675 	status = (*buf->input) (buf->closure,
676 				buf->last->bufp + buf->last->size,
677 				0, get, &nbytes);
678 	if (status != 0)
679 	    return status;
680 
681 	buf->last->size += nbytes;
682 	if (countp != NULL)
683 	    *countp += nbytes;
684 
685 	if (nbytes < get)
686 	{
687 	    /* If we did not fill the buffer, then presumably we read
688                all the available data.  */
689 	    return 0;
690 	}
691     }
692 
693     /*NOTREACHED*/
694 }
695 
696 /*
697  * Read a line (characters up to a \012) from an input buffer.  (We
698  * use \012 rather than \n for the benefit of non Unix clients for
699  * which \n means something else).  This returns 0 on success, or -1
700  * on end of file, or -2 if out of memory, or an error code.  If it
701  * succeeds, it sets *LINE to an allocated buffer holding the contents
702  * of the line.  The trailing \012 is not included in the buffer.  If
703  * LENP is not NULL, then *LENP is set to the number of bytes read;
704  * strlen may not work, because there may be embedded null bytes.
705  */
706 
707 int
708 buf_read_line (buf, line, lenp)
709      struct buffer *buf;
710      char **line;
711      int *lenp;
712 {
713     if (buf->input == NULL)
714         abort ();
715 
716     *line = NULL;
717 
718     while (1)
719     {
720 	int len, finallen = 0;
721 	struct buffer_data *data;
722 	char *nl;
723 
724 	/* See if there is a newline in BUF.  */
725 	len = 0;
726 	for (data = buf->data; data != NULL; data = data->next)
727 	{
728 	    nl = memchr (data->bufp, '\012', data->size);
729 	    if (nl != NULL)
730 	    {
731 	        finallen = nl - data->bufp;
732 	        len += finallen;
733 		break;
734 	    }
735 	    len += data->size;
736 	}
737 
738 	/* If we found a newline, copy the line into a memory buffer,
739            and remove it from BUF.  */
740 	if (data != NULL)
741 	{
742 	    char *p;
743 	    struct buffer_data *nldata;
744 
745 	    p = malloc (len + 1);
746 	    if (p == NULL)
747 		return -2;
748 	    *line = p;
749 
750 	    nldata = data;
751 	    data = buf->data;
752 	    while (data != nldata)
753 	    {
754 		struct buffer_data *next;
755 
756 		memcpy (p, data->bufp, data->size);
757 		p += data->size;
758 		next = data->next;
759 		data->next = free_buffer_data;
760 		free_buffer_data = data;
761 		data = next;
762 	    }
763 
764 	    memcpy (p, data->bufp, finallen);
765 	    p[finallen] = '\0';
766 
767 	    data->size -= finallen + 1;
768 	    data->bufp = nl + 1;
769 	    buf->data = data;
770 
771 	    if (lenp != NULL)
772 	        *lenp = len;
773 
774 	    return 0;
775 	}
776 
777 	/* Read more data until we get a newline.  */
778 	while (1)
779 	{
780 	    int size, status, nbytes;
781 	    char *mem;
782 
783 	    if (buf->data == NULL
784 		|| (buf->last->bufp + buf->last->size
785 		    == buf->last->text + BUFFER_DATA_SIZE))
786 	    {
787 		data = get_buffer_data ();
788 		if (data == NULL)
789 		{
790 		    (*buf->memory_error) (buf);
791 		    return -2;
792 		}
793 
794 		if (buf->data == NULL)
795 		    buf->data = data;
796 		else
797 		    buf->last->next = data;
798 		data->next = NULL;
799 		buf->last = data;
800 
801 		data->bufp = data->text;
802 		data->size = 0;
803 	    }
804 
805 	    mem = buf->last->bufp + buf->last->size;
806 	    size = (buf->last->text + BUFFER_DATA_SIZE) - mem;
807 
808 	    /* We need to read at least 1 byte.  We can handle up to
809                SIZE bytes.  This will only be efficient if the
810                underlying communication stream does its own buffering,
811                or is clever about getting more than 1 byte at a time.  */
812 	    status = (*buf->input) (buf->closure, mem, 1, size, &nbytes);
813 	    if (status != 0)
814 		return status;
815 
816 	    buf->last->size += nbytes;
817 
818 	    /* Optimize slightly to avoid an unnecessary call to
819                memchr.  */
820 	    if (nbytes == 1)
821 	    {
822 		if (*mem == '\012')
823 		    break;
824 	    }
825 	    else
826 	    {
827 		if (memchr (mem, '\012', nbytes) != NULL)
828 		    break;
829 	    }
830 	}
831     }
832 }
833 
834 /*
835  * Extract data from the input buffer BUF.  This will read up to WANT
836  * bytes from the buffer.  It will set *RETDATA to point at the bytes,
837  * and set *GOT to the number of bytes to be found there.  Any buffer
838  * call which uses BUF may change the contents of the buffer at *DATA,
839  * so the data should be fully processed before any further calls are
840  * made.  This returns 0 on success, or -1 on end of file, or -2 if
841  * out of memory, or an error code.
842  */
843 
844 int
845 buf_read_data (buf, want, retdata, got)
846      struct buffer *buf;
847      int want;
848      char **retdata;
849      int *got;
850 {
851     if (buf->input == NULL)
852 	abort ();
853 
854     while (buf->data != NULL && buf->data->size == 0)
855     {
856 	struct buffer_data *next;
857 
858 	next = buf->data->next;
859 	buf->data->next = free_buffer_data;
860 	free_buffer_data = buf->data;
861 	buf->data = next;
862 	if (next == NULL)
863 	    buf->last = NULL;
864     }
865 
866     if (buf->data == NULL)
867     {
868 	struct buffer_data *data;
869 	int get, status, nbytes;
870 
871 	data = get_buffer_data ();
872 	if (data == NULL)
873 	{
874 	    (*buf->memory_error) (buf);
875 	    return -2;
876 	}
877 
878 	buf->data = data;
879 	buf->last = data;
880 	data->next = NULL;
881 	data->bufp = data->text;
882 	data->size = 0;
883 
884 	if (want < BUFFER_DATA_SIZE)
885 	    get = want;
886 	else
887 	    get = BUFFER_DATA_SIZE;
888 	status = (*buf->input) (buf->closure, data->bufp, get,
889 				BUFFER_DATA_SIZE, &nbytes);
890 	if (status != 0)
891 	    return status;
892 
893 	data->size = nbytes;
894     }
895 
896     *retdata = buf->data->bufp;
897     if (want < buf->data->size)
898     {
899         *got = want;
900 	buf->data->size -= want;
901 	buf->data->bufp += want;
902     }
903     else
904     {
905         *got = buf->data->size;
906 	buf->data->size = 0;
907     }
908 
909     return 0;
910 }
911 
912 /*
913  * Copy lines from an input buffer to an output buffer.  This copies
914  * all complete lines (characters up to a newline) from INBUF to
915  * OUTBUF.  Each line in OUTBUF is preceded by the character COMMAND
916  * and a space.
917  */
918 
919 void
920 buf_copy_lines (outbuf, inbuf, command)
921      struct buffer *outbuf;
922      struct buffer *inbuf;
923      int command;
924 {
925     while (1)
926     {
927 	struct buffer_data *data;
928 	struct buffer_data *nldata;
929 	char *nl;
930 	int len;
931 
932 	/* See if there is a newline in INBUF.  */
933 	nldata = NULL;
934 	nl = NULL;
935 	for (data = inbuf->data; data != NULL; data = data->next)
936 	{
937 	    nl = memchr (data->bufp, '\n', data->size);
938 	    if (nl != NULL)
939 	    {
940 		nldata = data;
941 		break;
942 	    }
943 	}
944 
945 	if (nldata == NULL)
946 	{
947 	    /* There are no more lines in INBUF.  */
948 	    return;
949 	}
950 
951 	/* Put in the command.  */
952 	buf_append_char (outbuf, command);
953 	buf_append_char (outbuf, ' ');
954 
955 	if (inbuf->data != nldata)
956 	{
957 	    /*
958 	     * Simply move over all the buffers up to the one containing
959 	     * the newline.
960 	     */
961 	    for (data = inbuf->data; data->next != nldata; data = data->next)
962 		;
963 	    data->next = NULL;
964 	    buf_append_data (outbuf, inbuf->data, data);
965 	    inbuf->data = nldata;
966 	}
967 
968 	/*
969 	 * If the newline is at the very end of the buffer, just move
970 	 * the buffer onto OUTBUF.  Otherwise we must copy the data.
971 	 */
972 	len = nl + 1 - nldata->bufp;
973 	if (len == nldata->size)
974 	{
975 	    inbuf->data = nldata->next;
976 	    if (inbuf->data == NULL)
977 		inbuf->last = NULL;
978 
979 	    nldata->next = NULL;
980 	    buf_append_data (outbuf, nldata, nldata);
981 	}
982 	else
983 	{
984 	    buf_output (outbuf, nldata->bufp, len);
985 	    nldata->bufp += len;
986 	    nldata->size -= len;
987 	}
988     }
989 }
990 
991 /*
992  * Copy counted data from one buffer to another.  The count is an
993  * integer, host size, host byte order (it is only used across a
994  * pipe).  If there is enough data, it should be moved over.  If there
995  * is not enough data, it should remain on the original buffer.  A
996  * negative count is a special case.  if one is seen, *SPECIAL is set
997  * to the (negative) count value and no additional data is gathered
998  * from the buffer; normally *SPECIAL is set to 0.  This function
999  * returns the number of bytes it needs to see in order to actually
1000  * copy something over.
1001  */
1002 
1003 int
1004 buf_copy_counted (outbuf, inbuf, special)
1005      struct buffer *outbuf;
1006      struct buffer *inbuf;
1007      int *special;
1008 {
1009     *special = 0;
1010 
1011     while (1)
1012     {
1013 	struct buffer_data *data;
1014 	int need;
1015 	union
1016 	{
1017 	    char intbuf[sizeof (int)];
1018 	    int i;
1019 	} u;
1020 	char *intp;
1021 	int count;
1022 	struct buffer_data *start;
1023 	int startoff;
1024 	struct buffer_data *stop;
1025 	int stopwant;
1026 
1027 	/* See if we have enough bytes to figure out the count.  */
1028 	need = sizeof (int);
1029 	intp = u.intbuf;
1030 	for (data = inbuf->data; data != NULL; data = data->next)
1031 	{
1032 	    if (data->size >= need)
1033 	    {
1034 		memcpy (intp, data->bufp, need);
1035 		break;
1036 	    }
1037 	    memcpy (intp, data->bufp, data->size);
1038 	    intp += data->size;
1039 	    need -= data->size;
1040 	}
1041 	if (data == NULL)
1042 	{
1043 	    /* We don't have enough bytes to form an integer.  */
1044 	    return need;
1045 	}
1046 
1047 	count = u.i;
1048 	start = data;
1049 	startoff = need;
1050 
1051 	if (count < 0)
1052 	{
1053 	    /* A negative COUNT is a special case meaning that we
1054                don't need any further information.  */
1055 	    stop = start;
1056 	    stopwant = 0;
1057 	}
1058 	else
1059 	{
1060 	    /*
1061 	     * We have an integer in COUNT.  We have gotten all the
1062 	     * data from INBUF in all buffers before START, and we
1063 	     * have gotten STARTOFF bytes from START.  See if we have
1064 	     * enough bytes remaining in INBUF.
1065 	     */
1066 	    need = count - (start->size - startoff);
1067 	    if (need <= 0)
1068 	    {
1069 		stop = start;
1070 		stopwant = count;
1071 	    }
1072 	    else
1073 	    {
1074 		for (data = start->next; data != NULL; data = data->next)
1075 		{
1076 		    if (need <= data->size)
1077 			break;
1078 		    need -= data->size;
1079 		}
1080 		if (data == NULL)
1081 		{
1082 		    /* We don't have enough bytes.  */
1083 		    return need;
1084 		}
1085 		stop = data;
1086 		stopwant = need;
1087 	    }
1088 	}
1089 
1090 	/*
1091 	 * We have enough bytes.  Free any buffers in INBUF before
1092 	 * START, and remove STARTOFF bytes from START, so that we can
1093 	 * forget about STARTOFF.
1094 	 */
1095 	start->bufp += startoff;
1096 	start->size -= startoff;
1097 
1098 	if (start->size == 0)
1099 	    start = start->next;
1100 
1101 	if (stop->size == stopwant)
1102 	{
1103 	    stop = stop->next;
1104 	    stopwant = 0;
1105 	}
1106 
1107 	while (inbuf->data != start)
1108 	{
1109 	    data = inbuf->data;
1110 	    inbuf->data = data->next;
1111 	    data->next = free_buffer_data;
1112 	    free_buffer_data = data;
1113 	}
1114 
1115 	/* If COUNT is negative, set *SPECIAL and get out now.  */
1116 	if (count < 0)
1117 	{
1118 	    *special = count;
1119 	    return 0;
1120 	}
1121 
1122 	/*
1123 	 * We want to copy over the bytes from START through STOP.  We
1124 	 * only want STOPWANT bytes from STOP.
1125 	 */
1126 
1127 	if (start != stop)
1128 	{
1129 	    /* Attach the buffers from START through STOP to OUTBUF.  */
1130 	    for (data = start; data->next != stop; data = data->next)
1131 		;
1132 	    inbuf->data = stop;
1133 	    data->next = NULL;
1134 	    buf_append_data (outbuf, start, data);
1135 	}
1136 
1137 	if (stopwant > 0)
1138 	{
1139 	    buf_output (outbuf, stop->bufp, stopwant);
1140 	    stop->bufp += stopwant;
1141 	    stop->size -= stopwant;
1142 	}
1143     }
1144 
1145     /*NOTREACHED*/
1146 }
1147 
1148 /* Shut down a buffer.  This returns 0 on success, or an errno code.  */
1149 
1150 int
1151 buf_shutdown (buf)
1152      struct buffer *buf;
1153 {
1154     if (buf->shutdown)
1155 	return (*buf->shutdown) (buf->closure);
1156     return 0;
1157 }
1158 
1159 /* The simplest type of buffer is one built on top of a stdio FILE.
1160    For simplicity, and because it is all that is required, we do not
1161    implement setting this type of buffer into nonblocking mode.  The
1162    closure field is just a FILE *.  */
1163 
1164 static int stdio_buffer_input PROTO((void *, char *, int, int, int *));
1165 static int stdio_buffer_output PROTO((void *, const char *, int, int *));
1166 static int stdio_buffer_flush PROTO((void *));
1167 
1168 /* Initialize a buffer built on a stdio FILE.  */
1169 
1170 struct buffer
1171 *stdio_buffer_initialize (fp, input, memory)
1172      FILE *fp;
1173      int input;
1174      void (*memory) PROTO((struct buffer *));
1175 {
1176     return buf_initialize (input ? stdio_buffer_input : NULL,
1177 			   input ? NULL : stdio_buffer_output,
1178 			   input ? NULL : stdio_buffer_flush,
1179 			   (int (*) PROTO((void *, int))) NULL,
1180 			   (int (*) PROTO((void *))) NULL,
1181 			   memory,
1182 			   (void *) fp);
1183 }
1184 
1185 /* The buffer input function for a buffer built on a stdio FILE.  */
1186 
1187 static int
1188 stdio_buffer_input (closure, data, need, size, got)
1189      void *closure;
1190      char *data;
1191      int need;
1192      int size;
1193      int *got;
1194 {
1195     FILE *fp = (FILE *) closure;
1196     int nbytes;
1197 
1198     /* Since stdio does its own buffering, we don't worry about
1199        getting more bytes than we need.  */
1200 
1201     if (need == 0 || need == 1)
1202     {
1203         int ch;
1204 
1205 	ch = getc (fp);
1206 
1207 	if (ch == EOF)
1208 	{
1209 	    if (feof (fp))
1210 		return -1;
1211 	    else if (errno == 0)
1212 		return EIO;
1213 	    else
1214 		return errno;
1215 	}
1216 
1217 	*data = ch;
1218 	*got = 1;
1219 	return 0;
1220     }
1221 
1222     nbytes = fread (data, 1, need, fp);
1223 
1224     if (nbytes == 0)
1225     {
1226 	*got = 0;
1227 	if (feof (fp))
1228 	    return -1;
1229 	else if (errno == 0)
1230 	    return EIO;
1231 	else
1232 	    return errno;
1233     }
1234 
1235     *got = nbytes;
1236 
1237     return 0;
1238 }
1239 
1240 /* The buffer output function for a buffer built on a stdio FILE.  */
1241 
1242 static int
1243 stdio_buffer_output (closure, data, have, wrote)
1244      void *closure;
1245      const char *data;
1246      int have;
1247      int *wrote;
1248 {
1249     FILE *fp = (FILE *) closure;
1250 
1251     *wrote = 0;
1252 
1253     while (have > 0)
1254     {
1255 	int nbytes;
1256 
1257 	nbytes = fwrite (data, 1, have, fp);
1258 
1259 	if (nbytes != have)
1260 	{
1261 	    if (errno == 0)
1262 		return EIO;
1263 	    else
1264 		return errno;
1265 	}
1266 
1267 	*wrote += nbytes;
1268 	have -= nbytes;
1269 	data += nbytes;
1270     }
1271 
1272     return 0;
1273 }
1274 
1275 /* The buffer flush function for a buffer built on a stdio FILE.  */
1276 
1277 static int
1278 stdio_buffer_flush (closure)
1279      void *closure;
1280 {
1281     FILE *fp = (FILE *) closure;
1282 
1283     if (fflush (fp) != 0)
1284     {
1285 	if (errno == 0)
1286 	    return EIO;
1287 	else
1288 	    return errno;
1289     }
1290 
1291     return 0;
1292 }
1293 
1294 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */
1295