Мелкие доработки
This commit is contained in:
parent
291bbe6287
commit
e99716457e
4
demo.sh
4
demo.sh
@ -4,8 +4,8 @@ shopt -s expand_aliases
|
|||||||
alias rt="./rtracker"
|
alias rt="./rtracker"
|
||||||
|
|
||||||
rt Testing sleep... \
|
rt Testing sleep... \
|
||||||
% sleep 3
|
%% sleep 3
|
||||||
rt Hello world? \
|
rt Hello world? \
|
||||||
%o echo Hello world
|
%o echo Hello world
|
||||||
rt Producing some error \
|
rt Producing some error \
|
||||||
% rm not_exist
|
%% rm not_exist
|
||||||
|
24
rtracker.c
24
rtracker.c
@ -3,13 +3,19 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
#include "c_cross_pack/libs/threads.h"
|
#include "c_cross_pack/libs/threads.h"
|
||||||
|
|
||||||
|
#define CL_RED "\033[1;31m"
|
||||||
|
#define CL_YELLOW "\033[1;33m"
|
||||||
|
#define CL_GREEN "\033[1;32m"
|
||||||
|
#define CL_NORMAL "\033[1;0m"
|
||||||
|
|
||||||
char comment[1000] = "";
|
char comment[1000] = "";
|
||||||
char command[10000] = "";
|
char command[10000] = "";
|
||||||
|
|
||||||
// Out of command execution
|
// Output of command execution
|
||||||
char out[1000][100];
|
char out[1000][100];
|
||||||
int out_i = 0;
|
int out_i = 0;
|
||||||
|
|
||||||
@ -18,18 +24,18 @@ bool runCommand(char* command) {
|
|||||||
char path[1024];
|
char path[1024];
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
// Combine stdout and stderr using 2>&1
|
// Catch stdout and stderr
|
||||||
char fullCommand[1024];
|
char fullCommand[1024];
|
||||||
snprintf(fullCommand, sizeof(fullCommand), "%s 2>&1", command);
|
snprintf(fullCommand, sizeof(fullCommand), "%s 2>&1", command);
|
||||||
|
|
||||||
/* Open the command for reading. */
|
// Open the command for reading
|
||||||
fp = popen(fullCommand, "r");
|
fp = popen(fullCommand, "r");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
printf("Failed to run command\n");
|
printf("Failed to run command\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read the output a line at a time and store it in the out array */
|
// Read the output a line at a time and store it in the out array
|
||||||
while (fgets(path, sizeof(path), fp) != NULL) {
|
while (fgets(path, sizeof(path), fp) != NULL) {
|
||||||
strncpy(out[out_i], path, 100);
|
strncpy(out[out_i], path, 100);
|
||||||
out[out_i][99] = '\0'; // Ensure null termination
|
out[out_i][99] = '\0'; // Ensure null termination
|
||||||
@ -59,7 +65,7 @@ void* loadAnim() {
|
|||||||
char anim[] = {'-', '\\', '|', '/'};
|
char anim[] = {'-', '\\', '|', '/'};
|
||||||
while (true) {
|
while (true) {
|
||||||
for (size_t i = 0; i < sizeof(anim)/sizeof(anim[0]); i++) {
|
for (size_t i = 0; i < sizeof(anim)/sizeof(anim[0]); i++) {
|
||||||
printf("\r[\033[1;33m%c\033[1;0m] %s", anim[i], comment);
|
printf("\r[%s%c%s] %s", CL_YELLOW, anim[i], CL_NORMAL, comment);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
usleep(100 * 1000); // 100 ms
|
usleep(100 * 1000); // 100 ms
|
||||||
}
|
}
|
||||||
@ -73,14 +79,14 @@ void getOut() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
bool isCommand = false;
|
bool isCommand = false; // Flag for detect command section
|
||||||
bool forceOut = false;
|
bool forceOut = false;
|
||||||
for(int i = 1; i < argc; i++) {
|
for(int i = 1; i < argc; i++) {
|
||||||
if (isCommand == false) {
|
if (isCommand == false) {
|
||||||
if (strcmp(argv[i], "%o") == 0) {
|
if (strcmp(argv[i], "%o") == 0) {
|
||||||
isCommand = true;
|
isCommand = true;
|
||||||
forceOut = true;
|
forceOut = true;
|
||||||
} else if (strcmp(argv[i], "%") == 0) {
|
} else if (strcmp(argv[i], "%%") == 0) {
|
||||||
isCommand = true;
|
isCommand = true;
|
||||||
} else {
|
} else {
|
||||||
strcat(comment, argv[i]);
|
strcat(comment, argv[i]);
|
||||||
@ -96,13 +102,13 @@ int main(int argc, char **argv) {
|
|||||||
start_thread(&thread_handle, loadAnim);
|
start_thread(&thread_handle, loadAnim);
|
||||||
if (runCommand(command)) {
|
if (runCommand(command)) {
|
||||||
kill_thread(thread_handle);
|
kill_thread(thread_handle);
|
||||||
printf("\r[\033[1;32m🗸\033[1;0m] %s\n", comment);
|
printf("\r[%s🗸%s] %s\n", CL_GREEN, CL_NORMAL, comment);
|
||||||
if (forceOut == true) {
|
if (forceOut == true) {
|
||||||
getOut();
|
getOut();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
kill_thread(thread_handle);
|
kill_thread(thread_handle);
|
||||||
printf("\r[\033[1;31m𐄂\033[1;0m] %s\n", comment);
|
printf("\r[%s𐄂%s] %s\n", CL_RED, CL_NORMAL, comment);
|
||||||
// Get out of failed command
|
// Get out of failed command
|
||||||
getOut();
|
getOut();
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user