Annotation of src/usr.bin/tmux/util.c, Revision 1.1
1.1 ! nicm 1: /* $OpenBSD$ */
! 2:
! 3: /*
! 4: * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
! 5: *
! 6: * Permission to use, copy, modify, and distribute this software for any
! 7: * purpose with or without fee is hereby granted, provided that the above
! 8: * copyright notice and this permission notice appear in all copies.
! 9: *
! 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
! 15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
! 16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 17: */
! 18:
! 19: #include <sys/types.h>
! 20:
! 21: #include "tmux.h"
! 22:
! 23: /* Return a section of a string around a point. */
! 24: char *
! 25: section_string(char *buf, size_t len, size_t sectoff, size_t sectlen)
! 26: {
! 27: char *s;
! 28: size_t first, last;
! 29:
! 30: if (len <= sectlen) {
! 31: first = 0;
! 32: last = len;
! 33: } else if (sectoff < sectlen / 2) {
! 34: first = 0;
! 35: last = sectlen;
! 36: } else if (sectoff + sectlen / 2 > len) {
! 37: last = len;
! 38: first = last - sectlen;
! 39: } else {
! 40: first = sectoff - sectlen / 2;
! 41: last = first + sectlen;
! 42: }
! 43:
! 44: if (last - first > 3 && first != 0)
! 45: first += 3;
! 46: if (last - first > 3 && last != len)
! 47: last -= 3;
! 48:
! 49: xasprintf(&s, "%s%.*s%s", first == 0 ? "" : "...",
! 50: (int) (last - first), buf + first, last == len ? "" : "...");
! 51: return (s);
! 52: }
! 53:
! 54: /* Clean string of invisible characters. */
! 55: void
! 56: clean_string(const char *in, char *buf, size_t len)
! 57: {
! 58: const u_char *cp;
! 59: size_t off;
! 60:
! 61: off = 0;
! 62: for (cp = in; *cp != '\0'; cp++) {
! 63: if (off >= len)
! 64: break;
! 65: if (*cp >= 0x20 && *cp <= 0x7f)
! 66: buf[off++] = *cp;
! 67: else
! 68: off += xsnprintf(buf + off, len - off, "\\%03hho", *cp);
! 69: }
! 70: if (off < len)
! 71: buf[off] = '\0';
! 72: }