home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d500 / wiconify.lha / wIconify / wUtilities.lzh / wMakeWB / wMakeWB.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  4KB  |  163 lines

  1. /*
  2.  *  WMAKEWB     A companion utility to wIconify that allows you to
  3.  *              specify any screen as the current Workbench screen.
  4.  *
  5.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  6.  *  You may use this code, provided this copyright notice is kept intact.
  7.  */
  8.  
  9.  
  10. #define INTUITION_PREFERENCES_H             /* don't need 'em */
  11. #include <intuition/intuitionbase.h>
  12.  
  13. #define USAGE   "wMakeWB [screen]"
  14.  
  15. static char *program = "wMakeWB";
  16. static char *version = "v1.2";
  17. static char *copyright =
  18.    "Copyright (C) 1990 by Davide P. Cervone, all rights reserved.";
  19.  
  20.  
  21. #define INTUITION_REV   0L
  22. extern struct IntuitionBase *IntuitionBase;
  23. extern struct IntuitionBase *OpenLibrary();
  24.  
  25.  
  26. #define TOUPPER(c)      (((c)>='a'&&(c)<='z')?(c)-'a'+'A':c)
  27.  
  28. /*
  29.  *  PrefixMatch()
  30.  *
  31.  *  Case-insensitive prefix match.  NULL pointers and empty strings
  32.  *  only match themselves, and are not considered prefixes to any string.
  33.  *
  34.  *  Return <0 if the first is smaller than the second,
  35.  *         =0 if the first is a prefix of the second,
  36.  *         >1 if the first is larger than the second.
  37.  */
  38.  
  39. int PrefixMatch(s1,s2)
  40. char *s1,*s2;
  41. {
  42.    int match = -1;
  43.    
  44.    if (s1 && *s1)
  45.    {
  46.       if (s2)
  47.       {
  48.          while (TOUPPER(*s1) == TOUPPER(*s2) && *s2 != 0) s1++,s2++;
  49.          if (*s1 == 0) match = 0; else match = *s1 - *s2;
  50.       } else match = 1;
  51.    } else if (s2 == NULL || *s1 == 0) match = 0;
  52.    return(match);
  53. }
  54.  
  55.  
  56. /*
  57.  *  *FindScreen()
  58.  *
  59.  *  Lock IntuitionBase so nothing happens while we look.
  60.  *  If there is a name to look for,
  61.  *    Start at the first screen, and look for a screen whose title
  62.  *      matches the given name.  If none, return NULL.
  63.  *  Otherwise, use the active screen.
  64.  *  Unlock Intuition.
  65.  *  Return the screen pointer found, if any.
  66.  */
  67.  
  68. static struct Screen *FindScreen(ScreenName)
  69. char *ScreenName;
  70. {
  71.    struct Screen *theScreen;
  72.    long ILock, LockIBase();
  73.  
  74.    ILock = LockIBase(0L);
  75.    if (ScreenName)
  76.    {
  77.       theScreen = IntuitionBase->FirstScreen;
  78.       while (theScreen && PrefixMatch(ScreenName,theScreen->Title))
  79.          theScreen = theScreen->NextScreen;
  80.    } else {
  81.       theScreen = IntuitionBase->ActiveScreen;
  82.    }
  83.    UnlockIBase(ILock);
  84.    return(theScreen);
  85. }
  86.  
  87.  
  88. /*
  89.  *  Print()
  90.  *
  91.  *  Find the standard AmigaDOS output file and write the output string
  92.  *  to the output file.  This is intended as a substitute for printf()
  93.  *  when no formatting is required, and when you want a small executable.
  94.  */
  95.  
  96. static void Print(s)
  97. char *s;
  98. {
  99.    ULONG OutFile;
  100.    extern ULONG Output();
  101.    
  102.    OutFile = Output();
  103.    if (OutFile && s) Write(OutFile,s,strlen(s));
  104. }
  105.  
  106.  
  107. /*
  108.  *  Error()
  109.  *
  110.  *  Print the character string, and up to two optional strings, then
  111.  *  go to a new output line, close Intuition, and exit with an error.
  112.  */
  113.  
  114. static void Error(s,x1,x2)
  115. char *s,*x1,*x2;
  116. {
  117.    Print(s);
  118.    if (x1) Print(x1);
  119.    if (x2) Print(x2);
  120.    Print("\n");
  121.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  122.    _exit(10L);
  123. }
  124.  
  125.  
  126. /*
  127.  *  main()
  128.  *
  129.  *  If there are too many arguments, print the Usage message.
  130.  *  If a screen name is given, use it.
  131.  *  Open Intuition; if successful,
  132.  *    Find the specified screen.
  133.  *    If found, then
  134.  *      If wIconify is running, make the screen the WB screen.
  135.  *      Otherwise say that wIconify is not running.
  136.  *    Otherwise say that the screen can't be found.
  137.  *    Close Intuition.
  138.  *  Otherwise say that Intuition can't be openned.
  139.  */
  140.  
  141. void main(argc,argv)
  142. int argc;
  143. char *argv[];
  144. {
  145.    char *ScreenName = NULL;
  146.    struct Screen *theScreen;
  147.  
  148.    if (argc > 2) Error("Usage:  ",USAGE,NULL);
  149.    if (argc > 1 && argv[1]) ScreenName = argv[1];
  150.  
  151.    IntuitionBase = OpenLibrary("intuition.library",INTUITION_REV);
  152.    if (IntuitionBase)
  153.    {
  154.       theScreen = FindScreen(ScreenName);
  155.       if (theScreen)
  156.       {
  157.          if (wIconifyActive()) wMakeWB(theScreen);
  158.           else Error("wIconify not running or incompatible version",NULL,NULL);
  159.       } else Error("Can't find screen '",ScreenName,"'");
  160.       CloseLibrary(IntuitionBase);
  161.    } else Error("Can't Open Intuition Library!",NULL,NULL);
  162. }
  163.