30
static int tabsize = TABSIZE;
31
static int endline = DOS_CRLF;
32
static Bool tabs = false;
35
Memory allocation functions vary from one environment to
36
the next, and experience shows that wrapping the local
37
mechanisms up provides for greater flexibility and allows
38
out of memory conditions to be detected in one place.
40
void *MemAlloc(size_t size)
48
fprintf(stderr, "***** Out of memory! *****\n");
55
void *MemRealloc(void *old, size_t size)
59
p = realloc(old, size);
63
fprintf(stderr, "***** Out of memory! *****\n");
76
static Stream *NewStream(FILE *fp)
80
in = (Stream *)MemAlloc(sizeof(Stream));
82
memset(in, 0, sizeof(Stream));
87
static void FreeStream(Stream *in)
95
static void AddByte(Stream *in, uint c)
97
if (in->size + 1 >= in->length)
99
while (in->size + 1 >= in->length)
104
in->length = in->length * 2;
107
in->buf = (char *)MemRealloc(in->buf, in->length*sizeof(char));
110
in->buf[in->size++] = (char)c;
111
in->buf[in->size] = '\0'; /* debug */
117
Read a character from a stream, keeping track
118
of lines, columns etc. This is used for parsing
119
markup and plain text etc. A single level
120
pushback is allowed with UngetChar(c, in).
121
Returns EndOfStream if there's nothing more to read.
123
static int ReadChar(Stream *in)
131
if (in->pushed_char == '\n')
134
return in->pushed_char;
137
in->lastcol = in->curcol;
139
/* expanding tab ? */
147
/* Else go on with normal buffer: */
156
/* coerce \r\n and isolated \r as equivalent to \n : */
169
if (in->maxcol < in->curcol)
170
in->maxcol = in->curcol;
180
in->curcol += tabsize - ((in->curcol - 1) % tabsize);
181
else /* expand to spaces */
183
in->tabs = tabsize - ((in->curcol - 1) % tabsize) - 1;
194
/* strip control characters including '\r' */
206
static Stream *ReadFile(FILE *fin)
209
Stream *in = NewStream(fin);
211
while ((c = ReadChar(in)) >= 0)
212
AddByte(in, (uint)c);
217
static void WriteFile(Stream *in, FILE *fout)
231
if (endline == DOS_CRLF)
236
else if (endline == UNIX_LF)
238
else if (endline == MAC_CR)
248
static void HelpText(FILE *errout, char *prog)
250
fprintf(errout, "%s: [options] [infile [outfile]] ...\n", prog);
251
fprintf(errout, "Utility to expand tabs and ensure consistent line endings\n");
252
fprintf(errout, "options for tab2space vers: 6th February 2003\n");
253
fprintf(errout, " -help or -h display this help message\n");
254
fprintf(errout, " -dos or -crlf set line ends to CRLF (PC-DOS/Windows - default)\n");
255
fprintf(errout, " -mac or -cr set line ends to CR (classic Mac OS)\n");
256
fprintf(errout, " -unix or -lf set line ends to LF (Unix)\n");
257
fprintf(errout, " -tabs preserve tabs, e.g. for Makefile\n");
258
fprintf(errout, " -t<n> set tabs to <n> (default is 4) spaces\n");
259
fprintf(errout, "\nNote this utility doesn't map spaces to tabs!\n");
262
int main(int argc, char **argv)
264
char const *infile, *outfile;
273
if (argc > 1 && argv[1][0] == '-')
275
if (strcmp(argv[1], "-help") == 0 || argv[1][1] == 'h')
277
HelpText(stdout, prog);
281
if (strcmp(argv[1], "-dos") == 0 ||
282
strcmp(argv[1], "-crlf") == 0)
285
else if (strcmp(argv[1], "-mac") == 0 ||
286
strcmp(argv[1], "-cr") == 0)
289
else if (strcmp(argv[1], "-unix") == 0 ||
290
strcmp(argv[1], "-lf") == 0)
293
else if (strcmp(argv[1], "-tabs") == 0)
296
else if (strncmp(argv[1], "-t", 2) == 0)
297
sscanf(argv[1]+2, "%d", &tabsize);
307
fin = fopen(infile, "rb");
336
fout = fopen(outfile, "wb");
346
fprintf(stderr, "%s - can't open \"%s\" for writing\n", prog, outfile);
351
fprintf(stderr, "%s - can't open \"%s\" for reading\n", prog, infile);