home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / dev / lang / fpl / debug / fpldb.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  9KB  |  300 lines

  1. /************************************************************************
  2.  *                FREXX PROGRAMMING LANGUAGE DEBUGGER                *
  3.  ************************************************************************
  4.  
  5.  FPL debug process main source
  6.  
  7.  ************************************************************************/
  8.  
  9. /************************************************************************
  10.  *                                                                      *
  11.  * fpl.library - A shared library interpreting script langauge.         *
  12.  * Copyright (C) 1992-1994 FrexxWare                                    *
  13.  * Author: Daniel Stenberg                                              *
  14.  *                                                                      *
  15.  * This program is free software; you may redistribute for non          *
  16.  * commercial purposes only. Commercial programs must have a written    *
  17.  * permission from the author to use FPL. FPL is *NOT* public domain!   *
  18.  * Any provided source code is only for reference and for assurance     *
  19.  * that users should be able to compile FPL on any operating system     *
  20.  * he/she wants to use it in!                                           *
  21.  *                                                                      *
  22.  * You may not change, resource, patch files or in any way reverse      *
  23.  * engineer anything in the FPL package.                                *
  24.  *                                                                      *
  25.  * This program is distributed in the hope that it will be useful,      *
  26.  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
  27.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                 *
  28.  *                                                                      *
  29.  * Daniel Stenberg                                                      *
  30.  * Ankdammsgatan 36, 4tr                                                *
  31.  * S-171 43 Solna                                                       *
  32.  * Sweden                                                               *
  33.  *                                                                      *
  34.  * FidoNet 2:201/328    email:dast@sth.frontec.se                       *
  35.  *                                                                      *
  36.  ************************************************************************/
  37.  
  38. #include <dos/dos.h>
  39. #include <exec/execbase.h>
  40. #include <exec/memory.h>
  41. #include <exec/types.h>
  42. #include <exec/ports.h>
  43. #include <proto/dos.h>
  44. #include <proto/exec.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <stdarg.h>
  49.  
  50. #include "/src/script.h"
  51. #include "debugmail.h"
  52.  
  53. extern struct ExecBase *SysBase;
  54. struct GfxBase *GfxBase=NULL;
  55. struct IntuitionBase *IntuitionBase=NULL;
  56.  
  57.  
  58. #define TEMPLATE "W=Window/k,H=Help/s"
  59. struct args {
  60.   char *window;
  61.   ULONG help;
  62. };
  63.  
  64. struct Command {
  65.   char *name;
  66.   char flag;
  67.   long ignores;
  68. };
  69.  
  70. #define COMMAND_IGNORE        1 /* this output is hidden */
  71. #define COMMAND_NOTIGNOREABLE 2 /* this command's output can't be hidden! */
  72.  
  73. #define STRUCT_ENTRIES(x) sizeof(x)/sizeof(x[0])
  74.  
  75. struct args argarray;
  76. struct RDArgs *rdargs=NULL;
  77. struct MsgPort *TimerMP=NULL;
  78. struct timerequest *TimerIO=NULL;
  79.  
  80. BPTR output; /* output file handle for Mprintf() */
  81.  
  82. void Main(void);
  83. void __regargs Mprintf(char *, ...);
  84.  
  85. #define DEFAULT_WINDOW "CON:0/0/640/200/FPLdb/AUTO/CLOSE/WAIT/INACTIVE"
  86.  
  87. #define HELPTEXT "\
  88. Simply run this file to start the FPL debug process!\n\
  89. Default window string is \"%s\"\n"
  90.  
  91. struct Command cmds[]={
  92.   {"string", 0, 0},
  93.   {"function", 1, 0}, /* default ignore */
  94.   {"start", 0, 0},
  95.   {"exit", 0, 0},
  96.   {"hide", COMMAND_NOTIGNOREABLE, 0},
  97.   {"show", COMMAND_NOTIGNOREABLE, 0},
  98.   {"quit", COMMAND_NOTIGNOREABLE, 0},
  99.   {"lines", 0, 0}
  100. };
  101.  
  102. #define MAX_NO_FPL 5 /* max number of concurrent debugging libraries */
  103.  
  104. void *FPL_bases[MAX_NO_FPL]; /* to find them quick! */
  105.  
  106. long Debugging; /* number of current debugger users */
  107.  
  108. DebugRet ShowHide(char,    /* TRUE if it is to be hidden */
  109.                   char *); /* function string */
  110.  
  111. void InputLoop(void);
  112.  
  113. int main()
  114. {
  115.   output = Output();
  116.   if (SysBase->SoftVer>36) {
  117.     if ((rdargs=ReadArgs(TEMPLATE,(LONG *)&argarray,NULL))) {
  118.       if(argarray.help)
  119.         Mprintf(HELPTEXT, DEFAULT_WINDOW);
  120.       else {
  121.         output = Open(argarray.window?argarray.window:DEFAULT_WINDOW,
  122.                       MODE_NEWFILE);
  123.         if(output) {
  124.           InputLoop();
  125.           Close(output);
  126.         } else {
  127.           output = Output();
  128.           Mprintf("Couldn't open window!\n");
  129.         }
  130.       }
  131.       FreeArgs(rdargs);
  132.     } else
  133.       Mprintf("Bad input!\n");
  134.   } else
  135.     Mprintf("Kickstart must be over 36!\n");
  136.   return 0;
  137. }
  138.  
  139. void InputLoop()
  140. {
  141.   int count;
  142.   struct MsgPort *port;
  143.   char stop=FALSE;
  144.   int signal;
  145.   char *text;
  146.   struct Messy *mess;
  147.  
  148.   if (!FindPort(MSGPORT_NAME)) {
  149.     port=CreatePort(MSGPORT_NAME, 0);
  150.     if (port) {
  151.       Mprintf("FPLdb is ready to receive\n");
  152.       while (!stop) {
  153.         signal=Wait((1<<port->mp_SigBit) | SIGBREAKF_CTRL_C);
  154.         if (signal==SIGBREAKF_CTRL_C) {
  155.           stop=TRUE;
  156.         } else {
  157.           mess=(struct Messy *)GetMsg(port);
  158.           if(!(cmds[mess->flag].flag&COMMAND_IGNORE)) {
  159.             struct Data *scr = mess->scr;
  160.             switch(mess->flag) {
  161.             case MAIL_STRING:
  162.               /*
  163.                * Just a plain text string received from another FPLdb process!
  164.                */
  165.               Mprintf("> %s\n", (char *)mess->data);
  166.               break;
  167.             case MAIL_FUNCTION:
  168.               /*
  169.                * Someone invoked a fpl.library function!
  170.                */
  171.               Mprintf("fpl.library/%s(); by %s\n",
  172.                       (char *)mess->data,
  173.                       scr->identifier?scr->identifier:"<unknown>");
  174.               break;
  175.             case MAIL_START:
  176.               /*
  177.                * A process just initialized an FPL debug session!
  178.                */
  179.           for(count=1; count<MAX_NO_FPL; count++) {
  180.         if(FPL_bases[count]==scr) {
  181.           count=-1;
  182.           break;
  183.         }
  184.           }
  185.           if(count<0)
  186.             /* already debugging */
  187.             break;
  188.           for(count=1; count<MAX_NO_FPL; count++) {
  189.         if(!FPL_bases[count]) {
  190.           break;
  191.         }
  192.           }
  193.           if(count==MAX_NO_FPL) {
  194.             /* no room for more! */
  195.         mess->ret = FPLDB_FULL;
  196.         break;
  197.           }
  198.           FPL_bases[count] = scr;
  199.  
  200.           /*
  201.            * To find information regarding _this_ process really fast
  202.            * when called, we store the array position in our field
  203.            * in the main FPL structure.
  204.            */
  205.           scr->debugentry = (void *)count;
  206.           Debugging++;
  207.               Mprintf("'%s' just initialized a new FPL session\n", (char *)mess->data);
  208.           break;
  209.             case MAIL_EXIT:
  210.               /*
  211.                * A process just exited an FPL debug session!
  212.                */
  213.           FPL_bases[(int)scr->debugentry]=NULL;
  214.           Debugging--;
  215.               Mprintf("'%s' just exited its FPL session\n", (char *)mess->data);
  216.               break;
  217.           
  218.             case MAIL_HIDE:
  219.               /*
  220.                * A request to prevent a certain debug-output is received!
  221.                */
  222.             case MAIL_SHOW:
  223.               /*
  224.                * A request to show a certain debug-output is received!
  225.                */
  226.               mess->ret = ShowHide(mess->flag==MAIL_HIDE?1:0,
  227.                                    (char *)mess->data);
  228.               Mprintf("* received mail\n");
  229.               break;
  230.             case MAIL_EXITALL:
  231.               /*
  232.                * We should exit!
  233.                */
  234.               stop=TRUE;
  235.               break;
  236.         case MAIL_EXECUTE:
  237.           text = scr->text;
  238.           Mprintf("%ld :", scr->virprg);
  239.           while(*text && *text!='\n') {
  240.         FPutC(output, *text);
  241.         text++;
  242.           }
  243.               FPutC(output, '\n');
  244.           break;
  245.             default:
  246.               Mprintf("Unsupported mail received!\n");
  247.               break;
  248.             }
  249.           } else {
  250.             cmds[mess->flag].ignores++; /* count ignores! */
  251.           }
  252.           ReplyMsg((struct Message *)mess);
  253.         }
  254.       }
  255.       Mprintf("termination signal received!\n");
  256.       Forbid();
  257.       while(mess=(struct Messy *)GetMsg(port))
  258.         ReplyMsg((struct Message *)mess);
  259.       DeletePort(port);
  260.       Permit();
  261.     }
  262.   } else
  263.     Mprintf("FPLdb is already running\n");
  264. }
  265.  
  266.  
  267. DebugRet ShowHide(char hide,  /* TRUE if it is to be hidden */
  268.                   char *name) /* function string */
  269. {
  270.   DebugRet ret = FPLDB_OK;
  271.   int count;
  272.   int hit=-1;
  273.   for(count=0; count<STRUCT_ENTRIES(cmds); count++) {
  274.     if(!strcmp(cmds[count].name, name)) {
  275.       hit=count;
  276.       break;
  277.     }
  278.   }
  279.   if(hit>-1) {
  280.     if(cmds[hit].flag&COMMAND_NOTIGNOREABLE)
  281.       ret = FPLDB_CANT_IGNORE;
  282.     else if(hide)
  283.       cmds[hit].flag |= COMMAND_IGNORE;
  284.     else
  285.       cmds[hit].flag &= ~COMMAND_IGNORE;
  286.   } else
  287.     ret = FPLDB_NOTFOUND;
  288.   return ret;
  289. }
  290.  
  291. void __regargs Mprintf(char *format, ...)
  292. {
  293.   char buffer[256]; /* 256 chars os max length of string to print at once! */
  294.   va_list args;
  295.   va_start(args, format);
  296.   RawDoFmt(format, args, (void (*))"\x16\xc0\x4e\x75", buffer);
  297.   va_end(args);
  298.   Write(output, buffer, strlen(buffer));
  299. }
  300.