AmiTimeKeeper/main.c

158 lines
4.2 KiB
C

/*-
* Copyright (c) 2017-2020 Carsten Sonne Larsen <cs@innolan.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "config.h"
#include "global.h"
#include "setting.h"
#include "timer.h"
#include "net.h"
#include "mem.h"
#include "ptz.h"
#include "tz.h"
#include <workbench/startup.h>
#include "logmod.h"
#define MODULENAME "Main"
const char *vers = "\0$VER: " APP_ID;
static const char *template = KEYWORD_TEMPLATE;
static void GetCliSettings(void);
static void GetWbSettings(struct WBStartup *);
static void ShowLocalTime(void)
{
char *time = GetTimeText(AppLocale, LOCAL_TIME_NOW);
LogInfo("Local time is %s", time);
FreeMemSafe(time);
}
int main(int argc, char **argv)
{
InitMemSafe();
InitSettings();
LogInfo("%s", APP_TITLE);
if (OpenLibraries() != 0)
{
CloseLibraries();
CleanupSettings();
FreeAllSafe();
return RETURN_ERROR;
}
InitTimezone();
LoadSettings();
if (argc != 0)
GetCliSettings();
else
GetWbSettings((struct WBStartup *)argv);
ApplySettings();
SanitizeSettings();
ShowSettings();
StartBroker();
ShowLocalTime();
CleanupTimezone();
CloseSocketLibrary();
CloseLibraries();
CleanupSettings();
FreeAllSafe();
return RETURN_OK;
}
static void GetCliSettings(void)
{
struct AppSettings *settings;
struct RDArgs *inArgs;
long args[KEYWORD_COUNT];
int i;
for (i = 0; i < KEYWORD_COUNT; i++)
args[i] = 0;
settings = CreateSettings(CliSettingType);
inArgs = ReadArgs((void *)template, (void *)&args, NULL);
if (inArgs)
{
// Keyword order in template needs to match order in settingFunctions
for (i = 0; i < KEYWORD_COUNT; i++)
{
if (args[i] != 0)
{
settingFunctions[i].Function(settings, (void *)args[i]);
}
}
FreeArgs(inArgs);
}
CacheSettings(settings);
}
static void GetWbSettings(struct WBStartup *wbs)
{
struct AppSettings *settings;
struct DiskObject *diskObject;
STRPTR filename, arg;
BPTR oldDir;
int argNo, i;
filename = (STRPTR)AllocStringSafe(MAXFILEPATHLEN);
settings = CreateSettings(WbSettingType);
for (argNo = 0; argNo < wbs->sm_NumArgs; ++argNo)
{
if (wbs->sm_ArgList[argNo].wa_Lock != NULL)
{
oldDir = CurrentDir(wbs->sm_ArgList[argNo].wa_Lock);
diskObject = GetDiskObjectNew((void *)wbs->sm_ArgList[argNo].wa_Name);
if (diskObject)
{
for (i = 0; i < KEYWORD_COUNT; i++)
{
arg = FindToolType(diskObject->do_ToolTypes, (STRPTR)settingFunctions[i].Name);
if (arg)
{
settingFunctions[i].Function(settings, (char *)arg);
}
}
FreeDiskObject(diskObject);
}
CurrentDir(oldDir);
}
}
CacheSettings(settings);
FreeMemSafe(filename);
}