Fix unused function error
[deliverable/binutils-gdb.git] / readline / readline / macro.c
CommitLineData
d60d9f65
SS
1/* macro.c -- keyboard macros for readline. */
2
cb41b9e7 3/* Copyright (C) 1994-2009,2017 Free Software Foundation, Inc.
d60d9f65 4
cc88a640
JK
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.
d60d9f65 7
cc88a640
JK
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
d60d9f65
SS
11 (at your option) any later version.
12
cc88a640
JK
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
d60d9f65
SS
16 GNU General Public License for more details.
17
cc88a640
JK
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
d60d9f65
SS
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
1b17e766
EZ
49#include "rlprivate.h"
50#include "xmalloc.h"
d60d9f65 51
cb41b9e7
TT
52#define MAX_MACRO_LEVEL 16
53
d60d9f65
SS
54/* **************************************************************** */
55/* */
56/* Hacking Keyboard Macros */
57/* */
58/* **************************************************************** */
59
d60d9f65
SS
60/* The currently executing macro string. If this is non-zero,
61 then it is a malloc ()'ed string where input is coming from. */
9255ee31 62char *rl_executing_macro = (char *)NULL;
d60d9f65
SS
63
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. */
72static int current_macro_size;
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
cb41b9e7
TT
88static int macro_level = 0;
89
d60d9f65
SS
90/* Set up to read subsequent input from STRING.
91 STRING is free ()'ed when we are done with it. */
92void
cb41b9e7 93_rl_with_macro_input (char *string)
d60d9f65 94{
cb41b9e7
TT
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
102#if 0
103 if (rl_executing_macro) /* XXX - later */
104#endif
105 _rl_push_executing_macro ();
9255ee31 106 rl_executing_macro = string;
d60d9f65 107 executing_macro_index = 0;
9255ee31 108 RL_SETSTATE(RL_STATE_MACROINPUT);
d60d9f65
SS
109}
110
111/* Return the next character available from a macro, or 0 if
112 there are no macro characters. */
113int
cb41b9e7 114_rl_next_macro_key (void)
d60d9f65 115{
5bdf8622
DJ
116 int c;
117
9255ee31 118 if (rl_executing_macro == 0)
d60d9f65
SS
119 return (0);
120
9255ee31 121 if (rl_executing_macro[executing_macro_index] == 0)
d60d9f65
SS
122 {
123 _rl_pop_executing_macro ();
124 return (_rl_next_macro_key ());
125 }
126
5bdf8622
DJ
127#if defined (READLINE_CALLBACKS)
128 c = rl_executing_macro[executing_macro_index++];
cc88a640 129 if (RL_ISSTATE (RL_STATE_CALLBACK) && RL_ISSTATE (RL_STATE_READCMD|RL_STATE_MOREINPUT) && rl_executing_macro[executing_macro_index] == 0)
5bdf8622
DJ
130 _rl_pop_executing_macro ();
131 return c;
132#else
cb41b9e7
TT
133 /* XXX - consider doing the same as the callback code, just not testing
134 whether we're running in callback mode */
9255ee31 135 return (rl_executing_macro[executing_macro_index++]);
5bdf8622 136#endif
d60d9f65
SS
137}
138
775e241e 139int
cb41b9e7
TT
140_rl_peek_macro_key (void)
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
151int
152_rl_prev_macro_key (void)
775e241e
TT
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
d60d9f65
SS
164/* Save the currently executing macro on a stack of saved macros. */
165void
cb41b9e7 166_rl_push_executing_macro (void)
d60d9f65
SS
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;
9255ee31 173 saver->string = rl_executing_macro;
d60d9f65
SS
174
175 macro_list = saver;
cb41b9e7
TT
176
177 macro_level++;
d60d9f65
SS
178}
179
180/* Discard the current macro, replacing it with the one
181 on the top of the stack of saved macros. */
182void
cb41b9e7 183_rl_pop_executing_macro (void)
d60d9f65
SS
184{
185 struct saved_macro *macro;
186
9255ee31
EZ
187 FREE (rl_executing_macro);
188 rl_executing_macro = (char *)NULL;
d60d9f65
SS
189 executing_macro_index = 0;
190
191 if (macro_list)
192 {
193 macro = macro_list;
9255ee31 194 rl_executing_macro = macro_list->string;
d60d9f65
SS
195 executing_macro_index = macro_list->sindex;
196 macro_list = macro_list->next;
cc88a640 197 xfree (macro);
d60d9f65 198 }
9255ee31 199
cb41b9e7
TT
200 macro_level--;
201
9255ee31
EZ
202 if (rl_executing_macro == 0)
203 RL_UNSETSTATE(RL_STATE_MACROINPUT);
d60d9f65
SS
204}
205
206/* Add a character to the macro being built. */
207void
cb41b9e7 208_rl_add_macro_char (int c)
d60d9f65
SS
209{
210 if (current_macro_index + 1 >= current_macro_size)
211 {
212 if (current_macro == 0)
9255ee31 213 current_macro = (char *)xmalloc (current_macro_size = 25);
d60d9f65 214 else
9255ee31 215 current_macro = (char *)xrealloc (current_macro, current_macro_size += 25);
d60d9f65
SS
216 }
217
218 current_macro[current_macro_index++] = c;
219 current_macro[current_macro_index] = '\0';
220}
221
222void
cb41b9e7 223_rl_kill_kbd_macro (void)
d60d9f65
SS
224{
225 if (current_macro)
226 {
cc88a640 227 xfree (current_macro);
d60d9f65
SS
228 current_macro = (char *) NULL;
229 }
230 current_macro_size = current_macro_index = 0;
231
9255ee31
EZ
232 FREE (rl_executing_macro);
233 rl_executing_macro = (char *) NULL;
d60d9f65
SS
234 executing_macro_index = 0;
235
9255ee31 236 RL_UNSETSTATE(RL_STATE_MACRODEF);
d60d9f65
SS
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
cb41b9e7 246rl_start_kbd_macro (int ignore1, int ignore2)
d60d9f65 247{
9255ee31 248 if (RL_ISSTATE (RL_STATE_MACRODEF))
d60d9f65
SS
249 {
250 _rl_abort_internal ();
775e241e 251 return 1;
d60d9f65
SS
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
9255ee31 262 RL_SETSTATE(RL_STATE_MACRODEF);
d60d9f65
SS
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
cb41b9e7 270rl_end_kbd_macro (int count, int ignore)
d60d9f65 271{
9255ee31 272 if (RL_ISSTATE (RL_STATE_MACRODEF) == 0)
d60d9f65
SS
273 {
274 _rl_abort_internal ();
775e241e 275 return 1;
d60d9f65
SS
276 }
277
775e241e 278 current_macro_index -= rl_key_sequence_length;
d60d9f65
SS
279 current_macro[current_macro_index] = '\0';
280
9255ee31 281 RL_UNSETSTATE(RL_STATE_MACRODEF);
d60d9f65
SS
282
283 return (rl_call_last_kbd_macro (--count, 0));
284}
285
286/* Execute the most recently defined keyboard macro.
287 COUNT says how many times to execute it. */
288int
cb41b9e7 289rl_call_last_kbd_macro (int count, int ignore)
d60d9f65
SS
290{
291 if (current_macro == 0)
292 _rl_abort_internal ();
293
9255ee31 294 if (RL_ISSTATE (RL_STATE_MACRODEF))
d60d9f65 295 {
9255ee31 296 rl_ding (); /* no recursive macros */
d60d9f65
SS
297 current_macro[--current_macro_index] = '\0'; /* erase this char */
298 return 0;
299 }
300
301 while (count--)
302 _rl_with_macro_input (savestring (current_macro));
303 return 0;
304}
305
775e241e 306int
cb41b9e7 307rl_print_last_kbd_macro (int count, int ignore)
775e241e
TT
308{
309 char *m;
310
311 if (current_macro == 0)
312 {
313 rl_ding ();
314 return 0;
315 }
316 m = _rl_untranslate_macro_value (current_macro, 1);
317 rl_crlf ();
318 printf ("%s", m);
319 fflush (stdout);
320 rl_crlf ();
321 FREE (m);
322 rl_forced_update_display ();
323 rl_display_fixed = 1;
324
325 return 0;
326}
327
d60d9f65 328void
cb41b9e7 329rl_push_macro_input (char *macro)
d60d9f65
SS
330{
331 _rl_with_macro_input (macro);
332}
This page took 0.883243 seconds and 4 git commands to generate.