home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / util / appcon-38.73.lha / AppCon / Src / AppCon.c next >
C/C++ Source or Header  |  1994-05-14  |  15KB  |  593 lines

  1. /* AppCon Main Source
  2. **
  3. ** Done by Stephan Fuhrmann
  4. **
  5. */
  6.  
  7. #define MTYPE_APPWINDOW 7
  8. #define EXEC_MINIMUM 36
  9. #define MY_ID 3111973
  10. /*#define DEBUG_ME*/
  11.  
  12. #include <AppCon.h>
  13. #include <dos.h>
  14. //#include <stdio.h>
  15. //#include <stdlib.h>
  16.  
  17. #define FILENAME_MAX 128
  18.  
  19. #include <string.h>
  20.  
  21. #include <dos/dos.h>
  22. #include <dos/dosextens.h>
  23. #include <exec/execbase.h>
  24. #include <exec/ports.h>
  25. #include <exec/io.h>
  26. #include <exec/libraries.h>
  27. #include <exec/memory.h>
  28. #include <exec/interrupts.h>
  29. #include <exec/tasks.h>
  30. #include <devices/console.h>
  31. #include <devices/conunit.h>
  32. #include <devices/keymap.h>
  33. #include <devices/input.h>
  34. #include <devices/inputevent.h>
  35. #include <devices/timer.h>
  36. #include <intuition/intuition.h>
  37. #include <intuition/intuitionbase.h>
  38. #include <workbench/startup.h>
  39. #include <workbench/workbench.h>
  40.  
  41. #ifdef LATTICE
  42. int CXBRK(void) { return(0); }
  43. int chkabort(void) { return(0); }
  44. #endif
  45.  
  46. struct ExecBase *SysBase;
  47. struct IntuitionBase *IntuitionBase;
  48. struct Library *WorkbenchBase;
  49. struct Library *KeymapBase;
  50. struct DosLibrary *DOSBase;
  51. struct Library *UtilityBase;
  52.  
  53. #include <proto/exec.h>
  54. #include <proto/dos.h>
  55. #include <proto/intuition.h>
  56. #include <proto/wb.h>
  57. #include <proto/keymap.h>
  58. #include <proto/utility.h>
  59.  
  60. long timeout=10;    /* in tenths of a second */
  61.  
  62. const char vt[]=VERSTAG;
  63.  
  64. struct Process *AppConTask=0L;
  65. struct CommandLineInterface *AppCLI;
  66. struct Window *AppConWindow;
  67.  
  68. struct MsgPort *timeport=0L;
  69. struct MsgPort *appport=0L;
  70. struct MsgPort *ioreply=0L;
  71. struct IOStdReq *iorequest=0L;
  72. struct timerequest *timerio=0L;
  73.  
  74. struct AppWindow *AppWindow;
  75.  
  76. struct AppMessage *AppMessage;
  77.  
  78. struct InputEvent *InputEvent;
  79.  
  80. ULONG WaitSignals=SIGBREAKF_CTRL_C;
  81. ULONG ReceivedSignals;
  82.  
  83. char filename[FILENAME_MAX];
  84. char dirname[FILENAME_MAX];
  85. char pathname[FILENAME_MAX*2];
  86.  
  87. char AppConName[]="AppCon";
  88.  
  89. #define TEMPLATE    "TimeOut=Time/K/N"
  90. enum templates {OPT_TIMEOUT,OPT_COUNT};
  91.  
  92. LONG opts[OPT_COUNT];
  93.  
  94. char HelpPage[]=
  95.     "\nAppCon Help\n"\
  96.     "-----------\n"\
  97.     "© 1994 Stephan Fuhrmann\n\n"\
  98.     "Options\n"\
  99.     "---------\n"\
  100.     "TIMEOUT...timeout between checks of window closing, in tenths of a second\\NUMBER\n"\
  101.     "\nExample: AppCon TIMEOUT=10\n";
  102.  
  103. int breakother (struct Process *MyProc);
  104. struct InputEvent *SendEvents (struct IOStdReq *iorequest,struct InputEvent *SendUs);
  105. void MakeActive (struct Window *ActivateMe);
  106. struct InputEvent *BrewInputevent(char *ASCII);
  107. void FreeIEs (struct InputEvent *firstie);
  108. struct Window *GetConWindow (struct Process *ConProcess,struct MsgPort *MyPort);
  109. long AntiBlocking (struct Window *MyWin);
  110. void MegaAbort (struct IOStdReq *);
  111. void LaunchRequest (long secs,long micros,struct timerequest *mytimerio);
  112.  
  113. __saveds int main(int argc,char *argv)
  114. {
  115.     struct RDArgs *argsptr;
  116.     APTR oldname;
  117.  
  118.     SysBase=*(struct ExecBase **)4;
  119.  
  120.     AppConTask=(struct Process *) FindTask(NULL);
  121.  
  122.     oldname=AppConTask->pr_Task.tc_Node.ln_Name;
  123.     AppConTask->pr_Task.tc_Node.ln_Name=AppConName;
  124.  
  125.     if (!argc)
  126.         return (0);
  127.  
  128.     if (DOSBase=(struct DosLibrary *)OpenLibrary ("dos.library",37))
  129.     {
  130.         PutStr (VSTRING);
  131.         PutStr ("PUBLIC DOMAIN written 1993/94 by Stephan Fuhrmann\n");
  132.  
  133.         if (IntuitionBase=(struct IntuitionBase *)OpenLibrary ("intuition.library",37))
  134.         {
  135.             if (WorkbenchBase=OpenLibrary ("workbench.library",37))
  136.             {
  137.                 if (KeymapBase=OpenLibrary ("keymap.library",37))
  138.                 {
  139.                     if (UtilityBase=OpenLibrary ("utility.library",37))
  140.                     {
  141.                         if (breakother (AppConTask))
  142.                         {
  143.                             argsptr=AllocDosObject (DOS_RDARGS,0);
  144.     
  145.                             if (argsptr)
  146.                             {
  147.                                 argsptr->RDA_ExtHelp=HelpPage;
  148.     
  149.                                 if (!ReadArgs (TEMPLATE,opts,argsptr))
  150.                                 {
  151.                                     PrintFault (IoErr(),NULL);
  152.                                     FreeArgs (argsptr);
  153.                                     FreeDosObject (DOS_RDARGS,argsptr);
  154.                                     return (0);
  155.                                 }
  156.                 
  157.                                 if (opts[OPT_TIMEOUT])
  158.                                     timeout=*(LONG *)opts[OPT_TIMEOUT];
  159.     
  160.                                 if ((ioreply=CreateMsgPort()) && (appport=CreateMsgPort()) && (timeport=CreateMsgPort()))
  161.                                 {
  162.                                     appport->mp_Node.ln_Name=AppConName;
  163.     
  164.                                     WaitSignals |= 1L << (ioreply->mp_SigBit);
  165.                                     WaitSignals |= 1L << (timeport->mp_SigBit);
  166.                                     WaitSignals |= 1L << (appport->mp_SigBit);
  167.     
  168.                                     if ((iorequest=CreateIORequest(ioreply,sizeof(struct IOStdReq)))
  169.                                        && (timerio=CreateIORequest(timeport,sizeof(struct timerequest))))
  170.                                     {
  171.             
  172.                                         if (!OpenDevice ("timer.device",UNIT_VBLANK,(struct IORequest *)timerio,0))
  173.                                         {
  174.                             
  175.                                             if (!OpenDevice ("input.device",0L,(struct IORequest *)iorequest,0))
  176.                                             {
  177.     
  178.                                                 AppConWindow=GetConWindow (AppConTask,ioreply);
  179.         
  180.                                                 if (AppConWindow)
  181.                                                 {
  182.                                                     if (AppWindow=AddAppWindowA (MY_ID,NULL,AppConWindow,appport,NULL))
  183.                                                     {
  184.                                                         ReceivedSignals=0;
  185.         
  186.                                                         LaunchRequest (timeout / 10,(timeout % 10) * 100000,timerio);
  187.             
  188.                                                         while (!(ReceivedSignals & SIGBREAKF_CTRL_C))
  189.                                                         {
  190.                                                             ReceivedSignals=Wait (WaitSignals);
  191.     
  192.                                                             if (ReceivedSignals & (1L << (timeport->mp_SigBit)))
  193.                                                             {
  194.                                                                 ReceivedSignals &= ~ (1L << (timeport->mp_SigBit));
  195.         
  196.                                                                 MegaAbort ((struct IOStdReq *)timerio);
  197.                                                                 LaunchRequest (timeout / 10,(timeout % 10) * 100000,timerio);
  198.         
  199.                                                                 if (AntiBlocking (AppConTask->pr_ConsoleTask) <= 1)
  200.                                                                     Signal ((struct Task *)AppConTask,SIGBREAKF_CTRL_C);
  201.                                                             }
  202.         
  203.                                                             if (ReceivedSignals & (1L << (appport->mp_SigBit)))
  204.                                                             {
  205.                                                                 ReceivedSignals &= ~ (1L << (appport->mp_SigBit));
  206.             
  207.                                                                 while (AppMessage = (struct AppMessage *)GetMsg (appport))
  208.                                                                 {
  209.                                                                     register long cnt,max;
  210.                                                                     register struct WBArg *curarg;
  211.                                                                     struct WBArg *argptr;
  212.             
  213.                                                                     if ((AppMessage->am_Type)!=MTYPE_APPWINDOW)
  214.                                                                     {
  215. #ifdef DEBUG_ME
  216.                                                                         VPrintf ("Unknown type (%ld)!",&AppMessage->am_Type);
  217. #endif        
  218.                                                                         ReplyMsg((struct Message *)AppMessage);
  219.                                                                         continue;
  220.                                                                     }
  221.     
  222.                                                                     max=AppMessage->am_NumArgs;
  223.                                                                     curarg=AppMessage->am_ArgList;
  224.         
  225.                                                                     if (!max)
  226.                                                                     {
  227. #ifdef DEBUG_ME
  228.                                                                         VPrintf   ("Not enough arguments (%ld)!",&max);
  229. #endif    
  230.                                                                         ReplyMsg((struct Message *)AppMessage);
  231.                                                                         continue;
  232.                                                                     }
  233.     
  234.                                                                     argptr=curarg;
  235.         
  236.                                                                     for (cnt=0;cnt<max;cnt++)
  237.                                                                     {
  238.                                                                         int hasspace;
  239.                                                                         int plusend;
  240.     
  241.                                                                         if (argptr->wa_Lock)
  242.                                                                             NameFromLock(argptr->wa_Lock,dirname,FILENAME_MAX);
  243.         
  244.                                                                         strcpy (pathname,dirname);
  245.                 
  246.                                                                         if (argptr->wa_Name)
  247.                                                                             AddPart(pathname,argptr->wa_Name,FILENAME_MAX*2);
  248.     
  249.                                                                         if (cnt)
  250.                                                                         {
  251.                                                                             if (InputEvent=BrewInputevent (" "))
  252.                                                                             {
  253.                                                                                 MakeActive (AppConWindow);
  254.                 
  255.                                                                                 FreeIEs(SendEvents (iorequest,InputEvent));
  256.                                                                             }
  257.                                                                             else
  258.                                                                                 DisplayBeep(0);
  259.                                                                         }
  260.             
  261.                                                                         hasspace=(int)strchr(pathname,' ');
  262.                                                                         plusend=*(pathname+strlen(pathname)-1)=='+';
  263.     
  264.                                                                         if (hasspace || plusend)
  265.                                                                         {
  266.                                                                             if (InputEvent=BrewInputevent ("\""))
  267.                                                                             {
  268.                                                                                 MakeActive (AppConWindow);
  269.             
  270.                                                                                 FreeIEs(SendEvents (iorequest,InputEvent));
  271.                                                                             }
  272.                                                                             else
  273.                                                                                 DisplayBeep(0);
  274.                                                                         }
  275.         
  276.                                                                         if (InputEvent=BrewInputevent (pathname))
  277.                                                                         {
  278.                                                                             MakeActive (AppConWindow);
  279.             
  280.                                                                             FreeIEs (SendEvents (iorequest,InputEvent));
  281.                                                                         }
  282.                                                                         else
  283.                                                                             DisplayBeep (0);
  284.                                                     
  285.                                                                         if (hasspace || plusend)
  286.                                                                         {
  287.                                                                             if (InputEvent=BrewInputevent ("\""))
  288.                                                                             {
  289.                                                                                 MakeActive (AppConWindow);
  290.             
  291.                                                                                 FreeIEs(SendEvents (iorequ