Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/bash/src/lib/readline/undo.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* undo.c - manage list of changes to lines, offering opportunity to undo them */ | - | ||||||||||||||||||
2 | - | |||||||||||||||||||
3 | /* Copyright (C) 1987-2017 Free Software Foundation, Inc. | - | ||||||||||||||||||
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 | - | |||||||||||||||||||
52 | extern void _hs_replace_history_data PARAMS((int, histdata_t *, histdata_t *)); | - | ||||||||||||||||||
53 | - | |||||||||||||||||||
54 | /* Non-zero tells rl_delete_text and rl_insert_text to not add to | - | ||||||||||||||||||
55 | the undo list. */ | - | ||||||||||||||||||
56 | int _rl_doing_an_undo = 0; | - | ||||||||||||||||||
57 | - | |||||||||||||||||||
58 | /* How many unclosed undo groups we currently have. */ | - | ||||||||||||||||||
59 | int _rl_undo_group_level = 0; | - | ||||||||||||||||||
60 | - | |||||||||||||||||||
61 | /* The current undo list for THE_LINE. */ | - | ||||||||||||||||||
62 | UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL; | - | ||||||||||||||||||
63 | - | |||||||||||||||||||
64 | /* **************************************************************** */ | - | ||||||||||||||||||
65 | /* */ | - | ||||||||||||||||||
66 | /* Undo, and Undoing */ | - | ||||||||||||||||||
67 | /* */ | - | ||||||||||||||||||
68 | /* **************************************************************** */ | - | ||||||||||||||||||
69 | - | |||||||||||||||||||
70 | static UNDO_LIST * | - | ||||||||||||||||||
71 | alloc_undo_entry (enum undo_code what, int start, int end, char *text) | - | ||||||||||||||||||
72 | { | - | ||||||||||||||||||
73 | UNDO_LIST *temp; | - | ||||||||||||||||||
74 | - | |||||||||||||||||||
75 | temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST)); | - | ||||||||||||||||||
76 | temp->what = what; | - | ||||||||||||||||||
77 | temp->start = start; | - | ||||||||||||||||||
78 | temp->end = end; | - | ||||||||||||||||||
79 | temp->text = text; | - | ||||||||||||||||||
80 | - | |||||||||||||||||||
81 | temp->next = (UNDO_LIST *)NULL; | - | ||||||||||||||||||
82 | return temp; never executed: return temp; | 0 | ||||||||||||||||||
83 | } | - | ||||||||||||||||||
84 | - | |||||||||||||||||||
85 | /* Remember how to undo something. Concatenate some undos if that | - | ||||||||||||||||||
86 | seems right. */ | - | ||||||||||||||||||
87 | void | - | ||||||||||||||||||
88 | rl_add_undo (enum undo_code what, int start, int end, char *text) | - | ||||||||||||||||||
89 | { | - | ||||||||||||||||||
90 | UNDO_LIST *temp; | - | ||||||||||||||||||
91 | - | |||||||||||||||||||
92 | temp = alloc_undo_entry (what, start, end, text); | - | ||||||||||||||||||
93 | temp->next = rl_undo_list; | - | ||||||||||||||||||
94 | rl_undo_list = temp; | - | ||||||||||||||||||
95 | } never executed: end of block | 0 | ||||||||||||||||||
96 | - | |||||||||||||||||||
97 | /* Free an UNDO_LIST */ | - | ||||||||||||||||||
98 | void | - | ||||||||||||||||||
99 | _rl_free_undo_list (UNDO_LIST *ul) | - | ||||||||||||||||||
100 | { | - | ||||||||||||||||||
101 | UNDO_LIST *release; | - | ||||||||||||||||||
102 | - | |||||||||||||||||||
103 | while (ul)
| 0 | ||||||||||||||||||
104 | { | - | ||||||||||||||||||
105 | release = ul; | - | ||||||||||||||||||
106 | ul = ul->next; | - | ||||||||||||||||||
107 | - | |||||||||||||||||||
108 | if (release->what == UNDO_DELETE)
| 0 | ||||||||||||||||||
109 | xfree (release->text); never executed: xfree (release->text); | 0 | ||||||||||||||||||
110 | - | |||||||||||||||||||
111 | xfree (release); | - | ||||||||||||||||||
112 | } never executed: end of block | 0 | ||||||||||||||||||
113 | } never executed: end of block | 0 | ||||||||||||||||||
114 | - | |||||||||||||||||||
115 | /* Free the existing undo list. */ | - | ||||||||||||||||||
116 | void | - | ||||||||||||||||||
117 | rl_free_undo_list (void) | - | ||||||||||||||||||
118 | { | - | ||||||||||||||||||
119 | UNDO_LIST *release, *orig_list; | - | ||||||||||||||||||
120 | - | |||||||||||||||||||
121 | orig_list = rl_undo_list; | - | ||||||||||||||||||
122 | _rl_free_undo_list (rl_undo_list); | - | ||||||||||||||||||
123 | rl_undo_list = (UNDO_LIST *)NULL; | - | ||||||||||||||||||
124 | _hs_replace_history_data (-1, (histdata_t *)orig_list, (histdata_t *)NULL); | - | ||||||||||||||||||
125 | } never executed: end of block | 0 | ||||||||||||||||||
126 | - | |||||||||||||||||||
127 | UNDO_LIST * | - | ||||||||||||||||||
128 | _rl_copy_undo_entry (UNDO_LIST *entry) | - | ||||||||||||||||||
129 | { | - | ||||||||||||||||||
130 | UNDO_LIST *new; | - | ||||||||||||||||||
131 | - | |||||||||||||||||||
132 | new = alloc_undo_entry (entry->what, entry->start, entry->end, (char *)NULL); | - | ||||||||||||||||||
133 | new->text = entry->text ? savestring (entry->text) : 0;
| 0 | ||||||||||||||||||
134 | return new; never executed: return new; | 0 | ||||||||||||||||||
135 | } | - | ||||||||||||||||||
136 | - | |||||||||||||||||||
137 | UNDO_LIST * | - | ||||||||||||||||||
138 | _rl_copy_undo_list (UNDO_LIST *head) | - | ||||||||||||||||||
139 | { | - | ||||||||||||||||||
140 | UNDO_LIST *list, *new, *roving, *c; | - | ||||||||||||||||||
141 | - | |||||||||||||||||||
142 | if (head == 0)
| 0 | ||||||||||||||||||
143 | return head; never executed: return head; | 0 | ||||||||||||||||||
144 | - | |||||||||||||||||||
145 | list = head; | - | ||||||||||||||||||
146 | new = 0; | - | ||||||||||||||||||
147 | while (list)
| 0 | ||||||||||||||||||
148 | { | - | ||||||||||||||||||
149 | c = _rl_copy_undo_entry (list); | - | ||||||||||||||||||
150 | if (new == 0)
| 0 | ||||||||||||||||||
151 | roving = new = c; never executed: roving = new = c; | 0 | ||||||||||||||||||
152 | else | - | ||||||||||||||||||
153 | { | - | ||||||||||||||||||
154 | roving->next = c; | - | ||||||||||||||||||
155 | roving = roving->next; | - | ||||||||||||||||||
156 | } never executed: end of block | 0 | ||||||||||||||||||
157 | list = list->next; | - | ||||||||||||||||||
158 | } never executed: end of block | 0 | ||||||||||||||||||
159 | - | |||||||||||||||||||
160 | roving->next = 0; | - | ||||||||||||||||||
161 | return new; never executed: return new; | 0 | ||||||||||||||||||
162 | } | - | ||||||||||||||||||
163 | - | |||||||||||||||||||
164 | /* Undo the next thing in the list. Return 0 if there | - | ||||||||||||||||||
165 | is nothing to undo, or non-zero if there was. */ | - | ||||||||||||||||||
166 | int | - | ||||||||||||||||||
167 | rl_do_undo (void) | - | ||||||||||||||||||
168 | { | - | ||||||||||||||||||
169 | UNDO_LIST *release; | - | ||||||||||||||||||
170 | int waiting_for_begin, start, end; | - | ||||||||||||||||||
171 | HIST_ENTRY *cur, *temp; | - | ||||||||||||||||||
172 | - | |||||||||||||||||||
173 | #define TRANS(i) ((i) == -1 ? rl_point : ((i) == -2 ? rl_end : (i))) | - | ||||||||||||||||||
174 | - | |||||||||||||||||||
175 | start = end = waiting_for_begin = 0; | - | ||||||||||||||||||
176 | do | - | ||||||||||||||||||
177 | { | - | ||||||||||||||||||
178 | if (rl_undo_list == 0)
| 0 | ||||||||||||||||||
179 | return (0); never executed: return (0); | 0 | ||||||||||||||||||
180 | - | |||||||||||||||||||
181 | _rl_doing_an_undo = 1; | - | ||||||||||||||||||
182 | RL_SETSTATE(RL_STATE_UNDOING); | - | ||||||||||||||||||
183 | - | |||||||||||||||||||
184 | /* To better support vi-mode, a start or end value of -1 means | - | ||||||||||||||||||
185 | rl_point, and a value of -2 means rl_end. */ | - | ||||||||||||||||||
186 | if (rl_undo_list->what == UNDO_DELETE || rl_undo_list->what == UNDO_INSERT)
| 0 | ||||||||||||||||||
187 | { | - | ||||||||||||||||||
188 | start = TRANS (rl_undo_list->start);
| 0 | ||||||||||||||||||
189 | end = TRANS (rl_undo_list->end);
| 0 | ||||||||||||||||||
190 | } never executed: end of block | 0 | ||||||||||||||||||
191 | - | |||||||||||||||||||
192 | switch (rl_undo_list->what) | - | ||||||||||||||||||
193 | { | - | ||||||||||||||||||
194 | /* Undoing deletes means inserting some text. */ | - | ||||||||||||||||||
195 | case UNDO_DELETE: never executed: case UNDO_DELETE: | 0 | ||||||||||||||||||
196 | rl_point = start; | - | ||||||||||||||||||
197 | rl_insert_text (rl_undo_list->text); | - | ||||||||||||||||||
198 | xfree (rl_undo_list->text); | - | ||||||||||||||||||
199 | break; never executed: break; | 0 | ||||||||||||||||||
200 | - | |||||||||||||||||||
201 | /* Undoing inserts means deleting some text. */ | - | ||||||||||||||||||
202 | case UNDO_INSERT: never executed: case UNDO_INSERT: | 0 | ||||||||||||||||||
203 | rl_delete_text (start, end); | - | ||||||||||||||||||
204 | rl_point = start; | - | ||||||||||||||||||
205 | break; never executed: break; | 0 | ||||||||||||||||||
206 | - | |||||||||||||||||||
207 | /* Undoing an END means undoing everything 'til we get to a BEGIN. */ | - | ||||||||||||||||||
208 | case UNDO_END: never executed: case UNDO_END: | 0 | ||||||||||||||||||
209 | waiting_for_begin++; | - | ||||||||||||||||||
210 | break; never executed: break; | 0 | ||||||||||||||||||
211 | - | |||||||||||||||||||
212 | /* Undoing a BEGIN means that we are done with this group. */ | - | ||||||||||||||||||
213 | case UNDO_BEGIN: never executed: case UNDO_BEGIN: | 0 | ||||||||||||||||||
214 | if (waiting_for_begin)
| 0 | ||||||||||||||||||
215 | waiting_for_begin--; never executed: waiting_for_begin--; | 0 | ||||||||||||||||||
216 | else | - | ||||||||||||||||||
217 | rl_ding (); never executed: rl_ding (); | 0 | ||||||||||||||||||
218 | break; never executed: break; | 0 | ||||||||||||||||||
219 | } | - | ||||||||||||||||||
220 | - | |||||||||||||||||||
221 | _rl_doing_an_undo = 0; | - | ||||||||||||||||||
222 | RL_UNSETSTATE(RL_STATE_UNDOING); | - | ||||||||||||||||||
223 | - | |||||||||||||||||||
224 | release = rl_undo_list; | - | ||||||||||||||||||
225 | rl_undo_list = rl_undo_list->next; | - | ||||||||||||||||||
226 | - | |||||||||||||||||||
227 | /* If we are editing a history entry, make sure the change is replicated | - | ||||||||||||||||||
228 | in the history entry's line */ | - | ||||||||||||||||||
229 | cur = current_history (); | - | ||||||||||||||||||
230 | if (cur && cur->data && (UNDO_LIST *)cur->data == release)
| 0 | ||||||||||||||||||
231 | { | - | ||||||||||||||||||
232 | temp = replace_history_entry (where_history (), rl_line_buffer, (histdata_t)rl_undo_list); | - | ||||||||||||||||||
233 | xfree (temp->line); | - | ||||||||||||||||||
234 | FREE (temp->timestamp); never executed: free (temp->timestamp);
| 0 | ||||||||||||||||||
235 | xfree (temp); | - | ||||||||||||||||||
236 | } never executed: end of block | 0 | ||||||||||||||||||
237 | - | |||||||||||||||||||
238 | _hs_replace_history_data (-1, (histdata_t *)release, (histdata_t *)rl_undo_list); | - | ||||||||||||||||||
239 | - | |||||||||||||||||||
240 | xfree (release); | - | ||||||||||||||||||
241 | } never executed: end of block | 0 | ||||||||||||||||||
242 | while (waiting_for_begin);
| 0 | ||||||||||||||||||
243 | - | |||||||||||||||||||
244 | return (1); never executed: return (1); | 0 | ||||||||||||||||||
245 | } | - | ||||||||||||||||||
246 | #undef TRANS | - | ||||||||||||||||||
247 | - | |||||||||||||||||||
248 | int | - | ||||||||||||||||||
249 | _rl_fix_last_undo_of_type (int type, int start, int end) | - | ||||||||||||||||||
250 | { | - | ||||||||||||||||||
251 | UNDO_LIST *rl; | - | ||||||||||||||||||
252 | - | |||||||||||||||||||
253 | for (rl = rl_undo_list; rl; rl = rl->next)
| 0 | ||||||||||||||||||
254 | { | - | ||||||||||||||||||
255 | if (rl->what == type)
| 0 | ||||||||||||||||||
256 | { | - | ||||||||||||||||||
257 | rl->start = start; | - | ||||||||||||||||||
258 | rl->end = end; | - | ||||||||||||||||||
259 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
260 | } | - | ||||||||||||||||||
261 | } never executed: end of block | 0 | ||||||||||||||||||
262 | return 1; never executed: return 1; | 0 | ||||||||||||||||||
263 | } | - | ||||||||||||||||||
264 | - | |||||||||||||||||||
265 | /* Begin a group. Subsequent undos are undone as an atomic operation. */ | - | ||||||||||||||||||
266 | int | - | ||||||||||||||||||
267 | rl_begin_undo_group (void) | - | ||||||||||||||||||
268 | { | - | ||||||||||||||||||
269 | rl_add_undo (UNDO_BEGIN, 0, 0, 0); | - | ||||||||||||||||||
270 | _rl_undo_group_level++; | - | ||||||||||||||||||
271 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
272 | } | - | ||||||||||||||||||
273 | - | |||||||||||||||||||
274 | /* End an undo group started with rl_begin_undo_group (). */ | - | ||||||||||||||||||
275 | int | - | ||||||||||||||||||
276 | rl_end_undo_group (void) | - | ||||||||||||||||||
277 | { | - | ||||||||||||||||||
278 | rl_add_undo (UNDO_END, 0, 0, 0); | - | ||||||||||||||||||
279 | _rl_undo_group_level--; | - | ||||||||||||||||||
280 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
281 | } | - | ||||||||||||||||||
282 | - | |||||||||||||||||||
283 | /* Save an undo entry for the text from START to END. */ | - | ||||||||||||||||||
284 | int | - | ||||||||||||||||||
285 | rl_modifying (int start, int end) | - | ||||||||||||||||||
286 | { | - | ||||||||||||||||||
287 | if (start > end)
| 0 | ||||||||||||||||||
288 | { | - | ||||||||||||||||||
289 | SWAP (start, end); | - | ||||||||||||||||||
290 | } never executed: end of block | 0 | ||||||||||||||||||
291 | - | |||||||||||||||||||
292 | if (start != end)
| 0 | ||||||||||||||||||
293 | { | - | ||||||||||||||||||
294 | char *temp = rl_copy_text (start, end); | - | ||||||||||||||||||
295 | rl_begin_undo_group (); | - | ||||||||||||||||||
296 | rl_add_undo (UNDO_DELETE, start, end, temp); | - | ||||||||||||||||||
297 | rl_add_undo (UNDO_INSERT, start, end, (char *)NULL); | - | ||||||||||||||||||
298 | rl_end_undo_group (); | - | ||||||||||||||||||
299 | } never executed: end of block | 0 | ||||||||||||||||||
300 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
301 | } | - | ||||||||||||||||||
302 | - | |||||||||||||||||||
303 | /* Revert the current line to its previous state. */ | - | ||||||||||||||||||
304 | int | - | ||||||||||||||||||
305 | rl_revert_line (int count, int key) | - | ||||||||||||||||||
306 | { | - | ||||||||||||||||||
307 | if (rl_undo_list == 0)
| 0 | ||||||||||||||||||
308 | rl_ding (); never executed: rl_ding (); | 0 | ||||||||||||||||||
309 | else | - | ||||||||||||||||||
310 | { | - | ||||||||||||||||||
311 | while (rl_undo_list)
| 0 | ||||||||||||||||||
312 | rl_do_undo (); never executed: rl_do_undo (); | 0 | ||||||||||||||||||
313 | #if defined (VI_MODE) | - | ||||||||||||||||||
314 | if (rl_editing_mode == vi_mode)
| 0 | ||||||||||||||||||
315 | rl_point = rl_mark = 0; /* rl_end should be set correctly */ never executed: rl_point = rl_mark = 0; | 0 | ||||||||||||||||||
316 | #endif | - | ||||||||||||||||||
317 | } never executed: end of block | 0 | ||||||||||||||||||
318 | - | |||||||||||||||||||
319 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
320 | } | - | ||||||||||||||||||
321 | - | |||||||||||||||||||
322 | /* Do some undoing of things that were done. */ | - | ||||||||||||||||||
323 | int | - | ||||||||||||||||||
324 | rl_undo_command (int count, int key) | - | ||||||||||||||||||
325 | { | - | ||||||||||||||||||
326 | if (count < 0)
| 0 | ||||||||||||||||||
327 | return 0; /* Nothing to do. */ never executed: return 0; | 0 | ||||||||||||||||||
328 | - | |||||||||||||||||||
329 | while (count)
| 0 | ||||||||||||||||||
330 | { | - | ||||||||||||||||||
331 | if (rl_do_undo ())
| 0 | ||||||||||||||||||
332 | count--; never executed: count--; | 0 | ||||||||||||||||||
333 | else | - | ||||||||||||||||||
334 | { | - | ||||||||||||||||||
335 | rl_ding (); | - | ||||||||||||||||||
336 | break; never executed: break; | 0 | ||||||||||||||||||
337 | } | - | ||||||||||||||||||
338 | } | - | ||||||||||||||||||
339 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
340 | } | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |