-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiled.cpp
More file actions
1120 lines (1029 loc) · 33.4 KB
/
Copy pathcompiled.cpp
File metadata and controls
1120 lines (1029 loc) · 33.4 KB
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
//Aidan Wolter
//Allows program to rewrite themselves
#include <iostream>
#include <vector>
using namespace std;
//create the program from the string then tack on the program string at the end
extern string replicator_str;
void rewrite_program(string str, vector<string> *lines);
void rewrite_program(string str, vector<string> *lines) {
//PROGRAM compile
//change all the " to \"
//change all the \ to \\ .
string actual_str = string(str);
size_t index_quote = 0;
size_t index_slash = 0;
index_quote = actual_str.find("\"", 0);
index_slash = actual_str.find("\\", 0);
while((index_quote != string::npos) || (index_slash != string::npos)) {
if(index_quote < index_slash) {
actual_str.replace(index_quote, 1, "\\\"");
index_slash = actual_str.find("\\", index_quote+2);
index_quote = actual_str.find("\"", index_quote+2);
}
else {
actual_str.replace(index_slash, 1, "\\\\\\\\");
index_quote = actual_str.find("\"", index_slash+4);
index_slash = actual_str.find("\\", index_slash+4);
}
}
string str_full;
str_full += actual_str + "\n";
str_full += "string program_str = \\\"\\\"\n";
//for all strings to a newline
size_t index = 0;
size_t last_index = 0;
index = str.find("\n", index);
while(index != string::npos) {
//change all the " to \\\"
//change all the \ to \\\\ .
string line = str.substr(last_index, index-last_index);
index_quote = 0;
index_slash = 0;
index_quote = line.find("\"", 0);
index_slash = line.find("\\", 0);
while((index_quote != string::npos) || (index_slash != string::npos)) {
if(index_quote < index_slash) {
line.replace(index_quote, 1, "\\\\\\\"");
index_slash = line.find("\\", index_quote+4);
index_quote = line.find("\"", index_quote+4);
}
else {
line.replace(index_slash, 1, "\\\\\\\\\\\\\\\\");
index_quote = line.find("\"", index_slash+8);
index_slash = line.find("\\", index_slash+8);
}
}
str_full += "\\\"" + line + "\\\\\\\\n\\\"\n";
index++;
last_index = index;
index = str.find("\n", index);
}
str_full += ";\n";
//REPLICATOR compile
//change all the " to \"
//change all the \ to \\ .
string replicator_actual_str = string(replicator_str);
index_quote = 0;
index_slash = 0;
index_quote = replicator_actual_str.find("\"", 0);
index_slash = replicator_actual_str.find("\\", 0);
while((index_quote != string::npos) || (index_slash != string::npos)) {
if(index_quote < index_slash) {
replicator_actual_str.replace(index_quote, 1, "\\\"");
index_slash = replicator_actual_str.find("\\", index_quote+2);
index_quote = replicator_actual_str.find("\"", index_quote+2);
}
else {
replicator_actual_str.replace(index_slash, 1, "\\\\\\\\");
index_quote = replicator_actual_str.find("\"", index_slash+4);
index_slash = replicator_actual_str.find("\\", index_slash+4);
}
}
str_full += replicator_actual_str + "\n";
str_full += "string replicator_str = \\\"\\\"\n";
//for all strings to a newline
index = 0;
last_index = 0;
index = replicator_str.find("\n", index);
while(index != string::npos) {
//change all the " to \\\"
//change all the \ to \\\\ .
string line = replicator_str.substr(last_index, index-last_index);
index_quote = 0;
index_slash = 0;
index_quote = line.find("\"", 0);
index_slash = line.find("\\", 0);
while((index_quote != string::npos) || (index_slash != string::npos)) {
if(index_quote < index_slash) {
line.replace(index_quote, 1, "\\\\\\\"");
index_slash = line.find("\\", index_quote+4);
index_quote = line.find("\"", index_quote+4);
}
else {
line.replace(index_slash, 1, "\\\\\\\\\\\\\\\\");
index_quote = line.find("\"", index_slash+8);
index_slash = line.find("\\", index_slash+8);
}
}
str_full += "\\\"" + line + "\\\\\\\\n\\\"\n";
index++;
last_index = index;
index = replicator_str.find("\n", index);
}
str_full += ";";
//COMPILE and rewrite the program
//create the echo command to pipe into gcc
string echo_command = "echo \"" + str_full + "\"";
//create the entire command for compiling
string compile_command = echo_command + " | g++-4.9 -o it -xc++ -lncurses -std=c++11 - 2>&1";
//compile the program and replace
FILE *output = popen(compile_command.c_str(), "r");
lines->clear();
char cline[1024];
while(fgets(cline, 1024, output)) {
string line = cline;
lines->push_back(line);
}
return;
}
string replicator_str = ""
"//Aidan Wolter\n"
"//Allows program to rewrite themselves\n"
"\n"
"#include <iostream>\n"
"#include <vector>\n"
"\n"
"using namespace std;\n"
"\n"
"//create the program from the string then tack on the program string at the end\n"
"extern string replicator_str;\n"
"void rewrite_program(string str, vector<string> *lines);\n"
"void rewrite_program(string str, vector<string> *lines) {\n"
"\n"
" //PROGRAM compile\n"
"\n"
" //change all the \" to \\\"\n"
" //change all the \\ to \\\\ .\n"
" string actual_str = string(str);\n"
" size_t index_quote = 0;\n"
" size_t index_slash = 0;\n"
" index_quote = actual_str.find(\"\\\"\", 0);\n"
" index_slash = actual_str.find(\"\\\\\", 0);\n"
" while((index_quote != string::npos) || (index_slash != string::npos)) {\n"
" if(index_quote < index_slash) {\n"
" actual_str.replace(index_quote, 1, \"\\\\\\\"\");\n"
" index_slash = actual_str.find(\"\\\\\", index_quote+2);\n"
" index_quote = actual_str.find(\"\\\"\", index_quote+2);\n"
" }\n"
" else {\n"
" actual_str.replace(index_slash, 1, \"\\\\\\\\\\\\\\\\\");\n"
" index_quote = actual_str.find(\"\\\"\", index_slash+4);\n"
" index_slash = actual_str.find(\"\\\\\", index_slash+4);\n"
" }\n"
" }\n"
"\n"
" string str_full;\n"
" str_full += actual_str + \"\\n\";\n"
" str_full += \"string program_str = \\\\\\\"\\\\\\\"\\n\";\n"
"\n"
" //for all strings to a newline\n"
" size_t index = 0;\n"
" size_t last_index = 0;\n"
" index = str.find(\"\\n\", index);\n"
" while(index != string::npos) {\n"
"\n"
" //change all the \" to \\\\\\\"\n"
" //change all the \\ to \\\\\\\\ .\n"
" string line = str.substr(last_index, index-last_index);\n"
" index_quote = 0;\n"
" index_slash = 0;\n"
" index_quote = line.find(\"\\\"\", 0);\n"
" index_slash = line.find(\"\\\\\", 0);\n"
" while((index_quote != string::npos) || (index_slash != string::npos)) {\n"
" if(index_quote < index_slash) {\n"
" line.replace(index_quote, 1, \"\\\\\\\\\\\\\\\"\");\n"
" index_slash = line.find(\"\\\\\", index_quote+4);\n"
" index_quote = line.find(\"\\\"\", index_quote+4);\n"
" }\n"
" else {\n"
" line.replace(index_slash, 1, \"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");\n"
" index_quote = line.find(\"\\\"\", index_slash+8);\n"
" index_slash = line.find(\"\\\\\", index_slash+8);\n"
" }\n"
" }\n"
"\n"
" str_full += \"\\\\\\\"\" + line + \"\\\\\\\\\\\\\\\\n\\\\\\\"\\n\";\n"
" index++;\n"
" last_index = index;\n"
" index = str.find(\"\\n\", index);\n"
" }\n"
" str_full += \";\\n\";\n"
"\n"
" //REPLICATOR compile\n"
"\n"
" //change all the \" to \\\"\n"
" //change all the \\ to \\\\ .\n"
" string replicator_actual_str = string(replicator_str);\n"
" index_quote = 0;\n"
" index_slash = 0;\n"
" index_quote = replicator_actual_str.find(\"\\\"\", 0);\n"
" index_slash = replicator_actual_str.find(\"\\\\\", 0);\n"
" while((index_quote != string::npos) || (index_slash != string::npos)) {\n"
" if(index_quote < index_slash) {\n"
" replicator_actual_str.replace(index_quote, 1, \"\\\\\\\"\");\n"
" index_slash = replicator_actual_str.find(\"\\\\\", index_quote+2);\n"
" index_quote = replicator_actual_str.find(\"\\\"\", index_quote+2);\n"
" }\n"
" else {\n"
" replicator_actual_str.replace(index_slash, 1, \"\\\\\\\\\\\\\\\\\");\n"
" index_quote = replicator_actual_str.find(\"\\\"\", index_slash+4);\n"
" index_slash = replicator_actual_str.find(\"\\\\\", index_slash+4);\n"
" }\n"
" }\n"
"\n"
" str_full += replicator_actual_str + \"\\n\";\n"
" str_full += \"string replicator_str = \\\\\\\"\\\\\\\"\\n\";\n"
"\n"
" //for all strings to a newline\n"
" index = 0;\n"
" last_index = 0;\n"
" index = replicator_str.find(\"\\n\", index);\n"
" while(index != string::npos) {\n"
"\n"
" //change all the \" to \\\\\\\"\n"
" //change all the \\ to \\\\\\\\ .\n"
" string line = replicator_str.substr(last_index, index-last_index);\n"
" index_quote = 0;\n"
" index_slash = 0;\n"
" index_quote = line.find(\"\\\"\", 0);\n"
" index_slash = line.find(\"\\\\\", 0);\n"
" while((index_quote != string::npos) || (index_slash != string::npos)) {\n"
" if(index_quote < index_slash) {\n"
" line.replace(index_quote, 1, \"\\\\\\\\\\\\\\\"\");\n"
" index_slash = line.find(\"\\\\\", index_quote+4);\n"
" index_quote = line.find(\"\\\"\", index_quote+4);\n"
" }\n"
" else {\n"
" line.replace(index_slash, 1, \"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");\n"
" index_quote = line.find(\"\\\"\", index_slash+8);\n"
" index_slash = line.find(\"\\\\\", index_slash+8);\n"
" }\n"
" }\n"
"\n"
" str_full += \"\\\\\\\"\" + line + \"\\\\\\\\\\\\\\\\n\\\\\\\"\\n\";\n"
" index++;\n"
" last_index = index;\n"
" index = replicator_str.find(\"\\n\", index);\n"
" }\n"
" str_full += \";\";\n"
"\n"
" //COMPILE and rewrite the program\n"
"\n"
" //create the echo command to pipe into gcc\n"
" string echo_command = \"echo \\\"\" + str_full + \"\\\"\";\n"
"\n"
" //create the entire command for compiling\n"
" string compile_command = echo_command + \" | g++-4.9 -o it -xc++ -lncurses -std=c++11 - 2>&1\";\n"
"\n"
" //compile the program and replace\n"
" FILE *output = popen(compile_command.c_str(), \"r\");\n"
" lines->clear();\n"
"\n"
" char cline[1024];\n"
" while(fgets(cline, 1024, output)) {\n"
" string line = cline;\n"
" lines->push_back(line);\n"
" }\n"
"\n"
" return;\n"
"}\n"
"\n"
;
#include <iostream>
#include <sstream>
#include <vector>
#include <curses.h>
using namespace std;
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
void rewrite_program(string str, vector<string> *lines);
void rewrite_buffer(int start, int x, int y);
extern string program_str;
static void finish(int sig);
typedef enum it_mode_t{
mode_base,
mode_insert
} it_mode_t;
typedef enum it_buffer_t{
it_buffer_program,
it_buffer_compile_output
} it_buffer_t;
vector<string> buffer_lines;
vector<string> compile_output;
vector<string> program_lines;
//keep track of the window size and the cursor position
int winx, winy;
int curx, cury;
//save the state of the program buffer
int pcurx, pcury;
int p_start_posy;
//keep track of the current position within the file
int cur_file_posx = 0;
int cur_file_posy = 0;
//set the size of the footer
int footer_height = 2;
int main(int argc, char *argv[]) {
//get the initial coordinate positions
for(int i = 0; i < argc; i++) {
if(argv[i][0] == '+') {
sscanf(argv[i], "+%d,%d,%d", &cur_file_posy, &cury, &curx);
}
}
//create interrupt ^C to break
signal(SIGINT, finish);
//initialize library
initscr();
if(has_colors()) {
start_color();
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_BLACK, COLOR_WHITE);
attron(COLOR_PAIR(1));
}
//enable keyboard mapping
keypad(stdscr, TRUE);
//do not convert \n to \r\n
nonl();
//take input one character at a time and do not wait for \n
cbreak();
//disable automatic echo
noecho();
idlok(stdscr, TRUE);
scrollok(stdscr, TRUE);
//keep track of the current mode
it_mode_t mode = mode_base;
getmaxyx(stdscr, winy, winx);
winy = winy - footer_height;
//get the buffer_lines from the program string
istringstream program_str_stream(program_str);
string line;
while(getline(program_str_stream, line)) {
program_lines.push_back(line);
}
buffer_lines.insert(buffer_lines.begin(), program_lines.begin(), program_lines.end());
//ensure our specified initial starting coordinates do not go past the max
if(cur_file_posy > buffer_lines.size()-1) {
cur_file_posy = buffer_lines.size()-1;
}
if(cury > cur_file_posy) {
cury = cur_file_posy;
}
if(curx > buffer_lines[cur_file_posy].size()) {
curx = buffer_lines[cur_file_posy].size();
}
//print the screen with the program string
rewrite_buffer(cur_file_posy, curx, cury);
//get the initial coordinate positions
for(int i = 0; i < argc; i++) {
string arg = argv[i];
if(arg == "-r") {
move(winy+1, 0);
attron(COLOR_PAIR(2));
addstr("REWRITE SUCCESS");
attron(COLOR_PAIR(1));
move(cury, curx);
}
}
//infinitely loop over getting input
int buffer_contents = it_buffer_program;
for(;;) {
getyx(stdscr, cury, curx);
getmaxyx(stdscr, winy, winx);
winy = winy - footer_height;
switch(mode) {
case mode_base: {
int c = getch();
//enter insert mode
if(c == 'i') {
if(buffer_contents == it_buffer_program) {
mode = mode_insert;
}
}
//quit
else if(c == 'q') {
finish(0);
}
//rewrite the program
else if(c == 'w') {
//save the buffer into the lines of the program
program_lines.clear();
program_lines.insert(program_lines.begin(), buffer_lines.begin(), buffer_lines.end());
//recreate the program string
program_str = "";
for(auto line : program_lines) {
program_str += line + '\n';
}
rewrite_program(program_str, &compile_output);
//determine and show the result
if(compile_output.size() > 0) {
move(winy+1, 0);
attron(COLOR_PAIR(2));
addstr("REWRITE ERROR: type 'o' in command mode to see the output");
attron(COLOR_PAIR(1));
move(cury, curx);
}
else {
move(winy+1, 0);
addstr("REWRITE SUCCESS");
move(cury, curx);
string restart_str = "./it +" + to_string(cur_file_posy) + "," + to_string(cury) + "," + to_string(curx) + " -r";
system(restart_str.c_str());
finish(0);
}
}
//show compile output
else if(c == 'o') {
if(buffer_contents == it_buffer_program) {
program_lines.clear();
program_lines.insert(program_lines.begin(), buffer_lines.begin(), buffer_lines.end());
buffer_lines.clear();
buffer_lines.insert(buffer_lines.begin(), compile_output.begin(), compile_output.end());
buffer_contents = it_buffer_compile_output;
pcury = cury;
pcurx = curx;
p_start_posy = cur_file_posy;
rewrite_buffer(0, 0, 0);
}
else if(buffer_contents == it_buffer_compile_output) {
buffer_lines.clear();
buffer_lines.insert(buffer_lines.begin(), program_lines.begin(), program_lines.end());
buffer_contents = it_buffer_program;
cury = pcury;
curx = pcurx;
rewrite_buffer(p_start_posy, curx, cury);
}
}
//save to file
else if(c == 's') {
FILE *fp = fopen("main.cpp", "w");
for(auto line : buffer_lines) {
string newline = line + "\n";
fwrite(newline.c_str(), sizeof(char), newline.size(), fp);
}
}
//move the cursor around with hjkl
else if(c == 'h' || c == 'j' || c == 'k' || c == 'l') {
//remove any messages
move(winy+footer_height-1, 0);
clrtoeol();
if(c == 'h') {
if(cur_file_posx > 0) {
cur_file_posx = curx-1;
curx = cur_file_posx;
}
}
else if(c == 'j') {
if(cury < min(winy-1, buffer_lines.size()-1)) {
cur_file_posy++;
cury++;
}
else if(cur_file_posy < buffer_lines.size()-1) {
scroll(stdscr);
cur_file_posy++;
move(cury, 0);
addstr(buffer_lines[cur_file_posy].c_str());
}
//ensure we are placed correctly on the x axis
curx = cur_file_posx;
}
else if(c == 'k') {
if(cury > 0) {
cur_file_posy--;
cury--;
}
else if(cur_file_posy > 0) {
scrl(-1);
cur_file_posy--;
move(cury, 0);
addstr(buffer_lines[cur_file_posy].c_str());
}
//clear the footer lines
for(int i = winy; i < winy+footer_height; i++) {
move(i, 0);
clrtoeol();
}
//ensure we are placed correctly on the x axis
curx = cur_file_posx;
}
else if(c == 'l') {
if(curx < winx) {
cur_file_posx = curx+1;
curx = cur_file_posx;
}
}
//restrict the cursor's x coordinate to the length of the string
if(curx >= buffer_lines[cur_file_posy].size()) {
curx = buffer_lines[cur_file_posy].size();
}
move(cury, curx);
}
break;
}
case mode_insert: {
int c = getch();
//go back to base mode
if(c == 27) {
mode = mode_base;
}
//handle newlines by scrolling up and splitting buffer_lines
else if(c == '\r') {
string line_start = buffer_lines[cur_file_posy].substr(0, curx);
string line_end = buffer_lines[cur_file_posy].substr(curx, buffer_lines[cur_file_posy].size()-curx);
//rewrite the current line
buffer_lines[cur_file_posy] = line_start;
clrtoeol();
//add the new line
buffer_lines.insert(buffer_lines.begin()+cur_file_posy+1, line_end);
//reprint all the buffer_lines after
for(int i = cur_file_posy+1; i < buffer_lines.size(); i++) {
if(cury+i-cur_file_posy >= winy) {
break;
}
move(cury+i-cur_file_posy, 0);
clrtoeol();
addstr(buffer_lines[i].c_str());
}
//move the cursor down the cursor
cur_file_posy++;
cury++;
//scroll if the cursor passed the screen
move(cury, 0);
if(cury >= winy) {
scroll(stdscr);
cury = winy-1;
move(cury, 0);
addstr(buffer_lines[cur_file_posy].c_str());
move(cury, 0);
}
}
//delete or backspace
else if(c == 127 || c == 8) {
if(curx > 0) {
string line = buffer_lines[cur_file_posy];
buffer_lines[cur_file_posy] = line.substr(0, curx-1) + line.substr(curx, line.size()-curx);
cur_file_posx--;
curx--;
move(cury, curx);
delch();
}
else if(cur_file_posy > 0) {
int new_posx = buffer_lines[cur_file_posy-1].size();
string bigger_line = buffer_lines[cur_file_posy-1] + buffer_lines[cur_file_posy];
buffer_lines[cur_file_posy-1] = bigger_line;
buffer_lines.erase(buffer_lines.begin()+cur_file_posy);
if(cury > 0) {
//reprint all buffer_lines after
for(int i = cur_file_posy-1; i < buffer_lines.size(); i++) {
if(cury+i-cur_file_posy >= winy) {
break;
}
move(cury+i-cur_file_posy, 0);
clrtoeol();
addstr(buffer_lines[i].c_str());
}
//move the cursor
cur_file_posy--;
cury--;
move(cury, new_posx);
cur_file_posx = new_posx;
}
else {
move(cury, 0);
clrtoeol();
addstr(buffer_lines[cur_file_posy].c_str());
move(cury, new_posx);
cur_file_posx = new_posx;
}
}
}
//regular character
else {
//add the character to the line
string line_end = (char)c + buffer_lines[cur_file_posy].substr(curx, buffer_lines[cur_file_posy].size()-curx);
buffer_lines[cur_file_posy] = buffer_lines[cur_file_posy].substr(0, curx) + line_end;
move(cury, curx);
addstr(line_end.c_str());
curx++;
cur_file_posx = curx;
move(cury, curx);
}
break;
}
}
}
finish(0);
return 0;
}
void rewrite_buffer(int start, int x, int y) {
clear();
for(int i = start-y; i < buffer_lines.size() && i-start+y < winy; i++) {
move(i-start+y, 0);
addstr(buffer_lines[i].c_str());
}
move(y, x);
cur_file_posy = start;
cur_file_posx = x;
}
static void finish(int sig) {
endwin();
exit(0);
}
string program_str = ""
"#include <iostream>\n"
"#include <sstream>\n"
"#include <vector>\n"
"#include <curses.h>\n"
"\n"
"using namespace std;\n"
"\n"
"#define min(a,b) ((a) < (b) ? (a) : (b))\n"
"#define max(a,b) ((a) > (b) ? (a) : (b))\n"
"\n"
"void rewrite_program(string str, vector<string> *lines);\n"
"void rewrite_buffer(int start, int x, int y);\n"
"extern string program_str;\n"
"\n"
"static void finish(int sig);\n"
"\n"
"typedef enum it_mode_t{\n"
" mode_base,\n"
" mode_insert\n"
"} it_mode_t;\n"
"\n"
"typedef enum it_buffer_t{\n"
" it_buffer_program,\n"
" it_buffer_compile_output\n"
"} it_buffer_t;\n"
"\n"
"vector<string> buffer_lines;\n"
"vector<string> compile_output;\n"
"vector<string> program_lines;\n"
"\n"
"//keep track of the window size and the cursor position\n"
"int winx, winy;\n"
"int curx, cury;\n"
"\n"
"//save the state of the program buffer\n"
"int pcurx, pcury;\n"
"int p_start_posy;\n"
"\n"
"//keep track of the current position within the file\n"
"int cur_file_posx = 0;\n"
"int cur_file_posy = 0;\n"
"\n"
"//set the size of the footer\n"
"int footer_height = 2;\n"
"\n"
"int main(int argc, char *argv[]) {\n"
"\n"
" //get the initial coordinate positions\n"
" for(int i = 0; i < argc; i++) {\n"
" if(argv[i][0] == '+') {\n"
" sscanf(argv[i], \"+%d,%d,%d\", &cur_file_posy, &cury, &curx);\n"
" }\n"
" }\n"
"\n"
" //create interrupt ^C to break\n"
" signal(SIGINT, finish);\n"
"\n"
" //initialize library\n"
" initscr();\n"
"\n"
" if(has_colors()) {\n"
" start_color();\n"
" init_pair(1, COLOR_GREEN, COLOR_BLACK);\n"
" init_pair(2, COLOR_BLACK, COLOR_WHITE);\n"
" attron(COLOR_PAIR(1));\n"
" }\n"
"\n"
" //enable keyboard mapping\n"
" keypad(stdscr, TRUE);\n"
"\n"
" //do not convert \\n to \\r\\n\n"
" nonl();\n"
"\n"
" //take input one character at a time and do not wait for \\n\n"
" cbreak();\n"
"\n"
" //disable automatic echo\n"
" noecho();\n"
"\n"
" idlok(stdscr, TRUE);\n"
" scrollok(stdscr, TRUE);\n"
"\n"
" //keep track of the current mode\n"
" it_mode_t mode = mode_base;\n"
"\n"
" getmaxyx(stdscr, winy, winx);\n"
" winy = winy - footer_height;\n"
"\n"
" //get the buffer_lines from the program string\n"
" istringstream program_str_stream(program_str);\n"
" string line;\n"
" while(getline(program_str_stream, line)) {\n"
" program_lines.push_back(line);\n"
" }\n"
" buffer_lines.insert(buffer_lines.begin(), program_lines.begin(), program_lines.end());\n"
"\n"
" //ensure our specified initial starting coordinates do not go past the max\n"
" if(cur_file_posy > buffer_lines.size()-1) {\n"
" cur_file_posy = buffer_lines.size()-1;\n"
" }\n"
" if(cury > cur_file_posy) {\n"
" cury = cur_file_posy;\n"
" }\n"
" if(curx > buffer_lines[cur_file_posy].size()) {\n"
" curx = buffer_lines[cur_file_posy].size();\n"
" }\n"
"\n"
" //print the screen with the program string\n"
" rewrite_buffer(cur_file_posy, curx, cury);\n"
"\n"
" //get the initial coordinate positions\n"
" for(int i = 0; i < argc; i++) {\n"
" string arg = argv[i];\n"
" if(arg == \"-r\") {\n"
" move(winy+1, 0);\n"
" attron(COLOR_PAIR(2));\n"
" addstr(\"REWRITE SUCCESS\");\n"
" attron(COLOR_PAIR(1));\n"
" move(cury, curx);\n"
" }\n"
" }\n"
"\n"
" //infinitely loop over getting input\n"
" int buffer_contents = it_buffer_program;\n"
" for(;;) {\n"
" getyx(stdscr, cury, curx);\n"
" getmaxyx(stdscr, winy, winx);\n"
" winy = winy - footer_height;\n"
"\n"
" switch(mode) {\n"
" case mode_base: {\n"
" int c = getch();\n"
"\n"
" //enter insert mode\n"
" if(c == 'i') {\n"
" if(buffer_contents == it_buffer_program) {\n"
" mode = mode_insert;\n"
" }\n"
" }\n"
"\n"
" //quit\n"
" else if(c == 'q') {\n"
" finish(0);\n"
" }\n"
"\n"
" //rewrite the program\n"
" else if(c == 'w') {\n"
"\n"
" //save the buffer into the lines of the program\n"
" program_lines.clear();\n"
" program_lines.insert(program_lines.begin(), buffer_lines.begin(), buffer_lines.end());\n"
"\n"
" //recreate the program string\n"
" program_str = \"\";\n"
" for(auto line : program_lines) {\n"
" program_str += line + '\\n';\n"
" }\n"
"\n"
" rewrite_program(program_str, &compile_output);\n"
"\n"
" //determine and show the result\n"
" if(compile_output.size() > 0) {\n"
" move(winy+1, 0);\n"
" attron(COLOR_PAIR(2));\n"
" addstr(\"REWRITE ERROR: type 'o' in command mode to see the output\");\n"
" attron(COLOR_PAIR(1));\n"
" move(cury, curx);\n"
" }\n"
" else {\n"
" move(winy+1, 0);\n"
" addstr(\"REWRITE SUCCESS\");\n"
" move(cury, curx);\n"
" string restart_str = \"./it +\" + to_string(cur_file_posy) + \",\" + to_string(cury) + \",\" + to_string(curx) + \" -r\";\n"
" system(restart_str.c_str());\n"
" finish(0);\n"
" }\n"
" }\n"
"\n"
" //show compile output\n"
" else if(c == 'o') {\n"
"\n"
" if(buffer_contents == it_buffer_program) {\n"
" program_lines.clear();\n"
" program_lines.insert(program_lines.begin(), buffer_lines.begin(), buffer_lines.end());\n"
" buffer_lines.clear();\n"
" buffer_lines.insert(buffer_lines.begin(), compile_output.begin(), compile_output.end());\n"
" buffer_contents = it_buffer_compile_output;\n"
"\n"
" pcury = cury;\n"
" pcurx = curx;\n"
" p_start_posy = cur_file_posy;\n"
" rewrite_buffer(0, 0, 0);\n"
" }\n"
" else if(buffer_contents == it_buffer_compile_output) {\n"
" buffer_lines.clear();\n"
" buffer_lines.insert(buffer_lines.begin(), program_lines.begin(), program_lines.end());\n"
" buffer_contents = it_buffer_program;\n"
"\n"
" cury = pcury;\n"
" curx = pcurx;\n"
" rewrite_buffer(p_start_posy, curx, cury);\n"
" }\n"
" }\n"
"\n"
" //save to file\n"
" else if(c == 's') {\n"
" FILE *fp = fopen(\"main.cpp\", \"w\");\n"
" for(auto line : buffer_lines) {\n"
" string newline = line + \"\\n\";\n"
" fwrite(newline.c_str(), sizeof(char), newline.size(), fp);\n"
" }\n"
" }\n"
"\n"
" //move the cursor around with hjkl\n"
" else if(c == 'h' || c == 'j' || c == 'k' || c == 'l') {\n"
"\n"
" //remove any messages\n"
" move(winy+footer_height-1, 0);\n"
" clrtoeol();\n"
"\n"
" if(c == 'h') {\n"
" if(cur_file_posx > 0) {\n"
" cur_file_posx = curx-1;\n"
" curx = cur_file_posx;\n"
" }\n"
" }\n"
" else if(c == 'j') {\n"
" if(cury < min(winy-1, buffer_lines.size()-1)) {\n"
" cur_file_posy++;\n"
" cury++;\n"
" }\n"
" else if(cur_file_posy < buffer_lines.size()-1) {\n"
" scroll(stdscr);\n"
" cur_file_posy++;\n"
" move(cury, 0);\n"
" addstr(buffer_lines[cur_file_posy].c_str());\n"
" }\n"
"\n"
" //ensure we are placed correctly on the x axis\n"
" curx = cur_file_posx;\n"
" }\n"
" else if(c == 'k') {\n"
" if(cury > 0) {\n"
" cur_file_posy--;\n"
" cury--;\n"
" }\n"
" else if(cur_file_posy > 0) {\n"
" scrl(-1);\n"
" cur_file_posy--;\n"
" move(cury, 0);\n"
" addstr(buffer_lines[cur_file_posy].c_str());\n"
" }\n"
"\n"
" //clear the footer lines\n"
" for(int i = winy; i < winy+footer_height; i++) {\n"
" move(i, 0);\n"
" clrtoeol();\n"
" }\n"
"\n"
" //ensure we are placed correctly on the x axis\n"
" curx = cur_file_posx;\n"
" }\n"
" else if(c == 'l') {\n"
" if(curx < winx) {\n"
" cur_file_posx = curx+1;\n"
" curx = cur_file_posx;\n"
" }\n"
" }\n"
"\n"
" //restrict the cursor's x coordinate to the length of the string\n"
" if(curx >= buffer_lines[cur_file_posy].size()) {\n"
" curx = buffer_lines[cur_file_posy].size();\n"
" }\n"
" move(cury, curx);\n"
" }\n"
"\n"
" break;\n"
" }\n"
"\n"
" case mode_insert: {\n"
" int c = getch();\n"
"\n"
" //go back to base mode\n"
" if(c == 27) {\n"
" mode = mode_base;\n"
" }\n"
"\n"
" //handle newlines by scrolling up and splitting buffer_lines\n"