550N/A/*
550N/A*
1515N/A* Copyright (c) 1990, 2015, Oracle and/or its affiliates. All rights reserved.
550N/A*
550N/A* Permission is hereby granted, free of charge, to any person obtaining a
919N/A* copy of this software and associated documentation files (the "Software"),
919N/A* to deal in the Software without restriction, including without limitation
919N/A* the rights to use, copy, modify, merge, publish, distribute, sublicense,
919N/A* and/or sell copies of the Software, and to permit persons to whom the
919N/A* Software is furnished to do so, subject to the following conditions:
550N/A*
919N/A* The above copyright notice and this permission notice (including the next
919N/A* paragraph) shall be included in all copies or substantial portions of the
919N/A* Software.
550N/A*
919N/A* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
919N/A* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
919N/A* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
919N/A* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
919N/A* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
919N/A* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
919N/A* DEALINGS IN THE SOFTWARE.
550N/A*
550N/A*/
550N/A
550N/A
550N/A#include <stdio.h>
1233N/A#include <stdarg.h>
1233N/A#include <string.h>
550N/A#include "cmc.h"
550N/A
550N/A
550N/A/*
550N/A** Options and Arguments
550N/A*/
550N/A
550N/Achar *display_name = NULL; /* -display */
1233N/Aint warn_flag = 0; /* -warn */
1233N/Achar *basename_arg = NULL; /* optional argument */
550N/A
550N/Achar *program;
550N/A
550N/A
550N/Avoid
1233N/Afatal_error (const char *format, ...)
550N/A
550N/A{
1233N/A va_list args;
1233N/A
1233N/A va_start(args, format);
550N/A (void) fprintf(stderr, "%s: error: ", program);
1233N/A (void) vfprintf(stderr, format, args);
550N/A (void) fprintf(stderr, "\n");
1233N/A va_end(args);
550N/A exit(1);
550N/A}
550N/A
550N/A
550N/Avoid
1233N/Awarning (const char *format, ...)
550N/A
550N/A{
1233N/A va_list args;
1233N/A
1233N/A va_start(args, format);
550N/A (void) fprintf(stderr, "Warning: ");
1233N/A (void) vfprintf(stderr, format, args);
550N/A (void) fprintf(stderr, "\n");
1233N/A va_end(args);
550N/A exit(1);
550N/A}
550N/A
550N/Astatic void
1515N/Ausage (void)
550N/A{
550N/A /* Note: optional filename arg explicitly not documented */
550N/A fprintf(stderr, "usage: %s <op> [-display name] [-warn]\n", program);
550N/A fprintf(stderr, "<op> = save | init | discard | dealloc | show | NeWSinit\n");
550N/A exit(1);
550N/A}
550N/A
550N/A
550N/A/*
550N/A** Parse arguments
550N/A*/
550N/A
550N/Avoid
1515N/Aprocess_arguments (char **argv)
550N/A{
550N/A register char **a;
550N/A
550N/A for (a = argv; *a; a++) {
550N/A if (**a == '-') {
550N/A if (!strcmp(*a, "-warn")) {
1233N/A warn_flag = 1;
550N/A } else if (!strcmp(*a, "-display")) {
550N/A if (*++a)
550N/A display_name = *a;
550N/A else {
550N/A fprintf(stderr, "error: -display needs an argument\n");
550N/A usage();
550N/A }
550N/A } else {
550N/A fprintf(stderr, "error: unrecognized option '%s'\n", *a);
550N/A usage();
550N/A }
550N/A } else {
1233N/A if (basename_arg) {
550N/A fprintf(stderr, "error: unrecognized argument '%s'\n", *a);
550N/A usage();
550N/A } else
1233N/A basename_arg = *a;
550N/A }
550N/A }
550N/A}
550N/A
550N/A
550N/A/*ARGSUSED*/
1233N/Aint
1233N/Amain (int argc, char **argv)
550N/A{
1515N/A void (*op)(void);
550N/A
550N/A /* Initialize error handling */
550N/A program = argv[0];
550N/A
550N/A /* determine operation */
550N/A if (argc <= 1)
550N/A usage();
550N/A ++argv;
550N/A if (!strcmp("save", *argv))
550N/A op = cmc_save;
550N/A else if (!strcmp("init", *argv))
550N/A op = cmc_init;
550N/A else if (!strcmp("show", *argv))
550N/A op = cmc_show;
550N/A else if (!strcmp("discard", *argv))
550N/A op = cmc_discard;
550N/A else if (!strcmp("dealloc", *argv))
550N/A op = cmc_dealloc;
550N/A else if (!strcmp("NeWSinit", *argv))
550N/A op = cmc_NeWSinit;
550N/A else
550N/A usage();
550N/A
550N/A /* parse rest of arguments */
550N/A process_arguments(++argv);
550N/A
550N/A /* invoke operation */
550N/A op();
550N/A
550N/A exit(0);
550N/A}