init prog
[indent.git] / indent.c
CommitLineData
b811a28a
LM
1/* depend: */
2/* cflags: */
3/* linker: debug.o */
4
5#include <assert.h>
6#include <getopt.h>
7#include <malloc.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "debug.h"
13
14/* macros */
15
16#define CEIL(x, y) (((x) + (y) - 1) / (y))
17#define MIN(x, y) (((x) < (y)) ? (x) : (y))
18#define MAX(x, y) (((x) > (y)) ? (x) : (y))
19
20//#define BUFFERSIZE 4096
21#define BUFFERSIZE 256
22
23/* gobal variables */
24
25char *progname = NULL;
26
27/* help function */
28
29void usage (int ret)
30{
31 FILE *fd = ret ? stderr : stdout;
32 fprintf (fd, "usage: %s [-i file] [-h] [-m k&r|ansi|c99] [-o file] [-v]\n", progname);
33 fprintf (fd, " -i : input file\n");
34 fprintf (fd, " -h : help message\n");
35 fprintf (fd, " -m : indent mode\n");
36 fprintf (fd, " -o : output file\n");
37 fprintf (fd, " -v : verbose level (%d)\n", verbose);
38
39 exit (ret);
40}
41
42/* indent function */
43
44int indent (FILE *fin, FILE *fout) {
45 char buffer[BUFFERSIZE + 1] = {0};
46
47 char *pt = buffer;
48 while (!feof (fin)) {
49 int nb = fread (pt, 1, BUFFERSIZE - (pt - buffer), fin);
50 VERBOSE (DEBUG, fprintf (stdout, "buffer: %d\n", nb));
51 pt = buffer;
52 int i = 0;
53 while (pt[i] != '\0') {
54 if (pt[i++] == '\n') {
55 pt[i - 1] = 0;
56
57 /* process line */
58 char *line = pt;
59 VERBOSE (DEBUG, fprintf (stdout, "line: %d\n", strlen (line)));
60 VERBOSE (DEBUG, fprintf (stdout, "out: %s\n", line));
61 int k = 0;
62 int begin = 0;
63 while (line[k] != '\0') {
64 switch (line[k]) {
65 case ' ':
66 case '\t':
67 if (begin) trailing++;
68 break;
69 default:
70 begin = 0;
71
72 pt += i;
73 i = 0;
74 }
75 }
76 /* copy end buffer */
77 int j = 0;
78 if (pt - buffer < BUFFERSIZE) {
79 for (i = pt - buffer; i < BUFFERSIZE; i++) {
80 buffer[j++] = *pt++;
81 }
82 }
83 pt = buffer + j;
84 }
85
86 /* close all */
87 fclose (fin);
88 fclose (fout);
89
90 return 0;
91}
92
93/* main function */
94
95int main (int argc, char *argv[])
96{
97 char *input = NULL;
98 char *mode = "ansi";
99 char *output = NULL;
100
101 /* get basename */
102 char *pt = progname = argv[0];
103 while (*pt) {
104 if ((*pt == '/') || (*pt == '\\')) {
105 progname = pt + 1;
106 }
107 pt++;
108 }
109
110 int c;
111 while ((c = getopt(argc, argv, "i:hm:o:v:")) != EOF) {
112 switch (c) {
113 case 'i':
114 input = optarg;
115 break;
116 case 'm':
117 mode = optarg;
118 break;
119 case 'o':
120 output = optarg;
121 break;
122 case 'v':
123 verbose = atoi (optarg);
124 break;
125 case 'h':
126 default:
127 usage (c != 'h');
128 }
129 }
130 if (argc - optind != 0) {
131 fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]);
132 usage (1);
133 }
134
135 /* check input */
136 FILE *fin = NULL;
137 if (input) {
138 fin = fopen (input, "rb");
139 if (!fin) {
140 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input));
141 }
142 } else {
143 fin = stdin;
144 }
145
146 /* check output */
147 FILE *fout = NULL;
148 if (output) {
149 fout = fopen (input, "wb");
150 if (!fout) {
151 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
152 fclose (fin);
153 }
154 } else {
155 fout = stdout;
156 }
157
158 return indent (fin, fout);
159}
160
161// test: indent.exe -h
162// test: indent.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
163// test: indent.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
164// test: indent.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
165
166/* vim: set ts=4 sw=4 et: */