diff options
-rw-r--r-- | cb.c | 77 |
1 files changed, 77 insertions, 0 deletions
@@ -0,0 +1,77 @@ +#define _XOPEN_SOURCE 700 +#include <ctype.h> +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +static void output(int level, char *buf) +{ + while (level-- > 0) { + putchar('\t'); + } + while (isblank(*buf)) { + buf++; + } + puts(buf); +} + +static int cb(const char *path) +{ + FILE *in = stdin; + if (path && strcmp(path, "-") != 0) { + in = fopen(path, "r"); + } + + if (in == NULL) { + fprintf(stderr, "cb: %s: %s\n", path, strerror(errno)); + return 1; + } + + int c; + char buf[LINE_MAX] = {0}; + size_t pos = 0; + int level = 0; + + while ((c = fgetc(in)) != EOF) { + buf[pos++] = c; + if (c == '{' || c == ';' || c == '}') { + output(level, buf); + memset(buf, '\0', sizeof(buf)); + pos = 0; + if (c == '{') { + level++; + } + if (c == '}') { + level--; + } + } + } + + if (in != stdin) { + fclose(in); + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + setlocale(LC_ALL, ""); + + int c; + while ((c = getopt(argc, argv, "")) != -1) { + switch (c) { + default: + return 1; + } + } + + int r = 0; + do { + r |= cb(argv[optind++]); + } while (optind < argc); + return r; +} |