summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-10-23 16:12:09 -0400
committerJakob Kaivo <jkk@ung.org>2020-10-23 16:12:09 -0400
commit31ebc79503bee880ed8c22197f493f1a0141e50f (patch)
treefa6c4b0063aa32cbd9645fa5dd4a2e92386b971a
parentf216e17ee968179fea5d5355c9eafa1c66507305 (diff)
start outlinecb
-rw-r--r--cb.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/cb.c b/cb.c
new file mode 100644
index 0000000..602f5f6
--- /dev/null
+++ b/cb.c
@@ -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;
+}