BACK to addon.html#config_edit

/*   -*- buffer-read-only: t -*- vi: set ro:
 *  
 *  DO NOT EDIT THIS FILE   (edit-daemon-conf.c)
 *  
 *  It has been AutoGen-ed  Sunday October  5, 2008 at 12:56:24 PM PDT
 *  From the definitions    stdin
 *  and the template file   options
 *
 * Generated from AutoOpts 32:0:7 templates.
 */

/*
 *  This file was produced by an AutoOpts template.  AutoOpts is a
 *  copyrighted work.  This source file is not encumbered by AutoOpts
 *  licensing, but is provided under the licensing terms chosen by the
 *  edit-daemon-conf author or copyright holder.  AutoOpts is licensed under
 *  the terms of the LGPL.  The redistributable library (``libopts'') is
 *  licensed under the terms of either the LGPL or, at the users discretion,
 *  the BSD license.  See the AutoOpts and/or libopts sources for details.
 *
 * This source file is copyrighted and licensed under the following terms:
 *
 * edit-daemon-conf copyright 2008 Bruce Korb - all rights reserved
 *
 * edit-daemon-conf 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 3 of the License, or
 * (at your option) any later version.
 * 
 * edit-daemon-conf 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, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <limits.h>
#include <stdio.h>

#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define OPTION_CODE_COMPILE 1
#include "edit-daemon-conf.h"

#ifdef  __cplusplus
extern "C" {
#endif

/* TRANSLATORS: choose the translation for option names wisely because you
                cannot ever change your mind. */
tSCC zCopyright[] =
       "edit-daemon-conf copyright (c) 2008 Bruce Korb, all rights reserved";
tSCC zCopyrightNotice[620] =
"edit-daemon-conf is free software: you can redistribute it and/or modify it\n\
under the terms of the GNU General Public License as published by the Free\n\
Software Foundation, either version 3 of the License, or (at your option) any\n\
later version.\n\n\
edit-daemon-conf is distributed in the hope that it will be useful, but WITHOUT\n\
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\n\
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.\n\n\
You should have received a copy of the GNU General Public License along with\n\
this program.  If not, see <http://www.gnu.org/licenses/>.";

extern tUsageProc optionUsage;

#ifndef NULL
#  define NULL 0
#endif
#ifndef EXIT_SUCCESS
#  define  EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#  define  EXIT_FAILURE 1
#endif
/*
 *  Pid option description:
 */
tSCC    zPidText[] =
        "pid of daemon process";
tSCC    zPid_NAME[]                = "PID";
tSCC    zPid_Name[]                = "pid";
#define PID_FLAGS       (OPTST_DISABLED | OPTST_NO_INIT \
        | OPTST_SET_ARGTYPE(OPARG_TYPE_NUMERIC))

/*
 *  Pid_File option description:
 */
tSCC    zPid_FileText[] =
        "file containing daemon pid";
tSCC    zPid_File_NAME[]           = "PID_FILE";
tSCC    zPid_File_Name[]           = "pid-file";
#define PID_FILE_FLAGS       (OPTST_DISABLED | OPTST_NO_INIT \
        | OPTST_SET_ARGTYPE(OPARG_TYPE_FILE))

/*
 *  No_Sighup option description:
 */
tSCC    zNo_SighupText[] =
        "do not send SIGHUP to daemon";
tSCC    zNo_Sighup_NAME[]          = "NO_SIGHUP";
tSCC    zNo_Sighup_Name[]          = "no-sighup";
#define NO_SIGHUP_FLAGS       (OPTST_DISABLED | OPTST_NO_INIT)

/*
 *  Text option description:
 */
tSCC    zTextText[] =
        "string to emit on each iteration";
tSCC    zText_NAME[]               = "TEXT";
tSCC    zText_Name[]               = "text";
tSCC    zTextDefaultArg[]            = "default daemon text";
#define TEXT_FLAGS       (OPTST_DISABLED \
        | OPTST_SET_ARGTYPE(OPARG_TYPE_STRING))

/*
 *  Help/More_Help/Version option descriptions:
 */
tSCC zHelpText[]          = "Display usage information and exit";
tSCC zHelp_Name[]         = "help";
tSCC zMore_HelpText[]     = "Extended usage information passed thru pager";
tSCC zMore_Help_Name[]    = "more-help";
tSCC zVersionText[]       = "Output version information and exit";
tSCC zVersion_Name[]      = "version";
tSCC zResetText[]         = "Reset an option's state";
tSCC zReset_Name[]        = "reset-option";
tSCC zSave_OptsText[]     = "Save the option state to a config file";
tSCC zSave_Opts_Name[]    = "save-opts";
tSCC zLoad_OptsText[]     = "Load options from a config file";
tSCC zLoad_Opts_NAME[]    = "LOAD_OPTS";
tSCC zNotLoad_Opts_Name[] = "no-load-opts";
tSCC zNotLoad_Opts_Pfx[]  = "no";
#define zLoad_Opts_Name   (zNotLoad_Opts_Name + 3)
/*
 *  Declare option callback procedures
 */
extern tOptProc
    optionNumericVal, optionPagedUsage, optionPrintVersion, optionResetOpt;
static tOptProc
    doOptPid_File, doUsageOpt;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  Define the Edit_Daemon_Conf Option Descriptions.
 */
static tOptDesc optDesc[ OPTION_CT ] = {
  {  /* entry idx, value */ 0, VALUE_OPT_PID,
     /* equiv idx, value */ NOLIMIT, NOLIMIT,
     /* equivalenced to  */ INDEX_OPT_PID_FILE,
     /* min, max, act ct */ 0, 1, 0,
     /* opt state flags  */ PID_FLAGS, 0,
     /* last opt argumnt */ { NULL },
     /* arg list/cookie  */ NULL,
     /* must/cannot opts */ NULL, NULL,
     /* option proc      */ optionNumericVal,
     /* desc, NAME, name */ zPidText, zPid_NAME, zPid_Name,
     /* disablement strs */ NULL, NULL },

  {  /* entry idx, value */ 1, VALUE_OPT_PID_FILE,
     /* equiv idx, value */ NO_EQUIVALENT, 0,
     /* equivalenced to  */ NO_EQUIVALENT,
     /* min, max, act ct */ 0, 1, 0,
     /* opt state flags  */ PID_FILE_FLAGS, 0,
     /* last opt argumnt */ { NULL },
     /* arg list/cookie  */ NULL,
     /* must/cannot opts */ NULL, NULL,
     /* option proc      */ doOptPid_File,
     /* desc, NAME, name */ zPid_FileText, zPid_File_NAME, zPid_File_Name,
     /* disablement strs */ NULL, NULL },

  {  /* entry idx, value */ 2, VALUE_OPT_NO_SIGHUP,
     /* equiv idx, value */ NOLIMIT, NOLIMIT,
     /* equivalenced to  */ INDEX_OPT_PID_FILE,
     /* min, max, act ct */ 0, 1, 0,
     /* opt state flags  */ NO_SIGHUP_FLAGS, 0,
     /* last opt argumnt */ { NULL },
     /* arg list/cookie  */ NULL,
     /* must/cannot opts */ NULL, NULL,
     /* option proc      */ NULL,
     /* desc, NAME, name */ zNo_SighupText, zNo_Sighup_NAME, zNo_Sighup_Name,
     /* disablement strs */ NULL, NULL },

  {  /* entry idx, value */ 3, VALUE_OPT_TEXT,
     /* equiv idx, value */ 3, VALUE_OPT_TEXT,
     /* equivalenced to  */ NO_EQUIVALENT,
     /* min, max, act ct */ 0, 1, 0,
     /* opt state flags  */ TEXT_FLAGS, 0,
     /* last opt argumnt */ { zTextDefaultArg },
     /* arg list/cookie  */ NULL,
     /* must/cannot opts */ NULL, NULL,
     /* option proc      */ NULL,
     /* desc, NAME, name */ zTextText, zText_NAME, zText_Name,
     /* disablement strs */ NULL, NULL },

  {  /* entry idx, value */ INDEX_OPT_RESET_OPTION, VALUE_OPT_RESET_OPTION,
     /* equiv idx value  */ NO_EQUIVALENT, 0,
     /* equivalenced to  */ NO_EQUIVALENT,
     /* min, max, act ct */ 0, 1, 0,
     /* opt state flags  */ OPTST_SET_ARGTYPE(OPARG_TYPE_STRING)
			  | OPTST_NO_INIT, 0,
     /* last opt argumnt */ { NULL },
     /* arg list/cookie  */ NULL,
     /* must/cannot opts */ NULL,  NULL,
     /* option proc      */ optionResetOpt,
     /* desc, NAME, name */ zResetText, NULL, zReset_Name,
     /* disablement strs */ NULL, NULL },

#ifdef NO_OPTIONAL_OPT_ARGS
#  define VERSION_OPT_FLAGS     OPTST_IMM | OPTST_NO_INIT
#else
#  define VERSION_OPT_FLAGS     OPTST_SET_ARGTYPE(OPARG_TYPE_STRING) | \
                                OPTST_ARG_OPTIONAL | OPTST_IMM | OPTST_NO_INIT
#endif

  {  /* entry idx, value */ INDEX_OPT_VERSION, VALUE_OPT_VERSION,
     /* equiv idx value  */ NO_EQUIVALENT, 0,
     /* equivalenced to  */ NO_EQUIVALENT,
     /* min, max, act ct */ 0, 1, 0,
     /* opt state flags  */ VERSION_OPT_FLAGS, 0,
     /* last opt argumnt */ { NULL },
     /* arg list/cookie  */ NULL,
     /* must/cannot opts */ NULL, NULL,
     /* option proc      */ optionPrintVersion,
     /* desc, NAME, name */ zVersionText, NULL, zVersion_Name,
     /* disablement strs */ NULL, NULL },

#undef VERSION_OPT_FLAGS


  {  /* entry idx, value */ INDEX_OPT_HELP, VALUE_OPT_HELP,
     /* equiv idx value  */ NO_EQUIVALENT, 0,
     /* equivalenced to  */ NO_EQUIVALENT,
     /* min, max, act ct */ 0, 1, 0,
     /* opt state flags  */ OPTST_IMM | OPTST_NO_INIT, 0,
     /* last opt argumnt */ { NULL },
     /* arg list/cookie  */ NULL,
     /* must/cannot opts */ NULL, NULL,
     /* option proc      */ doUsageOpt,
     /* desc, NAME, name */ zHelpText, NULL, zHelp_Name,
     /* disablement strs */ NULL, NULL },

  {  /* entry idx, value */ INDEX_OPT_MORE_HELP, VALUE_OPT_MORE_HELP,
     /* equiv idx value  */ NO_EQUIVALENT, 0,
     /* equivalenced to  */ NO_EQUIVALENT,
     /* min, max, act ct */ 0, 1, 0,
     /* opt state flags  */ OPTST_IMM | OPTST_NO_INIT, 0,
     /* last opt argumnt */ { NULL },
     /* arg list/cookie  */ NULL,
     /* must/cannot opts */ NULL,  NULL,
     /* option proc      */ optionPagedUsage,
     /* desc, NAME, name */ zMore_HelpText, NULL, zMore_Help_Name,
     /* disablement strs */ NULL, NULL },

  {  /* entry idx, value */ INDEX_OPT_SAVE_OPTS, VALUE_OPT_SAVE_OPTS,
     /* equiv idx value  */ NO_EQUIVALENT, 0,
     /* equivalenced to  */ NO_EQUIVALENT,
     /* min, max, act ct */ 0, 1, 0,
     /* opt state flags  */ OPTST_SET_ARGTYPE(OPARG_TYPE_STRING)
                          | OPTST_ARG_OPTIONAL | OPTST_NO_INIT, 0,
     /* last opt argumnt */ { NULL },
     /* arg list/cookie  */ NULL,
     /* must/cannot opts */ NULL,  NULL,
     /* option proc      */ NULL,
     /* desc, NAME, name */ zSave_OptsText, NULL, zSave_Opts_Name,
     /* disablement strs */ NULL, NULL },

  {  /* entry idx, value */ INDEX_OPT_LOAD_OPTS, VALUE_OPT_LOAD_OPTS,
     /* equiv idx value  */ NO_EQUIVALENT, 0,
     /* equivalenced to  */ NO_EQUIVALENT,
     /* min, max, act ct */ 0, NOLIMIT, 0,
     /* opt state flags  */ OPTST_SET_ARGTYPE(OPARG_TYPE_STRING)
			  | OPTST_DISABLE_IMM, 0,
     /* last opt argumnt */ { NULL },
     /* arg list/cookie  */ NULL,
     /* must/cannot opts */ NULL, NULL,
     /* option proc      */ optionLoadOpt,
     /* desc, NAME, name */ zLoad_OptsText, zLoad_Opts_NAME, zLoad_Opts_Name,
     /* disablement strs */ zNotLoad_Opts_Name, zNotLoad_Opts_Pfx }
};

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  Define the Edit_Daemon_Conf Option Environment
 */
tSCC   zPROGNAME[]   = "EDIT_DAEMON_CONF";
tSCC   zUsageTitle[] =
"edit-daemon-conf - example dynamic config daemon - Ver. 1.2\n\
USAGE:  %s [ -<flag> [<val>] | --<name>[{=| }<val>] ]...\n";
tSCC   zRcName[]     = ".edit_daemon_confrc";
tSCC*  apzHomeList[] = {
       "/home/bkorb/ag/addon/config_edit/daemon.cfg",
       NULL };

tSCC   zBugsAddr[]    = "autogen-users@lists.sourceforge.net";
#define zExplain NULL
#define zDetail         NULL
tSCC    zFullVersion[] = EDIT_DAEMON_CONF_FULL_VERSION;
/* extracted from /home/root/usr/local/share/autogen/optcode.tpl near line 492 */

#if defined(ENABLE_NLS)
# define OPTPROC_BASE OPTPROC_TRANSLATE
  static tOptionXlateProc translate_option_strings;
#else
# define OPTPROC_BASE OPTPROC_NONE
# define translate_option_strings NULL
#endif /* ENABLE_NLS */

static optArgBucket_t const originalEdit_Daemon_ConfDefaults[ OPTION_CT ] = {
    { NULL },
    { NULL },
    { NULL },
    { zTextDefaultArg },
    { NULL },
    { NULL },
    { NULL },
    { NULL },
    { NULL },
    { NULL }
};
static void * const originalEdit_Daemon_ConfCookies[ OPTION_CT ] = {
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};


#define edit_daemon_conf_full_usage NULL
#define edit_daemon_conf_short_usage NULL
tOptions edit_daemon_confOptions = {
    OPTIONS_STRUCT_VERSION,
    0, NULL,                    /* original argc + argv    */
    ( OPTPROC_BASE
    + OPTPROC_ERRSTOP
    + OPTPROC_SHORTOPT
    + OPTPROC_LONGOPT
    + OPTPROC_NO_REQ_OPT
    + OPTPROC_NO_ARGS
    + OPTPROC_HAS_IMMED ),
    0, NULL,                    /* current option index, current option */
    NULL,         NULL,         zPROGNAME,
    zRcName,      zCopyright,   zCopyrightNotice,
    zFullVersion, apzHomeList,  zUsageTitle,
    zExplain,     zDetail,      optDesc,
    zBugsAddr,                  /* address to send bugs to */
    NULL, NULL,                 /* extensions/saved state  */
    optionUsage,       /* usage procedure */
    translate_option_strings,   /* translation procedure */
    /*
     *  Indexes to special options
     */
    { INDEX_OPT_MORE_HELP,
      INDEX_OPT_SAVE_OPTS,
      NO_EQUIVALENT /* index of '-#' option */,
      NO_EQUIVALENT /* index of default opt */
    },
    10 /* full option count */, 4 /* user option count */,
    edit_daemon_conf_full_usage, edit_daemon_conf_short_usage,
    originalEdit_Daemon_ConfDefaults, originalEdit_Daemon_ConfCookies
};

/*
 *  Create the static procedure(s) declared above.
 */
static void
doUsageOpt(
    tOptions*   pOptions,
    tOptDesc*   pOptDesc )
{
    if ((pOptDesc->fOptState & OPTST_RESET) != 0)
        return;

    USAGE( EXIT_SUCCESS );
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *   For the pid-file option.
 */
static void
doOptPid_File(tOptions* pOptions, tOptDesc* pOptDesc)
{
    static teOptFileType const  type =
        FTYPE_MODE_MUST_EXIST + FTYPE_MODE_FOPEN_FP;
    static tuFileMode           mode;
#ifndef FOPEN_BINARY_FLAG
#  define FOPEN_BINARY_FLAG
#endif
    mode.file_mode = "r" FOPEN_BINARY_FLAG;

    optionFileCheck(pOptions, pOptDesc, type, mode);
}
#include <sys/types.h>
#include <sys/wait.h>

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif

#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif

int
main(int argc, char** argv)
{
    int res = EXIT_SUCCESS;
    {
        int ct = optionProcess(&edit_daemon_confOptions, argc, argv);
        argc -= ct;
        argv += ct;
    }

    switch (argc) {
    case 1:
        SET_OPT_SAVE_OPTS(*argv);

    case 0:
        optionSaveFile(&edit_daemon_confOptions);
        break;

    default:
        fprintf(stderr, "%s error: too many operands\n",
                edit_daemon_confOptions.pzProgName);
        edit_daemon_confOptions.pUsageProc(&edit_daemon_confOptions, EXIT_FAILURE);
        /* NOTREACHED */
    }

    /* still here?  The options are fine and we've stored the new version */
    switch (WHICH_IDX_PID_FILE) {
    case INDEX_OPT_PID:
        if (kill(OPT_VALUE_PID, SIGHUP) != 0) {
        bad_kill:
            res = EXIT_FAILURE;
            fprintf(stderr, "fs error %d (%s) sending SIGHUP to pid %d\n",
                    errno, strerror(errno), OPT_VALUE_PID);
        }
        break;

    case INDEX_OPT_PID_FILE:
    {
        char buf[256];
        pid_t kill_pid;
        fread(buf, 1, sizeof(buf), OPT_VALUE_PID_FILE);
        kill_pid = (pid_t)strtoul(buf, NULL, 0);
        if (kill(kill_pid, SIGHUP) != 0)
            goto bad_kill;
        fclose(OPT_VALUE_PID_FILE);
        break;
    }

    case INDEX_OPT_NO_SIGHUP:
        return (EXIT_SUCCESS);

    case NO_EQUIVALENT:
    {
        /*
         *  No pid selected, so use "pkill" program.
         */
        static char const cmd_fmt[] =
            "pkill -hup -u %d daemon";
        char z[sizeof(cmd_fmt) + 10];
        uid_t myid = geteuid();
        sprintf(z, cmd_fmt, myid);
        res = system(z);
        if (res < 0)
            goto bad_kill;
        if (res != 0) {
            res = WEXITSTATUS(res);
            fprintf(stderr, "pkill exited with status %d\n\t%s\n", res, z);
        }
    }
    }

    return res;
}
/* extracted from /home/root/usr/local/share/autogen/optcode.tpl near line 625 */

#if ENABLE_NLS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <autoopts/usage-txt.h>

static char* AO_gettext( char const* pz );
static void  coerce_it(void** s);

static char*
AO_gettext( char const* pz )
{
    char* pzRes;
    if (pz == NULL)
        return NULL;
    pzRes = _(pz);
    if (pzRes == pz)
        return pzRes;
    pzRes = strdup( pzRes );
    if (pzRes == NULL) {
        fputs( _("No memory for duping translated strings\n"), stderr );
        exit( EXIT_FAILURE );
    }
    return pzRes;
}

static void coerce_it(void** s) { *s = AO_gettext(*s); }
#define COERSION(_f) \
  coerce_it((void*)&(edit_daemon_confOptions._f))

/*
 *  This invokes the translation code (e.g. gettext(3)).
 */
static void
translate_option_strings( void )
{
    /*
     *  Guard against re-translation.  It won't work.  The strings will have
     *  been changed by the first pass through this code.  One shot only.
     */
    if (option_usage_text.field_ct != 0) {

        /*
         *  Do the translations.  The first pointer follows the field count
         *  field.  The field count field is the size of a pointer.
         */
        tOptDesc* pOD = edit_daemon_confOptions.pOptDesc;
        char**    ppz = (char**)(void*)&(option_usage_text);
        int       ix  = option_usage_text.field_ct;

        do {
            ppz++;
            *ppz = AO_gettext(*ppz);
        } while (--ix > 0);

        COERSION(pzCopyright);
        COERSION(pzCopyNotice);
        COERSION(pzFullVersion);
        COERSION(pzUsageTitle);
        COERSION(pzExplain);
        COERSION(pzDetail);
        option_usage_text.field_ct = 0;

        for (ix = edit_daemon_confOptions.optCt; ix > 0; ix--, pOD++)
            coerce_it((void*)&(pOD->pzText));
    }

    if ((edit_daemon_confOptions.fOptSet & OPTPROC_NXLAT_OPT_CFG) == 0) {
        tOptDesc* pOD = edit_daemon_confOptions.pOptDesc;
        int       ix;

        for (ix = edit_daemon_confOptions.optCt; ix > 0; ix--, pOD++) {
            coerce_it((void*)&(pOD->pz_Name));
            coerce_it((void*)&(pOD->pz_DisableName));
            coerce_it((void*)&(pOD->pz_DisablePfx));
        }
        /* prevent re-translation */
        edit_daemon_confOptions.fOptSet |= OPTPROC_NXLAT_OPT_CFG | OPTPROC_NXLAT_OPT;
    }
}

#endif /* ENABLE_NLS */

#ifdef  __cplusplus
}
#endif
/* edit-daemon-conf.c ends here */