Imported Upstream version 1.6
[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, 2007, 2008, 2009, 2010, 2011, 2012
5     Free Software Foundation, Inc.
6
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "ed.h"
27
28
29 static const line_t **active_list = 0;  /* list of lines active in a global command */
30 static int active_size = 0;     /* size (in bytes) of active_list */
31 static int active_len = 0;      /* number of lines in active_list */
32 static int active_ptr = 0;      /* active_list index ( non-decreasing ) */
33 static int active_ndx = 0;      /* active_list index ( modulo active_last ) */
34
35
36 /* clear the global-active list */
37 void clear_active_list( void )
38   {
39   disable_interrupts();
40   if( active_list ) free( active_list );
41   active_list = 0;
42   active_size = active_len = active_ptr = active_ndx = 0;
43   enable_interrupts();
44   }
45
46
47 /* return the next global-active line node */
48 const line_t * next_active_node( void )
49   {
50   while( active_ptr < active_len && !active_list[active_ptr] )
51     ++active_ptr;
52   return ( active_ptr < active_len ) ? active_list[active_ptr++] : 0;
53   }
54
55
56 /* add a line node to the global-active list */
57 bool set_active_node( const line_t * const lp )
58   {
59   disable_interrupts();
60   if( !resize_line_buffer( &active_list, &active_size,
61                            ( active_len + 1 ) * sizeof (line_t **) ) )
62     {
63     show_strerror( 0, errno ); 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   }