home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / SPL.ZIP / spl.c next >
C/C++ Source or Header  |  1993-04-12  |  9KB  |  403 lines

  1. #define INCL_SPL
  2. #define    INCL_SPLDOSPRINT
  3. #include <os2.h>
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #define ARG_DRIVER  1
  10. #define ARG_QUEUE   2
  11. #define ARG_PRINTER 3
  12. #define ARG_PORT    4
  13. #define ARG_DEVICE  5
  14.  
  15. char textbuf [2048];
  16.  
  17. void usage (void)
  18. {
  19.     puts ("Usage: spl [driver|queue|printer|port|device] {name} {/d} {//node}");
  20.     exit (0);
  21. }
  22.  
  23. BOOL NonNull (PSZ psz)
  24. {
  25.     return psz && strlen(psz) > 0;
  26. }
  27.  
  28. int Enumerate (int iArg, PSZ pszComp)
  29. {
  30. int rc;
  31. ULONG cRet;
  32. ULONG cAvail;
  33. ULONG cNeeded;
  34. int i;
  35.  
  36.     switch (iArg)
  37.     {
  38.     case ARG_DRIVER:
  39.         {
  40.         PRDRIVINFO *pinfo;
  41.  
  42.         pinfo = (PRDRIVINFO *) textbuf;
  43.         rc = SplEnumDriver (pszComp, 0, textbuf, sizeof textbuf,
  44.         &cRet, &cAvail, &cNeeded, NULL);
  45.  
  46.         if (rc)
  47.         {
  48.         printf ("Error code %d\n", rc);
  49.         return rc;
  50.         }
  51.         else if (cRet == 0)
  52.         puts ("No drivers");
  53.         else
  54.         for (i = 0 ; i < cRet ; i++)
  55.             puts (pinfo[i].szDrivName);
  56.         }
  57.         break;
  58.  
  59.     case ARG_QUEUE:
  60.         {
  61.         PSZ *pinfo;
  62.  
  63.         pinfo = (PSZ *) textbuf;
  64.         rc = SplEnumQueue (pszComp, 5, textbuf, sizeof textbuf,
  65.         &cRet, &cAvail, &cNeeded, NULL);
  66.  
  67.         if (rc)
  68.         {
  69.         printf ("Error code %d\n", rc);
  70.         return rc;
  71.         }
  72.         else if (cRet == 0)
  73.         puts ("No queues");
  74.         else
  75.         for (i = 0 ; i < cRet ; i++)
  76.             puts (pinfo[i]);
  77.         }
  78.         break;
  79.  
  80.     case ARG_PRINTER:
  81.         {
  82.         PRINTERINFO *pinfo;
  83.         ULONG flType = SPL_PR_QUEUE | SPL_PR_DIRECT_DEVICE |
  84.         SPL_PR_QUEUED_DEVICE | SPL_PR_LOCAL_ONLY;
  85.         PSZ psz;
  86.  
  87.         pinfo = (PRINTERINFO *) textbuf;
  88.         rc = SplEnumPrinter (pszComp, 0, flType, textbuf, sizeof textbuf,
  89.         &cRet, &cAvail, &cNeeded, NULL);
  90.  
  91.         if (rc)
  92.         {
  93.         printf ("Error code %d\n", rc);
  94.         return rc;
  95.         }
  96.         else if (cRet == 0)
  97.         puts ("No printers");
  98.         else
  99.         for (i = 0 ; i < cRet ; i++)
  100.             {
  101.             switch (pinfo[i].flType)
  102.             {
  103.             case SPL_PR_QUEUE:
  104.                 psz = "Queue";            break;
  105.             case SPL_PR_QUEUED_DEVICE:
  106.                 psz = "Queued-Device";        break;
  107.             case SPL_PR_DIRECT_DEVICE:
  108.                 psz = "Direct-Device";        break;
  109.             default:
  110.                 psz = "Unknown";
  111.             }
  112.             printf ("%s, Type=%s", pinfo[i].pszPrintDestinationName,
  113.                 psz);
  114.             psz = pinfo[i].pszComputerName;
  115.             if (NonNull(psz))
  116.             printf (", Computer=%s", psz);
  117.             psz = pinfo[i].pszLocalName;
  118.             if (NonNull(psz))
  119.             printf (", LocalName=%s", psz);
  120.             psz = pinfo[i].pszDescription;
  121.             if (NonNull(psz))
  122.             printf (", Description=\"%s\"\n", psz);
  123.             else
  124.             putchar ('\n');
  125.  
  126.             }
  127.         }
  128.         break;
  129.  
  130.     case ARG_PORT:
  131.         {
  132.         PRPORTINFO *pinfo;
  133.  
  134.         pinfo = (PRPORTINFO *) textbuf;
  135.         rc = SplEnumPort (pszComp, 0, textbuf, sizeof textbuf,
  136.         &cRet, &cAvail, &cNeeded, NULL);
  137.  
  138.         if (rc)
  139.         {
  140.         printf ("Error code %d\n", rc);
  141.         return rc;
  142.         }
  143.         else if (cRet == 0)
  144.         puts ("No ports");
  145.         else
  146.         for (i = 0 ; i < cRet ; i++)
  147.             puts (pinfo[i].szPortName);
  148.         }
  149.         break;
  150.  
  151.     case ARG_DEVICE:
  152.         {
  153.         PRDINFO3 *pinfo;
  154.         PSZ psz;
  155.  
  156.         pinfo = (PRDINFO3 *) textbuf;
  157.         rc = SplEnumDevice (pszComp, 3, textbuf, sizeof textbuf,
  158.         &cRet, &cAvail, &cNeeded, NULL);
  159.  
  160.         if (rc)
  161.         {
  162.         printf ("Error code %d\n", rc);
  163.         return rc;
  164.         }
  165.         else if (cRet == 0)
  166.         puts ("No devices");
  167.         else
  168.         for (i = 0 ; i < cRet ; i++)
  169.             {
  170.             psz = pinfo[i].pszLogAddr;
  171.             if (psz && strcmp(psz,""))
  172.             printf ("%s (%s)\n", pinfo[i].pszPrinterName, psz);
  173.             else
  174.             puts (pinfo[i].pszPrinterName);
  175.             }
  176.         }
  177.         break;
  178.     }
  179.     return 0;
  180. }
  181.  
  182. int Query (int iArg, PSZ pszName, PSZ pszComp)
  183. {
  184. int rc;
  185. ULONG cNeeded;
  186.  
  187.     switch (iArg)
  188.     {
  189.     case ARG_DRIVER:
  190.     case ARG_PRINTER:
  191.     case ARG_PORT:
  192.         printf ("You can only query devices or queues\n");
  193.         return 1;
  194.  
  195.     case ARG_QUEUE:
  196.         #define info MAKETYPE(textbuf, PRQINFO6)
  197.  
  198.         rc = SplQueryQueue (pszComp, pszName, 6, textbuf,
  199.         sizeof textbuf, &cNeeded);
  200.  
  201.         if (rc)
  202.         {
  203.         printf ("Error code %d\n", rc);
  204.         return rc;
  205.         }
  206.         else
  207.         {
  208.         printf ("Priority = %d\n", info.uPriority);
  209.         printf ("StartTime = %d\n", info.uStartTime);
  210.         printf ("UntilTime = %d\n", info.uUntilTime);
  211.         if (info.fsType == PRQ3_TYPE_RAW)
  212.             puts ("Type = RAW");
  213.         else if (info.fsType == PRQ3_TYPE_BYPASS)
  214.             puts ("Type = QP_BYPASS");
  215.         else
  216.             puts ("Type = STANDARD");
  217.         if (NonNull(info.pszSepFile))
  218.             printf ("Separator = %s\n", info.pszSepFile);
  219.         if (NonNull(info.pszPrProc))
  220.             printf ("QPROC = %s\n", info.pszPrProc);
  221.         if (NonNull(info.pszParms))
  222.             printf ("Queue Parms = %s\n", info.pszParms);
  223.         if (NonNull(info.pszComment))
  224.             printf ("Comment = %s\n", info.pszComment);
  225.         fputs (info.fsStatus & PRQ3_PAUSED ? "Held" : "Released",
  226.                 stdout);
  227.         if (info.fsStatus & PRQ3_PENDING)
  228.             fputs (", Pending deletion", stdout);
  229.         printf ("\nNumber of jobs = %d\n", info.cJobs);
  230.         printf ("Printers = %s\n", info.pszPrinters);
  231.         printf ("Driver = %s\n", info.pszDriverName);
  232.         if (NonNull(info.pszRemoteComputerName))
  233.             printf ("Remote Computer = %s\n", info.pszRemoteComputerName);
  234.         if (NonNull(info.pszRemoteQueueName))
  235.             printf ("Remote Queue = %s\n", info.pszRemoteQueueName);
  236.         }
  237.         #undef info
  238.         break;
  239.  
  240.     case ARG_DEVICE:
  241.         #define info MAKETYPE(textbuf, PRDINFO3)
  242.  
  243.         rc = SplQueryDevice (pszComp, pszName, 3, textbuf,
  244.         sizeof textbuf, &cNeeded);
  245.  
  246.         if (rc)
  247.         {
  248.         printf ("Error code %d\n", rc);
  249.         return rc;
  250.         }
  251.         else
  252.         {
  253.         if (NonNull (info.pszLogAddr))
  254.             printf ("Logical Address = %s\n", info.pszLogAddr);
  255.         switch (info.fsStatus & PRD_STATUS_MASK)
  256.             {
  257.             case PRD_ACTIVE:
  258.             puts ("Processing");            break;
  259.             case PRD_PAUSED:
  260.             puts ("Not processing, or paused");    break;
  261.             }
  262.         if (info.fsStatus & PRJ_COMPLETE)
  263.             puts ("Job complete");
  264.         if (info.fsStatus & PRJ_INTERV)
  265.             puts ("Intervention required");
  266.         if (info.fsStatus & PRJ_ERROR)
  267.             puts ("Error occurred");
  268.         if (info.fsStatus & PRJ_DESTOFFLINE)
  269.             puts ("Device offline");
  270.         if (info.fsStatus & PRJ_DESTPAUSED)
  271.             puts ("Device paused");
  272.         if (info.fsStatus & PRJ_NOTIFY)
  273.             puts ("Raise alert");
  274.         if (info.fsStatus & PRJ_DESTNOPAPER)
  275.             puts ("No paper");
  276.         if (NonNull (info.pszStatus))
  277.             printf ("Status = %s\n", info.pszStatus);
  278.         if (NonNull (info.pszComment))
  279.             printf ("Description = %s\n", info.pszComment);
  280.         if (NonNull (info.pszDrivers))
  281.             printf ("Drivers = %s\n", info.pszDrivers);
  282.         printf ("Time = %d\n", info.time);
  283.         printf ("Timeout = %d\n", info.usTimeOut);
  284.         }
  285.         #undef info
  286.         break;
  287.  
  288.     }
  289.     return 0;
  290. }
  291.  
  292.  
  293. int Delete (int iArg, PSZ pszName, PSZ pszComp)
  294. {
  295. int rc;
  296.  
  297.     switch (iArg)
  298.     {
  299.     case ARG_DRIVER:
  300.     case ARG_PRINTER:
  301.     case ARG_PORT:
  302.         printf ("You can only delete devices or queues\n");
  303.         return 1;
  304.  
  305.     case ARG_QUEUE:
  306.         rc = SplDeleteQueue (pszComp, pszName);
  307.         if (rc)
  308.         {
  309.         printf ("Error code %d\n", rc);
  310.         return rc;
  311.         }
  312.         break;
  313.  
  314.     case ARG_DEVICE:
  315.         rc = SplDeleteDevice (pszComp, pszName);
  316.         if (rc)
  317.         {
  318.         printf ("Error code %d\n", rc);
  319.         return rc;
  320.         }
  321.         break;
  322.  
  323.     }
  324.     return 0;
  325. }
  326.  
  327.  
  328.  
  329. int OpKind (char *arg)
  330. {
  331.  
  332.     if (stricmp (arg, "driver") == 0)
  333.     return ARG_DRIVER;
  334.     else if (stricmp (arg, "queue") == 0)
  335.     return ARG_QUEUE;
  336.     else if (stricmp (arg, "printer") == 0)
  337.     return ARG_PRINTER;
  338.     else if (stricmp (arg, "port") == 0)
  339.     return ARG_PORT;
  340.     else if (stricmp (arg, "device") == 0)
  341.     return ARG_DEVICE;
  342.     else
  343.     return 0;
  344. }
  345.  
  346. int main (int argc, char **argv)
  347. {
  348. int i;
  349.  
  350. int iArg = 0;
  351. BOOL fDelete = FALSE;
  352. PSZ pszComp = NULL;
  353. PSZ pszName = NULL;
  354.  
  355.     if (argc < 2)
  356.     usage ();
  357.  
  358.     for (i = 1 ; i < argc ; i++)
  359.     if (argv[i][0] == '/')
  360.         {
  361.         if (stricmp (argv[i]+1, "d") == 0)
  362.         {
  363.         if (fDelete)
  364.             usage ();
  365.         else
  366.             fDelete = TRUE;
  367.         }
  368.         else
  369.         usage ();
  370.         }
  371.     else if ( argv[i][0] == '\\' && argv[i][1] == '\\' )
  372.         {
  373.         if (pszComp)
  374.         usage ();
  375.         else
  376.         pszComp = argv[i];
  377.         }
  378.     else if (iArg == 0)
  379.         {
  380.         iArg = OpKind (argv[i]);
  381.         if (iArg == 0)
  382.         usage ();
  383.         }
  384.     else if (pszName == 0)
  385.         pszName = argv[i];
  386.     else
  387.         usage ();
  388.  
  389.     if (pszName == NULL)
  390.     {
  391.     if (fDelete == TRUE)
  392.         usage ();
  393.     else
  394.         return Enumerate (iArg, pszComp);
  395.     }
  396.     else if (fDelete)
  397.     return Delete (iArg, pszName, pszComp);
  398.     else
  399.     return Query (iArg, pszName, pszComp);
  400.  
  401.     return 0;
  402. }
  403.