]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/macro.c
final set of ANSI C changes
[thirdparty/bash.git] / lib / readline / macro.c
CommitLineData
ccc6cda3
JA
1/* macro.c -- keyboard macros for readline. */
2
514049fa 3/* Copyright (C) 1994-2009,2017 Free Software Foundation, Inc.
ccc6cda3 4
2e4498b3
CR
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
ccc6cda3 7
2e4498b3
CR
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
ccc6cda3
JA
11 (at your option) any later version.
12
2e4498b3
CR
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ccc6cda3
JA
16 GNU General Public License for more details.
17
2e4498b3
CR
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
20*/
21
ccc6cda3
JA
22#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
25# include <config.h>
26#endif
27
28#include <sys/types.h>
29
30#if defined (HAVE_UNISTD_H)
31# include <unistd.h> /* for _POSIX_VERSION */
32#endif /* HAVE_UNISTD_H */
33
34#if defined (HAVE_STDLIB_H)
35# include <stdlib.h>
36#else
37# include "ansi_stdlib.h"
38#endif /* HAVE_STDLIB_H */
39
40#include <stdio.h>
41
42/* System-specific feature definitions and include files. */
43#include "rldefs.h"
44
45/* Some standard library routines. */
46#include "readline.h"
47#include "history.h"
48
bb70624e
JA
49#include "rlprivate.h"
50#include "xmalloc.h"
ccc6cda3 51
61c476d2
CR
52#define MAX_MACRO_LEVEL 16
53
ccc6cda3
JA
54/* **************************************************************** */
55/* */
56/* Hacking Keyboard Macros */
57/* */
58/* **************************************************************** */
59
ccc6cda3
JA
60/* The currently executing macro string. If this is non-zero,
61 then it is a malloc ()'ed string where input is coming from. */
28ef6c31
JA
62char *rl_executing_macro = (char *)NULL;
63
ccc6cda3
JA
64/* The offset in the above string to the next character to be read. */
65static int executing_macro_index;
66
67/* The current macro string being built. Characters get stuffed
68 in here by add_macro_char (). */
69static char *current_macro = (char *)NULL;
70
71/* The size of the buffer allocated to current_macro. */
2e725f73 72static size_t current_macro_size;
ccc6cda3
JA
73
74/* The index at which characters are being added to current_macro. */
75static int current_macro_index;
76
77/* A structure used to save nested macro strings.
78 It is a linked list of string/index for each saved macro. */
79struct saved_macro {
80 struct saved_macro *next;
81 char *string;
82 int sindex;
83};
84
85/* The list of saved macros. */
86static struct saved_macro *macro_list = (struct saved_macro *)NULL;
87
61c476d2
CR
88static int macro_level = 0;
89
ccc6cda3
JA
90/* Set up to read subsequent input from STRING.
91 STRING is free ()'ed when we are done with it. */
92void
514049fa 93_rl_with_macro_input (char *string)
ccc6cda3 94{
61c476d2
CR
95 if (macro_level > MAX_MACRO_LEVEL)
96 {
97 _rl_errmsg ("maximum macro execution nesting level exceeded");
98 _rl_abort_internal ();
99 return;
100 }
101
ec1101c3
CR
102#if 0
103 if (rl_executing_macro) /* XXX - later */
104#endif
105 _rl_push_executing_macro ();
28ef6c31 106 rl_executing_macro = string;
ccc6cda3 107 executing_macro_index = 0;
28ef6c31 108 RL_SETSTATE(RL_STATE_MACROINPUT);
ccc6cda3
JA
109}
110
111/* Return the next character available from a macro, or 0 if
112 there are no macro characters. */
113int
514049fa 114_rl_next_macro_key (void)
ccc6cda3 115{
19e1dd93
CR
116 int c;
117
28ef6c31 118 if (rl_executing_macro == 0)
ccc6cda3
JA
119 return (0);
120
28ef6c31 121 if (rl_executing_macro[executing_macro_index] == 0)
ccc6cda3
JA
122 {
123 _rl_pop_executing_macro ();
124 return (_rl_next_macro_key ());
125 }
126
19e1dd93
CR
127#if defined (READLINE_CALLBACKS)
128 c = rl_executing_macro[executing_macro_index++];
d3ad40de 129 if (RL_ISSTATE (RL_STATE_CALLBACK) && RL_ISSTATE (RL_STATE_READCMD|RL_STATE_MOREINPUT) && rl_executing_macro[executing_macro_index] == 0)
19e1dd93
CR
130 _rl_pop_executing_macro ();
131 return c;
132#else
ec1101c3
CR
133 /* XXX - consider doing the same as the callback code, just not testing
134 whether we're running in callback mode */
28ef6c31 135 return (rl_executing_macro[executing_macro_index++]);
19e1dd93 136#endif
ccc6cda3
JA
137}
138
ec1101c3 139int
514049fa 140_rl_peek_macro_key (void)
ec1101c3
CR
141{
142 if (rl_executing_macro == 0)
143 return (0);
144 if (rl_executing_macro[executing_macro_index] == 0 && (macro_list == 0 || macro_list->string == 0))
145 return (0);
146 if (rl_executing_macro[executing_macro_index] == 0 && macro_list && macro_list->string)
147 return (macro_list->string[0]);
148 return (rl_executing_macro[executing_macro_index]);
149}
150
9711fdc5 151int
514049fa 152_rl_prev_macro_key (void)
9711fdc5
CR
153{
154 if (rl_executing_macro == 0)
155 return (0);
156
157 if (executing_macro_index == 0)
158 return (0);
159
160 executing_macro_index--;
161 return (rl_executing_macro[executing_macro_index]);
162}
163
ccc6cda3
JA
164/* Save the currently executing macro on a stack of saved macros. */
165void
514049fa 166_rl_push_executing_macro (void)
ccc6cda3
JA
167{
168 struct saved_macro *saver;
169
170 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
171 saver->next = macro_list;
172 saver->sindex = executing_macro_index;
28ef6c31 173 saver->string = rl_executing_macro;
ccc6cda3
JA
174
175 macro_list = saver;
61c476d2
CR
176
177 macro_level++;
ccc6cda3
JA
178}
179
180/* Discard the current macro, replacing it with the one
181 on the top of the stack of saved macros. */
182void
514049fa 183_rl_pop_executing_macro (void)
ccc6cda3
JA
184{
185 struct saved_macro *macro;
186
28ef6c31
JA
187 FREE (rl_executing_macro);
188 rl_executing_macro = (char *)NULL;
ccc6cda3
JA
189 executing_macro_index = 0;
190
191 if (macro_list)
192 {
193 macro = macro_list;
28ef6c31 194 rl_executing_macro = macro_list->string;
ccc6cda3
JA
195 executing_macro_index = macro_list->sindex;
196 macro_list = macro_list->next;
d3ad40de 197 xfree (macro);
ccc6cda3 198 }
28ef6c31 199
61c476d2
CR
200 macro_level--;
201
28ef6c31
JA
202 if (rl_executing_macro == 0)
203 RL_UNSETSTATE(RL_STATE_MACROINPUT);
ccc6cda3
JA
204}
205
206/* Add a character to the macro being built. */
207void
514049fa 208_rl_add_macro_char (int c)
ccc6cda3
JA
209{
210 if (current_macro_index + 1 >= current_macro_size)
211 {
212 if (current_macro == 0)
f73dda09 213 current_macro = (char *)xmalloc (current_macro_size = 25);
ccc6cda3 214 else
f73dda09 215 current_macro = (char *)xrealloc (current_macro, current_macro_size += 25);
ccc6cda3
JA
216 }
217
218 current_macro[current_macro_index++] = c;
219 current_macro[current_macro_index] = '\0';
220}
221
222void
514049fa 223_rl_kill_kbd_macro (void)
ccc6cda3
JA
224{
225 if (current_macro)
226 {
d3ad40de 227 xfree (current_macro);
ccc6cda3
JA
228 current_macro = (char *) NULL;
229 }
230 current_macro_size = current_macro_index = 0;
231
28ef6c31
JA
232 FREE (rl_executing_macro);
233 rl_executing_macro = (char *) NULL;
ccc6cda3
JA
234 executing_macro_index = 0;
235
28ef6c31 236 RL_UNSETSTATE(RL_STATE_MACRODEF);
ccc6cda3
JA
237}
238
239/* Begin defining a keyboard macro.
240 Keystrokes are recorded as they are executed.
241 End the definition with rl_end_kbd_macro ().
242 If a numeric argument was explicitly typed, then append this
243 definition to the end of the existing macro, and start by
244 re-executing the existing macro. */
245int
514049fa 246rl_start_kbd_macro (int ignore1, int ignore2)
ccc6cda3 247{
7117c2d2 248 if (RL_ISSTATE (RL_STATE_MACRODEF))
ccc6cda3
JA
249 {
250 _rl_abort_internal ();
c28fcb7e 251 return 1;
ccc6cda3
JA
252 }
253
254 if (rl_explicit_arg)
255 {
256 if (current_macro)
257 _rl_with_macro_input (savestring (current_macro));
258 }
259 else
260 current_macro_index = 0;
261
28ef6c31 262 RL_SETSTATE(RL_STATE_MACRODEF);
ccc6cda3
JA
263 return 0;
264}
265
266/* Stop defining a keyboard macro.
267 A numeric argument says to execute the macro right now,
268 that many times, counting the definition as the first time. */
269int
514049fa 270rl_end_kbd_macro (int count, int ignore)
ccc6cda3 271{
7117c2d2 272 if (RL_ISSTATE (RL_STATE_MACRODEF) == 0)
ccc6cda3
JA
273 {
274 _rl_abort_internal ();
c28fcb7e 275 return 1;
ccc6cda3
JA
276 }
277
2dead0c4 278 current_macro_index -= rl_key_sequence_length;
19849600
CR
279 if (current_macro_index < 0)
280 current_macro_index = 0;
ccc6cda3
JA
281 current_macro[current_macro_index] = '\0';
282
28ef6c31 283 RL_UNSETSTATE(RL_STATE_MACRODEF);
ccc6cda3
JA
284
285 return (rl_call_last_kbd_macro (--count, 0));
286}
287
288/* Execute the most recently defined keyboard macro.
289 COUNT says how many times to execute it. */
290int
514049fa 291rl_call_last_kbd_macro (int count, int ignore)
ccc6cda3
JA
292{
293 if (current_macro == 0)
294 _rl_abort_internal ();
295
7117c2d2 296 if (RL_ISSTATE (RL_STATE_MACRODEF))
ccc6cda3 297 {
28ef6c31 298 rl_ding (); /* no recursive macros */
ccc6cda3
JA
299 current_macro[--current_macro_index] = '\0'; /* erase this char */
300 return 0;
301 }
302
303 while (count--)
304 _rl_with_macro_input (savestring (current_macro));
305 return 0;
306}
307
ba4ab055 308int
514049fa 309rl_print_last_kbd_macro (int count, int ignore)
ba4ab055
CR
310{
311 char *m;
312
313 if (current_macro == 0)
314 {
315 rl_ding ();
316 return 0;
317 }
318 m = _rl_untranslate_macro_value (current_macro, 1);
319 rl_crlf ();
320 printf ("%s", m);
321 fflush (stdout);
322 rl_crlf ();
323 FREE (m);
324 rl_forced_update_display ();
325 rl_display_fixed = 1;
326
327 return 0;
328}
329
ccc6cda3 330void
514049fa 331rl_push_macro_input (char *macro)
ccc6cda3
JA
332{
333 _rl_with_macro_input (macro);
334}