From: <st...@us...> - 2004-03-11 21:09:01
|
Update of /cvsroot/uts/uts/src/cam/camd In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23704/cam/camd Modified Files: main.c Log Message: Added command line options to teld and camd. Index: main.c =================================================================== RCS file: /cvsroot/uts/uts/src/cam/camd/main.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** main.c 9 Mar 2004 23:12:47 -0000 1.3 --- main.c 11 Mar 2004 20:42:06 -0000 1.4 *************** *** 17,21 **** --- 17,23 ---- #include <stdio.h> #include <stdlib.h> + #include <string.h> #include <signal.h> + #include <getopt.h> #include <setjmp.h> #include "skterror.h" *************** *** 28,46 **** static void goto_bg(void); int main(int argc, char ** argv) { cam_info *cam; - char *uts_dir; char tmpbuf[256]; FILE *conffile; ! printf("\nCCD daemon ver. 0.3sec - secretary version.\n\n"); ! uts_dir = getenv("UTS_DIR"); ! snprintf(tmpbuf, 255, "%s/etc/camd.conf", uts_dir); ! conffile = fopen(tmpbuf, "r"); if (conffile == NULL) { printf("Config file not found.\n"); --- 30,84 ---- + #define PROGRAM_NAME "CCD camera controlling daemon." + #define URL "http://www.astro.ufsc.br/~andre/" + #ifndef VERSION + #define VERSION "Beta" + #endif + + #define ROOT_DIR_ENV_VAR "UTS_DIR" + #define DEF_ROOT_DIR "/usr/local/uts" + #define DEF_CONF_FILE_NAME "/etc/camd.conf" + + + /*** Global variables filled by get_args() ***/ + char *exec_name; /* Name of executable file */ + static char *conffname; /* configuration file */ + static char *logfname; /* log file */ + static int detach = FALSE; /* should the program detach from terminal? */ + + + static void print_title(void); + static void print_help(void); + static void print_version(void); + static void print_usage(void); + static void get_args(int argc, char **argv); static void goto_bg(void); + /*-------------------------------------------------------------------------*/ + + int main(int argc, char ** argv) { cam_info *cam; char tmpbuf[256]; FILE *conffile; ! /* Parse arguments */ ! get_args(argc, argv); ! /* Open log file and redirect stdout, if required */ ! if (logfname != NULL) { ! fprintf(stderr, " -> Logging to file \"%s\".\n", logfname); ! stdout = freopen(logfname, "a", stdout); ! if (stdout == NULL) { ! fprintf(stderr, "*** Error opening file \"%s\".\n", ! logfname); ! exit(1); ! } ! } ! ! /* Load camfits command line */ ! conffile = fopen(conffname, "r"); if (conffile == NULL) { printf("Config file not found.\n"); *************** *** 48,54 **** } fgets(tmpbuf, 255, conffile); ! printf("Jumping into background...\n"); ! goto_bg(); signal(SIGPIPE, pipe_error); /* error handling */ --- 86,98 ---- } fgets(tmpbuf, 255, conffile); + fclose(conffile); ! if (detach) { ! /* Detach from terminal (daemon mode) */ ! fprintf(stderr, " -> Jumping into background...\n\n"); ! goto_bg(); ! /* close stdout if no logfile specified */ ! if (logfname == NULL) fclose(stdout); ! } signal(SIGPIPE, pipe_error); /* error handling */ *************** *** 65,68 **** --- 109,256 ---- + /*-------------------------------------------------------------------------*/ + + static void print_title(void) + { + printf("%s - version: %s\n", PROGRAM_NAME, VERSION); + } + + /*-------------------------------------------------------------------------*/ + + static void print_help(void) + { + print_title(); + printf("This program requires Socket PortMaster (Spm) and\n"); + printf("Socket Secretary (sec) running.\n\n\n"); + print_usage(); + printf("\nIf no config file is specified, this program will search " + "under the environment\n" + "variable %s:\n\n" + " ${%s}%s\n\n", ROOT_DIR_ENV_VAR, ROOT_DIR_ENV_VAR, + DEF_CONF_FILE_NAME); + printf("If this file does not exists, this program will try " + "the following file:\n\n" + " %s%s\n\n\n", DEF_ROOT_DIR, DEF_CONF_FILE_NAME); + printf("Command line options:\n\n" + " -h / --help\n" + " This help screen.\n\n" + + " -v / --version\n" + " Display version and copyright " + " information.\n\n" + + " -d / --detach\n" + " Daemon mode.\n" + " Put the program in background.\n\n" + + " -o <logfile> / --output <logfile>\n" + " Redirect output to <logfile>,\n" + " instead of showing it on the terminal.\n\n\n" + ); + + printf("Visit the home page\n" + "%s\n" + "for more information.\n\n", URL); + exit(0); + } + + /*-------------------------------------------------------------------------*/ + + static void print_version(void) + { + print_title(); + printf("Copyright (C) 2004 Andre Luiz de Amorim\n"); + printf("%s comes with NO WARRANTY,\n" + "to the extent permitted by law.\n", PROGRAM_NAME); + printf("You may redistribute copies of %s\n" + "under the terms of the GNU General Public License.\n" + "For more information about these matters,\n" + "see the files named COPYING.\n", PROGRAM_NAME); + + exit(0); + } + + + /*-------------------------------------------------------------------------*/ + + static void print_usage(void) + { + printf("SYNTAX:\n" + " %s [-o <logfile>] [-d] [config_file]\n\n" + " %s {-h|--help}\n\n", exec_name, exec_name); + } + + /*-------------------------------------------------------------------------*/ + + static void get_args(int argc, char **argv) + { + int next_opt; + const char *short_options = "hvdo:"; + const struct option long_options [] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'v' }, + { "detach", 0, NULL, 'd' }, + { "output", 1, NULL, 'o' }, + { NULL, 0, NULL, 0 } + }; + + exec_name = argv[0]; + conffname = NULL; + logfname = NULL; + do { + next_opt = getopt_long(argc, argv, short_options, + long_options, NULL); + switch (next_opt) { + case 'h': + print_help(); + break; + + case 'v': + print_version(); + break; + + case 'd': + detach = TRUE; + break; + + case 'o': + /* log file name */ + logfname = optarg; + break; + + case '?': + /* Invalid option */ + print_usage(); + exit(1); + break; + + case -1: + /* end of options */ + break; + + default: + abort(); + break; + } + } while (next_opt != -1); + + if (optind < argc) + /* config file is the first non-option argument */ + conffname = argv[optind]; + + /* If config file is not specified, try to get it from environment */ + if (conffname == NULL) { + int n; + char *root_dir = getenv(ROOT_DIR_ENV_VAR); + if (root_dir == NULL) root_dir = DEF_ROOT_DIR; + + n = strlen(root_dir) + strlen(DEF_CONF_FILE_NAME) + 1; + conffname = calloc(n, sizeof(char)); + strncpy(conffname, root_dir, n); + strncat(conffname, DEF_CONF_FILE_NAME, n); + } + } + + /*-------------------------------------------------------------------------*/ static void goto_bg(void) *************** *** 79,80 **** --- 267,270 ---- } + /*-------------------------------------------------------------------------*/ + |