/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2007 Robert N. M. Watson4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/cdefs.h>29#include <err.h>30#include <stdio.h>31#include <stdlib.h>32#include <string.h>33#include <sysexits.h>34#include <unistd.h>3536#include "ddb.h"3738void ddb_readfile(char *file);39void ddb_main(int argc, char *argv[]);4041void42usage(void)43{4445fprintf(stderr, "usage: ddb capture [-M core] [-N system] print\n");46fprintf(stderr, " ddb capture [-M core] [-N system] status\n");47fprintf(stderr, " ddb script scriptname\n");48fprintf(stderr, " ddb script scriptname=script\n");49fprintf(stderr, " ddb scripts\n");50fprintf(stderr, " ddb unscript scriptname\n");51fprintf(stderr, " ddb pathname\n");52exit(EX_USAGE);53}5455void56ddb_readfile(char *filename)57{58char buf[BUFSIZ];59FILE* f;6061if ((f = fopen(filename, "r")) == NULL)62err(EX_UNAVAILABLE, "fopen: %s", filename);6364#define WHITESP " \t"65#define MAXARG 266while (fgets(buf, BUFSIZ, f)) {67int argc = 0;68char *argv[MAXARG];69size_t spn;7071spn = strlen(buf);72if (buf[spn-1] == '\n')73buf[spn-1] = '\0';7475spn = strspn(buf, WHITESP);76argv[0] = buf + spn;77if (*argv[0] == '#' || *argv[0] == '\0')78continue;79argc++;8081spn = strcspn(argv[0], WHITESP);82argv[1] = argv[0] + spn + strspn(argv[0] + spn, WHITESP);83argv[0][spn] = '\0';84if (*argv[1] != '\0')85argc++;8687#ifdef DEBUG88{89int i;90printf("argc = %d\n", argc);91for (i = 0; i < argc; i++) {92printf("arg[%d] = %s\n", i, argv[i]);93}94}95#endif96ddb_main(argc, argv);97}98fclose(f);99}100101void102ddb_main(int argc, char *argv[])103{104105if (argc < 1)106usage();107108if (strcmp(argv[0], "capture") == 0)109ddb_capture(argc, argv);110else if (strcmp(argv[0], "script") == 0)111ddb_script(argc, argv);112else if (strcmp(argv[0], "scripts") == 0)113ddb_scripts(argc, argv);114else if (strcmp(argv[0], "unscript") == 0)115ddb_unscript(argc, argv);116else117usage();118}119120int121main(int argc, char *argv[])122{123124/*125* If we've only got one argument and it's an absolute path to a file,126* interpret as a file to be read in.127*/128if (argc == 2 && argv[1][0] == '/' && access(argv[1], R_OK) == 0)129ddb_readfile(argv[1]);130else131ddb_main(argc-1, argv+1);132exit(EX_OK);133}134135136