Annotation of embedaddon/readline/macro.c, revision 1.1.1.2
1.1 misho 1: /* macro.c -- keyboard macros for readline. */
2:
1.1.1.2 ! misho 3: /* Copyright (C) 1994-2009,2017 Free Software Foundation, Inc.
1.1 misho 4:
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.
7:
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
11: (at your option) any later version.
12:
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
16: GNU General Public License for more details.
17:
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:
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:
1.1.1.2 ! misho 52: #define MAX_MACRO_LEVEL 16
! 53:
1.1 misho 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: /* The offset in the above string to the next character to be read. */
65: static int executing_macro_index;
66:
67: /* The current macro string being built. Characters get stuffed
68: in here by add_macro_char (). */
69: static char *current_macro = (char *)NULL;
70:
71: /* The size of the buffer allocated to current_macro. */
72: static int current_macro_size;
73:
74: /* The index at which characters are being added to current_macro. */
75: static 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. */
79: struct saved_macro {
80: struct saved_macro *next;
81: char *string;
82: int sindex;
83: };
84:
85: /* The list of saved macros. */
86: static struct saved_macro *macro_list = (struct saved_macro *)NULL;
87:
1.1.1.2 ! misho 88: static int macro_level = 0;
! 89:
1.1 misho 90: /* Set up to read subsequent input from STRING.
91: STRING is free ()'ed when we are done with it. */
92: void
1.1.1.2 ! misho 93: _rl_with_macro_input (char *string)
1.1 misho 94: {
1.1.1.2 ! misho 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 ();
1.1 misho 106: rl_executing_macro = string;
107: executing_macro_index = 0;
108: RL_SETSTATE(RL_STATE_MACROINPUT);
109: }
110:
111: /* Return the next character available from a macro, or 0 if
112: there are no macro characters. */
113: int
1.1.1.2 ! misho 114: _rl_next_macro_key (void)
1.1 misho 115: {
116: int c;
117:
118: if (rl_executing_macro == 0)
119: return (0);
120:
121: if (rl_executing_macro[executing_macro_index] == 0)
122: {
123: _rl_pop_executing_macro ();
124: return (_rl_next_macro_key ());
125: }
126:
127: #if defined (READLINE_CALLBACKS)
128: c = rl_executing_macro[executing_macro_index++];
129: if (RL_ISSTATE (RL_STATE_CALLBACK) && RL_ISSTATE (RL_STATE_READCMD|RL_STATE_MOREINPUT) && rl_executing_macro[executing_macro_index] == 0)
130: _rl_pop_executing_macro ();
131: return c;
132: #else
1.1.1.2 ! misho 133: /* XXX - consider doing the same as the callback code, just not testing
! 134: whether we're running in callback mode */
1.1 misho 135: return (rl_executing_macro[executing_macro_index++]);
136: #endif
137: }
138:
139: int
1.1.1.2 ! misho 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:
! 151: int
! 152: _rl_prev_macro_key (void)
1.1 misho 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:
164: /* Save the currently executing macro on a stack of saved macros. */
165: void
1.1.1.2 ! misho 166: _rl_push_executing_macro (void)
1.1 misho 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;
173: saver->string = rl_executing_macro;
174:
175: macro_list = saver;
1.1.1.2 ! misho 176:
! 177: macro_level++;
1.1 misho 178: }
179:
180: /* Discard the current macro, replacing it with the one
181: on the top of the stack of saved macros. */
182: void
1.1.1.2 ! misho 183: _rl_pop_executing_macro (void)
1.1 misho 184: {
185: struct saved_macro *macro;
186:
187: FREE (rl_executing_macro);
188: rl_executing_macro = (char *)NULL;
189: executing_macro_index = 0;
190:
191: if (macro_list)
192: {
193: macro = macro_list;
194: rl_executing_macro = macro_list->string;
195: executing_macro_index = macro_list->sindex;
196: macro_list = macro_list->next;
197: xfree (macro);
198: }
199:
1.1.1.2 ! misho 200: macro_level--;
! 201:
1.1 misho 202: if (rl_executing_macro == 0)
203: RL_UNSETSTATE(RL_STATE_MACROINPUT);
204: }
205:
206: /* Add a character to the macro being built. */
207: void
1.1.1.2 ! misho 208: _rl_add_macro_char (int c)
1.1 misho 209: {
210: if (current_macro_index + 1 >= current_macro_size)
211: {
212: if (current_macro == 0)
213: current_macro = (char *)xmalloc (current_macro_size = 25);
214: else
215: current_macro = (char *)xrealloc (current_macro, current_macro_size += 25);
216: }
217:
218: current_macro[current_macro_index++] = c;
219: current_macro[current_macro_index] = '\0';
220: }
221:
222: void
1.1.1.2 ! misho 223: _rl_kill_kbd_macro (void)
1.1 misho 224: {
225: if (current_macro)
226: {
227: xfree (current_macro);
228: current_macro = (char *) NULL;
229: }
230: current_macro_size = current_macro_index = 0;
231:
232: FREE (rl_executing_macro);
233: rl_executing_macro = (char *) NULL;
234: executing_macro_index = 0;
235:
236: RL_UNSETSTATE(RL_STATE_MACRODEF);
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. */
245: int
1.1.1.2 ! misho 246: rl_start_kbd_macro (int ignore1, int ignore2)
1.1 misho 247: {
248: if (RL_ISSTATE (RL_STATE_MACRODEF))
249: {
250: _rl_abort_internal ();
1.1.1.2 ! misho 251: return 1;
1.1 misho 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:
262: RL_SETSTATE(RL_STATE_MACRODEF);
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. */
269: int
1.1.1.2 ! misho 270: rl_end_kbd_macro (int count, int ignore)
1.1 misho 271: {
272: if (RL_ISSTATE (RL_STATE_MACRODEF) == 0)
273: {
274: _rl_abort_internal ();
1.1.1.2 ! misho 275: return 1;
1.1 misho 276: }
277:
278: current_macro_index -= rl_key_sequence_length;
279: current_macro[current_macro_index] = '\0';
280:
281: RL_UNSETSTATE(RL_STATE_MACRODEF);
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. */
288: int
1.1.1.2 ! misho 289: rl_call_last_kbd_macro (int count, int ignore)
1.1 misho 290: {
291: if (current_macro == 0)
292: _rl_abort_internal ();
293:
294: if (RL_ISSTATE (RL_STATE_MACRODEF))
295: {
296: rl_ding (); /* no recursive macros */
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:
306: int
1.1.1.2 ! misho 307: rl_print_last_kbd_macro (int count, int ignore)
1.1 misho 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:
328: void
1.1.1.2 ! misho 329: rl_push_macro_input (char *macro)
1.1 misho 330: {
331: _rl_with_macro_input (macro);
332: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>