347f89bf9906606ec05853a03728327b9796d8c2
[platform/upstream/bash.git] / lib / readline / macro.c
1 /* macro.c -- keyboard macros for readline. */
2
3 /* Copyright (C) 1994 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library, a library for
6    reading lines of text with interactive input and history editing.
7
8    The GNU Readline Library is free software; you can redistribute it
9    and/or modify it under the terms of the GNU General Public License
10    as published by the Free Software Foundation; either version 2, or
11    (at your option) any later version.
12
13    The GNU Readline Library is distributed in the hope that it will be
14    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    The GNU General Public License is often shipped with GNU software, and
19    is generally kept in a file called COPYING or LICENSE.  If you do not
20    have a copy of the license, write to the Free Software Foundation,
21    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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
49 #include "rlprivate.h"
50 #include "xmalloc.h"
51
52 #define SWAP(s, e)  do { int t; t = s; s = e; e = t; } while (0)
53
54 /* **************************************************************** */
55 /*                                                                  */
56 /*                      Hacking Keyboard Macros                     */
57 /*                                                                  */
58 /* **************************************************************** */
59
60 /* The currently executing macro string.  If this is non-zero,
61    then it is a malloc ()'ed string where input is coming from. */
62 char *rl_executing_macro = (char *)NULL;
63
64 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
65 int _rl_defining_kbd_macro = 0;
66
67 /* The offset in the above string to the next character to be read. */
68 static int executing_macro_index;
69
70 /* The current macro string being built.  Characters get stuffed
71    in here by add_macro_char (). */
72 static char *current_macro = (char *)NULL;
73
74 /* The size of the buffer allocated to current_macro. */
75 static int current_macro_size;
76
77 /* The index at which characters are being added to current_macro. */
78 static int current_macro_index;
79
80 /* A structure used to save nested macro strings.
81    It is a linked list of string/index for each saved macro. */
82 struct saved_macro {
83   struct saved_macro *next;
84   char *string;
85   int sindex;
86 };
87
88 /* The list of saved macros. */
89 static struct saved_macro *macro_list = (struct saved_macro *)NULL;
90
91 /* Set up to read subsequent input from STRING.
92    STRING is free ()'ed when we are done with it. */
93 void
94 _rl_with_macro_input (string)
95      char *string;
96 {
97   _rl_push_executing_macro ();
98   rl_executing_macro = string;
99   executing_macro_index = 0;
100   RL_SETSTATE(RL_STATE_MACROINPUT);
101 }
102
103 /* Return the next character available from a macro, or 0 if
104    there are no macro characters. */
105 int
106 _rl_next_macro_key ()
107 {
108   if (rl_executing_macro == 0)
109     return (0);
110
111   if (rl_executing_macro[executing_macro_index] == 0)
112     {
113       _rl_pop_executing_macro ();
114       return (_rl_next_macro_key ());
115     }
116
117   return (rl_executing_macro[executing_macro_index++]);
118 }
119
120 /* Save the currently executing macro on a stack of saved macros. */
121 void
122 _rl_push_executing_macro ()
123 {
124   struct saved_macro *saver;
125
126   saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
127   saver->next = macro_list;
128   saver->sindex = executing_macro_index;
129   saver->string = rl_executing_macro;
130
131   macro_list = saver;
132 }
133
134 /* Discard the current macro, replacing it with the one
135    on the top of the stack of saved macros. */
136 void
137 _rl_pop_executing_macro ()
138 {
139   struct saved_macro *macro;
140
141   FREE (rl_executing_macro);
142   rl_executing_macro = (char *)NULL;
143   executing_macro_index = 0;
144
145   if (macro_list)
146     {
147       macro = macro_list;
148       rl_executing_macro = macro_list->string;
149       executing_macro_index = macro_list->sindex;
150       macro_list = macro_list->next;
151       free (macro);
152     }
153
154   if (rl_executing_macro == 0)
155     RL_UNSETSTATE(RL_STATE_MACROINPUT);
156 }
157
158 /* Add a character to the macro being built. */
159 void
160 _rl_add_macro_char (c)
161      int c;
162 {
163   if (current_macro_index + 1 >= current_macro_size)
164     {
165       if (current_macro == 0)
166         current_macro = (char *)xmalloc (current_macro_size = 25);
167       else
168         current_macro = (char *)xrealloc (current_macro, current_macro_size += 25);
169     }
170
171   current_macro[current_macro_index++] = c;
172   current_macro[current_macro_index] = '\0';
173 }
174
175 void
176 _rl_kill_kbd_macro ()
177 {
178   if (current_macro)
179     {
180       free (current_macro);
181       current_macro = (char *) NULL;
182     }
183   current_macro_size = current_macro_index = 0;
184
185   FREE (rl_executing_macro);
186   rl_executing_macro = (char *) NULL;
187   executing_macro_index = 0;
188
189   _rl_defining_kbd_macro = 0;
190   RL_UNSETSTATE(RL_STATE_MACRODEF);
191 }
192
193 /* Begin defining a keyboard macro.
194    Keystrokes are recorded as they are executed.
195    End the definition with rl_end_kbd_macro ().
196    If a numeric argument was explicitly typed, then append this
197    definition to the end of the existing macro, and start by
198    re-executing the existing macro. */
199 int
200 rl_start_kbd_macro (ignore1, ignore2)
201      int ignore1, ignore2;
202 {
203   if (_rl_defining_kbd_macro)
204     {
205       _rl_abort_internal ();
206       return -1;
207     }
208
209   if (rl_explicit_arg)
210     {
211       if (current_macro)
212         _rl_with_macro_input (savestring (current_macro));
213     }
214   else
215     current_macro_index = 0;
216
217   _rl_defining_kbd_macro = 1;
218   RL_SETSTATE(RL_STATE_MACRODEF);
219   return 0;
220 }
221
222 /* Stop defining a keyboard macro.
223    A numeric argument says to execute the macro right now,
224    that many times, counting the definition as the first time. */
225 int
226 rl_end_kbd_macro (count, ignore)
227      int count, ignore;
228 {
229   if (_rl_defining_kbd_macro == 0)
230     {
231       _rl_abort_internal ();
232       return -1;
233     }
234
235   current_macro_index -= rl_key_sequence_length - 1;
236   current_macro[current_macro_index] = '\0';
237
238   _rl_defining_kbd_macro = 0;
239   RL_UNSETSTATE(RL_STATE_MACRODEF);
240
241   return (rl_call_last_kbd_macro (--count, 0));
242 }
243
244 /* Execute the most recently defined keyboard macro.
245    COUNT says how many times to execute it. */
246 int
247 rl_call_last_kbd_macro (count, ignore)
248      int count, ignore;
249 {
250   if (current_macro == 0)
251     _rl_abort_internal ();
252
253   if (_rl_defining_kbd_macro)
254     {
255       rl_ding ();               /* no recursive macros */
256       current_macro[--current_macro_index] = '\0';      /* erase this char */
257       return 0;
258     }
259
260   while (count--)
261     _rl_with_macro_input (savestring (current_macro));
262   return 0;
263 }
264
265 void
266 rl_push_macro_input (macro)
267      char *macro;
268 {
269   _rl_with_macro_input (macro);
270 }