1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2011,2012 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Thomas E. Dickey                 1997-on                        *
32  ****************************************************************************/
33 /*
34  *	trace_buf.c - Tracing/Debugging buffers (attributes)
35  */
36 
37 #include <curses.priv.h>
38 
39 MODULE_ID("$Id: trace_buf.c,v 1.21 2020/02/02 23:34:34 tom Exp $")
40 
41 #ifdef TRACE
42 
43 #define MyList _nc_globals.tracebuf_ptr
44 #define MySize _nc_globals.tracebuf_used
45 
46 static char *
_nc_trace_alloc(int bufnum,size_t want)47 _nc_trace_alloc(int bufnum, size_t want)
48 {
49     char *result = 0;
50 
51     if (bufnum >= 0) {
52 	if ((size_t) (bufnum + 1) > MySize) {
53 	    size_t need = (size_t) (bufnum + 1) * 2;
54 	    if ((MyList = typeRealloc(TRACEBUF, need, MyList)) != 0) {
55 		while (need > MySize)
56 		    MyList[MySize++].text = 0;
57 	    }
58 	}
59 
60 	if (MyList != 0) {
61 	    if (MyList[bufnum].text == 0
62 		|| want > MyList[bufnum].size) {
63 		MyList[bufnum].text = typeRealloc(char, want, MyList[bufnum].text);
64 		if (MyList[bufnum].text != 0)
65 		    MyList[bufnum].size = want;
66 	    }
67 	    result = MyList[bufnum].text;
68 	}
69     }
70 #if NO_LEAKS
71     else {
72 	if (MySize) {
73 	    if (MyList) {
74 		while (MySize--) {
75 		    if (MyList[MySize].text != 0) {
76 			free(MyList[MySize].text);
77 		    }
78 		}
79 		free(MyList);
80 		MyList = 0;
81 	    }
82 	    MySize = 0;
83 	}
84     }
85 #endif
86     return result;
87 }
88 
89 /*
90  * (re)Allocate a buffer big enough for the caller's wants.
91  */
92 NCURSES_EXPORT(char *)
_nc_trace_buf(int bufnum,size_t want)93 _nc_trace_buf(int bufnum, size_t want)
94 {
95     char *result = _nc_trace_alloc(bufnum, want);
96     if (result != 0)
97 	*result = '\0';
98     return result;
99 }
100 
101 /*
102  * Append a new string to an existing buffer.
103  */
104 NCURSES_EXPORT(char *)
_nc_trace_bufcat(int bufnum,const char * value)105 _nc_trace_bufcat(int bufnum, const char *value)
106 {
107     char *buffer = _nc_trace_alloc(bufnum, (size_t) 0);
108     if (buffer != 0) {
109 	size_t have = strlen(buffer);
110 	size_t need = strlen(value) + have;
111 
112 	buffer = _nc_trace_alloc(bufnum, 1 + need);
113 	if (buffer != 0)
114 	    _nc_STRCPY(buffer + have, value, need);
115 
116     }
117     return buffer;
118 }
119 #else
120 EMPTY_MODULE(_nc_empty_trace_buf)
121 #endif /* TRACE */
122