check-doc.pl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/perl
  2. # checking-doc original script by trojal
  3. # modified by lighta
  4. use strict;
  5. use File::Basename;
  6. use Getopt::Long;
  7. my $sHelp = 0;
  8. my $sCmd = $1;
  9. my $sAtcf = "../doc/atcommands.txt";
  10. my $sSctf = "../doc/script_commands.txt";
  11. my $sLeftOverChk = 0;
  12. my $sTarget = "All";
  13. my $sValidTarget = "All|Script|Atc";
  14. my($filename, $dir, $suffix) = fileparse($0);
  15. chdir $dir; #put ourself like was called in tool folder
  16. GetArgs();
  17. Main($sTarget);
  18. sub GetArgs {
  19. GetOptions(
  20. 'atcf=s' => \$sAtcf, #atc doc file
  21. 'scriptf=s' => \$sSctf, #script doc file
  22. 'target=s' => \$sTarget, #Target (wich setup to run)
  23. 'leftover=i' => \$sLeftOverChk, #should we chk if all doc are linked to a src ?
  24. 'help!' => \$sHelp,
  25. ) or $sHelp=1; #display help if invalid option
  26. if( $sHelp ) {
  27. print "Incorect option specified, available option are:\n"
  28. ."\t --atcf filename => file (specify atcommand doc to use)\n"
  29. ."\t --scriptf filename => file (specify script doc to use)\n"
  30. ."\t --leftover=0|1 => should we run reverse chk for leftover in documentation ?\n"
  31. ."\t --target => target (specify wich check to run [$sValidTarget])\n";
  32. exit;
  33. }
  34. unless($sTarget =~ /$sValidTarget/i){
  35. print "Incorect target specified, available target are:\n"
  36. ."\t --target => target (specify wich check to run [(default)$sValidTarget])\n";
  37. exit;
  38. }
  39. }
  40. sub Main { my ($sCmd) = @_;
  41. if($sCmd=~/both|all/i){ #both is keep as backard compatibility here cf check-doc.sh
  42. $sCmd = "script|atc";
  43. }
  44. if($sCmd=~/script/i){ #find which script commands are missing from doc/script_commands.txt
  45. Script_Chk();
  46. }
  47. if($sCmd=~/atc/i){ #find which atcommands are missing from doc/atcommands.txt
  48. Atc_Chk();
  49. }
  50. }
  51. sub Chk { my($raA,$raB) = @_;
  52. my @aMissing = ();
  53. foreach my $sA (@$raA){
  54. my $sFound = 0;
  55. foreach my $sB (@$raB){
  56. $sFound=1 if($sA eq $sB);
  57. }
  58. unless($sFound){
  59. push(@aMissing,$sA);
  60. }
  61. }
  62. return \@aMissing;
  63. }
  64. sub Script_Chk {
  65. my @aSct_src = ("../src/map/script.c","../src/custom/script_def.inc");
  66. my @aDef_sct = ();
  67. my @aDoc_sct = ();
  68. my $raMiss_sct;
  69. foreach my $sSct_srcf (@aSct_src){
  70. open FILE_SRC, "<$sSct_srcf" || die "couldn't open file $sSct_srcf \n";
  71. while(<FILE_SRC>){
  72. next if($_ =~ /^#/); #ignoe include, define or macro
  73. if($_ =~ /BUILDIN_DEF|BUILDIN_DEF2/){
  74. $_ =~ s/\s+$//; #Remove trailing spaces.
  75. $_ =~ s/^\s+//; #Remove leading spaces.
  76. if($_ =~ /^BUILDIN_DEF2/){
  77. my @line = split('"',$_);
  78. push(@aDef_sct,$line[1]);
  79. }
  80. elsif($_ =~ /^BUILDIN_DEF/){
  81. my @line = split(',',$_);
  82. @line = split('\(',$line[0]);
  83. push(@aDef_sct,$line[1]);
  84. }
  85. }
  86. }
  87. close FILE_SRC;
  88. }
  89. open FILE_DOC, "$sSctf" || die "couldn't open file $sSctf \n";
  90. while(<FILE_DOC>){
  91. next if($_ =~ /^\*\*|^\*\s|^\s+/); #discard **, * foo, foo
  92. next if(/^\s+/);
  93. if($_ =~ /^\*/){
  94. my @line = split(' ',$_);
  95. @line = split('\(',$line[0]);
  96. @line = split('\<',$line[0]);
  97. $line[0] =~ s/\(|\{|\*|\r|\s|\;|\)|\"|\,//g; #todo please harmonize command definition for easier parse
  98. next if($line[0] eq "Name" || $line[0] eq "" || $line[0] eq "function"
  99. || $line[0] eq "if" || $line[0] eq "while" || $line[0] eq "do" || $line[0] eq "for" ); #exception list
  100. push(@aDoc_sct,$line[0]);
  101. }
  102. }
  103. close FILE_DOC;
  104. $raMiss_sct = Chk(\@aDef_sct,\@aDoc_sct); #check missing documentation
  105. if(scalar(@$raMiss_sct)){
  106. print "Missing script documentation for function :{\n";
  107. foreach(@$raMiss_sct){
  108. print "\t$_ \n";
  109. }
  110. print "}\n\n";
  111. }
  112. else { print "All script command in Src are documented, no issues found\n"; }
  113. if($sLeftOverChk){
  114. my $raLeftover_sct = Chk(\@aDoc_sct,\@aDef_sct); #we just inverse the chk for leftover
  115. if(scalar(@$raLeftover_sct)){
  116. print "Those script command was found in doc but no source associated, leftover ? :{\n";
  117. foreach(@$raLeftover_sct){
  118. print "\t$_ \n";
  119. }
  120. print "}\n\n";
  121. }
  122. else { print "All script command in documentation match a source BUILDIN, no leftover found\n"; }
  123. }
  124. }
  125. sub Atc_Chk {
  126. my @aAct_src = ("../src/map/atcommand.c","../src/custom/atcommand_def.inc");
  127. my @aDef_act = ();
  128. my @aDoc_act = ();
  129. my $raMiss_act;
  130. foreach my $sAct_srcf (@aAct_src){
  131. open FILE_SRC, "<$sAct_srcf" || die "couldn't open file $sAct_srcf \n";
  132. while(<FILE_SRC>){
  133. next if($_ =~ /^#/); #ignoe include, define or macro
  134. if($_ =~ /ACMD_DEF|ACMD_DEF2|ACMD_DEFR|ACMD_DEF2R/){
  135. $_ =~ s/\s+$//; #Remove trailing spaces.
  136. $_ =~ s/^\s+//; #Remove leading spaces.
  137. if($_ =~ /^ACMD_DEF2|^ACMD_DEF2R/){
  138. my @line = split('"',$_);
  139. push(@aDef_act,$line[1]);
  140. }
  141. elsif($_ =~ /^ACMD_DEF|^ACMD_DEFR/){
  142. my @line = split(',',$_);
  143. @line = split('\(',$line[0]);
  144. $line[1] =~ s/\)//; #Remove closing brace
  145. push(@aDef_act,$line[1]);
  146. }
  147. }
  148. }
  149. close FILE_SRC;
  150. }
  151. open FILE_DOC, "$sAtcf" || die "couldn't open file $sAtcf \n";
  152. while(<FILE_DOC>){
  153. next if($_ =~ /^\*\*|^\*\s|^\s+/); #discard **, * foo, foo
  154. next if(/^\s+/);
  155. if($_ =~ /^\@/){
  156. my @line = split(' ',$_);
  157. @line = split('\(',$line[0]);
  158. @line = split('\<',$line[0]);
  159. $line[0] =~ s/\(|\{|\@|\r|\s|\;|\)|\"|\,//g; #todo please harmonize command definition for easier parse
  160. push(@aDoc_act,$line[0]);
  161. }
  162. }
  163. close FILE_DOC;
  164. $raMiss_act = Chk(\@aDef_act,\@aDoc_act); #check missing documentation
  165. if(scalar(@$raMiss_act)){
  166. print "Missing atcommand documentation for function :{\n";
  167. foreach(@$raMiss_act){
  168. print "\t$_ \n";
  169. }
  170. print "}\n\n";
  171. }
  172. else { print "All atcommand in Src are documented, no issues found\n"; }
  173. if($sLeftOverChk){
  174. my $raLeftover_sct = Chk(\@aDoc_act,\@aDef_act); #we just inverse the chk for leftover
  175. if(scalar(@$raLeftover_sct)){
  176. print "Those atcommand command was found in doc but no source associated, leftover ? : {\n";
  177. foreach(@$raLeftover_sct){
  178. print "\t$_ \n";
  179. }
  180. print "}\n\n";
  181. }
  182. else { print "All atcommand in documentation match a source ATCMD, no leftover found\n"; }
  183. }
  184. }