aboutsummaryrefslogtreecommitdiff
path: root/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c196
1 files changed, 196 insertions, 0 deletions
diff --git a/parse.c b/parse.c
new file mode 100644
index 0000000..3134397
--- /dev/null
+++ b/parse.c
@@ -0,0 +1,196 @@
+/*
+ * parse.c - a small program to format the search results
+ * (and in the future also the info results).
+ *
+ * Copyright 2017, Einhard Leichtfuß
+ *
+ * This file is part of auria.
+ *
+ * auria is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * auria is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with auria. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Call as `parse <left-offset> <right-offset> [color]'. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+
+#define COLOR_NOCOLOR "\e[0m"
+#define COLOR_BVIOLET "\e[1;35m"
+#define COLOR_BWHITE "\e[1;38m"
+#define COLOR_BGREEN "\e[1;32m"
+
+#define cond_set_color(c) (color ? printf("%s", (c)) : 0)
+
+#define print_line_offset(offset) printf("%*c", (offset), ' ')
+
+#define print_word_offset(line_offset) \
+ (first ? print_line_offset(line_offset) : putchar(' '))
+
+_Bool color; /* colorized output */
+int k; /* left line offset */
+int j; /* right line offset */
+_Bool break_lines; /* set iff stdout is a tty */
+unsigned short width; /* terminal width, not relevant if ! break_lines */
+unsigned short n; /* actual broken line maximum length; excl. offsets */
+
+int main(int argc, char **argv)
+{
+ if (argc < 3)
+ return 1;
+
+ k = atoi(argv[1]);
+ j = atoi(argv[2]);
+
+ if (k < 0 || j < 0)
+ return 1;
+
+ color = 0;
+ if (argc >= 4 && strcmp(argv[3], "color") == 0)
+ {
+ color = 1;
+ }
+
+ if (isatty(STDOUT_FILENO))
+ {
+ break_lines = 1;
+
+ struct winsize w;
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1)
+ return 1;
+
+ width = w.ws_col;
+ n = width - k - j;
+ }
+ else
+ {
+ break_lines = 0;
+ }
+
+
+ /*
+ * Parse input line for line.
+ */
+ int c;
+
+ while (1)
+ {
+ c = getchar();
+ if (c == EOF || c == '\n')
+ return 1;
+
+ /* Read and print package name - delimited by space. */
+ cond_set_color(COLOR_NOCOLOR COLOR_BVIOLET);
+ fputs("aur/", stdout);
+ cond_set_color(COLOR_NOCOLOR COLOR_BWHITE);
+ while (1)
+ {
+ if (c == ' ')
+ break;
+ if (c == EOF || c == '\n')
+ return 1;
+ putchar(c);
+ c = getchar();
+ }
+ cond_set_color(COLOR_NOCOLOR);
+ putchar(' ');
+
+
+ /* Read and print version string - delimited by space. */
+ cond_set_color(COLOR_BGREEN);
+ while (1)
+ {
+ c = getchar();
+ if (c == ' ')
+ break;
+ if (c == EOF || c == '\n')
+ return 1;
+ putchar(c);
+ }
+ cond_set_color(COLOR_NOCOLOR);
+ putchar('\n');
+
+
+ /* Read description - delimited by newline - and print it broken into
+ * lines of max. n chars with an offset of k chars, iff break_lines
+ * is set to 1 */
+ int i = 0;
+ int wi = 0;
+ char word[n];
+ _Bool first = 1;
+
+ c = getchar();
+ while (1)
+ {
+ if (c == '\t' || c == '\v')
+ c = ' ';
+
+ if (c == '\n' || c == EOF)
+ {
+ if (wi != 0)
+ {
+ print_word_offset(k);
+ printf("%.*s\n", wi, word);
+ }
+ else if (! first)
+ putchar('\n');
+
+ if (c == '\n')
+ break;
+ else /* c == EOF */
+ return 0;
+ }
+ else if (c == ' ')
+ {
+ if (wi != 0)
+ {
+ i++;
+ print_word_offset(k);
+ printf("%.*s", wi, word);
+ first = 0;
+ wi = 0;
+ }
+ c = getchar();
+ }
+ else if (break_lines && i >= n)
+ {
+ if (first)
+ {
+ print_line_offset(k);
+ printf("%.*s\n", wi, word);
+ wi = 0;
+ i = 0;
+ }
+ else
+ {
+ putchar('\n');
+ first = 1;
+ i = wi;
+ }
+ }
+ else
+ {
+ word[wi] = c;
+ wi++;
+ i++;
+ c = getchar();
+ }
+ }
+ }
+
+ /* Should never be reached. */
+ return 0;
+}