home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / examples / cback / back.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  5.7 KB  |  193 lines

  1. /*
  2.     SAS/C® Background Example
  3.     version 6.50   1993
  4.  
  5.     Copyright (c) 1992-1993 SAS Institute, Inc, Cary, NC, USA
  6.     All Rights Reserved
  7.  
  8.     This is a program which demonstrates how to write a simple background
  9.     process.  This program opens a window and writes a message.  You may close
  10.     the CLI it was run from before closing the window.
  11.  
  12.     You should compile this program with the following options:
  13.  
  14.                STRUCTUREEQUIVALENCE
  15.                LINK
  16.                STARTUP=cback
  17.  
  18.  
  19.     STRUCTUREEQUIVALENCE is not required, but it does circumvent several
  20.     warnings that would otherwise be produced.
  21.  
  22.     STARTUP=cback tells slink to link with the background startup code.  This
  23.     is required to produce a background process.  If you link in a separate step,
  24.     you must link with cback.o instead of c.o.
  25.  
  26.     *** IMPORTANT **  You must remember when writing a background process not to 
  27.     call printf() or other standard IO functions.  If you do, you will probably
  28.     crash the machine.
  29.  
  30.     You may notice that this program does not open any AmigaDOS libraries.  Instead
  31.     it takes advantage of the fact that the Version 6 development system 
  32.     opens needed AmigaDOS libraries for you.
  33.      
  34. */
  35.  
  36. /**************************  INCLUDES  ************************/
  37. #include <exec/types.h>
  38. #include <exec/execbase.h>
  39. #include <intuition/intuition.h>
  40. #include <proto/intuition.h>
  41. #include <proto/exec.h>
  42. #include <proto/dos.h>
  43. #include <dos/dos.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46.  
  47.  
  48. /**************************  CONSTANTS    ***********************/
  49. #define BANNER "\x9B""0;33mSAS/C® Background Example\x9B""0m \nCopyright \xA9 1992-1993 SAS Institute, Inc.\n"
  50.  
  51. /************************** VERSION INFO ***********************/
  52. static const char __version[] = "$VER: SASC_schelp 6.50 (26.08.93)";
  53.  
  54. /**************************  PROTOTYPES  **********************/
  55. void clean_exit(struct Window *, struct IOStdReq *, int);
  56. void action(void);
  57.  
  58.  
  59. /**********************  CBACK DECLARATIONS  ******************/
  60. long __stack = 4000;            /* Amount of stack space our task needs */
  61. char *__procname = "SAS/C® Background";  /* The name of the task to create       */
  62. long __priority = 0;            /* The priority to run the task at    */
  63. long __BackGroundIO = 1;      /* Flag indicating we want to send I/O to the
  64.                      original shell.  We will print a banner.
  65.                      NOTE:  This variable may also be called
  66.                      _BackGroundIO.  Notice the single
  67.                      underscore.                 */
  68. extern BPTR _Backstdout;      /* File handle pointing to originating shell
  69.                    (standard output when run in background) */
  70.  
  71. /********************************************************************/
  72.  
  73. extern struct ExecBase *SysBase;
  74.  
  75. void main(void)
  76. {
  77.    /*  Write a copyright banner */
  78.    if (_Backstdout)
  79.       {
  80.       Write(_Backstdout, BANNER, sizeof(BANNER));
  81.       Close(_Backstdout);
  82.       }
  83.  
  84.    /*  Call a function performing some action.    In this case the function will
  85.        open a window and wait for the user to close it.  */
  86.    action();
  87.  
  88.    clean_exit(NULL, NULL, NULL);
  89. }
  90.  
  91. /********************************************************************/
  92.  
  93. /*  Clean up and exit */
  94. void clean_exit(struct Window * w, struct IOStdReq *writereq, int closedev)
  95. {
  96.    if (closedev)
  97.       CloseDevice(writereq);
  98.  
  99.    if (w)
  100.        CloseWindow(w);
  101.  
  102.    exit(0);
  103. }
  104.  
  105.  
  106. /********************************************************************/
  107.  
  108.  
  109. /*  This function will open an Intuition window, attach a console device to it,
  110.     and then write to the window.  The function will then wait until the user
  111.     clicks on the close gadget of the window before closing the window and
  112.     exiting the program.    */
  113.  
  114. void action(void)
  115. {
  116.     struct Window *w;
  117.  
  118.     struct NewWindow nw = {
  119.         100,100,                  /* Starting corner */
  120.         292,100,                  /* Width, height */
  121.         2,1,                      /* Detail, Block pens */
  122.         CLOSEWINDOW | NEWSIZE,    /* IDCMP flags */
  123.         WINDOWDEPTH | WINDOWDRAG | WINDOWCLOSE | WINDOWSIZING,
  124.                               /* Window flags */
  125.         NULL,                      /* Pointer to first gadget */
  126.         NULL,                      /* Pointer to checkmark */
  127.         "SAS/C Background Example",       /* Title */
  128.         NULL,                      /* Screen pointer */
  129.         NULL,                      /* Bitmap pointer */
  130.         100,100,(USHORT)~0,(USHORT)~0,  /* Allow window to open to
  131.                        maximum screen size    */
  132.         WBENCHSCREEN              /* Type of screen */
  133.         };
  134.  
  135.     struct IntuiMessage * msg = NULL;
  136.  
  137.     struct IOStdReq *writereq;
  138.  
  139.     int type;
  140.     WORD mclass;
  141.     int done = 0;
  142.     BYTE error;
  143.     char *string = "You may close the CLI window now and this window will remain.";
  144.  
  145.     /*  Set type of window opened based on AmigaDOS version number */
  146.     if(SysBase->LibNode.lib_Version < 37)
  147.        {
  148.        type = 0;
  149.        nw.Flags |= SMART_REFRESH;
  150.        }
  151.     else 
  152.        {
  153.        type = 3;   
  154.        nw.Flags |= SIMPLE_REFRESH;
  155.        }
  156.  
  157.     /*    Open the window.   */
  158.     w = OpenWindow(&nw);
  159.     if(w  == NULL)
  160.        clean_exit(w, NULL, 0);
  161.  
  162.     writereq = calloc(1, sizeof(struct IOStdReq));
  163.  
  164.     /*    First open the console device, then write a message to the window.  */
  165.     writereq->io_Data = (APTR) w;
  166.     writereq->io_Length = sizeof(struct Window);
  167.     error = OpenDevice("console.device", type, writereq, 0);
  168.     if (error != NULL)
  169.        clean_exit(w, writereq, 0);
  170.  
  171.     writereq->io_Command = CMD_WRITE;
  172.     writereq->io_Data = (APTR) string;
  173.     writereq->io_Length = strlen(string);
  174.     DoIO(writereq);
  175.  
  176.     /*    Wait for a signal.  */
  177.     while ( !done )
  178.     {
  179.        WaitPort(w->UserPort);
  180.        while(msg = (struct IntuiMessage *) GetMsg(w->UserPort))
  181.        {
  182.           /*  When a signal is received, reply to it and evaluate it  */
  183.           mclass = msg->Class;
  184.           ReplyMsg ( (struct Message *) msg);
  185.           if (mclass == CLOSEWINDOW)
  186.             done = 1;
  187.        }
  188.     }
  189.  
  190.     clean_exit(w, writereq, 1);
  191. }
  192.  
  193.