src/cline.c

changeset 79
e4592d0292e7
parent 66
be2084398c37
--- a/src/cline.c	Sun Nov 10 14:06:03 2024 +0100
+++ b/src/cline.c	Mon Apr 07 20:43:52 2025 +0200
@@ -1,5 +1,5 @@
 /*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  * Copyright 2018 Mike Becker. All rights reserved.
  * 
  * Redistribution and use in source and binary forms, with or without
@@ -24,13 +24,17 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "cline.h"
+#ifndef VERSION
+#error "VERSION macro must be set by build system"
+#endif
+
+#include "stdinc.h"
+#include "settings.h"
 #include "scanner.h"
-#include "settings.h"
 #include "arguments.h"
 #include "regex_parser.h"
 
-void printHelpText() {
+static void printHelpText() {
   printf(
     "\nUsage:"
     "\n      cline [Options] [Directories...]"
@@ -40,6 +44,7 @@
     "\n  -b <level>          - binary file heuristics level (default medium)"
     "\n                        One of: ignore low medium high"
     "\n  -c                  - Count non-whitespace characters instead of lines"
+    "\n  -d                  - Report only directory sums"
     "\n  -E <pattern>        - Excludes any line matching the <pattern>"
     "\n  -e <start> <end>    - Excludes lines between <start> and <end>"
     "\n                        You may use these options multiple times"
@@ -74,19 +79,22 @@
     "\n");
 }
 
-int exit_with_version(settings_t* settings) {
+static int exit_with_version(settings_t* settings) {
   printf("cline - Version: " VERSION "\n");
   destroy_settings_t(settings);
   return 0;
 }
 
-int exit_with_help(settings_t* settings, int code) {
+static int exit_with_help(settings_t* settings, int code) {
   printf("cline - Version: " VERSION "\n");
   printHelpText();
   destroy_settings_t(settings);
   return code;
 }
 
+static const char * sepline_double = "===============================================================================\n";
+static const char * sepline_single = "-------------------------------------------------------------------------------\n";
+
 int main(int argc, char** argv) {
 
   /* Settings */
@@ -108,7 +116,7 @@
 
   for (int t = 1 ; t < argc ; t++) {
 
-    int argflags = checkArgument(argv[t], "hsSrRmvVbeEic");
+    int argflags = checkArgument(argv[t], "hsSrRmvVbeEicd");
     int paropt = 0;
 
     /* h */
@@ -203,11 +211,12 @@
     /* i */
     if ((argflags & 2048) > 0) {
       /* cannot be used together with -V */
-      if (registerArgument(&checked, 128)) {
+      if ((checked & 128) > 0) {
         return exit_with_help(settings, 1);
       }
       settings->individual_sums = true;
     }
+    /* c */
     if ((argflags & 4096) > 0) {
         if (registerArgument(&checked, 4096)) {
             return exit_with_help(settings, 1);
@@ -215,6 +224,16 @@
         settings->count_chars = true;
         settings->regex->count_chars = true;
     }
+    /* d */
+    if ((argflags & 8192) > 0) {
+      if (registerArgument(&checked, 8192)) {
+        return exit_with_help(settings, 1);
+      }
+      /* ignored together with -V */
+      if ((checked & 128) == 0) {
+        settings->dirsOnly = true;
+      }
+    }
     if (argflags == 0) {
       /* SHORTCUTS */
       if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) {
@@ -247,33 +266,25 @@
     
     unsigned total = 0;
     if (directories->count == 0) {
-        add_string(directories, ".");
+        add_string(directories, "./");
     }
     for (unsigned t = 0 ; t < directories->count ; t++) {
       scanDirectory((scanner_t){directories->items[t], 0}, settings,
           output, result);
       total += result->result;
-      if (directories->count > 1 ) {
-        outbuf = (char*) malloc(81);
-        memset(outbuf, '-', 79);
-        outbuf[79] = '\n';
-        outbuf[80] = 0;
-        add_string(output, outbuf);
+      if (directories->count > 1) {
+        add_string(output, strdup(sepline_single));
         outbuf = (char*) malloc(81);
         snprintf(outbuf, 81, "%-63s%10u %s\n", directories->items[t],
                 result->result, result_type);
         add_string(output, outbuf);
-        outbuf = (char*) malloc(81);
-        memset(outbuf, '-', 79);
-        outbuf[79] = '\n';
-        outbuf[80] = 0;
-        add_string(output, outbuf);
+        add_string(output, strdup(sepline_single));
       }
     }
     destroy_string_list_t(directories);
 
     /* Print result */
-    if (settings->verbose) {
+    if (output != NULL) {
       for (int i = 0 ; i < output->count ; i++) {
         printf("%s", output->items[i]);
         free(output->items[i]);
@@ -281,9 +292,7 @@
       
       if (result->ext) {
         if (result->ext->count > 0) {
-          for (unsigned t = 0 ; t < 79 ; t++) {
-            printf("=");
-          }
+          fwrite(sepline_double, 1, 80, stdout);
           printf("\nIndividual sums:\n");
           for (unsigned t = 0 ; t < result->ext->count ; t++) {
             printf(" %-62s%10u %s\n",
@@ -293,11 +302,14 @@
           }
         }
       }
-      
-      for (unsigned t = 0 ; t < 79 ; t++) {
-        printf("=");
+
+      if (output->count > 0) {
+        fwrite(sepline_double, 1, 80, stdout);
+        printf("%73d %s\n", total, result_type);
+      } else {
+        /* If we only need to output a total, output it differently */
+        printf("%d %s\n", total, result_type);
       }
-      printf("\n%73d %s\n", total, result_type);
 
       if (settings->confusing_lnlen &&
           settings->regex->pattern_list->count > 0) {

mercurial