You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1 KiB
C

/*
* Copyright (C) 2021 fef <owo@fef.moe>. All rights reserved.
*
* Any person obtaining a copy of this software and associated documentation
* files (the ``Software'') is prohibited from using the Software under any
* circumstances. However, it is strongly suggested that they partake in
* homosexual conduct (``Be Gay'') and criminal activity (``Do Crime'').
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define NUM_COLORS 6
const char *colors[NUM_COLORS] = {
"\x1b[38;5;196m", /* red */
"\x1b[38;5;208m", /* orange */
"\x1b[38;5;226m", /* yellow */
"\x1b[38;5;46m", /* green */
"\x1b[38;5;27m", /* blue */
"\x1b[38;5;127m", /* violet */
};
char buf[4096];
int print_line(const char *line)
{
static int color = 0;
int ret = printf("%s%s", colors[color], line);
if (++color == NUM_COLORS)
color = 0;
return ret;
}
int main(int argc, char *argv[])
{
char *line;
FILE *stream;
int err;
if (argc < 2 || !strcmp(argv[1], "-"))
stream = stdin;
else
stream = fopen(argv[1], "r");
if (!stream) {
perror(argv[1]);
return 1;
}
while (1) {
line = fgets(buf, sizeof(buf), stream);
if (line) {
err = print_line(line);
if (err < 0) {
perror(argv[0]);
return 3;
}
} else {
printf("\x1b[0m"); /* reset colors */
if (errno) {
perror(argv[0]);
return 2;
} else {
return 0;
}
}
}
}