Blame frontend/console.c

Packit 47f805
#ifdef HAVE_CONFIG_H
Packit 47f805
# include <config.h>
Packit 47f805
#endif
Packit 47f805
Packit 47f805
#ifdef STDC_HEADERS
Packit 47f805
# include <stdlib.h>
Packit 47f805
# include <string.h>
Packit 47f805
#else
Packit 47f805
# ifndef HAVE_STRCHR
Packit 47f805
#  define strchr index
Packit 47f805
#  define strrchr rindex
Packit 47f805
# endif
Packit 47f805
char   *strchr(), *strrchr();
Packit 47f805
# ifndef HAVE_MEMCPY
Packit 47f805
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
Packit 47f805
#  define memmove(d, s, n) bcopy ((s), (d), (n))
Packit 47f805
# endif
Packit 47f805
#endif
Packit 47f805
Packit 47f805
#if defined(HAVE_NCURSES_TERMCAP_H)
Packit 47f805
# include <ncurses/termcap.h>
Packit 47f805
#elif defined(HAVE_TERMCAP_H)
Packit 47f805
# include <termcap.h>
Packit 47f805
#elif defined(HAVE_TERMCAP)
Packit 47f805
# include <curses.h>
Packit 47f805
# if !defined(__bsdi__)
Packit 47f805
#  include <term.h>
Packit 47f805
# endif
Packit 47f805
#endif
Packit 47f805
Packit 47f805
#include <stdio.h>
Packit 47f805
#include <stdarg.h>
Packit 47f805
#include "console.h"
Packit 47f805
#include "main.h"
Packit 47f805
Packit 47f805
#ifdef WITH_DMALLOC
Packit 47f805
#include <dmalloc.h>
Packit 47f805
#endif
Packit 47f805
Packit 47f805
#define CLASS_ID           0x434F4E53
Packit 47f805
#define REPORT_BUFF_SIZE   1024
Packit 47f805
Packit 47f805
#if defined(_WIN32)  &&  !defined(__CYGWIN__)
Packit 47f805
# include <windows.h>
Packit 47f805
#endif
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
static int
Packit 47f805
my_console_printing(FILE * fp, const char *format, va_list ap)
Packit 47f805
{
Packit 47f805
    if (fp != NULL)
Packit 47f805
        return vfprintf(fp, format, ap);
Packit 47f805
    return 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
static int
Packit 47f805
my_error_printing(FILE * fp, const char *format, va_list ap)
Packit 47f805
{
Packit 47f805
    if (fp != NULL)
Packit 47f805
        return vfprintf(fp, format, ap);
Packit 47f805
    return 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
static int
Packit 47f805
my_report_printing(FILE * fp, const char *format, va_list ap)
Packit 47f805
{
Packit 47f805
    if (fp != NULL)
Packit 47f805
        return vfprintf(fp, format, ap);
Packit 47f805
    return 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
/*
Packit 47f805
 * Taken from Termcap_Manual.html:
Packit 47f805
 *
Packit 47f805
 * With the Unix version of termcap, you must allocate space for the description yourself and pass
Packit 47f805
 * the address of the space as the argument buffer. There is no way you can tell how much space is
Packit 47f805
 * needed, so the convention is to allocate a buffer 2048 characters long and assume that is
Packit 47f805
 * enough.  (Formerly the convention was to allocate 1024 characters and assume that was enough.
Packit 47f805
 * But one day, for one kind of terminal, that was not enough.)
Packit 47f805
 */
Packit 47f805
Packit 47f805
#ifdef HAVE_TERMCAP
Packit 47f805
Packit 47f805
static void
Packit 47f805
get_termcap_string(char const* id, char* dest, size_t n)
Packit 47f805
{
Packit 47f805
    char    tc[16];
Packit 47f805
    char   *tp = tc;
Packit 47f805
    tp[0] = '\0';
Packit 47f805
    tp = tgetstr(id, &tp);
Packit 47f805
    if (tp != NULL && dest != NULL && n > 0) {
Packit 47f805
        strncpy(dest, tp, n);
Packit 47f805
        dest[n-1] = '\0';
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
static void 
Packit 47f805
get_termcap_number(char const* id, int* dest, int low, int high)
Packit 47f805
{
Packit 47f805
    int const val = tgetnum(id);
Packit 47f805
    if (low <= val && val <= high) {
Packit 47f805
        *dest = val;
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
static void
Packit 47f805
apply_termcap_settings(Console_IO_t * const mfp)
Packit 47f805
{
Packit 47f805
    /* try to catch additional information about special console sequences */
Packit 47f805
    char const* term_name = getenv("TERM");
Packit 47f805
    if (NULL != term_name) {
Packit 47f805
        char    term_buff[4096];
Packit 47f805
        int const ret = tgetent(term_buff, term_name);
Packit 47f805
        if (1 == ret) {
Packit 47f805
            get_termcap_number("co", &mfp->disp_width, 40, 512);
Packit 47f805
            get_termcap_number("li", &mfp->disp_height, 16, 256);
Packit 47f805
            get_termcap_string("up", mfp->str_up, sizeof(mfp->str_up));
Packit 47f805
            get_termcap_string("md", mfp->str_emph, sizeof(mfp->str_emph));
Packit 47f805
            get_termcap_string("me", mfp->str_norm, sizeof(mfp->str_norm));
Packit 47f805
            get_termcap_string("ce", mfp->str_clreoln, sizeof(mfp->str_clreoln));
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
#endif /* TERMCAP_AVAILABLE */
Packit 47f805
Packit 47f805
static int
Packit 47f805
init_console(Console_IO_t * const mfp)
Packit 47f805
{
Packit 47f805
    /* setup basics of brhist I/O channels */
Packit 47f805
    mfp->disp_width = 80;
Packit 47f805
    mfp->disp_height = 25;
Packit 47f805
    mfp->Console_fp = stderr;
Packit 47f805
    mfp->Error_fp = stderr;
Packit 47f805
    mfp->Report_fp = NULL;
Packit 47f805
Packit 47f805
    /*mfp -> Console_buff = calloc ( 1, REPORT_BUFF_SIZE ); */
Packit 47f805
    setvbuf(mfp->Console_fp, mfp->Console_buff, _IOFBF, sizeof(mfp->Console_buff));
Packit 47f805
/*  setvbuf ( mfp -> Error_fp  , NULL                   , _IONBF, 0                                ); */
Packit 47f805
Packit 47f805
#if defined(_WIN32)  &&  !defined(__CYGWIN__)
Packit 47f805
    mfp->Console_Handle = GetStdHandle(STD_ERROR_HANDLE);
Packit 47f805
#endif
Packit 47f805
Packit 47f805
    strcpy(mfp->str_up, "\033[A");
Packit 47f805
Packit 47f805
#ifdef HAVE_TERMCAP
Packit 47f805
    apply_termcap_settings(mfp);
Packit 47f805
#endif /* TERMCAP_AVAILABLE */
Packit 47f805
Packit 47f805
    mfp->ClassID = CLASS_ID;
Packit 47f805
Packit 47f805
#if defined(_WIN32)  &&  !defined(__CYGWIN__)
Packit 47f805
    mfp->Console_file_type = GetFileType(Console_IO.Console_Handle);
Packit 47f805
#else
Packit 47f805
    mfp->Console_file_type = 0;
Packit 47f805
#endif
Packit 47f805
    return 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
static void
Packit 47f805
deinit_console(Console_IO_t * const mfp)
Packit 47f805
{
Packit 47f805
    if (mfp->Report_fp != NULL) {
Packit 47f805
        fclose(mfp->Report_fp);
Packit 47f805
        mfp->Report_fp = NULL;
Packit 47f805
    }
Packit 47f805
    fflush(mfp->Console_fp);
Packit 47f805
    setvbuf(mfp->Console_fp, NULL, _IONBF, (size_t) 0);
Packit 47f805
Packit 47f805
    memset(mfp->Console_buff, 0x55, REPORT_BUFF_SIZE);
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
/*  LAME console
Packit 47f805
 */
Packit 47f805
Console_IO_t Console_IO;
Packit 47f805
Packit 47f805
int
Packit 47f805
frontend_open_console(void)
Packit 47f805
{
Packit 47f805
    return init_console(&Console_IO);
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
frontend_close_console(void)
Packit 47f805
{
Packit 47f805
    deinit_console(&Console_IO);
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
frontend_debugf(const char *format, va_list ap)
Packit 47f805
{
Packit 47f805
    (void) my_report_printing(Console_IO.Report_fp, format, ap);
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
frontend_msgf(const char *format, va_list ap)
Packit 47f805
{
Packit 47f805
    (void) my_console_printing(Console_IO.Console_fp, format, ap);
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
frontend_errorf(const char *format, va_list ap)
Packit 47f805
{
Packit 47f805
    (void) my_error_printing(Console_IO.Error_fp, format, ap);
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
frontend_print_null(const char *format, va_list ap)
Packit 47f805
{
Packit 47f805
    (void) format;
Packit 47f805
    (void) ap;
Packit 47f805
}
Packit 47f805
Packit 47f805
int
Packit 47f805
console_printf(const char *format, ...)
Packit 47f805
{
Packit 47f805
    va_list args;
Packit 47f805
    int     ret;
Packit 47f805
Packit 47f805
    va_start(args, format);
Packit 47f805
    ret = my_console_printing(Console_IO.Console_fp, format, args);
Packit 47f805
    va_end(args);
Packit 47f805
Packit 47f805
    return ret;
Packit 47f805
}
Packit 47f805
Packit 47f805
int
Packit 47f805
error_printf(const char *format, ...)
Packit 47f805
{
Packit 47f805
    va_list args;
Packit 47f805
    int     ret;
Packit 47f805
Packit 47f805
    va_start(args, format);
Packit 47f805
    ret = my_console_printing(Console_IO.Error_fp, format, args);
Packit 47f805
    va_end(args);
Packit 47f805
Packit 47f805
    return ret;
Packit 47f805
}
Packit 47f805
Packit 47f805
int
Packit 47f805
report_printf(const char *format, ...)
Packit 47f805
{
Packit 47f805
    va_list args;
Packit 47f805
    int     ret;
Packit 47f805
Packit 47f805
    va_start(args, format);
Packit 47f805
    ret = my_console_printing(Console_IO.Report_fp, format, args);
Packit 47f805
    va_end(args);
Packit 47f805
Packit 47f805
    return ret;
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
console_flush()
Packit 47f805
{
Packit 47f805
    fflush(Console_IO.Console_fp);
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
error_flush()
Packit 47f805
{
Packit 47f805
    fflush(Console_IO.Error_fp);
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
report_flush()
Packit 47f805
{
Packit 47f805
    fflush(Console_IO.Report_fp);
Packit 47f805
}
Packit 47f805
Packit 47f805
void
Packit 47f805
console_up(int n_lines)
Packit 47f805
{
Packit 47f805
#if defined(_WIN32)  &&  !defined(__CYGWIN__)
Packit 47f805
    if (Console_IO.Console_file_type != FILE_TYPE_PIPE) {
Packit 47f805
        COORD   Pos;
Packit 47f805
        CONSOLE_SCREEN_BUFFER_INFO CSBI;
Packit 47f805
Packit 47f805
        console_flush();
Packit 47f805
        GetConsoleScreenBufferInfo(Console_IO.Console_Handle, &CSBI);
Packit 47f805
        Pos.Y = (SHORT)(CSBI.dwCursorPosition.Y - n_lines);
Packit 47f805
        Pos.X = 0;
Packit 47f805
        SetConsoleCursorPosition(Console_IO.Console_Handle, Pos);
Packit 47f805
    }
Packit 47f805
#else
Packit 47f805
    while (n_lines-- > 0)
Packit 47f805
        fputs(Console_IO.str_up, Console_IO.Console_fp);
Packit 47f805
    console_flush();
Packit 47f805
#endif
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
void
Packit 47f805
set_debug_file(const char *fn)
Packit 47f805
{
Packit 47f805
    if (Console_IO.Report_fp == NULL) {
Packit 47f805
        Console_IO.Report_fp = lame_fopen(fn, "a");
Packit 47f805
        if (Console_IO.Report_fp != NULL) {
Packit 47f805
            error_printf("writing debug info into: %s\n", fn);
Packit 47f805
        }
Packit 47f805
        else {
Packit 47f805
            error_printf("Error: can't open for debug info: %s\n", fn);
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
/* end of console.c */