--- /dev/null
+/* 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: */