aboutsummaryrefslogtreecommitdiff
path: root/parse.c
blob: bfe43fa06f3da64a3ed437c93dfff9513c9c73b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * parse.c - a small program to format the search results
 *           (and in the future also the info results).
 *
 * Copyright 2017, 2018 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 */

_Bool passthrough_word();


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);
		ungetc(c, stdin);
		if (! passthrough_word()) return 1;
		cond_set_color(COLOR_NOCOLOR);
		putchar(' ');


		/* Read and print version string - delimited by space. */
		cond_set_color(COLOR_BGREEN);
		if (! passthrough_word()) return 1;
		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;
}


_Bool passthrough_word()
{
	char c;

	while (1)
	{
		c = getchar();
		if (c == ' ')
			return 1;
		if (c == EOF || c == '\n')
			return 0;
		putchar(c);
	}
}