[BACK]Return to paste.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/paste.c, Revision 1.36

1.36    ! nicm        1: /* $OpenBSD: paste.c,v 1.35 2016/10/05 12:34:05 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.34      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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: #include <sys/time.h>
                     21:
1.12      nicm       22: #include <stdlib.h>
1.1       nicm       23: #include <string.h>
1.9       nicm       24: #include <vis.h>
1.1       nicm       25:
                     26: #include "tmux.h"
                     27:
1.7       nicm       28: /*
1.19      nicm       29:  * Set of paste buffers. Note that paste buffer data is not necessarily a C
1.7       nicm       30:  * string!
                     31:  */
1.1       nicm       32:
1.28      nicm       33: struct paste_buffer {
                     34:        char            *data;
                     35:        size_t           size;
                     36:
                     37:        char            *name;
1.35      nicm       38:        time_t           created;
1.28      nicm       39:        int              automatic;
                     40:        u_int            order;
                     41:
                     42:        RB_ENTRY(paste_buffer) name_entry;
                     43:        RB_ENTRY(paste_buffer) time_entry;
                     44: };
                     45:
1.36    ! nicm       46: static u_int   paste_next_index;
        !            47: static u_int   paste_next_order;
        !            48: static u_int   paste_num_automatic;
1.19      nicm       49: RB_HEAD(paste_name_tree, paste_buffer) paste_by_name;
                     50: RB_HEAD(paste_time_tree, paste_buffer) paste_by_time;
                     51:
1.36    ! nicm       52: static int     paste_cmp_names(const struct paste_buffer *,
        !            53:                    const struct paste_buffer *);
        !            54: RB_GENERATE_STATIC(paste_name_tree, paste_buffer, name_entry, paste_cmp_names);
        !            55:
        !            56: static int     paste_cmp_times(const struct paste_buffer *,
        !            57:                    const struct paste_buffer *);
        !            58: RB_GENERATE_STATIC(paste_time_tree, paste_buffer, time_entry, paste_cmp_times);
1.18      nicm       59:
1.36    ! nicm       60: static int
1.19      nicm       61: paste_cmp_names(const struct paste_buffer *a, const struct paste_buffer *b)
1.1       nicm       62: {
1.19      nicm       63:        return (strcmp(a->name, b->name));
                     64: }
1.1       nicm       65:
1.36    ! nicm       66: static int
1.19      nicm       67: paste_cmp_times(const struct paste_buffer *a, const struct paste_buffer *b)
                     68: {
                     69:        if (a->order > b->order)
                     70:                return (-1);
                     71:        if (a->order < b->order)
                     72:                return (1);
                     73:        return (0);
1.1       nicm       74: }
                     75:
1.28      nicm       76: /* Get paste buffer name. */
                     77: const char *
                     78: paste_buffer_name(struct paste_buffer *pb)
                     79: {
                     80:        return (pb->name);
                     81: }
                     82:
1.35      nicm       83: /* Get paste buffer order. */
                     84: u_int
                     85: paste_buffer_order(struct paste_buffer *pb)
                     86: {
                     87:        return (pb->order);
                     88: }
                     89:
                     90: /* Get paste buffer created. */
                     91: time_t
                     92: paste_buffer_created(struct paste_buffer *pb)
                     93: {
                     94:        return (pb->created);
                     95: }
                     96:
1.28      nicm       97: /* Get paste buffer data. */
                     98: const char *
                     99: paste_buffer_data(struct paste_buffer *pb, size_t *size)
                    100: {
                    101:        if (size != NULL)
                    102:                *size = pb->size;
                    103:        return (pb->data);
                    104: }
                    105:
1.35      nicm      106: /* Walk paste buffers by time. */
1.1       nicm      107: struct paste_buffer *
1.19      nicm      108: paste_walk(struct paste_buffer *pb)
1.1       nicm      109: {
1.19      nicm      110:        if (pb == NULL)
                    111:                return (RB_MIN(paste_time_tree, &paste_by_time));
                    112:        return (RB_NEXT(paste_time_tree, &paste_by_time, pb));
1.1       nicm      113: }
                    114:
1.21      nicm      115: /* Get the most recent automatic buffer. */
1.1       nicm      116: struct paste_buffer *
1.28      nicm      117: paste_get_top(const char **name)
1.1       nicm      118: {
1.19      nicm      119:        struct paste_buffer     *pb;
                    120:
                    121:        pb = RB_MIN(paste_time_tree, &paste_by_time);
                    122:        if (pb == NULL)
1.1       nicm      123:                return (NULL);
1.28      nicm      124:        if (name != NULL)
                    125:                *name = pb->name;
1.19      nicm      126:        return (pb);
1.1       nicm      127: }
                    128:
1.19      nicm      129: /* Get a paste buffer by name. */
                    130: struct paste_buffer *
                    131: paste_get_name(const char *name)
                    132: {
                    133:        struct paste_buffer     pbfind;
1.1       nicm      134:
1.19      nicm      135:        if (name == NULL || *name == '\0')
                    136:                return (NULL);
1.1       nicm      137:
1.23      nicm      138:        pbfind.name = (char *)name;
1.19      nicm      139:        return (RB_FIND(paste_name_tree, &paste_by_name, &pbfind));
1.1       nicm      140: }
                    141:
1.30      nicm      142: /* Free a paste buffer. */
                    143: void
                    144: paste_free(struct paste_buffer *pb)
1.1       nicm      145: {
1.19      nicm      146:        RB_REMOVE(paste_name_tree, &paste_by_name, pb);
                    147:        RB_REMOVE(paste_time_tree, &paste_by_time, pb);
                    148:        if (pb->automatic)
                    149:                paste_num_automatic--;
1.1       nicm      150:
1.12      nicm      151:        free(pb->data);
1.19      nicm      152:        free(pb->name);
1.12      nicm      153:        free(pb);
1.1       nicm      154: }
                    155:
1.8       nicm      156: /*
1.19      nicm      157:  * Add an automatic buffer, freeing the oldest automatic item if at limit. Note
1.7       nicm      158:  * that the caller is responsible for allocating data.
                    159:  */
1.1       nicm      160: void
1.19      nicm      161: paste_add(char *data, size_t size)
1.1       nicm      162: {
1.19      nicm      163:        struct paste_buffer     *pb, *pb1;
                    164:        u_int                    limit;
1.2       nicm      165:
1.7       nicm      166:        if (size == 0)
1.2       nicm      167:                return;
1.1       nicm      168:
1.32      nicm      169:        limit = options_get_number(global_options, "buffer-limit");
1.19      nicm      170:        RB_FOREACH_REVERSE_SAFE(pb, paste_time_tree, &paste_by_time, pb1) {
                    171:                if (paste_num_automatic < limit)
                    172:                        break;
                    173:                if (pb->automatic)
1.30      nicm      174:                        paste_free(pb);
1.3       nicm      175:        }
1.1       nicm      176:
                    177:        pb = xmalloc(sizeof *pb);
1.19      nicm      178:
                    179:        pb->name = NULL;
                    180:        do {
                    181:                free(pb->name);
                    182:                xasprintf(&pb->name, "buffer%04u", paste_next_index);
                    183:                paste_next_index++;
                    184:        } while (paste_get_name(pb->name) != NULL);
1.1       nicm      185:
                    186:        pb->data = data;
1.4       nicm      187:        pb->size = size;
1.19      nicm      188:
                    189:        pb->automatic = 1;
                    190:        paste_num_automatic++;
                    191:
1.35      nicm      192:        pb->created = time(NULL);
                    193:
1.19      nicm      194:        pb->order = paste_next_order++;
                    195:        RB_INSERT(paste_name_tree, &paste_by_name, pb);
                    196:        RB_INSERT(paste_time_tree, &paste_by_time, pb);
1.1       nicm      197: }
                    198:
1.19      nicm      199: /* Rename a paste buffer. */
                    200: int
                    201: paste_rename(const char *oldname, const char *newname, char **cause)
                    202: {
1.20      nicm      203:        struct paste_buffer     *pb, *pb_new;
1.19      nicm      204:
                    205:        if (cause != NULL)
                    206:                *cause = NULL;
                    207:
                    208:        if (oldname == NULL || *oldname == '\0') {
                    209:                if (cause != NULL)
                    210:                        *cause = xstrdup("no buffer");
                    211:                return (-1);
                    212:        }
                    213:        if (newname == NULL || *newname == '\0') {
                    214:                if (cause != NULL)
                    215:                        *cause = xstrdup("new name is empty");
                    216:                return (-1);
                    217:        }
                    218:
                    219:        pb = paste_get_name(oldname);
                    220:        if (pb == NULL) {
                    221:                if (cause != NULL)
1.20      nicm      222:                        xasprintf(cause, "no buffer %s", oldname);
                    223:                return (-1);
                    224:        }
                    225:
                    226:        pb_new = paste_get_name(newname);
                    227:        if (pb_new != NULL) {
                    228:                if (cause != NULL)
                    229:                        xasprintf(cause, "buffer %s already exists", newname);
1.19      nicm      230:                return (-1);
                    231:        }
                    232:
                    233:        RB_REMOVE(paste_name_tree, &paste_by_name, pb);
                    234:
                    235:        free(pb->name);
                    236:        pb->name = xstrdup(newname);
                    237:
                    238:        if (pb->automatic)
                    239:                paste_num_automatic--;
                    240:        pb->automatic = 0;
                    241:
                    242:        RB_INSERT(paste_name_tree, &paste_by_name, pb);
                    243:
                    244:        return (0);
                    245: }
1.7       nicm      246:
1.8       nicm      247: /*
1.19      nicm      248:  * Add or replace an item in the store. Note that the caller is responsible for
1.7       nicm      249:  * allocating data.
                    250:  */
1.1       nicm      251: int
1.19      nicm      252: paste_set(char *data, size_t size, const char *name, char **cause)
1.1       nicm      253: {
1.30      nicm      254:        struct paste_buffer     *pb, *old;
1.7       nicm      255:
1.19      nicm      256:        if (cause != NULL)
                    257:                *cause = NULL;
                    258:
1.15      nicm      259:        if (size == 0) {
                    260:                free(data);
1.7       nicm      261:                return (0);
1.15      nicm      262:        }
1.19      nicm      263:        if (name == NULL) {
                    264:                paste_add(data, size);
                    265:                return (0);
                    266:        }
1.1       nicm      267:
1.19      nicm      268:        if (*name == '\0') {
                    269:                if (cause != NULL)
                    270:                        *cause = xstrdup("empty buffer name");
1.1       nicm      271:                return (-1);
1.19      nicm      272:        }
                    273:
                    274:        pb = xmalloc(sizeof *pb);
1.1       nicm      275:
1.19      nicm      276:        pb->name = xstrdup(name);
1.1       nicm      277:
                    278:        pb->data = data;
1.4       nicm      279:        pb->size = size;
1.19      nicm      280:
                    281:        pb->automatic = 0;
                    282:        pb->order = paste_next_order++;
1.35      nicm      283:
                    284:        pb->created = time(NULL);
1.27      nicm      285:
1.30      nicm      286:        if ((old = paste_get_name(name)) != NULL)
                    287:                paste_free(old);
1.19      nicm      288:
                    289:        RB_INSERT(paste_name_tree, &paste_by_name, pb);
                    290:        RB_INSERT(paste_time_tree, &paste_by_time, pb);
1.1       nicm      291:
                    292:        return (0);
1.9       nicm      293: }
                    294:
1.17      nicm      295: /* Convert start of buffer into a nice string. */
1.9       nicm      296: char *
1.33      nicm      297: paste_make_sample(struct paste_buffer *pb)
1.9       nicm      298: {
1.17      nicm      299:        char            *buf;
                    300:        size_t           len, used;
                    301:        const int        flags = VIS_OCTAL|VIS_TAB|VIS_NL;
                    302:        const size_t     width = 200;
1.9       nicm      303:
                    304:        len = pb->size;
                    305:        if (len > width)
                    306:                len = width;
1.25      nicm      307:        buf = xreallocarray(NULL, len, 4 + 4);
1.9       nicm      308:
1.33      nicm      309:        used = utf8_strvis(buf, pb->data, len, flags);
1.13      nicm      310:        if (pb->size > width || used > width)
1.17      nicm      311:                strlcpy(buf + width, "...", 4);
1.9       nicm      312:        return (buf);
1.1       nicm      313: }