| version 1.9, 2004/04/08 13:08:22 | version 1.18, 2007/12/16 14:58:24 | 
| Line 1 | Line 1 | 
 | #include        "compiler.h" | #include        "compiler.h" | 
 | #include        <stdarg.h> | #include        <stdarg.h> | 
 | #include        "strres.h" | #include        "strres.h" | 
 |  | #include        "textfile.h" | 
 |  | #include        "oemtext.h" | 
 | #include        "dosio.h" | #include        "dosio.h" | 
 | #include        "ini.h" | #include        "ini.h" | 
 |  |  | 
 |  |  | 
 | #ifdef TRACE | #ifdef TRACE | 
 |  |  | 
 | #define FILEBUFSIZE                     (1 << 20) |  | 
 | // #define FILELASTBUFONLY |  | 
 |  |  | 
 | #ifdef STRICT | #ifdef STRICT | 
 | #define SUBCLASSPROC    WNDPROC | #define SUBCLASSPROC    WNDPROC | 
 | #else | #else | 
| Line 24 | Line 23 | 
 | #define VIEW_SIZE               12 | #define VIEW_SIZE               12 | 
 |  |  | 
 | typedef struct { | typedef struct { | 
| BYTE    en; | UINT8           en; | 
| FILEH   fh; | TEXTFILEH       tf; | 
| HWND    hwnd; | HWND            hwnd; | 
 | } TRACEWIN; | } TRACEWIN; | 
 |  |  | 
 | typedef struct { | typedef struct { | 
| Line 36  typedef struct { | Line 35  typedef struct { | 
 | int             height; | int             height; | 
 | } TRACECFG; | } TRACECFG; | 
 |  |  | 
| extern  HINSTANCE       hInst; | extern  HINSTANCE       g_hInstance; | 
| extern  HINSTANCE       hPrev; |  | 
 |  |  | 
 | enum { | enum { | 
 | IDM_TRACE1              = 3300, | IDM_TRACE1              = 3300, | 
| Line 47  enum { | Line 45  enum { | 
 | IDM_TRACECL | IDM_TRACECL | 
 | }; | }; | 
 |  |  | 
| static const char       ProgTitle[] = "console"; | static const TCHAR ProgTitle[] = _T("console"); | 
| static const char       ClassName[] = "TRACE-console"; | static const TCHAR ClassName[] = _T("TRACE-console"); | 
| static const char       ClassEdit[] = "EDIT"; | static const TCHAR ClassEdit[] = _T("EDIT"); | 
| static const char       trace1[] = "TRACE"; | static const TCHAR viewfont[] = _T(VIEW_TEXT); | 
| static const char       trace2[] = "VERBOSE"; | static const TCHAR trace1[] = _T("TRACE"); | 
| static const char       traceen[] = "Enable"; | static const TCHAR trace2[] = _T("VERBOSE"); | 
| static const char       tracefh[] = "File out"; | static const TCHAR traceen[] = _T("Enable"); | 
| static const char       tracecl[] = "Clear"; | static const TCHAR tracefh[] = _T("File out"); | 
| static const char       crlf[] = "\r\n"; | static const TCHAR tracecl[] = _T("Clear"); | 
|  | static const OEMCHAR crlf[] = OEMTEXT("\r\n"); | 
 |  |  | 
 | static  TRACEWIN        tracewin; | static  TRACEWIN        tracewin; | 
 | static  HWND            hView = NULL; | static  HWND            hView = NULL; | 
 | static  HFONT           hfView = NULL; | static  HFONT           hfView = NULL; | 
 | static  HBRUSH          hBrush = NULL; | static  HBRUSH          hBrush = NULL; | 
| static  char            szView[VIEW_BUFFERSIZE]; | static  int                     viewpos; | 
|  | static  int                     viewleng; | 
|  | static  TCHAR           viewbuf[VIEW_BUFFERSIZE * 2]; | 
 | static  TRACECFG        tracecfg; | static  TRACECFG        tracecfg; | 
 |  | static  int                     devpos; | 
 |  | static  char            devstr[256]; | 
 |  |  | 
| static const char       np2trace[] = "np2trace.ini"; | static const OEMCHAR np2trace[] = OEMTEXT("np2trace.ini"); | 
| static const char       inititle[] = "TRACE"; | static const OEMCHAR inititle[] = OEMTEXT("TRACE"); | 
| static const INITBL     initbl[4] = { | static const PFTBL initbl[4] = { | 
| {"posx",        INITYPE_SINT32, &tracecfg.posx,         0}, | PFVAL("posx",   PFTYPE_SINT32,  &tracecfg.posx), | 
| {"posy",        INITYPE_SINT32, &tracecfg.posy,         0}, | PFVAL("posy",   PFTYPE_SINT32,  &tracecfg.posy), | 
| {"width",       INITYPE_SINT32, &tracecfg.width,        0}, | PFVAL("width",  PFTYPE_SINT32,  &tracecfg.width), | 
| {"height",      INITYPE_SINT32, &tracecfg.height,       0}}; | PFVAL("height", PFTYPE_SINT32,  &tracecfg.height)}; | 
 |  |  | 
 | static void View_ScrollToBottom(HWND hWnd) { | static void View_ScrollToBottom(HWND hWnd) { | 
 |  |  | 
| Line 83  static void View_ScrollToBottom(HWND hWn | Line 86  static void View_ScrollToBottom(HWND hWn | 
 |  |  | 
 | static void View_ClrString(void) { | static void View_ClrString(void) { | 
 |  |  | 
| szView[0] = '\0'; | viewpos = 0; | 
| SetWindowText(hView, szView); | viewleng = 0; | 
|  | viewbuf[0] = '\0'; | 
|  | SetWindowText(hView, viewbuf); | 
 | } | } | 
 |  |  | 
| static void View_AddString(const char *lpszString) { | static void View_AddString(const OEMCHAR *string) { | 
 |  |  | 
| int             len, vlen; | int             slen; | 
| char    *p; | int             vpos; | 
|  | int             vlen; | 
|  | TCHAR   c; | 
 |  |  | 
| len = strlen(lpszString); | #if defined(OEMCHAR_SAME_TCHAR) | 
| if ((!len) || ((len + 3) > VIEW_BUFFERSIZE)) { | slen = lstrlen(string); | 
|  | #else | 
|  | slen = oemtotchar(NULL, 0, string, (UINT)-1) - 1; | 
|  | #endif | 
|  | if ((slen == 0) || ((slen + 3) > VIEW_BUFFERSIZE)) { | 
 | return; | return; | 
 | } | } | 
| vlen = strlen(szView); | vpos = viewpos; | 
| if ((vlen + len + 3) > VIEW_BUFFERSIZE) { | vlen = viewleng; | 
| p = szView; | if ((vpos + vlen + slen + 3) > (VIEW_BUFFERSIZE * 2)) { | 
| while(*p) { | while(vlen > 0) { | 
 | vlen--; | vlen--; | 
| if ((*p++ == 0x0a) && ((vlen + len + 3) <= VIEW_BUFFERSIZE)) { | c = viewbuf[vpos++]; | 
|  | if ((c == 0x0a) && ((vlen + slen + 3) <= VIEW_BUFFERSIZE)) { | 
 | break; | break; | 
 | } | } | 
 | } | } | 
| strcpy(szView, p); | if (vpos >= VIEW_BUFFERSIZE) { | 
| } | if (vlen) { | 
| strcat(szView, lpszString); | CopyMemory(viewbuf, viewbuf + vpos, vlen * sizeof(TCHAR)); | 
| strcat(szView, crlf); | } | 
| SetWindowText(hView, szView); | vpos = 0; | 
| View_ScrollToBottom(hView); | viewpos = 0; | 
| } |  | 
|  |  | 
|  |  | 
| // ---- |  | 
|  |  | 
| #if defined(FILEBUFSIZE) |  | 
| static  char    filebuf[FILEBUFSIZE]; |  | 
| static  UINT32  filebufpos; |  | 
| #endif |  | 
|  |  | 
| static void trfh_close(void) { |  | 
|  |  | 
| FILEH   fh; |  | 
|  |  | 
| fh = tracewin.fh; |  | 
| tracewin.fh = FILEH_INVALID; |  | 
| if (fh != FILEH_INVALID) { |  | 
| #if defined(FILEBUFSIZE) |  | 
| UINT size = filebufpos & (FILEBUFSIZE - 1); |  | 
| #if defined(FILELASTBUFONLY) |  | 
| if (filebufpos >= FILEBUFSIZE) { |  | 
| file_write(fh, filebuf + size, FILEBUFSIZE - size); |  | 
| } |  | 
| #endif |  | 
| if (size) { |  | 
| file_write(fh, filebuf, size); |  | 
| } |  | 
| #endif |  | 
| file_close(fh); |  | 
| } |  | 
| } |  | 
|  |  | 
| static void trfh_open(const char *fname) { |  | 
|  |  | 
| trfh_close(); |  | 
| tracewin.fh = file_create(fname); |  | 
| #if defined(FILEBUFSIZE) |  | 
| filebufpos = 0; |  | 
| #endif |  | 
| } |  | 
|  |  | 
| static void trfh_add(const char *buf) { |  | 
|  |  | 
| UINT    size; |  | 
|  |  | 
| size = strlen(buf); |  | 
| #if defined(FILEBUFSIZE) |  | 
| while(size) { |  | 
| UINT pos = filebufpos & (FILEBUFSIZE - 1); |  | 
| UINT rem = FILEBUFSIZE - pos; |  | 
| if (size >= rem) { |  | 
| CopyMemory(filebuf + pos, buf, rem); |  | 
| filebufpos += rem; |  | 
| buf += rem; |  | 
| size -= rem; |  | 
| #if !defined(FILELASTBUFONLY) |  | 
| file_write(tracewin.fh, filebuf, FILEBUFSIZE); |  | 
| #endif |  | 
| } |  | 
| else { |  | 
| CopyMemory(filebuf + pos, buf, size); |  | 
| filebufpos += size; |  | 
| break; |  | 
 | } | } | 
 | } | } | 
 |  | #if defined(OEMCHAR_SAME_TCHAR) | 
 |  | CopyMemory(viewbuf + vpos + vlen, string, slen * sizeof(TCHAR)); | 
 | #else | #else | 
| file_write(tracewin.fh, buf, size); | oemtotchar(viewbuf + vpos + vlen, slen + 1, string, (UINT)-1); | 
 | #endif | #endif | 
 |  | vlen += slen; | 
 |  | viewbuf[vpos + vlen + 0] = '\r'; | 
 |  | viewbuf[vpos + vlen + 1] = '\n'; | 
 |  | viewbuf[vpos + vlen + 2] = '\0'; | 
 |  | viewleng = vlen + 2; | 
 |  | SetWindowText(hView, viewbuf + vpos); | 
 |  | View_ScrollToBottom(hView); | 
 | } | } | 
 |  |  | 
 |  |  | 
| Line 218  static LRESULT CALLBACK traceproc(HWND h | Line 176  static LRESULT CALLBACK traceproc(HWND h | 
 | WS_CHILD | WS_VISIBLE | ES_READONLY | ES_LEFT | | WS_CHILD | WS_VISIBLE | ES_READONLY | ES_LEFT | | 
 | ES_MULTILINE | WS_VSCROLL | ES_AUTOVSCROLL, | ES_MULTILINE | WS_VSCROLL | ES_AUTOVSCROLL, | 
 | 0, 0, rc.right, rc.bottom, | 0, 0, rc.right, rc.bottom, | 
| hWnd, (HMENU)IDC_VIEW, hInst, NULL); | hWnd, (HMENU)IDC_VIEW, g_hInstance, NULL); | 
 | if (!hView) { | if (!hView) { | 
 | break; | break; | 
 | } | } | 
| Line 226  static LRESULT CALLBACK traceproc(HWND h | Line 184  static LRESULT CALLBACK traceproc(HWND h | 
 |  |  | 
 | hfView = CreateFont(VIEW_SIZE, 0, 0, 0, 0, 0, 0, 0, | hfView = CreateFont(VIEW_SIZE, 0, 0, 0, 0, 0, 0, 0, | 
 | SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, | SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, | 
| DEFAULT_QUALITY, FIXED_PITCH, VIEW_TEXT); | DEFAULT_QUALITY, FIXED_PITCH, viewfont); | 
 | if (!hfView) { | if (!hfView) { | 
 | break; | break; | 
 | } | } | 
| Line 260  static LRESULT CALLBACK traceproc(HWND h | Line 218  static LRESULT CALLBACK traceproc(HWND h | 
 | break; | break; | 
 |  |  | 
 | case IDM_TRACEFH: | case IDM_TRACEFH: | 
| if (tracewin.fh != FILEH_INVALID) { | if (tracewin.tf != NULL) { | 
| trfh_close(); | textfile_close(tracewin.tf); | 
|  | tracewin.tf = NULL; | 
 | } | } | 
 | else { | else { | 
| trfh_open("traceout.txt"); | tracewin.tf = textfile_create(OEMTEXT("traceout.txt"), | 
|  | 0x800); | 
 | } | } | 
 | hmenu = GetSystemMenu(hWnd, FALSE); | hmenu = GetSystemMenu(hWnd, FALSE); | 
 | CheckMenuItem(hmenu, IDM_TRACEFH, | CheckMenuItem(hmenu, IDM_TRACEFH, | 
| (tracewin.fh != FILEH_INVALID)? | (tracewin.tf)?MF_CHECKED:MF_UNCHECKED); | 
| MF_CHECKED:MF_UNCHECKED); |  | 
 | break; | break; | 
 |  |  | 
 | case IDM_TRACECL: | case IDM_TRACECL: | 
| Line 347  void trace_init(void) { | Line 306  void trace_init(void) { | 
 |  |  | 
 | HWND    hwnd; | HWND    hwnd; | 
 |  |  | 
| if (!hPrev) { | ZeroMemory(&tracewin, sizeof(tracewin)); | 
| WNDCLASS wc; | WNDCLASS wc; | 
| wc.style = CS_HREDRAW | CS_VREDRAW; | wc.style = CS_HREDRAW | CS_VREDRAW; | 
| wc.lpfnWndProc = traceproc; | wc.lpfnWndProc = traceproc; | 
| wc.cbClsExtra = 0; | wc.cbClsExtra = 0; | 
| wc.cbWndExtra = 0; | wc.cbWndExtra = 0; | 
| wc.hInstance = hInst; | wc.hInstance = g_hInstance; | 
| wc.hIcon = NULL; | wc.hIcon = NULL; | 
| wc.hCursor = LoadCursor(NULL, IDC_ARROW); | wc.hCursor = LoadCursor(NULL, IDC_ARROW); | 
| wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); | wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); | 
| wc.lpszMenuName = NULL; | wc.lpszMenuName = NULL; | 
| wc.lpszClassName = (LPCSTR)ClassName; | wc.lpszClassName = ClassName; | 
| if (!RegisterClass(&wc)) { | if (!RegisterClass(&wc)) { | 
| return; | return; | 
| } |  | 
 | } | } | 
 |  |  | 
 | #if 1 | #if 1 | 
| tracewin.en = 0; | tracewin.en = 4; | 
| tracewin.fh = FILEH_INVALID; |  | 
 | #else | #else | 
| tracewin.en = 0; | tracewin.en = 1; | 
| tracewin.fh = FILEH_INVALID; | tracewin.tf = textfile_create(OEMTEXT("traceout.txt"), 0x800); | 
| trfh_open("traces.txt"); |  | 
 | #endif | #endif | 
 |  |  | 
 | tracecfg.posx = CW_USEDEFAULT; | tracecfg.posx = CW_USEDEFAULT; | 
 | tracecfg.posy = CW_USEDEFAULT; | tracecfg.posy = CW_USEDEFAULT; | 
 | tracecfg.width = CW_USEDEFAULT; | tracecfg.width = CW_USEDEFAULT; | 
 | tracecfg.height = CW_USEDEFAULT; | tracecfg.height = CW_USEDEFAULT; | 
| ini_read(file_getcd(np2trace), inititle, initbl, 4); | ini_read(file_getcd(np2trace), inititle, initbl, NELEMENTS(initbl)); | 
 |  |  | 
 | hwnd = CreateWindowEx(WS_EX_CONTROLPARENT, | hwnd = CreateWindowEx(WS_EX_CONTROLPARENT, | 
 | ClassName, ProgTitle, | ClassName, ProgTitle, | 
 | WS_OVERLAPPEDWINDOW, | WS_OVERLAPPEDWINDOW, | 
 | tracecfg.posx, tracecfg.posy, | tracecfg.posx, tracecfg.posy, | 
 | tracecfg.width, tracecfg.height, | tracecfg.width, tracecfg.height, | 
| NULL, NULL, hInst, NULL); | NULL, NULL, g_hInstance, NULL); | 
 | tracewin.hwnd = hwnd; | tracewin.hwnd = hwnd; | 
 | if (hwnd == NULL) { | if (hwnd == NULL) { | 
 | return; | return; | 
| Line 395  void trace_init(void) { | Line 351  void trace_init(void) { | 
 |  |  | 
 | void trace_term(void) { | void trace_term(void) { | 
 |  |  | 
| if (tracewin.fh != FILEH_INVALID) { | if (tracewin.tf != NULL) { | 
| trfh_close(); | textfile_close(tracewin.tf); | 
|  | tracewin.tf = NULL; | 
 | } | } | 
 | if (tracewin.hwnd) { | if (tracewin.hwnd) { | 
 | DestroyWindow(tracewin.hwnd); | DestroyWindow(tracewin.hwnd); | 
 | tracewin.hwnd = NULL; | tracewin.hwnd = NULL; | 
| ini_write(file_getcd(np2trace), inititle, initbl, 4); | ini_write(file_getcd(np2trace), inititle, initbl, NELEMENTS(initbl)); | 
 | } | } | 
 | } | } | 
 |  |  | 
| Line 409  void trace_fmt(const char *fmt, ...) { | Line 366  void trace_fmt(const char *fmt, ...) { | 
 |  |  | 
 | BOOL    en; | BOOL    en; | 
 | va_list ap; | va_list ap; | 
| char    buf[0x1000]; | OEMCHAR buf[0x1000]; | 
 |  |  | 
 | en = (tracewin.en & 1) && | en = (tracewin.en & 1) && | 
| ((tracewin.en & 4) || (tracewin.fh != FILEH_INVALID)); | ((tracewin.en & 4) || (tracewin.tf != NULL)); | 
 | if (en) { | if (en) { | 
 | va_start(ap, fmt); | va_start(ap, fmt); | 
 |  | #if defined(OSLANG_UCS2) | 
 |  | OEMCHAR cnvfmt[0x800]; | 
 |  | MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, fmt, -1, | 
 |  | cnvfmt, NELEMENTS(cnvfmt)); | 
 |  | vswprintf(buf, cnvfmt, ap); | 
 |  | #else | 
 | vsprintf(buf, fmt, ap); | vsprintf(buf, fmt, ap); | 
 |  | #endif | 
 | va_end(ap); | va_end(ap); | 
 | if ((tracewin.en & 4) && (hView)) { | if ((tracewin.en & 4) && (hView)) { | 
 | View_AddString(buf); | View_AddString(buf); | 
 | } | } | 
| if (tracewin.fh != FILEH_INVALID) { | if (tracewin.tf != NULL) { | 
| trfh_add(buf); | textfile_write(tracewin.tf, buf); | 
| trfh_add(crlf); | textfile_write(tracewin.tf, crlf); | 
 | } | } | 
 | } | } | 
 | } | } | 
| Line 431  void trace_fmt2(const char *fmt, ...) { | Line 395  void trace_fmt2(const char *fmt, ...) { | 
 |  |  | 
 | BOOL    en; | BOOL    en; | 
 | va_list ap; | va_list ap; | 
| char    buf[0x1000]; | OEMCHAR buf[0x1000]; | 
 |  |  | 
 | en = (tracewin.en & 2) && | en = (tracewin.en & 2) && | 
| ((tracewin.en & 4) || (tracewin.fh != FILEH_INVALID)); | ((tracewin.en & 4) || (tracewin.tf != NULL)); | 
 | if (en) { | if (en) { | 
 | va_start(ap, fmt); | va_start(ap, fmt); | 
 |  | #if defined(OSLANG_UCS2) | 
 |  | OEMCHAR cnvfmt[0x800]; | 
 |  | MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, fmt, -1, | 
 |  | cnvfmt, NELEMENTS(cnvfmt)); | 
 |  | vswprintf(buf, cnvfmt, ap); | 
 |  | #else | 
 | vsprintf(buf, fmt, ap); | vsprintf(buf, fmt, ap); | 
 |  | #endif | 
 | va_end(ap); | va_end(ap); | 
 | if ((tracewin.en & 4) && (hView)) { | if ((tracewin.en & 4) && (hView)) { | 
 | View_AddString(buf); | View_AddString(buf); | 
 | } | } | 
| if (tracewin.fh != FILEH_INVALID) { | if (tracewin.tf != FILEH_INVALID) { | 
| trfh_add(buf); | textfile_write(tracewin.tf, buf); | 
| trfh_add(crlf); | textfile_write(tracewin.tf, crlf); | 
 | } | } | 
 | } | } | 
 | } | } | 
 |  |  | 
 |  | void trace_char(char c) { | 
 |  |  | 
 |  | if ((c == 0x0a) || (c == 0x0d)) { | 
 |  | if (devpos) { | 
 |  | devstr[devpos] = '\0'; | 
 |  | #if defined(OSLANG_UCS2) | 
 |  | TCHAR pdevstr[0x800]; | 
 |  | MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, devstr, -1, | 
 |  | pdevstr, NELEMENTS(pdevstr)); | 
 |  | #else | 
 |  | const OEMCHAR *pdevstr = devstr; | 
 |  | #endif | 
 |  | if ((tracewin.en & 4) && (hView)) { | 
 |  | View_AddString(pdevstr); | 
 |  | } | 
 |  | if (tracewin.tf != NULL) { | 
 |  | textfile_write(tracewin.tf, pdevstr); | 
 |  | textfile_write(tracewin.tf, crlf); | 
 |  | } | 
 |  | devpos = 0; | 
 |  | } | 
 |  | } | 
 |  | else { | 
 |  | if (devpos < (sizeof(devstr) - 1)) { | 
 |  | devstr[devpos++] = c; | 
 |  | } | 
 |  | } | 
 |  | } | 
 | #endif | #endif | 
 |  |  |