17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3*1da57d55SToomas Soome  *
47c478bd9Sstevel@tonic-gate  * All rights reserved.
5*1da57d55SToomas Soome  *
67c478bd9Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
77c478bd9Sstevel@tonic-gate  * copy of this software and associated documentation files (the
87c478bd9Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
97c478bd9Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
107c478bd9Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
117c478bd9Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
127c478bd9Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
137c478bd9Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
147c478bd9Sstevel@tonic-gate  * permission notice appear in supporting documentation.
15*1da57d55SToomas Soome  *
167c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
177c478bd9Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
187c478bd9Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
197c478bd9Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
207c478bd9Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
217c478bd9Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
227c478bd9Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
237c478bd9Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
247c478bd9Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25*1da57d55SToomas Soome  *
267c478bd9Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
277c478bd9Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
287c478bd9Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
297c478bd9Sstevel@tonic-gate  * of the copyright holder.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "ioutil.h"
387c478bd9Sstevel@tonic-gate #include "chrqueue.h"
397c478bd9Sstevel@tonic-gate #include "freelist.h"
407c478bd9Sstevel@tonic-gate #include "errmsg.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Set the number of bytes allocated to each node of the list of
447c478bd9Sstevel@tonic-gate  * character buffers. This facility is designed principally as
457c478bd9Sstevel@tonic-gate  * an expandible I/O output buffer, so use the stdio buffer size
467c478bd9Sstevel@tonic-gate  * where available.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate #ifdef BUFSIZ
497c478bd9Sstevel@tonic-gate #define GL_CQ_SIZE BUFSIZ
507c478bd9Sstevel@tonic-gate #else
517c478bd9Sstevel@tonic-gate #define GL_CQ_SIZE 512
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * The queue is contained in a list of fixed sized buffers. New nodes
567c478bd9Sstevel@tonic-gate  * are appended to this list as needed to accomodate newly added bytes.
577c478bd9Sstevel@tonic-gate  * Old nodes at the head of the list are removed as they are emptied.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate typedef struct CqCharBuff CqCharBuff;
607c478bd9Sstevel@tonic-gate struct CqCharBuff {
617c478bd9Sstevel@tonic-gate   CqCharBuff *next;          /* The next node in the list of buffers */
627c478bd9Sstevel@tonic-gate   char bytes[GL_CQ_SIZE];    /* The fixed size buffer of this node */
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Define the structure that is used to contain a list of character
677c478bd9Sstevel@tonic-gate  * buffers.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate struct GlCharQueue {
707c478bd9Sstevel@tonic-gate   ErrMsg *err;          /* A buffer in which to record error messages */
717c478bd9Sstevel@tonic-gate   FreeList *bufmem;     /* A free-list of CqCharBuff structures */
727c478bd9Sstevel@tonic-gate   struct {
737c478bd9Sstevel@tonic-gate     CqCharBuff *head;   /* The head of the list of output buffers */
747c478bd9Sstevel@tonic-gate     CqCharBuff *tail;   /* The tail of the list of output buffers */
757c478bd9Sstevel@tonic-gate   } buffers;
767c478bd9Sstevel@tonic-gate   int nflush;           /* The total number of characters that have been */
777c478bd9Sstevel@tonic-gate                         /*  flushed from the start of the queue since */
787c478bd9Sstevel@tonic-gate                         /*  _glq_empty_queue() was last called. */
797c478bd9Sstevel@tonic-gate   int ntotal;           /* The total number of characters that have been */
807c478bd9Sstevel@tonic-gate                         /*  appended to the queue since _glq_empty_queue() */
817c478bd9Sstevel@tonic-gate                         /*  was last called. */
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*.......................................................................
857c478bd9Sstevel@tonic-gate  * Create a new GlCharQueue object.
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  * Output:
887c478bd9Sstevel@tonic-gate  *  return  GlCharQueue *  The new object, or NULL on error.
897c478bd9Sstevel@tonic-gate  */
_new_GlCharQueue(void)907c478bd9Sstevel@tonic-gate GlCharQueue *_new_GlCharQueue(void)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate   GlCharQueue *cq;  /* The object to be returned */
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Allocate the container.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate   cq = malloc(sizeof(GlCharQueue));
977c478bd9Sstevel@tonic-gate   if(!cq) {
987c478bd9Sstevel@tonic-gate     errno = ENOMEM;
997c478bd9Sstevel@tonic-gate     return NULL;
1007c478bd9Sstevel@tonic-gate   };
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * Before attempting any operation that might fail, initialize the
1037c478bd9Sstevel@tonic-gate  * container at least up to the point at which it can safely be passed
1047c478bd9Sstevel@tonic-gate  * to del_GlCharQueue().
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate   cq->err = NULL;
1077c478bd9Sstevel@tonic-gate   cq->bufmem = NULL;
1087c478bd9Sstevel@tonic-gate   cq->buffers.head = NULL;
1097c478bd9Sstevel@tonic-gate   cq->buffers.tail = NULL;
1107c478bd9Sstevel@tonic-gate   cq->nflush = cq->ntotal = 0;
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * Allocate a place to record error messages.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate   cq->err = _new_ErrMsg();
1157c478bd9Sstevel@tonic-gate   if(!cq->err)
1167c478bd9Sstevel@tonic-gate     return _del_GlCharQueue(cq);
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * Allocate the freelist of CqCharBuff structures.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate   cq->bufmem = _new_FreeList(sizeof(CqCharBuff), 1);
1217c478bd9Sstevel@tonic-gate   if(!cq->bufmem)
1227c478bd9Sstevel@tonic-gate     return _del_GlCharQueue(cq);
1237c478bd9Sstevel@tonic-gate   return cq;
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*.......................................................................
1277c478bd9Sstevel@tonic-gate  * Delete a GlCharQueue object.
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * Input:
1307c478bd9Sstevel@tonic-gate  *  cq     GlCharQueue *  The object to be deleted.
1317c478bd9Sstevel@tonic-gate  * Output:
1327c478bd9Sstevel@tonic-gate  *  return GlCharQueue *  The deleted object (always NULL).
1337c478bd9Sstevel@tonic-gate  */
_del_GlCharQueue(GlCharQueue * cq)1347c478bd9Sstevel@tonic-gate GlCharQueue *_del_GlCharQueue(GlCharQueue *cq)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate   if(cq) {
1377c478bd9Sstevel@tonic-gate     cq->err = _del_ErrMsg(cq->err);
1387c478bd9Sstevel@tonic-gate     cq->bufmem = _del_FreeList(cq->bufmem, 1);
1397c478bd9Sstevel@tonic-gate     free(cq);
1407c478bd9Sstevel@tonic-gate   };
1417c478bd9Sstevel@tonic-gate   return NULL;
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*.......................................................................
1457c478bd9Sstevel@tonic-gate  * Append an array of n characters to a character queue.
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  * Input:
1487c478bd9Sstevel@tonic-gate  *  cq        GlCharQueue *  The queue to append to.
1497c478bd9Sstevel@tonic-gate  *  chars      const char *  The array of n characters to be appended.
1507c478bd9Sstevel@tonic-gate  *  n                 int    The number of characters in chars[].
1517c478bd9Sstevel@tonic-gate  *  write_fn  GL_WRITE_FN *  The function to call to output characters,
1527c478bd9Sstevel@tonic-gate  *                           or 0 to simply discard the contents of the
1537c478bd9Sstevel@tonic-gate  *                           queue. This will be called whenever the
1547c478bd9Sstevel@tonic-gate  *                           buffer becomes full. If it fails to release
1557c478bd9Sstevel@tonic-gate  *                           any space, the buffer will be extended.
1567c478bd9Sstevel@tonic-gate  *  data             void *  Anonymous data to pass to write_fn().
1577c478bd9Sstevel@tonic-gate  * Output:
1587c478bd9Sstevel@tonic-gate  *  return        int    The number of characters successfully
1597c478bd9Sstevel@tonic-gate  *                       appended. This will only be < n on error.
1607c478bd9Sstevel@tonic-gate  */
_glq_append_chars(GlCharQueue * cq,const char * chars,int n,GlWriteFn * write_fn,void * data)1617c478bd9Sstevel@tonic-gate int _glq_append_chars(GlCharQueue *cq, const char *chars, int n,
1627c478bd9Sstevel@tonic-gate 		      GlWriteFn *write_fn, void *data)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate   int ndone = 0;  /* The number of characters appended so far */
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Check the arguments.
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate   if(!cq || !chars) {
1697c478bd9Sstevel@tonic-gate     errno = EINVAL;
1707c478bd9Sstevel@tonic-gate     return 0;
1717c478bd9Sstevel@tonic-gate   };
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * The appended characters may have to be split between multiple
1747c478bd9Sstevel@tonic-gate  * buffers, so loop for each buffer.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate   while(ndone < n) {
1777c478bd9Sstevel@tonic-gate     int ntodo;     /* The number of characters remaining to be appended */
1787c478bd9Sstevel@tonic-gate     int nleft;     /* The amount of space remaining in cq->buffers.tail */
1797c478bd9Sstevel@tonic-gate     int nnew;      /* The number of characters to append to cq->buffers.tail */
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * Compute the offset at which the next character should be written
1827c478bd9Sstevel@tonic-gate  * into the tail buffer segment.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate     int boff = cq->ntotal % GL_CQ_SIZE;
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * Since we don't allocate a new buffer until we have at least one
1877c478bd9Sstevel@tonic-gate  * character to write into it, if boff is 0 at this point, it means
1887c478bd9Sstevel@tonic-gate  * that we hit the end of the tail buffer segment on the last append,
1897c478bd9Sstevel@tonic-gate  * so we need to allocate a new one.
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  * If allocating this new node will require a call to malloc(), as
1927c478bd9Sstevel@tonic-gate  * opposed to using a currently unused node in the freelist, first try
1937c478bd9Sstevel@tonic-gate  * flushing the current contents of the buffer to the terminal. When
1947c478bd9Sstevel@tonic-gate  * write_fn() uses blocking I/O, this stops the buffer size ever getting
1957c478bd9Sstevel@tonic-gate  * bigger than a single buffer node. When it is non-blocking, it helps
1967c478bd9Sstevel@tonic-gate  * to keep the amount of memory, but it isn't gauranteed to do so.
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate     if(boff == 0 && _idle_FreeListNodes(cq->bufmem) == 0) {
1997c478bd9Sstevel@tonic-gate       switch(_glq_flush_queue(cq, write_fn, data)) {
2007c478bd9Sstevel@tonic-gate       case GLQ_FLUSH_DONE:
2017c478bd9Sstevel@tonic-gate 	break;
2027c478bd9Sstevel@tonic-gate       case GLQ_FLUSH_AGAIN:
2037c478bd9Sstevel@tonic-gate 	errno = 0;          /* Don't confuse the caller */
2047c478bd9Sstevel@tonic-gate 	break;
2057c478bd9Sstevel@tonic-gate       default:
2067c478bd9Sstevel@tonic-gate 	return ndone;       /* Error */
2077c478bd9Sstevel@tonic-gate       };
2087c478bd9Sstevel@tonic-gate       boff = cq->ntotal % GL_CQ_SIZE;
2097c478bd9Sstevel@tonic-gate     };
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * Since we don't allocate a new buffer until we have at least one
2127c478bd9Sstevel@tonic-gate  * character to write into it, if boff is 0 at this point, it means
2137c478bd9Sstevel@tonic-gate  * that we hit the end of the tail buffer segment on the last append,
2147c478bd9Sstevel@tonic-gate  * so we need to allocate a new one.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate     if(boff == 0) {
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * Allocate the new node.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate       CqCharBuff *node = (CqCharBuff *) _new_FreeListNode(cq->bufmem);
2217c478bd9Sstevel@tonic-gate       if(!node) {
2227c478bd9Sstevel@tonic-gate 	_err_record_msg(cq->err, "Insufficient memory to buffer output.",
2237c478bd9Sstevel@tonic-gate 			END_ERR_MSG);
2247c478bd9Sstevel@tonic-gate 	return ndone;
2257c478bd9Sstevel@tonic-gate       };
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * Initialize the node.
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate       node->next = NULL;
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * Append the new node to the tail of the list.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate       if(cq->buffers.tail)
2347c478bd9Sstevel@tonic-gate 	cq->buffers.tail->next = node;
2357c478bd9Sstevel@tonic-gate       else
2367c478bd9Sstevel@tonic-gate 	cq->buffers.head = node;
2377c478bd9Sstevel@tonic-gate       cq->buffers.tail = node;
2387c478bd9Sstevel@tonic-gate     };
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * How much room is there for new characters in the current tail node?
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate     nleft = GL_CQ_SIZE - boff;
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * How many characters remain to be appended?
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate     ntodo = n - ndone;
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * How many characters should we append to the current tail node?
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate     nnew = nleft < ntodo ? nleft : ntodo;
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * Append the latest prefix of nnew characters.
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate     memcpy(cq->buffers.tail->bytes + boff, chars + ndone, nnew);
2557c478bd9Sstevel@tonic-gate     cq->ntotal += nnew;
2567c478bd9Sstevel@tonic-gate     ndone += nnew;
2577c478bd9Sstevel@tonic-gate   };
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Return the count of the number of characters successfully appended.
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate   return ndone;
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate /*.......................................................................
2657c478bd9Sstevel@tonic-gate  * Discard the contents of a queue of characters.
2667c478bd9Sstevel@tonic-gate  *
2677c478bd9Sstevel@tonic-gate  * Input:
2687c478bd9Sstevel@tonic-gate  *  cq    GlCharQueue *  The queue to clear.
2697c478bd9Sstevel@tonic-gate  */
_glq_empty_queue(GlCharQueue * cq)2707c478bd9Sstevel@tonic-gate void _glq_empty_queue(GlCharQueue *cq)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate   if(cq) {
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * Return all list nodes to their respective free-lists.
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate     _rst_FreeList(cq->bufmem);
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * Mark the lists as empty.
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate     cq->buffers.head = cq->buffers.tail = NULL;
2817c478bd9Sstevel@tonic-gate     cq->nflush = cq->ntotal = 0;
2827c478bd9Sstevel@tonic-gate   };
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*.......................................................................
2867c478bd9Sstevel@tonic-gate  * Return a count of the number of characters currently in the queue.
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  * Input:
2897c478bd9Sstevel@tonic-gate  *  cq    GlCharQueue *  The queue of interest.
2907c478bd9Sstevel@tonic-gate  * Output:
2917c478bd9Sstevel@tonic-gate  *  return        int    The number of characters in the queue.
2927c478bd9Sstevel@tonic-gate  */
_glq_char_count(GlCharQueue * cq)2937c478bd9Sstevel@tonic-gate int _glq_char_count(GlCharQueue *cq)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate   return (cq && cq->buffers.head) ? (cq->ntotal - cq->nflush) : 0;
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate /*.......................................................................
2997c478bd9Sstevel@tonic-gate  * Write as many characters as possible from the start of a character
3007c478bd9Sstevel@tonic-gate  * queue via a given output callback function, removing those written
3017c478bd9Sstevel@tonic-gate  * from the queue.
3027c478bd9Sstevel@tonic-gate  *
3037c478bd9Sstevel@tonic-gate  * Input:
3047c478bd9Sstevel@tonic-gate  *  cq        GlCharQueue *  The queue to write characters from.
3057c478bd9Sstevel@tonic-gate  *  write_fn  GL_WRITE_FN *  The function to call to output characters,
3067c478bd9Sstevel@tonic-gate  *                           or 0 to simply discard the contents of the
3077c478bd9Sstevel@tonic-gate  *                           queue.
3087c478bd9Sstevel@tonic-gate  *  data             void *  Anonymous data to pass to write_fn().
3097c478bd9Sstevel@tonic-gate  * Output:
3107c478bd9Sstevel@tonic-gate  *  return   GlFlushState    The status of the flush operation:
3117c478bd9Sstevel@tonic-gate  *                             GLQ_FLUSH_DONE  -  The flush operation
3127c478bd9Sstevel@tonic-gate  *                                                completed successfully.
3137c478bd9Sstevel@tonic-gate  *                             GLQ_FLUSH_AGAIN -  The flush operation
3147c478bd9Sstevel@tonic-gate  *                                                couldn't be completed
3157c478bd9Sstevel@tonic-gate  *                                                on this call. Call this
3167c478bd9Sstevel@tonic-gate  *                                                function again when the
3177c478bd9Sstevel@tonic-gate  *                                                output channel can accept
3187c478bd9Sstevel@tonic-gate  *                                                further output.
3197c478bd9Sstevel@tonic-gate  *                             GLQ_FLUSH_ERROR    Unrecoverable error.
3207c478bd9Sstevel@tonic-gate  */
_glq_flush_queue(GlCharQueue * cq,GlWriteFn * write_fn,void * data)3217c478bd9Sstevel@tonic-gate GlqFlushState _glq_flush_queue(GlCharQueue *cq, GlWriteFn *write_fn,
3227c478bd9Sstevel@tonic-gate 			       void *data)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * Check the arguments.
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate   if(!cq) {
3287c478bd9Sstevel@tonic-gate     errno = EINVAL;
3297c478bd9Sstevel@tonic-gate     return GLQ_FLUSH_ERROR;
3307c478bd9Sstevel@tonic-gate   };
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate  * If possible keep writing until all of the chained buffers have been
3337c478bd9Sstevel@tonic-gate  * emptied and removed from the list.
3347c478bd9Sstevel@tonic-gate  */
3357c478bd9Sstevel@tonic-gate   while(cq->buffers.head) {
3367c478bd9Sstevel@tonic-gate /*
3377c478bd9Sstevel@tonic-gate  * Are we looking at the only node in the list?
3387c478bd9Sstevel@tonic-gate  */
3397c478bd9Sstevel@tonic-gate     int is_tail = cq->buffers.head == cq->buffers.tail;
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * How many characters more than an exact multiple of the buffer-segment
3427c478bd9Sstevel@tonic-gate  * size have been added to the buffer so far?
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate     int nmodulo = cq->ntotal % GL_CQ_SIZE;
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * How many characters of the buffer segment at the head of the list
3477c478bd9Sstevel@tonic-gate  * have been used? Note that this includes any characters that have
3487c478bd9Sstevel@tonic-gate  * already been flushed. Also note that if nmodulo==0, this means that
3497c478bd9Sstevel@tonic-gate  * the tail buffer segment is full. The reason for this is that we
3507c478bd9Sstevel@tonic-gate  * don't allocate new tail buffer segments until there is at least one
3517c478bd9Sstevel@tonic-gate  * character to be added to them.
3527c478bd9Sstevel@tonic-gate  */
3537c478bd9Sstevel@tonic-gate     int nhead = (!is_tail || nmodulo == 0) ? GL_CQ_SIZE : nmodulo;
3547c478bd9Sstevel@tonic-gate /*
3557c478bd9Sstevel@tonic-gate  * How many characters remain to be flushed from the buffer
3567c478bd9Sstevel@tonic-gate  * at the head of the list?
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate     int nbuff = nhead - (cq->nflush % GL_CQ_SIZE);
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  * Attempt to write this number.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate     int nnew = write_fn(data, cq->buffers.head->bytes +
3637c478bd9Sstevel@tonic-gate 			cq->nflush % GL_CQ_SIZE, nbuff);
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * Was anything written?
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate     if(nnew > 0) {
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * Increment the count of the number of characters that have
3707c478bd9Sstevel@tonic-gate  * been flushed from the head of the queue.
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate       cq->nflush += nnew;
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate  * If we succeded in writing all of the contents of the current
3757c478bd9Sstevel@tonic-gate  * buffer segment, remove it from the queue.
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate       if(nnew == nbuff) {
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate  * If we just emptied the last node left in the list, then the queue is
3807c478bd9Sstevel@tonic-gate  * now empty and should be reset.
3817c478bd9Sstevel@tonic-gate  */
3827c478bd9Sstevel@tonic-gate 	if(is_tail) {
3837c478bd9Sstevel@tonic-gate 	  _glq_empty_queue(cq);
3847c478bd9Sstevel@tonic-gate 	} else {
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate  * Get the node to be removed from the head of the list.
3877c478bd9Sstevel@tonic-gate  */
3887c478bd9Sstevel@tonic-gate 	  CqCharBuff *node = cq->buffers.head;
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * Make the node that follows it the new head of the queue.
3917c478bd9Sstevel@tonic-gate  */
3927c478bd9Sstevel@tonic-gate 	  cq->buffers.head = node->next;
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * Return it to the freelist.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate 	  node = (CqCharBuff *) _del_FreeListNode(cq->bufmem, node);
3977c478bd9Sstevel@tonic-gate 	};
3987c478bd9Sstevel@tonic-gate       };
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate  * If the write blocked, request that this function be called again
4017c478bd9Sstevel@tonic-gate  * when space to write next becomes available.
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate     } else if(nnew==0) {
4047c478bd9Sstevel@tonic-gate       return GLQ_FLUSH_AGAIN;
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * I/O error.
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate     } else {
4097c478bd9Sstevel@tonic-gate       _err_record_msg(cq->err, "Error writing to terminal", END_ERR_MSG);
4107c478bd9Sstevel@tonic-gate       return GLQ_FLUSH_ERROR;
4117c478bd9Sstevel@tonic-gate     };
4127c478bd9Sstevel@tonic-gate   };
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate  * To get here the queue must now be empty.
4157c478bd9Sstevel@tonic-gate  */
4167c478bd9Sstevel@tonic-gate   return GLQ_FLUSH_DONE;
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*.......................................................................
4207c478bd9Sstevel@tonic-gate  * Return extra information (ie. in addition to that provided by errno)
4217c478bd9Sstevel@tonic-gate  * about the last error to occur in any of the public functions of this
4227c478bd9Sstevel@tonic-gate  * module.
4237c478bd9Sstevel@tonic-gate  *
4247c478bd9Sstevel@tonic-gate  * Input:
4257c478bd9Sstevel@tonic-gate  *  cq     GlCharQueue *  The container of the history list.
4267c478bd9Sstevel@tonic-gate  * Output:
4277c478bd9Sstevel@tonic-gate  *  return  const char *  A pointer to the internal buffer in which
4287c478bd9Sstevel@tonic-gate  *                        the error message is temporarily stored.
4297c478bd9Sstevel@tonic-gate  */
_glq_last_error(GlCharQueue * cq)4307c478bd9Sstevel@tonic-gate const char *_glq_last_error(GlCharQueue *cq)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate   return cq ? _err_get_msg(cq->err) : "NULL GlCharQueue argument";
4337c478bd9Sstevel@tonic-gate }
434