-
Notifications
You must be signed in to change notification settings - Fork 88
/
aha.c
1213 lines (1140 loc) · 35.7 KB
/
aha.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
Alternatively, the contents of this file may be used under the terms
of the GNU Lesser General Public license version 2 or later (LGPLv2+),
in which case the provisions of LGPL License are applicable instead of
those above.
For feedback and questions about my Files and Projects please mail me,
Alexander Matthes (Ziz) , ziz_at_mailbox.org
*/
#define AHA_VERSION "0.5.1"
#define AHA_YEAR "2020"
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// table for vt220 character set, see also
// https://whitefiles.org/b1_s/1_free_guides/fg2cd/pgs/c03b.htm
const char ansi_vt220_character_set[256][16] =
{
"␀","␁","␂","␃","␄","␅","␆","␇", //00..07
"␈","␉","␊","␋","␌","␍","␎","␏", //08..0f
"␐","␑","␒","␓","␔","␕","␖","␗", //10..17
"␘","␙","␚","␛","␜","␝","␞","␟", //18..1f
" " ,"!" ,"\"" ,"#" ,"$" ,"%" ,"&" ,"'" , //20..27
"(" ,")" ,"*" ,"+" ,"," ,"-" ,"." ,"/" , //28..2f
"0" ,"1" ,"2" ,"3" ,"4" ,"5" ,"6" ,"7" , //30..37
"8" ,"9" ,":" ,";" ,"<" ,"=" ,">" ,"?" , //38..3f
"@" ,"A" ,"B" ,"C" ,"D" ,"E" ,"F" ,"G" , //40..47
"H" ,"I" ,"J" ,"K" ,"L" ,"M" ,"N" ,"O" , //48..4f
"P" ,"Q" ,"R" ,"S" ,"T" ,"U" ,"V" ,"W" , //50..57
"X" ,"Y" ,"Z" ,"[" ,"\\" ,"]" ,"^" ,"_" , //58..5f
"`" ,"a" ,"b" ,"c" ,"d" ,"e" ,"f" ,"g" , //60..67
"h" ,"i" ,"j" ,"k" ,"l" ,"m" ,"n" ,"o" , //68..6f
"p" ,"q" ,"r" ,"s" ,"t" ,"u" ,"v" ,"w" , //70..77
"x" ,"y" ,"z" ,"{" ,"|" ,"}" ,"~" ,"␡", //78..7f
"◆","▒","␉","␌","␍","␊","°","±", //80..87
"␀","␋","┘","┐","┌","└","┼","⎺", //88..8f
"⎻","─","⎼","⎽","├","┤","┴","┬", //90..97
"│","≤","≥","π ","≠","£" ,"•","␡", //98..9f
"█","¡","¢","£"," " ,"¥" ," " ,"§", //a0..a7
"¤","©","º","�qb;"," " ," " ," " ," " , //a8..af
"⎼","⎽","²","³","´","µ","¶","·", //b0..b7
"¸","¹","º","»","¼","½","¾","¿", //b8..bf
"À","Á","Â","Ã","Ä","Å","Æ","Ç", //c0..c7
"È","É","Ê","Ë","Ì","Í","Î","Ï", //c8..cf
" " ,"Ñ","Ò","Ó","Ô","Õ","Ö","Œ", //d0..d7
"Ø","Ù","Ú","Û","Ü","Ÿ"," " ,"ß", //d8..df
"à","á","â","ã","ä","å","æ","ç", //e0..e7
"è","é","ê","ë","ì","í","î","ï", //e8..ef
" " ,"ñ","ò","ó","ô","õ","ö","œ", //f0..f7
"ø","ù","ú","û","ü","ÿ"," " ,"█", //f8..ff
};
int getNextChar(register FILE* fp)
{
int c;
if ((c = fgetc(fp)) != EOF)
return c;
perror("Error while parsing input");
exit(EXIT_FAILURE);
}
typedef struct selem *pelem;
typedef struct selem {
unsigned char digit[8];
unsigned char digitcount;
long int value;
pelem next;
} telem;
pelem parseInsert(char* s)
{
pelem firstelem=NULL;
pelem momelem=NULL;
unsigned char digit[8];
unsigned char digitcount=0;
long int value=0;
int pos=0;
for (pos=0;pos<1024;pos++)
{
if (s[pos]=='[')
continue;
if (s[pos]==';' || s[pos]==':' || s[pos]==0)
{
if (digitcount==0)
{
digit[0]=0;
digitcount=1;
}
pelem newelem=(pelem)malloc(sizeof(telem));
if (newelem==NULL)
{
perror("Failed to allocate memory for parseInsert()");
exit(EXIT_FAILURE);
}
memcpy(newelem->digit, digit, sizeof(digit));
newelem->digitcount=digitcount;
newelem->value=value;
newelem->next=NULL;
if (momelem==NULL)
firstelem=newelem;
else
momelem->next=newelem;
momelem=newelem;
digitcount=0;
memset(digit,0,sizeof(digit));
value=0;
if (s[pos]==0)
break;
}
else
if (digitcount < sizeof(digit))
{
digit[digitcount]=s[pos]-'0';
value=(value*10)+digit[digitcount];
digitcount++;
}
}
return firstelem;
}
void deleteParse(pelem elem)
{
while (elem!=NULL)
{
pelem temp=elem->next;
free(elem);
elem=temp;
}
}
void printHtml(char *text) {
while(1) {
switch(*text) {
case '\0': return;
case '"': printf("""); break;
case '&': printf("&"); break;
case '<': printf("<"); break;
case '>': printf(">"); break;
default:
putc(*text, stdout);
}
++text;
}
}
enum ColorScheme {
SCHEME_WHITE,
SCHEME_BLACK,
SCHEME_PINK
};
struct Options {
enum ColorScheme colorscheme;
char* filename;
FILE *fp;
int htop_fix;
int iso;
int line_break;
int no_header;
int stylesheet;
char *title;
int word_wrap;
int no_xml;
char* lang;
char* css;
int ignore_cr;
char* bodystyle;
};
int divide (int dividend, int divisor){
div_t result;
result = div (dividend, divisor);
return result.quot;
}
void make_rgb (int color_id, char str_rgb[12]){
if (color_id < 16 || color_id > 255)
return;
if (color_id >= 232)
{
int index = color_id - 232;
int grey = index * 256 / 24;
sprintf(str_rgb, "%02x%02x%02x", grey, grey, grey);
return;
}
int index_R = divide((color_id - 16), 36);
int rgb_R;
if (index_R > 0){
rgb_R = 55 + index_R * 40;
} else {
rgb_R = 0;
}
int index_G = divide(((color_id - 16) % 36), 6);
int rgb_G;
if (index_G > 0){
rgb_G = 55 + index_G * 40;
} else {
rgb_G = 0;
}
int index_B = ((color_id - 16) % 6);
int rgb_B;
if (index_B > 0){
rgb_B = 55 + index_B * 40;
} else {
rgb_B = 0;
}
sprintf(str_rgb, "%02x%02x%02x", rgb_R, rgb_G, rgb_B);
}
#define VERSION_PRINTF_MAKRO \
printf("\033[1;31mAnsi Html Adapter\033[0m Version "AHA_VERSION"\n");
struct Options parseArgs(int argc, char* args[])
{
struct Options opts = (struct Options){
.colorscheme = SCHEME_WHITE,
.filename = NULL,
.fp = stdin,
.htop_fix = 0,
.iso = -1,
.line_break = 0,
.no_header = 0,
.stylesheet = 0,
.title = NULL,
.word_wrap = 0,
.no_xml = 0,
.lang = NULL,
.ignore_cr = 0,
.bodystyle = NULL
};
//Searching Parameters
for (int p = 1;p<argc;p++)
{
if ((strcmp(args[p],(char*)"--help")==0) || (strcmp(args[p],(char*)"-h")==0) || (strcmp(args[p],(char*)"-?")==0))
{
VERSION_PRINTF_MAKRO
printf("\033[1maha\033[0m takes SGR-colored Input and prints W3C conform HTML-Code\n");
printf("use: \033[1maha\033[0m <\033[4moptions\033[0m> [\033[4m-f file\033[0m]\n");
printf(" \033[1maha\033[0m (\033[4m--help\033[0m|\033[4m-h\033[0m|\033[4m-?\033[0m)\n");
printf("\033[1maha\033[0m reads the Input from a file or stdin and writes HTML-Code to stdout\n");
printf("\033[4mOptions\033[0m:\n");
printf(" --black, -b: \033[1;30m\033[1;47mBlack\033[0m Background and \033[1;37mWhite\033[0m \"standard color\"\n");
printf(" --pink, -p: \033[1;35mPink\033[0m Background\n");
printf(" --style X, -y X: Set the style used in the <body> element\n");
printf(" --stylesheet, -s: Use a stylesheet instead of inline styles\n");
printf(" --iso X, -i X: Uses ISO 8859-X instead of utf-8. X must be 1..16\n");
printf(" --title X, -t X: Gives the html output the title \"X\" instead of\n");
printf(" \"stdin\" or the filename\n");
printf(" --lang X, -L X: Uses the ISO 639-1 code X for the language\n");
printf(" --line-fix, -l: Uses a fix for inputs using control sequences to\n");
printf(" change the cursor position like htop. It's a hot fix,\n");
printf(" it may not work with any program like htop. Example:\n");
printf(" \033[1mecho\033[0m q | \033[1mhtop\033[0m | \033[1maha\033[0m -l > htop.htm\n");
printf(" --word-wrap, -w: Wrap long lines in the html file. This works with\n");
printf(" CSS3 supporting browsers as well as many older ones.\n");
printf(" --no-header, -n: Don't include header into generated HTML,\n");
printf(" useful for inclusion in full HTML files.\n");
printf(" --no-xml, -x: Don't use doctype xml but html (may useful for old \n");
printf(" browsers like IE)\n");
printf(" --css X, -c X: Add css file X to the output. In fact just adds \n");
printf(" <link rel=\"stylesheet\" href=\"X\" /> to the header.\n");
printf(" --ignore-cr, -r: Ignore all carriage-returns (ASCII sign 13, \\r)\n");
printf(" which may lead to double new lines in html.\n");
printf("\033[4mExamples\033[0m:\n");
printf(" Create an HTML file with a black background, a custom title and\n");
printf(" a larger font-size using \033[1maha\033[0m's help:\n");
printf("\n");
printf(" \033[1;3;33m$\033[0m \033[1maha\033[0m -h | \033[1maha\033[0m -b -t '\033[1maha\033[0m help' -y 'font-size:1.8em' > \033[1maha\033[0m-help.html\n");
printf("\n");
printf(" Create an HTML file with a white background using the output of \033[3mdiff\033[0m\n");
printf("\n");
printf(" \033[1;3;33m$\033[0m diff -u --color=always oldfile.c newfile.c | \033[1maha\033[0m > diff.html\n");
printf("\n");
printf(" Create an HTML file with a black background from the output of \033[3mhtop\033[0m.\n");
printf(" You have to use option -l due the other new-line-commands \033[3mhtop\033[0m uses:\n");
printf("\n");
printf(" \033[1;3;33m$\033[0m echo q | htop | \033[1maha\033[0m -b -l > htop.html\n");
printf("\n");
printf(" Create an HTML file from the output of this man page. \033[3mman\033[0m uses bold\n");
printf(" and underline formatting from \033[3mnroff\033[0m, which \033[3mul\033[0m converts to SGR:\n");
printf("\n");
printf(" \033[1;3;33m$\033[0m MAN_KEEP_FORMATTING=1 COLUMNS=80 man \033[1maha\033[0m | ul | \033[1maha\033[0m > man-\033[1maha\033[0m.html\n");
printf("\n");
printf("Copyleft \033[1;32mAlexander Matthes\033[0m aka \033[4mZiz\033[0m "AHA_YEAR"\n");
printf(" \033[5;[email protected]\033[0m\n");
printf(" \033[5;36mhttps://github.com/theZiz/aha\033[0m\n");
printf("This application is subject to the \033[1;34mMPL1.1\033[0m or \033[1;34mLGPLv2+\033[0m.\n");
exit(EXIT_SUCCESS);
}
else
if ((strcmp(args[p],(char*)"--version")==0) || (strcmp(args[p],(char*)"-v")==0))
{
VERSION_PRINTF_MAKRO
exit(EXIT_SUCCESS);
}
else
if ((strcmp(args[p],"--title")==0) || (strcmp(args[p],"-t")==0))
{
if (p+1>=argc)
{
fprintf(stderr,"No title given!\n");
exit(EXIT_FAILURE);
}
opts.title=args[p+1];
p++;
}
else
if ((strcmp(args[p],"--line-fix")==0) || (strcmp(args[p],"-l")==0))
{
opts.htop_fix=1;
}
else
if ((strcmp(args[p],"--no-header")==0) || (strcmp(args[p],"-n")==0))
{
opts.no_header=1;
}
else
if ((strcmp(args[p],"--word-wrap")==0) || (strcmp(args[p],"-w")==0))
opts.word_wrap=1;
else
if ((strcmp(args[p],"--black")==0) || (strcmp(args[p],"-b")==0))
opts.colorscheme=SCHEME_BLACK;
else
if ((strcmp(args[p],"--pink")==0) || (strcmp(args[p],"-p")==0))
opts.colorscheme=SCHEME_PINK;
else
if ((strcmp(args[p],"--stylesheet")==0) || (strcmp(args[p],"-s")==0))
opts.stylesheet=1;
else
if ((strcmp(args[p],"--iso")==0) || (strcmp(args[p],"-i")==0))
{
if (p+1>=argc)
{
fprintf(stderr,"No ISO code given!\n");
exit(EXIT_FAILURE);
}
opts.iso = atoi(args[p+1]);
if (opts.iso<1 || opts.iso>16)
{
fprintf(stderr,"not a valid ISO code: ISO 8859-%s\n",args[p+1]);
exit(EXIT_FAILURE);
}
p++;
}
else
if (strcmp(args[p],"-f")==0)
{
if (p+1>=argc)
{
fprintf(stderr,"no file to read given after \"-f\"!\n");
exit(EXIT_FAILURE);
}
opts.fp = fopen(args[p+1],"r");
if (opts.fp==NULL)
{
char *errstr = strerror(errno);
fprintf(stderr,"Failed to open file \"%s\": %s\n",args[p+1],errstr);
exit(EXIT_FAILURE);
}
p++;
opts.filename=args[p];
}
else
if ((strcmp(args[p],"--no-xml")==0) || (strcmp(args[p],"-x")==0))
opts.no_xml=1;
else
if ((strcmp(args[p],"--lang")==0) || (strcmp(args[p],"-L")==0))
{
if (p+1>=argc)
{
fprintf(stderr,"No ISO lang code given!\n");
exit(EXIT_FAILURE);
}
opts.lang = args[p+1];
p++;
}
else
if ((strcmp(args[p],"--css")==0) || (strcmp(args[p],"-c")==0))
{
if (p+1>=argc)
{
fprintf(stderr,"No css file given!\n");
exit(EXIT_FAILURE);
}
opts.css = args[p+1];
p++;
}
else
if ((strcmp(args[p],"--ignore-cr")==0) || (strcmp(args[p],"-r")==0))
opts.ignore_cr=1;
else
if ((strcmp(args[p],"--style")==0) || (strcmp(args[p],"-y")==0))
{
if (p+1>=argc)
{
fprintf(stderr,"No style given!\n");
exit(EXIT_FAILURE);
}
opts.bodystyle=args[p+1];
p++;
}
else
{
fprintf(stderr,"Unknown parameter \"%s\"\n",args[p]);
exit(EXIT_FAILURE);
}
}
return opts;
}
enum ColorMode {
MODE_3BIT,
MODE_8BIT,
MODE_24BIT
};
struct State {
int fc, bc;
int bold;
int italic;
int underline;
int blink;
int crossedout;
enum ColorMode fc_colormode;
enum ColorMode bc_colormode;
int highlighted; //for fc AND bc although not correct...
};
void swapColors(struct State *const state) {
if (state->bc_colormode == MODE_3BIT && state->bc == -1)
state->bc = 8;
if (state->fc_colormode == MODE_3BIT && state->fc == -1)
state->fc = 9;
int temp = state->bc;
state->bc = state->fc;
state->fc = temp;
enum ColorMode temp_colormode = state->bc_colormode;
state->bc_colormode = state->fc_colormode;
state->fc_colormode = temp_colormode;
}
const struct State default_state = {
.fc = -1, //Standard Foreground Color //IRC-Color+8
.bc = -1, //Standard Background Color //IRC-Color+8
.bold = 0,
.italic = 0,
.underline = 0,
.blink = 0,
.crossedout = 0,
.fc_colormode = MODE_3BIT,
.bc_colormode = MODE_3BIT,
.highlighted = 0,
};
int statesDiffer(const struct State *const old, const struct State *const new) {
return
(old->fc != new->fc) ||
(old->bc != new->bc) ||
(old->bold != new->bold) ||
(old->italic != new->italic) ||
(old->underline != new->underline) ||
(old->blink != new->blink) ||
(old->crossedout != new->crossedout) ||
(old->fc_colormode != new->fc_colormode) ||
(old->bc_colormode != new->bc_colormode) ||
(old->highlighted != new->highlighted);
}
void printHeader(const struct Options *opts)
{
char encoding[16] = "UTF-8";
if(opts->iso>0) snprintf(encoding, sizeof(encoding), "ISO-8859-%i", opts->iso);
if (opts->no_xml)
printf("<!DOCTYPE html>\n");
else
printf("<?xml version=\"1.0\" encoding=\"%s\" ?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n", encoding);
printf("<!-- This file was created with the aha Ansi HTML Adapter. https://github.com/theZiz/aha -->\n");
if (opts->no_xml)
{
if (opts->lang)
{
printf("<html lang=\"");
printHtml(opts->lang);
printf("\">\n");
}
else
{
printf("<html>\n");
}
printf("<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n", encoding);
}
else
{
if (opts->lang)
{
printf("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"");
printHtml(opts->lang);
printf("\">\n");
}
else
{
printf("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
}
printf("<head>\n<meta http-equiv=\"Content-Type\" content=\"application/xml+xhtml; charset=%s\"/>\n", encoding);
}
printf("<title>");
printHtml(opts->title ? opts->title : opts->filename ? opts->filename : "stdin");
printf("</title>\n");
if (opts->css)
{
printf("<link rel=\"stylesheet\" href=\"");
printHtml(opts->css);
printf("\">\n");
}
int style_tag = 0;
if (opts->stylesheet)
{
printf("<style type=\"text/css\">\n");
style_tag = 1;
switch (opts->colorscheme)
{
case SCHEME_BLACK: printf("body {color: white; background-color: black;}\n");
printf(".reset {color: white;}\n");
printf(".bg-reset {background-color: black;}\n");
printf(".inverted {color: black;}\n");
printf(".bg-inverted {background-color: white;}\n");
break;
case SCHEME_PINK: printf("body {background-color: pink;}\n");
printf(".reset {color: black;}\n");
printf(".bg-reset {background-color: pink;}\n");
printf(".inverted {color: pink;}\n");
printf(".bg-inverted {background-color: black;}\n");
break;
default: printf(".reset {color: black;}\n");
printf(".bg-reset {background-color: white;}\n");
printf(".inverted {color: white;}\n");
printf(".bg-inverted {background-color: black;}\n");
}
if (opts->colorscheme != SCHEME_BLACK)
{
printf(".dimgray {color: dimgray;}\n");
printf(".red {color: red;}\n");
printf(".green {color: green;}\n");
printf(".yellow {color: olive;}\n");
printf(".blue {color: blue;}\n");
printf(".purple {color: purple;}\n");
printf(".cyan {color: teal;}\n");
printf(".white {color: gray;}\n");
printf(".bg-black {background-color: black;}\n");
printf(".bg-red {background-color: red;}\n");
printf(".bg-green {background-color: green;}\n");
printf(".bg-yellow {background-color: olive;}\n");
printf(".bg-blue {background-color: blue;}\n");
printf(".bg-purple {background-color: purple;}\n");
printf(".bg-cyan {background-color: teal;}\n");
printf(".bg-white {background-color: gray;}\n");
}
else
{
printf(".dimgray {color: dimgray;}\n");
printf(".red {color: red;}\n");
printf(".green {color: lime;}\n");
printf(".yellow {color: yellow;}\n");
printf(".blue {color: #3333FF;}\n");
printf(".purple {color: fuchsia;}\n");
printf(".cyan {color: aqua;}\n");
printf(".white {color: white;}\n");
printf(".bg-black {background-color: black;}\n");
printf(".bg-red {background-color: red;}\n");
printf(".bg-green {background-color: lime;}\n");
printf(".bg-yellow {background-color: yellow;}\n");
printf(".bg-blue {background-color: #3333FF;}\n");
printf(".bg-purple {background-color: fuchsia;}\n");
printf(".bg-cyan {background-color: aqua;}\n");
printf(".bg-white {background-color: white;}\n");
}
printf(".underline {text-decoration: underline;}\n");
printf(".bold {font-weight: bold;}\n");
printf(".italic {font-style: italic;}\n");
printf(".blink {text-decoration: blink;}\n");
printf(".crossed-out {text-decoration: line-through;}\n");
printf(".highlighted {filter: contrast(70%%) brightness(190%%);}\n");
}
if (opts->word_wrap)
{
if (!style_tag) {
printf("<style type=\"text/css\">\n");
style_tag = 1;
}
printf("pre {white-space: pre-wrap; white-space: -moz-pre-wrap !important;\n");
printf("white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;}\n");
}
if (style_tag)
printf("</style>\n");
printf("</head>\n");
if (opts->stylesheet)
{
printf("<body>\n");
}
else
{
printf("<body");
if(opts->bodystyle || opts->colorscheme==SCHEME_BLACK || opts->colorscheme==SCHEME_PINK)
{
int styles=0;
printf(" style=\"");
if(opts->colorscheme==SCHEME_BLACK) {
++styles;
printf("color:white; background-color:black");
} else if(opts->colorscheme==SCHEME_PINK) {
++styles;
printf("background-color:pink");
}
if(opts->bodystyle) {
if(styles) printf(";");
printHtml(opts->bodystyle);
}
printf("\"");
}
printf(">\n");
}
printf("<pre>\n");
}
int main(int argc,char* args[])
{
struct Options opts = parseArgs(argc, args);
register FILE* fp = opts.fp;
char* fcstyle[10] = {
opts.stylesheet ? "dimgray " : "color:dimgray;", //Black
opts.stylesheet ? "red " : "color:red;", //Red
opts.stylesheet ? "green " : opts.colorscheme==SCHEME_BLACK ? "color:lime;" : "color:green;", //Green
opts.stylesheet ? "yellow " : opts.colorscheme==SCHEME_BLACK ? "color:yellow;" : "color:olive;", //Yellow
opts.stylesheet ? "blue " : opts.colorscheme==SCHEME_BLACK ? "color:#3333FF;" : "color:blue;", //Blue
opts.stylesheet ? "purple " : opts.colorscheme==SCHEME_BLACK ? "color:fuchsia;" : "color:purple;", //Purple
opts.stylesheet ? "cyan " : opts.colorscheme==SCHEME_BLACK ? "color:aqua;" : "color:teal;", //Cyan
opts.stylesheet ? "white " : opts.colorscheme==SCHEME_BLACK ? "color:white;" : "color:gray;", //White
opts.stylesheet ? "inverted " : opts.colorscheme==SCHEME_BLACK ? "color:black;" : opts.colorscheme==SCHEME_PINK ? "color:pink;" : "color:white;", //Background
opts.stylesheet ? "reset " : opts.colorscheme==SCHEME_BLACK ? "color:white;" : "color:black;" //Foreground
};
char* bcstyle[10] = {
opts.stylesheet ? "bg-black " : "background-color:black;", //Black
opts.stylesheet ? "bg-red " : "background-color:red;", //Red
opts.stylesheet ? "bg-green " : opts.colorscheme==SCHEME_BLACK ? "background-color:lime;" : "background-color:green;", //Green
opts.stylesheet ? "bg-yellow " : opts.colorscheme==SCHEME_BLACK ? "background-color:yellow;" : "background-color:olive;", //Yellow
opts.stylesheet ? "bg-blue " : opts.colorscheme==SCHEME_BLACK ? "background-color:#3333FF;" : "background-color:blue;", //Blue
opts.stylesheet ? "bg-purple " : opts.colorscheme==SCHEME_BLACK ? "background-color:fuchsia;" : "background-color:purple;", //Purple
opts.stylesheet ? "bg-cyan " : opts.colorscheme==SCHEME_BLACK ? "background-color:aqua;" : "background-color:teal;", //Cyan
opts.stylesheet ? "bg-white " : opts.colorscheme==SCHEME_BLACK ? "background-color:white;" : "background-color:gray;", //White
opts.stylesheet ? "bg-reset " : opts.colorscheme==SCHEME_BLACK ? "background-color:black;" : opts.colorscheme==SCHEME_PINK ? "background-color:pink;" : "background-color:white;", //Background
opts.stylesheet ? "bg-inverted " : opts.colorscheme==SCHEME_BLACK ? "background-color:white;" : "background-color:black;", //Foreground
};
if (!opts.no_header)
printHeader(&opts);
//Begin of Conversion
struct State state = default_state;
struct State oldstate;
int c;
int negative = 0; //No negative image
int special_char = 0; //No special characters
int line=0;
int momline=0;
int newline=-1;
while ((c=fgetc(fp)) != EOF)
{
if (c=='\033')
{
oldstate = state;
//Searching the end (a letter) and safe the insert:
c=getNextChar(fp);
if ( c == '[' ) // CSI code, see https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
{
char buffer[1024];
buffer[0] = '[';
int counter=1;
while ((c<'A') || ((c>'Z') && (c<'a')) || (c>'z'))
{
c=getNextChar(fp);
buffer[counter]=c;
if (c=='>') //end of htop
break;
counter++;
if (counter>1022)
break;
}
buffer[counter-1]=0;
pelem elem;
switch (c)
{
case 'm':
elem=parseInsert(buffer);
pelem momelem=elem;
while (momelem!=NULL)
{
switch (momelem->value)
{
case 0: // 0 - Reset all
state = default_state;
negative=0; special_char=0;
break;
case 1: // 1 - Enable Bold
state.bold=1;
break;
case 3: // 3 - Enable Italic
state.italic=1;
break;
case 4: // 4 - Enable underline
state.underline=1;
break;
case 5: // 5 - Slow Blink
state.blink=1;
break;
case 7: // 7 - Inverse video
swapColors(&state);
negative = !negative;
break;
case 9: // 9 - Enable Crossed-out
state.crossedout=1;
break;
case 21: // 21 - Reset bold
case 22: // 22 - Not bold, not "high intensity" color
state.bold=0;
break;
case 23: // 23 - Reset italic
state.italic=0;
break;
case 24: // 23 - Reset underline
state.underline=0;
break;
case 25: // 25 - Reset blink
state.blink=0;
break;
case 27: // 27 - Reset Inverted
if (negative)
{
swapColors(&state); //7, 7X is not defined (and supported)
negative = 0;
}
break;
case 29: // 29 - Reset crossed-out
state.crossedout=0;
break;
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38: // 3X - Set foreground color
{
int *dest = &(state.fc);
if (negative != 0)
dest=&(state.bc);
if (momelem->value == 38 &&
momelem->next &&
momelem->next->value == 5 &&
momelem->next->next)// 38;5;<n> -> 8 Bit
{
momelem = momelem->next->next;
state.fc_colormode = MODE_8BIT;
if (momelem->value >=8 && momelem->value <=15)
{
state.highlighted = 1;
*dest = momelem->value-8;
}
else
{
state.highlighted = 0;
*dest = momelem->value;
}
}
else
if (momelem->value == 38 &&
momelem->next &&
momelem->next->value == 2 &&
momelem->next->next)// 38;2;<n> -> 24 Bit
{
momelem = momelem->next->next;
pelem r,g,b;
r = momelem;
momelem = momelem->next;
g = momelem;
if ( momelem )
momelem = momelem->next;
b = momelem;
if ( r && g && b )
{
state.highlighted = 0;
state.fc_colormode = MODE_24BIT;
*dest =
(r->value & 255) * 65536 +
(g->value & 255) * 256 +
(b->value & 255);
}
}
else
{
state.fc_colormode = MODE_3BIT;
state.highlighted = 0;
*dest=momelem->value-30;
}
}
break;
case 39: // Set foreground color to default
state.fc_colormode = MODE_3BIT;
state.highlighted = 0;
state.fc = -1;
break;
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
case 48: // 4X - Set background color
{
int *dest = &(state.bc);
if (negative != 0)
dest=&(state.fc);
if (momelem->value == 48 &&
momelem->next &&
momelem->next->value == 5 &&
momelem->next->next)// 48;5;<n> -> 8 Bit
{
momelem = momelem->next->next;
state.bc_colormode = MODE_8BIT;
if (momelem->value >=8 && momelem->value <=15)
{
state.highlighted = 1;
*dest = momelem->value-8;
}
else
{
state.highlighted = 0;
*dest = momelem->value;
}
}
else
if (momelem->value == 48 &&
momelem->next &&
momelem->next->value == 2 &&
momelem->next->next)// 48;2;<n> -> 24 Bit
{
momelem = momelem->next->next;
pelem r,g,b;
r = momelem;
momelem = momelem->next;
g = momelem;
if ( momelem )
momelem = momelem->next;
b = momelem;
if ( r && g && b )
{
state.bc_colormode = MODE_24BIT;
state.highlighted = 0;
*dest =
(r->value & 255) * 65536 +
(g->value & 255) * 256 +
(b->value & 255);
}
}
else
{
state.bc_colormode = MODE_3BIT;
state.highlighted = 0;
*dest=momelem->value-40;
}
}
break;
case 49: // Set background color to default
state.bc_colormode = MODE_3BIT;
state.highlighted = 0;
state.bc = -1;
break;
case 90:
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97: // 9X - Set foreground color highlighted
{
int *dest = &(state.fc);
if (negative != 0)
dest=&(state.bc);
state.fc_colormode = MODE_3BIT;
state.highlighted = 1;
*dest=momelem->value-90;
}
break;
case 100:
case 101:
case 102:
case 103:
case 104:
case 105:
case 106:
case 107: // 10X - Set background color highlighted
{
int *dest = &(state.bc);
if (negative != 0)
dest=&(state.fc);
state.bc_colormode = MODE_3BIT;
state.highlighted = 1;
*dest=momelem->value-100;
}
break;
}
momelem=momelem->next;
}
deleteParse(elem);
break;
case 'H':
if (opts.htop_fix) //a little dirty ...
{
elem=parseInsert(buffer);
pelem second=elem->next;
if (second==NULL)
second=elem;
newline=second->digit[0]-1;
if (second->digitcount>1)
newline=(newline+1)*10+second->digit[1]-1;
if (second->digitcount>2)
newline=(newline+1)*10+second->digit[2]-1;
deleteParse(elem);
if (newline<line)
opts.line_break=1;
}
break;
}
if (opts.htop_fix)
if (opts.line_break)
{
for (;line<80;line++)
printf(" ");
}