home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / misc / tstpubscr.lha / TstPubScr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  8.5 KB  |  320 lines

  1. /*
  2.     <TABSTEP=3>
  3.  
  4.     TstPubScr V1
  5.  
  6.     Tests whether there's a specific public-screen out there.
  7.     Returns AmigaOS Errorlevel RETURN_ERROR(==10) if not found !
  8.  
  9.     TstPubScr PUBSCRNAME,FORMAT/K,DEFAULT/S,FORCE/S,BATCH/S,LOCK=UNLOCK/S
  10.  
  11.     PUBSCRNAME        :    Name of the pubscreen you want to access.
  12.                             If found, its name will be printed to stdout
  13.                             and the prog returns RETURN_OK(==0).
  14.                             If this screen couldn't be found, a blank "" will be
  15.                             return (indicates default pubscreen) and returncode
  16.                             RETURN_WARN(==5) will be returned.
  17.                             You may disable this feature by FORCE:
  18.     FORCE                :    Do not print out alternative name of the default pubscr.
  19.     DEFAULT            :    Get name of default publicscreen ("").
  20.     FORMAT            :    Pattern to format output (include '%s' for the name of
  21.                             the publicscreen found).
  22.                             Note that the name of the public screen will always be
  23.                             quoted !
  24.     BATCH                :    Does not print out any information except the requested
  25.                             name (thus you won't ever have any trash in an env.-
  26.                             variable).
  27.     LOCK & UNLOCK    :    This switch says TstPubScr to keep a lock on the related
  28.                             publicscreen.
  29.                             Call TstPubScr twice (with the same parameters; you may
  30.                             replace 'LOCK' by 'UNLOCK' !) to unlock that specific screen.
  31.                             Uses ENV: to store address of the screens locked !
  32.  
  33.     No parameter    :    List names of all public screens.
  34.  
  35.     (c)6.3.1995 by Hans Bühler, Codex Design Software
  36. */
  37.  
  38. #include    <exec/types.h>
  39. #include    <exec/exec.h>
  40. #include    <dos/dos.h>
  41. #include    <dos/rdargs.h>
  42. #include    <intuition/intuition.h>
  43. #include    <intuition/screens.h>
  44. #include    <stdio.h>
  45. #include    <string.h>
  46. #include    <stdlib.h>
  47.  
  48. #include    <proto/exec.h>
  49. #include    <proto/dos.h>
  50. #include    <proto/intuition.h>
  51.  
  52. // ------------------------------------
  53. // defines
  54. // ------------------------------------
  55.  
  56. #define    PROGNAME            "TstPubScr V1.00"
  57. #define    NAMELEN            40
  58. #define    CONDEFLEN        255
  59. #define    NAMEPATTERN        "TstPubScr: pubscr_$%08lx_lock"
  60.  
  61. /* offsets */
  62. #define    ARG_PUBSCRNAME    0
  63. #define    ARG_FORMAT        1
  64. #define    ARG_DEFAULT        2
  65. #define    ARG_FORCE        3
  66. #define    ARG_BATCH        4
  67. #define    ARG_LOCK            5
  68. #define    ARG_NUM            6
  69.  
  70. /* defaults */
  71. #define    DEF_PUBSCRNAME    0
  72. #define    DEF_FORMAT        (LONG)("%s")
  73. #define    DEF_DEFAULT        FALSE
  74. #define    DEF_FORCE        FALSE
  75. #define    DEF_BATCH        FALSE
  76. #define    DEF_LOCK            FALSE
  77.  
  78. #define    DEF_CONDEF        "CON:100/50/400/100/ " PROGNAME ": public screen list/AUTO/CLOSE"
  79.  
  80. /* some shortcuts */
  81. #define    Eprintf            if(!Args[ARG_BATCH]) printf
  82.  
  83. // ------------------------------------
  84. // types
  85. // ------------------------------------
  86.  
  87. struct NewPort        {    struct MsgPort        Port;
  88.                             char                    Name[NAMELEN+1];
  89.                         };
  90.  
  91. struct NodeCopy    {    struct NodeCopy    *Next;
  92.                             char                    Name[MAXPUBSCREENNAME+2];
  93.                         };
  94.  
  95. // ------------------------------------
  96. // proto
  97. // ------------------------------------
  98.  
  99. int    PrintPubScrList(void);
  100.  
  101. // ------------------------------------
  102. // global
  103. // ------------------------------------
  104.  
  105. static char                CmdLine[]        =    "PUBSCRNAME,"
  106.                                                     "FORMAT/K,"
  107.                                                     "DEFAULT/S,"
  108.                                                     "FORCE/S,"
  109.                                                     "BATCH/S,"
  110.                                                     "LOCK=UNLOCK/S";
  111.  
  112. static LONG                Args[ARG_NUM]    =    {    DEF_PUBSCRNAME,
  113.                                                         DEF_FORMAT,
  114.                                                         DEF_DEFAULT,
  115.                                                         DEF_FORCE,
  116.                                                         DEF_BATCH,
  117.                                                         DEF_LOCK
  118.                                                     };
  119.  
  120. static const char        HelpTxt[]        =    PROGNAME " (w)6.3.1995 by Hans Bühler, Codex Design Software\n"
  121.                                                     "\n"
  122.                                                     " This programm is able to find out whether a specific (or default)\n"
  123.                                                     "public screen is known to your system.\n"
  124.                                                     " It is dedicated for use in batch scripts.\n"
  125.                                                     "\n"
  126.                                                     "PUBSCRNAME : Name of public screen you want to find.\n"
  127.                                                     "             If found, RETURN_OK (0) is returned and its\n"
  128.                                                     "             name will be written to stdout.\n"
  129.                                                     "             In other cases, RETURN_WARN (5) will be returned\n"
  130.                                                     "             and the default pub name will be put out (\"\").\n"
  131.                                                     "FORMAT     : Output format string. Use '%s' for pubname.\n"
  132.                                                     "             Name will be quoted.\n"
  133.                                                     "DEFAULT    : Get default pubscreens name (\"\").\n"
  134.                                                     "FORCE      : Do not put out the default public screen name if\n"
  135.                                                     "             PUBSCRNAME is unknown to your system.\n"
  136.                                                     "BATCH      : Supress all output except the pub name.\n"
  137.                                                     "(UN)LOCK   : Lock screen. You MUST unlock it later: Use the same\n"
  138.                                                     "             cmdline (you may replace 'LOCK' by 'UNLOCK', but that\n"
  139.                                                     "             really wouldn't matter...), and run the program again.\n"
  140.                                                     "\n"
  141.                                                     " For further information, refer to the manual.\n"
  142.                                                     "This program is freeware. Source available.\n"
  143.                                                     "Suggestions/questions/etc. to:\n"
  144.                                                     " e-mail  h0348kil@rz.hu-berlin.de\n"
  145.                                                     " phone   ++49 (0)30 3963374\n";
  146.  
  147. static struct RDArgs    SetArgs            =    {    {    0,0,0    },
  148.                                                         0,0,0,HelpTxt,0
  149.                                                     };
  150.  
  151. char    __stdiowin[CONDEFLEN+1]            =    DEF_CONDEF;
  152.  
  153. // ------------------------------------
  154. // fkt.
  155. // ------------------------------------
  156.  
  157. int main(int argc, char **argp)
  158. {
  159.     struct RDArgs    *rdargs;
  160.     struct Screen    *pubscr;
  161.     char                *pubname,
  162.                         portname[MAXPUBSCREENNAME+3];
  163.     struct NewPort    *port;
  164.     int                error        =    RETURN_OK;
  165.  
  166.     if( rdargs = ReadArgs(CmdLine,Args,&SetArgs))
  167.     {
  168.         if(!Args[ARG_PUBSCRNAME] && !Args[ARG_DEFAULT])
  169.             error    =    PrintPubScrList();
  170.         else
  171.         {
  172.             pubname    =    (char *)(Args[ARG_DEFAULT] ? 0 : Args[ARG_PUBSCRNAME]);
  173.  
  174.             if(!(pubscr = LockPubScreen(pubname)))
  175.             {
  176.                 if(!Args[ARG_DEFAULT])
  177.                     error    =    RETURN_WARN;
  178.  
  179.                 if(!Args[ARG_FORCE] && pubname)
  180.                     pubscr = LockPubScreen(pubname = 0);
  181.             }
  182.  
  183.             if(!pubscr)
  184.             {
  185.                 Eprintf(    "ERROR: Can't find public-screen '%s' !\n",
  186.                             Args[ARG_PUBSCRNAME] ? (char *)Args[ARG_PUBSCRNAME] : "<default>");
  187.                 error    =    RETURN_ERROR;
  188.             }
  189.             else
  190.             {
  191.                 if(Args[ARG_LOCK])
  192.                 {
  193.                     sprintf(portname,NAMEPATTERN,pubscr);
  194.  
  195.                     if(port = (struct NewPort *)FindPort(portname))        // already locked ?
  196.                     {
  197.                         RemPort((struct MsgPort *)port);
  198.                         FreeVec(port);
  199.  
  200.                         UnlockPubScreen(0,pubscr);            // unlock twice !
  201.                     }
  202.                     else
  203.                         if(port = AllocVec(sizeof(struct NewPort),MEMF_CLEAR|MEMF_PUBLIC))
  204.                         {
  205.                             port->Port.mp_Flags            =    PA_IGNORE;
  206.                             port->Port.mp_Node.ln_Name    =    port->Name;
  207.                             strcpy(port->Name,portname);
  208.                             AddPort((struct MsgPort *)port);
  209.  
  210.                             LockPubScreen(pubname);            // lock it twice !
  211.                         }
  212.                         else
  213.                             Eprintf(    "ERROR: Can't lock publicscreen; not enough\n"
  214.                                         " memory (%ld bytes) to store lock !\n",
  215.                                         sizeof(struct NewPort));
  216.                 }
  217.  
  218.                 sprintf(portname,"\"%s\"",pubname);
  219.                 printf((char *)Args[ARG_FORMAT],portname);
  220.                 UnlockPubScreen(0,pubscr);
  221.             }
  222.         }
  223.  
  224.         FreeArgs(rdargs);
  225.     }
  226.     else
  227.     {
  228.         if(!Args[ARG_BATCH])
  229.             PrintFault(IoErr(),"ReadArgs() error: ");
  230.  
  231.         error    =    RETURN_FAIL;
  232.     }
  233.  
  234.  
  235.     /* shut down */
  236.  
  237.     exit(error);
  238. }
  239.  
  240. // ------------------------------------
  241. // additional funx
  242. // ------------------------------------
  243.  
  244. /******************************************************/
  245. /* Function made to browse through the pubscreen list */
  246. /* --------------------------------------------------    */
  247. /* return:                        RETURN_xxx code                */
  248. /******************************************************/
  249.  
  250. int PrintPubScrList(void)
  251. {
  252.     struct NodeCopy        *first,*last,*copy;
  253.     struct PubScreenNode    *pubnode;
  254.     struct List                *publist;
  255.     char                        buf[MAXPUBSCREENNAME+3];
  256.     BOOL                        MemErr    =    FALSE;
  257.     int                        ret        =    RETURN_OK;
  258.  
  259.     first    =    last    =    0;
  260.  
  261.     if(publist = LockPubScreenList())
  262.     {
  263.         // -- load list names --
  264.  
  265.         for(    pubnode = (struct PubScreenNode *)publist->lh_Head;
  266.                 pubnode->psn_Node.ln_Succ;
  267.                 pubnode = (struct PubScreenNode *)pubnode->psn_Node.ln_Succ)
  268.             if(!(pubnode->psn_Flags & PSNF_PRIVATE))
  269.             {
  270.                 if( copy = AllocVec(sizeof(struct NodeCopy),MEMF_CLEAR|MEMF_PUBLIC) )
  271.                 {
  272.                     if(last)
  273.                         last->Next    =    copy;
  274.                     else
  275.                         first            =    copy;
  276.  
  277.                     last                =    copy;
  278.  
  279.                     strncpy(copy->Name,pubnode->psn_Node.ln_Name,MAXPUBSCREENNAME);
  280.                 }
  281.                 else
  282.                 {
  283.                     MemErr    =    TRUE;
  284.                     break;
  285.                 }
  286.             }
  287.  
  288.         UnlockPubScreenList();
  289.  
  290.         // -- put out names --
  291.  
  292.         Eprintf("PubScreens known to your system:\n\n");
  293.  
  294.         while(first)
  295.         {
  296.             sprintf(buf,"\"%s\"",first->Name);
  297.             printf((char *)Args[ARG_FORMAT],buf);
  298.             putchar('\n');
  299.  
  300.             copy    =    first;
  301.             first    =    first->Next;
  302.  
  303.             FreeVec(copy);
  304.         }
  305.  
  306.         if(MemErr)
  307.         {
  308.             Eprintf("ERROR: Can't load further pubscrnames; out of memory !\n");
  309.             ret    =    RETURN_ERROR;
  310.         }
  311.     }
  312.     else
  313.     {
  314.         Eprintf("ERROR: Can't lock pubscreenlist !\n");
  315.         ret    =    RETURN_FAIL;
  316.     }
  317.  
  318.     return ret;
  319. }
  320.