Blame global.c

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