home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PMDEBUG.ZIP / PMDEBUG.C < prev    next >
C/C++ Source or Header  |  1990-03-05  |  23KB  |  1,004 lines

  1. /*
  2.  *    dumb TTY in a window
  3.  */
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #include <stdarg.h>
  7. #include <stdlib.h>
  8. #include <malloc.h>
  9. #include <string.h>
  10. #include <time.h>
  11.  
  12. typedef void far *HANDLE;
  13.  
  14. #define INCL_DOS
  15. #define INCL_WIN
  16. #define INCL_GPI
  17. #include "os2.h"
  18.  
  19. static UCHAR debug = 0, sync=0, minimized = 0;
  20.  
  21. static HDC       hdm ;
  22. static HPS       hpm ;
  23.  
  24. static SEL sel=0;
  25. static HAB hab;
  26. static HMQ hmq;
  27. static UCHAR fonttype;
  28.  
  29. static HPS hps=NULL;
  30. static HWND hwndframe=NULL, hwndclient, hwndclienthost;
  31. static UCHAR busy, flush=0;
  32. static char title[60]= "Debug Window";
  33.  
  34. #define DEBUG_PUTSEVENT     0
  35. #define DEBUG_LOGEVENT      1
  36.  
  37. static HQUEUE hq;
  38. static QUEUERESULT qresc;
  39. static char queuename[] = "\\QUEUES\\PMDEBUG";
  40.  
  41. #define FKEY     0x9F
  42. #define CR       13
  43. #define LF       10
  44. #define BS        8
  45. #define ESC      27
  46. #define CURCOLOR -1
  47.  
  48. FILE *fp=NULL;
  49. char LOG[] = "LO";
  50. char filename[80];
  51.  
  52. static USHORT ixmax, iymax, xscreen, yscreen, cxchar, cychar, cydesc;
  53. static int arg_c; char **arg_v;
  54.  
  55. static char *screenbuf=NULL;
  56. static USHORT line=0, ncols, nlines, col=0, nlineswin, ncolswin;
  57.  
  58. static FONTMETRICS fm[10];
  59. static FATTRS vfat;
  60. static char fname[8];
  61.  
  62. static int ezfloadsmallfont(HPS hps, int type)
  63. {
  64.    long nfonts=5L;
  65.    POINTL ptl;
  66.    RECTL rcl;
  67.    int i;
  68.    
  69.    ptl.x = 10;
  70.    ptl.y = 10;
  71.    if (!GpiLoadFonts( hab,"seefonts"))
  72.       return 0;
  73.    
  74.    GpiQueryFonts ( hps, (ULONG) QF_PRIVATE,
  75.       NULL, &nfonts, (LONG) sizeof(FONTMETRICS), fm);
  76.  
  77.    i = type;
  78.    vfat.usRecordLength  =  sizeof(FATTRS) ;
  79.    vfat.lMatch          =  fm[i].lMatch ;
  80.    strcpy(vfat.szFacename, fm[i].szFacename) ;
  81.    vfat.idRegistry      =  fm[i].idRegistry ;
  82.    vfat.usCodePage      =  fm[i].usCodePage ;
  83.    vfat.fsType          =  FATTR_TYPE_FIXED;
  84.  
  85.    if (GpiCreateLogFont( hps, (PSTR8) fname, (LONG)i+1, &vfat ) != 2)
  86.       return 0;
  87.    GpiSetCharSet( hps,(LONG)i+1);
  88.    return 1;
  89. }
  90.  
  91. int error_message(USHORT flag, char *name, char *str,...)
  92. {
  93.    int i;
  94.    USHORT flag1=MB_OK;
  95.    char *p;
  96.    char _buffer[288];
  97.    va_list array;
  98.  
  99.    va_start(array,str);
  100.    vsprintf(_buffer,str,array);
  101.  
  102.    switch (flag & 7)
  103.    {
  104.       case 1:
  105.       flag1 |= MB_ICONHAND;
  106.       break;
  107.  
  108.       case 2:
  109.       flag1 |= MB_ICONQUESTION;
  110.       break;
  111.  
  112.       case 3:
  113.       flag1 |= MB_ICONEXCLAMATION;
  114.       break;
  115.    }
  116.  
  117.    i = WinMessageBox (HWND_DESKTOP, hwndframe ? hwndframe : HWND_DESKTOP,
  118.       _buffer,name,0,flag1);
  119.    if (i == MBID_YES)
  120.       return 0;
  121.    return i;
  122. }
  123.  
  124. #define RESTYPE 300
  125. #define MAXICONS 40
  126. #define ncolors 16
  127. #define CX      32
  128. #define CY      32
  129.  
  130. typedef struct
  131. {
  132.    BITMAPINFOHEADER bh;
  133.    RGB rgb[16];
  134.    char data[CX*CY*4/8];
  135. } COLORICON;
  136. typedef COLORICON far *PCOLORICON;
  137.  
  138. typedef struct
  139. {
  140.    USHORT id;
  141.    PCOLORICON cic;
  142. } ICONTABLE ;
  143. static ICONTABLE icontable[MAXICONS];
  144. static USHORT nicons=0;
  145.  
  146. static RECTL r = {0,0,CX,CY };
  147.  
  148. HANDLE pascal far loadicon(USHORT icon_id)
  149. {
  150.    SEL sel;
  151.    HANDLE p;
  152.    int i;
  153.  
  154.    if (nicons == MAXICONS)
  155.       return NULL;
  156.    i = DosGetResource(NULL, RESTYPE, icon_id, &sel);
  157.    if (i)
  158.       return NULL;
  159.    icontable[nicons].id = icon_id;
  160.    p = MAKEP(sel,0);
  161.    icontable[nicons].cic = p;
  162.    nicons++;
  163.    return p;
  164. }
  165.  
  166. int pascal far color_icon(HANDLE hwndf, HANDLE hwnd, int id, int flag)
  167. {
  168.    BITMAPINFO *b;
  169.    HPS hps;
  170.    HBITMAP h;
  171.    POINTL ptl;
  172.    static int xold,yold;
  173.    static PCOLORICON cic;
  174.    int i;
  175.    SWP swp;
  176.  
  177.    WinQueryWindowPos(hwndf,&swp);     /* not minimized */
  178.    if (!(swp.fs & SWP_MINIMIZE))
  179.       return 0;
  180.  
  181.    for (i=0; i<nicons; i++)          /* search id in table */
  182.    {
  183.       if (icontable[i].id == id)
  184.          break;
  185.    }
  186.    if (i == nicons) return FALSE;    /* not found */
  187.  
  188.    cic = icontable[i].cic;
  189.    ptl.x = 0;
  190.    ptl.y = 0;
  191.    hps = WinBeginPaint (hwnd,NULL, NULL);
  192.    h = GpiCreateBitmap(hps, &cic->bh, CBM_INIT, 
  193.       cic->data, (PBITMAPINFO)&cic->bh);
  194.    if (h)
  195.    {
  196.       WinDrawBitmap(hps,h,&r,&ptl,0L,0L,DBM_IMAGEATTRS);
  197.       GpiDeleteBitmap(h);
  198.    }
  199.    WinEndPaint(hps);
  200.    if (!h)
  201.       return FALSE;
  202.    return TRUE;
  203. }
  204.  
  205. static int menuchange(HANDLE hwnd, USHORT menuitem, USHORT flag, char *ptr,...)
  206. {
  207.    HWND h;
  208.    va_list array;
  209.    char buf[200],*p;
  210.    USHORT mode,flag1;
  211.  
  212.    if (!hwnd) hwnd = hwndframe;
  213.    flag1 = flag & 0xFF;
  214.    h = WinWindowFromID(hwnd, FID_MENU);
  215.    if (h)
  216.    {
  217.       p = ptr;
  218.       if (ptr && strchr(ptr,'%'))
  219.       {
  220.          va_start(array,ptr);
  221.          vsprintf(buf,ptr,array);
  222.          p = buf;
  223.       }
  224.       switch (flag1)
  225.       {
  226.          case 0:
  227.          case 1:
  228.             switch (flag >> 8)
  229.             {
  230.                case 0:
  231.                   mode = MIA_DISABLED;
  232.                   break;
  233.                case 1:
  234.                   mode = MIA_CHECKED;
  235.                   flag ^= 1;        /* menu checked flag is reversed in OS/2 */
  236.                   break;
  237.             }
  238.             WinSendMsg(h, MM_SETITEMATTR, MPFROM2SHORT(menuitem, TRUE),
  239.                                     MPFROM2SHORT(mode, (flag & 1) ? 0 : mode));
  240.             break;
  241.          case 2:
  242.             WinSendMsg(h, MM_SETITEMTEXT, MPFROMSHORT(menuitem), MPFROMP(p));
  243.             break;
  244.       }
  245.    }
  246. }
  247.                              /* should be called from WM_CHAR message */
  248. static int charfrommsg(MPARAM mp1, MPARAM mp2)
  249. {
  250.    int i,ctrl=0,vk; USHORT ch;
  251.    int state = 0;
  252.  
  253.    ch = SHORT1FROMMP(mp2);       /* char code */
  254.    vk = SHORT2FROMMP(mp2);       /* virtual key code */
  255.  
  256.    i = SHORT1FROMMP(mp1);
  257.    if (i & KC_KEYUP)
  258.       return 0 ;
  259.  
  260.    if (i & KC_INVALIDCHAR)
  261.       return 0 ;
  262.  
  263.    if (i & KC_SHIFT)
  264.       state |= 0x300;
  265.    if (i & KC_CTRL)
  266.       state |= 0x400;
  267.    if (i & KC_ALT)
  268.       state |= 0x800;
  269.    if (!vk)
  270.    {
  271.       ctrl = (i & KC_CTRL);
  272.    }
  273.    else
  274.    {
  275.       switch (vk)
  276.       {
  277.          case VK_SHIFT:
  278.          case VK_CTRL:
  279.          case VK_ALT:
  280.             return 0;
  281.          case VK_BREAK:           /* abortion key Ctrl-Break */
  282.             return 0xFF;
  283.          case VK_BACKSPACE:
  284.             return 8 | state;
  285.          case VK_ENTER:
  286.          case VK_NEWLINE:
  287.             return 13 | state;
  288.          case VK_ESC:
  289.             return 27 | state;
  290.          case VK_SPACE:
  291.             return ' ' | state;
  292.          case VK_TAB:
  293.             return 9   | state;
  294.          default:
  295.             return vk | 0x80 | state;
  296.       }
  297.    }
  298.  
  299.    i = CHAR1FROMMP(mp2);         /* ascii code */
  300.    if (i && ctrl)
  301.    {
  302.       return (i & 0x1F) | state;           /* control key */
  303.    }
  304.  
  305.    return i; /* | state; */
  306. }
  307.  
  308. int tasklist(char *str,...)
  309. {
  310.    char buffer[288];
  311.    va_list array;
  312.    static SWCNTRL swctl = { 0 , 0 , 0 , 0 , 0 , SWL_VISIBLE ,
  313.                              SWL_JUMPABLE , NULL , 0 } ;
  314.    PID pid;
  315.    TID tid;
  316.    static HSWITCH hsw = NULL;
  317.  
  318.    if (!hwndframe) return;
  319.    va_start(array,str);
  320.    vsprintf(buffer,str,array);
  321.    buffer[MAXNAMEL] = 0;      /* truncate to max length */
  322.    strcpy(swctl.szSwtitle,buffer);
  323.  
  324.     /* add ourselves to the switch list */
  325.    WinQueryWindowProcess ( hwndframe , & pid , & tid ) ;
  326.    swctl.hwnd = hwndframe ;
  327.    swctl.idProcess = pid ;
  328.    if (!hsw)
  329.       hsw = WinAddSwitchEntry (&swctl) ;
  330.    else
  331.       WinChangeSwitchEntry(hsw, &swctl);
  332.    WinSetWindowText(hwndframe, buffer);
  333.    return strlen(buffer);
  334. }
  335.  
  336. wrtext(int l, int c, char *txt, USHORT color, USHORT nbytes)
  337. {
  338.    POINTL ptl;
  339.    RECTL r;
  340.  
  341.    if (!hps || minimized)
  342.       return ;
  343.    if (!nbytes)
  344.       nbytes = strlen(txt);
  345.    l = nlines-1-l;
  346.    ptl.x = c*cxchar;
  347.    ptl.y = cydesc+l*cychar;
  348.    r.xLeft = ptl.x;
  349.    r.yTop  = ptl.y + cychar -1 - cydesc;
  350.    r.xRight = ptl.x + nbytes*cxchar -1;
  351.    r.yBottom =   ptl.y - cydesc;
  352.    if (color != -1)
  353.       GpiSetColor(hps, (ULONG)color);
  354.    GpiCharStringPosAt (hps, &ptl, &r, CHS_OPAQUE, (LONG)nbytes ,txt, NULL);
  355. }
  356. MRESULT EXPENTRY wndproc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2);
  357.  
  358. setwindow()
  359. {
  360.    ULONG ll = SWP_SHOW;
  361.  
  362.    if (argoption('i'))
  363.       ll |= SWP_MINIMIZE;
  364.  
  365.    WinSetWindowPos(hwndframe, NULL, 0, 0, 0, 0, ll);
  366. }
  367.  
  368. winitall()
  369. {
  370.    static ULONG fl = FCF_TITLEBAR      | FCF_SYSMENU | FCF_ACCELTABLE |
  371.                      FCF_SIZEBORDER    | FCF_MINMAX  | 
  372.                      FCF_SHELLPOSITION | FCF_MENU;
  373.    ULONG ll;
  374.    QMSG         q ;
  375.    int restore();
  376.    
  377.    hab = WinInitialize(0);
  378.    hmq = WinCreateMsgQueue(hab,0);
  379.    ll = CS_SIZEREDRAW;
  380.    if (sync = argoption('s'))
  381.       ll |= CS_SYNCPAINT;
  382.    WinRegisterClass (
  383.                   hab,                /* Anchor block handle */
  384.                   title,              /* Name of class being registered */
  385.                   wndproc,            /* Window procedure for class */
  386.                   ll,                 /* Class style */
  387.                   0) ;                /* Extra bytes to reserve */
  388.  
  389.    hwndframe = WinCreateStdWindow (
  390.                   HWND_DESKTOP,       /* Parent window handle */
  391.                   0L,
  392.                                       /* Style of frame window */
  393.                   &fl,                /* Pointer to control data */
  394.                   title,              /* Client window class name */
  395.                   title,              /* Title bar text */
  396.                   0L,                 /* Style of client window */
  397.                   NULL,               /* Module handle for resources */
  398.                   0x234,              /* ID of resources */
  399.                   &hwndclient) ;      /* Pointer to client window handle */
  400.  
  401.  
  402.    if (hwndframe)
  403.    {
  404.       onexit(restore);
  405.       while (WinGetMsg(hab, &q, NULL, 0,0))
  406.          WinDispatchMsg(hab, &q);
  407.  
  408.       WinDestroyWindow (hwndframe) ;
  409.    }
  410.    WinDestroyMsgQueue(hmq);
  411.    WinTerminate(hab);
  412. }
  413.  
  414.  
  415. static int scrollup()
  416. {
  417.    RECTL r;
  418.    r.xLeft = 0;
  419.    r.xRight = ixmax;
  420.    r.yBottom = 0;
  421.    r.yTop = cychar;
  422.    if (!minimized)
  423.       WinScrollWindow(hwndclient, 0, cychar, NULL, NULL, NULL, NULL,
  424.          SW_INVALIDATERGN);
  425. }
  426.  
  427. void far getinput()
  428. {
  429.    int i;
  430.    UCHAR ch;
  431.    while (1)
  432.    {
  433.       if ((i = fgetc(stdin)) != EOF)
  434.       {
  435.          ch = i;
  436.          _debug_putchr(ch);
  437.       }
  438.    }
  439. }
  440.  
  441. static int redraw()
  442. {
  443.    int i,k=nlines-nlineswin;
  444.  
  445.    busy=1;
  446.    i = (line+1+k)%nlines;
  447.  
  448.    while (k < nlines)
  449.    {
  450.       if (i == nlines)
  451.          i = 0;
  452.       wrtext(k,0,&screenbuf[i*(ncols+1)], CURCOLOR, ncolswin);
  453.       i++;
  454.       k++;
  455.    }
  456.    busy=0;
  457.    return 0 ;
  458. }
  459. #define const
  460.  
  461. static int _debug_puts(char far *txt)
  462. {
  463.    int i,n; char far *q;
  464.  
  465.    if (!hwndframe) return ;
  466.    q = txt;
  467.    while (*q)
  468.    {
  469.       _debug_putchr(*q);
  470.       q++;
  471.    }
  472.    if (fp && flush)
  473.       fflush(fp);
  474. }
  475.  
  476. static int _debug_putchr(char ch)
  477. {
  478.    int i,j;
  479.  
  480.    if (!hwndframe || !hps) return;
  481.    if (!screenbuf) return ;
  482. /*    while (busy && sync) ; */
  483.    if (fp) fputc(ch, fp);
  484.  
  485.    switch (ch)
  486.    {
  487.       case CR:
  488.          col = 0;
  489.          break;
  490.       case LF:
  491.          line++;
  492.          if (line == nlines)
  493.             line = 0;
  494.          col=0;
  495.          memset(&screenbuf[line*(ncols+1)],' ', ncols);
  496.          screenbuf[line*(ncols+1)+ncols] = 0;
  497.  
  498.          scrollup();
  499.          break;
  500.       case BS:
  501.          if (col)
  502.          {
  503.             col--;
  504.             i = ' ';
  505.             wrtext(nlines-1,col,(char*)&i,CURCOLOR,1);
  506.             screenbuf[line*(ncols+1)+col] = ' ';
  507.          }
  508.          break;
  509.       default:
  510.          if (col < ncols)
  511.          {
  512.             wrtext(nlines-1,col,&ch,CURCOLOR,1);
  513.             screenbuf[line*(ncols+1)+col] = ch;
  514.             col++;
  515.          }
  516.          break;
  517.    }
  518. }         
  519.  
  520. void setup_screenbuf()
  521. {
  522.    int i;
  523.    FONTMETRICS fm ;
  524.  
  525.    if (screenbuf) free(screenbuf);
  526.    ezfloadsmallfont(hps, fonttype);
  527.    GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
  528.    cxchar = (int) fm.lAveCharWidth ;
  529.    cychar = (int) fm.lMaxBaselineExt ;
  530.    cydesc = (int) fm.lMaxDescender ;
  531.  
  532.    ncols = xscreen/cxchar;
  533.    nlines = yscreen/cychar;
  534.    screenbuf = malloc((ncols+1)*nlines);
  535.  
  536.    for (i=0; i<nlines; i++)            /* init with blanks */
  537.    {
  538.       memset(&screenbuf[i*(ncols+1)],' ', ncols);
  539.       screenbuf[i*(ncols+1)+ncols] = 0;
  540.    }
  541.    col = 0;
  542.    line = 0;
  543. }
  544.  
  545. void far readqueue()
  546. {
  547.    USHORT i,len,pri;
  548.    char far *p;
  549.  
  550.    HAB hab = WinInitialize(0);
  551.  
  552.    while (hq)
  553.    {
  554.       int counter;
  555.  
  556.       DosReadQueue(hq, &qresc, &len, &p, 0, DCWW_WAIT, (UCHAR*)&pri, NULL);
  557.  
  558.       switch (qresc.usEventCode)
  559.       {
  560.          case DEBUG_PUTSEVENT:
  561.  
  562.          for (i=0; i<len; i++)
  563.          {
  564.             _debug_putchr(p[i]);
  565.          }
  566.          break;
  567.   
  568.          case DEBUG_LOGEVENT:
  569.          _debug_logmessage((PQMSG)p);
  570.          break;
  571.       }
  572.       DosFreeSeg(SELECTOROF(p));
  573.    }
  574. }
  575.  
  576. static int plinit()
  577. {
  578.    HDC hdc;
  579.    ULONG x,y; int i;
  580.    SIZEL hello;
  581.    char *stk;
  582.  
  583.    hdc = WinOpenWindowDC(hwndclient);
  584.  
  585.    DevQueryCaps(hdc,CAPS_WIDTH,  1L, &x);
  586.    DevQueryCaps(hdc,CAPS_HEIGHT, 1L, &y);
  587.    xscreen = x; 
  588.    yscreen = y;
  589.    hello.cx = 0;
  590.    hello.cy = 0;
  591.  
  592.    hps = GpiCreatePS(hab,hdc,&hello,PU_PELS | GPIF_DEFAULT | GPIT_NORMAL |
  593.          GPIA_ASSOC);
  594.  
  595.    setup_screenbuf();
  596. #define STKSIZE 4096
  597.  
  598.    stk = malloc(STKSIZE);
  599.  
  600.    if (argoption('z'))       /* enable input from stdin */
  601.    {
  602.       if (stk)
  603.      /*     _beginthread(getinput, stk, STKSIZE, NULL); */
  604.          DosCreateThread(getinput, (PTID)&i, stk+STKSIZE);
  605.    }
  606.    else
  607.    {
  608.  
  609.       if (!DosOpenQueue((USHORT*)&i, &hq, queuename))
  610.       {
  611.          DosCloseQueue(hq);
  612.          error_message(3, "Duplicate", "Debug window already present!");
  613.          exit(1);
  614.       }
  615.       if (DosCreateQueue(&hq, QUE_FIFO, queuename))
  616.          return 1;
  617.       DosCreateThread(readqueue, (PTID)&i, stk+STKSIZE);
  618.    }
  619.    return 0;
  620. }
  621.  
  622. resize()
  623. {
  624.    nlineswin = (iymax+cychar-1)/cychar;
  625.    ncolswin  = (ixmax+cxchar-1)/cxchar;
  626. }
  627.  
  628. help()
  629. {
  630.    error_message(2,"HELP",
  631.    "PMDEBUG ver. 1\n"
  632.    "(C) 1990 C Freak Software Inc.\n\n"
  633.    "Usage: PMDEBUG [-s] [-1] [-fname] [-i]\n"
  634.    "-s: synchronous redraw\n"
  635.    "-1: small 6x8 font\n"
  636.    "-f<filename> named log file\n"
  637.    "   -f as last parameter: no log\n"
  638.    "-i: start under icon\n"
  639. /*    "-z: input rather than debug window\n" */
  640. /*    "    e.g. PMDEBUG -i < file\n" */
  641. /*    "name: title bar\n" */
  642.    );
  643. }
  644.  
  645. MRESULT EXPENTRY wndproc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  646. {
  647.    int i;
  648.    char _buf[400];
  649.  
  650.    switch (msg)
  651.    {
  652.       case WM_CREATE:
  653.          hwndclient = hwnd;
  654.          hwndframe = WinQueryWindow(hwnd,QW_PARENT,0);
  655.          initall();
  656.          if (plinit()) return 1L;
  657.          setwindow();
  658.          loadicon(0x234);
  659.          break;
  660.  
  661.       case WM_SETFOCUS:
  662.          return 0L;
  663.  
  664.       case WM_SIZE:
  665.          ixmax = SHORT1FROMMP(mp2);
  666.          iymax = SHORT2FROMMP(mp2);
  667.          resize();      
  668.          break;
  669.  
  670.       case WM_PAINT:
  671.          if (color_icon(hwndframe, hwndclient, 0x234, 0))
  672.          {
  673.             minimized = 1;
  674.             return 1L;
  675.          }
  676.          minimized = 0;
  677.          WinBeginPaint(hwnd, hps, NULL);
  678.          redraw();
  679.          WinEndPaint(hps);
  680.          break;
  681.  
  682.       case WM_COMMAND:
  683.          i = SHORT1FROMMP(mp1);
  684.          goto chr;
  685.  
  686.       case WM_CHAR:
  687.          i = charfrommsg(mp1,mp2);
  688.          chr:
  689.          keyevent(i);
  690.          return 0;
  691.  
  692.       case WM_DESTROY:
  693.          hwndframe = NULL;
  694.          GpiAssociate(hps,NULL);
  695.          GpiDestroyPS(hps);
  696.          if (sel)
  697.             DosFreeSeg(sel);
  698.          break;
  699.  
  700.    }
  701.    return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  702. }
  703.  
  704. static char buf[80];
  705. typedef struct {
  706.    char *name;
  707.    USHORT msg;
  708. } MSGS;
  709.  
  710. static MSGS msgs[] = 
  711. {
  712.  "WM_NULL                ", 0x0000,
  713.  "WM_CREATE              ", 0x0001,
  714.  "WM_DESTROY             ", 0x0002,
  715.  "WM_OTHERWINDOWDESTROYED", 0x0003,
  716.  "WM_ENABLE              ", 0x0004,
  717.  "WM_SHOW                ", 0x0005,
  718.  "WM_MOVE                ", 0x0006,
  719.  "WM_SIZE                ", 0x0007,
  720.  "WM_ADJUSTWINDOWPOS     ", 0x0008,
  721.  "WM_CALCVALIDRECTS      ", 0x0009,
  722.  "WM_SETWINDOWPARAMS     ", 0x000a,
  723.  "WM_QUERYWINDOWPARAMS   ", 0x000b,
  724.  "WM_HITTEST             ", 0x000c,
  725.  "WM_ACTIVATE            ", 0x000d,
  726.  "WM_SETFOCUS            ", 0x000f,
  727.  "WM_SETSELECTION        ", 0x0010,
  728.  "WM_COMMAND             ", 0x0020,
  729.  "WM_SYSCOMMAND          ", 0x0021,
  730.  "WM_HELP                ", 0x0022,
  731.  "WM_PAINT               ", 0x0023,
  732.  "WM_TIMER               ", 0x0024,
  733.  "WM_SEM1                ", 0x0025,
  734.  "WM_SEM2                ", 0x0026,
  735.  "WM_SEM3                ", 0x0027,
  736.  "WM_SEM4                ", 0x0028,
  737.  "WM_CLOSE               ", 0x0029,
  738.  "WM_QUIT                ", 0x002a,
  739.  "WM_SYSCOLORCHANGE      ", 0x002b,
  740.  "WM_SYSVALUECHANGED     ", 0x002d,
  741.  "WM_CONTROL             ", 0x0030,
  742.  "WM_VSCROLL             ", 0x0031,
  743.  "WM_HSCROLL             ", 0x0032,
  744.  "WM_INITMENU            ", 0x0033,
  745.  "WM_MENUSELECT          ", 0x0034,
  746.  "WM_MENUEND             ", 0x0035,
  747.  "WM_DRAWITEM            ", 0x0036,
  748.  "WM_MEASUREITEM         ", 0x0037,
  749.  "WM_CONTROLPOINTER      ", 0x0038,
  750.  "WM_CONTROLHEAP         ", 0x0039,
  751.  "WM_QUERYDLGCODE        ", 0x003a,
  752.  "WM_INITDLG             ", 0x003b,
  753.  "WM_SUBSTITUTESTRING    ", 0x003c,
  754.  "WM_MATCHMNEMONIC       ", 0x003d,
  755.  "WM_USER                ", 0x1000,
  756.  "WM_MOUSEFIRST          ", 0x0070,
  757.  "WM_MOUSELAST           ", 0x0079,
  758.  "WM_BUTTONCLICKFIRST    ", 0x0071,
  759.  "WM_BUTTONCLICKLAST     ", 0x0079,
  760.  "WM_MOUSEMOVE           ", 0x0070,
  761.  "WM_BUTTON1DOWN         ", 0x0071,
  762.  "WM_BUTTON1UP           ", 0x0072,
  763.  "WM_BUTTON1DBLCLK       ", 0x0073,
  764.  "WM_BUTTON2DOWN         ", 0x0074,
  765.  "WM_BUTTON2UP           ", 0x0075,
  766.  "WM_BUTTON2DBLCLK       ", 0x0076,
  767.  "WM_BUTTON3DOWN         ", 0x0077,
  768.  "WM_BUTTON3UP           ", 0x0078,
  769.  "WM_BUTTON3DBLCLK       ", 0x0079,
  770.  "WM_CHAR                ", 0x007a,
  771.  "WM_VIOCHAR             ", 0x007b,
  772.  "WM_FLASHWINDOW         ", 0x0040,
  773.  "WM_FORMATFRAME         ", 0x0041,
  774.  "WM_UPDATEFRAME         ", 0x0042,
  775.  "WM_FOCUSCHANGE         ", 0x0043,
  776.  "WM_SETBORDERSIZE       ", 0x0044,
  777.  "WM_TRACKFRAME          ", 0x0045,
  778.  "WM_MINMAXFRAME         ", 0x0046,
  779.  "WM_SETICON             ", 0x0047,
  780.  "WM_QUERYICON           ", 0x0048,
  781.  "WM_SETACCELTABLE       ", 0x0049,
  782.  "WM_QUERYACCELTABLE     ", 0x004a,
  783.  "WM_TRANSLATEACCEL      ", 0x004b,
  784.  "WM_QUERYTRACKINFO      ", 0x004c,
  785.  "WM_QUERYBORDERSIZE     ", 0x004d,
  786.  "WM_NEXTMENU            ", 0x004e,
  787.  "WM_ERASEBACKGROUND     ", 0x004f,
  788.  "WM_QUERYFRAMEINFO      ", 0x0050,
  789.  "WM_QUERYFOCUSCHAIN     ", 0x0051,
  790.  "WM_QUERYFRAMECTLCOUNT  ", 0x0059,
  791.  "WM_RENDERFMT           ", 0x0060,
  792.  "WM_RENDERALLFMTS       ", 0x0061,
  793.  "WM_DESTROYCLIPBOARD    ", 0x0062,
  794.  "WM_PAINTCLIPBOARD      ", 0x0063,
  795.  "WM_SIZECLIPBOARD       ", 0x0064,
  796.  "WM_HSCROLLCLIPBOARD    ", 0x0065,
  797.  "WM_VSCROLLCLIPBOARD    ", 0x0066,
  798.  "WM_DRAWCLIPBOARD       ", 0x0067,
  799.  "WM_DDE_FIRST           ", 0x00A0,
  800.  "WM_DDE_INITIATE        ", 0x00A0,
  801.  "WM_DDE_REQUEST         ", 0x00A1,
  802.  "WM_DDE_ACK             ", 0x00A2,
  803.  "WM_DDE_DATA            ", 0x00A3,
  804.  "WM_DDE_ADVISE          ", 0x00A4,
  805.  "WM_DDE_UNADVISE        ", 0x00A5,
  806.  "WM_DDE_POKE            ", 0x00A6,
  807.  "WM_DDE_EXECUTE         ", 0x00A7,
  808.  "WM_DDE_TERMINATE       ", 0x00A8,
  809.  "WM_DDE_INITIATEACK     ", 0x00A9,
  810.  "WM_DDE_LAST            ", 0x00AF,
  811.  "WM_QUERYCONVERTPOS     ", 0x00b0,
  812. } ;
  813.  
  814. static int _debug_logmessage(QMSG far *q)
  815. {
  816.    int i;
  817.    char *p = buf;
  818.    char tmpbuf[200];
  819.  
  820.    sprintf(buf, "%04Xh", q->msg);
  821.    for (i = 0; i<sizeof(msgs)/sizeof(*msgs); i++)
  822.    {
  823.       if (q->msg == msgs[i].msg)
  824.       {
  825.          p = msgs[i].name;
  826.          break;
  827.       }
  828.    }
  829.    sprintf(tmpbuf,"hwnd=%08lXh,msg=%-23s,mp1=%08lXh,mp2=%08lXh\n",
  830.       q->hwnd,
  831.       p,
  832.       q->mp1,
  833.       q->mp2);
  834.    _debug_puts(tmpbuf);
  835. /*    fputs(tmpbuf,stdout); */
  836. }
  837.  
  838. int argoption(char letter)
  839. {
  840.    char c,d;
  841.    int j;
  842.    for (j=1; j<arg_c; j++)
  843.    {
  844.       c = arg_v[j][0]; d = toupper(arg_v[j][1]);
  845.       if (c == '/' || c == '-')
  846.       {
  847.          if (d == toupper(letter))
  848.             return(j);
  849.       }
  850.    }
  851.    return(0);
  852. }
  853.  
  854. restore()
  855. {
  856.    if (fp) 
  857.    {
  858.       fclose(fp); fp = NULL;
  859.    }
  860.    if (hq)
  861.    {
  862.       DosCloseQueue(hq); hq = NULL;
  863.    }
  864.    if (hwndframe)
  865.       WinDestroyWindow(hwndframe);
  866. }
  867.  
  868. open_log(char *filename)
  869. {
  870.    int l,i;
  871.    char *p;
  872.  
  873.  
  874.    i = 0;
  875.    while (1)
  876.    {
  877.       fp = fopen(filename,"a");
  878.       if (!fp)
  879.       {
  880.          if (i == 0)
  881.             error_message(1,"ERROR", "File %s cannot be opened\n"
  882.                   "Default (%s) is chosen.", filename,LOG);
  883.          else
  884.          {
  885.             error_message(1,"ERROR", "File %s cannot be opened\n"
  886.                   "Disk full or other error.", filename);
  887.             exit(1);
  888.          }
  889.       }
  890.       else
  891.          break;
  892.       strcpy(filename,LOG);
  893.       i++;
  894.    }
  895. }
  896.  
  897. close_log()
  898. {
  899.    fclose(fp);
  900.    fp = NULL;
  901. }
  902.  
  903. logmenuonoff()
  904. {
  905.    menuchange(NULL, 'L', fp    ? 0x101 : 0x100, NULL);
  906.    menuchange(NULL, 'F', flush ? 0x101 : 0x100, NULL);
  907.    if (fp)
  908.       tasklist("%s : %s",title,filename);
  909.    else
  910.       tasklist(title);
  911. }
  912.  
  913. initall()
  914. {
  915.    if (*filename)
  916.       open_log(filename);
  917.    logmenuonoff();
  918. }
  919.  
  920. keyevent(int i)
  921. {
  922.    switch (tolower(i&0xFF))
  923.    {
  924.       case '?':
  925.       case FKEY+1:
  926.          help();
  927.          break;
  928.  
  929.       case 0xFF:
  930.       case 3:
  931.       case FKEY+3:
  932.          exit(0);
  933.    /*    case 'l': */
  934.    /*       fonttype = 1; */
  935.    /*       goto small; */
  936.    /*    case 's': */
  937.    /*       fonttype = 0; */
  938.    /*    small: */
  939.       case 'c':
  940.          setup_screenbuf();
  941.          resize();
  942.          WinInvalidateRect(hwndclient, NULL, 0);
  943.          break;
  944.  
  945.       case 'd':
  946.          DosEnterCritSec();
  947.          if (fp)
  948.             close_log();
  949.          remove(filename);
  950.          DosExitCritSec();
  951.          logmenuonoff();
  952.          break;
  953.  
  954.       case 'l':
  955.          DosEnterCritSec();
  956.          if (fp)
  957.             close_log();
  958.          else
  959.             open_log(filename);
  960.          DosExitCritSec();
  961.          logmenuonoff();
  962.          break;
  963.  
  964.       case 'f':
  965.          DosEnterCritSec();
  966.          flush ^= 1;
  967.          if (flush && fp)
  968.             fflush(fp);
  969.          DosExitCritSec();
  970.          logmenuonoff();
  971.          break;
  972.  
  973. #if 0
  974.       case FKEY+2:
  975.       if (argoption('d'))
  976.          debug_logmessage( hwnd, msg, mp1, mp2, "main loop:");
  977. #endif
  978.       break;
  979.    }
  980.    return 0;
  981. }
  982.  
  983. main(int argc, char **argv)
  984. {
  985.    int i;
  986.  
  987.    arg_c = argc;
  988.    arg_v = argv;
  989.    if (debug = argoption('d'))
  990.       ;
  991.  
  992.    if (i = argoption('f'))
  993.    {
  994.       strcpy(filename, arg_v[i]+2);       /* -ffilename  */
  995.                                           /*    or       */
  996.       if (!*filename && arg_c > i+1 && !strchr("/-",*arg_v[i+1]))
  997.          strcpy(filename, arg_v[i+1]);    /* -f filename */
  998.    }
  999.    else
  1000.       strcpy(filename,LOG);
  1001.    fonttype = argoption('1') ? 0 : 1;
  1002.    winitall();
  1003. }
  1004.