# # This is a PERL include-file, part of electronic # document distribution system for PSESCAPE-97 # # $Id: utils.pl,v 1.31 1997/02/20 00:31:03 steinhau Exp $ # $Source: /local/web/PSESCAPE-97/perl/RCS/utils.pl,v $ # # Author: Steinar Hauan # sub GetArrayIndex { local($str,@array) = @_; for ( $i = 0; $i <= $#array; $i++ ) { if ( "$str" eq "$array[$i]" ) { return ($i); } } return (-1); } sub GetListItem { local($index,@list) = @_; if ( $index < 0 || $index > $#list ) { return $UNKNOWN; } else { return ($list[$index]); } } sub GetDocStatus { local($docstatus) = @_; return (&GetListItem($docstat,@DOC_STATUS)); } sub GetDocType { local($doctype) = @_; return (&GetListItem($doctype,@DOC_TYPES)); } sub GetError { local($error) = @_; return (&GetListItem($error,@ERROR_TYPES)); } sub Debug { local($mesg) = @_; if ( $DEBUG > 0 ) { print STDERR "(Debug) $mesg\n"; } } # # the header table has the following structure: # header[0] = document titlebar # header[1] = general formatting statement # header[i] = local formatting info # header[i+1] = text # sub WriteComplexHead { local($FD,@header) = @_; local($line) = ""; print $FD "\n"; print $FD "$header[0]"; printf($FD "<%s>\n",$header[1]); for ( $i=2; $i <= $#header; $i += 2 ) { if ( $header[$i] ne '' ) { printf($FD "<%s> %s ", $header[$i],$header[$i+1],$header[$i]); } else { printf($FD "%s",$header[$i+1]); } } printf($FD "
\n",$header[1]); } sub IdentifyFile { local($fn) = @_; if ( ! -f $fn ){ return 0; } open(FD,"$FILE $fn |") || die "(FATAL) Unable to execute '$FILE'\n"; chop(local($line) = ); $line =~ s/^$fn:(\s*)//g; close(FD); return ($line); } sub FindFileFormat { local($fname) = @_; chop($fdata = `file $fname`); ($fn,$ftype) = split(/\t/,$fdata); if ( !grep(/PostScript document/,$ftype) ) { open(FD, "$fname") || die "(FATAL) Unknown error ($!)\n"; while() { chop; if ( /ENTER LANGUAGE/ ) { ($trash,$ftype) = split(/=/); } } close(FD); } return ($ftype); } sub AcceptFile { local($fn) = @_; local($type) = &IdentifyFile($fn); $_ = $type; if ( /PostScript/ || /PS Document/ || /HP PC Driver/ ) { return 1; } else { return 0; } } sub SafeMove { local($from,$tofile) = @_; &Debug("Moving\n\t'$from'\n\t-> '$tofile'"); if ( $tofile eq '' ) { print STDERR "\tTarget definition is void\n"; return 1; } if ( ! -f $from ) { print STDERR "File '$from' doesn't exist\n"; print STDERR "Unable to create target '$tofile'\n"; return 1; } if ( -f $tofile ) { print STDERR "Refused to overwrite '$tofile'\n"; print STDERR "Source file '$from' unchanged.\n"; return 1; } system("$CP $from $tofile"); if ( ! -f $tofile ) { print STDERR "Copy operation failed ($!)\n"; print STDERR "Source file '$from' unchanged.\n"; return 1; } system("$UNLINK $from"); return 0; } sub GetTime { local($tmark) = @_; ($hour,$min,$sec,$day,$month,$year) = (localtime($tmark))[2,1,0,3,4,5]; $month++; if ( $month<10){ $month=join("","0",$month); } if ( $day<10){ $day=join("","0",$day); } if ($hour<10){ $hour=join("","0",$hour); } if ($min<10){ $min=join("","0",$min); } if ($sec<10){ $sec=join("","0",$sec); } $date = sprintf("%s%s%s-%s:%s:%s",$day,$month,$year,$hour,$min,$sec); return ($date); } sub Dir2Num { local($dir) = @_; $dir =~ s/^$ARTICLE_DIR\///g; # remove prefix local($sess,$ref) = split(/\//,$dir); $sess =~ s/^SESSION_//; $ref =~ s/^REF_//; if ( $sess < 1 || $ref < 1 ) { return (-1); } local($gref) = 1000*$sess + $ref; return ($gref); } sub Num2Dir { local($gref) = @_; local($sess) = ($gref / 1000); # integer division local($ref) = ($gref % 1000); # integer remainder local($dir) = sprintf("%s/SESSION_%d/REF_%d", $ARTICLE_DIR,$sess,$ref); return ($dir); } sub GetCurrentDate { return &GetTime(time); } sub ReadConfig { local($ctrlfile) = @_; local(%data); open(ABSTRACT_FD,"$ctrlfile") || die "(FATAL) Error opening '$ctrlfile' ($!)\n"; while () { chop; foreach $tag (@TAGS) { if ( /^$tag:/ ) { ( $data{$tag} = $_ ) =~ s/^$tag:(\s*)//g; $data{$tag} =~ s/(\s*)$//g; } } } close(ABSTRACT_FD); local($errors) = 0; foreach $tag (@TAGS) { #&Debug("tag='$tag' data='$data{$tag}'"); if ( $data{$tag} eq '' && "$tag" ne "email" ) { $errors++; } } # &Debug("$errors errors parsing control-file"); return ($errors,%data); } sub ReadPaperConfig { local($ctrlfile) = @_; local(%data); open(PAPER_FD,"$ctrlfile") || die "(FATAL) Error opening '$ctrlfile' ($!)\n"; while () { chop; foreach $tag (@PAPER_TAGS) { if ( /^$tag:/ ) { ( $data{$tag} = $_ ) =~ s/^$tag:(\s*)//g; $data{$tag} =~ s/(\s*)$//g; } } } close(PAPER_FD); foreach $tag (@PAPER_TAGS) { if ( $data{$tag} eq '' ){ $data{$tag} = "NONE"; } } return (%data); } sub ReadFinalConfig { local($ctrlfile) = @_; local(%data); open(FINAL_FD,"$ctrlfile") || die "(FATAL) Error opening '$ctrlfile' ($!)\n"; while () { chop; foreach $tag (@FINAL_TAGS) { if ( /^$tag:/ ) { ( $data{$tag} = $_ ) =~ s/^$tag:(\s*)//g; $data{$tag} =~ s/(\s*)$//g; } } } close(FINAL_FD); foreach $tag (@FINAL_TAGS) { if ( $data{$tag} eq '' ){ $data{$tag} = "NONE"; } } return (%data); } sub WriteAdr { $myself = "$0"; if ( -l $myself ){ $myself = readlink($myself); } $mod_time = localtime((stat($myself))[9]); print "
pseadm\@kjemi.unit.no
\n"; print "Last modified: $mod_time\n"; } sub GetPaperList { local($gref); undef %paperlist; open(PL,"$FIND $ARTICLE_DIR -type d -print |"); while() { chop; next unless ( /SESSION\_/ ); next if ( -f "$_/IGNORE" || -f "$_/REJECT" ); $gref = &Dir2Num($_); if ( $gref > 1000 ) { $paperlist{$gref} = 1; } } close(PL); return (%paperlist); } sub GetAbstracts { chdir($ARTICLE_DIR) || die "(FATAL) Unable to use dir $ARTICLE_DIR ($!)"; local($session,$ref,$gref); local(@list); for $session (`ls -1`) { chop($session); next unless -d $session; next if ( "$session" eq "reject" ); for $ref (`ls -1tr $session`) { chop($ref); next unless -d ( "$session/$ref" ); local($dir) = join('/',$ARTICLE_DIR,$session,$ref); $gref = &Dir2Num($dir); local($ctrl) = sprintf("%s/abstract.ps\@control",$dir,$ref); if ( ! -f "$session/$ref/IGNORE" ) { $str = join(':',$gref,$dir,$ctrl); push(@list,"$str"); } } } return (@list); } sub SubstTemplate { local($fname,@subst) = @_; local($*,$fil,$a); $* = 1; # Enable multiline matching $fil = &ReadFile($fname); $fil =~ s/^\s*\#.*\n//g; # Remove comments for($a = 0; $a <= $#subst; $a += 2) { $fil =~ s/$subst[$a]/$subst[$a+1]/g; } return($fil); } sub ReadFile { local($file) = @_; local($/,$fil); undef $/; # To slurp the *whole* file. open(FIL, $file) || return(); $fil = ; close(FIL); return($fil); } sub FormatStr{ local($str,$len,$spaces) = @_; @words = split(/ /,$str); local($sep) = "\n"; for ($i=0; $i<$spaces; $i++){ $sep .= " "; } local($result) = ""; local($cumlen) = 0; $stop = 0; for $word (@words) { $cumlen += length($word); if ( $cumlen > $len ) { $result .= $sep; $cumlen = length($word); } if ( "$result" ne "" ){ $result .= " "; } $result .= $word; } return ($result); } sub FormatAdr { local($raw) = @_; local(@lines) = split(/,/,$raw); # convert country to uppercase $lines[$#lines] =~ y/a-z/A-Z/; local($form) = ""; for $l (@lines) { $l =~ s/^(\s+)//; $form .= "$l\n"; } return($form); } # perl requires this 1;