[0001]
[0002]
[0003]
[0004]
[0005]
[0006]
[0007]
[0008]
[0009]
[0010]
[0011]
[0012]
[0013]
[0014]
[0015]
[0016]
[0017]
[0018]
[0019]
[0020]
[0021]
[0022]
[0023]
[0024]
[0025]
[0026]
[0027]
[0028]
[0029]
[0030]
[0031]
[0032]
[0033]
[0034]
[0035]
[0036]
[0037]
[0038]
[0039]
[0040]
[0041]
[0042]
[0043]
[0044]
[0045]
[0046]
[0047]
[0048]
[0049]
[0050]
[0051]
[0052]
[0053]
[0054]
[0055]
[0056]
[0057]
[0058]
[0059]
[0060]
[0061]
[0062]
[0063]
[0064]
[0065]
[0066]
[0067]
[0068]
[0069]
[0070]
[0071]
[0072]
[0073]
[0074]
[0075]
[0076]
[0077]
[0078]
[0079]
[0080]
[0081]
[0082]
[0083]
[0084]
[0085]
[0086]
[0087]
[0088]
[0089]
[0090]
[0091]
[0092]
[0093]
[0094]
[0095]
[0096]
[0097]
[0098]
[0099]
[0100]
[0101]
[0102]
[0103]
[0104]
[0105]
[0106]
[0107]
[0108]
[0109]
[0110]
[0111]
[0112]
[0113]
[0114]
[0115]
[0116]
[0117]
[0118]
[0119]
[0120]
[0121]
[0122]
[0123]
[0124]
[0125]
[0126]
[0127]
[0128]
[0129]
[0130]
[0131]
[0132]
[0133]
[0134]
[0135]
[0136]
[0137]
[0138]
[0139]
[0140]
[0141]
[0142]
[0143]
[0144]
[0145]
[0146]
[0147]
[0148]
[0149]
[0150]
[0151]
[0152]
[0153]
[0154]
[0155]
[0156]
[0157]
[0158]
[0159]
[0160]
[0161]
[0162]
[0163]
[0164]
[0165]
[0166]
[0167]
[0168]
[0169]
[0170]
[0171]
[0172]
[0173]
[0174]
[0175]
[0176]
[0177]
[0178]
[0179]
[0180]
[0181]
[0182]
[0183]
[0184]
[0185]
[0186]
[0187]
[0188]
[0189]
[0190]
[0191]
[0192]
[0193]
[0194]
[0195]
[0196]
[0197]
[0198]
[0199]
[0200]
[0201]
[0202]
[0203]
[0204]
[0205]
[0206]
[0207]
[0208]
[0209]
[0210]
[0211]
[0212]
[0213]
[0214]
[0215]
[0216]
[0217]
[0218]
[0219]
[0220]
[0221]
[0222]
[0223]
[0224]
[0225]
[0226]
[0227]
[0228]
[0229]
[0230]
[0231]
[0232]
[0233]
[0234]
[0235]
[0236]
[0237]
[0238]
[0239]
[0240]
[0241]
[0242]
[0243]
[0244]
[0245]
[0246]
[0247]
[0248]
[0249]
[0250]
[0251]
[0252]
[0253]
[0254]
[0255]
[0256]
[0257]
[0258]
[0259]
[0260]
[0261]
[0262]
[0263]
[0264]
[0265]
[0266]
[0267]
[0268]
[0269]
[0270]
[0271]
[0272]
[0273]
[0274]
[0275]
[0276]
[0277]
[0278]
[0279]
[0280]
[0281]
[0282]
[0283]
[0284]
[0285]
[0286]
[0287]
[0288]
[0289]
[0290]
[0291]
[0292]
[0293]
[0294]
[0295]
[0296]
[0297]
[0298]
[0299]
[0300]
[0301]
[0302]
[0303]
[0304]
[0305]
[0306]
[0307]
[0308]
[0309]
[0310]
[0311]
[0312]
[0313]
[0314]
[0315]
[0316]
[0317]
[0318]
[0319]
[0320]
[0321]
[0322]
[0323]
[0324]
[0325]
[0326]
[0327]
[0328]
[0329]
[0330]
[0331]
[0332]
[0333]
[0334]
[0335]
[0336]
[0337]
[0338]
[0339]
[0340]
[0341]
[0342]
[0343]
[0344]
[0345]
[0346]
[0347]
[0348]
[0349]
[0350]
[0351]
[0352]
[0353]
[0354]
[0355]
[0356]
[0357]
[0358]
[0359]
[0360]
[0361]
[0362]
[0363]
[0364]
[0365]
[0366]
[0367]
[0368]
[0369]
[0370]
[0371]
[0372]
[0373]
[0374]
[0375]
[0376]
[0377]
[0378]
[0379]
[0380]
[0381]
[0382]
[0383]
[0384]
[0385]
[0386]
[0387]
[0388]
[0389]
[0390]
[0391]
[0392]
[0393]
[0394]
[0395]
[0396]
[0397]
[0398]
[0399]
[0400]
[0401]
[0402]
[0403]
[0404]
[0405]
[0406]
[0407]
[0408]
[0409]
[0410]
[0411]
[0412]
[0413]
[0414]
[0415]
[0416]
[0417]
[0418]
[0419]
[0420]
[0421]
[0422]
[0423]
[0424]
[0425]
[0426]
[0427]
[0428]
[0429]
[0430]
[0431]
[0432]
[0433]
[0434]
[0435]
[0436]
[0437]
[0438]
[0439]
[0440]
[0441]
[0442]
[0443]
[0444]
[0445]
[0446]
[0447]
[0448]
[0449]
[0450]
[0451]
[0452]
[0453]
[0454]
[0455]
/*****************************************************************************/
/*
                              C-c[o]d[e]c[ou]nt.c

Count of lines of code.
Count of lines of comment.


BUILD DETAILS
-------------
$ @BUILD_CCDCNT BUILD  !compile+link
$ @BUILD_CCDCNT LINK   !link-only


COPYRIGHT
---------
Copyright (C) 2015-2021 Mark G.Daniel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


VERSION HISTORY
---------------
21-JAN-2015  MGD  v1.0.0, initial (q&d) development
*/
/*****************************************************************************/

#define SOFTWAREVN "1.0.0"
#define SOFTWARENM "CCDCNT"
#ifdef __ALPHA
#  define SOFTWAREID SOFTWARENM " AXP-" SOFTWAREVN
#endif
#ifdef __ia64
#  define SOFTWAREID SOFTWARENM " IA64-" SOFTWAREVN
#endif
#ifdef __VAX
#  error VAX no longer implemented
#endif
#ifdef __x86_64
#  define SOFTWAREID SOFTWARENM " X86-" SOFTWAREVN
#endif

/* standard C header files */
#include <ctype.h>
#include <errno.h>
#include <stat.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unixio.h>

/* VMS header files */
#include <fabdef.h>
#include <namdef.h>
#include <rmsdef.h>
#include <ssdef.h>
#include <stsdef.h>
#include <starlet.h>

/* macros */
#define FI_LI __FILE__, __LINE__

#define ODS_MAX_FILE_NAME_LENGTH 1024
#define ODS2_MAX_FILE_NAME_LENGTH 255

#define VMSnok(x) (!((x) & STS$M_SUCCESS))
#define VMSok(x) ((x) & STS$M_SUCCESS)

#define USHORTPTR __unaligned unsigned short*
#define SAME2(ptr,val) (*(USHORTPTR)(ptr) == val)
#define SLASHESC(ptr) (*ptr == '\\' && *(ptr+1))

int Debug = 0;

char  Utility [] = "CCDCNT";

/*****************************************************************************/
/*
*/

main (int argc, char** argv)
       
{
static char Dash80 [] = "-----------------------------------------\
-------------------------------------";

   int  cnt, size, status,
        BothCharCount,
        BothLineCount,
        CodeCharCount,
        CodeLineCount,
        CommentCharCount,
        CommentLineCount,
        CommentCount,
        DoOverAll = 0,
        ExpFileNameLength,
        FileCharCount,
        FileLineCount,
        PrevCodeCharCount,
        PrevCommentCharCount,
        ResFileNameLength,
        TotalBothCharCount = 0,
        TotalBothLineCount = 0,
        TotalCharCount = 0,
        TotalCodeCharCount = 0,
        TotalCodeLineCount = 0,
        TotalCommentCharCount = 0,
        TotalCommentLineCount = 0,
        TotalCommentCount = 0,
        TotalFileCount = 0,
        TotalLineCount = 0;
   char  ExpFileName [ODS_MAX_FILE_NAME_LENGTH+1],
         ResFileName [ODS_MAX_FILE_NAME_LENGTH+1];
   char  *aptr, *cptr, *fsptr, *sptr, *zptr;
   stat_t  statBuf;
   FILE  *fptr;
   struct FAB  SearchFab;
   struct NAM  SearchNam;
#ifdef ODS_EXTENDED
   struct NAML  SearchNaml;
#endif /* ODS_EXTENDED */

   /*********/
   /* begin */
   /*********/

   fsptr = "*.C;0";
   for (cnt = 1; cnt < argc; cnt++)
   {
      if (argv[cnt][0] == '/')
      {
         if (!strcasecmp(argv[cnt],"/overall") ||
             !strcasecmp(argv[cnt],"/brief"))
            DoOverAll = 1;
      }
      else
         fsptr = argv[cnt];
   }

   SearchFab = cc$rms_fab;
   SearchFab.fab$l_dna = "*.*;0";
   SearchFab.fab$b_dns = 5;

#ifdef ODS_EXTENDED
   SearchFab.fab$l_fna = (char*)-1;
   SearchFab.fab$b_fns = 0;
   SearchFab.fab$l_nam = (struct namdef*)&SearchNaml;

   ENAMEL_RMS_NAML(SearchNaml)
   SearchNaml.naml$l_long_filename = fsptr;
   SearchNaml.naml$l_long_filename_size = strlen(fsptr);
   SearchNaml.naml$l_long_expand = ExpFileName;
   SearchNaml.naml$l_long_expand_alloc = sizeof(ExpFileName)-1;
   SearchNaml.naml$l_long_result = ResFileName;
   SearchNaml.naml$l_long_result_alloc = sizeof(ResFileName)-1;
#else /* ODS_EXTENDED */
   SearchFab.fab$l_fna = fsptr;
   SearchFab.fab$b_fns = strlen(fsptr);
   SearchFab.fab$l_nam = &SearchNam;

   SearchNam = cc$rms_nam;
   SearchNam.nam$l_esa = ExpFileName;
   SearchNam.nam$b_ess = ODS2_MAX_FILE_NAME_LENGTH;
   SearchNam.nam$l_rsa = ResFileName;
   SearchNam.nam$b_rss = ODS2_MAX_FILE_NAME_LENGTH;
#endif /* ODS_EXTENDED */

   if (VMSnok (status = sys$parse (&SearchFab, 0, 0)))
      return (status);

#ifdef ODS_EXTENDED
   SearchNaml.naml$l_long_ver[SearchNaml.naml$l_long_ver_size] = '\0';
   ExpFileNameLength = (SearchNaml.naml$l_long_ver - ExpFileName) +
                       SearchNaml.naml$l_long_ver_size;
#else /* ODS_EXTENDED */
   SearchNam.nam$l_ver[SearchNam.nam$b_ver] = '\0';
   ExpFileNameLength = (SearchNam.nam$l_ver - ExpFileName) +
                       SearchNam.nam$b_ver;
#endif /* ODS_EXTENDED */
   if (Debug)
      fprintf (stdout, "ExpFileName %d |%s|\n",
               ExpFileNameLength, ExpFileName);

   /*****************/
   /* for each file */
   /*****************/

   while (VMSok (status = sys$search (&SearchFab, 0, 0)))
   {
#ifdef ODS_EXTENDED
      SearchNaml.naml$l_long_ver[SearchNaml.naml$l_long_ver_size] = '\0';
      ResFileNameLength = (SearchNaml.naml$l_long_ver - ResFileName) +
                          SearchNaml.naml$l_long_ver_size;
#else /* ODS_EXTENDED */
      SearchNam.nam$l_ver[SearchNam.nam$b_ver] = '\0';
      ResFileNameLength = (SearchNam.nam$l_ver - ResFileName) +
                          SearchNam.nam$b_ver;
#endif /* ODS_EXTENDED */
      if (Debug)
         fprintf (stdout, "ResFileName %d |%s|\n",
                 ResFileNameLength, ResFileName);

      if (stat (ResFileName, &statBuf) < 0)
      {
         fprintf (stdout, "%s-E-ACCESS, %%X%08.08X %s\n",
                  Utility, vaxc$errno, ResFileName);
         continue;
      }

      size = statBuf.st_size;
      if (Debug) fprintf (stdout, "%d bytes\n", size);
      if (!(aptr = calloc (1, size+1024))) exit (vaxc$errno);

      /****************/
      /* process file */
      /****************/

      if ((fptr = fopen (ResFileName, "r", "shr=get")) != NULL)
      {
         if (statBuf.st_fab_rfm == FAB$C_VAR ||
             statBuf.st_fab_rfm == FAB$C_VFC)
         {
            zptr = (sptr = aptr) + size;
            while (fgets (sptr, zptr-sptr, fptr))
               sptr += strlen(sptr);
            size = sptr - aptr;
         }
         else
            size = fread (aptr, 1, size, fptr);
         if (Debug) fprintf (stdout, "%d bytes\n", size);

         fclose (fptr);

         if (!TotalFileCount)
            fprintf (stdout, "\
     ------Total------ -----------Code------------ ----------Comment----------\n\
       Lines     Chars   Lines          Chars        Lines          Chars\n\
     ------- --------- ------- ---- --------- ---- ------- ---- --------- ----\n\
");

         if (size)
         {
            BothLineCount = BothCharCount = CodeCharCount = CodeLineCount =
               CommentCharCount = CommentLineCount = CommentCount =
               FileCharCount = FileLineCount = PrevCodeCharCount =
               PrevCommentCharCount = 0;

            TotalFileCount++;
            FileCharCount = size;
            zptr = (cptr = aptr) + size;
            while (cptr < zptr)
            {
               if (SAME2(cptr,'/*'))
               {
                  /***********/
                  /* comment */
                  /***********/

                  cptr += 2;
                  CommentCharCount += 2;
                  CommentCount++;
                  while (cptr < zptr && !SAME2(cptr,'*/'))
                  {
                     if (SLASHESC(cptr))
                     {
                        CommentCharCount++;
                        cptr++;
                     }
                     if (*cptr == '\n')
                     {
                        FileLineCount++;
                        BothLineCount++;
                     }
                     if (!isspace(*cptr)) CommentCharCount++;
                     cptr++;
                  }
                  if (cptr < zptr)
                  {
                     CommentCharCount += 2;
                     cptr += 2;
                  }
               }
               else
               if (SAME2(cptr,'//'))
               {
                  /********************/
                  /* comment (to EOL) */
                  /********************/

                  cptr += 2;
                  CommentCharCount += 2;
                  CommentCount++;
                  // to end of line
                  while (cptr < zptr && *cptr != '\n')
                  {
                     if (SLASHESC(cptr))
                     {
                        CommentCharCount++;
                        cptr++;
                     }
                     if (!isspace(*cptr)) CommentCharCount++;
                     cptr++;
                  }
                  if (cptr < zptr) cptr++;
               }
               else
               if (*cptr == '\"')
               {
                  /***********/
                  /* literal */
                  /***********/

                  CodeCharCount++;
                  cptr++;
                  while (cptr < zptr && *cptr != '\"')
                  {
                     if (SLASHESC(cptr))
                     {
                        CodeCharCount++;
                        cptr++;
                     }
                     else
                     if (*cptr == '\n')
                     {
                        FileLineCount++;
                        BothLineCount++;
                        CodeLineCount++;
                     }
                     CodeCharCount++;
                     cptr++;
                  }
                  if (cptr < zptr)
                  {
                     CodeCharCount++;
                     cptr++;
                  }
               }
               else
               if (*cptr == '\n')
               {
                  /************/
                  /* new line */
                  /************/

                  FileLineCount++;
                  if (CodeCharCount > PrevCodeCharCount || 
                      CommentCharCount > PrevCommentCharCount)
                  {
                     BothLineCount++;
                     if (CodeCharCount > PrevCodeCharCount)
                        CodeLineCount++;
                     PrevCodeCharCount = CodeCharCount;
                     if (CommentCharCount > PrevCommentCharCount)
                        CommentLineCount++;
                     PrevCommentCharCount = CommentCharCount;
                  }
                  cptr++;
               }
               else
               if (!isspace(*cptr))
               {
                  /*************************/
                  /* significant non-space */
                  /*************************/

                  CodeCharCount++;
                  cptr++;
               }
               else
                  cptr++;
            }

            BothCharCount = CodeCharCount + CommentCharCount;
            TotalBothCharCount += BothCharCount;
            TotalBothLineCount += BothLineCount;
            TotalCharCount += FileCharCount;
            TotalCodeCharCount += CodeCharCount;
            TotalCodeLineCount += CodeLineCount;
            TotalCommentCharCount += CommentCharCount;
            TotalCommentLineCount += CommentLineCount;
            TotalCommentCount += CommentCount;
            TotalLineCount += FileLineCount;

            if (!DoOverAll)
            {
               if (strchr(fsptr,'*'))
                  fprintf (stdout, "%s%*.*s\n",
                           ResFileName,
                           78-ResFileNameLength, 78-ResFileNameLength, Dash80);

               fprintf (stdout,
"%04d %7d %9d %7d %3d%% %9d %3d%% %7d %3d%% %9d %3d%%\n",
                        TotalFileCount,
                        FileLineCount,
                        TotalCharCount,
                        CodeLineCount, CodeLineCount * 100 / BothLineCount,
                        CodeCharCount, CodeCharCount * 100 / BothCharCount,
                        CommentLineCount, CommentLineCount * 100 / BothLineCount,
                        CommentCharCount, CommentCharCount * 100 / BothCharCount);
            }
         }
         else
            fprintf (stdout, "%s-E-READ, %%X%08.08X %s\n",
                     Utility, vaxc$errno, ResFileName);
      }
      else
         fprintf (stdout, "%s-E-OPEN, %%X%08.08X %s\n",
                  Utility, vaxc$errno, ResFileName);

      free (aptr);
   }

   /**********/
   /* report */
   /**********/

   if (!TotalFileCount) exit (status);

   if (TotalFileCount > 1)
   {
      if (!DoOverAll)
         fprintf (stdout, "%33.33s OVERALL %36.36s\n", Dash80, Dash80);

      fprintf (stdout,
"%04d %7d %9d %7d %3d%% %9d %3d%% %7d %3d%% %9d %3d%%\n",
               TotalFileCount,
               TotalLineCount,
               TotalCharCount,
               TotalCodeLineCount,
               TotalCodeLineCount * 100 / TotalBothLineCount,
               TotalCodeCharCount,
               TotalCodeCharCount * 100 / TotalBothCharCount,
               TotalCommentLineCount,
               TotalCommentLineCount * 100 / TotalBothLineCount,
               TotalCommentCharCount,
               TotalCommentCharCount * 100 / TotalBothCharCount);
   }

   if (status == RMS$_NMF) status = SS$_NORMAL;

   return (status);
}

/*****************************************************************************/