Blame global.c

Packit 4e1bf9
/* global.c: global command routines for the ed line editor */
Packit 4e1bf9
/*  GNU ed - The GNU line editor.
Packit 4e1bf9
    Copyright (C) 1993, 1994 Andrew Moore, Talke Studio
Packit 4e1bf9
    Copyright (C) 2006-2017 Antonio Diaz Diaz.
Packit 4e1bf9
Packit 4e1bf9
    This program is free software: you can redistribute it and/or modify
Packit 4e1bf9
    it under the terms of the GNU General Public License as published by
Packit 4e1bf9
    the Free Software Foundation, either version 2 of the License, or
Packit 4e1bf9
    (at your option) any later version.
Packit 4e1bf9
Packit 4e1bf9
    This program is distributed in the hope that it will be useful,
Packit 4e1bf9
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4e1bf9
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4e1bf9
    GNU General Public License for more details.
Packit 4e1bf9
Packit 4e1bf9
    You should have received a copy of the GNU General Public License
Packit 4e1bf9
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 4e1bf9
*/
Packit 4e1bf9
Packit 4e1bf9
#include <errno.h>
Packit 4e1bf9
#include <stdio.h>
Packit 4e1bf9
#include <stdlib.h>
Packit 4e1bf9
#include <string.h>
Packit 4e1bf9
Packit 4e1bf9
#include "ed.h"
Packit 4e1bf9
Packit 4e1bf9
Packit 4e1bf9
static const line_t **active_list = 0;	/* list of lines active in a global command */
Packit 4e1bf9
static int active_size = 0;	/* size (in bytes) of active_list */
Packit 4e1bf9
static int active_len = 0;	/* number of lines in active_list */
Packit 4e1bf9
static int active_ptr = 0;	/* active_list index ( non-decreasing ) */
Packit 4e1bf9
static int active_ndx = 0;	/* active_list index ( modulo active_last ) */
Packit 4e1bf9
Packit 4e1bf9
Packit 4e1bf9
/* clear the global-active list */
Packit 4e1bf9
void clear_active_list( void )
Packit 4e1bf9
  {
Packit 4e1bf9
  disable_interrupts();
Packit 4e1bf9
  if( active_list ) free( active_list );
Packit 4e1bf9
  active_list = 0;
Packit 4e1bf9
  active_size = active_len = active_ptr = active_ndx = 0;
Packit 4e1bf9
  enable_interrupts();
Packit 4e1bf9
  }
Packit 4e1bf9
Packit 4e1bf9
Packit 4e1bf9
/* return the next global-active line node */
Packit 4e1bf9
const line_t * next_active_node( void )
Packit 4e1bf9
  {
Packit 4e1bf9
  while( active_ptr < active_len && !active_list[active_ptr] )
Packit 4e1bf9
    ++active_ptr;
Packit 4e1bf9
  return ( active_ptr < active_len ) ? active_list[active_ptr++] : 0;
Packit 4e1bf9
  }
Packit 4e1bf9
Packit 4e1bf9
Packit 4e1bf9
/* add a line node to the global-active list */
Packit 4e1bf9
bool set_active_node( const line_t * const lp )
Packit 4e1bf9
  {
Packit 4e1bf9
  disable_interrupts();
Packit 4e1bf9
  if( !resize_line_buffer( &active_list, &active_size,
Packit 4e1bf9
                           ( active_len + 1 ) * sizeof (line_t **) ) )
Packit 4e1bf9
    {
Packit 4e1bf9
    show_strerror( 0, errno );
Packit 4e1bf9
    set_error_msg( "Memory exhausted" );
Packit 4e1bf9
    enable_interrupts();
Packit 4e1bf9
    return false;
Packit 4e1bf9
    }
Packit 4e1bf9
  enable_interrupts();
Packit 4e1bf9
  active_list[active_len++] = lp;
Packit 4e1bf9
  return true;
Packit 4e1bf9
  }
Packit 4e1bf9
Packit 4e1bf9
Packit 4e1bf9
/* remove a range of lines from the global-active list */
Packit 4e1bf9
void unset_active_nodes( const line_t * bp, const line_t * const ep )
Packit 4e1bf9
  {
Packit 4e1bf9
  while( bp != ep )
Packit 4e1bf9
    {
Packit 4e1bf9
    int i;
Packit 4e1bf9
    for( i = 0; i < active_len; ++i )
Packit 4e1bf9
      {
Packit 4e1bf9
      if( ++active_ndx >= active_len ) active_ndx = 0;
Packit 4e1bf9
      if( active_list[active_ndx] == bp )
Packit 4e1bf9
        { active_list[active_ndx] = 0; break; }
Packit 4e1bf9
      }
Packit 4e1bf9
    bp = bp->q_forw;
Packit 4e1bf9
    }
Packit 4e1bf9
  }