Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2020,2023 Thomas E. Dickey * |
| 3 | * Copyright 1998-2011,2012 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 39 | MODULE_ID("$Id: trace_buf.c,v 1.22 2023/06/24 13:37:25 tom Exp $") |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 40 | |
| 41 | #ifdef TRACE |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 42 | |
| 43 | #define MyList _nc_globals.tracebuf_ptr |
| 44 | #define MySize _nc_globals.tracebuf_used |
| 45 | |
| 46 | static 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 Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 53 | size_t need = (size_t) (bufnum + 1) * 2; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 *) |
| 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 *) |
| 105 | _nc_trace_bufcat(int bufnum, const char *value) |
| 106 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 107 | char *buffer; |
| 108 | |
| 109 | if (value == NULL) |
| 110 | value = ""; |
| 111 | buffer = _nc_trace_alloc(bufnum, (size_t) 0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 112 | if (buffer != 0) { |
| 113 | size_t have = strlen(buffer); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 114 | size_t need = strlen(value) + have; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 115 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 116 | buffer = _nc_trace_alloc(bufnum, 1 + need); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 117 | if (buffer != 0) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 118 | _nc_STRCPY(buffer + have, value, need); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 119 | |
| 120 | } |
| 121 | return buffer; |
| 122 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 123 | #else |
| 124 | EMPTY_MODULE(_nc_empty_trace_buf) |
| 125 | #endif /* TRACE */ |