Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSearch / msgs.c
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 librararies 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 #define PROGNAME        "MSGS"
54
55 /****************************************/
56 /*                                      */
57 /*          DtSearchHasMessages         */
58 /*                                      */
59 /****************************************/
60 int     DtSearchHasMessages (void)
61 { return (ausapi_msglist != NULL); }
62
63
64 /****************************************/
65 /*                                      */
66 /*          DtSearchFreeMessages        */
67 /*                                      */
68 /****************************************/
69 void    DtSearchFreeMessages (void)
70 { free_llist (&ausapi_msglist); }
71
72
73 /****************************************/
74 /*                                      */
75 /*          DtSearchAddMessage          */
76 /*                                      */
77 /****************************************/
78 /* Mallocs space for new message and appends copy of passed msg
79  * to end of global msglist.  This function allocates memory
80  * for the messages--THE CALLER MUST FREE THE MESSAGES,
81  * preferably using DtSearchFreeMessages() (free_llist()).
82  * DtSearchAddMessage() was formerly called append_msglist() in msgutil.c.
83  */
84 void    DtSearchAddMessage (char *msg)
85 {
86     LLIST       *new;
87     LLIST       **pp;
88     new = austext_malloc (strlen(msg) + sizeof(LLIST) + 2,
89         PROGNAME"47", NULL);
90     new->link = NULL;
91     new->data = new + 1;        /* hop over exactly 1 LLIST structure */
92     strcpy (new->data, msg);
93     for ( pp = &ausapi_msglist;  *pp != NULL;  pp = &((*pp)->link) ) ;
94     *pp = new;
95     return;
96 } /* DtSearchAddMessage() */
97
98
99 /****************************************/
100 /*                                      */
101 /*          DtSearchGetMessages         */
102 /*                                      */
103 /****************************************/
104 /* July 1994,
105  * DtSearchGetMessages was formerly flatmessages().
106  * Copies all msgs in ausapi_msglist into single, flat text buffer.
107  */
108 char    *DtSearchGetMessages (void)
109 {
110     char                *targ, *src;
111     size_t              totlen = 0L;
112     LLIST               *llptr;
113     static char         *flatbuf = NULL;
114     static size_t       flatbufsz = 0L;
115
116     /* Since function is often used as an arg in printf,
117      * be sure to return something safe when there are no msgs.
118      */
119     if (ausapi_msglist == NULL)
120         return "";
121
122     /* First Pass: Get the total length.
123      * including room for inserted \n's.
124      */
125     for (llptr = ausapi_msglist;  llptr != NULL;  llptr = llptr->link)
126         totlen += strlen(llptr->data) + 2;
127     if (totlen > flatbufsz) {
128         if (flatbuf)
129             free (flatbuf);
130         flatbuf = austext_malloc (totlen + 4, PROGNAME"73", NULL);
131         flatbufsz = totlen;
132     }
133
134     /* Second Pass: Copy the messages into the flat buffer.
135      * Make sure there are no less than 2 \n's at end of each msg--
136      * one to terminate msg (it may already be in the original
137      * msg string) and an added one to separate from the next msg.
138      */
139     targ = flatbuf;
140     for (llptr = ausapi_msglist;  llptr != NULL;  llptr = llptr->link) {
141         src = (char *) llptr->data;
142         while (*src != 0)
143             *targ++ = *src++;
144         *targ++ = '\n';
145         if (*(--src) != '\n')
146             *targ++ = '\n';
147     }
148
149     /* Overlay the last two \n so the whole string won't end.
150      * This prevents ugly spacing problems when function
151      * is used directly in information dialog boxes.  It also
152      * means regular writes to stdout etc will usually have to
153      * insert their own final \n.
154      */
155     targ[-2] = 0;
156     return flatbuf;
157 }  /* DtSearchGetMessages() */
158
159 /********************** MSGS.C *****************************/
160