[BACK]Return to cmd-load-buffer.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-load-buffer.c, Revision 1.20

1.20    ! nicm        1: /* $OpenBSD: cmd-load-buffer.c,v 1.19 2011/10/23 08:34:01 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
                      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:
1.12      nicm       19: #include <sys/types.h>
                     20:
1.1       nicm       21: #include <errno.h>
1.11      nicm       22: #include <stdio.h>
1.1       nicm       23: #include <stdlib.h>
                     24: #include <string.h>
1.12      nicm       25: #include <unistd.h>
1.1       nicm       26:
                     27: #include "tmux.h"
                     28:
                     29: /*
1.18      nicm       30:  * Loads a paste buffer from a file.
1.1       nicm       31:  */
                     32:
                     33: int    cmd_load_buffer_exec(struct cmd *, struct cmd_ctx *);
1.20    ! nicm       34: void   cmd_load_buffer_callback(struct client *, int, void *);
1.1       nicm       35:
                     36: const struct cmd_entry cmd_load_buffer_entry = {
                     37:        "load-buffer", "loadb",
1.16      nicm       38:        "b:", 1, 1,
1.15      nicm       39:        CMD_BUFFER_USAGE " path",
1.16      nicm       40:        0,
                     41:        NULL,
                     42:        NULL,
                     43:        cmd_load_buffer_exec
1.1       nicm       44: };
                     45:
                     46: int
                     47: cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
                     48: {
1.16      nicm       49:        struct args     *args = self->args;
                     50:        struct client   *c = ctx->cmdclient;
1.19      nicm       51:        struct session  *s;
1.16      nicm       52:        FILE            *f;
1.19      nicm       53:        const char      *path, *newpath, *wd;
1.16      nicm       54:        char            *pdata, *new_pdata, *cause;
                     55:        size_t           psize;
                     56:        u_int            limit;
1.20    ! nicm       57:        int              ch, error, buffer, *buffer_ptr;
1.16      nicm       58:
                     59:        if (!args_has(args, 'b'))
                     60:                buffer = -1;
                     61:        else {
                     62:                buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
                     63:                if (cause != NULL) {
                     64:                        ctx->error(ctx, "buffer %s", cause);
                     65:                        xfree(cause);
                     66:                        return (-1);
                     67:                }
                     68:        }
1.1       nicm       69:
1.16      nicm       70:        path = args->argv[0];
                     71:        if (strcmp(path, "-") == 0) {
                     72:                buffer_ptr = xmalloc(sizeof *buffer_ptr);
                     73:                *buffer_ptr = buffer;
                     74:
1.20    ! nicm       75:                error = server_set_stdin_callback (c, cmd_load_buffer_callback,
        !            76:                    buffer_ptr, &cause);
        !            77:                if (error != 0) {
        !            78:                        ctx->error(ctx, "%s: %s", path, cause);
        !            79:                        xfree(cause);
        !            80:                        return (-1);
        !            81:                }
1.13      nicm       82:                return (1);
                     83:        }
                     84:
1.19      nicm       85:        if (c != NULL)
                     86:                wd = c->cwd;
                     87:        else if ((s = cmd_current_session(ctx, 0)) != NULL) {
                     88:                wd = options_get_string(&s->options, "default-path");
                     89:                if (*wd == '\0')
                     90:                        wd = s->cwd;
                     91:        } else
                     92:                wd = NULL;
                     93:        if (wd != NULL && *wd != '\0') {
                     94:                newpath = get_full_path(wd, path);
                     95:                if (newpath != NULL)
                     96:                        path = newpath;
                     97:        }
1.16      nicm       98:        if ((f = fopen(path, "rb")) == NULL) {
                     99:                ctx->error(ctx, "%s: %s", path, strerror(errno));
1.13      nicm      100:                return (-1);
1.1       nicm      101:        }
                    102:
1.11      nicm      103:        pdata = NULL;
                    104:        psize = 0;
                    105:        while ((ch = getc(f)) != EOF) {
                    106:                /* Do not let the server die due to memory exhaustion. */
                    107:                if ((new_pdata = realloc(pdata, psize + 2)) == NULL) {
                    108:                        ctx->error(ctx, "realloc error: %s", strerror(errno));
                    109:                        goto error;
                    110:                }
                    111:                pdata = new_pdata;
                    112:                pdata[psize++] = ch;
1.1       nicm      113:        }
1.11      nicm      114:        if (ferror(f)) {
1.16      nicm      115:                ctx->error(ctx, "%s: read error", path);
1.9       nicm      116:                goto error;
1.1       nicm      117:        }
1.11      nicm      118:        if (pdata != NULL)
                    119:                pdata[psize] = '\0';
1.1       nicm      120:
1.13      nicm      121:        fclose(f);
1.1       nicm      122:
1.15      nicm      123:        limit = options_get_number(&global_options, "buffer-limit");
1.16      nicm      124:        if (buffer == -1) {
1.15      nicm      125:                paste_add(&global_buffers, pdata, psize, limit);
1.1       nicm      126:                return (0);
                    127:        }
1.16      nicm      128:        if (paste_replace(&global_buffers, buffer, pdata, psize) != 0) {
                    129:                ctx->error(ctx, "no buffer %d", buffer);
1.18      nicm      130:                xfree(pdata);
1.13      nicm      131:                return (-1);
1.1       nicm      132:        }
                    133:
                    134:        return (0);
1.9       nicm      135:
                    136: error:
                    137:        if (pdata != NULL)
                    138:                xfree(pdata);
1.13      nicm      139:        if (f != NULL)
                    140:                fclose(f);
1.9       nicm      141:        return (-1);
1.13      nicm      142: }
                    143:
                    144: void
1.20    ! nicm      145: cmd_load_buffer_callback(struct client *c, int closed, void *data)
1.13      nicm      146: {
1.16      nicm      147:        int     *buffer = data;
1.15      nicm      148:        char    *pdata;
                    149:        size_t   psize;
                    150:        u_int    limit;
1.13      nicm      151:
1.20    ! nicm      152:        if (!closed)
        !           153:                return;
        !           154:        c->stdin_callback = NULL;
        !           155:
        !           156:        c->references--;
1.13      nicm      157:        c->flags |= CLIENT_EXIT;
                    158:
1.20    ! nicm      159:        psize = EVBUFFER_LENGTH(c->stdin_data);
1.16      nicm      160:        if (psize == 0 || (pdata = malloc(psize + 1)) == NULL) {
1.17      nicm      161:                xfree(data);
1.15      nicm      162:                return;
1.16      nicm      163:        }
1.20    ! nicm      164:        memcpy(pdata, EVBUFFER_DATA(c->stdin_data), psize);
1.13      nicm      165:        pdata[psize] = '\0';
1.20    ! nicm      166:        evbuffer_drain(c->stdin_data, psize);
1.13      nicm      167:
1.15      nicm      168:        limit = options_get_number(&global_options, "buffer-limit");
                    169:        if (*buffer == -1)
                    170:                paste_add(&global_buffers, pdata, psize, limit);
                    171:        else if (paste_replace(&global_buffers, *buffer, pdata, psize) != 0) {
1.13      nicm      172:                /* No context so can't use server_client_msg_error. */
1.20    ! nicm      173:                evbuffer_add_printf(c->stderr_data, "no buffer %d\n", *buffer);
        !           174:                server_push_stderr(c);
1.13      nicm      175:        }
1.16      nicm      176:
1.17      nicm      177:        xfree(data);
1.1       nicm      178: }