11058Sant/****************************************************************************
22362Sohair * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
31058Sant *                                                                          *
41058Sant * Permission is hereby granted, free of charge, to any person obtaining a  *
51058Sant * copy of this software and associated documentation files (the            *
61058Sant * "Software"), to deal in the Software without restriction, including      *
71058Sant * without limitation the rights to use, copy, modify, merge, publish,      *
81058Sant * distribute, distribute with modifications, sublicense, and/or sell       *
91058Sant * copies of the Software, and to permit persons to whom the Software is    *
101058Sant * furnished to do so, subject to the following conditions:                 *
111058Sant *                                                                          *
121058Sant * The above copyright notice and this permission notice shall be included  *
131058Sant * in all copies or substantial portions of the Software.                   *
141058Sant *                                                                          *
151058Sant * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
161058Sant * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
171058Sant * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
181058Sant * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
192362Sohair * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
202362Sohair * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
212362Sohair * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
221058Sant *                                                                          *
231058Sant * Except as contained in this notice, the name(s) of the above copyright   *
241058Sant * holders shall not be used in advertising or otherwise to promote the     *
251058Sant * sale, use or other dealings in this Software without prior written       *
2615235Sgoetz * authorization.                                                           *
271058Sant ****************************************************************************/
281058Sant
291058Sant/****************************************************************************
301058Sant *  Author: Thomas E. Dickey                 1997-on                        *
311058Sant ****************************************************************************/
321058Sant/*
331058Sant *	trace_buf.c - Tracing/Debugging buffers (attributes)
341058Sant */
351058Sant
361058Sant#include <curses.priv.h>
371058Sant
381058SantMODULE_ID("$Id: trace_buf.c,v 1.14 2008/08/03 15:13:56 tom Exp $")
391058Sant
401058Sant#define MyList _nc_globals.tracebuf_ptr
411058Sant#define MySize _nc_globals.tracebuf_used
421058Sant
431058Santstatic char *
441058Sant_nc_trace_alloc(int bufnum, size_t want)
451058Sant{
461058Sant    char *result = 0;
471058Sant
481058Sant    if (bufnum >= 0) {
491058Sant	if ((size_t) (bufnum + 1) > MySize) {
501058Sant	    size_t need = (bufnum + 1) * 2;
511058Sant	    if ((MyList = typeRealloc(TRACEBUF, need, MyList)) != 0) {
521058Sant		while (need > MySize)
531058Sant		    MyList[MySize++].text = 0;
541058Sant	    }
551058Sant	}
561058Sant
571058Sant	if (MyList != 0) {
581058Sant	    if (MyList[bufnum].text == 0
591058Sant		|| want > MyList[bufnum].size) {
601058Sant		MyList[bufnum].text = typeRealloc(char, want, MyList[bufnum].text);
611058Sant		if (MyList[bufnum].text != 0)
621058Sant		    MyList[bufnum].size = want;
631058Sant	    }
641058Sant	    result = MyList[bufnum].text;
651058Sant	}
661058Sant    }
671058Sant#if NO_LEAKS
681058Sant    else {
691058Sant	if (MySize) {
701058Sant	    if (MyList) {
711058Sant		while (MySize--) {
721058Sant		    if (MyList[MySize].text != 0) {
731058Sant			free(MyList[MySize].text);
741058Sant		    }
751058Sant		}
761058Sant		free(MyList);
771058Sant		MyList = 0;
781058Sant	    }
791058Sant	    MySize = 0;
801058Sant	}
811058Sant    }
821058Sant#endif
831058Sant    return result;
841058Sant}
851058Sant
861058Sant/*
871058Sant * (re)Allocate a buffer big enough for the caller's wants.
881058Sant */
891058SantNCURSES_EXPORT(char *)
901058Sant_nc_trace_buf(int bufnum, size_t want)
911058Sant{
921058Sant    char *result = _nc_trace_alloc(bufnum, want);
931058Sant    if (result != 0)
941058Sant	*result = '\0';
951058Sant    return result;
961058Sant}
97
98/*
99 * Append a new string to an existing buffer.
100 */
101NCURSES_EXPORT(char *)
102_nc_trace_bufcat(int bufnum, const char *value)
103{
104    char *buffer = _nc_trace_alloc(bufnum, 0);
105    if (buffer != 0) {
106	size_t have = strlen(buffer);
107
108	buffer = _nc_trace_alloc(bufnum, 1 + have + strlen(value));
109	if (buffer != 0)
110	    (void) strcpy(buffer + have, value);
111
112    }
113    return buffer;
114}
115