Imported Upstream version 1.12
[platform/upstream/ed.git] / global.c
1 /* global.c: global command routines for the ed line editor */
2 /*  GNU ed - The GNU line editor.
3     Copyright (C) 1993, 1994 Andrew Moore, Talke Studio
4     Copyright (C) 2006-2015 Antonio Diaz Diaz.
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "ed.h"
26
27
28 static const line_t **active_list = 0;  /* list of lines active in a global command */
29 static int active_size = 0;     /* size (in bytes) of active_list */
30 static int active_len = 0;      /* number of lines in active_list */
31 static int active_ptr = 0;      /* active_list index ( non-decreasing ) */
32 static int active_ndx = 0;      /* active_list index ( modulo active_last ) */
33
34
35 /* clear the global-active list */
36 void clear_active_list( void )
37   {
38   disable_interrupts();
39   if( active_list ) free( active_list );
40   active_list = 0;
41   active_size = active_len = active_ptr = active_ndx = 0;
42   enable_interrupts();
43   }
44
45
46 /* return the next global-active line node */
47 const line_t * next_active_node( void )
48   {
49   while( active_ptr < active_len && !active_list[active_ptr] )
50     ++active_ptr;
51   return ( active_ptr < active_len ) ? active_list[active_ptr++] : 0;
52   }
53
54
55 /* add a line node to the global-active list */
56 bool set_active_node( const line_t * const lp )
57   {
58   disable_interrupts();
59   if( !resize_line_buffer( &active_list, &active_size,
60                            ( active_len + 1 ) * sizeof (line_t **) ) )
61     {
62     show_strerror( 0, errno );
63     set_error_msg( "Memory exhausted" );
64     enable_interrupts();
65     return false;
66     }
67   enable_interrupts();
68   active_list[active_len++] = lp;
69   return true;
70   }
71
72
73 /* remove a range of lines from the global-active list */
74 void unset_active_nodes( const line_t * bp, const line_t * const ep )
75   {
76   while( bp != ep )
77     {
78     int i;
79     for( i = 0; i < active_len; ++i )
80       {
81       if( ++active_ndx >= active_len ) active_ndx = 0;
82       if( active_list[active_ndx] == bp )
83         { active_list[active_ndx] = 0; break; }
84       }
85     bp = bp->q_forw;
86     }
87   }