summaryrefslogtreecommitdiff
path: root/global.c
blob: 2a71e58babaccef022a6a6f9dc236bc28c0c0e48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* global.c: global command routines for the ed line editor */
/*  GNU ed - The GNU line editor.
    Copyright (C) 1993, 1994 Andrew Moore, Talke Studio
    Copyright (C) 2006, 2007, 2008, 2009 Antonio Diaz Diaz.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ed.h"


static const line_t **active_list = 0;	/* list of lines active in a global command */
static int active_size = 0;	/* size (in bytes) of active_list */
static int active_len = 0;	/* number of lines in active_list */
static int active_ptr = 0;	/* active_list index ( non-decreasing ) */
static int active_ndx = 0;	/* active_list index ( modulo active_last ) */


/* clear the global-active list */
void clear_active_list( void )
  {
  disable_interrupts();
  if( active_list ) free( active_list );
  active_list = 0;
  active_size = active_len = active_ptr = active_ndx = 0;
  enable_interrupts();
  }


/* return the next global-active line node */
const line_t *next_active_node( void )
  {
  while( active_ptr < active_len && !active_list[active_ptr] )
    ++active_ptr;
  return ( active_ptr < active_len ) ? active_list[active_ptr++] : 0;
  }


/* add a line node to the global-active list */
char set_active_node( const line_t *lp )
  {
  disable_interrupts();
  if( !resize_line_buffer( &active_list, &active_size,
                           ( active_len + 1 ) * sizeof( line_t ** ) ) )
    {
    show_strerror( 0, errno ); set_error_msg( "Memory exhausted" );
    enable_interrupts();
    return 0;
    }
  enable_interrupts();
  active_list[active_len++] = lp;
  return 1;
  }


/* remove a range of lines from the global-active list */
void unset_active_nodes( const line_t *np, const line_t *mp )
  {
  const line_t *lp = np;

  while( lp != mp )
    {
    int i;
    for( i = 0; i < active_len; ++i )
      {
      if( ++active_ndx >= active_len ) active_ndx = 0;
      if( active_list[active_ndx] == lp )
        { active_list[active_ndx] = 0; break; }
      }
    lp = lp->q_forw;
    }
  }