-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdwindow.cpp
More file actions
1459 lines (1258 loc) · 55.5 KB
/
cmdwindow.cpp
File metadata and controls
1459 lines (1258 loc) · 55.5 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
/*
* CMD Window
*
* Maintainer: Park Jiwoo
*
* Copyright (C) 2024 Park-Jiwoo
*
*/
#include <QWidget>
#include <QKeyEvent>
#include <QString>
#include <QTextEdit>
#include <QRegularExpression>
#include <QMetaType>
#include <QDebug>
#include <QTimer>
#include <QStack>
#include <QChar>
#include <QMessageBox>
#include <QProcess>
#include <QInputDialog>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QLabel>
#include <QDialog>
#include <cstdarg>
#include <QFile>
#include <QDir>
#include <QVariant>
#include <QFileInfo>
#include <QList>
#include "cmdwindow.h"
#include "ui_cmdwindow.h"
#include "smartpointerdialog.h"
// kernel engine
#include "kernel_engine.h"
#include "kernel_smartptr.h"
// 전역에서 접근 가능한 QTextEdit 포인터
QTextEdit* globalProgressLog = nullptr;
// C 정적 라이브러리
#include "kernel_print.h"
// #include "kernel_asm.h"
// 직접 하드코딩된 C 함수
extern "C" {
bool kernel_create_process(const char *process_name);
void kernel_list_processes();
bool kernel_kill_process(const char *process_name);
void register_print_function(void (*print_function)(const char *str));
void az_printf(const char *format, ...);
void kernel_putchar(char c);
sem_t* init_semaphore(int value);
pthread_mutex_t* init_mutex();
void* thread_function(void* arg);
void* semaphore_thread(void* arg);
void* mutex_thread(void* arg);
void run_multithreading(int num_threads, int use_semaphore, ...);
void kernel_chat(int num_args, ...);
//int main_1();
//int main_2();
//int main_3();
//int main_4();
//int main_5();
//int main_6();
//int main_7();
}
#define DEFAULT_TCP_PORT 5100
/*
* @brief CMD 창 생성자
* @param parent 부모 위젯, 기본값은 nullptr
* @details CMD 창을 초기화하고 스타일 및 초기 설정을 적용합니다.
*/
CmdWindow::CmdWindow(QWidget *parent)
: QDialog(parent), ui(new Ui::cmdWindow)
{
ui->setupUi(this);
// runTests();
kernel_chat(2, "127.0.0.1", DEFAULT_TCP_PORT);
ui->textEdit->setStyleSheet("background-color: black; color: yellow; font-family: 'Courier'; font-size: 12px;");
ui->textEdit->installEventFilter(this); // 이벤트 필터 설치
// C 코드에서 사용할 출력 함수 등록
register_print_function(qt_print);
// 초기 명령어 프롬프트
ui->textEdit->append("QT Kernel OS Version 1.0");
ui->textEdit->append("\nkernel> ");
}
/*
* @brief CMD 창 소멸자
* @details CMD 창에서 사용한 자원을 해제합니다.
*/
CmdWindow::~CmdWindow() {
delete ui;
}
/*
* @brief 키보드 입력 이벤트 처리
* @param event 키보드 이벤트 객체
* @details Enter 키를 누를 경우, 명령어 입력을 처리합니다.
*/
void CmdWindow::keyPressEvent(QKeyEvent *event) {
if (event->key() != Qt::Key_Return && event->key() != Qt::Key_Enter) {
QWidget::keyPressEvent(event); // 다른 키에 대한 기본 처리
}
}
/*
* @brief 이벤트 필터
* @param obj 이벤트가 발생한 객체
* @param event 처리할 이벤트
* @return bool 이벤트를 처리했는지 여부
* @details CMD 창에서 특정 키 입력(Enter, 화살표 키, Backspace)에 대한 동작을 커스터마이징합니다.
*/
bool CmdWindow::eventFilter(QObject *obj, QEvent *event) {
if (obj == ui->textEdit && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
QTextCursor cursor = ui->textEdit->textCursor();
int blockNumber = cursor.blockNumber();
int positionInBlock = cursor.positionInBlock();
// Enter 키 처리
if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
on_submitButton_clicked();
return true; // Enter 키 이벤트를 처리했음을 알림
}
// 위/왼쪽 화살표 키 처리
if (keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Left) {
if (blockNumber == ui->textEdit->document()->blockCount() - 1) {
if (positionInBlock <= 8) {
return true; // 커서가 "kernel> " 앞쪽으로 이동하지 않도록 막음
}
return QWidget::eventFilter(obj, event);
} else {
return true; // 커서가 이전 줄로 이동하지 않도록 막음
}
}
// Backspace 키 처리
if (keyEvent->key() == Qt::Key_Backspace) {
if (blockNumber == ui->textEdit->document()->blockCount() - 1) {
if (positionInBlock <= 8) {
return true; // "kernel> " 앞쪽의 텍스트 삭제를 막음
}
}
}
}
return QWidget::eventFilter(obj, event);
}
/*
* @brief 명령어 제출 처리
* @details Enter 키가 눌렸을 때, 사용자가 입력한 명령어를 처리합니다.
*/
void CmdWindow::on_submitButton_clicked() {
// 현재 텍스트를 가져옴
QString command = ui->textEdit->toPlainText().split("\n").last().replace("kernel> ", "");
handleCommand(command);
// 새로운 명령어를 위한 프롬프트 추가 (한 줄 개행만)
ui->textEdit->moveCursor(QTextCursor::End);
ui->textEdit->insertPlainText("\nkernel> ");
ui->textEdit->ensureCursorVisible();
// 항상 커서를 마지막으로 이동
QTextCursor cursor = ui->textEdit->textCursor();
cursor.movePosition(QTextCursor::End);
ui->textEdit->setTextCursor(cursor);
// 스크롤을 끝까지 내림
QScrollBar *scrollBar = ui->textEdit->verticalScrollBar();
scrollBar->setValue(scrollBar->maximum());
}
void CmdWindow::createProcessWithMessage(const QString &message) {
QProcess *process = new QProcess(this);
// 모달 창으로 생성한 프로세스의 진행 상황을 표시
QDialog *modalDialog = new QDialog(this);
QVBoxLayout *layout = new QVBoxLayout();
QLabel *label = new QLabel("Process running with message: " + message, modalDialog);
layout->addWidget(label);
modalDialog->setLayout(layout);
modalDialog->show();
process->start("echo", QStringList() << message);
connect(process, &QProcess::readyReadStandardOutput, [=]() {
QString output = process->readAllStandardOutput();
label->setText("Process Output: " + output);
});
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [=]() {
modalDialog->accept();
process->deleteLater(); // 프로세스 종료 후 자동으로 메모리 해제
});
}
/*
* @brief 명령어 처리 함수
* @param command 사용자가 입력한 명령어
* @details 사용자가 입력한 명령어를 분석하고, 해당 명령어에 맞는 동작을 수행합니다.
*/
// void CmdWindow::handleCommand(const QString &command) {
// static SmartPtr sp; // 기존의 스마트 포인터 전역 변수
// static SmartPointerDialog *dialog = nullptr; // 모달 창은 한 번만 생성
// static QMap<int*, SmartPtr> smartPointers; // 여러 스마트 포인터를 관리하기 위한 맵
// if (!dialog) {
// // 모달 창이 없으면 생성하여 표시 (부모를 nullptr로 설정)
// dialog = new SmartPointerDialog(nullptr);
// dialog->setWindowFlag(Qt::Window); // 독립된 창으로 설정
// dialog->show();
// }
// if (command == "exit") {
// close();
// }
// else if (command == "help") {
// ui->textEdit->append("Available commands:");
// ui->textEdit->append(" create <process_name> - Create a new process with the given name");
// ui->textEdit->append(" create printf(\"message\") - Print a message");
// ui->textEdit->append(" printf(\"message\") - Print a message using kernel_printf");
// ui->textEdit->append(" create_smart_ptr");
// ui->textEdit->append(" retain_smart_ptr");
// ui->textEdit->append(" release_smart_ptr");
// ui->textEdit->append(" show_smart_ptr_value");
// ui->textEdit->append(" show_ref_count");
// ui->textEdit->append(" kill_ptr <pointer_value> - Kill a specific smart pointer");
// ui->textEdit->append(" multithreading - Run a multithreading test");
// ui->textEdit->append(" semaphore - Run a semaphore test");
// ui->textEdit->append(" mutex - Run a mutex test");
// ui->textEdit->append(" help_modal - Show help in a new modal window");
// ui->textEdit->append(" printf_test : kp_test");
// ui->textEdit->append(" asm <number> - Run the kernel_asm_<number> function.");
// ui->textEdit->append(" kill <process_name> - Kill the process with the given name");
// ui->textEdit->append(" list - List all processes");
// ui->textEdit->append(" show_memory_usage - Show memory usage of smart pointers");
// ui->textEdit->append(" copy_smart_ptr - Copy a smart pointer");
// ui->textEdit->append(" create_smart_ptr_with_timer <seconds> - Create a smart pointer with auto-release");
// ui->textEdit->append(" clear - Clear the screen");
// ui->textEdit->append(" exit - Exit the shell");
// }
// else if (command.startsWith("kill_ptr ")) {
// QStringList parts = command.split(" ");
// if (parts.size() == 2) {
// bool ok;
// int ptrValue = parts[1].toInt(&ok);
// if (ok) {
// int* foundPtr = nullptr;
// // 스마트 포인터 맵에서 값을 찾기
// for (int* ptr : smartPointers.keys()) {
// if (ptr == reinterpret_cast<int*>(ptrValue)) { // 포인터 값을 직접 참조
// foundPtr = ptr;
// break;
// }
// }
// if (foundPtr) {
// SmartPtr sp = smartPointers[foundPtr];
// release(&sp);
// if (*(sp.ref_count) == 0) {
// dialog->removeSmartPointer(*foundPtr); // 모달 창에서 삭제
// smartPointers.remove(foundPtr); // 맵에서 삭제
// delete foundPtr; // 메모리 해제
// ui->textEdit->append("Smart pointer deleted.");
// } else {
// ui->textEdit->append("Pointer reference count is not zero.");
// }
// } else {
// ui->textEdit->append("Pointer value not found.");
// }
// } else {
// ui->textEdit->append("Invalid pointer value.");
// }
// } else {
// ui->textEdit->append("Usage: kill_ptr <pointer_value>");
// }
// }
// else if (command == "help_modal") {
// // 새로운 모달 창을 열어 모든 명령어 표시
// QDialog *helpDialog = new QDialog(this);
// QVBoxLayout *layout = new QVBoxLayout(helpDialog);
// QTextEdit *helpText = new QTextEdit(helpDialog);
// helpText->setText("Available commands:\n"
// " create <process_name> - Create a new process with the given name\n"
// " create printf(\"message\") - Print a message\n"
// " printf(\"message\") - Print a message using kernel_printf\n"
// " create_smart_ptr - Create a smart pointer\n"
// " retain_smart_ptr - Retain a smart pointer\n"
// " release_smart_ptr - Release a smart pointer\n"
// " show_smart_ptr_value - Show the value of a smart pointer\n"
// " show_ref_count - Show reference count of a smart pointer\n"
// " kill_ptr <pointer_value> - Kill a specific smart pointer\n"
// " multithreading - Run a multithreading test\n"
// " semaphore - Run a semaphore test\n"
// " mutex - Run a mutex test\n"
// " help_modal - Show help in a new modal window\n"
// " printf_test : kp_test - Test printf functionality\n"
// " asm <number> - Run the kernel_asm_<number> function\n"
// " kill <process_name> - Kill the process with the given name\n"
// " list - List all processes\n"
// " show_memory_usage - Show memory usage of smart pointers\n"
// " copy_smart_ptr - Copy a smart pointer\n"
// " create_smart_ptr_with_timer <seconds> - Create a smart pointer with auto-release\n"
// " clear - Clear the screen\n"
// " exit - Exit the shell");
// helpText->setReadOnly(true);
// layout->addWidget(helpText);
// helpDialog->setLayout(layout);
// helpDialog->setWindowTitle("Help Commands");
// helpDialog->show();
// }
// else if (command == "show_memory_usage") {
// // 스마트 포인터 메모리 사용량을 추적
// int totalMemory = 0;
// for (int* ptr : smartPointers.keys()) {
// totalMemory += sizeof(*ptr); // 할당된 포인터 크기를 합산
// }
// ui->textEdit->append("Total memory used by smart pointers: " + QString::number(totalMemory) + " bytes.");
// }
// else if (command == "copy_smart_ptr") {
// if (!smartPointers.isEmpty()) {
// SmartPtr originalSp = smartPointers.begin().value();
// SmartPtr copiedSp = originalSp; // 스마트 포인터 복사
// ui->textEdit->append("Smart pointer copied. New reference count: " + QString::number(*(copiedSp.ref_count)));
// }
// }
// else if (command.startsWith("create_smart_ptr_with_timer ")) {
// QStringList parts = command.split(" ");
// if (parts.size() == 2) {
// bool ok;
// int timeout = parts[1].toInt(&ok);
// if (ok) {
// // 타이머를 이용한 스마트 포인터 생성 및 자동 삭제
// int* testData = new int;
// *testData = rand() % 1000;
// sp = create_smart_ptr(*testData, 1, nullptr);
// smartPointers[testData] = sp;
// // 타이머 시작
// QTimer::singleShot(timeout * 1000, this, [=]() {
// if (*(sp.ref_count) == 1) { // 참조 카운트가 1일 때만 해제
// release(&sp);
// ui->textEdit->append("Smart pointer auto-released after " + QString::number(timeout) + " seconds.");
// dialog->removeSmartPointer(*testData); // 모달에서 삭제
// smartPointers.remove(testData); // 맵에서 삭제
// delete testData;
// } else {
// ui->textEdit->append("Smart pointer was not released due to non-zero reference count.");
// }
// });
// ui->textEdit->append("Smart pointer created with auto-release in " + QString::number(timeout) + " seconds.");
// } else {
// ui->textEdit->append("Invalid timer value.");
// }
// } else {
// ui->textEdit->append("Usage: create_smart_ptr_with_timer <seconds>");
// }
// }
// else if (command == "create_smart_ptr") {
// // 포인터 생성 및 스마트 포인터 할당
// int* testData = new int;
// *testData = rand() % 1000; // 0~999 사이의 랜덤 값
// sp = create_smart_ptr(*testData, 1, nullptr);
// smartPointers[testData] = sp; // 생성된 스마트 포인터를 맵에 추가
// // CMD 창에 메시지 출력
// ui->textEdit->append(QString("Smart pointer created with value: %1").arg(*testData));
// kernel_printf("Smart pointer created with value: %d\n", *testData);
// // 모달 창에 포인터 추가
// dialog->addSmartPointer(*testData, *(sp.ref_count));
// }
// else if (command == "retain_smart_ptr") {
// if (!smartPointers.isEmpty()) {
// // 맵에서 첫 번째 포인터를 참조 카운트 증가
// SmartPtr &sp = smartPointers.begin().value();
// retain(&sp);
// ui->textEdit->append("Smart pointer retained.");
// dialog->addSmartPointer(*((int*)sp.ptr), *(sp.ref_count)); // 모달 창에 업데이트
// }
// }
// else if (command == "release_smart_ptr") {
// if (!smartPointers.isEmpty()) {
// // 맵에서 첫 번째 포인터를 참조 카운트 감소 및 해제
// int* ptr = smartPointers.begin().key();
// SmartPtr sp = smartPointers[ptr];
// release(&sp);
// ui->textEdit->append("Smart pointer released.");
// if (*(sp.ref_count) == 0) {
// dialog->removeSmartPointer(*ptr); // 참조 카운트가 0이면 삭제
// smartPointers.remove(ptr); // 맵에서 포인터 삭제
// delete ptr; // 포인터 메모리 해제
// } else {
// dialog->addSmartPointer(*ptr, *(sp.ref_count)); // 참조 카운트 업데이트
// }
// }
// }
// else if (command == "show_smart_ptr_value") {
// if (!smartPointers.isEmpty()) {
// int* value = (int*)smartPointers.begin().key();
// ui->textEdit->append("Smart pointer value: " + QString::number(*value));
// kernel_printf("Smart pointer value: %d\n", *value);
// }
// }
// else if (command == "show_ref_count") {
// if (!smartPointers.isEmpty()) {
// SmartPtr sp = smartPointers.begin().value();
// ui->textEdit->append("Smart pointer reference count: " + QString::number(*(sp.ref_count)));
// kernel_printf("Smart pointer reference count: %d\n", *(sp.ref_count));
// }
// }
// else if (command == "multithreading") {
// runMultithreadingTest();
// }
// else if (command == "semaphore") {
// runSemaphoreTest();
// }
// else if (command == "mutex") {
// runMutexTest();
// }
// else if (command.startsWith("create printf(")) {
// QRegularExpression re(R"raw(create printf\("(.*)"\))raw");
// QRegularExpressionMatch match = re.match(command);
// if (match.hasMatch()) {
// QString message = match.captured(1);
// // az_printf("\n%s", message.toStdString().c_str());
// ui->textEdit->append("Create process with message >> "+message);
// createProcessWithMessage(message);
// } else {
// ui->textEdit->append("Invalid command format. Use: create printf(\"message\")");
// }
// }
// else if (command.startsWith("asm ")) {
// QStringList parts = command.split(" ");
// if (parts.size() == 2) {
// int number = parts[1].toInt();
// switch (number) {
// case 1:
// ui->textEdit->append("Executed kernel_asm_1.");
// main_1(); // kernel_asm_1.a의 main_1 함수 호출
// break;
// case 2:
// ui->textEdit->append("Executed kernel_asm_2.");
// main_2(); // kernel_asm_2.a의 main_2 함수 호출
// break;
// case 3:
// ui->textEdit->append("Executed kernel_asm_3.");
// main_3(); // kernel_asm_3.a의 main_3 함수 호출
// break;
// case 4:
// ui->textEdit->append("Executed kernel_asm_4.");
// main_4(); // kernel_asm_4.a의 main_4 함수 호출
// break;
// case 5:
// ui->textEdit->append("Executed kernel_asm_5.");
// main_5(); // kernel_asm_5.a의 main_5 함수 호출
// break;
// case 6:
// ui->textEdit->append("Executed kernel_asm_6.");
// main_6(); // kernel_asm_6.a의 main_6 함수 호출
// break;
// case 7:
// ui->textEdit->append("Executed kernel_asm_7.");
// main_7(); // kernel_asm_7.a의 main_7 함수 호출
// break;
// default:
// ui->textEdit->append("Invalid number. Please enter a number between 1 and 7.");
// }
// } else {
// ui->textEdit->append("Usage: asm <number>");
// }
// }
// else if (command.startsWith("printf(")) {
// QRegularExpression re1(R"raw(printf\("([^"]*)"\))raw");
// QRegularExpression re2(R"raw(printf\("([^"]*)"\s*,\s*(.*)\))raw");
// QRegularExpressionMatch match1 = re1.match(command);
// QRegularExpressionMatch match2 = re2.match(command);
// if (match1.hasMatch()) {
// // 단순 메시지 출력
// QString message = match1.captured(1);
// QString debugMessage = message;
// debugMessage.replace("\\n", "\n");
// qDebug().noquote() << debugMessage;
// ui->textEdit->append(message);
// kernel_printf("%s", message.toStdString().c_str());
// ui->textEdit->append("");
// /** test **/
// kernel_printf("\n\n******** Test kernel_printf Function ********\n");
// kernel_printf("Hello world!\n");
// kernel_printf("A single character : %c \n", 'T');
// kernel_printf("An integer : %d \n", 37);
// kernel_printf("An integer : %d \n", 299);
// kernel_printf("5-4 = %d\n", 1);
// int a = 1;
// int b = 2;
// kernel_printf("%d + %d = %d\n", a, b, a + b);
// kernel_printf("%d\t\t\t String.\n", 12345678);
// kernel_printf("-650\n");
// kernel_printf("%+d\n", 430);
// kernel_printf("%+1d\n", 650);
// kernel_printf("%+10d\n", 499);
// kernel_printf("% 3d\n", 1230);
// kernel_printf("%08d\n", 342);
// kernel_printf("%+03d\n", -430);
// kernel_printf("%3d\n", -43);
// kernel_printf("%u\n", 23919293929392);
// kernel_printf("%+-u\n", 12345);
// kernel_printf("%+10u\n", 12345);
// kernel_printf("%-4s\n", "Az");
// kernel_printf("%o\n", 333);
// kernel_printf("%-0#+10o\n", 2048);
// kernel_printf("%X\n", 12345678);
// kernel_printf("%#+x\n", 12345678);
// kernel_printf("\n\nfunction call Test\n");
// kernel_printf("%d + %d = %d", 5, 6, function_Test(5, 6));
// kernel_printf("\n");
// /** 정적 라이브러리를 갖고 오는 방법은 두가지임. 위의 kernel_printf는 kernel_printf.h헤더이며 아래는 kernel_lib.h임. **/
// /** 따라서 이 코드 파일에서 kernel_printf.h만 했기 때문에 아래도 헤더파일로 선언해야하는데 **/
// /** 공부목적을 위해서 헤더 파일이 아닌 extern 메크로를 사용함 **/
// /** 선언을 하니 워닝이 사라짐 **/
// /*** kernel_putchar ***/
// kernel_putchar('H');
// kernel_putchar('E');
// kernel_putchar('L');
// kernel_putchar('L');
// kernel_putchar('O');
// kernel_putchar('\n');
// kernel_putchar('V');
// kernel_putchar('E');
// kernel_putchar('D');
// kernel_putchar('A');
// kernel_putchar('\n');
// kernel_printf("-\n");
// kernel_printf(".\n");
// kernel_printf("/\n");
// kernel_printf("'()*+,-./\n");
// }
// else if (match2.hasMatch()) {
// // 포맷 문자열과 가변 인자 처리
// QString format = match2.captured(1);
// QString arguments = match2.captured(2).trimmed();
// qDebug() << "\nFormat:" << format;
// qDebug() << "Arguments:" << arguments;
// ui->textEdit->append(format);
// if (!arguments.isEmpty()) {
// QVariantList args = parseArguments(arguments);
// executeKernelPrintf(format, args);
// } else {
// kernel_printf(format.toStdString().c_str());
// }
// ui->textEdit->append("");
// }
// else {
// ui->textEdit->append("Invalid command format. Use: kernel_printf(\"message\") or kernel_printf(\"format\", values)");
// }
// }
// else if (command == "kp_test") {
// test_kernel_printf();
// }
// else if (command.startsWith("create ")) {
// QString process_name = command.mid(7);
// bool success = kernel_create_process(process_name.toStdString().c_str());
// if (!success) {
// ui->textEdit->append("Failed to create process: " + process_name);
// }
// }
// else if (command.startsWith("kill ")) {
// QString process_name = command.mid(5);
// bool success = kernel_kill_process(process_name.toStdString().c_str());
// if (!success) {
// ui->textEdit->append("No running process found with name: " + process_name);
// }
// }
// else if (command == "list") {
// kernel_list_processes();
// }
// else if (command == "clear") {
// ui->textEdit->clear();
// }
// else {
// ui->textEdit->append("Unknown command. Type 'help' for a list of commands.");
// }
// }
/*
* @brief 명령어 처리 함수
* @param command 사용자가 입력한 명령어
*/
void CmdWindow::handleCommand(const QString &command) {
if (command == "exit") {
close();
} else if (command == "help") {
ui->textEdit->append("Available commands:");
ui->textEdit->append(" pwd - Print working directory");
ui->textEdit->append(" ls - List files and directories");
ui->textEdit->append(" cd <directory> - Change directory");
ui->textEdit->append(" cp <source> <destination> - Copy a file");
ui->textEdit->append(" mv <source> <destination> - Move a file");
ui->textEdit->append(" df -h - Show disk usage");
ui->textEdit->append(" du <directory> - Show directory size");
ui->textEdit->append(" printf(\"message\") - Print a message");
ui->textEdit->append(" history - Show command history");
ui->textEdit->append(" mkdir <directory> - Create a directory");
ui->textEdit->append(" rmdir <directory> - Remove a directory");
ui->textEdit->append(" clear - Clear the screen");
ui->textEdit->append(" ifconfig - Show network interfaces");
ui->textEdit->append(" netstat - Show network connections");
ui->textEdit->append(" help_modal - Show help in a new modal window");
ui->textEdit->append(" exit - Exit the shell");
ui->textEdit->append(" shutdown || poweroff - Shutdown the system");
}
if (command == "help_modal") {
// 새로운 모달 창을 열어 모든 명령어 표시
QDialog *helpDialog = new QDialog(this);
QVBoxLayout *layout = new QVBoxLayout(helpDialog);
QTextEdit *helpText = new QTextEdit(helpDialog);
// 명령어 목록 설정
helpText->setText("Available commands:\n"
" pwd - Print working directory\n"
" ls - List files and directories\n"
" cd <directory> - Change directory\n"
" cp <source> <destination> - Copy a file\n"
" mv <source> <destination> - Move a file\n"
" df -h - Show disk usage\n"
" du <directory> - Show directory size\n"
" printf(\"message\") - Print a message\n"
" history - Show command history\n"
" mkdir <directory> - Create a directory\n"
" rmdir <directory> - Remove a directory\n"
" clear - Clear the screen\n"
" ifconfig - Show network interfaces\n"
" netstat - Show network connections\n"
" help_modal - Show help in a new modal window\n"
" exit - Exit the shell\n"
" shutdown || poweroff - Shutdown the system\n");
// 텍스트 편집기를 읽기 전용으로 설정
helpText->setReadOnly(true);
// 레이아웃에 텍스트 편집기 추가
layout->addWidget(helpText);
// 모달 창의 레이아웃 설정
helpDialog->setLayout(layout);
helpDialog->setWindowTitle("Help Commands");
// 모달 창 표시
helpDialog->exec(); // exec()를 사용하여 모달로 열림
}
else if (command.startsWith("printf(")) {
QRegularExpression re1(R"raw(printf\("([^"]*)"\))raw");
QRegularExpression re2(R"raw(printf\("([^"]*)"\s*,\s*(.*)\))raw");
QRegularExpressionMatch match1 = re1.match(command);
QRegularExpressionMatch match2 = re2.match(command);
if (match1.hasMatch()) {
// 단순 메시지 출력
QString message = match1.captured(1);
QString debugMessage = message;
debugMessage.replace("\\n", "\n");
qDebug().noquote() << debugMessage;
ui->textEdit->append(message);
kernel_printf("%s", message.toStdString().c_str());
ui->textEdit->append("");
}
else if (match2.hasMatch()) {
// 포맷 문자열과 가변 인자 처리
QString format = match2.captured(1);
QString arguments = match2.captured(2).trimmed();
qDebug() << "\nFormat:" << format;
qDebug() << "Arguments:" << arguments;
ui->textEdit->append(format);
if (!arguments.isEmpty()) {
QVariantList args = parseArguments(arguments);
executeKernelPrintf(format, args);
} else {
kernel_printf(format.toStdString().c_str());
}
ui->textEdit->append("");
}
else {
ui->textEdit->append("Invalid command format. Use: kernel_printf(\"message\") or kernel_printf(\"format\", values)");
}
}
else if (command.startsWith("cp ")) {
QStringList parts = command.split(" ");
if (parts.size() == 3) {
QStringList args;
args << parts[0] << parts[1] << parts[2]; // QList<QVariant> -> QStringList
runUnifiedProcess(args);
} else {
ui->textEdit->append("Usage: cp <source> <destination>");
}
}
else if (command.startsWith("mv ")) {
QStringList parts = command.split(" ");
if (parts.size() == 3) {
QStringList args;
args << parts[0] << parts[1] << parts[2];
runUnifiedProcess(args);
} else {
ui->textEdit->append("Usage: mv <source> <destination>");
}
}
else if (command == "clear") {
ui->textEdit->clear();
//on_submitButton_clicked();
//ui->textEdit->append("kernel> ");
}
else if (command == "pwd") {
runUnifiedProcess(QStringList() << "pwd");
}
else if (command == "ls") {
runUnifiedProcess(QStringList() << "ls" << "-alc");
}
else if (command.startsWith("cd ")) {
QString directory = command.mid(3);
QDir::setCurrent(directory);
ui->textEdit->append("Changed directory to: " + directory);
}
else if (command == "history") {
for (const QString &cmd : commandHistory) {
ui->textEdit->append(cmd);
}
}
else if (command.startsWith("mkdir ")) {
QString directory = command.mid(6);
QDir().mkdir(directory);
ui->textEdit->append("Directory created: " + directory);
}
else if (command.startsWith("rmdir ")) {
QString directory = command.mid(6);
QDir().rmdir(directory);
ui->textEdit->append("Directory removed: " + directory);
}
else if (command == "df -h") {
// QProcess를 이용해 시스템 명령어 실행
QProcess process;
process.start("df", QStringList() << "-h");
// 프로세스가 완료될 때까지 기다림
process.waitForFinished();
// 프로세스의 출력 결과를 읽음
QString output = process.readAllStandardOutput();
// 출력 결과를 ui->textEdit에 추가 (노란색으로 표시됨)
ui->textEdit->append(output);
}
else if (command.startsWith("du ")) {
QStringList parts = command.split(" ");
if (parts.size() == 2) {
runUnifiedProcess(QStringList() << "du" << parts[1]); // QList<QVariant> -> QStringList
} else {
ui->textEdit->append("Usage: du <directory>");
}
}
else if (command.startsWith("echo ")) {
QString message = command.mid(5).trimmed(); // echo 이후의 메시지 추출
ui->textEdit->append(message); // 화면에 출력
kernel_printf("%s\n", message.toStdString().c_str()); // 콘솔에 출력
}
else if (command == "ifconfig") {
QProcess process;
process.start("ifconfig");
process.waitForFinished();
QString output = process.readAllStandardOutput();
ui->textEdit->append(output); // 결과를 UI에 출력
}
else if (command == "top") {
QProcess process;
process.start("top");
process.waitForFinished();
QString output = process.readAllStandardOutput();
ui->textEdit->append(output); // 결과를 UI에 출력
}
else if (command == "netstat") {
QProcess process;
process.start("netstat", QStringList() << "-tuln");
process.waitForFinished();
QString output = process.readAllStandardOutput();
ui->textEdit->append(output); // 결과를 UI에 출력
}
else if (command == "shutdown" || command == "poweroff") {
ui->textEdit->append("Shutting down...");
QApplication::quit(); // 애플리케이션 종료
}
else {
ui->textEdit->append("Unknown command. Type 'help' for a list of commands.");
}
commandHistory.append(command);
}
/*
* @brief 가변 인자 대신 QList<QVariant>를 사용하여 명령어를 처리하는 함수
* @param args 명령어와 인자를 QList<QVariant>로 전달
*/
/*
* @brief 가변 인자 대신 QStringList를 사용하여 명령어를 처리하는 함수
* 프로세스 생성 시 스마트 포인터를 사용하여 메모리를 관리합니다.
* @param args 명령어와 인자를 QStringList로 전달
*/
void CmdWindow::runUnifiedProcess(const QStringList &args) {
static SmartPtr sp; // 기존의 스마트 포인터 전역 변수
static QMap<int*, SmartPtr> smartPointers; // 여러 스마트 포인터를 관리하기 위한 맵
if (args.isEmpty()) {
kernel_printf("No command provided.\n");
return;
}
QString command = args[0];
commandHistory.append(command); // 명령어를 히스토리에 추가
kernel_printf("Executing command: %s\n", command.toStdString().c_str());
ui->textEdit->append("Executing command: " + command);
// 포인터 생성 및 스마트 포인터 할당
int* testData = new int;
*testData = rand() % 1000; // 0~999 사이의 랜덤 값
sp = create_smart_ptr(*testData, 1, nullptr);
smartPointers[testData] = sp; // 생성된 스마트 포인터를 맵에 추가
ui->textEdit->append(QString("Smart pointer created with value: %1").arg(*testData));
kernel_printf("Smart pointer created with value: %d\n", *testData);
ui->textEdit->append("Smart pointer created with value: " + QString::number(*testData));
// 스마트 포인터로 프로세스 데이터를 관리
auto processData = std::make_shared<int>(rand() % 1000);
kernel_printf("Process Smart Pointer Created With Value : %d\n", *processData);
ui->textEdit->append("Process Smart Pointer Created With Value : " + QString::number(*processData));
QProcess process;
QString output;
// 명령어 처리
// 파일 및 디렉토리 명령어
if (command == "pwd") {
process.start("pwd");
}
if (command == "cd" && args.size() == 2) {
QString directory = command.mid(3).trimmed();
if (QDir::setCurrent(directory)) {
ui->textEdit->append("Changed directory to: " + QDir::currentPath());
kernel_printf("Directory changed to: %s\n", QDir::currentPath().toStdString().c_str());
} else {
ui->textEdit->append("Failed to change directory: " + directory);
kernel_printf("Error: Failed to change directory to: %s\n", directory.toStdString().c_str());
}
}
if (command == "ls") {
process.start("ls", QStringList() << "-alc");
}
// history
if(args[0] == "history") {
kernel_printf("Command history:\n");
ui->textEdit->append("Command history:");
for(int i=0; i<commandHistory.size(); i++) {
QString historyEntry = QString::number(i+1) + ": " + commandHistory[i];
kernel_printf("%s\n", historyEntry.toStdString().c_str());
ui->textEdit->append(historyEntry);
}
}
if (command == "mkdir" && args.size() == 2) {
QDir().mkdir(args[1]);
kernel_printf("Directory %s created\n", args[1].toStdString().c_str());
return;
}
if (command == "rmdir" && args.size() == 2) {
QDir().rmdir(args[1]);
kernel_printf("Directory %s removed\n", args[1].toStdString().c_str());
return;
}
if (command == "cp" && args.size() == 3) {
QString src = args[1];
QString dst = args[2];
// 소스 파일 확인
if (!QFile::exists(src)) {
kernel_printf("Error: Source file does not exist: %s\n", src.toStdString().c_str());
ui->textEdit->append("Error: Source file does not exist: " + src);
return;
}
// 목적지 파일 경로 유효성 확인 (목적지 폴더가 존재하는지)
QFileInfo dstInfo(dst);
if (!dstInfo.dir().exists()) {
kernel_printf("Error: Destination directory does not exist: %s\n", dstInfo.dir().absolutePath().toStdString().c_str());
ui->textEdit->append("Error: Destination directory does not exist: " + dstInfo.dir().absolutePath());
return;
}
// 파일 복사
if (QFile::copy(src, dst)) {
kernel_printf("File copied from %s to %s\n", src.toStdString().c_str(), dst.toStdString().c_str());
ui->textEdit->append("File copied from " + src + " to " + dst);
} else {
kernel_printf("Error: Failed to copy file. Please check permissions or if the file already exists.\n");
ui->textEdit->append("Error: Failed to copy file. Please check permissions or if the file already exists.");
}
}
if (command == "mv" && args.size() == 3) {
QString src = args[1];
QString dst = args[2];
if (!QFile::exists(src)) {
kernel_printf("Error: Source file does not exist: %s\n", src.toStdString().c_str());
ui->textEdit->append("Error: Source file does not exist: " + src);
return;
}
if (!QFile::rename(src, dst)) {
kernel_printf("Error: Failed to move file.\n");
ui->textEdit->append("Error: Failed to move file.");
} else {
kernel_printf("File moved from %s to %s\n", src.toStdString().c_str(), dst.toStdString().c_str());