Ejemplo n.º 1
0
winmsg_esc_ex(WinNames, const bool hide_cur, Window *win)
{
	Window *oldfore = 0;
	size_t max = wmbc_bytesleft(wmbc);

	if (display) {
		oldfore = D_fore;
		D_fore = win;
	}

	/* TODO: no need to enforce a limit here */
	AddWindows(wmbc, max - 1,
		hide_cur
			| (esc->flags.lng ? 0 : 2)
			| (esc->flags.plus ? 4 : 0)
			| (esc->flags.minus ? 8 : 0),
		win ? win->w_number : -1);

	if (display)
		D_fore = oldfore;

	if (*wmbc->p)
		wmc_set(cond);

	wmbc_fastfw0(wmbc);
}
Ejemplo n.º 2
0
/* Write data to the buffer using a printf-style format string. If needed, the
 * buffer will be automatically expanded to accomodate the resulting string and
 * is therefore protected against overflows. */
int wmbc_printf(WinMsgBufContext *wmbc, const char *fmt, ...)
{
	va_list ap;
	size_t  n, max;

	/* to prevent buffer overflows, cap the number of bytes to the remaining
	 * buffer size */
	va_start(ap, fmt);
	max = wmbc_bytesleft(wmbc);
	n = vsnprintf(wmbc->p, max, fmt, ap);
	va_end(ap);

	/* more space is needed if vsnprintf returns a larger number than our max,
	 * in which case we should accomodate by dynamically resizing the buffer and
	 * trying again */
	if (n > max) {
		if (!_wmbc_expand(wmbc, wmb_size(wmbc->buf) + n - max)) {
			/* failed to allocate additional memory; this will simply have to do */
			wmbc_fastfw_end(wmbc);
			va_end(ap);
			return max;
		}

		va_start(ap, fmt);
		size_t m = vsnprintf(wmbc->p, n + 1, fmt, ap);
		assert(m == n); /* this should never fail */
		va_end(ap);
	}

	wmbc_fastfw0(wmbc);
	return n;
}
Ejemplo n.º 3
0
winmsg_esc_ex(Wflags, Window *win)
{
	*wmbc->p = '\0';

	if (win)
		AddWindowFlags(wmbc->p, wmbc_bytesleft(wmbc), win);

	if (*wmbc->p)
		wmc_set(cond);

	wmbc_fastfw0(wmbc);
}
Ejemplo n.º 4
0
/* Sets a character at the current buffer position and increments the pointer.
 * The terminating null character is not retained. The buffer will be
 * dynamically resized as needed. */
void wmbc_putchar(WinMsgBufContext *wmbc, char c)
{
	/* attempt to accomodate this character, but bail out silenty if it cannot
	 * fit */
	if (!wmbc_bytesleft(wmbc)) {
		if (!_wmbc_expand(wmbc, wmbc->buf->size + 1)) {
			return;
		}
	}

	*wmbc->p++ = c;
}
Ejemplo n.º 5
0
/* Write a terminating null byte to the buffer and return a pointer to the
 * buffer contents. This should not be used to modify the buffer. If buffer is
 * full and expansion fails, then the last byte in the buffer will be replaced
 * with the null byte. */
const char *wmbc_finish(WinMsgBufContext *wmbc)
{
	if (!wmbc_bytesleft(wmbc)) {
		size_t size = wmbc->buf->size + 1;
		if (wmb_expand(wmbc->buf, size) < size) {
			/* we must terminate the string or we may cause big problems for the
			 * caller; overwrite the last char :x */
			wmbc->p--;
		}
	}

	*wmbc->p = '\0';
	return wmb_contents(wmbc->buf);
}
Ejemplo n.º 6
0
/* Copies a string into the buffer, dynamically resizing the buffer as needed to
 * accomodate length N. If S is shorter than N characters in length, the
 * remaining bytes are filled will nulls. The context pointer is adjusted to the
 * terminating null byte. A pointer to the first copied character in the buffer
 * is returned; it shall not be used to modify the buffer. */
const char *wmbc_strncpy(WinMsgBufContext *wmbc, const char *s, size_t n)
{
	size_t l = wmbc_bytesleft(wmbc);

	/* silently fail in the event that we cannot accomodate */
	if (l < n) {
		size_t size = wmbc->buf->size + (n - l);
		if (!_wmbc_expand(wmbc, size)) {
			/* TODO: we should copy what can fit. */
			return NULL;
		}
	}

	char *p = wmbc->p;
	strncpy(wmbc->p, s, n);
	wmbc->p += n;
	return p;
}