home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / maxsn103.zip / snoop.c < prev    next >
C/C++ Source or Header  |  1995-01-10  |  12KB  |  514 lines

  1.  
  2. /* Simple client-end example of a MaxSnoop app.
  3.  *
  4.  * EMX 0.8h
  5.  */
  6.  
  7. #define ESCAPE 27
  8. #define INCL_NOPM
  9. #define INCL_DOS
  10. #define INCL_VIO
  11. #define INCL_DOSERRORS
  12. #define INCL_DOSPROCESS
  13. #include <os2.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <sys/winmgr.h>
  18. #include <sys/kbdscan.h>
  19. #include <sys/video.h>
  20. #include <ctype.h>
  21.  
  22.  
  23. char BangColour = 127;
  24. char PlusColour = 127;
  25. char ColonColour = 127;
  26. char PoundColour = 127;
  27. char EqualsColour = 127;
  28. char StarColour = 127;
  29. char ** buffer;
  30. wm_handle main_window;
  31. int scr_size[2];
  32. int top = -1;
  33. int offset = 0;
  34. int size = 100;
  35. char normal_key_code = 0;
  36. char extended_code = 0;
  37. char Help_Open = 0;
  38. wm_handle Help;
  39.  
  40.  
  41.  
  42.  
  43. static char * Colours[9] = {
  44.   "BLACK",
  45.   "BLUE",
  46.   "GREEN",
  47.   "CYAN",
  48.   "RED",
  49.   "MAGENTA",
  50.   "YELLOW",
  51.   "BROWN",
  52.   "WHITE"
  53. };
  54.  
  55.  
  56.  
  57.  
  58. int GetColour(char *);
  59. void Colour_Line(char, char);
  60. static HPIPE OpenPipe(char *name);
  61. static USHORT PRead(HPIPE hp, VOID *pv, ULONG cb, USHORT *pbr);
  62. void displaylist(void);
  63. void handle_key_event(char * not_done, HPIPE hp);
  64. void ARGS();
  65. void HELP();
  66. int rc;
  67.  
  68. void main(int argc, char **argv)
  69. {
  70.   int l;
  71.   USHORT bytes;
  72.   char msg[258];
  73.   HPIPE hp = 0;
  74.   VIOMODEINFO ModeData;
  75.   int i;
  76.   char not_done = 1;
  77.   char * lines = "23";
  78.   char * pipename = "";
  79.   ULONG rc;
  80.  
  81.   if (argc < 2)
  82.     {
  83.       ARGS();
  84.       exit(-1);
  85.     }
  86.   for (i=1; i < argc; ++i)
  87.     {
  88.       if (argv[i][0] != '-')
  89.     {
  90.       printf("I don't understand the commandline arg %s\n",argv[i]);
  91.       ARGS();
  92.     }
  93.       else if (argv[i][1] == '-')
  94.     {
  95.       printf("I don't understand the commandline arg %s\n",argv[i]);
  96.       ARGS();
  97.     }
  98.       else
  99.     switch (argv[i][1]){
  100.     case 'p':
  101.       pipename = &argv[i][2];
  102.       break;
  103.     case '+':
  104.       PlusColour = GetColour(&argv[i][2]);
  105.       break;
  106.     case '#':
  107.       PoundColour = GetColour(&argv[i][2]);
  108.       break;
  109.     case '!':
  110.       BangColour = GetColour(&argv[i][2]);
  111.       break;
  112.     case ':':
  113.       ColonColour = GetColour(&argv[i][2]);
  114.       break;
  115.     case '=':
  116.       EqualsColour = GetColour(&argv[i][2]);
  117.       break;
  118.     case '*':
  119.       EqualsColour = GetColour(&argv[i][2]);
  120.       break;
  121.     case 's':
  122.       size = atoi(&argv[i][2]);
  123.       if (size < 100){
  124.         size = 100;
  125.         printf("you must use at least 100 lines of scroll back buffer\n");
  126.         printf("I will set the default to 100 for you.\n");
  127.         sleep(2);
  128.       }
  129.       break;
  130.     case 'l':
  131.       lines = &argv[i][2];
  132.       break;
  133.     default:
  134.       ARGS();
  135.     }
  136.     }
  137.  
  138.   if (!strlen(pipename)){
  139.     ARGS();
  140.     exit(-1);
  141.   }
  142.  
  143.   size = 1000;
  144.  
  145.   buffer =  malloc(size*sizeof(char *));
  146.  
  147.   rc = VioGetMode(&ModeData, 0);
  148.   scr_size[0] = ModeData.col;
  149.   scr_size[1] = ModeData.row;
  150.  
  151.   ModeData.row = atoi(lines);
  152.   ModeData.cb = sizeof(ModeData);
  153.   ModeData.color = 4;
  154.   ModeData.fbType = 1;
  155.   ModeData.hres = 640;
  156.   ModeData.vres = 16*ModeData.row;
  157.   rc = VioSetMode(&ModeData, 0);
  158.  
  159.   rc = VioGetMode(&ModeData, 0);
  160.   /* scr_size[0] = ModeData.col;*/
  161.   ModeData.row = atoi(lines);
  162.   rc = VioSetMode(&ModeData, 0);
  163.   scr_size[0] = 80;
  164.   scr_size[1] = ModeData.row;
  165.   VioScrollDn(0, 0, scr_size[1], scr_size[2], scr_size[1], " ", 0);
  166.  
  167.   for (i = 0; i < size; ++i) buffer[i] = NULL;
  168.  
  169.   rc = wm_init(2);
  170.   main_window = wm_create(0,0,scr_size[0],scr_size[1],0,
  171.               F_WHITE|B_BLACK|INTENSITY,F_WHITE|B_BLACK|INTENSITY);
  172.   wm_open(main_window);
  173.   wm_wrap(main_window, 0);
  174.   while(not_done){
  175.     hp = OpenPipe(pipename);
  176.     while (hp)
  177.       {
  178.     if (!(rc = PRead(hp, msg, sizeof(msg)-2, &bytes))){
  179.       msg[bytes] = 0;
  180.       l = 0;
  181.       while ((31 < msg[l]) && (l < strlen(msg))) ++l;
  182.       msg[l] = 0;
  183.       msg[++l] = 0; 
  184.       ++top;
  185.       if (top == size) top = 0;
  186.       msg[scr_size[0]] = 0;
  187.       buffer[top] = realloc(buffer[top], strlen(msg) + 1);
  188.       strcpy(buffer[top], msg);
  189.  
  190.       if (offset == 0){
  191.         wm_scroll(main_window, 1);
  192.         if (buffer[top] != NULL){
  193.           wm_puts_at(main_window, 0, scr_size[1]-1, buffer[top]);
  194.           Colour_Line(scr_size[1]-1, buffer[top][0]);}}
  195.       else{
  196.         offset = 0;
  197.         displaylist();}
  198.     }
  199.     else if ((rc != ERROR_MORE_DATA) &&
  200.          (rc != ERROR_NO_DATA)){
  201.       rc = DosClose(hp);
  202.       hp = 0;}
  203.     if ((normal_key_code = _read_kbd(0,0,0)) >= 0){
  204.       if (!normal_key_code)
  205.         extended_code = _read_kbd(0,0,0);
  206.       handle_key_event(¬_done, hp);
  207.     }
  208.     else
  209.       _sleep2(25);
  210.       }
  211.   }
  212. }
  213.  
  214.  
  215.  
  216.  
  217. int GetColour(char *p)
  218. {
  219.   int i;
  220.  
  221.   for (i = B_BLACK; i < B_WHITE; i += B_BLUE)
  222.     if (strnicmp(Colours[i/B_BLUE], p, strlen(Colours[i/B_BLUE])) == 0)
  223.       return i;
  224.   return(F_WHITE);
  225. }
  226.  
  227.  
  228.  
  229.  
  230.  
  231. void Colour_Line(char loc, char character)
  232. {
  233.   switch (character){
  234.     
  235.   case '!':
  236.     if (BangColour != 127)
  237.       wm_puta_at(main_window,0,loc,F_WHITE|BangColour|INTENSITY,scr_size[0]);
  238.     break;
  239.   case ':':
  240.     if (ColonColour != 127)
  241.       wm_puta_at(main_window,0,loc,F_WHITE|ColonColour|INTENSITY,scr_size[0]);
  242.     break;
  243.   case '#':
  244.     if (PoundColour != 127)
  245.       wm_puta_at(main_window,0,loc,F_WHITE|PoundColour|INTENSITY,scr_size[0]);
  246.     break;
  247.   case '+':
  248.     if (PlusColour != 127)
  249.       wm_puta_at(main_window,0,loc,F_WHITE|PlusColour|INTENSITY,scr_size[0]);
  250.     break;
  251.   case '=':
  252.     if (EqualsColour != 127)
  253.       wm_puta_at(main_window,0,loc,F_WHITE|EqualsColour|INTENSITY,scr_size[0]);
  254.     break;
  255.   case '*':
  256.     if (StarColour != 127)
  257.       wm_puta_at(main_window,0,loc,F_WHITE|StarColour|INTENSITY,scr_size[0]);
  258.     break;
  259.   }
  260. }
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267. void ARGS()
  268. {
  269.   printf("\n             Text Snoop\n\n");
  270.   printf("From the maker of MaxFile/2.  Code is adapted from work done by\n");
  271.   printf("Peter Fitzsimmons.\n\n");
  272.   printf("This program takes one of many arguments arguments\n");
  273.   printf("-p<pipename> where <pipename> is the name of the snoop pipe\n");
  274.   printf("-s<num>      where <num> is the number of log lines to retain\n");
  275.   printf("-l<num>      where <num> is the number of screen lines\n");
  276.   printf("-!<colour>   where <colour> is the colour for lines begining with !\n");
  277.   printf("-=<colour>   where <colour> is the colour for lines begining with =\n");
  278.   printf("-+<colour>   where <colour> is the colour for lines begining with +\n");
  279.   printf("-#<colour>   where <colour> is the colour for lines begining with #\n");
  280.   printf("-*<colour>   where <colour> is the colour for lines begining with *\n");
  281.   printf("-:<colour>   where <colour> is the colour for lines begining with :\n");
  282.  
  283.   printf("\n <colour> can be any of the following: BLUE, GREEN, CYAN, RED, MAGENTA\n");
  284.   printf("                                       YELLOW, BROWN, or WHITE\n");
  285.  
  286.   printf("\n\nyou must at least specify a pipe to read\n\n");
  287.   exit(-1);
  288. }
  289.  
  290.  
  291.  
  292. void displaylist(void)
  293. {
  294.   int place;
  295.   int i;
  296.   int TOP = top;
  297.  
  298.   wm_clear(main_window);
  299.   for (i = 0; i < scr_size[1]; ++i)
  300.     {
  301.       place = TOP + offset - i;
  302.       if (place < 0) place = place + size;
  303.       if (buffer[place]  != NULL){
  304.     wm_puts_at(main_window, 0, scr_size[1]-1-i, buffer[place]);
  305.     Colour_Line(scr_size[1]-i-1, buffer[place][0]);
  306.       }
  307.     }
  308. }
  309.  
  310.  
  311.  
  312. void handle_key_event(char * not_done, HPIPE hp)
  313. {
  314.   int place;
  315.  
  316.   if (normal_key_code){
  317.     switch (normal_key_code){
  318.     case 'x': case 'X':
  319.       * not_done = 0;
  320.       DosClose(hp);
  321.       wm_close_all();
  322.       exit(0);
  323.       break;
  324.     case ESCAPE:
  325.       if (Help_Open){
  326.     wm_close(Help);
  327.     wm_delete(Help);
  328.     Help_Open = 0;}
  329.       break;
  330.     }
  331.   }
  332.   else if (extended_code){
  333.     switch (extended_code){
  334.     case K_PAGEUP:
  335.       if (offset != scr_size[1] - size){
  336.     offset = offset - scr_size[1] + 1;
  337.     if (-offset > size-scr_size[1]) offset = -size + scr_size[1];
  338.     displaylist();
  339.       }
  340.       break;
  341.     case K_HOME:
  342.       if (offset != scr_size[1] - size){
  343.     offset =  scr_size[1] - size;
  344.     displaylist();
  345.       }
  346.     case K_UP:
  347.       if (offset != scr_size[1] - size){
  348.     --offset;
  349.     place = top + offset - scr_size[1] + 1;
  350.     if (place < 0) place = place + size;
  351.     wm_ins_line(main_window, 0, 1);
  352.     if (buffer[place]  != NULL){
  353.       wm_puts_at(main_window, 0, 0, buffer[place]);
  354.       Colour_Line(0, buffer[place][0]);
  355.     }
  356.       }
  357.       break;
  358.     case K_PAGEDOWN:
  359.       if (offset){
  360.     offset = offset + scr_size[1] - 1;
  361.     if (offset > 0) offset = 0;
  362.     displaylist();}
  363.       break;
  364.     case K_END:
  365.       if (offset){
  366.     offset = 0;
  367.     displaylist();}
  368.       break;
  369.     case K_DOWN:
  370.       if (offset){
  371.     ++offset;
  372.     place = top + offset;
  373.     if (place < 0) place = place + size;
  374.     wm_scroll(main_window, 1);
  375.     if (buffer[place]  != NULL){
  376.       wm_puts_at(main_window, 0, scr_size[1]-1, buffer[place]);
  377.       Colour_Line(scr_size[1]-1, buffer[place][0]);
  378.     }
  379.       }
  380.       break;
  381.     case K_F1:
  382.       HELP();
  383.       break;
  384.     }
  385.   }
  386.   extended_code = 0;
  387.   normal_key_code = 0;
  388. }
  389.  
  390.  
  391. /* if the pipe doesn't exist, this function polls for it.  I don't
  392.  * like polling,  but we have no choice,  since the Snooper is
  393.  * usually always running,  but the Snoopee is not.
  394.  */
  395. static HPIPE OpenPipe(char *name)
  396. {
  397.   char msg[100];
  398.   char *pname;
  399.   USHORT rc;
  400.   ULONG us;
  401.   HPIPE hf;
  402.   int shown = FALSE;
  403.   int more = 1;
  404.   char junk;
  405.   
  406.   pname = name;
  407.   if(!pname)
  408.     pname = "\\pipe\\maxsnoop";
  409.   
  410.   for(;;){
  411.     rc = DosOpen((PCSZ) pname, &hf, &us, 0L, FILE_NORMAL,
  412.                  FILE_OPEN,
  413.                  OPEN_ACCESS_READONLY  | OPEN_SHARE_DENYREADWRITE | OPEN_FLAGS_NOINHERIT,
  414.                  0L);
  415.     if(!rc)
  416.       break;
  417.     if ((normal_key_code = _read_kbd(0,0,0)) >= 0){
  418.       if (!normal_key_code)
  419.     extended_code = _read_kbd(0,0,0);
  420.       handle_key_event(&junk, hf);
  421.     }
  422.  
  423.     if(rc == ERROR_PIPE_BUSY){
  424.       if (more){
  425.     more = !more;
  426.     strcpy(msg, "Pipe is busy, waiting to connect...");
  427.     ++top;
  428.     if (top == size) top = 0;
  429.     msg[scr_size[0]] = 0;
  430.     buffer[top] = realloc(buffer[top], strlen(msg) + 1);
  431.     strcpy(buffer[top], msg);
  432.     if (offset == 0){
  433.       wm_scroll(main_window, 1);
  434.       wm_puts_at(main_window, 0, scr_size[1]-1, buffer[top]);}
  435.     else{
  436.       offset = 0;
  437.       displaylist();}
  438.       }
  439.       rc = DosWaitNPipe(pname, 10L);
  440.       if(rc && rc != ERROR_SEM_TIMEOUT)
  441.     break;
  442.     }
  443.     else{
  444.       if(!shown){
  445.     
  446.     strcpy(msg, "Pipe doesn't exist; polling continues...");
  447.     ++top;
  448.     if (top == size) top = 0;
  449.  
  450.     msg[scr_size[0]] = 0;
  451.  
  452.     buffer[top] = realloc(buffer[top], strlen(msg) + 1);
  453.     strcpy(buffer[top], msg);
  454.     shown = 1;
  455.  
  456.     if (offset == 0){
  457.       wm_scroll(main_window, 1);
  458.       wm_puts_at(main_window, 0, scr_size[1]-1, buffer[top]);
  459.     }
  460.     else{
  461.       offset = 0;
  462.       displaylist();}
  463.       }
  464.       _sleep2(25);
  465.     }
  466.   }
  467.   
  468.   if(rc){
  469.     
  470.     printf("dosopen error %d\n", rc);
  471.     return 0;
  472.   }
  473.   DosSetNPHState(hf,  NP_READMODE_MESSAGE | NP_NOWAIT);
  474.   return hf;
  475. }
  476.  
  477. static USHORT PRead(HPIPE hp, VOID *pv, ULONG cb, USHORT *pbr)
  478. {
  479.   USHORT rc;
  480.   ULONG br=0;
  481.   ULONG state;
  482.   AVAILDATA ad;
  483.   
  484.   rc = DosRead(hp, pv, cb, &br);
  485.   if(!rc && br == 0){
  486.     DosSleep(1L);   /* yield to LanMan on zero byte read,  
  487.                so it can detect any pipe error.*/
  488.     rc = DosPeekNPipe(hp, "", 0, &br, &ad, &state);
  489.     if(!rc && state != 3)
  490.       rc = ERROR_BROKEN_PIPE;}
  491.   *pbr = br;
  492.   return rc;
  493. }
  494.  
  495.  
  496. void HELP()
  497. {
  498.   if (!Help_Open){
  499.     Help_Open = 1;
  500.     Help = wm_create(18,scr_size[1]/2-2,62, scr_size[1]/2+1, 2,
  501.              F_RED|B_BLACK|INTENSITY,F_YELLOW|B_BLACK|INTENSITY);
  502.     wm_border(Help, 2, B_BLACK|F_RED|INTENSITY,"press <esc> to close"
  503.           ,1, B_BLACK|F_BLUE|INTENSITY);
  504.     wm_open(Help);
  505.     wm_top(Help);
  506.     wm_puts_at(Help, 2,0,"Page UP, Down:  Scroll up, down one page");
  507.     wm_puts_at(Help, 2,1,"Home, End:      Go to top, bottem");
  508.     wm_puts_at(Help, 2,2,"Arrow UP, Down: Scroll up, down one line");
  509.     wm_puts_at(Help, 2,3,"x, X:           Quit");
  510.   }
  511. }
  512.  
  513.  
  514.