home *** CD-ROM | disk | FTP | other *** search
- /*-- Rev Header - do NOT edit!
- *
- * Filename : Inc.cc
- * Purpose : Increments an environment variable
- *
- * Program : Inc
- * Author : Gerhard Müller
- * Copyright: (c) by Gerhard Müller
- * Creation : Sun Nov 21 20:40:43 1993
- *
- * compile : makefile
- *
- *
- * Compile version : 0.1
- * Ext. Version : 0.1
- *
- * REVISION HISTORY
- *
- * Date C-Version E-Version Comment
- * --------- --------- ----------- -------
- * 21.11.93 work started
- *
- *
- *
- *-- REV_END --
- */
-
- /*
- * C-Includes, C-Definitionen
- *
- */
-
- #define class _class
- #define template _template
-
- extern "C" {
- #include <exec/types.h>
- #include <exec/ports.h>
- #include <exec/memory.h>
- #include <exec/execbase.h>
- #include <dos/dos.h>
- #include <dos/dosextens.h>
- #include <dos/exall.h>
- #include <dos/rdargs.h>
- #include <clib/alib_protos.h>
- #include <clib/alib_stdio_protos.h>
- #include <inline/stubs.h>
- #include <clib/dos_protos.h>
- #ifdef __OPTIMIZE__
- #include <inline/exec.h>
- #include <inline/dos.h>
- #include <inline/utility.h>
- #else
- #include <clib/exec_protos.h>
- #include <clib/dos_protos.h>
- #include <clib/utility_protos.h>
- #endif
-
- int atexit(void (*)(void));
- }
-
- #undef template
- #undef class
-
-
- extern "C" {
-
- /* dont mangle our global symbols */
-
- //Library *UtilityBase;
-
- extern ExecBase *SysBase;
-
- extern BPTR stdin;
- extern BPTR stdout;
- }
-
-
- /*
- * C++-Includes, C++-Definitionen
- *
- */
-
- #include "add/baserel/add.h" // prototypes for all new C-functions
- #include "add/baserel/OwnError.h" // defines err
-
-
- /*
- * our function-prototypes
- *
- */
-
- void OutputError(void);
- void MyExit();
- BOOL MyInit(int alen, char *aptr);
- int main(int alen, char *aptr);
-
-
- /* Version-String */
-
- char *version = "\0$VER: Inc 0.01 (" __DATE__ ")";
-
-
-
- /* things for commandline-parsing */
-
- #define TEMPLATE "VARNAME,LOCAL/S,GLOBAL/S,FILE/K,OUTPUT/S"
-
- typedef struct { UBYTE *varname; ULONG local; ULONG global; UBYTE *file; ULONG output;} ARGS;
- #define DEFAULT { NULL, FALSE, FALSE, NULL, FALSE }
-
- ARGS args = DEFAULT; /* here the arguments are stored */
-
- struct RDArgs *rda = NULL; /* for CLI-startup */
- UBYTE **ToolTypes; /* for WB-startup */
-
-
-
-
-
- /*
- * This function gets called by exit(), look im MyInit() for corresponding atexit() call
- * frees all system-depended things
- */
-
-
- void MyExit()
- {
- /* Free arguments passed by commandline allocated by ReadArgs() */
-
- if( rda )
- {
- FreeArgs( rda );
- FreeDosObject( DOS_RDARGS, rda );
- }
-
-
- /* close all our libraries */
-
- // if(UtilityBase)
- // CloseLibrary(UtilityBase);
-
- }
-
-
- /*
- * Open some libraries and make the commandline-parsing
- *
- */
-
- BOOL MyInit(int alen, char *aptr)
- {
- BOOL ok=FALSE;
-
- /* Are we running under control of Kickstart 2.0 or higher? */
-
- if(SysBase -> LibNode . lib_Version < 37)
- {
- err.NotifyError("needs Workbench & Kickstart Version 2.04 and above !\n");
- return FALSE;
- }
-
-
- atexit(MyExit); // the function MyExit should be called at exit()-time
-
-
- /* open some libraries, not really needed in this programm, just to show how to do */
-
- // if(UtilityBase = OpenLibrary((UBYTE *)"utility.library",37))
- // {
- // // we have all our libraries
- // ok=TRUE;
- // }
-
- // if(!ok) return FALSE;
-
-
- /* normal startup-code.... */
-
- ok=FALSE;
-
- if(alen)
- {
- /* start from CLI */
- /* parse arguements */
-
- if(rda = (RDArgs *)AllocDosObject( DOS_RDARGS, NULL ))
- {
- rda->RDA_ExtHelp = (UBYTE *)"Increments the contents of an environment variable or file\n";
-
- if(ReadArgs( (UBYTE *)TEMPLATE, (LONG *) &args, rda ) )
- {
- /* Args erfolgreich gelesen */
-
- ok=TRUE;
- }
- else
- {
- err.DosError(IoErr(), "Error parsing commandline"); /* prints the appropriate err message */
- }
- }
- else
- err.NotifyError("Out of memory error !");
- }
- else
- {
- /* Workbench-Start */
-
- err.NotifyError("Start only from CLI !");
-
- ok=FALSE;
- }
-
- return ok;
- }
-
-
-
- /*
- * Here the testprogram starts, uses some misc features
- *
- */
-
-
- int main(int alen, char *aptr)
- {
- if(MyInit(alen,aptr)) // all things went well
- {
- BPTR fh;
-
- if (args.varname)
- {
- ULONG flags=0;
- if(args.local)
- {
- flags=GVF_LOCAL_ONLY;
- }
- else
- {
- if (args.global)
- {
- flags=GVF_GLOBAL_ONLY;
- }
- }
-
- UBYTE Buffer[32];
-
- LONG len=GetVar(args.varname,Buffer,31,flags);
-
- Buffer[31]=0;
-
- if(len==-1)
- {
- /* Error */
- err.NotifyError("Can't access variable!");
- }
- else
- {
- volatile LONG zahl=0;
- StrToLong(Buffer, &zahl);
-
- zahl=zahl+1;
-
- sprintf(Buffer, (UBYTE *)"%ld\n", zahl);
-
- SetVar(args.varname, Buffer, strlen((char *)Buffer), flags);
-
- if(args.output) Printf((UBYTE *)"%ld\n",zahl);
- }
- }
- else
- {
- /* Filename ? */
-
- if (args.file)
- {
- if(fh=Open(args.file,MODE_OLDFILE))
- {
- UBYTE Buffer[32];
-
- LONG len=Read(fh, Buffer, 31);
-
- if(len>0 && len < 32)
- {
- Buffer[31]=0;
-
- volatile LONG zahl=0;
- StrToLong(Buffer, &zahl);
-
- zahl++;
-
- sprintf(Buffer, (UBYTE *)"%ld\n", zahl);
-
- Close(fh);
-
- if(fh=Open(args.file,MODE_NEWFILE))
- {
- Write(fh, Buffer, strlen((char *)Buffer));
-
- Close(fh);
-
- if(args.output) Printf((UBYTE *)"%ld\n",zahl);
-
- }
- else
- err.NotifyError("Can't open file for writing !");
- }
- else
- err.NotifyError("File contains no variable !");
- }
- else
- err.NotifyError("Can't open file for reading !");
-
- }
- else
- err.NotifyError("No variable name or Filename given !");
-
- }
- } // MyInit()
-
-
- // if an error occurred...
-
- OutputError();
-
- return(0); // exit is called if we fall through
- }
-
-
-
-
- /*
- * Output error-message if we have one
- *
- */
-
-
- void OutputError(void)
- {
- if(err)
- {
- char *err_mem=0;
- char *es;
- LONG err_nr=err.SetErrorNumber(0);
-
- if(es=err.have_error_string())
- {
- Printf((UBYTE *)"%s\n", es);
- }
- else
- {
- if(err_nr)
- {
- Printf((UBYTE *)"Error-Number: %ld\n", err_nr);
- }
- }
- }
- }
-