source: trunk/src/arm4_control.cpp @ 704

Revision 685, 24.1 KB checked in by dcarter, 3 years ago (diff)

see #125: Changes for improved C++ portability

Line 
1/**********************************************************************
2 * Copyright (c) 2005-2008 David Carter <dcarter@arm4.org> and others.
3 * All rights reserved.   This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * David Carter - Initial API and implementation
10 **********************************************************************/
11
12#include "config.h"
13 
14#include <sys/types.h>
15#include <sys/time.h>
16#include <errno.h>
17#include <signal.h>
18
19#include <cstddef>
20#include <cstdio>
21#include <cstdlib>
22#include <cstring>
23#include <cmath>
24
25#ifdef HAVE_GETOPT_H
26#include <getopt.h>
27#endif
28
29#include <db.h>
30#include "arm4.h"
31#include "arm4agent.h"
32#include "Arm4db.h"
33#include "Logger.h"
34#include "Arm4dbDaemonSharedMemory.h"
35
36/* Prototypes */
37static void usage (void);
38
39static void
40print_trace_flags (arm4_shm_transaction_trace_t flags)
41{
42        if (flags & arm4_shm_transaction_trace_all)
43                printf (" all");
44        else if (flags & arm4_shm_transaction_trace_n)
45                printf (" sample");
46        else if (flags & arm4_shm_transaction_trace_interval)
47                printf (" interval");
48        if (flags & arm4_shm_transaction_trace_use_default)
49                printf (" default");
50        if (flags & arm4_shm_transaction_trace_honor_request)
51                printf (" application");
52}
53
54static void
55print_appdex_flags (arm4_appdex_t flags)
56{
57        if (flags & arm4_appdex_on)
58                printf (" on");
59        else
60                printf (" off");
61       
62        if (flags & arm4_appdex_use_default)
63                printf (" default");
64        if (flags & arm4_appdex_autolearn)
65                printf (" autolearn");
66}
67
68static void
69print_id (const arm_id_t &id)
70{
71        for (int i  = 0; i < 16; i++)
72                printf ("%02x", id.id_u.uint8 [i]);
73}
74
75static arm_error_t
76get_id (arm_id_t &id, const char *string_ptr)
77{
78        if (strlen (string_ptr) != 32)
79                return ARM_FAILURE;
80
81        int hex_value = 0;
82        for (int i = 0; i < 16; i++)
83        {
84                int size = sscanf (&(string_ptr [i*2]), "%02x", &hex_value);
85                id.id_u.uint8 [i] = (hex_value & 0x0FF);
86                if (size != 1)
87                {
88                        return ARM_FAILURE;
89                }
90        }
91        return ARM_SUCCESS;
92}
93
94static void
95help_archive (void)
96{
97        printf ("archive - archive the current database\n");
98}
99
100static void
101command_archive (int argc, char *argv[], int optind)
102{
103        /* No parameters to archive */
104        if (argc != 1)
105        {
106                help_archive ();
107                exit (0);
108        }
109
110        Arm4dbDaemonSharedMemory::utilityCommand (ARM4_SHM_COMMAND_BACKUP);
111}
112
113static void
114help_collect (void)
115{
116        printf ("collect - show current collection state\n");
117        printf ("collect status - show current collection state\n");
118        printf ("collect yes - turn collection on\n");
119        printf ("collect on - turn collection on\n");
120        printf ("collect no - turn collection off\n");
121        printf ("collect off - turn collection off\n");
122}
123
124static void
125command_collect (int argc, char *argv[], int optind)
126{
127        if ((argc < 1) || (argc > 2))
128        {
129                help_collect ();
130                exit (0);
131        }
132
133        Arm4dbDaemonSharedMemory shm;
134        if ((argc == 1) ||
135                (strcasecmp (argv [optind + 1], "status") == 0))
136        {
137                /* do nothing until the end */;
138        }
139        else if (
140                (strcasecmp (argv [optind + 1], "yes") == 0) ||
141                (strcasecmp (argv [optind + 1], "on") == 0))
142        {
143                shm.setNullCollector (ARM_FALSE);
144        }
145        else if (
146                (strcasecmp (argv [optind + 1], "no") == 0) ||
147                (strcasecmp (argv [optind + 1], "off") == 0))
148        {
149                shm.setNullCollector (ARM_TRUE);
150        }
151        else
152        {
153                help_collect ();
154                exit (0);
155        }
156
157    if (shm.getNullCollector ())
158        printf ("Collection is OFF\n");
159    else
160        printf ("Collection is ON\n");
161}
162
163static void
164help_checkpoint (void)
165{
166        printf ("collect - perform an immediate database checkpoint operation\n");
167}
168
169static void
170command_checkpoint (int argc, char *argv[], int optind)
171{
172        if (argc != 1)
173        {
174                help_checkpoint ();
175                exit (0);
176        }
177
178        Arm4dbDaemonSharedMemory::utilityCommand (ARM4_SHM_COMMAND_CHECKPOINT);
179}
180
181static void
182help_status (void)
183{
184        printf ("status - show the arm4_daemon status\n");
185}
186
187static void
188command_status (int argc, char *argv[], int optind)
189{
190        pid_t daemon_pid;
191
192        /* No parameters to stop */
193        if (argc != 1)
194        {
195                help_status ();
196                exit (0);
197        }
198
199        /* Stop the daemon */
200        daemon_pid = Arm4dbDaemonSharedMemory::getActiveDaemon ();
201        if (daemon_pid != (pid_t) 0)
202        printf ("arm4_daemon is alive\n");
203    else
204        printf ("No arm4_daemon process is running\n");
205}
206
207static void
208help_stop (void)
209{
210        printf ("stop - stop the current arm4_daemon instance\n");
211}
212
213static void
214command_stop (int argc, char *argv[], int optind)
215{
216        pid_t daemon_pid;
217
218        /* No parameters to stop */
219        if (argc != 1)
220        {
221                help_stop ();
222                exit (0);
223        }
224
225        /* Stop the daemon */
226        daemon_pid = Arm4dbDaemonSharedMemory::getActiveDaemon ();
227        if (daemon_pid != (pid_t) 0)
228                kill (daemon_pid, SIGTERM);
229}
230
231static void
232help_remove (void)
233{
234        printf ("remove - remove shared memory regions and semaphores\n");
235}
236
237static void
238command_remove (int argc, char *argv[], int optind)
239{
240        /* No parameters to command_remove */
241        if (argc != 1)
242        {
243                help_remove ();
244                exit (0);
245        }
246
247        /* Check if a daemon is running */
248    if (Arm4dbDaemonSharedMemory::getActiveDaemon () != (pid_t) 0)
249    {
250        fprintf (stderr, "arm4_daemon is running. Please stop the daemon first.\n");
251        exit (1);
252    }
253
254    /* Remove the shared memory */
255        Arm4dbDaemonSharedMemory::shmDestroy ();
256}
257
258static void
259help_wipe (void)
260{
261    printf ("wipe - remove the current database, shared memory regions, and semaphores\n");
262}
263
264static void
265command_wipe (int argc, char *argv[], int optind)
266{
267        /* No parameters to command_wipe */
268        if (argc != 1)
269        {
270                help_wipe ();
271                exit (0);
272        }
273
274        /* Check if a daemon is running */
275    if (Arm4dbDaemonSharedMemory::getActiveDaemon () != (pid_t) 0)
276    {
277        fprintf (stderr, "arm4_daemon is running. Please stop the daemon first.\n");
278        exit (1);
279    }
280
281    /* Remove the shared memory */
282        Arm4dbDaemonSharedMemory::shmDestroy ();
283
284    /* Remove the databases */
285        Arm4db::getArm4db ()->removeDatabases ();
286}
287
288static void
289help_application (void)
290{
291    printf ("\tapplication - displays the current application definitions\n");
292    printf ("\tapplication {app_id} - displays the configuration for a specific application id\n");
293    printf ("\tapplication {app_id} transactions - displays the transactions associated a specific application id\n");
294}
295
296static void
297display_app_definitions (void)
298{
299        // iterate through the list of application definitions
300        std::list<arm_id_t> *app_lptr = Arm4db::getArm4db ()->getApplicationList ();
301
302        if (app_lptr->empty ())
303        {
304                printf ("No applications found\n");
305                delete app_lptr;
306                return;
307        }
308
309        std::list<arm_id_t>::iterator current = app_lptr->begin();
310        arm4db_application_summary_t summary;
311        while (current != app_lptr->end())
312        {
313                Arm4db::getArm4db ()->getApplicationInformation (*current, &summary);
314
315                // print the information
316                printf ("Application: %s\n", summary.application_name);
317                printf ("  ID: ");
318                print_id (summary.appl_id);
319                printf ("\n");
320                printf ("  Character set: %d\n", summary.charset);
321                printf ("\n");
322               
323                current++;
324        }
325       
326        delete app_lptr;
327}
328
329static void
330display_app_config (const arm_id_t &app_id)
331{
332        arm4db_application_summary_t summary;
333
334        Arm4db::getArm4db ()->getApplicationInformation (app_id, &summary);
335
336        // print the information
337        printf ("Application: %s\n", summary.application_name);
338        printf ("  Default transaction flags:");
339        print_trace_flags (summary.default_trace_flags);
340        printf ("\n");
341        printf ("  Default transaction parameter: %lld\n", summary.default_trace_parameter);
342        printf ("  Default appdex threshold: %lld\n", summary.default_appdex_threshold);
343        printf ("\n");
344}
345
346static void
347display_app_transactions (const arm_id_t &app_id)
348{
349        // iterate through the list of application definitions
350        std::list<arm_id_t> *tran_lptr = Arm4db::getArm4db ()->getTransactionList (app_id);
351
352        if (tran_lptr->empty ())
353        {
354                printf ("No transactions found\n");
355                delete tran_lptr;
356                return;
357        }
358
359        std::list<arm_id_t>::iterator current = tran_lptr->begin();
360        arm4db_transaction_summary_t summary;
361        while (current != tran_lptr->end())
362        {
363                Arm4db::getArm4db ()->getTransactionInformation (*current, &summary);
364
365                // print the information
366                printf ("Transaction: %s\n", summary.transaction_name);
367                printf ("  Transaction ID: ");
368                print_id (summary.tran_id);
369                printf ("\n");
370                printf ("  Application ID: ");
371                print_id (summary.appl_id);
372                printf ("\n");
373                printf ("  URI: %s\n", summary.uri);
374                printf ("\n");
375               
376                current++;
377        }
378       
379        delete tran_lptr;
380}
381
382static void
383command_application (int argc, char *argv[], int optind)
384{
385        /* No parameters to command_wipe */
386        if ((argc < 1) || (argc > 3))
387        {
388                help_application ();
389                exit (0);
390        }
391
392        /* Check if a daemon is running */
393        Arm4dbDaemonSharedMemory::shmAttach ();
394       
395        if (argc == 1)
396        {
397                display_app_definitions ();
398        }
399        else
400        {
401                /* Get the application id, and see if there's anything after that */
402                arm_id_t app_id;
403
404                if (get_id (app_id, argv [optind + 1]) != ARM_SUCCESS)
405                        printf ("Invalid id %s\n", argv [optind + 1]);
406                else if (argc == 2)
407                {
408                        display_app_config (app_id);
409                }
410                else if (strcasecmp (argv [optind + 2], "transactions") == 0)
411                {
412                        display_app_transactions (app_id);
413                }
414                else
415                {
416                        help_application ();
417                }
418        }
419
420}
421
422static void
423help_transaction (void)
424{
425    printf ("\ttransaction - displays the current transaction definitions\n");
426    printf ("\ttransaction {tran_id} - displays the configuration for a specific transaction id\n");
427    printf ("\ttransaction {tran_id} trace all - trace all transaction instances\n");
428    printf ("\ttransaction {tran_id} trace none - turn tracing off\n");
429    printf ("\ttransaction {tran_id} trace {n} - trace every nth transaction instance\n");
430    printf ("\ttransaction {tran_id} trace {n}[s|S] - trace one transaction instance every n tenths of seconds\n");
431    printf ("\ttransaction {tran_id} trace application - honor application trace requests\n");
432    printf ("\ttransaction {tran_id} trace noapplication - ignore application trace requests\n");
433    printf ("\ttransaction {tran_id} trace default - use application default settings\n");
434}
435
436static void
437display_tran_definitions (void)
438{
439        // iterate through the list of transaction definitions
440        std::list<arm_id_t> *tran_lptr = Arm4db::getArm4db ()->getTransactionList ();
441
442        if (tran_lptr->empty ())
443        {
444                printf ("No transactions found\n");
445                delete tran_lptr;
446                return;
447        }
448
449        std::list<arm_id_t>::iterator current = tran_lptr->begin();
450        arm4db_transaction_summary_t summary;
451        while (current != tran_lptr->end())
452        {
453                Arm4db::getArm4db ()->getTransactionInformation (*current, &summary);
454
455                // print the information
456                printf ("Transaction: %s\n", summary.transaction_name);
457                printf ("  Transaction ID: ");
458                print_id (summary.tran_id);
459                printf ("\n");
460                printf ("  Application ID: ");
461                print_id (summary.appl_id);
462                printf ("\n");
463                printf ("  URI: %s\n", summary.uri);
464                printf ("\n");
465               
466                current++;
467        }
468       
469        delete tran_lptr;
470}
471
472static void
473display_tran_config (const arm_id_t &tran_id)
474{
475        arm4db_transaction_summary_t summary;
476
477        if (Arm4db::getArm4db ()->getTransactionInformation (tran_id, &summary) != 0)
478        {
479                printf ("Unable to obtain transaction information\n");
480                exit (0);
481        }
482
483        // print the information
484        printf ("Transaction: %s\n", summary.transaction_name);
485        printf ("  Transaction flags:");
486        print_trace_flags (summary.trace_flags);
487        printf ("\n");
488        printf ("  Transaction parameter: %lld\n", summary.trace_parameter);
489        printf ("  Appdex flags:");
490        print_appdex_flags (summary.appdex_flags);
491        printf ("\n");
492        printf ("  Appdex threshold: %lld\n", summary.appdex_threshold);
493        printf ("\n");
494}
495
496static void
497config_trace (const arm_id_t &tran_id, const char *arg_ptr)
498{
499        arm4db_transaction_summary_t summary;
500
501        if (Arm4db::getArm4db ()->getTransactionInformation (tran_id, &summary) != 0)
502        {
503                printf ("Unable to obtain transaction information\n");
504                exit (0);
505        }
506
507    if (strcasecmp (arg_ptr, "all") == 0)
508        {
509        int flags = (int) summary.trace_flags;
510        flags &= ~(arm4_shm_transaction_trace_n | arm4_shm_transaction_trace_interval | arm4_shm_transaction_trace_use_default);
511        flags |= arm4_shm_transaction_trace_all;
512        summary.trace_flags = (arm4_shm_transaction_trace_t) flags;
513        summary.trace_parameter = 0;
514        }
515        else if (strcasecmp (arg_ptr, "none") == 0)
516        {
517        int flags = (int) summary.trace_flags;
518        flags &= ~(arm4_shm_transaction_trace_all | arm4_shm_transaction_trace_n | arm4_shm_transaction_trace_interval | arm4_shm_transaction_trace_use_default);
519        summary.trace_flags = (arm4_shm_transaction_trace_t) flags;
520        summary.trace_parameter = 0;
521        }
522        else if (strcasecmp (arg_ptr, "application") == 0)
523        {
524        int flags = (int) summary.trace_flags;
525        flags |= arm4_shm_transaction_trace_honor_request;
526        summary.trace_flags = (arm4_shm_transaction_trace_t) flags;
527        }
528        else if (strcasecmp (arg_ptr, "noapplication") == 0)
529        {
530        int flags = (int) summary.trace_flags;
531        flags &= ~(arm4_shm_transaction_trace_honor_request);
532        summary.trace_flags = (arm4_shm_transaction_trace_t) flags;
533        }
534        else if (strcasecmp (arg_ptr, "default") == 0)
535        {
536        int flags = (int) summary.trace_flags;
537        flags &= ~(arm4_shm_transaction_trace_all | arm4_shm_transaction_trace_n | arm4_shm_transaction_trace_interval);
538        flags |= arm4_shm_transaction_trace_use_default;
539        summary.trace_flags = (arm4_shm_transaction_trace_t) flags;
540        summary.trace_parameter = 0;
541        }
542        else
543        {
544                arm_int64_t trace_parameter;
545                char seconds;
546               
547                int count = sscanf (arg_ptr, "%lld%c", &trace_parameter, &seconds);
548                if (count == 1)
549                {
550                        // Sample count
551                int flags = (int) summary.trace_flags;
552                flags &= ~(arm4_shm_transaction_trace_all | arm4_shm_transaction_trace_interval | arm4_shm_transaction_trace_use_default);
553                flags |= arm4_shm_transaction_trace_n;
554                summary.trace_flags = (arm4_shm_transaction_trace_t) flags;
555                summary.trace_parameter = trace_parameter;
556                }
557                else if ((count == 2) && (toupper(seconds) == 'S'))
558                {
559                        // Interval seconds
560                int flags = (int) summary.trace_flags;
561                flags &= ~(arm4_shm_transaction_trace_all | arm4_shm_transaction_trace_n | arm4_shm_transaction_trace_use_default);
562                flags |= arm4_shm_transaction_trace_interval;
563                summary.trace_flags = (arm4_shm_transaction_trace_t) flags;
564                summary.trace_parameter = trace_parameter;
565                }
566                else
567                {
568                        help_transaction ();
569                        exit (0);
570                }
571        }
572
573    // Store the changed configuration
574    Arm4db::getArm4db ()->setTransactionTraceInformation (tran_id, summary);
575
576        Arm4dbDaemonSharedMemory shm;
577    shm.setTransactionTraceInformation (tran_id, summary);
578   
579    // Display the updated information
580    display_tran_config (tran_id);
581}
582
583static void
584command_transaction (int argc, char *argv[], int optind)
585{
586        /* No parameters to command_wipe */
587        if ((argc < 1) || (argc > 4))
588        {
589                help_transaction ();
590                exit (0);
591        }
592
593        /* Check if a daemon is running */
594        Arm4dbDaemonSharedMemory::shmAttach ();
595       
596        if (argc == 1)
597        {
598                display_tran_definitions ();
599        }
600        else
601        {
602                /* Get the application id, and see if there's anything after that */
603                arm_id_t tran_id;
604
605                if (get_id (tran_id, argv [optind + 1]) != ARM_SUCCESS)
606                        printf ("Invalid id %s\n", argv [optind + 1]);
607                else if (argc == 2)
608                {
609                        display_tran_config (tran_id);
610                }
611                else if ((argc == 4) && (strcasecmp (argv [optind + 2], "trace") == 0))
612                {
613                        config_trace (tran_id, argv [optind + 3]);
614                }
615                else
616                {
617                        help_transaction ();
618                        exit (0);
619                }
620        }
621
622}
623
624static void
625help_export (void)
626{
627    printf ("\texport definitions - exports application, metric, and transaction definitions to stdout\n");
628    printf ("\texport all - exports definitions and instance data to stdout\n");
629}
630
631static void
632export_definitions (void)
633{
634        Arm4dbUtilityLock lock;
635
636        Arm4db *arm4db_ptr = Arm4db::getArm4db ();
637        arm_error_t status = arm4db_ptr->openDatabases ();
638        if (status != ARM_SUCCESS)
639        {
640                lock.unlock ();
641                exit (0);
642        }
643
644    arm4db_ptr->exportDefinitions (std::cout);
645               
646        arm4db_ptr->closeDatabases ();
647
648        lock.unlock ();
649}
650
651static void
652export_all (void)
653{
654        Arm4dbUtilityLock lock;
655
656        Arm4db *arm4db_ptr = Arm4db::getArm4db ();
657        arm_error_t status = arm4db_ptr->openDatabases ();
658        if (status != ARM_SUCCESS)
659        {
660                lock.unlock ();
661                exit (0);
662        }
663
664    arm4db_ptr->exportAll (std::cout);
665               
666        arm4db_ptr->closeDatabases ();
667
668        lock.unlock ();
669}
670
671static void
672command_export (int argc, char *argv[], int optind)
673{
674        /* No parameters to command_wipe */
675        if (argc != 2)
676        {
677                help_export ();
678                exit (0);
679        }
680
681        /* Check if a daemon is running */
682        Arm4dbDaemonSharedMemory::shmAttach ();
683
684        if (strcasecmp(argv[optind + 1],"definitions") == 0)
685        {
686                export_definitions ();
687        }
688        else if (strcasecmp(argv[optind + 1],"all") == 0)
689        {
690                export_all ();
691        }
692        else
693        {
694                help_export ();
695                exit (0);
696        }
697}
698
699
700static void
701help_import (void)
702{
703    printf ("\timport filename - imports an exported database from the file\n");
704}
705
706static void
707import_xml (const char *filename_ptr)
708{
709        Arm4dbUtilityLock lock;
710
711        Arm4db *arm4db_ptr = Arm4db::getArm4db ();
712        arm_error_t status = arm4db_ptr->openDatabases ();
713        if (status != ARM_SUCCESS)
714        {
715                lock.unlock ();
716                exit (0);
717        }
718
719    arm4db_ptr->import (filename_ptr);
720               
721        arm4db_ptr->closeDatabases ();
722
723        lock.unlock ();
724}
725
726static void
727command_import (int argc, char *argv[], int optind)
728{
729        /* No parameters to command_wipe */
730        if (argc != 2)
731        {
732                help_import ();
733                exit (0);
734        }
735
736        /* Check if a daemon is running */
737        Arm4dbDaemonSharedMemory::shmAttach ();
738       
739        // Check that we have access to the file
740       
741        import_xml (argv[optind + 1]);
742}
743
744static void
745help_debug (void)
746{
747    printf ("\tdebug level - Set the debug level. Higher is more information, 0 is off\n");
748}
749
750static void
751command_debug (int argc, char *argv[], int optind)
752{
753        /* No parameters to command_wipe */
754        if (argc != 2)
755        {
756                help_export ();
757                exit (0);
758        }
759
760        /* Check if a daemon is running */
761        Arm4dbDaemonSharedMemory::shmAttach ();
762       
763        Arm4dbUtilityLock lock;
764
765        int level = std::atoi (argv[optind + 1]);
766        Arm4dbSharedMemory::setDebugLevel (level);
767
768        lock.unlock ();
769}
770
771static void
772help_commands (void)
773{
774        printf ("Valid commands:\n");
775        printf ("\tarchive - archive the current database\n");
776        printf ("\tcollect - turn collection on or off\n");
777        printf ("\tcheckpoint - perform a database chckpoint operation\n");
778        printf ("\tremove - remove shared memory regions and semaphores\n");
779    printf ("\tstatus - show the arm4_daemon status\n");
780        printf ("\tstop - stop the current arm4_daemon instance\n");
781        printf ("\twipe - remove the current database, shared memory regions, and semaphores\n");
782        printf ("\tapplication - display and configure application definitions\n");
783        printf ("\ttransaction - display and configure transaction definitions\n");
784        printf ("\texport - export database to xml\n");
785        printf ("\timport - import database from xml\n");
786        printf ("\tdebug - set debugging level\n");
787        printf ("\tcommands - show valid commands\n");
788        printf ("\nType help <command> for information on that command\n");
789}
790
791static void
792command_help (int argc, char *argv[], int optind)
793{
794        /* No parameters to archive */
795        if (argc > 2)
796        {
797                usage ();
798                exit (0);
799        }
800
801        if ((argc == 1) || (strcasecmp (argv [optind + 1], "commands") == 0))
802        {
803                help_commands ();
804        }
805        else if (strcasecmp (argv [2], "archive") == 0)
806        {
807                help_archive ();
808        }
809        else if (strcasecmp (argv [2], "collect") == 0)
810        {
811                help_collect ();
812        }
813        else if (strcasecmp (argv [2], "checkpoint") == 0)
814        {
815                help_checkpoint ();
816        }
817        else if (strcasecmp (argv[2], "remove") == 0)
818        {
819                help_remove ();
820        }
821    else if (strcasecmp (argv[2], "status") == 0)
822    {
823        help_status ();
824    }
825    else if (strcasecmp (argv[2], "stop") == 0)
826    {
827        help_stop ();
828    }
829        else if (strcasecmp (argv[2], "wipe") == 0)
830        {
831                help_wipe ();
832        }
833        else if (strcasecmp (argv[2], "application") == 0)
834        {
835                help_application ();
836        }
837        else if (strcasecmp (argv[2], "transaction") == 0)
838        {
839                help_transaction ();
840        }
841        else if (strcasecmp (argv[2], "export") == 0)
842        {
843                help_export ();
844        }
845        else if (strcasecmp (argv[2], "import") == 0)
846        {
847                help_import ();
848        }
849        else if (strcasecmp (argv[2], "debug") == 0)
850        {
851                help_debug ();
852        }
853        else
854        {
855                fprintf (stderr, "Unrecognized command %s\n", argv[1]);
856                help_commands ();
857        }
858
859}
860
861static void
862process_command (int argc, char *argv[], int optind)
863{
864        if (strcasecmp (argv [optind], "archive") == 0)
865        {
866                command_archive (argc, argv, optind);
867        }
868        else if (strcasecmp (argv[optind], "collect") == 0)
869        {
870                command_collect (argc, argv, optind);
871        }
872        else if (strcasecmp (argv[optind], "checkpoint") == 0)
873        {
874                command_checkpoint (argc, argv, optind);
875        }
876    else if (strcasecmp (argv[optind], "remove") == 0)
877    {
878        command_remove (argc, argv, optind);
879    }
880        else if (strcasecmp (argv[optind], "status") == 0)
881        {
882                command_status (argc, argv, optind);
883        }
884    else if (strcasecmp (argv[optind], "stop") == 0)
885    {
886        command_stop (argc, argv, optind);
887    }
888    else if (strcasecmp (argv[optind], "wipe") == 0)
889    {
890        command_wipe (argc, argv, optind);
891    }
892    else if (strcasecmp (argv[optind], "application") == 0)
893    {
894        command_application (argc, argv, optind);
895    }
896    else if (strcasecmp (argv[optind], "transaction") == 0)
897    {
898        command_transaction (argc, argv, optind);
899    }
900    else if (strcasecmp (argv[optind], "export") == 0)
901    {
902        command_export (argc, argv, optind);
903    }
904    else if (strcasecmp (argv[optind], "import") == 0)
905    {
906        command_import (argc, argv, optind);
907    }
908        else if (strcasecmp (argv[optind], "debug") == 0)
909        {
910                command_debug (argc, argv, optind);
911        }
912        else if (strcasecmp (argv[optind], "help") == 0)
913        {
914                command_help (argc, argv, optind);
915        }
916        else
917        {
918                fprintf (stderr, "Unrecognized command %s\n", argv[optind]);
919                usage ();
920        }
921}
922
923static void
924initialize_configuration (const char *config_filename)
925{
926    /* Set the DB parameters */
927        Arm4dbConfig::setConfig (config_filename);
928}
929
930static void
931version (void)
932{
933        printf ("Package %s\n", PACKAGE);
934        printf ("Version %s\n", VERSION);
935}
936
937static void
938usage (void)
939{
940        printf ("Usage:\n");
941#ifdef HAVE_GETOPT_LONG
942        printf ("\tarm4_control --help|-h\n");
943        printf ("\t\tShow this help message\n");
944        printf ("\tarm4_control --version|-V\n");
945        printf ("\t\tDisplay implementation version\n");
946        printf ("\tarm4_control [--config|-C config_file] [commands...]\n");
947    printf ("\t\t--config specifies a path to the configuration file. The default is /etc/arm4.conf\n");
948        printf ("\t\tThe commands 'help' or 'help commands' will show all available commands\n");
949#else
950        printf ("\tarm4_control -h\n");
951        printf ("\t\tShow this help message\n");
952        printf ("\tarm4_control -V\n");
953        printf ("\t\tDisplay implementation version\n");
954        printf ("\tarm4_control [-C config_file] [commands...]\n");
955        printf ("\t\t--config specifies a path to the configuration file. The default is /etc/arm4.conf\n");
956        printf ("\t\tThe commands 'help' or 'help commands' will show all available commands\n");
957#endif
958}
959
960int
961main (int argc, char *argv[])
962{
963        int c;
964    char config_filename [1024];
965
966    // Initialize the logger
967    Logger::getLogger().setName ("arm4_control");
968    Logger::getLogger().setFile (stderr);
969
970    /* Set the default config file */
971    strcpy (config_filename, "/etc/arm4.conf");
972
973    while (1)
974        {
975#ifdef HAVE_GETOPT_LONG
976                static struct option long_options[] =
977                        {
978                                        /* These options set a flag. */
979                                        /* These options don't set a flag. We distinguish them by their indices. */
980                                {"help",     no_argument,       0, 'h'},
981                                {"version",  no_argument,       0, 'V'},
982                {"config",   required_argument, 0, 'C'},
983                                {0, 0, 0, 0}
984                        };
985
986                /* getopt_long stores the option index here. */
987                int option_index = 0;
988 
989                c = getopt_long (argc, argv, "ashVC:",
990                                                long_options, &option_index);
991#else
992                c = getopt (argc, argv, "ashVC:");
993#endif
994 
995                /* Detect the end of the options. */
996                if (c == -1)
997                        break;
998 
999                switch (c)
1000                {
1001                case 0:
1002                        /* If this option set a flag, do nothing else now. */
1003#ifdef HAVE_GETOPT_LONG
1004                        if (long_options[option_index].flag != 0)
1005                                break;
1006
1007                        printf ("option %s", long_options[option_index].name);
1008                        if (optarg)
1009                                printf (" with arg %s", optarg);
1010                        printf ("\n");
1011#endif
1012                        break;
1013 
1014                case '?':
1015                        /* getopt_long already printed an error message. */
1016                        break;
1017                       
1018                case 'h':
1019                        usage ();
1020                        exit (0);
1021
1022                case 'V':
1023                        version ();
1024                        exit (0);
1025
1026        case 'C':
1027            strncpy (config_filename, optarg, sizeof(config_filename));
1028            break;
1029
1030                default:
1031                        usage ();
1032                        abort ();
1033                }
1034        }
1035
1036    initialize_configuration (config_filename);
1037
1038        if ((argc - optind) < 1)
1039        {
1040                usage ();
1041                exit (0);
1042        }
1043
1044        process_command (argc - optind, argv, optind);
1045
1046
1047        return 0;
1048}
1049
Note: See TracBrowser for help on using the repository browser.