blob: 91b12e45c248aaab6e10b724965799cc5d5257f7 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020,2023 Thomas E. Dickey *
3 * Copyright 1998-2011,2012 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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
micky3879b9f5e72025-07-08 18:04:53 -040039MODULE_ID("$Id: trace_buf.c,v 1.22 2023/06/24 13:37:25 tom Exp $")
Steve Kondikae271bc2015-11-15 02:50:53 +010040
41#ifdef TRACE
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053042
43#define MyList _nc_globals.tracebuf_ptr
44#define MySize _nc_globals.tracebuf_used
45
46static char *
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) {
Steve Kondikae271bc2015-11-15 02:50:53 +010053 size_t need = (size_t) (bufnum + 1) * 2;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053054 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 */
92NCURSES_EXPORT(char *)
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 */
104NCURSES_EXPORT(char *)
105_nc_trace_bufcat(int bufnum, const char *value)
106{
micky3879b9f5e72025-07-08 18:04:53 -0400107 char *buffer;
108
109 if (value == NULL)
110 value = "";
111 buffer = _nc_trace_alloc(bufnum, (size_t) 0);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530112 if (buffer != 0) {
113 size_t have = strlen(buffer);
Steve Kondikae271bc2015-11-15 02:50:53 +0100114 size_t need = strlen(value) + have;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530115
Steve Kondikae271bc2015-11-15 02:50:53 +0100116 buffer = _nc_trace_alloc(bufnum, 1 + need);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530117 if (buffer != 0)
Steve Kondikae271bc2015-11-15 02:50:53 +0100118 _nc_STRCPY(buffer + have, value, need);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530119
120 }
121 return buffer;
122}
Steve Kondikae271bc2015-11-15 02:50:53 +0100123#else
124EMPTY_MODULE(_nc_empty_trace_buf)
125#endif /* TRACE */