init prog
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 5 Dec 2023 12:55:43 +0000 (13:55 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 5 Dec 2023 12:55:43 +0000 (13:55 +0100)
debug.c [new file with mode: 0644]
debug.h [new file with mode: 0644]
indent.c [new file with mode: 0644]

diff --git a/debug.c b/debug.c
new file mode 100644 (file)
index 0000000..151e615
--- /dev/null
+++ b/debug.c
@@ -0,0 +1,5 @@
+#include "debug.h"
+
+int verbose = 1;
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/debug.h b/debug.h
new file mode 100644 (file)
index 0000000..32dc0ca
--- /dev/null
+++ b/debug.h
@@ -0,0 +1,21 @@
+#ifndef __DEBUG_H__
+#define __DEBUG_H__
+
+/* constants */
+
+#define DEBUG 3
+#define INFO 2
+#define WARNING 1
+#define ERROR 0
+
+/* macros */
+
+#define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
+
+/* gobal variables */
+
+extern int verbose;
+
+#endif /* __DEBUG_H__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/indent.c b/indent.c
new file mode 100644 (file)
index 0000000..2b88345
--- /dev/null
+++ b/indent.c
@@ -0,0 +1,166 @@
+/* depend: */
+/* cflags: */
+/* linker: debug.o */
+
+#include <assert.h>
+#include <getopt.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "debug.h"
+
+/* macros */
+
+#define CEIL(x, y) (((x) + (y) - 1) / (y))
+#define MIN(x, y) (((x) < (y)) ? (x) : (y))
+#define MAX(x, y) (((x) > (y)) ? (x) : (y))
+
+//#define BUFFERSIZE 4096
+#define BUFFERSIZE 256
+
+/* gobal variables */
+
+char *progname = NULL;
+
+/* help function */
+
+void usage (int ret)
+{
+    FILE *fd = ret ? stderr : stdout;
+    fprintf (fd, "usage: %s [-i file] [-h] [-m k&r|ansi|c99] [-o file] [-v]\n", progname);
+    fprintf (fd, " -i : input file\n");
+    fprintf (fd, " -h : help message\n");
+    fprintf (fd, " -m : indent mode\n");
+    fprintf (fd, " -o : output file\n");
+    fprintf (fd, " -v : verbose level (%d)\n", verbose);
+
+    exit (ret);
+}
+
+/* indent function */
+
+int indent (FILE *fin, FILE *fout) {
+    char buffer[BUFFERSIZE + 1] = {0};
+
+    char *pt = buffer;
+    while (!feof (fin)) {
+        int nb = fread (pt, 1, BUFFERSIZE - (pt - buffer), fin);
+        VERBOSE (DEBUG, fprintf (stdout, "buffer: %d\n", nb));
+        pt = buffer;
+        int i = 0;
+        while (pt[i] != '\0') {
+            if (pt[i++] == '\n') {
+                pt[i - 1] = 0;
+
+                /* process line */
+                char *line = pt;
+                VERBOSE (DEBUG, fprintf (stdout, "line: %d\n", strlen (line)));
+                VERBOSE (DEBUG, fprintf (stdout, "out: %s\n", line));
+                int k = 0;
+                int begin = 0;
+                while (line[k] != '\0') {
+                    switch (line[k]) {
+                    case ' ':
+                    case '\t':
+                        if (begin) trailing++;
+                        break;
+                    default:
+                        begin = 0;
+
+                pt += i;
+                i = 0;
+            }
+        }
+        /* copy end buffer */
+        int j = 0;
+        if (pt - buffer < BUFFERSIZE) {
+            for (i = pt - buffer; i < BUFFERSIZE; i++) {
+                buffer[j++] = *pt++;
+            }
+        } 
+        pt = buffer + j;
+    }
+
+    /* close all */
+    fclose (fin);
+    fclose (fout);
+
+    return 0;
+}
+
+/* main function */
+
+int main (int argc, char *argv[]) 
+{
+    char *input = NULL;
+    char *mode = "ansi";
+    char *output = NULL;
+   
+    /* get basename */
+    char *pt = progname = argv[0];
+    while (*pt) {
+        if ((*pt == '/') || (*pt == '\\')) {
+           progname = pt + 1;
+        }
+        pt++;
+    }
+
+    int c;
+    while ((c = getopt(argc, argv, "i:hm:o:v:")) != EOF) {
+        switch (c) {
+        case 'i':
+            input = optarg;
+            break;
+        case 'm':
+            mode = optarg;
+            break;
+        case 'o':
+            output = optarg;
+            break;
+        case 'v':
+            verbose = atoi (optarg);
+            break;
+        case 'h':
+        default:
+            usage (c != 'h');
+        }
+    }
+    if (argc - optind != 0) {
+        fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]);
+        usage (1);
+    }
+
+    /* check input */
+    FILE *fin = NULL;
+    if (input) {
+        fin = fopen (input, "rb");
+        if (!fin) {
+            VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input));
+        }
+    } else {
+        fin = stdin;
+    }
+
+    /* check output */
+    FILE *fout = NULL;
+    if (output) {
+        fout = fopen (input, "wb");
+        if (!fout) {
+            VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
+            fclose (fin);
+        }
+    } else {
+        fout = stdout;
+    }
+
+    return indent (fin, fout);
+}
+
+// test: indent.exe -h
+// test: indent.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+// test: indent.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
+// test: indent.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+
+/* vim: set ts=4 sw=4 et: */