summaryrefslogtreecommitdiffstats
path: root/knetwortdeconf/backends/general.pl.in
blob: 4657487e7b6c62a3593e3440d544e83db42bd355 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#!/usr/bin/env perl
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-

# Common stuff for the ximian-setup-tools backends.
#
# Copyright (C) 2000-2001 Ximian, Inc.
#
# Authors: Hans Petter Jansson <hpj@ximian.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


$SCRIPTSDIR = "@scriptsdir@";
if ($SCRIPTSDIR =~ /^@scriptsdir[@]/)
{
    $SCRIPTSDIR = ".";
    $DOTIN = ".in";
}

require "$SCRIPTSDIR/report.pl$DOTIN";
require "$SCRIPTSDIR/platform.pl$DOTIN";
require "$SCRIPTSDIR/xml.pl$DOTIN";

eval "use Locale::gettext";
$eval_gettext = $@;
eval "use POSIX";
$eval_posix = $@;
eval "use Encode";
$eval_encode = $@;

$has_i18n = (($eval_gettext eq "") && ($eval_posix eq "") && ($eval_encode eq ""));

if ($has_i18n)
{
  # set up i18n stuff
  &setlocale (LC_MESSAGES, "");
  &bindtextdomain ("@GETTEXT_PACKAGE@", "@localedir@");

  # Big stupid hack, but it's the best I can do until
  # distros switch to perl's gettext 1.04...
  eval "&bind_textdomain_codeset (\"@GETTEXT_PACKAGE@\", \"UTF-8\")";
  &textdomain ("@GETTEXT_PACKAGE@");

  eval "sub _ { return gettext (shift); }";
}
else
{
  # fake the gettext calls
  eval "sub _ { return shift; }";
}

# --- Operation modifying variables --- #


# Variables are set to their default value, which may be overridden by user. Note
# that a $prefix of "" will cause the configurator to use '/' as the base path,
# and disables creation of directories and writing of previously non-existent
# files.

# We should get rid of all these globals.

$gst_name = "";       # Short name of tool.
# $gst_version = "";    # Version of tool - [major.minor.revision]. Deprecated: now in hash
#                         structure generated by &gst_init.
# $gst_operation = "";  # Major operation user wants to perform - [get | set | filter]. Same as gst_version.

$gst_prefix = "";
$gst_do_verbose = 0;
$gst_do_report = 0;

$gst_debug = 0;
$gst_do_immediate = 1;


# Location management stuff
$gst_location = "";
$gst_no_archive = 0;

sub gst_print_usage_synopsis
{
  my ($tool) = @_;
  my ($ops_syn, $i);
  my @ops = qw (get set filter);

  foreach $i (@ops)
  {
    $ops_syn .= "--$i | " if exists $ {$$tool{"directives"}}{$i};
  }
  
  print STDERR "Usage: $$tool{name}-conf <${ops_syn}--interface | --directive | --help | --version>\n";

  print STDERR " " x length $$tool{"name"};
  print STDERR "             [--disable-immediate] [--prefix <location>]\n";

  print STDERR " " x length $$tool{"name"};
  print STDERR "             [--progress] [--report] [--verbose]\n\n";
}

sub gst_print_usage_generic
{
  my ($tool) = @_;
  my (%usage, $i);
  my @ops = qw (get set filter);

  my $usage_generic_head =<< "end_of_usage_generic;";
       Major operations (specify one of these):

end_of_usage_generic;

  my $usage_generic_tail =<< "end_of_usage_generic;";
       -i --interface Shows the available backend directives for interactive mode,
                      in XML format.

                      Interactive mode is set when no -g, -s or -f arguments are
                      given.

       -d --directive <directive> Takes a \'name::arg1::arg2...::argN\' directive
                      value as comming from standard input in interactive mode.

       -h --help      Prints this page to standard error.

          --version   Prints version information to standard output.

       Modifiers (specify any combination of these):

          --platform  <name-ver>  Overrides the detection of your platform\'s
                      name and version, e.g. redhat-6.2. Use with care. See the
                      documentation for a full list of supported platforms.

          --disable-immediate  With --set, prevents the configurator from
                      running any commands that make immediate changes to
                      the system configuration. Use with --prefix to make a
                      dry run that won\'t affect your configuration.

                      With --get, suppresses running of non-vital external
                      programs that might take a long time to finish.

       -p --prefix <location>  Specifies a directory prefix where the
                      configuration is looked for or stored. When storing
                      (with --set), directories and files may be created.

          --progress  Prints machine-readable progress information to standard
                      output, before any XML, consisting of three-digit
                      percentages always starting with \'0\'.

          --report    Prints machine-readable diagnostic messages to standard
                      output, before any XML. Each message has a unique
                      three-digit ID. The report ends in a blank line.

       -v --verbose   Prints human-readable diagnostic messages to standard
                      error.
end_of_usage_generic;

  $usage{"get"} =<< "end_of_usage_generic;";
       -g --get       Prints the current configuration to standard output, as
                      a standalone XML document. The configuration is read from
                      the host\'s system config files.

end_of_usage_generic;
  $usage{"set"} =<< "end_of_usage_generic;";
       -s --set       Updates the current configuration from a standalone XML
                      document read from standard input. The format is the same 
                      as for the document generated with --get.

end_of_usage_generic;
  $usage{"filter"} =<< "end_of_usage_generic;";
       -f --filter    Reads XML configuration from standard input, parses it,
                      and writes the configurator\'s impression of it back to
                      standard output. Good for debugging and parsing tests.

end_of_usage_generic;

  print STDERR $usage_generic_head;

  foreach $i (@ops)
  {
    print STDERR $usage{$i} if exists $ {$$tool{"directives"}}{$i};
  }

  print STDERR $usage_generic_tail;
}

# if $exit_code is provided (ne undef), exit with that code at the end.
sub gst_print_usage
{
  my ($tool, $exit_code) = @_;

  &gst_print_usage_synopsis ($tool);
  print STDERR $$tool{"description"} . "\n";
  &gst_print_usage_generic ($tool);

  exit $exit_code if $exit_code ne undef;
}

sub gst_print_version
{
  my ($tool, $exit_code) = @_;

  print "$$tool{name} $$tool{version}\n";

  exit $exit_code if $exit_code ne undef;
}

# --- Initialization and finalization --- #


sub gst_set_operation
{
  my ($tool, $operation) = @_;

  if ($tool{"operation"} ne "")
  {
    print STDERR "Error: You may specify only one major operation.\n\n";
    &gst_print_usage ($tool, 1);
    exit (1);
  }

  $$tool{"operation"} = $operation;
}

sub gst_set_with_param
{
  my ($tool, $arg_name, $value) = @_;
  
  if ($$tool{$arg_name} ne "")
  {
    print STDERR "Error: You may specify --$arg_name only once.\n\n";
    &gst_print_usage ($tool, 1);
  }
  
  if ($value eq "")
  {
    print STDERR "Error: You must specify an argument to the --$arg_name option.\n\n";
    &gst_print_usage ($tool, 1);
  }
  
  $$tool{$arg_name} = $value;
}

sub gst_set_op_directive
{
  my ($tool, $directive) = @_;

  &gst_set_with_param ($tool, "directive", $directive);
  &gst_set_operation ($tool, "directive");
}

sub gst_set_prefix
{
  my ($tool, $prefix) = @_;
  
  &gst_set_with_param ($tool, "prefix", $prefix);
  $gst_prefix = $prefix;
}

sub gst_set_dist
{
  my ($tool, $dist) = @_;
  
  &gst_set_with_param ($tool, "platform", $dist);
  $gst_dist = $dist;
}

sub gst_set_location
{
  my ($tool, $location) = @_;

  &gst_set_with_param ($tool, "location", $location);
  $gst_location = $location;
}

sub gst_merge_std_directives
{
  my ($tool) = @_;
  my ($directives, $i);
  my %std_directives =
      (
# platforms directive to do later.       
       "platforms"    => [ \&gst_platform_list, [],
                           "Print XML showing platforms supported by backend." ],
       "platform_set" => [ \&gst_platform_set,    ["platform"],
                           "Force the selected platform. platform arg must be one of the listed in the" .
                           "reports." ],
       "interface"    => [ \&gst_interface_directive, [],
                           "Print XML showing backend capabilities." ],
       "end"          => [ \&gst_end_directive,   [],
                           "Finish gracefuly and exit with success." ]
       );

  $directives = $$tool{"directives"};
  # Standard directives may be overriden.
  foreach $i (keys %std_directives)
  {
    $$directives{$i} = $std_directives{$i} if !exists $$directives{$i};
  }
}

sub gst_is_tool
{
  my ($tool) = @_;

  if ((ref $tool eq "HASH") &&
      (exists $$tool{"is_tool"}) &&
      ($$tool{"is_tool"} == 1))
  {
    return 1;
  }

  return 0;
}

sub gst_init
{
  my ($name, $version, $description, $directives, @args) = @_;
  my (%tool, $arg);

  # print a CR for synchronysm with the frontend
  print "\n";

  # Set the output autoflush.
  $old_fh = select (STDOUT); $| = 1; select ($old_fh);
  $old_fh = select (STDERR); $| = 1; select ($old_fh);

  $tool{"is_tool"} = 1;

  # Set backend descriptors.

  $tool{"name"} = $gst_name = $name;
  $tool{"version"} = $version;
  $tool{"description"} = $description;
  $tool{"directives"} = $directives;
  
  &gst_merge_std_directives (\%tool);

  # Parse arguments.
  
  while ($arg = shift (@args))
  {
    if    ($arg eq "--get"       || $arg eq "-g") { &gst_set_operation (\%tool, "get"); }
    elsif ($arg eq "--set"       || $arg eq "-s") { &gst_set_operation (\%tool, "set"); }
    elsif ($arg eq "--filter"    || $arg eq "-f") { &gst_set_operation (\%tool, "filter"); }
    elsif ($arg eq "--directive" || $arg eq "-d") { &gst_set_op_directive (\%tool, shift @args); }
    elsif ($arg eq "--interface" || $arg eq "-i") { &gst_interface_print  (\%tool, 0); }
    elsif ($arg eq "--help"      || $arg eq "-h") { &gst_print_usage   (\%tool, 0); }
    elsif ($arg eq "--version")                   { &gst_print_version (\%tool, 0); }
    elsif ($arg eq "--prefix"    || $arg eq "-p") { &gst_set_prefix    (\%tool, shift @args); }
    elsif ($arg eq "--platform")                  { &gst_set_dist      (\%tool, shift @args); }
    elsif ($arg eq "--progress")                  { $tool{"progress"} = $gst_progress = 1; }
    elsif ($arg eq "--location")                  { &gst_set_location  (\%tool, shift @args); }
    elsif ($arg eq "--no-archive")                { $tool{"no_archive"} = $gst_no_archive = 1; }
    elsif ($arg eq "--debug")                     { $tool{"debug"} = $gst_debug = 1; }
    elsif ($arg eq "--verbose"   || $arg eq "-v")
    {
      $tool{"do_verbose"} = $gst_do_verbose = 1;
      &gst_report_set_threshold (99);
    }
    elsif ($arg eq "--report")
    {
      $tool{"do_report"} = $gst_do_report = 1;
      &gst_report_set_threshold (99);
    }
    else
    {
      print STDERR "Error: Unrecognized option '$arg'.\n\n";
      &gst_print_usage (\%tool, 1);
    }
  }
  
  # See if debug requested in env.
  
  $tool{"debug"} = $gst_debug = 1 if ($ENV{"SET_ME_UP_HARDER"});
  
  # Set up subsystems.

  &gst_platform_get_system (\%tool);
  &gst_platform_guess (\%tool) if !$tool{"platform"};
  &gst_report_begin ();

  return \%tool;
}

sub gst_terminate
{
  &gst_report_set_threshold (-1);
  &gst_debug_close_all ();
  exit (0);
}

sub gst_end_directive
{
  my ($tool) = @_;

  &gst_report_end ();
  &gst_xml_print_request_end ();
  &gst_terminate ();
}


sub gst_interface_print_comment
{
  my ($name, $directive) = @_;
  my %std_comments =
      ("get" =>
       "Prints the current configuration to standard output, as " .
       "a standalone XML document. The configuration is read from " .
       "the host\'s system config files.",
       
       "set" =>
       "Updates the current configuration from a standalone XML " .
       "document read from standard input. The format is the same " .
       "as for the document generated with --get.",

       "filter" =>
       "Reads XML configuration from standard input, parses it, " .
       "and writes the configurator\'s impression of it back to " .
       "standard output. Good for debugging and parsing tests."
       );

  $comment = $$directive[2];
  $comment = $std_comments{$name} if (exists $std_comments{$name});

  if ($comment)
  {
    &gst_xml_print_line ("<comment>");
    &gst_xml_print_line ($comment);
    &gst_xml_print_line ("</comment>");
  }
}

# if $exit_code is provided (ne undef), exit with that code at the end.
sub gst_interface_print
{
  my ($tool, $exit_code) = @_;
  my ($directives, $key);

  $directives = $$tool{"directives"};

  &gst_xml_print_begin ("interface");
  foreach $key (sort keys %$directives)
  {
    my $comment = $ {$$directives{$key}}[2];
    my @args = @{ $ {$$directives{$key}}[1]};
    my $arg;
    
    &gst_xml_container_enter ("directive");
    &gst_xml_print_line ("<name>$key</name>");
    &gst_interface_print_comment ($key, $$directives{$key});

    while ($arg = shift (@args))
    {
      if ($arg =~ /\*$/)
      {
        my $tmp = $arg;

        &gst_report ("directive_invalid", $key) if ($#args != -1);
        chop $tmp;
        &gst_xml_print_line ("<var-arguments>$tmp</var-arguments>");
      }
      else
      {
        &gst_xml_print_line ("<argument>$arg</argument>");
      }
    }

    &gst_xml_container_leave ();
  }
  &gst_xml_print_end ("interface");

  exit $exit_code if $exit_code ne undef;
}


sub gst_interface_directive
{
  my ($tool) = @_;

  &gst_report_end ();
  &gst_interface_print ($tool);
}


sub gst_directive_fail
{
  my (@report_args) = @_;
  
  &gst_report (@report_args);
  &gst_report_end ();
  &gst_xml_print_request_end ();
  &gst_debug_close_all ();
}

# This sepparates a line in args by the directive line format,
# doing the necessary escape sequence manipulations.
sub gst_directive_parse_line
{
  my ($line) = @_;
  my ($arg, @args);
  
  chomp $line;
  $line =~ s/\\\\/___escape\\___/g;
  $line =~ s/\\::/___escape2:___/g;
  @args = split ("::", $line);
  
  foreach $arg (@args)
  {
    $arg =~ s/___escape2:___/::/g;
    $arg =~ s/___escape\\___/\\/g;
  }

  return @args;
}

# Normal use for the direcives hash in the backends is:
#
# "name" => [ \&sub, [ "arg1", "arg2", "arg3",... "argN" ], "comment" ]
#
# name        name of the directive that will be used in interactive mode.
# sub         is the function that runs the directive.
# arg1...argN show the number of arguments that the function may use. The
#             name of the argument is used for documentation purposes for
#             the interfaces XML (dumped by the "interfaces" directive).
#             An argument ending with * means that 0 or more arguments
#             may be given.
# comment     documents the directive in a brief way, for the interface XML.
#
# Example:
#
# "install_font" => [ \&gst_font_install, [ "directory", "file", "morefiles*" ], "Installs fonts." ]
#
# This means that when an interactive mode directive is given, such as:
#
# install_font::/usr/share/fonts::/tmp/myfile::/tmp/myfile2
#
# the function gst_font_install will be called, with the tool structure, /usr/share/fonts,
# /tmp/myfile and /tmp/myfile2 as arguments. Directives with 1 or 0 arguments
# would be rejected, as we are requiring 2, and optionaly allowing more.
# Check enable_iface in network-conf.in for an example of a directive handler.
#
# The generated interface XML piece for this entry would be:
#
# <directive>
#  <name>gst_font_install</name>
#  <comment>
#  Installs fonts.
#  </comment>
#  <argument>directory</argument>
#  <argument>file</argument>
#  <var-arguments>morefiles</var-arguments>
# </directive>


sub gst_directive_run
{
  my ($tool, $line) = @_;
  my ($key, @args, $directives, $proc, $reqargs, $i);

  ($key, @args) = &gst_directive_parse_line ($line);
  $directives = $$tool{"directives"};

  &gst_report_begin ();

  if (!exists $$directives{$key})
  {
    &gst_directive_fail ("directive_unsup", $key);
    return;
  }

  $reqargs = [];
  foreach $i (@{$ {$$directives{$key}}[1]})
  {
    push @$reqargs, $i if not ($i =~ /\*$/);
  }
  
  if (scalar @args < scalar @$reqargs)
  {
    &gst_directive_fail ("directive_lowargs", $key, scalar (@$reqargs), join (',', $key, @args));
    return;
  }

  $reqargs = $ {$$directives{$key}}[1];
  if ((scalar @args != scalar @$reqargs) &&
      !($$reqargs[$#$reqargs] =~ /\*$/))
  {
    &gst_directive_fail ("directive_badargs", $key, scalar (@$reqargs), join (',', $key, @args));
    return;
  }

  &gst_report ("directive_run", $key, join (',', @args));

  $proc = $ {$$directives{$key}}[0];
  &$proc ($tool, @args);

  &gst_xml_print_request_end ();
  &gst_debug_close_all ();
}


sub gst_run
{
  my ($tool) = @_;
  my ($line);

  if ($$tool{"operation"} ne "directive")
  {
    my @stdops = qw (get set filter);
    my ($op);

    foreach $op (@stdops)
    {
      if ($$tool{"operation"} eq $op)
      {
        $$tool{"operation"} = "directive";
        $$tool{"directive"} = $op;
      }
    }
  }

  &gst_report_end ();

  if ($$tool{"directive"})
  {
    &gst_directive_run ($tool, $$tool{"directive"});
    &gst_terminate ();
  }

  while ($line = <STDIN>)
  {
    &gst_directive_run ($tool, $line);
  }
}

1;