home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / sbin / cups-genppdconfig.5.0 < prev    next >
Encoding:
Text File  |  2006-05-03  |  13.4 KB  |  477 lines

  1. #! /usr/bin/perl -w
  2. # $Id: cups-genppdconfig.in,v 1.12 2005/08/02 20:27:31 rleigh Exp $
  3. # A user-friendly dialog-based wrapper for cups-genppd(8).
  4. # Copyright (C) 2002 Roger Leigh <rleigh@debian.org>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  
  20. use strict;
  21. use File::Basename;
  22. use File::Find;
  23. use File::Temp qw(tempfile unlink0);
  24. use IO::Handle;
  25. use Getopt::Std;
  26. use POSIX;
  27.  
  28. sub init_data();
  29. sub init_defaults();
  30. sub main_menu();
  31. sub display_help;
  32. sub choose_printers;
  33. sub choose_languages;
  34. sub choose_location;
  35. sub create_ppds;
  36. sub create_dir($);
  37. sub dialog_read($$);
  38. sub dialog_read_list (\%\@$$);
  39.  
  40. my $DIALOG = "";                        # version of dialog to call
  41. my $BACKTITLE = "Gutenprint CUPS PPD creation"; # dialog screen title
  42. my %printers;                                   # master list of printers
  43. my %languages;                                  # master list of languages
  44. my @used_printers;                              # printer PPDs on system
  45. my @used_languages;                             # languages used on system
  46. my @chosen_printers = ();                       # chosen printers
  47. my @chosen_languages = ();                      # chosen languages
  48. my $version = "5.0";
  49. my $chosen_location = "/usr/share/ppd/gutenprint/$version";
  50.                                                 # chosen PPD prefix
  51. my $silent = 0;                                 # no dialog
  52.  
  53.  
  54. # Set chosen_location from command-line.
  55. our $opt_d;
  56. our $opt_u;
  57. getopts('d:u');
  58. if ($opt_d) {
  59.     $chosen_location = create_dir($opt_d);
  60. }
  61.  
  62. # Initialise everything
  63. init_data();
  64. init_defaults();
  65.  
  66. # Run non-interactively if `-u' was specified.
  67. if ( $opt_u ) {
  68.     $silent = 1;
  69.     create_ppds;
  70.     exit 0;
  71. }
  72.  
  73. while (my $option = main_menu()) { # Display main menu and run selection
  74.     if ($option eq "Help") {
  75.     display_help();
  76.     } elsif ($option eq "Printers") {
  77.     choose_printers();
  78.     } elsif ($option eq "Languages") {
  79.     choose_languages();
  80.     } elsif ($option eq "Directory") {
  81.     choose_location();
  82.     } elsif ($option eq "Create") {
  83.     create_ppds();
  84.     } elsif ($option eq "Exit") {
  85.     exit 0;
  86.     } else {
  87.     die "Invalid menu option: $option";
  88.     }
  89. }
  90.  
  91. exit 0;
  92.  
  93.  
  94. #
  95. # init_data() - Populate master printer and language hashes.
  96. #
  97. sub init_data() {
  98.     my $model;
  99.     my $desc;
  100.     my $lang;
  101. # Get printer drivers and descriptions, then store in a hash.
  102.     open GENPPD, "cups-genppd.$version -M -v |" or die "can't fork cups-genppd.$version: $!";
  103.     while (<GENPPD>) {
  104.     ($model, $desc) = /([\w-]+)\W+(.*)/;
  105.     chomp ($model);
  106.     chomp ($desc);
  107.     $printers{$model} = $desc;
  108.     }
  109.     close GENPPD or die "can't close cups-genppd.$version pipe: $!";
  110. # Get available languages, then store in hash.
  111.     open GENPPD, "cups-genppd.$version -L |" or die "can't fork cups-genppd.$version: $!";
  112.     while (<GENPPD>) {
  113.     $lang = $_;
  114.     chomp ($lang);
  115.     $languages{$lang} = "(No description)";
  116.     }
  117.     $languages{"en"} = "US English";
  118.     close GENPPD or die "can't close cups-genppd.$version pipe: $!";
  119. # Set defaults
  120.     @chosen_languages = ("en");
  121. }
  122.  
  123.  
  124. #
  125. # init_defaults() - Get defaults from PPD files and directories.
  126. #
  127. sub init_defaults() {
  128.     # Find all PPD files that we could regenerate
  129.     my %found_ppds;
  130.     if (-d $chosen_location) {
  131.     find({wanted => \&find_printers}, $chosen_location);
  132.     foreach (@used_printers) {
  133.         my $tmp;
  134.         $tmp = basename($_);
  135.         chomp ($tmp);
  136.         $tmp =~ s/(^.*)\.ppd.*/$1/;
  137.         $found_ppds{$tmp} = "" if $printers{$tmp};
  138.     }
  139.     }
  140.     @chosen_printers = ();
  141.     foreach (sort keys %found_ppds) {
  142.     push @chosen_printers, $_;
  143.     }
  144.  
  145.     # Find all language directories that could be used
  146.     my %found_langs;
  147.     if (-d $chosen_location) {
  148.     find({wanted => \&find_languages}, $chosen_location);
  149.     foreach (@used_languages) {
  150.         my $tmp;
  151.         $tmp = basename($_);
  152.         chomp ($tmp);
  153.         $found_langs{$tmp} = "" if $languages{$tmp};
  154.     }
  155.     }
  156.     @chosen_languages = ();
  157.     foreach (sort keys %found_langs) {
  158.     push @chosen_languages, $_;
  159.     }
  160.     if (! @chosen_languages) {
  161.     push @chosen_languages, "en";
  162.     }
  163. }
  164.  
  165.  
  166. #
  167. # find-*() - callbacks for File::Find::find().
  168. #
  169. sub find_printers {
  170.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  171.  
  172.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  173.     -f _ &&
  174.     /^.*\.ppd.*\z/s && push @used_printers, $_;
  175. }
  176.  
  177. sub find_languages {
  178.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  179.  
  180.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  181.     -d _ && push @used_languages, $_;
  182. }
  183.  
  184.  
  185. # string
  186. # main_menu() - Display main menu.
  187. #               Return string containing selection.
  188. #
  189. sub main_menu() {
  190.     my $option;
  191.     my @menu_options;
  192.     my $menu_desc = << "END";
  193. Generate Gutenprint PPD files for use with CUPS.  This program is a user-friendly interface for cups-genppd(8).
  194.  
  195. Current PPD directory: $chosen_location
  196.  
  197. Hint: if the cursor keys cause problems, you may have more luck with +/- and TAB.
  198. END
  199.     @menu_options = (
  200.     [ "Help", "Display help text" ],
  201.     [ "Directory", "Choose PPD location" ],
  202.     [ "Printers", "Choose printers" ],
  203.     [ "Languages", "Choose languages" ],
  204.     [ "Create", "Create PPDs" ],
  205.     [ "Exit", "Exit the program" ]
  206.     );
  207.     my $dialog_options;
  208.     for my $i ( 0 .. $#menu_options) {
  209.     for my $j ( 0 .. $#{$menu_options[$i]}) {
  210.         $dialog_options .= "\"$menu_options[$i][$j]\" ";
  211.     }
  212.     }
  213.     while (defined($option = dialog_read("cups-genppdconfig", "--no-cancel --default-item Printers --menu \"$menu_desc\" 20 70 6 $dialog_options"))) {
  214.     chomp ($option);
  215.     return $option;
  216.     }
  217. }
  218.  
  219.  
  220. #
  221. # display_help() - Display help text.
  222. #
  223. sub display_help {
  224.     my $help_text = <<"END";
  225. cups-genppdconfig is a program to generate PPD files which enable the
  226. Gutenprint printer drivers to be used with CUPS, the Common UNIX
  227. Printing System.  A PPD file is a PostScript Printer Description,
  228. which describes the capabilities of a printer.  For each printer model
  229. that you wish to use, you will have to generate the corresponding PPD
  230. file.
  231.  
  232. There are three steps to generating the PPDs:
  233.  
  234.  
  235. [0. Directory]
  236.  
  237. The default base directory to create PPD files in is
  238. /usr/share/ppd/gutenprint, and this is displayed on the
  239. main menu.  Choose the "Directory" option to change this, but in
  240. almost every case the default should be used.  Don\'t alter the default
  241. unless you know what you are doing.
  242.  
  243.  
  244. 1. Printers
  245.  
  246. Choose the "Printers" menu option.  The dialog box shows a complete
  247. list of all the printers supported by Gutenprint.  Use the up and down
  248. cursor keys to move between the printers and SPACE to select the
  249. models you want.  Next, press ENTER to confirm the selections.
  250.  
  251. If no printers are chosen, then a PPD file will be created for *every*
  252. model.
  253.  
  254.  
  255. 2. Languages
  256.  
  257. PPD files can be produced in several languages.  Choose the
  258. "Languages" menu option and, as for the "Printers" menu, select the
  259. languages that you wish to use and press ENTER to confirm the
  260. selections.
  261.  
  262. Since a PPD file can only be translated into one language, selecting
  263. multiple languages is posible, but of limited usefulness (for each
  264. printer, a separate PPD file for each language will be produced).
  265.  
  266.  
  267. 3. Save the selections
  268.  
  269. Choose the "Save" menu option to generate the PPD files you requested.
  270. The files will be created in the default CUPS data directory
  271. \$cups_prefix/share/model/gutenprint.  Translations will be saved in
  272. subdirectories named according to the locale/language.
  273.  
  274.  
  275. Note that unselecting already selected entries in the Printer and
  276. Language Selection dialogs will *not* remove these from the
  277. filesystem; it will simply not cause them to be generated when you
  278. choose "Save".  To remove a printer, delete the PPD file from each
  279. language directory it appears in.  To remove a language, delete the
  280. directory named with the corresponding language code, and all its
  281. contents.
  282.  
  283. Once you have finished, choose the "Exit" menu option, to leave the
  284. program.  Note that your selections will be lost, so make sure you
  285. saved your selections first, if you wanted to keep them.
  286. END
  287.     my($HELPFILE, $helpfilename) = tempfile("cups-genppdconfig-helpXXXXXX",
  288.                         UNLINK => 1)
  289.     or die "can't open temporary help file";
  290.     print $HELPFILE "$help_text";
  291.     $HELPFILE->flush();
  292.     dialog_read("Help", "--textbox $helpfilename 18 76");
  293.     unlink0($HELPFILE, $helpfilename) or die "Error unlinking help file $helpfilename safely: $!";
  294.     close($HELPFILE) or die "can't close help file $helpfilename: $!";
  295.     return;
  296. }
  297.  
  298.  
  299. #
  300. # choose_printers() - Select printers from master list.
  301. #                     Default none (so create all printers).
  302. #
  303. sub choose_printers {
  304.     my $title = "Printer selection";
  305.     my $options = "--checklist \"Choose the printer models you wish to use with CUPS.\" 18 54 11";
  306.     dialog_read_list(%printers, @chosen_printers, $title, $options);
  307. }
  308.  
  309.  
  310. #
  311. # choose_languages() - Select languages from master list.
  312. #                      Default is US English (en).
  313. #
  314. sub choose_languages {
  315.     my $title = "Language selection";
  316.     my $options = "--checklist \"Choose the languages you wish to use with CUPS.\" 18 54 11";
  317.     dialog_read_list(%languages, @chosen_languages, $title, $options);
  318. }
  319.  
  320.  
  321. #
  322. # choosen_location() - Select PPD prefix directory and create it if
  323. #                      not present.
  324. #
  325. sub choose_location {
  326.     my $location;
  327.     $location = dialog_read("Location selection",
  328.                 "--inputbox \"Choose a directory to create the PPD files in.\" 8 61 $chosen_location");
  329.     if (!defined($location)) {
  330.     $location = "";
  331.     }
  332.     $chosen_location = create_dir($location); # make sure directory exists
  333.     init_defaults; # use new location to get default selections
  334. }
  335.  
  336.  
  337. #
  338. # create_dir($dir) - Create named directory.
  339. #                    $dir will have excess `/'s pruned.
  340. #
  341. sub create_dir ($) {
  342.     my $location = $_[0];
  343.     my $dir;
  344.     my $count = 0;
  345.     if ($location =~ m/^\//) {
  346.     $dir = "/";
  347.     }
  348.     foreach (split /\//, $location) {
  349.     if ($_ eq "") {
  350.         next;
  351.     }
  352.     if ($count == 1) {
  353.         $dir .= "/";
  354.     }
  355.     $count = 1;
  356.     $dir .= $_;
  357.     if (!-d $dir) # directory does not exist, so create it
  358.     {
  359.         mkdir $dir || die "can't create directory \`$dir\': $!";
  360.     }
  361.     }
  362.     return $dir;
  363. }
  364.  
  365.  
  366. #
  367. # create_ppds() - Create PPD files.
  368. #
  369. sub create_ppds {
  370.     create_dir($chosen_location); # make the destination directory
  371.     my $total = scalar(@chosen_printers);
  372.     my $printers;
  373.     my $count;
  374.     my $language;
  375.     my $percent;
  376.     my $file;
  377.     if (!@chosen_printers) { # calculate total files for guage
  378.     $total = scalar(keys(%printers));
  379.     }
  380.     $total = $total * scalar(@chosen_languages);
  381.     if (@chosen_printers) { # construct printer list for dialog
  382.     foreach (@chosen_printers) {
  383.         $printers .= "$_ ";
  384.     }
  385.     } else {
  386.     $printers = "";
  387.     }
  388.     if (! $silent) {
  389.     open DIALOG, "| $DIALOG --sleep 2 --backtitle \"$BACKTITLE\" --title \"Creating PPD files\" --guage \"Language: \nPPD files: \" 10 72 0"
  390.         or die "can't fork dialog: $!";
  391.     }
  392.     $count = 0;
  393.     foreach $language (@chosen_languages) { # loop through languages
  394.     open GENPPD,
  395.     "LANGUAGE=$language cups-genppd.$version -v -p $chosen_location/$language $printers 2>&1 |"
  396.         or die "can't fork cups-genppd: $!";
  397.     $file = "";
  398.     while ( defined($file = <GENPPD>)) { # dump genppd stats into guage
  399.         chomp($file);
  400.         $count++;
  401.         $percent = int (($count/$total)*100);
  402.         if ($percent > 100) {
  403.         $percent = 100;
  404.         }
  405.         if (! $silent) {
  406.         print DIALOG "$percent\n";
  407.         print DIALOG "XXX\nLanguage: $language\nPPD files: $count/$total\n\n$file\nXXX\n";
  408.         DIALOG->flush();
  409.         } else {
  410.         print "$file\n";
  411.         STDOUT->flush();
  412.         }
  413.     }
  414.     close GENPPD or die "can't close cups-genppd pipe: $!";
  415.     }
  416.     if (! $silent) {
  417.     print DIALOG "100\nXXX\nLanguage: \nPPD files: $total/$total\n\nCompleted\nXXX\n";
  418.     close DIALOG or die "can't close dialog pipe";
  419.     }
  420. }
  421.  
  422.  
  423. # scalar
  424. # dialog($title, $command) - Create a dialog.
  425. #                            Returns dialog results.
  426. sub dialog_read ($$) {
  427.     my($title, $command) = @_;
  428.     my $result = ""; # must not be undefined, just empty
  429.     my $status;
  430.     my $line;
  431.     open DIALOG, "$DIALOG --backtitle \"$BACKTITLE\" --title \"$title\" $command 2>&1 |";
  432.     while (defined($line = <DIALOG>)) {
  433.     $result .= $line;
  434.     }
  435.     close DIALOG or ($! == 0) or die "can't close dialog pipe: $!";
  436.     if ($? >> 8)
  437.     {
  438.     return undef;
  439.     }
  440.     return $result;
  441. }
  442.  
  443.  
  444. #
  445. # dialog_read_list(%masterlist
  446. #                  @chosenlist
  447. #                  $title
  448. #                  $dialog) - Construct list dialog, entries from %masterlist,
  449. #                             defaults from @chosenlist.
  450. #
  451. sub dialog_read_list (\%\@$$) {
  452.     my $masterlist = $_[0];
  453.     my $list = $_[1];
  454.     my $title = $_[2];
  455.     my $dialogoptions = $_[3];
  456.     my $tmplist;
  457.     my $dialoglist = "";
  458.     my $item;
  459.     my $selected;
  460. # Make a list for use with dialog.
  461.     foreach $item (sort keys %$masterlist) {
  462.     $selected = "off";
  463.     foreach (@$list) {
  464.         if ($item eq $_) {
  465.         $selected = "on";
  466.         last;
  467.         }
  468.     }
  469.     $dialoglist .= "$item \"$masterlist->{$item}\" $selected ";
  470.     }
  471.     $tmplist = dialog_read("$title", "$dialogoptions $dialoglist");
  472.     if (defined($tmplist)) {
  473.     $tmplist =~ s/\"//g;
  474.     @$list=split(/ /, $tmplist);
  475.     }
  476. }
  477.