summaryrefslogtreecommitdiff
path: root/src/script.c
blob: 91fec730e75bdbea10bb8c111efe08e7e9da4da3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (C) 1997-2000, Michael Jennings
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

static const char cvs_ident[] = "$Id$";

#include "config.h"
#include "feature.h"

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <X11/cursorfont.h>
#include <signal.h>

#include "command.h"
#include "options.h"
#include "pixmap.h"
#include "screen.h"
#include "script.h"
#include "startup.h"
#include "system.h"

static eterm_script_handler_t script_handlers[] =
{
  { "die",       script_handler_exit },
  { "exec",      script_handler_spawn },
  { "exit",      script_handler_exit },
  { "quit",      script_handler_exit },
  { "save",      script_handler_save },
  { "search",    script_handler_search },
  { "spawn",     script_handler_spawn },

  { "nop", script_handler_nop }
};
static size_t handler_count = sizeof(script_handlers) / sizeof(eterm_script_handler_t);

#if 0
void
eterm_handle_winop(char *action)
{
  char *winid;
  Window win = 0;

  ASSERT(action != NULL);

  winid = strchr(action, ' ');
  if (winid) {
    win = (Window) strtoul(winid + 1, (char **) NULL, 0);
  }
  if (win == 0) {		/* If no window ID was given, or if the strtoul() call failed */
    win = TermWin.parent;
  }
  if (!BEG_STRCASECMP(action, "raise")) {
    XRaiseWindow(Xdisplay, win);
  } else if (!BEG_STRCASECMP(action, "lower")) {
    XLowerWindow(Xdisplay, win);
  } else if (!BEG_STRCASECMP(action, "map")) {
    XMapWindow(Xdisplay, win);
  } else if (!BEG_STRCASECMP(action, "unmap")) {
    XUnmapWindow(Xdisplay, win);
  } else if (!BEG_STRCASECMP(action, "move")) {
    int x, y, n;
    char *xx, *yy;

    n = num_words(action);
    if (n == 3 || n == 4) {
      if (n == 3) {
        win = TermWin.parent;
      }
      xx = get_pword(n - 1, action);
      yy = get_pword(n, action);
      x = (int) strtol(xx, (char **) NULL, 0);
      y = (int) strtol(yy, (char **) NULL, 0);
      XMoveWindow(Xdisplay, win, x, y);
    }
  } else if (!BEG_STRCASECMP(action, "resize")) {
    int w, h, n;
    char *ww, *hh;

    n = num_words(action);
    if (n == 3 || n == 4) {
      if (n == 3) {
        win = TermWin.parent;
      }
      ww = get_pword(n - 1, action);
      hh = get_pword(n, action);
      w = (int) strtol(ww, (char **) NULL, 0);
      h = (int) strtol(hh, (char **) NULL, 0);
      XResizeWindow(Xdisplay, win, w, h);
    }
  } else if (!BEG_STRCASECMP(action, "kill")) {
    XKillClient(Xdisplay, win);
  } else if (!BEG_STRCASECMP(action, "iconify")) {
    XIconifyWindow(Xdisplay, win, Xscreen);
  } else {
    print_error("IPC Error:  Unrecognized window operation \"%s\"\n", action);
  }
}
#endif

/********* HANDLERS **********/
void
script_handler_exit(char **params)
{
  unsigned char code = 0;
  char *tmp;

  if (params && *params) {
    if (isdigit(params[0][0]) || (params[0][0] == '-' && isdigit(params[0][1]))) {
      code = (unsigned char) atoi(params[0]);
    } else {
      tmp = join(" ", params);
      printf("Exiting:  %s\n", tmp);
      FREE(tmp);
    }
  }
  exit(code);
}

void
script_handler_save(char **params)
{
  if (params && *params) {
    if (!strcasecmp(params[0], "theme")) {
      save_config(params[1], SAVE_THEME_CONFIG);
    } else {
      save_config(params[0], SAVE_USER_CONFIG);
    }
  } else {
    save_config(NULL, SAVE_USER_CONFIG);
  }
}

void
script_handler_search(char **params)
{
  scr_search_scrollback(params ? params[0] : NULL);
}

void
script_handler_spawn(char **params)
{
  char *tmp;

  if (params && *params) {
    tmp = join(" ", params);
    system_no_wait(tmp);
    FREE(tmp);
  } else {
    system_no_wait("Eterm");
  }
}

void
script_handler_nop(char **params)
{
  USE_VAR(params);
}

/********* ENGINE *********/
eterm_script_handler_t *
script_find_handler(const char *name)
{
  register unsigned long i;

  for (i = 0; i < handler_count; i++) {
    /* Small optimization.  Only call strcasecmp() if the first letter matches. */
    if ((tolower(name[0]) == tolower(script_handlers[i].name[0]))
        && !strcasecmp(name, script_handlers[i].name)) {
      return &script_handlers[i];
    }
  }
  return NULL;
}

void
script_parse(char *s)
{
  char **token_list, **param_list;
  register char *pstr;
  register unsigned long i;
  char *func_name, *params, *tmp;
  size_t len;
  eterm_script_handler_t *func;

  REQUIRE(s != NULL);

  D_SCRIPT(("Parsing:  \"%s\"\n", s));

  token_list = split(";", s);
  if (token_list == NULL) {
    D_SCRIPT(("No tokens found; ignoring script.\n"));
    return;
  }

  for (i = 0; token_list[i]; i++) {
    pstr = token_list[i];
    chomp(pstr);
    if (!(*pstr)) {
      continue;
    }
    if ((params = strchr(pstr, '(')) != NULL) {
      if (params != pstr) {
        len = params - pstr;
        func_name = (char *) MALLOC(len + 1);
        strncpy(func_name, pstr, len);
        func_name[len] = 0;
      } else {
        print_error("Error in script \"%s\":  Missing function name before \"%s\".\n", s, params);
        free_array((void **) token_list, 0);
        return;
      }
    } else {
      func_name = STRDUP(pstr);
    }
    if (!func_name) {
      free_array((void **) token_list, 0);
      return;
    }
    if (params) {
      params++;
      if ((tmp = strrchr(params, ')')) != NULL) {
        *tmp = 0;
      } else {
        print_error("Error in script \"%s\":  Missing closing parentheses for \"%s\".\n", s, token_list[i]);
        free_array((void **) token_list, 0);
        return;
      }
      param_list = split(", \t", params);
    } else {
      param_list = NULL;
    }
    D_SCRIPT(("Calling function %s with parameters:  %s\n", func_name, NONULL(params)));
    if ((func = script_find_handler(func_name)) != NULL) {
      (func->handler)(param_list);
    } else {
      print_error("Error in script \"%s\":  No such function \"%s\".\n", s, func_name);
    }
  }

  if (params) {
    free_array((void **) param_list, 0);
  }
  free_array((void **) token_list, 0);
}