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
|
/* Copyright (C) 2014 jaseg <github@jaseg.net>
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#define _GNU_SOURCE //for fmemopen
#include <stdint.h>
#include <stdio.h>
#include <wchar.h>
#include <ctype.h>
#include <error.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <unistd.h>
#include <sys/time.h>
#include "fgetwc_fix.h"
static char helpstr[] = "\n"
"Usage: lolcat [-h horizontal_speed] [-v vertical_speed] [--] [FILES...]\n"
"\n"
"Concatenate FILE(s), or standard input, to standard output.\n"
"With no FILE, or when FILE is -, read standard input.\n"
"\n"
" -h <d>: Horizontal rainbow frequency (default: 0.23)\n"
" -v <d>: Vertical rainbow frequency (default: 0.1)\n"
" -f: Force color even when stdout is not a tty\n"
" --version: Print version and exit\n"
" --help: Show this message\n"
"\n"
"Examples:\n"
" lolcat f - g Output f's contents, then stdin, then g's contents.\n"
" lolcat Copy standard input to standard output.\n"
" fortune | lolcat Display a rainbow cookie.\n"
"\n"
"Report lolcat bugs to <http://www.github.org/jaseg/lolcat/issues>\n"
"lolcat home page: <http://www.github.org/jaseg/lolcat/>\n"
"Original idea: <http://www.github.org/busyloop/lolcat/>\n";
#define ARRAY_SIZE(foo) (sizeof(foo)/sizeof(foo[0]))
const char codes[] = {39,38,44,43,49,48,84,83,119,118,154,148,184,178,214,208,209,203,204,198,199,163,164,128,129,93,99,63,69,33};
/* CAUTION! this function uses a function-static variable! */
void find_escape_sequences(int c, int *state){
if(c == '\033'){ /* Escape sequence YAY */
*state = 1;
}else if(*state == 1){
if(('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
*state = 2;
}else{
*state = 0;
}
}
void usage(){
printf("Usage: lolcat [-h horizontal_speed] [-v vertical_speed] [--] [FILES...]\n");
exit(1);
}
void version(){
printf("lolcat version 0.1, (c) 2014 jaseg\n");
exit(0);
}
int main(int argc, char **argv){
int c, cc=-1, i, l=0;
int colors=(isatty(1) == 1);
double freq_h = 0.23, freq_v = 0.1;
struct timeval tv;
gettimeofday(&tv, NULL);
double offx = (tv.tv_sec%300)/300.0;
for(i=1;i<argc;i++){
char *endptr;
if(!strcmp(argv[i], "-h")){
if((++i)<argc){
freq_h = strtod(argv[i], &endptr);
if(*endptr)
usage();
}else{
usage();
}
}else if(!strcmp(argv[i], "-v")){
if((++i)<argc){
freq_v = strtod(argv[i], &endptr);
if(*endptr)
usage();
}else{
usage();
}
}else if(!strcmp(argv[i], "-f")){
colors = 1;
}else if(!strcmp(argv[i], "--version")){
version();
}else{
if(!strcmp(argv[i], "--"))
i++;
break;
}
}
char **inputs = argv+i;
char **inputs_end = argv+argc;
if(inputs == inputs_end){
char *foo[] = {"-"};
inputs = foo;
inputs_end = inputs+1;
}
setlocale(LC_ALL, "");
i=0;
for(char **filename=inputs; filename<inputs_end; filename++){
FILE *f = stdin;
int escape_state = 0;
if(!strcmp(*filename, "--help"))
f = fmemopen(helpstr, strlen(helpstr), "r");
else if(strcmp(*filename, "-"))
f = fopen(*filename, "r");
if(!f){
fprintf(stderr, "Cannot open input file \"%s\": %s\n", *filename, strerror(errno));
return 2;
}
while((c = _fgetwc_fixed(f)) > 0){
if(colors){
find_escape_sequences(c, &escape_state);
if(!escape_state){
if(c == '\n'){
l++;
i = 0;
}else if(!iscntrl(c)){
int ncc = offx*ARRAY_SIZE(codes) + (int)((i++)*freq_h + l*freq_v);
if(cc != ncc)
printf("\033[38;5;%hhum", codes[(cc = ncc) % ARRAY_SIZE(codes)]);
}
}
}
printf("%lc", c);
if(escape_state == 2)
printf("\033[38;5;%hhum", codes[cc % ARRAY_SIZE(codes)]);
}
printf("\n\033[0m");
cc = -1;
fclose(f);
if(c != WEOF && c != 0){
fprintf(stderr, "Error reading input file \"%s\": %s\n", *filename, strerror(errno));
return 2;
}
}
}
|