1174915Srwatson/*-
2174915Srwatson * Copyright (c) 2007 Robert N. M. Watson
3174915Srwatson * All rights reserved.
4174915Srwatson *
5174915Srwatson * Redistribution and use in source and binary forms, with or without
6174915Srwatson * modification, are permitted provided that the following conditions
7174915Srwatson * are met:
8174915Srwatson * 1. Redistributions of source code must retain the above copyright
9174915Srwatson *    notice, this list of conditions and the following disclaimer.
10174915Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11174915Srwatson *    notice, this list of conditions and the following disclaimer in the
12174915Srwatson *    documentation and/or other materials provided with the distribution.
13174915Srwatson *
14174915Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15174915Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16174915Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17174915Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18174915Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19174915Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20174915Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21174915Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22174915Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23174915Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24174915Srwatson * SUCH DAMAGE.
25174915Srwatson */
26174915Srwatson
27174915Srwatson#include <sys/cdefs.h>
28174915Srwatson__FBSDID("$FreeBSD$");
29174915Srwatson
30176833Sbrooks#include <err.h>
31174915Srwatson#include <stdio.h>
32174915Srwatson#include <stdlib.h>
33174915Srwatson#include <string.h>
34174915Srwatson#include <sysexits.h>
35176833Sbrooks#include <unistd.h>
36174915Srwatson
37174915Srwatson#include "ddb.h"
38174915Srwatson
39176833Sbrooksvoid ddb_readfile(char *file);
40176833Sbrooksvoid ddb_main(int argc, char *argv[]);
41176833Sbrooks
42174915Srwatsonvoid
43174915Srwatsonusage(void)
44174915Srwatson{
45174915Srwatson
46178515Srwatson	fprintf(stderr, "usage: ddb capture [-M core] [-N system] print\n");
47178515Srwatson	fprintf(stderr, "       ddb capture [-M core] [-N system] status\n");
48178515Srwatson	fprintf(stderr, "       ddb script scriptname\n");
49177909Sru	fprintf(stderr, "       ddb script scriptname=script\n");
50177909Sru	fprintf(stderr, "       ddb scripts\n");
51177909Sru	fprintf(stderr, "       ddb unscript scriptname\n");
52177909Sru	fprintf(stderr, "       ddb pathname\n");
53174915Srwatson	exit(EX_USAGE);
54174915Srwatson}
55174915Srwatson
56176833Sbrooksvoid
57176833Sbrooksddb_readfile(char *filename)
58174915Srwatson{
59176833Sbrooks	char    buf[BUFSIZ];
60176833Sbrooks	FILE*	f;
61174915Srwatson
62176833Sbrooks	if ((f = fopen(filename, "r")) == NULL)
63176833Sbrooks		err(EX_UNAVAILABLE, "fopen: %s", filename);
64176833Sbrooks
65176833Sbrooks#define WHITESP		" \t"
66176833Sbrooks#define MAXARG	 	2
67176833Sbrooks	while (fgets(buf, BUFSIZ, f)) {
68176833Sbrooks		int argc = 0;
69176833Sbrooks		char *argv[MAXARG];
70176833Sbrooks		size_t spn;
71176833Sbrooks
72176833Sbrooks		spn = strlen(buf);
73176833Sbrooks		if (buf[spn-1] == '\n')
74176833Sbrooks			buf[spn-1] = '\0';
75176833Sbrooks
76176833Sbrooks		spn = strspn(buf, WHITESP);
77176833Sbrooks		argv[0] = buf + spn;
78176833Sbrooks		if (*argv[0] == '#' || *argv[0] == '\0')
79176833Sbrooks			continue;
80176833Sbrooks		argc++;
81176833Sbrooks
82176833Sbrooks		spn = strcspn(argv[0], WHITESP);
83242544Seadler		argv[1] = argv[0] + spn + strspn(argv[0] + spn, WHITESP);
84176833Sbrooks		argv[0][spn] = '\0';
85176833Sbrooks		if (*argv[1] != '\0')
86176833Sbrooks			argc++;
87176833Sbrooks
88176833Sbrooks#ifdef DEBUG
89176833Sbrooks		{
90176833Sbrooks			int i;
91176833Sbrooks			printf("argc = %d\n", argc);
92176833Sbrooks			for (i = 0; i < argc; i++) {
93176833Sbrooks				printf("arg[%d] = %s\n", i, argv[i]);
94176833Sbrooks			}
95176833Sbrooks		}
96176833Sbrooks#endif
97176833Sbrooks		ddb_main(argc, argv);
98176833Sbrooks	}
99215658Skevlo	fclose(f);
100176833Sbrooks}
101176833Sbrooks
102176833Sbrooksvoid
103176833Sbrooksddb_main(int argc, char *argv[])
104176833Sbrooks{
105176833Sbrooks
106176833Sbrooks	if (argc < 1)
107174915Srwatson		usage();
108174915Srwatson
109178515Srwatson	if (strcmp(argv[0], "capture") == 0)
110178515Srwatson		ddb_capture(argc, argv);
111178515Srwatson	else if (strcmp(argv[0], "script") == 0)
112174915Srwatson		ddb_script(argc, argv);
113174915Srwatson	else if (strcmp(argv[0], "scripts") == 0)
114174915Srwatson		ddb_scripts(argc, argv);
115174915Srwatson	else if (strcmp(argv[0], "unscript") == 0)
116174915Srwatson		ddb_unscript(argc, argv);
117174915Srwatson	else
118174915Srwatson		usage();
119176833Sbrooks}
120176833Sbrooks
121176833Sbrooksint
122176833Sbrooksmain(int argc, char *argv[])
123176833Sbrooks{
124176833Sbrooks
125176833Sbrooks	/*
126176833Sbrooks	 * If we've only got one argument and it's an absolute path to a file,
127176833Sbrooks	 * interpret as a file to be read in.
128176833Sbrooks	 */
129176833Sbrooks	if (argc == 2 && argv[1][0] == '/' && access(argv[1], R_OK) == 0)
130176833Sbrooks		ddb_readfile(argv[1]);
131176833Sbrooks	else
132176833Sbrooks		ddb_main(argc-1, argv+1);
133174915Srwatson	exit(EX_OK);
134174915Srwatson}
135