1 /*
2 * scripts.c
3 * vim: expandtab:ts=4:sts=4:sw=4
4 *
5 * Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
6 *
7 * This file is part of Profanity.
8 *
9 * Profanity is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Profanity is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Profanity. If not, see <https://www.gnu.org/licenses/>.
21 *
22 * In addition, as a special exception, the copyright holders give permission to
23 * link the code of portions of this program with the OpenSSL library under
24 * certain conditions as described in each individual source file, and
25 * distribute linked combinations including the two.
26 *
27 * You must obey the GNU General Public License in all respects for all of the
28 * code used other than OpenSSL. If you modify file(s) with this exception, you
29 * may extend this exception to your version of the file(s), but you are not
30 * obligated to do so. If you do not wish to do so, delete this exception
31 * statement from your version. If you delete this exception statement from all
32 * source files in the program, then also delete it here.
33 *
34 */
35
36 #include "config.h"
37
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <sys/stat.h>
42
43 #include <glib.h>
44 #include <glib/gstdio.h>
45
46 #include "common.h"
47 #include "log.h"
48 #include "config/files.h"
49 #include "command/cmd_defs.h"
50 #include "ui/ui.h"
51 #include "ui/window_list.h"
52 #include "xmpp/xmpp.h"
53
54 void
scripts_init(void)55 scripts_init(void)
56 {
57 char* scriptsdir = files_get_data_path(DIR_SCRIPTS);
58
59 // mkdir if doesn't exist
60 errno = 0;
61 int res = g_mkdir_with_parents(scriptsdir, S_IRWXU);
62 if (res == -1) {
63 const char* errmsg = strerror(errno);
64 if (errmsg) {
65 log_error("Error creating directory: %s, %s", scriptsdir, errmsg);
66 } else {
67 log_error("Error creating directory: %s", scriptsdir);
68 }
69 }
70
71 free(scriptsdir);
72 }
73
74 GSList*
scripts_list(void)75 scripts_list(void)
76 {
77 char* scriptsdir = files_get_data_path(DIR_SCRIPTS);
78
79 GSList* result = NULL;
80 GDir* scripts = g_dir_open(scriptsdir, 0, NULL);
81 free(scriptsdir);
82
83 if (scripts) {
84 const gchar* script = g_dir_read_name(scripts);
85 while (script) {
86 result = g_slist_append(result, strdup(script));
87 script = g_dir_read_name(scripts);
88 }
89 g_dir_close(scripts);
90 }
91
92 return result;
93 }
94
95 GSList*
scripts_read(const char * const script)96 scripts_read(const char* const script)
97 {
98 char* scriptsdir = files_get_data_path(DIR_SCRIPTS);
99 GString* scriptpath = g_string_new(scriptsdir);
100 free(scriptsdir);
101 g_string_append(scriptpath, "/");
102 g_string_append(scriptpath, script);
103
104 FILE* scriptfile = g_fopen(scriptpath->str, "r");
105 if (!scriptfile) {
106 log_info("Script not found: %s", scriptpath->str);
107 g_string_free(scriptpath, TRUE);
108 return NULL;
109 }
110
111 g_string_free(scriptpath, TRUE);
112
113 char* line = NULL;
114 size_t len = 0;
115 ssize_t read;
116 GSList* result = NULL;
117
118 while ((read = getline(&line, &len, scriptfile)) != -1) {
119 if (g_str_has_suffix(line, "\n")) {
120 result = g_slist_append(result, g_strndup(line, strlen(line) - 1));
121 } else {
122 result = g_slist_append(result, strdup(line));
123 }
124 }
125
126 fclose(scriptfile);
127 if (line)
128 free(line);
129
130 return result;
131 }
132
133 gboolean
scripts_exec(const char * const script)134 scripts_exec(const char* const script)
135 {
136 char* scriptsdir = files_get_data_path(DIR_SCRIPTS);
137 GString* scriptpath = g_string_new(scriptsdir);
138 free(scriptsdir);
139 g_string_append(scriptpath, "/");
140 g_string_append(scriptpath, script);
141
142 FILE* scriptfile = g_fopen(scriptpath->str, "r");
143 if (!scriptfile) {
144 log_info("Script not found: %s", scriptpath->str);
145 g_string_free(scriptpath, TRUE);
146 return FALSE;
147 }
148
149 g_string_free(scriptpath, TRUE);
150
151 char* line = NULL;
152 size_t len = 0;
153 ssize_t read;
154
155 while ((read = getline(&line, &len, scriptfile)) != -1) {
156 ProfWin* win = wins_get_current();
157 cmd_process_input(win, line);
158 session_process_events();
159 ui_update();
160 }
161
162 fclose(scriptfile);
163 if (line)
164 free(line);
165
166 return TRUE;
167 }
168