[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.12

1.12    ! nicm        1: /* $OpenBSD: cmd-load-buffer.c,v 1.11 2010/02/22 20:33:12 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: /*
                     30:  * Loads a session paste buffer from a file.
                     31:  */
                     32:
                     33: int    cmd_load_buffer_exec(struct cmd *, struct cmd_ctx *);
                     34:
                     35: const struct cmd_entry cmd_load_buffer_entry = {
                     36:        "load-buffer", "loadb",
                     37:        CMD_BUFFER_SESSION_USAGE " path",
1.8       nicm       38:        CMD_ARG1, "",
1.1       nicm       39:        cmd_buffer_init,
                     40:        cmd_buffer_parse,
                     41:        cmd_load_buffer_exec,
                     42:        cmd_buffer_free,
                     43:        cmd_buffer_print
                     44: };
                     45:
                     46: int
                     47: cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
                     48: {
                     49:        struct cmd_buffer_data  *data = self->data;
                     50:        struct session          *s;
1.12    ! nicm       51:        FILE                    *f, *close_f;
1.11      nicm       52:        char                    *pdata, *new_pdata;
1.9       nicm       53:        size_t                   psize;
1.6       nicm       54:        u_int                    limit;
1.11      nicm       55:        int                      ch;
1.1       nicm       56:
                     57:        if ((s = cmd_find_session(ctx, data->target)) == NULL)
                     58:                return (-1);
                     59:
1.12    ! nicm       60:        if (strcmp(data->arg, "-") == 0 ) {
        !            61:                if (ctx->cmdclient == NULL) {
        !            62:                        ctx->error(ctx, "%s: can't read from stdin", data->arg);
        !            63:                        return (-1);
        !            64:                }
        !            65:                f = ctx->cmdclient->stdin_file;
        !            66:                if (isatty(fileno(ctx->cmdclient->stdin_file))) {
        !            67:                        ctx->error(ctx, "%s: stdin is a tty", data->arg);
        !            68:                        return (-1);
        !            69:                }
        !            70:                close_f = NULL;
        !            71:        } else {
        !            72:                if ((f = fopen(data->arg, "rb")) == NULL) {
        !            73:                        ctx->error(ctx, "%s: %s", data->arg, strerror(errno));
        !            74:                        return (-1);
        !            75:                }
        !            76:                close_f = f;
1.1       nicm       77:        }
                     78:
1.11      nicm       79:        pdata = NULL;
                     80:        psize = 0;
                     81:        while ((ch = getc(f)) != EOF) {
                     82:                /* Do not let the server die due to memory exhaustion. */
                     83:                if ((new_pdata = realloc(pdata, psize + 2)) == NULL) {
                     84:                        ctx->error(ctx, "realloc error: %s", strerror(errno));
                     85:                        goto error;
                     86:                }
                     87:                pdata = new_pdata;
                     88:                pdata[psize++] = ch;
1.1       nicm       89:        }
1.11      nicm       90:        if (ferror(f)) {
                     91:                ctx->error(ctx, "%s: read error", data->arg);
1.9       nicm       92:                goto error;
1.1       nicm       93:        }
1.11      nicm       94:        if (pdata != NULL)
                     95:                pdata[psize] = '\0';
1.1       nicm       96:
1.12    ! nicm       97:        if (close_f != NULL)
        !            98:                fclose(close_f);
1.1       nicm       99:
                    100:        limit = options_get_number(&s->options, "buffer-limit");
                    101:        if (data->buffer == -1) {
1.9       nicm      102:                paste_add(&s->buffers, pdata, psize, limit);
1.1       nicm      103:                return (0);
                    104:        }
1.9       nicm      105:        if (paste_replace(&s->buffers, data->buffer, pdata, psize) != 0) {
1.1       nicm      106:                ctx->error(ctx, "no buffer %d", data->buffer);
1.9       nicm      107:                goto error;
1.1       nicm      108:        }
                    109:
                    110:        return (0);
1.9       nicm      111:
                    112: error:
                    113:        if (pdata != NULL)
                    114:                xfree(pdata);
1.12    ! nicm      115:        if (close_f != NULL)
        !           116:                fclose(close_f);
1.9       nicm      117:        return (-1);
1.1       nicm      118: }