home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 103.lha / IntDevExpl / main.c < prev    next >
C/C++ Source or Header  |  1986-11-21  |  3KB  |  154 lines

  1. /*
  2. ** FILE:    main.c
  3. ** PROGRAM:    idev
  4. ** VERSION:    1.0
  5. **
  6. ** DESCRIPTIPON:
  7. **    This file contains main().  It checks the arguments, creates
  8. **    the reply port and IORequest, and initializes the IORequest
  9. **    through OpenDevice.  It then calls idev() (see idev.[ch])
  10. **    to perform the user interaction.  When idev() returns, the
  11. **    IORequest and reply port are cleaned up, and the device is
  12. **    closed.
  13. **
  14. **    Quit() is the function which handles cleanup.  If fmt is not
  15. **    NULL, an information line of the form:
  16. **        progname: fmt_msg
  17. **    is printed before cleanup.  The extra arguments are passed to
  18. **    printf() for the format string (in case it includes %X's).
  19. **    Finally, exit() is called with exitval.
  20. **
  21. **    Cmdname is set to argv[0] (the name of the program) upon
  22. **    program entry.
  23. **
  24. ** HISTORY:
  25. **    26-Aug-87    Original Version   (Ed Puckett)
  26. */
  27.  
  28. #include <exec/types.h>
  29. #include <exec/ports.h>
  30. #include <exec/io.h>
  31. #include <exec/devices.h>
  32. #include <stdio.h>
  33.  
  34. #include "main.h"
  35. #include "idev.h"
  36.  
  37.  
  38.  
  39. char  *cmdname;
  40.  
  41. static BOOL  devopen = FALSE;
  42.  
  43. static struct MsgPort    *devport = NULL;
  44. static struct IOStdReq    *ioreq     = NULL;
  45.  
  46.  
  47.  
  48. void  main (argc, argv)
  49.  
  50. int   argc;
  51. char  *argv[];
  52.  
  53. { char           *devname;
  54.   int           unitnum;
  55.   ULONG        flags;
  56.   int           error;
  57.   ULONG        getnum();
  58.   void           usage();
  59.   struct MsgPort   *CreatePort();
  60.   struct IOStdReq  *CreateExtIO();
  61.  
  62.  
  63.   if (argc == 0)
  64.     exit (0);        /* don't run from WB */
  65.  
  66.  
  67.   cmdname= argv[0];
  68.  
  69.   if (argc != 4)
  70.     usage ();
  71.  
  72.   devopen= FALSE;
  73.   devport= NULL;
  74.   ioreq=   NULL;
  75.  
  76.   devname= argv[1];
  77.   unitnum= getnum (argv[2], "unitnum");
  78.   flags=   getnum (argv[3], "flags");
  79.  
  80.  
  81.   if ((devport= CreatePort (NULL, 0)) == NULL)
  82.     quit (Q_RSRCERR, "can't create message port", 0, 0, 0);
  83.  
  84.   if ((ioreq= CreateExtIO (devport, IOREQSIZE)) == NULL)
  85.     quit (Q_RSRCERR, "can't create I/O request", 0, 0, 0);
  86.  
  87.   if ((error= OpenDevice (devname, unitnum, ioreq, flags)) != 0)
  88.     quit (Q_RSRCERR, "can't open device \"%s\"; error = %d", (int) devname, error, 0);
  89.  
  90.   devopen= TRUE;
  91.  
  92.  
  93.   idev (devname, unitnum, ioreq, flags);
  94.  
  95.  
  96.   quit (Q_OK, NULL, 0, 0, 0);
  97. }
  98.  
  99.  
  100.  
  101. void  quit (exitval, fmt, arg1, arg2, arg3)
  102.  
  103. int   exitval;
  104. char  *fmt;
  105. int   arg1, arg2, arg3;
  106.  
  107. { if (fmt != NULL)
  108.     { fprintf (stderr, "%s: ", cmdname);
  109.       fprintf (stderr, fmt, arg1, arg2, arg3);
  110.       fputs ("\n", stderr);
  111.     }
  112.  
  113.   if (devopen)
  114.     { CloseDevice (ioreq);
  115.       devopen= FALSE;
  116.     }
  117.  
  118.   if (ioreq != NULL)
  119.     { DeleteExtIO (ioreq, IOREQSIZE);
  120.       ioreq= NULL;
  121.     }
  122.  
  123.   if (devport != NULL)
  124.     { DeletePort (devport);
  125.       devport= NULL;
  126.     }
  127.  
  128.   exit (exitval);
  129. }
  130.  
  131.  
  132.  
  133. static void  usage ()
  134.  
  135. { fprintf (stderr, "Usage: %s devname unitnum flags\n", cmdname);
  136.   quit (Q_USAGE, NULL, 0, 0, 0);
  137. }
  138.  
  139.  
  140.  
  141. static ULONG  getnum (str, varname)
  142.  
  143. char   *str;
  144. char   *varname;
  145.  
  146. { ULONG  num;
  147.  
  148.   if (sscanf (str, "%ld", &num) != 1)
  149.     quit (Q_ARGERR, "%s must be an integer", varname, 0, 0);
  150.  
  151.   return num;
  152. }
  153.  
  154.