/* * ClipTool (Udklipsværktøj) - A Commodities Exchange Application * Copyright (C) 1994 Torsten Poulin * * main.c - reads arguments and sets up system resources * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * The author can be contacted by mail at * Torsten Poulin * Banebrinken 99, 2, 77 * DK-2400 Copenhagen NV * Denmark * or via email: torsten@diku.dk * * Created 15-Jan-94 * $Log: main.c,v $ * Revision 1.1 94/02/20 21:28:43 Torsten * Initial revision * */ #include "cliptool.h" #define STRINGARRAY #include "ctstrings.h" #define NSTRINGS (sizeof AppStrings / sizeof(struct AppString)) #ifdef __AMIGADATE__ #define COMPDATE __AMIGADATE__ /* * __AMIGADATE__ is defined by SAS/C 6.51; it resolves to a datestamp * string in the format required for the AmigaDOS VERSION command. * The format is "(dd.mm.yy)", where dd is the day (1-31), * mm is the month (1-12), and yy is the year. * If it is unknown, we use the value below: */ #else #define COMPDATE "(22.1.94)" #endif /* Embedded strings for Version, Copyright, and ident */ static char const version[] = "$VER: ClipTool 38.1 " COMPDATE; static char const copyright[] = "$COPYRIGHT: ©1994 Torsten Poulin$"; static char const RCSid[] = "$Id: main.c,v 1.1 94/02/20 21:28:43 Torsten Exp $"; UBYTE *verstring = (UBYTE *) &version[6]; struct Library *IntuitionBase; struct Library *CxBase; struct Library *IconBase; struct Library *WorkbenchBase; struct Library *GadToolsBase; struct Library *GfxBase; struct Library *IFFParseBase; struct Library *LayersBase; struct Library *AslBase; struct Library *RexxSysBase; struct Library *LocaleBase; struct Window *guiwin; struct AppWindow *appwin; struct AppIcon *appicon; long xoffset, yoffset; struct MsgPort *broker_mp; struct MsgPort *appwin_mp; struct MsgPort *appicon_mp; struct MsgPort *arexx_mp; CxObj *broker; ULONG cxsigflag, awsigflag, aisigflag, rxsigflag; struct FileRequester *filereq; struct Catalog *catalog; BOOL nothidden; BOOL guiminimized; BOOL simplebuttons; BOOL createicons; BOOL usedropicon; long unitnumber; UBYTE *portname, *hotkey; /* Private variables */ static UBYTE defportname[] = "CLIPTOOL"; static UBYTE defhotkey[] = "control lalt c"; static BYTE cxpriority; /* Private functions */ static void setupbroker(void); static void getwbarguments(int, char **); static void getshellarguments(void); static void openres(void); static void closeres(int result); static void openrexx(void); static void closerexx(void); static void loadlocalestrings(void); void main(int argc, char **argv) { openres(); if (argc == 0) getwbarguments(argc, argv); else getshellarguments(); loadlocalestrings(); initnewmenustruct(); if (usedropicon) { opendropicon(); if (!appicon) usedropicon = FALSE; } setupbroker(); closeres(0); } /* * ClipTool knows the following arguments/tooltypes: * CX_PRIORITY/K/N (=0) * CX_POPKEY/K (=f1) * CX_POPUP/K (=yes|no) * EMBOSSED/K (=yes|no) * CREATEICONS/K (=yes|no) * UNIT/K/N (=0-255) * APPICON/K (=yes|no) * PORTNAME/K (=CLIPTOOL) * * The function below is also capable of handling shell arguments, * but the current trend is to use ReadArgs(). */ static void getwbarguments(int argc, char **argv) { UBYTE **tooltypes; UBYTE *s; tooltypes = ArgArrayInit(argc, argv); cxpriority = (BYTE) ArgInt(tooltypes, "CX_PRIORITY", 0); if (cxpriority < -128) cxpriority = -128; else if (cxpriority > 127) cxpriority = 127; s = ArgString(tooltypes, "CX_POPKEY", defhotkey); if (hotkey = malloc(strlen(s) + 1)) strcpy(hotkey, s); else hotkey = defhotkey; s = ArgString(tooltypes, "CX_POPUP", "yes"); if (stricmp(s, "yes") == 0) nothidden = TRUE; s = ArgString(tooltypes, "EMBOSSED", "yes"); if (stricmp(s, "yes") == 0) simplebuttons = FALSE; else simplebuttons = TRUE; s = ArgString(tooltypes, "CREATEICONS", "yes"); if (stricmp(s, "yes") == 0) createicons = TRUE; s = ArgString(tooltypes, "UNIT", "0"); unitnumber = atol(s); if (unitnumber < 0) unitnumber = 0; else if (unitnumber > 255) unitnumber = 255; s = ArgString(tooltypes, "APPICON", "yes"); if (stricmp(s, "yes") == 0) usedropicon = TRUE; s = ArgString(tooltypes, "PORTNAME", defportname); if (portname = malloc(strlen(s) + 1)) strcpy(portname, s); else portname = defportname; ArgArrayDone(); } static long shellargs[8]; static void getshellarguments(void) { struct RDArgs *rda; if (rda = ReadArgs("CX_PRIORITY/K/N,CX_POPKEY/K,CX_POPUP/K," "EMBOSSED/K,CREATEICONS/K,UNIT/K/N," "APPICON/K,PORTNAME/K", shellargs, NULL)) { if (shellargs[0]) { cxpriority = *(long *) shellargs[0]; if (cxpriority < -128) cxpriority = -128; else if (cxpriority > 127) cxpriority = 127; } hotkey = defhotkey; if (shellargs[1]) if (hotkey = malloc(strlen((UBYTE *) shellargs[1]) + 1)) strcpy(hotkey, (char *) shellargs[1]); else hotkey = defhotkey; nothidden = TRUE; if (shellargs[2]) if (stricmp((UBYTE *) shellargs[2], "no") == 0) nothidden = FALSE; if (shellargs[3]) if (stricmp((UBYTE *) shellargs[3], "no") == 0) simplebuttons = TRUE; createicons = TRUE; if (shellargs[4]) if (stricmp((UBYTE *) shellargs[4], "no") == 0) createicons = FALSE; if (shellargs[5]) { unitnumber = *(long *) shellargs[5]; if (unitnumber < 0) unitnumber = 0; else if (unitnumber > 255) unitnumber = 255; } usedropicon = TRUE; if (shellargs[6]) if (stricmp((UBYTE *) shellargs[6], "no") == 0) usedropicon = FALSE; portname = defportname; if (shellargs[7]) if (portname = malloc(strlen((UBYTE *) shellargs[7]) + 1)) strcpy(portname, (char *) shellargs[7]); else portname = defportname; FreeArgs(rda); } else { PrintFault(IoErr(), ls(MSG_CLIPTOOL)); closeres(20); } } /* * Open the necessary resources */ static void openres(void) { IntuitionBase = OpenLibrary("intuition.library", 37L); CxBase = OpenLibrary("commodities.library", 37L); IconBase = OpenLibrary("icon.library", 36L); WorkbenchBase = OpenLibrary("workbench.library", 37L); GadToolsBase = OpenLibrary("gadtools.library", 37L); GfxBase = OpenLibrary("graphics.library", 37L); IFFParseBase = OpenLibrary("iffparse.library", 37L); LayersBase = OpenLibrary("layers.library", 37L); AslBase = OpenLibrary("asl.library", 37L); RexxSysBase = OpenLibrary("rexxsyslib.library", 0L); LocaleBase = OpenLibrary("locale.library", 38L); if (LocaleBase) catalog = OpenCatalog(NULL, "cliptool.catalog", OC_BuiltInLanguage, (ULONG) "english", TAG_END); if (AslBase) filereq = (struct FileRequester *) AllocAslRequestTags(ASL_FileRequest, ASL_LeftEdge, 20, ASL_TopEdge, 30, TAG_END); if (!(IntuitionBase && CxBase && IconBase && WorkbenchBase && GadToolsBase && GfxBase && IFFParseBase && LayersBase && AslBase && filereq)) closeres(20); } /* * Close them again */ static void closeres(int result) { closedropicon(); if (guiwin) closegui(); if (filereq) FreeAslRequest(filereq); if (catalog) CloseCatalog(catalog); if (LocaleBase) CloseLibrary(LocaleBase); if (RexxSysBase) CloseLibrary(RexxSysBase); if (AslBase) CloseLibrary(AslBase); if (LayersBase) CloseLibrary(LayersBase); if (IFFParseBase) CloseLibrary(IFFParseBase); if (GfxBase) CloseLibrary(GfxBase); if (GadToolsBase) CloseLibrary(GadToolsBase); if (WorkbenchBase) CloseLibrary(WorkbenchBase); if (IconBase) CloseLibrary(IconBase); if (CxBase) CloseLibrary(CxBase); if (IntuitionBase) CloseLibrary(IntuitionBase); exit(result); } /* * Set up the Commodities Exchange broker and call handleevents(). */ static void setupbroker(void) { struct NewBroker ctbroker; CxObj *filter, *sender, *translate; CxMsg *msg; ctbroker.nb_Version = NB_VERSION; ctbroker.nb_Name = "ClipTool"; /* not localized */ ctbroker.nb_Title = verstring; ctbroker.nb_Descr = ls(MSG_DESCRIPTION); ctbroker.nb_Unique = NBU_UNIQUE | NBU_NOTIFY; ctbroker.nb_Flags = COF_SHOW_HIDE; ctbroker.nb_Pri = cxpriority; ctbroker.nb_ReservedChannel = 0; if (broker_mp = CreateMsgPort()) { ctbroker.nb_Port = broker_mp; cxsigflag = 1L << broker_mp->mp_SigBit; if (broker = CxBroker(&ctbroker, NULL)) { if (filter = CxFilter(hotkey)) { AttachCxObj(broker, filter); if (sender = CxSender(broker_mp, EVENT_HOTKEY)) { AttachCxObj(filter, sender); if (translate = CxTranslate(NULL)) { AttachCxObj(filter, translate); if (!CxObjError(filter)) { ActivateCxObj(broker, 1L); openrexx(); handleevents(); closerexx(); } } } } DeleteCxObjAll(broker); /* get rid of remaining CxMsgs */ while (msg = (CxMsg *) GetMsg(broker_mp)) ReplyMsg((struct Message *) msg); } DeleteMsgPort(broker_mp); } } static void openrexx(void) { if (RexxSysBase) { Forbid(); if (!FindPort(portname)) { if (arexx_mp = CreateMsgPort()) { arexx_mp->mp_Node.ln_Pri = 1L; arexx_mp->mp_Node.ln_Name = portname; AddPort(arexx_mp); } } Permit(); rxsigflag = 1 << arexx_mp->mp_SigBit; } } static void closerexx(void) { struct RexxMsg *rxmsg; UBYTE *s; if (arexx_mp) { RemPort(arexx_mp); while ((rxmsg = (struct RexxMsg *) GetMsg(arexx_mp))) { if (CheckRexxMsg((struct Message *) rxmsg)) { rxmsg->rm_Result1 = 20; rxmsg->rm_Result2 = (long) NULL; if (rxmsg->rm_Action & RXFF_RESULT) if (s = CreateArgstring("0", 1L)) rxmsg->rm_Result2 = (long) s; } ReplyMsg((struct Message *) rxmsg); } DeleteMsgPort(arexx_mp); } } /* * Make the look-up table point at the local strings, if possible. */ static void loadlocalestrings(void) { int i; if (catalog) for (i = 0; i < NSTRINGS; i++) AppStrings[i].as_Str = GetCatalogStr(catalog, i, AppStrings[i].as_Str); } STRPTR ls(LONG id) { return AppStrings[id].as_Str; }