Skip to content

Commit

Permalink
Merge pull request #23 from hackerschoice/ansi
Browse files Browse the repository at this point in the history
ANSI fixes and cleanup
  • Loading branch information
rootTHC authored Feb 23, 2021
2 parents e427d6d + 95c6e89 commit 8f5b430
Show file tree
Hide file tree
Showing 15 changed files with 417 additions and 258 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dnl Process this File with autoconf to produce a configure script.
AC_PREREQ(2.61)
AC_INIT([gsocket], 1.4.24)
AC_INIT([gsocket], 1.4.25)
dnl AC_CONFIG_AUX_DIR(config-x86_64-apple-darwin19.6.0)
AC_CONFIG_AUX_DIR(config)
AC_CANONICAL_SYSTEM
Expand Down
6 changes: 5 additions & 1 deletion include/gsocket/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ typedef struct
void GS_BUF_init(GS_BUF *gsb, size_t sz_min_free);
void GS_BUF_free(GS_BUF *gsb);
int GS_BUF_resize(GS_BUF *gsb, size_t sz_new);
int GS_BUF_add(GS_BUF *gsb, size_t len);
int GS_BUF_add_length(GS_BUF *gsb, size_t len);
int GS_BUF_add_data(GS_BUF *gsb, void *data, size_t len);
int GS_BUF_del(GS_BUF *gsb, size_t len);
int GS_BUF_memmove(GS_BUF *gsb, void *data, size_t len);

#define GS_BUF_empty(gsb) (gsb)->sz_used = 0;
#define GS_BUF_DATA(gsb) (gsb)->data
#define GS_BUF_IS_INIT(gsb) ((gsb)->sz_max_add!=0)
#define GS_BUF_UNUSED(gsb) ((gsb)->sz_total - (gsb)->sz_used)
#define GS_BUF_RSRC(gsb) (gsb)->data
#define GS_BUF_WDST(gsb) ((uint8_t *)(gsb)->data + (gsb)->sz_used)
Expand Down
2 changes: 2 additions & 0 deletions include/gsocket/gsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
#define GS_TOKEN_SIZE (16) /* 128 bit */

#define GS_TV_TO_USEC(tv) ((uint64_t)(tv)->tv_sec * 1000000 + (tv)->tv_usec)
#define GS_TV_TO_MSEC(tv) ((uint64_t)(tv)->tv_sec * 1000 + (tv)->tv_usec/1000)
#define GS_TV_DIFF(tv_a, tv_b) (GS_TV_TO_USEC(tv_b) - GS_TV_TO_USEC(tv_a))
#define GS_SEC_TO_USEC(sec) ((uint64_t)sec * 1000000)
#define GS_MSEC_TO_USEC(ms) ((uint64_t)ms * 1000)
#define GS_USEC_TO_SEC(usec) (usec / 1000000)
#define GS_USEC_TO_MSEC(usec) (usec / 1000)
#define GS_USEC_TO_TV(tv, usec) do { (tv)->tv_sec = (usec) / 1000000; (tv)->tv_usec = (usec) % 1000000; } while(0)

#define GS_SECRET_MAX_LEN (256 / 8) /* max length in bytes */
Expand Down
13 changes: 12 additions & 1 deletion lib/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ GS_BUF_resize(GS_BUF *gsb, size_t sz_new)
}

int
GS_BUF_add(GS_BUF *gsb, size_t len)
GS_BUF_add_length(GS_BUF *gsb, size_t len)
{
// Bail. There is sz_max_add space available but looks like caller wrote
// more ata...
Expand All @@ -65,6 +65,17 @@ GS_BUF_add_data(GS_BUF *gsb, void *data, size_t len)
return 0;
}

int
GS_BUF_memmove(GS_BUF *gsb, void *data, size_t len)
{
GS_BUF_resize(gsb, len);
memmove((uint8_t *)gsb->data + gsb->sz_used, data, len);

gsb->sz_used += len;

return 0;
}

/*
* Consume data from beginning.
*/
Expand Down
2 changes: 1 addition & 1 deletion man/gs-netcat.1
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Generate a secure random password and output it to standard output.
.It Fl l
Server mode. The default mode is client.
.It Fl q
Quite mode. Do not output any warnings or errors.
Quiet mode. Do not output any warnings or errors.
.It Fl w
Client to wait for the listening server to become available.
.It Fl r
Expand Down
4 changes: 2 additions & 2 deletions tools/4_gs-netcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1147,10 +1147,10 @@ my_getopt(int argc, char *argv[])
if (gopt.is_daemon)
{
if (gopt.is_logfile == 0)
gopt.is_quite = 1;
gopt.is_quiet = 1;
}

if (gopt.is_quite != 0)
if (gopt.is_quiet != 0)
{
gopt.log_fp = NULL;
gopt.err_fp = NULL;
Expand Down
44 changes: 32 additions & 12 deletions tools/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct _gopt
int is_multi_peer; /* -p / -S / -d [client & server] */
int is_daemon;
int is_logfile;
int is_quite;
int is_quiet;
int is_win_resized; // window size changed (signal)
int is_console; // console is being displayed
int is_pong_pending; // Server: Answer to PING waiting to be send
Expand Down Expand Up @@ -210,7 +210,8 @@ struct _peer
#define GSC_FL_IS_SERVER (0x01)


extern struct _gopt gopt;
extern struct _gopt gopt; // declared in utils.c

#define xfprintf(fp, a...) do {if (fp != NULL) { fprintf(fp, a); fflush(fp); } } while (0)

#define int_ntoa(x) inet_ntoa(*((struct in_addr *)&x))
Expand All @@ -233,16 +234,34 @@ extern struct _gopt gopt;
#define D_BYEL(a) "\033[1;33m"a"\033[0m"
#define D_BBLU(a) "\033[1;34m"a"\033[0m"
#define D_BMAG(a) "\033[1;35m"a"\033[0m"

#ifdef DEBUG
# define DEBUGF(a...) do{xfprintf(gopt.err_fp, "DEBUG %s:%d: ", __func__, __LINE__); xfprintf(gopt.err_fp, a); }while(0)
# define DEBUGF_R(a...) do{xfprintf(gopt.err_fp, "DEBUG %s:%d: ", __func__, __LINE__); xfprintf(gopt.err_fp, "\033[1;31m"); xfprintf(gopt.err_fp, a); xfprintf(gopt.err_fp, "\033[0m"); }while(0)
# define DEBUGF_G(a...) do{xfprintf(gopt.err_fp, "DEBUG %s:%d: ", __func__, __LINE__); xfprintf(gopt.err_fp, "\033[1;32m"); xfprintf(gopt.err_fp, a); xfprintf(gopt.err_fp, "\033[0m"); }while(0)
# define DEBUGF_B(a...) do{xfprintf(gopt.err_fp, "DEBUG %s:%d: ", __func__, __LINE__); xfprintf(gopt.err_fp, "\033[1;34m"); xfprintf(gopt.err_fp, a); xfprintf(gopt.err_fp, "\033[0m"); }while(0)
# define DEBUGF_Y(a...) do{xfprintf(gopt.err_fp, "DEBUG %s:%d: ", __func__, __LINE__); xfprintf(gopt.err_fp, "\033[1;33m"); xfprintf(gopt.err_fp, a); xfprintf(gopt.err_fp, "\033[0m"); }while(0)
# define DEBUGF_M(a...) do{xfprintf(gopt.err_fp, "DEBUG %s:%d: ", __func__, __LINE__); xfprintf(gopt.err_fp, "\033[1;35m"); xfprintf(gopt.err_fp, a); xfprintf(gopt.err_fp, "\033[0m"); }while(0)
# define DEBUGF_C(a...) do{xfprintf(gopt.err_fp, "DEBUG %s:%d: ", __func__, __LINE__); xfprintf(gopt.err_fp, "\033[1;36m"); xfprintf(gopt.err_fp, a); xfprintf(gopt.err_fp, "\033[0m"); }while(0)
# define DEBUGF_W(a...) do{xfprintf(gopt.err_fp, "DEBUG %s:%d: ", __func__, __LINE__); xfprintf(gopt.err_fp, "\033[1;37m"); xfprintf(gopt.err_fp, a); xfprintf(gopt.err_fp, "\033[0m"); }while(0)
#else
struct _g_debug_ctx
{
struct timeval tv_last;
struct timeval tv_now;
};

extern struct _g_debug_ctx g_dbg_ctx; // declared in utils.c

#define DEBUGF_T(xcolor, a...) do { \
gettimeofday(&g_dbg_ctx.tv_now, NULL); \
if (g_dbg_ctx.tv_last.tv_sec == 0) { memcpy(&g_dbg_ctx.tv_last, &g_dbg_ctx.tv_now, sizeof g_dbg_ctx.tv_last); } \
xfprintf(gopt.err_fp, "DEBUG %4llu %s:%d %s", GS_TV_TO_MSEC(&g_dbg_ctx.tv_now) - GS_TV_TO_MSEC(&g_dbg_ctx.tv_last), __func__, __LINE__, xcolor?xcolor:""); \
memcpy(&g_dbg_ctx.tv_last, &g_dbg_ctx.tv_now, sizeof g_dbg_ctx.tv_last); \
xfprintf(gopt.err_fp, a); \
if (xcolor) { xfprintf(gopt.err_fp, "\033[0m"); } \
} while (0)

# define DEBUGF(a...) do{DEBUGF_T(NULL, a); } while(0)
# define DEBUGF_R(a...) do{DEBUGF_T("\033[1;31m", a); } while(0)
# define DEBUGF_G(a...) do{DEBUGF_T("\033[1;32m", a); } while(0)
# define DEBUGF_B(a...) do{DEBUGF_T("\033[1;34m", a); } while(0)
# define DEBUGF_Y(a...) do{DEBUGF_T("\033[1;33m", a); } while(0)
# define DEBUGF_M(a...) do{DEBUGF_T("\033[1;35m", a); } while(0)
# define DEBUGF_C(a...) do{DEBUGF_T("\033[1;36m", a); } while(0)
# define DEBUGF_W(a...) do{DEBUGF_T("\033[1;37m", a); } while(0)
#else // DEBUG
# define DEBUGF(a...)
# define DEBUGF_R(a...)
# define DEBUGF_G(a...)
Expand All @@ -251,6 +270,7 @@ extern struct _gopt gopt;
# define DEBUGF_M(a...)
# define DEBUGF_C(a...)
# define DEBUGF_W(a...)
# define DEBUGF_A(a...)
#endif

// Increase ptr by number of characters added to ptr.
Expand Down Expand Up @@ -319,7 +339,7 @@ extern struct _gopt gopt;
#ifdef DEBUG
# define HEXDUMP(a, _len) do { \
size_t _n = 0; \
xfprintf(gopt.err_fp, "%s:%d HEX ", __FILE__, __LINE__); \
xfprintf(gopt.err_fp, "%s:%d HEX[%zd] ", __FILE__, __LINE__, _len); \
while (_n < (_len)) xfprintf(gopt.err_fp, "%2.2x", ((unsigned char *)a)[_n++]); \
xfprintf(gopt.err_fp, "\n"); \
} while (0)
Expand Down
Loading

0 comments on commit 8f5b430

Please sign in to comment.