summaryrefslogtreecommitdiff
path: root/lib/sbin/ParamLatex.pl
blob: 9fa22fad74173a5da859132cfe1df274091dd93b (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
#!/usr/bin/perl -s

#########################
# ->> ParamLatex.pl <<- #
#########################################################################
#                                                                       #
#                      standard help function                           #
#                                                                       #
#########################################################################
if ($h || $help) {
   print "--> ParamLatex.pl <--\n";
   print "Options:\n";
   print "\t-th=        : (semi opt) thorn to process\n";
   print "\t-arr=       : (semi opt) arrangement to process\n";
   print "\t-processall : (opt) process all arrangements\n";
   print "\t-directory= : (opt) dir. of arrangements (default arrangements/)\n";
   print "\t-thornlist= : (opt) list specific thorns to process\n";
   print "\t-dump       : (opt) dumps output to screen, not in Latex\n";
   print "\t-scope=     : (opt) restricted/global/private/shares/all (default all)\n";
   print "\t-sort=      : (opt) by: scope, type, name (default name)\n";
   print "\t\t-reverse    : (opt) reverse whatever the sorting is\n";
   print "\t-grouping=  : (opt) file ouput grouping scope (bythorn/byarrangement/all)\n";
   print "\t-document   : (opt) creates a TeX document, not just a table\n";
   print "\t-width=     : (opt) fixed width of table (default 160mm)\n";
   print "\t-spacing=   : (opt) vertical spacing between elements (default 6mm)\n";
   print "\t-outdir=    : (opt) directory to dump output files, default is .\n";
   print "\t-section    : (opt) makes this a section of a greater document\n";
   print "\t-h/-help    : (opt) this screen\n";
   print "Example:\n";
   print "\tperl -s /lib/sbin/ParamLatex.pl -outfile=mytable.tex -width=8.5cm\n";
   exit 0;
}

#########################################################################
#                                                                       #
#  This program will take as input a thorn, and output a latex table    #
#  that contains the information in that thorns param.ccl file.         #
#                                                                       #
#########################################################################

##############
# REQUIRE(S) #
##############

$sbin_dir = "lib/sbin";
require "$sbin_dir/parameter_parser.pl";
require "$sbin_dir/CSTUtils.pl";

###########################
# COMMAND LINE VAR. CHECK #
###########################

if (! $width)   {$width="160mm";}
if (! $sort)   {$sort="name";}
if (! $spacing) {$spacing="6mm";}
if (((! $th) && (! $arr)) && (! defined $processall)) {
   die "\nNo -th= or -arr= (or -processall) specified, nothing to process!\n";
}
if (! $directory) {$directory="arrangements/";} 
if (! $grouping)  {$grouping="bythorn";}


##################
# INITIALIZATION #
##################

@valid_types = qw(restricted global private shared);

$start_directory=`pwd`;
chomp($start_directory);

if (! $outdir) {
    $outdir = "./";
} else {
   if ($outdir =~ /^\//) {
      if (! -d "$outdir") {
         mkdir($outdir, 0755);
         print STDERR "\nCreating directory: $outdir" if ($verbose);
      }
   } else {
      if (! -d "$start_directory/$outdir") {
         mkdir("$start_directory/$outdir", 0755);
         print STDERR "\nCreating directory: $start_directory/$outdir" if ($verbose);
      }
   }
   if (! ($outdir =~ /\/$/)) {
      $outdir .= "/";
   }
}

print STDERR "\nOutput directory is: $outdir" if ($verbose);

if (defined $thornlist) {
   &Read_ThornList($thornlist);
 }

#################################################
# FIND THORN(S)/ARRANGEMENT(S) AND CREATE LATEX #
#################################################

@arrangements = &Find_Directories($directory);

&StartDocument("MasterTable") if ((! $dump) && ($grouping eq "all"));

foreach $arrangement (sort @arrangements) {
   @thorns = &Find_Directories($directory . $arrangement);

   &StartDocument($arrangement) if ((! $dump) && ($grouping eq "byarrangement"));
   THORN: foreach $thorn (@thorns) 
   {
   #   print STDERR "\nTHORN: $arrangement $thorn";
      if (($processall) || (($thorn eq $th) || ($arr eq $arrangement))) 
      { 

         ## do not create a file for this thorn if it is not in the THORNLIST (if one is specified)
	 if ((defined $thornlist) && (! defined $thornlist{"$arrangement/$thorn"})) {
            next THORN;
	 }

         if (-e "$directory$arrangement/${thorn}/param.ccl") 
         {
            &StartDocument($thorn, $arrangement) if ((! $dump) && ($grouping eq "bythorn"));

            $$thorn{$thorn} = "${directory}${arrangement}/${thorn}";
            %parameter_database = &create_parameter_database(%$thorn);
            &ReadLatexDatabase(%parameter_database);
            &FormatTable;

            &EndDocument if ((! $dump) && ($grouping eq "bythorn"));

	    ###################################################
            # reset the variables in case they are used again #
            undef %alreadydone;

  	    foreach $group_name (@valid_types) {
#                print STDERR "\n\t\t-->$group_name";
      		foreach $table (@$group_name) {
 #                  print STDERR "[$table]";
		   undef %$table;
		}
	    }
            # END of reset #
            ################
         }       
      }
   }
   &EndDocument if ((! $dump) && ($grouping eq "byarrangement")); 
}

&EndDocument if ((! $dump) && ($grouping eq "all")); 

print "\n" if ($verbose);
#########################################################################
#                 END OF MAIN SECTION OF THE PROGRAM                    # 
#########################################################################

#########################################################################
#                     BEGINNING OF SUB-ROUTINES                         #
#########################################################################

#########################################################################
# ReadLatexDataBase                                                     #
#   Calls parameter_parser.pl, which will read in the param.ccl file    #
#   from a single thorn and return all data as a %hash table, which we  #
#   will then parse to put into our own %hash tables named according to #
#   the variable names.                                                 #
#                                                                       #
#   %(variable_name)  : any number of hashes created with their names   #
#                       being the variable names, they then have $keys  #
#                       (descriptions) with $values (well, values)      #
#                          (e.g.) $name{"default"} = "Cactus";          #
#   @global       \                                                     #
#   @restricted    \  -> arrays containing the names of the relative    #
#   @private       /     hashes that are contained within their scopes  #
#   @shared       /             (masterlist contained in @valid_types)  #
#########################################################################
sub ReadLatexDatabase
{
  my(%parameter_database) = @_;
  my($field);
  my(@temp) = ();
  my($temp) = "";
  my($name) = "";
  
  #######################
  # reinitialize values #
  foreach $typ (@valid_types) {
     while (defined pop(@$typ)) {pop @$typ;}
  }
                                 # where is global type???

  foreach $field (sort keys %parameter_database)
  {
      print STDERR "\n$field --> $parameter_database{$field}" if ($verbose);

      &Clean;
         #################
         # ADD TO SCOPES #
         #################
      if ($field =~ /\sPRIVATE\s/) {
	@private = split/ /, $parameter_database{$field};	
      } elsif ($field =~ /\sRESTRICTED\s/) {
        @restricted = split/ /, $parameter_database{$field};
      } elsif ($field =~ /\sSHARES\s+(\w+)\s+var/) {
        @temp = split/ /, $parameter_database{$field};
 
        foreach $var (@temp) {
           $var=~ tr/A-Z/a-z/;
           $$var{"shared"}=$1;
        }
        push @shared, @temp;
      } else {
        #####################
        # ADD VARIABLE HASH #
        #####################

        if ($field =~ /(.*?)\s(.*?)\s(.*)/) {
           $name = $2; 
           $field_des=$3;
           $temp = $1; 
        } else {
   	   #print STDERR "\n\nTHROWNING AWAY: $field $paramater_database{$field}"; 
        }

        $name =~ tr/A-Z/a-z/;
        $$name{$field_des}=$parameter_database{$field};

        $$name{"name"} = $name;

        $$name{"thorn"} = $temp;
      }
   } #-- foreach
   print "\n";

   ################################
   # ADD ELEMENT $$table{"scope"} #
   ################################

   foreach $group_name (@valid_types) 
   {
      foreach $table (@$group_name) 
      {
       #  print STDERR "\n\t\t!!!!!!!! $table";
         $table =~ tr/A-Z/a-z/;
      # print STDERR "\n$table";
         $$table{"scope"} = $group_name;
         if ($$table{"shared"}) {
            $$table{"scope"} .= " from " . $$table{"shared"};
         } 
      } 

   } 
   return $name;
} ## END :ReadLatexDatabase:

#########################################################################
# FormatTable                                                           #
#    Primary function to be used to format the LaTex output to whatever #
#    sorting or grouping mechanism specified by the user.               #
#########################################################################
sub FormatTable {
        # sort: scope, type, name
   my (@all);
   while (defined pop(@all)) {pop @all;}

   if ($sort eq "scope") {
      if ((defined $scope) && ($scope ne "all")) {  # only one scope to output
           if ($dump) {
               &Dump(sort @$scope);
            } else {
               &CreateLatexTable(sort @$scope);
            }
      } else {
         foreach $type (@valid_types) {
            if ($dump) {
               &Dump(sort @$type);
            } else {
               &CreateLatexTable(sort @$type);
            }
         }
      }
   } else {
      if ((defined $scope) && ($scope ne "all")) {   # only print one scope
         @all = @$scope;
      } else {                                       # print all scopes
         foreach $type (@valid_types) {
             push @all, @$type;
          }   
      }

      if ($sort eq "name") {
         @all = sort @all; 
      } else {
         @all = sort SortByType @all;
      }

      if ($dump) {
        &Dump(@all);
      } else {
        &CreateLatexTable(@all);
      }      
  }

    print "\n" if ($verbose || $dump);
} ## END :FormatTable:

#########################################################################
# CreateLatexTable                                                      #
#    Intermediary function in the process of creating a LaTex table that#
#    loops through an array (of name passed in) and outputs LaTex in    #
#    THE ORDER SPECIFIED IN THAT ARRAY.  So, there you have it, the     #
#    foundation for sorting. :)                                         #
#########################################################################
sub CreateLatexTable {
   my (@cur_group) = @_;

   foreach $table (@cur_group) 
   {
      $table =~ tr/A-Z/a-z/;

      if (lc($$table{"thorn"}) eq lc($thorn)) {
         if (! defined $alreadydone{"$thorn$table"}) { 
            &LatexTableElement;
            $alreadydone{"$thorn$table"} = 1;   # to try to only print each once.
          }
      } 
   }
} ## END :CreateLatexTable:


#########################################################################
# StartDocument                                                         #
#    Opens the file OUT with the name sent in to this function a .tex   #
#    extension in the working directory for output of LaTeX code.       #
#                                                                       #
#    If specified with the -document flag on the command line, this     #
#    function will also print basic LaTeX commands to start a document  #
#########################################################################
sub StartDocument {
   my ($group_name) = shift;
   my ($arrangement) = shift;

   die "Cannot group tables for output, internal error in &StartDocument" if (! defined $group_name);

   chdir ($start_directory);
   open(OUT, ">$outdir${arrangement}_${group_name}_par.tex") || die "cannot open $outdir${group_name}_par.tex for output: $!";
   $oldfilehandle = select OUT;

   if ($document) {
      print "\\documentclass[12pt,a4paper]\{article\} \n";
      print "\\begin\{document\} \n\n";
   } elsif ($section) {
      $group_name =~ s/\_/\\\_/g;
      print "\\section{Parameters} \n\n";
   }


} ## :StartDocument:

#########################################################################
# EndDocument                                                           #
#    Ends a LaTeX document (if -document) specified, and closes the OUT #
#    file, which is used to write the LaTeX table to.                   #
#########################################################################
sub EndDocument {
   if ($document) {
      print "\\end\{document\} \n";
   }
   close OUT;
   select $oldfilehandle;
} ## :EndDocument:

#########################################################################
# LatexTableElement                                                     #
#    Takes whatever table element is currently reffered to by $table    #
#    and prints it out into a LaTeX table.  Only nifty things it curr.  #
#    does is NOT print ranges for BOOLEAN and SHARED elements.          #
#########################################################################
sub LatexTableElement {
   $table =~ tr/A-Z/a-z/;
   my ($name) = $table;
   my ($description) = $$table{"description"};
   my ($default) = $$table{"default"};
   $default =~ s/\_/\\\_/g;

   $name =~ s/\_/\\\_/g;

   $description =~ s/\_/\\\_/g;
   
   # new addition 4/2001
   $description =~ s/\^/\\\^/g;

   print "$table{\"thorn\"}";
 
   $parawidth = $width;
   $parawidth =~ /(\d+)(.*)/;

   $parawidth = ($1 - 35) . $2;

   if (! (($$table{"description"} !~ /\w/) && (defined $$table{"shared"}))) {
      print "\\begin\{tabular*\}\{$width\}\{|c|c|c|\@\{\\extracolsep\{\\fill\}\}r|\} \\hline \n";
      print "\{\\bf Name\} & \{\\bf Default\} & \{\\bf Scope\} & \{\\bf Type\} \\\\ \n";
      print "\\hline \n";
      print "$name & $default & $$table{\"scope\"} & $$table{\"type\"} \\\\ \n";
      print "\\hline\\hline\n";
      print "\\multicolumn\{4\}\{|l|\}\{\\rule[-2mm]\{-2mm\}\{0.5cm\}\{\\bf Description\}~\\vline~ \n";
      print "\\parbox\{${parawidth}\}\{\\it $description\}\} \\\\ \n";
      print "\\hline \n";
   } else {
      print "\\begin\{tabular*\}\{$width\}\{|c|c|\@\{\\extracolsep\{\\fill\}\}r|\} \\hline \n";
      print "\{\\bf Name\} & \{\\bf Scope\} & \{\\bf Type\} \\\\ \n";
      print "\\hline \n";
      print "$name & $$table{\"scope\"} & $$table{\"type\"} \\\\ \n";
      print "\\hline \n";
   }

   print "\\end\{tabular*\} \n\n";

   if ((($$table{"type"} ne "BOOLEAN") && ! $$table{"shared"}) && ($$table{"ranges"} > 0)) {

      print "\\vspace\{1mm\} \n\n";

      print "\\begin\{tabular*\}\{$width\}\{|c|l@\{\\extracolsep\{\\fill\}\}r|\} \\hline \n";
      print "\{\\bf Range\} & & \\\\ \n";
      print "\\hline\\hline \n";

      for ($i=1; $i <= $$table{"ranges"}; $i++) {
         $tempvar =  $$table{"range $i description"};
         $tempvar =~  s/\_/\\\_/g;

         $tempvar2 =  $$table{"range $i range"};
         $tempvar2 =~  s/\_/\\\_/g;

         # new addition 4/2001
         $tempvar2 =~ s/\^/\\\^/g;
         $tempvar2 =~ s/\$/\\\$/g;

         print "$tempvar2 & $tempvar & \\\\ \\hline \n";
      }
      print "\\end\{tabular*\} \n\n";
   }

   print "\\vspace\{$spacing\} \n\n";
} ## END :LatexTableElement:

#########################################################################
# Find_Directories                                                      #
#    Finds the current directories that are not CVS stuff contained w/in#
#    the directory name that is sent to this function, used to find     #
#    thorns and arrangements in this program.                           #
#########################################################################
sub Find_Directories {
   my(@good_directories);
   chdir ("$start_directory") || die "cannot chdir to $start_directory: $!";

   chdir("$_[0]") || die "cannot change directory to $_[0] : $!";
   open(LS, "ls -p|");
   while(chomp($name = <LS>)) {
      if ((($name =~ /\/$/) && ($name ne "History/")) && ($name ne "CVS/"))  {
         $name =~ s#/##;
         push(@good_directories, $name);
      }
   }   
   close(LS);
   chdir ($start_directory);
   return (@good_directories);
} ## END :Find_Directories:

#########################################################################
# SortByType                                                            #
#    Sorts the ouput by "type" within their respective thorns, as we can#
#    have repetitive variable names within different thorns, currently  #
#    output is restricted to internal (within thorn) sorting.           #
#########################################################################
sub SortByType {
   if (lc($$a{"type"}) cmp lc($$b{"type"}) < 0) {
      return -1;
   } elsif (lc($$a{"type"}) cmp lc($$b{"type"}) > 0) {
      return 1;
   } else {
      return -1;
   }
} ## END :SortByType:

#########################################################################
# SortByName                                                            #
#    Sorts the ouput by "name" within their respective thorns, as we can#
#    have repetitive variable names within different thorns, currently  #
#    output is restricted to internal (within thorn) sorting.           #
#########################################################################
sub SortByName {
   my ($first) = $$a{"name"};
   my ($second) = $$b{"name"};

   if (lc($first) cmp lc($second) < 0) {
      print "\n$first : $second -1";
      return -1;
   } elsif (lc($$a{"name"}) cmp lc($$b{"name"}) > 0) {
      print "\n$$a{\"name\"} : $$b{\"name\"} 0";
      return 1;
   } else {
      print "\n$$a{\"name\"} : $$b{\"name\"} -1 (2)";
      return -1;
   }
} ## END :SortByName:

#########################################################################
# Dump                                                                  #
#    Function to dump output to the screen rather than to a .tex file   #
#    table, this does NOT create latex, simple provides an easy way to  #
#    view the variables from the command line without use of Latex.     #
#########################################################################
sub Dump {
  my (@cur_group) = @_;
 
  print "\n$thorn variables:\n";

  foreach $value (@cur_group) {
    if (lc($$value{"thorn"}) eq lc($thorn)) { 
      print "\t$value:\n";
      foreach $key (keys %$value) {
	  print "\t\t$key -> $$value{$key}\n";
      }
    }
  }
} ## END :Dump:

#########################################################################
# Clean                                                                 #
#    Function to perform any cleaning that may need to be done to       #
#    variables before they are put into a hash of their name.           #
#########################################################################
sub Clean {
} ## END :Clean:


##########################
# Reads in the thornlist #
##########################
sub Read_ThornList 
{
   my ($thornlist) = shift;

   chomp($directory);

   open (TL, "$thornlist") 
      || die "cannot open thornlist ($thornlist) for reading: $!";

   while (<TL>) 
   {
      s/(.*?)#.*/\1/;            # read up to the first "#"
      s/\s+//g;                  # replace any spaces with nothing
      if (/\w+/) {               
         $thornlist{$_} = 1;
      }
   }
   close TL;     
}