home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / common / msasc.c < prev    next >
C/C++ Source or Header  |  2001-12-12  |  6KB  |  190 lines

  1. /*  msasc.c -- special common code for Amiga SAS/C  */
  2.  
  3.  
  4. #include "../h/gsupport.h"
  5.  
  6. #if AMIGA && __SASC
  7. #include <workbench/startup.h>
  8. #include <rexx/rxslib.h>
  9. #include <proto/dos.h>
  10. #include <proto/icon.h>
  11. #include <proto/wb.h>
  12. #include <proto/rexxsyslib.h>
  13. #include <proto/exec.h>
  14.  
  15. int _WBargc;
  16. char **_WBargv;
  17. struct MsgPort *_IconPort = NULL;
  18. char *_PortName;
  19.  
  20. /* This is an SAS/C auto-initialization routine.  It extracts the
  21.  * filename arguments from the ArgList in the Workbench startup message
  22.  * and generates an ANSI argv, argc from them.  These are given the
  23.  * global pointers _WBargc and _WBargv.  It also checks the Tooltypes for
  24.  * a WINDOW specification and points the ToolWindow to it.  (NOTE: the
  25.  * ToolWindow is a reserved hook in the WBStartup structure which is
  26.  * currently unused. When the Workbench supports editing the ToolWindow,
  27.  * this ToolType will become obsolete.)  The priority is set to 400 so
  28.  * this will run before the stdio initialization (_iob.c).  The code in
  29.  * _iob.c sets up the default console window according to the ToolWindow
  30.  * specification, provided it is not NULL.
  31.  */
  32.  
  33. int __stdargs _STI_400_WBstartup(void) {
  34.    struct WBArg *wba;
  35.    struct DiskObject *dob;
  36.    int n;
  37.    char buf[512];
  38.    char *windowspec;
  39.  
  40.    _WBargc = 0;
  41.    if(_WBenchMsg == NULL || Output() != NULL) return 0;
  42.    _WBargv = (char **)malloc((_WBenchMsg->sm_NumArgs + 4)*sizeof(char *));
  43.    if(_WBargv == NULL) return 1;
  44.    wba = _WBenchMsg->sm_ArgList;
  45.  
  46.    /* Change to the WB icon's directory */
  47.    CurrentDir((wba+1)->wa_Lock);
  48.  
  49.    /* Get the window specification */
  50.    if(dob = GetDiskObject((wba+1)->wa_Name)) {
  51.       if(dob->do_ToolTypes){
  52.          windowspec = FindToolType(dob->do_ToolTypes, "WINDOW");
  53.          if (windowspec){
  54.             _WBenchMsg->sm_ToolWindow = malloc(strlen(windowspec)+1);
  55.             strcpy(_WBenchMsg->sm_ToolWindow, windowspec);
  56.             }
  57.          }
  58.       FreeDiskObject(dob);
  59.       }
  60.  
  61.    /* Create argc and argv */
  62.    for(n = 0; n < _WBenchMsg->sm_NumArgs; n++, wba++){
  63.       if (wba->wa_Name != NULL &&
  64.               NameFromLock(wba->wa_Lock, buf, sizeof(buf)) != 0) {
  65.          AddPart(buf, wba->wa_Name, sizeof(buf));
  66.          _WBargv[_WBargc] = (char *)malloc(strlen(buf) + 1);
  67.          if (_WBargv[_WBargc] == NULL) return 1;
  68.          strcpy(_WBargv[_WBargc], buf);
  69.          _WBargc++;
  70.          }
  71.       }
  72.  
  73.    /* Just in case ANSI is watching ... */
  74.    _WBargv[_WBargc] = NULL;
  75.    }
  76.  
  77. /* We open and close our message port with this auto-initializer and
  78.  * auto-terminator to minimize disruption of the Icon code.
  79.  */
  80.  
  81. void _STI_10000_OpenPort(void) {
  82.    char  *name;
  83.    char  *end;
  84.    int   n = 1;
  85.    char  buf[256];
  86.  
  87.    if( GetProgramName(buf, 256) == 0) {
  88.      if (_WBargv == NULL) return;
  89.      else strcpy(buf, _WBargv[0]);
  90.      }
  91.  
  92.    name = FilePart(buf);
  93.    _PortName = malloc(strlen(name) + 2);
  94.    strcpy(_PortName, name);
  95.    end = _PortName + strlen(_PortName);
  96.    /* In case there are many of us */
  97.    while ( FindPort(_PortName) != NULL ) {
  98.       sprintf(end, "%d", n++);
  99.       if (n > 9) return;
  100.       }
  101.    _IconPort = CreatePort(_PortName, 0);
  102.    }
  103.  
  104. void _STD_10000_ClosePort(void) {
  105.    struct Message *msg;
  106.  
  107.    if (_IconPort) {
  108.       while (msg = GetMsg(_IconPort)) ReplyMsg(msg);
  109.       DeletePort(_IconPort);
  110.       }
  111.    }
  112.  
  113. /*
  114.  * This posts an error message to the ARexx Clip List.
  115.  * The clip is named <_PortName>Clip.<errorcount>.  The value
  116.  * string contains the file name, line number, error number and
  117.  *  error message.
  118.  */
  119. static int errorcount = 0;
  120.  
  121. void PostClip(char *file, int line, int number, char *text) {
  122.    struct MsgPort *rexxport;
  123.    struct RexxMsg *rxmsg;
  124.    char name[128];
  125.    char value[512];
  126.  
  127.    if ( _IconPort ) {
  128.       if ( rxmsg = CreateRexxMsg(_IconPort, NULL, NULL) ) {
  129.          errorcount++;
  130.          sprintf(name, "%sClip.%d", _PortName, errorcount);
  131.          sprintf(value, "File: %s Line: %d Number: %d Text: %s",
  132.                          file, line, number, text);
  133.          rxmsg->rm_Action = RXADDCON;
  134.          ARG0(rxmsg) = name;
  135.          ARG1(rxmsg) = value;
  136.          ARG2(rxmsg) = (unsigned char *)(strlen(value) + 1);
  137.          Forbid();
  138.          rexxport = FindPort("REXX");
  139.          if ( rexxport ) {
  140.             PutMsg(rexxport, (struct Message *)rxmsg);
  141.             WaitPort(_IconPort);
  142.             }
  143.          Permit();
  144.          GetMsg(_IconPort);
  145.          DeleteRexxMsg(rxmsg);
  146.          }
  147.       }
  148.    }
  149.  
  150.  
  151. /*
  152.  * This function sends a message to the resident ARexx process telling it to
  153.  * run the specified script with argument a stem for the names of the clips
  154.  * containing error information.  The intended use is to invoke an editor
  155.  *  when a fatal error is encountered.
  156.  */
  157.  
  158. void CallARexx(char *script) {
  159.    struct MsgPort *rexxport;
  160.    struct RexxMsg *rxmsg;
  161.    char command[512];
  162.  
  163.    if ( _IconPort ) {
  164.       if ( rxmsg = CreateRexxMsg(_IconPort, NULL, NULL) ) {
  165.          sprintf(command, "%s %sClip", script, _PortName);
  166.          rxmsg->rm_Action = RXCOMM | RXFB_NOIO;
  167.          ARG0(rxmsg) = command;
  168.          if (FillRexxMsg(rxmsg,1,0) ) {
  169.             Forbid();
  170.             rexxport = FindPort("REXX");
  171.             if ( rexxport ) {
  172.                PutMsg(rexxport, (struct Message *)rxmsg);
  173.                WaitPort(_IconPort);
  174.                }
  175.             Permit();
  176.             GetMsg(_IconPort);
  177.             ClearRexxMsg(rxmsg,1);
  178.             }
  179.          DeleteRexxMsg(rxmsg);
  180.          }
  181.       }
  182.    }
  183.  
  184.  
  185. #else                                  /* AMIGA && __SASC */
  186.  
  187. char junkclocal; /* avoid empty module */
  188.  
  189. #endif                    /* AMIGA && __SASC */
  190.