diff --git a/src/ccode.vala b/src/ccode.vala index dad59f3..6a6d4a5 100755 --- a/src/ccode.vala +++ b/src/ccode.vala @@ -26,13 +26,6 @@ public extern int get_uid_by_user (string username); //DOC: chech root user public extern bool is_root (); -// ### nostd.c -// All private functions. please look util/interface.vala -private extern void no_stdin (); -private extern void no_stdout (); -private extern void no_stderr (); -private extern void reset_std (); - //DOC: ### sandbox.c //DOC: `void sandbox (string type, string[] args):` //DOC: run command in sandboxed area diff --git a/src/ccode/error.c b/src/ccode/error.c index 4ba219f..136a52b 100755 --- a/src/ccode/error.c +++ b/src/ccode/error.c @@ -29,7 +29,6 @@ void error(int status){ size_t len = 0; char** errs = array_get(error_arr, &len); if(len > 0 && !get_bool ("ignore-error")) { - printf("%ld\n", len); for(i=0;i #include #include +#include +#include /* functions from vala source */ -void print(char* msg); -void print_stderr(char* msg); -void error_add(char* msg); int get_bool(char* val); void colorize_init(); /* string add function*/ char* str_add(char* s1, char* s2); +/* string builder */ +char* build_string(char* format, ...); -/* headers of functions */ + +extern char* colorize(char* message, int color); +extern char* gettext(char* message); + +#define yellow 33 +#define green 32 +#define _(A) gettext(A) + +#include +#include + +/* define function headers */ void cprint(char* msg); void cprint_stderr(char* msg); void cprint_dummy(char* msg); -void warning_fn(char* msg); -#ifdef DEBUG -void debug_fn(char* msg); -#endif -void info_fn(char* msg); /* function pointer type definitions */ @@ -45,10 +52,46 @@ void print_fn(char* message, int new_line, int err){ write(err+1, message, strlen(message)); } +void warning_fn (char* message) { + print_stderr (build_string("%s: %s",colorize (_ ("WARNING"), yellow), message)); +} + +void info_fn (char* message) { + print_stderr (build_string("%s: %s",colorize (_ ("INFO"), yellow), message)); +} + void cprint(char* message){ message = str_add(message, "\n"); write(1, message, strlen(message)); } + + +void set_terminal_title (char* msg) { + #if NOCOLOR + return; + #else + if (!get_bool ("no-color")) { + print_fn (build_string("%s%s%s","\x1b]2;", msg, "\x07"), false, false); + } + #endif +} + + +#ifdef DEBUG +static long last_debug_time = -1; +void debug_fn(char *message) { + if (last_debug_time == -1) { + last_debug_time = time(NULL); + } + + time_t current_time = time(NULL); + int elapsed_time = (int)(current_time - last_debug_time); + + print_stderr (build_string( "[%s:%d]: %s\n", "DEBUG", elapsed_time, message)); + + last_debug_time = current_time; +} +#endif void cprint_stderr(char* message){ message = str_add(message, "\n"); write(2, message, strlen(message)); diff --git a/src/include/logger.h b/src/include/logger.h new file mode 100755 index 0000000..a623004 --- /dev/null +++ b/src/include/logger.h @@ -0,0 +1,18 @@ +#ifndef LOGGER_H +#define LOGGER_H + +void print(char* msg); +void warning(char* msg); +#if DEBUG +void debug(char* msg); +void debug_fn(char* message); +#endif +void info(char* msg); +void print_stderr(char* msg); +void warning_fn(char* message); +void info_fn(char* message); +void logger_init(); +void set_terminal_title(char* message); + +#endif /* LOGGER_H */ + diff --git a/src/interpreter.vala b/src/interpreter.vala index 3722afc..16e30cc 100755 --- a/src/interpreter.vala +++ b/src/interpreter.vala @@ -8,6 +8,10 @@ public void add_script(string data) { } } +private extern void no_stdin(); +private extern void reset_std(); + + public int ymp_run() { variable_integer[] labels = {}; bool label_found = false; @@ -78,10 +82,10 @@ public int ymp_run() { } lock_operation(); if (get_bool("disable-stdin")) { - nostdin(); + no_stdin(); } int status = operation_main(proc[i].type, proc[i].args); - resetstd(); + reset_std(); unlock_operation(); if (status != 0) { string type = proc[i].type; diff --git a/src/util/interface.vala b/src/util/interface.vala index 8db5d6b..c03bdfc 100755 --- a/src/util/interface.vala +++ b/src/util/interface.vala @@ -104,41 +104,3 @@ public void print_with_amogus (string data) { } } - -//DOC: `void nostdin ():` -//DOC: close stdin. Ignore input. This function may broke application. -public void nostdin () { - nostdin_enabled = true; - set_value_readonly ("ask", "false"); - no_stdin (); -} - -//DOC: `void nostdout ():` -//DOC: close stdout -public void nostdout () { - set_value_readonly ("no-stdout", "true"); - no_stdout (); -} - -//DOC: `void nostderr ():` -//DOC: close stderr -public void nostderr () { - set_value_readonly ("no-stderr", "true"); - no_stderr (); -} - -//DOC: `void resetstd ():` -//DOC: restore stdout and stdin -public void resetstd () { - set_value_readonly ("no-stdout", "false"); - set_value_readonly ("no-stderr", "false"); - reset_std (); -} - -//DOC: `void nostd ():` -//DOC: close stdin, stdout and stderr -public void nostd () { - nostdin (); - nostdout (); - nostderr (); -} diff --git a/src/util/logger.vala b/src/util/logger.vala deleted file mode 100755 index 7b0621c..0000000 --- a/src/util/logger.vala +++ /dev/null @@ -1,63 +0,0 @@ - -//DOC: ## logging functions - -//DOC: `void print (string message):` -//DOC: write standard messages to stdout -//DOC: Example usage: -//DOC: ```vala -//DOC: print ("Hello world!"); -//DOC: ``` -public extern void print (string msg); -//DOC: `void warning (string message):` -//DOC: write warning message like this: -//DOC: ```yaml -//DOC: WARNING: message -//DOC: ``` -public extern void warning (string msg); -#if DEBUG -//DOC: `void debug (string message):` -//DOC: write debug messages. Its only print if debug mode enabled. -public extern void debug (string msg); -//DOC: `void info (string message):` -//DOC: write additional info messages. -#endif -public extern void info (string msg); - -//DOC: `void print_stderr (string message):` -//DOC: same with print but write to stderr -public extern void print_stderr (string msg); - -public void warning_fn (string message) { - print_stderr ("%s: %s".printf (colorize (_ ("WARNING"), yellow), message)); -} - -#if DEBUG -private long last_debug_time = -1; -public void debug_fn (string message) { - if (last_debug_time == -1) { - last_debug_time = get_epoch(); - } - print_stderr ("[%s:%d]: %s".printf (colorize (_ ("DEBUG"), blue), (int) (get_epoch () - last_debug_time) , message)); - last_debug_time = get_epoch(); -} -#endif - -public void info_fn (string message) { - print_stderr ("%s: %s".printf (colorize (_ ("INFO"), green), message)); -} - -public extern void logger_init (); - - -//DOC: `void set_terminal_title (string msg):` -//DOC: set terminal title -public void set_terminal_title (string msg) { - #if NOCOLOR - return; - #else - if (!get_bool ("no-color")) { - print_fn ("\x1b]2;" + msg + "\x07", false, false); - } - #endif -} - diff --git a/src/vapi/logger.vapi b/src/vapi/logger.vapi new file mode 100755 index 0000000..f84adf8 --- /dev/null +++ b/src/vapi/logger.vapi @@ -0,0 +1,31 @@ +[CCode (cheader_filename = "logger.h")] +public void print(string msg); + +[CCode (cheader_filename = "logger.h")] +public void warning(string msg); + +#if DEBUG +[CCode (cheader_filename = "logger.h")] +public void debug(string msg); + +[CCode (cheader_filename = "logger.h")] +public void debug_fn (string message); +#endif + +[CCode (cheader_filename = "logger.h")] +public void info(string msg); + +[CCode (cheader_filename = "logger.h")] +public void print_stderr(string msg); + +[CCode (cheader_filename = "logger.h")] +public void warning_fn (string message); + +[CCode (cheader_filename = "logger.h")] +public void info_fn (string message); + +[CCode (cheader_filename = "logger.h")] +public void logger_init (); + +[CCode (cheader_filename = "logger.h")] +public void set_terminal_title (string message);