1 /* $OpenBSD: cmd-source-file.c,v 1.5 2009/09/21 15:32:06 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2008 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 19 #include <sys/types.h> 20 21 #include "tmux.h" 22 23 /* 24 * Sources a configuration file. 25 */ 26 27 int cmd_source_file_parse(struct cmd *, int, char **, char **); 28 int cmd_source_file_exec(struct cmd *, struct cmd_ctx *); 29 void cmd_source_file_free(struct cmd *); 30 void cmd_source_file_init(struct cmd *, int); 31 size_t cmd_source_file_print(struct cmd *, char *, size_t); 32 33 struct cmd_source_file_data { 34 char *path; 35 }; 36 37 const struct cmd_entry cmd_source_file_entry = { 38 "source-file", "source", 39 "path", 40 0, 0, 41 cmd_source_file_init, 42 cmd_source_file_parse, 43 cmd_source_file_exec, 44 cmd_source_file_free, 45 cmd_source_file_print 46 }; 47 48 void 49 cmd_source_file_init(struct cmd *self, unused int arg) 50 { 51 struct cmd_source_file_data *data; 52 53 self->data = data = xmalloc(sizeof *data); 54 data->path = NULL; 55 } 56 57 int 58 cmd_source_file_parse(struct cmd *self, int argc, char **argv, char **cause) 59 { 60 struct cmd_source_file_data *data; 61 int opt; 62 63 self->entry->init(self, KEYC_NONE); 64 data = self->data; 65 66 while ((opt = getopt(argc, argv, "")) != -1) { 67 switch (opt) { 68 default: 69 goto usage; 70 } 71 } 72 argc -= optind; 73 argv += optind; 74 if (argc != 1) 75 goto usage; 76 77 data->path = xstrdup(argv[0]); 78 return (0); 79 80 usage: 81 xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage); 82 83 self->entry->free(self); 84 return (-1); 85 } 86 87 int 88 cmd_source_file_exec(struct cmd *self, struct cmd_ctx *ctx) 89 { 90 struct cmd_source_file_data *data = self->data; 91 char *cause; 92 93 if (load_cfg(data->path, ctx, &cause) != 0) { 94 ctx->error(ctx, "%s", cause); 95 xfree(cause); 96 return (-1); 97 } 98 99 return (0); 100 } 101 102 void 103 cmd_source_file_free(struct cmd *self) 104 { 105 struct cmd_source_file_data *data = self->data; 106 107 if (data->path != NULL) 108 xfree(data->path); 109 xfree(data); 110 } 111 112 size_t 113 cmd_source_file_print(struct cmd *self, char *buf, size_t len) 114 { 115 struct cmd_source_file_data *data = self->data; 116 size_t off = 0; 117 118 off += xsnprintf(buf, len, "%s", self->entry->name); 119 if (data == NULL) 120 return (off); 121 if (off < len && data->path != NULL) 122 off += cmd_prarg(buf + off, len - off, " ", data->path); 123 return (off); 124 } 125