CONSOLE_SCREEN_BUFFER_INFO cinfo;
GetConsoleScreenBufferInfo(
- (h == stdout) ?
+ (GMQCC_IS_STDOUT(h)) ?
GetStdHandle(STD_OUTPUT_HANDLE) :
GetStdHandle(STD_ERROR_HANDLE), &cinfo
);
}
/* restore */
SetConsoleTextAttribute(
- (h == stdout) ?
+ (GMQCC_IS_STDOUT(h)) ?
GetStdHandle(STD_OUTPUT_HANDLE) :
GetStdHandle(STD_ERROR_HANDLE),
icolor
console.color_err = !!(isatty(STDERR_FILENO));
if (console.handle_out == stderr || console.handle_out == stdout)
console.color_out = !!(isatty(STDOUT_FILENO));
-
- #ifndef _WIN32
- {
- char buf[4];
-
- /*
- * This is such a hack. But I'm not linking in any libraries to
- * do this stupidity. It's insane there is simply not a ttyhascolor
- * in unistd.h
- */
- FILE *tput = popen("tput colors", "r");
- if (!tput) {
- /*
- * disable colors since we can't determine without tput
- * which should be guranteed on all *nix OSes
- */
- console.color_err = 0;
- console.color_out = 0;
- }
-
- /*
- * Handle to tput was a success, lets read in the amount of
- * color support. It should be at minimal 8.
- */
- fread(buf, sizeof(buf), 1, tput);
- buf[3] = '\0';
-
- if (atoi(buf) < 8) {
- console.color_err = 0;
- console.color_out = 0;
- }
-
- /*
- * We made it this far, which means we support colors in the
- * terminal.
- */
- fclose(tput);
- }
- #endif
}
/*
con_close();
if (GMQCC_IS_DEFINE((FILE*)out)) {
- console.handle_out = (((FILE*)err) == stdout) ? stdout : stderr;
+ console.handle_out = (((FILE*)out) == stdout) ? stdout : stderr;
con_enablecolor();
} else if (!(console.handle_out = fopen(out, "w"))) return 0;
con_enablecolor();
} else if (!(console.handle_err = fopen(err, "w"))) return 0;
+ // no buffering
+ setvbuf(console.handle_out, NULL, _IONBF, 0);
+ setvbuf(console.handle_err, NULL, _IONBF, 0);
+
return 1;
}
return con_write(console.handle_out, fmt, va);
}
+/*
+ * Standard stdout/stderr printf functions used generally where they need
+ * to be used.
+ */
int con_err(const char *fmt, ...) {
va_list va;
int ln = 0;
va_end (va);
return ln;
}
+
+/*
+ * Utility console message writes for lexer contexts. These will allow
+ * for reporting of file:line based on lexer context, These are used
+ * heavily in the parser/ir/ast.
+ */
+void con_vprintmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap) {
+ /* color selection table */
+ static int sel[] = {
+ CON_WHITE,
+ CON_CYAN,
+ CON_RED
+ };
+
+ int err = !!(level == LVL_ERROR);
+ int color = (err) ? console.color_err : console.color_out;
+ int (*print)(const char *, ...) = (err) ? &con_err : &con_out;
+
+ if (color)
+ print("\033[0;%dm%s:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, sel[level], msgtype);
+ else
+ print("%s:%d: %s: ", name, (int)line, msgtype);
+
+ con_verr(msg, ap);
+ fprintf (stderr, "\n");
+}
+
+void con_printmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, ...) {
+ va_list va;
+ va_start(va, msg);
+ con_vprintmsg(level, name, line, msgtype, msg, va);
+ va_end (va);
+}
+
+void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap) {
+ con_vprintmsg(lvl, ((lex_ctx*)ctx)->file, ((lex_ctx*)ctx)->line, msgtype, msg, ap);
+}
+
+void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...) {
+ va_list va;
+ va_start(va, msg);
+ con_cvprintmsg(ctx, lvl, msgtype, msg, va);
+ va_end (va);
+}