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