msgs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * COMPONENT_NAME: austext
  25. *
  26. * FUNCTIONS: DtSearchAddMessage
  27. * DtSearchFreeMessages
  28. * DtSearchGetMessages
  29. * DtSearchHasMessages
  30. *
  31. * ORIGINS: 27
  32. *
  33. *
  34. * (C) COPYRIGHT International Business Machines Corp. 1995
  35. * All Rights Reserved
  36. * Licensed Materials - Property of IBM
  37. * US Government Users Restricted Rights - Use, duplication or
  38. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  39. */
  40. /********************** MSGS.C *****************************
  41. * $XConsortium: msgs.c /main/4 1996/05/07 13:40:27 drk $
  42. * August 1995.
  43. * Handles access to global ausapi_msglist for DtSearch.
  44. *
  45. * $Log$
  46. * Revision 2.2 1995/10/26 14:35:04 miker
  47. * Added prolog.
  48. *
  49. * Revision 2.1 1995/09/22 21:19:23 miker
  50. * Freeze DtSearch 0.1, AusText 2.1.8
  51. */
  52. #include "SearchP.h"
  53. #include <stdlib.h>
  54. #define PROGNAME "MSGS"
  55. /****************************************/
  56. /* */
  57. /* DtSearchHasMessages */
  58. /* */
  59. /****************************************/
  60. int DtSearchHasMessages (void)
  61. { return (ausapi_msglist != NULL); }
  62. /****************************************/
  63. /* */
  64. /* DtSearchFreeMessages */
  65. /* */
  66. /****************************************/
  67. void DtSearchFreeMessages (void)
  68. { free_llist (&ausapi_msglist); }
  69. /****************************************/
  70. /* */
  71. /* DtSearchAddMessage */
  72. /* */
  73. /****************************************/
  74. /* Mallocs space for new message and appends copy of passed msg
  75. * to end of global msglist. This function allocates memory
  76. * for the messages--THE CALLER MUST FREE THE MESSAGES,
  77. * preferably using DtSearchFreeMessages() (free_llist()).
  78. * DtSearchAddMessage() was formerly called append_msglist() in msgutil.c.
  79. */
  80. void DtSearchAddMessage (char *msg)
  81. {
  82. LLIST *new;
  83. LLIST **pp;
  84. new = austext_malloc (strlen(msg) + sizeof(LLIST) + 2,
  85. PROGNAME"47", NULL);
  86. new->link = NULL;
  87. new->data = new + 1; /* hop over exactly 1 LLIST structure */
  88. strcpy (new->data, msg);
  89. for ( pp = &ausapi_msglist; *pp != NULL; pp = &((*pp)->link) ) ;
  90. *pp = new;
  91. return;
  92. } /* DtSearchAddMessage() */
  93. /****************************************/
  94. /* */
  95. /* DtSearchGetMessages */
  96. /* */
  97. /****************************************/
  98. /* July 1994,
  99. * DtSearchGetMessages was formerly flatmessages().
  100. * Copies all msgs in ausapi_msglist into single, flat text buffer.
  101. */
  102. char *DtSearchGetMessages (void)
  103. {
  104. char *targ, *src;
  105. size_t totlen = 0L;
  106. LLIST *llptr;
  107. static char *flatbuf = NULL;
  108. static size_t flatbufsz = 0L;
  109. /* Since function is often used as an arg in printf,
  110. * be sure to return something safe when there are no msgs.
  111. */
  112. if (ausapi_msglist == NULL)
  113. return "";
  114. /* First Pass: Get the total length.
  115. * including room for inserted \n's.
  116. */
  117. for (llptr = ausapi_msglist; llptr != NULL; llptr = llptr->link)
  118. totlen += strlen(llptr->data) + 2;
  119. if (totlen > flatbufsz) {
  120. if (flatbuf)
  121. free (flatbuf);
  122. flatbuf = austext_malloc (totlen + 4, PROGNAME"73", NULL);
  123. flatbufsz = totlen;
  124. }
  125. /* Second Pass: Copy the messages into the flat buffer.
  126. * Make sure there are no less than 2 \n's at end of each msg--
  127. * one to terminate msg (it may already be in the original
  128. * msg string) and an added one to separate from the next msg.
  129. */
  130. targ = flatbuf;
  131. for (llptr = ausapi_msglist; llptr != NULL; llptr = llptr->link) {
  132. src = (char *) llptr->data;
  133. while (*src != 0)
  134. *targ++ = *src++;
  135. *targ++ = '\n';
  136. if (*(--src) != '\n')
  137. *targ++ = '\n';
  138. }
  139. /* Overlay the last two \n so the whole string won't end.
  140. * This prevents ugly spacing problems when function
  141. * is used directly in information dialog boxes. It also
  142. * means regular writes to stdout etc will usually have to
  143. * insert their own final \n.
  144. */
  145. targ[-2] = 0;
  146. return flatbuf;
  147. } /* DtSearchGetMessages() */
  148. /********************** MSGS.C *****************************/