Alt-CWB-ambs

 view release on metacpan or  search on metacpan

script/cwb-regedit  view on Meta::CPAN

    while (match_command() =~ /^:([psa])$/) {
      get_block();
      die "Syntax error: arguments missing for :add $COMMAND\n"
        unless @ARGS > 0;
      add_attributes($1, @ARGS);
      $CHANGES++;
    }
  }
  elsif ($COMMAND eq ":del") {  # delete attribute(s)
    die "Syntax error: arguments missing for :del\n"
      unless @ARGS > 0;
    delete_attributes(@ARGS);
    $CHANGES++;
  }
  elsif ($COMMAND eq ":list") { # list attributes of specified type
    die "Syntax error: :list command must be followed by :p, :s or :a\n"
      unless @ARGS == 0 and match_command() =~ /^:[psa]$/;
    while (match_command() =~ /^:([psa])$/) {
      get_block();
      die "Syntax error: no arguments allowed for :list $COMMAND\n"
        unless @ARGS == 0;
      list_attributes($1);
    }
  }
  else {
    die "Command $COMMAND is not valid at this point. Aborted.\n";
  }
}

## write back if any changes have been made
if ($CHANGES > 0) {
  my $regfile = $reg->filename;
  # make a backup first
  system "cp", "-p", $regfile, "$regfile~"; # ignore errors (e.g. if we don't have permissions to make a copy)
  $reg->write;
  print "Changes saved to ", $reg->filename, "\n";
}


##
##  subroutines
##

## delete specified attributes (all of them must exist)
sub delete_attributes {
  my @atts = @_;
  my @missing = grep {not defined $reg->attribute($_)} @atts;
  die "Error in :del command: one or more attributes do not exist [@missing]. Aborted.\n"
    if @missing;
  print "Deleting attributes: @atts\n"; 
  foreach my $a (@atts) {
    $reg->delete_attribute($a);
  }
}

## add attributes of specified type
sub add_attributes {
  my $type = shift;
  my @atts = @_;
  if ($type eq "s") {           # automatic expansion of s-attribute specs in cwb-encode format
    my @expanded = ();
    foreach my $spec (@_) {
      if ($spec =~ /[:+]/) {
        die "Syntax error in s-attribute specifier '$spec'. Aborted.\n"
          unless $spec =~ /^([a-z0-9_-]+):([0-9])(\+([a-z0-9_+-]+))?$/;
        my $base = $1;
        my $recursion = $2;
        my @xmlatt = ($4) ? split /\+/, $4 : ();
        foreach my $i ("", 1 .. $recursion) {
          foreach my $ext ("", map {"_$_"} @xmlatt) {
            push @expanded, "$base$ext$i";
          }
        }
      }
      else {
        push @expanded, $spec;
      }
    }
    @atts = @expanded;
  }
  my @invalid = grep {not /^[a-z_][a-z0-9_-]*$/} @atts;
  die "Error in :add :$type command: invalid attribute name(s) [@invalid]. Aborted.\n"
    if @invalid;
  print "Adding $type-attributes: @atts\n";
  foreach my $a (@atts) {
    my $exist = $reg->attribute($a);
    if ($exist) {
      die "Error: attribute '$a' already declared as $exist-attribute. Aborted.\n"
        unless $exist eq $type;
      print "[$type-attribute '$a' already declared]\n";
    }
    $reg->add_attribute($a, $type);
  }
}

## list attributes of specified type
sub list_attributes {
  my $type = shift;
  my @atts = $reg->list_attributes($type);
  print "@atts\n";
}

## check whether next token looks like a command (without removing it)
sub is_command {
  return( @ARGV and $ARGV[0] =~ /^:/ );
}

## match next token as command (without removing it) and return normalised name ("" if not a command)
sub match_command {
  my $cmd = (@_) ? shift : $ARGV[0];
  my $norm = "";
  for ($cmd) {
    last unless defined $cmd;
    $norm = ":info"  if /^:info$/i;
    $norm = ":id"    if /^:id$/i;
    $norm = ":home"  if /^:h(ome)?$/i;
    $norm = ":name"  if /^:n(ame)?$/i;
    $norm = ":ifile" if /^:ifile$/i;
    $norm = ":prop"  if /^:pr(op)?$/i;
    $norm = ":add"   if /^:add$/i;
    $norm = lc($cmd) if /^:[psa]$/i;
    $norm = ":del"   if /^:d(el(ete)?)?$/i;
    $norm = ":list"   if /^:l(ist)?$/i;
  }
  return $norm;
}

## get a command block from the command line (command plus any number of non-command arguments)
## result is stored in global variables $COMMAND and @ARGS; returns FALSE at end of input 
sub get_block {
  $COMMAND = "";
  @ARGS = ();
  return 0
    unless @ARGV > 0;
  die "Syntax error: expected command, got '$ARGV[0]'\n"
    unless is_command();
  $COMMAND = match_command();
  die "Syntax error: unknown command '$ARGV[0]'\n"
    unless $COMMAND;



( run in 0.596 second using v1.01-cache-2.11-cpan-97f6503c9c8 )