-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheader.pl
executable file
·1577 lines (1398 loc) · 59.4 KB
/
header.pl
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#------------------------------------------------------------------------------#
# vi: set sw=4 ts=4 ai: ("set modeline" in ~/.exrc) #
#------------------------------------------------------------------------------#
# Program : header.pl #
# #
# Author : Ton Kersten The Netherlands #
# #
# Start date : 10 December 2004 Start time : 08:33 #
# #
# Description : Create a standard program header, like this one. #
# The file ~/.name.info is used for name and #
# address info. #
# It should contain in this order: #
# name <coder name> #
# firm <firm name> #
# adr1 <address line> #
# adr2 <address line> #
# adr3 <address line> #
# zipc <zipcode> #
# cntr <country> #
# tele Tel: <telephone number> #
# tfax Fax: <fax number> #
# mail <email address> #
# cpri <copyright owner> #
# If the file ~/.name.info cannot be found the values from the #
# %MYOWN hash are used. If this file does not contain all the #
# required lines, the defaults from the %MYOWN hash are used. #
# All fields should be there!! #
# #
# If no language is given, a default of "bash" is used. #
# #
# Remarks : Adjust the #
# %author, #
# %hbs, #
# %ends, #
# %delims #
# to your own wishes #
# #
# Updates : A lot. #
#------------------------------------------------------------------------------#
use strict;
use warnings;
use diagnostics;
use Env;
use Getopt::Long;
use POSIX qw(locale_h strftime); # Needed for locale support #
#------------------------------------------------------------------------------#
# V e r s i o n i n f o r m a t i o n #
#------------------------------------------------------------------------------#
# $Id:: header.pl 160 2012-09-27 08:49:27 tonk $: #
# $Revision:: 160 $: #
# $Author:: Ton Kersten <[email protected]> $: #
# $Date:: 2012-09-27 08:49:27 +0200 (Thu, 27 Sep 2012) $: #
# $Hash:: 3500a8aedcab734ba0874623fe23896c775f3508 (tonk) $: #
#------------------------------------------------------------------------------#
# E n d o f v e r s i o n i n f o r m a t i o n #
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
# Define the header version information #
#------------------------------------------------------------------------------#
my $HeaderVersion = "4.50";
#------------------------------------------------------------------------------#
# Make sure we have a correct locale #
#------------------------------------------------------------------------------#
$ENV{LC_ALL} = 'C'; # use the "C" locale #
setlocale(LC_ALL, "C");
#------------------------------------------------------------------------------#
# Find out if we do have a real operating system #
#------------------------------------------------------------------------------#
my $UNIX = ( ($^O !~ /win/i) && ($^O !~ /cygwin/i) );
#------------------------------------------------------------------------------#
# Get the current time and date #
#------------------------------------------------------------------------------#
my (undef, $min, $hour, $mday, $mon, $year, $wday, undef) = localtime();
$year += 1900;
my $serial = $year . $mon . $mday . "01";
#------------------------------------------------------------------------------#
# Set the default author and the other structures #
#------------------------------------------------------------------------------#
my %MYOWN =
( name => 'Ton Kersten',
firm => 'Ton Kersten',
adr1 => 'My street address',
adr2 => 'My home town',
adr3 => '',
zipc => 'My zip code',
cntr => 'My country',
tele => 'Tel: +31xxxxxxxxxx',
tfax => 'Fax: +31xxxxxxxxx',
mail => '[email protected]',
cpri => 'Ton Kersten',
);
my %author =
( name => '',
firm => '',
adr1 => '',
adr2 => '',
adr3 => '',
cntr => '',
zipc => '',
tele => '',
tfax => '',
mail => '',
cpri => '',
);
my @fields =
( "name", # Author name #
"firm", # Company name #
"adr1", # Company address #
"adr2", # Company address #
"adr3", # Company address #
"cntr", # Country #
"zipc", # Zip code #
"tele", # Telephone number #
"tfax", # Telefax number #
"mail", # E-mail address #
"cpri", # The copyright owner #
);
my %default =
( hlang => "en",
lang => "bash",
copy => "none",
short => "no",
simple => "no",
notrail => "0",
vcs => "git", # svn, git or none #
tab => 4,
width => 80,
);
#------------------------------------------------------------------------------#
# According to GNU, you should leave this alone #
#------------------------------------------------------------------------------#
my $me = "(c) 2001-$year by $MYOWN{name} ";
#------------------------------------------------------------------------------#
# Language definitions #
#------------------------------------------------------------------------------#
my %hbs = # Start the header with this #
( bash => "#!/bin/bash" ,
ksh => "#!/bin/ksh",
perl => "#!/usr/bin/perl" ,
rexx => "#!/usr/bin/rexx\n" .
"/* Comment needed for Rexx */",
php => "<?php",
html => "<html lang=\"nl\" xmlns=\"http://www.w3.org/1999/xhtml\">\n" .
"<body>\n" .
"<head>\n" ,
in => "#\n" .
"# {{ ansible_managed }}\n" .
"#\n\n",
init => "#!/bin/bash\n" .
"# chkconfig: 2345 85 15\n" .
"# description:",
python => "#!/usr/bin/env python\n" .
"# -*- coding: utf-8 -*-\n",
yml => "---",
);
my %ends = # End the header with this #
( latex => "%\n" .
"\\documentclass[a4paper,11pt,twoside,mctitle,dutch]{boek}\n" .
"\\usepackage[dutch,english]{babel}\n" .
"\\selectlanguage{dutch}\n" .
"\\parindent 0pt\n" .
"\\parskip \\baselineskip\n%\n%\n" .
"\\title{Document Skelet}\n\n" .
"\\author{\\small Author name \\\\\n" .
" \\small Company \\\\\n" .
" \\small Address line 1 \\\\\n" .
" \\small Address line 2 \\\\\n" .
" \\small Country \\\\\n" .
" \\small email: {\\small \\tt $MYOWN{mail}}}\n\n" .
"\\date{CONCEPT --- \\today\\ --- CONCEPT}\n\n" .
"\\begin{document}\n" .
"\\selectlanguage{dutch}\n" .
"\\maketitle\n\n\n\n\n" .
"\\end{document}" ,
ksh => "IAM=\"\$\{0##*/}\"\n" .
"CRD=\"\$( [[ \"\${0:0:2}\" = \"./\" ]] &&\n" .
" { printf \"\${PWD}/\${0#./}\"\n" .
" } || {\n" .
" printf \"\${0}\"\n".
" })\"\n" .
"CRD=\"\$\{CRD%/*}\"\n" .
"CUR=\"\$\{PWD}\"\n" ,
bash => "IAM=\"\$\{0##*/}\"\n" .
"CRD=\"\$( [[ \"\${0:0:2}\" = \"./\" ]] &&\n" .
" { printf \"\${PWD}/\${0#./}\"\n" .
" } || {\n" .
" printf \"\${0}\"\n".
" })\"\n" .
"CRD=\"\$\{CRD%/*}\"\n" .
"CUR=\"\$\{PWD}\"\n" ,
perl => "require 5;\n" .
"use strict;\n" .
"use warnings;\n" .
"use Carp;\n" .
"use Getopt::Std;\n",
atroff => ".LAN ned\n" .
".TYP dictaat\n" .
".TIT\n" .
".TIT\n" .
".TIT\n" .
".TIT \$Date\$\n" .
".FIR AT Computing\n" .
".VER \$Revision\$\n" .
".CPR $year\n" ,
html => "<title>" .
"</title>\n\n\n\n" .
"</head>\n" .
"</body>\n" .
"</html>\n" ,
c => "#include <stdio.h>\n" .
"#include <stdlib.h>\n" .
"#include <strings.h>\n" ,
in => "# " . "-"x70 . "\n" .
"# Copyright (c) 2011-{{ ansible_date_time.year }} by {{ name }} - {{ company }}\n" .
"# \$Id\$\n" .
"# " . "-"x70 . "\n" ,
spec => "Name: RPM Name\n" .
"Version: %(echo \${VERSION})\n" .
"Release: %(echo \${RELEASE})\n" .
"License: Distributable\n" .
"Group: Applications/Tools\n" .
"Packager: %{_packager}\n" .
"Vendor: %{_vendor}\n" .
"Source: %{name}-%{version}.tar.gz\n" .
"Summary: Short description\n" .
"Buildroot: %{_buildroot}\n" .
"Distribution: AT Computing RPM Manager\n" .
"BuildArch: i386\n" .
"\#AutoReqProv: no\n",
zone => "\$TTL 1H\n" .
"\$ORIGIN domain.tld.\n" .
"@ IN SOA host.domain.tld. adminmail.domain.tld. (\n" .
" $serial ; Serial\n" .
" 6H ; Refresh\n" .
" 2H ; Retry\n" .
" 7D ; Expire\n" .
" 1H ; Negative Cache TTL\n" .
" )",
python => "'''\nYour DocString Here\n\n'''\n\n" .
"import sys\n" .
"if __name__ == '__main__' :\n".
" main()\n",
);
my %delims =
( #--------------------------------------------------------------------------#
# Comment delimiter hash #
# Key: Language #
# List: comment start #
# comment dash #
# comment end #
# comment left for GIT/SVN comment #
# prelim comments (like #!/bin/bash for the bourne again shell) #
# #
# Newlines are allowed #
#--------------------------------------------------------------------------#
atroff => [".COM #", "-", "#" , $hbs{atroff} ],
bash => ["#" , "-", "#" , $hbs{bash} ],
c => ["/*" , "-", "*/" , $hbs{c} ],
config => ["#" , "-", "#" , $hbs{config} ],
go => ["/*" , "-", "*/" , $hbs{go} ],
html => ["<!-- " , "=", " -->", $hbs{html} ],
in => ["#" , "-", "#" , $hbs{in} ],
init => ["#" , "-", "#" , $hbs{init} ],
js => ["/*" , "-", "*/" , $hbs{js} ],
ksh => ["#" , "-", "#" , $hbs{ksh} ],
latex => ["%" , "-", "%" , $hbs{latex} ],
nagios => ["#" , "-", "#" , $hbs{bash} ],
pascal => ["(*" , "-", "*)" , $hbs{pascal} ],
perl => ["#" , "-", "#" , $hbs{perl} ],
php => ["/*" , "-", "*/" , $hbs{php} ],
puppet => ["#" , "-", "#" , $hbs{puppet} ],
python => ["#" , "-", "#" , $hbs{python} ],
rexx => ["/*" , "-", "*/" , $hbs{rexx} ],
spec => ["#" , "-", "#" , $hbs{spec} ],
tic => ["#" , "-", "#" , $hbs{tic} ],
vim => ["\"" , "-", "\"" , $hbs{vim} ],
yml => ["#" , "-", "#" , $hbs{yml} ],
zone => [";" , "-", ";" , $hbs{zone} ],
);
#------------------------------------------------------------------------------#
# Copyright message definition. #
#------------------------------------------------------------------------------#
my @gnu = (
'This program is free software; you can redistribute it and/or modify it',
'under the terms of the GNU 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.',
'',
'You should have received a copy of the GNU General Public License',
'along with this program; if not, write to the',
'Free Software Foundation, Inc.,',
' 59 Temple Place - Suite 330,',
' Boston, MA 02111-1307,',
' USA',
'',
'See the GNU General Public License for more details.',
'URL: http://www.gnu.org/copyleft/gpl.html.'
);
#------------------------------------------------------------------------------#
# Define global variables #
#------------------------------------------------------------------------------#
my $wd = $default{width}; # The comment width #
my $cs; # The comment start #
my $ce; # The comment end #
my $da; # The comment 'dashes' #
my $hashbang; # Possible prelim #!.... #
my $cl; # Comment like /*---- .... ----*/ #
my $el; # Comment like /* .... */ #
my $ts; # Tab stop setting #
my $nameinfo = $ENV{"HOME"} . "/.name.info";
my $vimcomment = "(\"set modeline\" in ~/.exrc)";
if ($UNIX != 1 )
{ my $nameinfo = "C:\\name.info";
my $vimcomment = "(\"set modeline\" in c:\\vimrc)";
}
my ($cop1, $cop2, $cop3, $progtext, $authtext, $startdate, $starttime, $desctext);
my ($cvsbegin, $cvseinde, $langset, $shellset, $shellexmp, $perlexmp, $default, $headline, $parmtext);
my ($updates, $prereqs, $exitcs, $functext, $setshell, $returns);
my ($rundir, $clobcom, $unsetcom, $errexitcom, $vcs);
my ($puppet1); # I know it looks weird, but this is needed to stop git.vi
my ($puppet2); # from expanding the Puppet tags.
#------------------------------------------------------------------------------#
# Define the language dependent strings #
#------------------------------------------------------------------------------#
sub setlang($)
{
my $lng = shift;
if ( $lng eq "nl" )
{
$cop1 = '(c) Copyright %4d van ';
$cop2 = 'Alle rechten voorbehouden. Gehele of gedeeltelijke reproductie is,';
$cop3 = 'zonder schiftelijke toestemming van de copyrighthouder, verboden.';
$default = "standaard";
$cvsbegin = "Versie informatie";
$cvseinde = "Einde versie informatie";
$progtext = "Programma :";
$authtext = "Auteur :";
$startdate = "Datum :";
$starttime = "Tijd :";
$desctext = "Omschrijving :";
$parmtext = "Parameters :";
$functext = "Functie :";
$returns = "Resultaat :";
$prereqs = "Voorwaarden :";
$exitcs = "Stop codes :";
$updates = "Aanpassingen : (Nog) geen";
$langset = "Zorg altijd voor goede taalinstellingen";
$shellset = "Bewaar de shell settings";
$setshell = "Set en unset de benodigde shell settings";
$perlexmp = "Perl functie voorbeeld";
$shellexmp = "Shell functie voorbeeld";
$headline = "Header gegenereerd door \"header " . $HeaderVersion . "\"";
$clobcom = "Overschrijf bestaande files, indien nodig";
$unsetcom = "Sta geen uninitialized variabelen toe";
$errexitcom = "Returncode controle uit";
$rundir = "Bepaal de programmanaam en de 'running directory'";
$puppet1 = "Dit bestand staat onder controle van Puppet";
$puppet2 = "Bewerk niet dit bestand maar het bestand op de Puppet Master";
}
else
{
$cop1 = '(c) Copyright %4d by ';
$cop2 = 'All rights are reserved. Reproduction in whole or in part is';
$cop3 = 'prohibited without the written consent of the copyrightowner';
$default = "default";
$cvsbegin = "Version information";
$cvseinde = "End of version information";
$progtext = "Program :";
$authtext = "Author :";
$startdate = "Date :";
$starttime = "Time :";
$desctext = "Description :";
$parmtext = "Parameters :";
$functext = "Function :";
$returns = "Returns :";
$prereqs = "Pre reqs :";
$exitcs = "Exit codes :";
$updates = "Updates : None (yet)";
$langset = "Make sure all language settings are correct";
$shellset = "Save the shell settings";
$setshell = "Set and unset the needed shell settings";
$perlexmp = "Perl function example";
$shellexmp = "Shell function example";
$headline = "Header generated by \"header " . $HeaderVersion . "\"";
$clobcom = "Overwrite existing files, if needed";
$unsetcom = "Don't allow uninitialized variables";
$errexitcom = "No returncode checking";
$rundir = "Determine the program name and the 'running directory'";
$puppet1 = "This file is under Puppet control";
$puppet2 = "Do NOT edit this file, but change the file on the Puppet Master";
}
}
#------------------------------------------------------------------------------#
# Here come the subroutines #
#------------------------------------------------------------------------------#
sub unknownlang($)
{ #--------------------------------------------------------------------------#
# Display an error message and stop #
# #
# Returns : nothing #
#--------------------------------------------------------------------------#
my $lang = shift;
#--------------------------------------------------------------------------#
# Undefined language requested. Show the known ones #
# and EXIT!! the program #
#--------------------------------------------------------------------------#
local $" = "\n\t";
my @known = sort keys %delims;
my $warnstring;
($warnstring = <<WARN) =~ s/^\s+//gm;
Unsupported language or illegal option: $lang
Supported languages:
@known
WARN
warn $warnstring;
exit(10);
}
sub right($$)
{ #--------------------------------------------------------------------------#
# Right align a string with spaces to the left #
# #
# Returns : right aligned text padded with blanks to length width #
# #
# Parameters: 1) width to align the text in #
# 2) the text to align #
#--------------------------------------------------------------------------#
my $width = shift;
my $line = shift;
#--------------------------------------------------------------------------#
# Determine the front spaces. #
#--------------------------------------------------------------------------#
my $front = $width - length($line);
return " " x $front . $line;
}
sub left($$)
{ #--------------------------------------------------------------------------#
# Pad a line with white space #
# #
# Returns : left aligned text padded with blanks to length width #
# #
# Parameters: 1) width to align the text in #
# 2) the text to align #
#--------------------------------------------------------------------------#
my $width = shift;
my $line = shift;
#--------------------------------------------------------------------------#
# Determine the front spaces. #
#--------------------------------------------------------------------------#
my $trail = $width - length($line);
return $line . " " x $trail;
}
sub centerchar($$$)
{ #--------------------------------------------------------------------------#
# Centre a string in the available space #
# #
# Returns : centered text padded with $chars to length width #
# #
# Parameters: 1) width to center the text in #
# 2) the text to align #
#--------------------------------------------------------------------------#
my $width = shift;
my $pad = shift;
my $line = shift;
#--------------------------------------------------------------------------#
# Solve problems with odd and even length strings #
#--------------------------------------------------------------------------#
use integer;
#--------------------------------------------------------------------------#
# Determine the front and end spaces. #
#--------------------------------------------------------------------------#
my $front = ($width - length($line)) / 2;
my $trail = $width - length($line) - $front;
return $pad x $front . $line . $pad x $trail;
}
sub center($$)
{ #--------------------------------------------------------------------------#
# Centre a string in the available space #
# #
# Returns : centered text padded with blanks to #
# length width #
# #
# Parameters: 1) width to center the text in #
# 2) the text to align #
#--------------------------------------------------------------------------#
my $width = shift;
my $line = shift;
return centerchar($wd, " ", $line);
}
sub spaceline($)
{ #--------------------------------------------------------------------------#
# Space a line, meaning that every character is surrounded #
# with spaces. #
# #
# Returns : The spaced line #
# #
# Parameters: 1) the line to space #
#--------------------------------------------------------------------------#
my $line = shift;
my $out = "";
while ( $line ne "" )
{
my $char = substr($line, 0, 1);
$line = substr($line, 1);
if ( $out eq "" )
{ $out = $char;
}
else
{ $out = $out . " " . $char;
}
}
return $out;
}
sub comment($)
{ #--------------------------------------------------------------------------#
# Determine the comment and prelim strings #
# Parameters: 1) language for comment strings #
# #
# Returns : list of comment info #
# comment_start, #
# comment_dash, #
# comment_end, #
# hashbang #
# #
# Parameters: 1) language for which the comment strings are wanted #
#--------------------------------------------------------------------------#
my $lang = shift;
if($delims{$lang})
{ return @{$delims{$lang}};
}
else
{ unknownlang($lang);
}
}
sub printline($$$;$)
{ #--------------------------------------------------------------------------#
# Print the lines nice. #
# #
# Returns : nothing #
# #
# Parameters: 1) width to print the text in #
# 2) boolean: print an empty line after the printed line #
# 3) first part of the text to print (left hand side) #
# 4) second part of the text to print (right hand) optional #
#--------------------------------------------------------------------------#
my ($wd, $empty, $lhs, $rhs) = @_;
my $l = $wd - 30;
my $r = $wd - $l;
if($rhs)
{ print OUT $cs . left($l, $lhs);
print OUT right($r, $rhs) . $ce . "\n";
}
else
{ print OUT $cs . left($wd, $lhs) . $ce . "\n";
}
print OUT $el if $empty;
}
sub printwithcomment($$$)
{ #--------------------------------------------------------------------------#
# Print the lines nice and place a comment at the right #
# #
# Returns : nothing #
# #
# Parameters: 1) width to print the text in #
# 2) first part of the text to print (left hand side) #
# 3) second part of the text to print (right hand side) #
#--------------------------------------------------------------------------#
my ($wd, $lhs, $rhs) = @_;
my $l = $wd - 50;
my $r = $wd - $l;
print OUT left($l, $lhs);
print OUT $cs . " " . left($r-2, $rhs) . " " . $ce . "\n";
}
sub help()
{
#--------------------------------------------------------------------------#
# Display the help and exit #
#--------------------------------------------------------------------------#
print "\theader $HeaderVersion by $MYOWN{name}\n";
print "\tSyntax: header [options]\n\n";
print "\tOptions:\n";
print "\t\t--nameinfo=filename\tUse an alternative name.info file <~/.name.info>\n";
print "\t\t--language=language\tProgramming language \<$default{lang}>\n";
print "\t\t--file=filename\t\tOutput filename\n";
print "\t\t--copyright=cpy\t\tCopyright message (<short>|yes|gnu|none)\n";
print "\t\t--short\t\t\tUse a short header <$default{short}>\n";
print "\t\t--simple\t\tUse a simple header <$default{simple}>\n";
print "\t\t--stdout\t\tPrint the output to stdout\n";
print "\t\t--notrail\t\tDo NOT print the trailing copyright\n";
print "\t\t--headlang=nl|en\tWhich header language to use <$default{hlang}>\n";
print "\t\t--tabstop=n|--ts=n\tWhat tabstop size to use <$default{tab}>\n";
print "\t\t--width=n\t\tWhat headerwidth to use <$default{width}>\n";
print "\t\t--vcs=git|svn|shortsvn|none\tWhat version control system to use <$default{vcs}>\n";
print "\t\t--help\t\t\tPrint this help\n\n";
print "\tOptions can be abbriviated, as long as they can be uniquely identified\n\n";
local $" = "\n\t";
my @known = join(" ", sort keys %delims);
print "Supported program languages:\n\t@known\n";
exit 0;
}
#------------------------------------------------------------------------------#
# Here the main program starts. Read the options #
#------------------------------------------------------------------------------#
my ($namefile, $lang, $name, $copy, $short, $simple, $stdo, $hlang,
$tab, $width, $help, $notrail);
GetOptions( "nameinfo=s", => \$namefile,
"language=s" => \$lang,
"file=s" => \$name,
"copyright=s" => \$copy,
"short" => \$short,
"simple=s" => \$simple,
"stdout" => \$stdo,
"notrail" => \$notrail,
"headlang=s" => \$hlang,
"tabstop=n" => \$tab,
"ts=n" => \$tab,
"width=n" => \$width,
"vcs=s" => \$vcs,
"help" => \$help);
$hlang = $hlang || $default{hlang}; # The default header language #
setlang($hlang); # Reset the language #
help if ($help); # Help requested #
$lang = $lang || $default{lang}; # Define the (default) language #
$name = $name || $default; # Define the program name #
$copy = $copy || $default{copy}; # Print 'copyright' #
$short = $short || $default{short}; # Print the short header #
$simple = $simple || $default{simple}; # Print the simple header #
$notrail = $notrail || $default{notrail}; # Print no trailing message #
$ts = $tab || $default{tab}; # Which tab stop to use #
$wd = $width || $default{width}; # Which width to use #
$vcs = $vcs || $default{vcs}; # Which version control system #
setlang($hlang);
$nameinfo = $namefile || $nameinfo; # Define the (default) nameinfo file #
my $vimsettings;
if ( $lang eq "puppet" )
{ # These settings need to be like this to comply with the Puppet coding style
$ts = 4;
$vimsettings = "vi: set sw=$ts ts=$ts ai:";
}
elsif ( $lang eq "yml" )
{ $vimsettings = "vi: set sw=$ts ts=$ts ai expandtab ff=unix:";
}
else
{ $vimsettings = "vi: set sw=$ts ts=$ts ai:";
}
#------------------------------------------------------------------------------#
# If a name is supplied place it in that file #
#------------------------------------------------------------------------------#
my $fileopen = 0;
if ($name eq $default)
{ open OUT, ">-";
}
else
{ if ($stdo)
{ open OUT, ">-";
}
else
{ open OUT, "> $name" or die "Cannot open $name";
$fileopen = 1;
}
}
#------------------------------------------------------------------------------#
# Open and read the nameinfo file #
#------------------------------------------------------------------------------#
my $ni = 1;
open NAME, $nameinfo or $ni = 0;
if ( $ni == 1 )
{
while (my $line = <NAME>)
{ chomp($line);
next if ($line =~ /^\s*#/);
next if ($line =~ /^\s*$/);
foreach my $field (@fields)
{ my ($fld, @val) = split(/ /, $line) ;
$fld = $field if (! $fld);
my $vl = join(" ", @val);
$author{$fld} = $vl || $MYOWN{$field};
}
}
close NAME;
}
else
{
printf STDERR "\n\nThe nameinfo file $nameinfo could not be found.";
printf STDERR "Using defaults.\n\n\n";
foreach my $field (@fields)
{ $author{$field} = $MYOWN{$field};
}
}
#------------------------------------------------------------------------------#
# Define the comment strings #
#------------------------------------------------------------------------------#
($cs, $da, $ce, $hashbang) = comment(lc($lang));
$wd = $wd - length($cs) - length($ce);
$wd += 4 if length($cs) >= 3;
$cl = $cs . $da x $wd . $ce . "\n"; # The standard comment line #
$el = $cs . " " x $wd . $ce . "\n"; # The standard empty comment line #
#------------------------------------------------------------------------------#
# Create the current date and time in nice formats #
#------------------------------------------------------------------------------#
(undef, $min, $hour, $mday, $mon, $year, $wday, undef) = localtime();
$year += 1900;
my $now = sprintf "%02d-%02d-$year", $mday, $mon+1;
my $tim = sprintf "%02d:%02d", $hour, $min;
$cvsbegin = " " . spaceline($cvsbegin) . " ";
$cvseinde = " " . spaceline($cvseinde) . " ";
#------------------------------------------------------------------------------#
# Translate the yyyy into the year and form the copyright string #
#------------------------------------------------------------------------------#
my $copyright = (sprintf $cop1, $year) . $author{cpri};
#------------------------------------------------------------------------------#
# Print it all out #
#------------------------------------------------------------------------------#
print OUT "$hashbang\n" if $hashbang;
$author{firm} = "" if ($author{firm} eq $author{name});
#------------------------------------------------------------------------------#
# If we have a Jinja 2 template, we are done #
#------------------------------------------------------------------------------#
if ($lang eq "in")
{ print OUT "$ends{$lang}" if $ends{$lang};
exit;
}
#------------------------------------------------------------------------------#
# Do we have a C header file? #
#------------------------------------------------------------------------------#
my $head = substr $name, -1, 1;
my $hname = "";
my $headerfile = 0;
if ($head eq "h" && $lang eq "c")
{ $headerfile = 1;
$hname = uc($name);
$hname =~ tr/./_/;
}
#------------------------------------------------------------------------------#
# Print it #
#------------------------------------------------------------------------------#
if ($lang eq "config")
{ $progtext = "Config file :";
}
if ($lang eq "puppet")
{ $progtext = "Puppet file :";
}
if ($lang eq "zone")
{ $progtext = "Zone file :";
}
if (lc($simple) eq "yes")
{
print OUT "$cs $vimsettings\n";
print OUT "$cs\n";
print OUT "$cs $author{name}\n";
print OUT "$cs $author{mail}\n";
print OUT "$cs $now\n\n";
}
else
{
print OUT $cl;
printline($wd, 0, " $vimsettings", "$vimcomment ");
print OUT $cl;
printline($wd, 1, " $progtext $name");
$short = "yes" if ($author{firm} eq "");
if (lc($short) eq "no")
{ my $spc = " " x length($authtext);
printline($wd, 0, " $authtext $author{name}", "$author{mail} ");
printline($wd, 0, " $spc $author{firm}", "$author{adr1} ");
printline($wd, 0, " $spc $author{zipc} $author{adr2}", "$author{cntr} ");
#printline($wd, 0, " $spc $author{adr3}") if ( $author{adr3} ne '' );
printline($wd, 1, " $spc $author{tele}", "$author{tfax} ");
printline($wd, 1, " $startdate $now", "$starttime $tim ");
#printline($wd, 0, " $spc $MYOWN{mail}", "$MYOWN{firm} ");
#printline($wd, 1, " $spc $MYOWN{adr1}, $MYOWN{adr2}", "$MYOWN{zipc}, $MYOWN{cntr} ");
}
else
{
printline($wd, 1, " $authtext $author{name}", "$author{mail} ");
printline($wd, 0, " $startdate $now", "$starttime $tim ");
}
if (lc($short) eq "no")
{ printline($wd, 1, " $desctext");
if ($lang ne "config")
{ printline($wd, 0, " $parmtext");
#printline($wd, 1, " $prereqs ");
#printline($wd, 0, " $exitcs 0 -> OK");
#printline($wd, 1, " <> 0 -> !OK");
}
# printline($wd, 0, " $updates ");
}
#--------------------------------------------------------------------------#
# Print extra copyright #
#--------------------------------------------------------------------------#
if(lc($copy) eq "yes")
{ print OUT $cl;
print OUT $cs . center($wd, $copyright) . $ce . "\n";
print OUT $cs . center($wd, $cop2) . $ce . "\n";
print OUT $cs . center($wd, $cop3) . $ce . "\n";
}
#--------------------------------------------------------------------------#
# Print extra copyright (Short version) #
#--------------------------------------------------------------------------#
if(lc($copy) eq "short")
{ print OUT $cl;
print OUT $cs . center($wd, $copyright) . $ce . "\n";
}
#--------------------------------------------------------------------------#
# Print GNU copyright #
#--------------------------------------------------------------------------#
if(lc($copy) eq "gnu")
{ print OUT $cl;
print OUT $cs . center($wd, $copyright) . $ce . "\n";
print OUT $cl;
for ( my $i = 0; $i <= $#gnu; $i++ )
{
printline($wd, 0, " " . $gnu[$i]);
}
}
print OUT "$cl";
}
#------------------------------------------------------------------------------#
# Puppet stuff (directly after the header) #
#------------------------------------------------------------------------------#
if (lc($lang) eq "puppet")
{ if (lc($simple) eq "yes")
{
print OUT "$cs $puppet1\n";
print OUT "$cs $puppet2\n\n";
}
else
{ print OUT "\n";
print OUT "$cl";
printline($wd, 0, " " .$puppet1);
printline($wd, 0, " " .$puppet2);
#printline($wd, 0, " \$Fname" . "::", "\$: ");
print OUT "$cl";
}
}
#------------------------------------------------------------------------------#
# Print the end of the preliminary (GIT/SVN information) #
#------------------------------------------------------------------------------#
if ( $vcs ne "none" )
{ print OUT "\n";
if ( $vcs eq "svn" )
{
print OUT "$cl";
print OUT "$cs" . center($wd, $cvsbegin) . $ce . "\n";
print OUT "$cl";
printline($wd, 0, " \$Id" . "::", "\$: ");
printline($wd, 0, " \$Revision" . "::", "\$: ");
printline($wd, 0, " \$Author" . "::", "\$: ");
printline($wd, 0, " \$Date" . "::", "\$: ");
printline($wd, 0, " \$URL" . "::", "\$: ");
print OUT "$cl";
print OUT "$cs" . center($wd, $cvseinde) . $ce . "\n";
print OUT "$cl\n";
}
elsif ( $vcs eq "git" )
{
print OUT "$cl";
print OUT "$cs" . center($wd, $cvsbegin) . $ce . "\n";
print OUT "$cl";
printline($wd, 0, " \$Id" . "::", "\$: ");
printline($wd, 0, " \$Revision" . "::", "\$: ");
printline($wd, 0, " \$Author" . "::", "\$: ");
printline($wd, 0, " \$Date" . "::", "\$: ");
printline($wd, 0, " \$Hash" . "::", "\$: ");
print OUT "$cl";
print OUT "$cs" . center($wd, $cvseinde) . $ce . "\n";
print OUT "$cl\n";
}
else
{
print OUT "$cs \$Id\$\n";
print OUT "$cs \$URL\$\n\n";
}
}
#------------------------------------------------------------------------------#
# Extra lines for C header files #
#------------------------------------------------------------------------------#
if ($headerfile == 1)
{ print OUT "#ifndef $hname\n";
print OUT "#define $hname\n\n";
}
if ( (lc($lang) eq "bash") ||
(lc($lang) eq "ksh") ||
(lc($lang) eq "nagios") ||
(lc($lang) eq "init") )
{ print OUT "$cl";
printline($wd, 0, " $rundir");