ddb.c revision 178515
1258945Sroberto/*-
2258945Sroberto * Copyright (c) 2007 Robert N. M. Watson
3258945Sroberto * All rights reserved.
4258945Sroberto *
5258945Sroberto * Redistribution and use in source and binary forms, with or without
6258945Sroberto * modification, are permitted provided that the following conditions
7258945Sroberto * are met:
8258945Sroberto * 1. Redistributions of source code must retain the above copyright
9258945Sroberto *    notice, this list of conditions and the following disclaimer.
10258945Sroberto * 2. Redistributions in binary form must reproduce the above copyright
11258945Sroberto *    notice, this list of conditions and the following disclaimer in the
12258945Sroberto *    documentation and/or other materials provided with the distribution.
13258945Sroberto *
14258945Sroberto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15258945Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16258945Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17258945Sroberto * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18258945Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19258945Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20258945Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21258945Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22258945Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23258945Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24258945Sroberto * SUCH DAMAGE.
25258945Sroberto */
26258945Sroberto
27258945Sroberto#include <sys/cdefs.h>
28258945Sroberto__FBSDID("$FreeBSD: head/sbin/ddb/ddb.c 178515 2008-04-25 17:34:09Z rwatson $");
29258945Sroberto
30258945Sroberto#include <err.h>
31258945Sroberto#include <stdio.h>
32258945Sroberto#include <stdlib.h>
33258945Sroberto#include <string.h>
34258945Sroberto#include <sysexits.h>
35258945Sroberto#include <unistd.h>
36258945Sroberto
37258945Sroberto#include "ddb.h"
38258945Sroberto
39258945Srobertovoid ddb_readfile(char *file);
40258945Srobertovoid ddb_main(int argc, char *argv[]);
41258945Sroberto
42258945Srobertovoid
43258945Srobertousage(void)
44258945Sroberto{
45258945Sroberto
46258945Sroberto	fprintf(stderr, "usage: ddb capture [-M core] [-N system] print\n");
47258945Sroberto	fprintf(stderr, "       ddb capture [-M core] [-N system] status\n");
48258945Sroberto	fprintf(stderr, "       ddb script scriptname\n");
49258945Sroberto	fprintf(stderr, "       ddb script scriptname=script\n");
50258945Sroberto	fprintf(stderr, "       ddb scripts\n");
51258945Sroberto	fprintf(stderr, "       ddb unscript scriptname\n");
52258945Sroberto	fprintf(stderr, "       ddb pathname\n");
53258945Sroberto	exit(EX_USAGE);
54258945Sroberto}
55258945Sroberto
56258945Srobertovoid
57258945Srobertoddb_readfile(char *filename)
58258945Sroberto{
59258945Sroberto	char    buf[BUFSIZ];
60258945Sroberto	FILE*	f;
61258945Sroberto
62258945Sroberto	if ((f = fopen(filename, "r")) == NULL)
63258945Sroberto		err(EX_UNAVAILABLE, "fopen: %s", filename);
64258945Sroberto
65258945Sroberto#define WHITESP		" \t"
66258945Sroberto#define MAXARG	 	2
67258945Sroberto	while (fgets(buf, BUFSIZ, f)) {
68258945Sroberto		int argc = 0;
69258945Sroberto		char *argv[MAXARG];
70258945Sroberto		size_t spn;
71258945Sroberto
72258945Sroberto		spn = strlen(buf);
73258945Sroberto		if (buf[spn-1] == '\n')
74258945Sroberto			buf[spn-1] = '\0';
75258945Sroberto
76258945Sroberto		spn = strspn(buf, WHITESP);
77258945Sroberto		argv[0] = buf + spn;
78258945Sroberto		if (*argv[0] == '#' || *argv[0] == '\0')
79258945Sroberto			continue;
80258945Sroberto		argc++;
81258945Sroberto
82258945Sroberto		spn = strcspn(argv[0], WHITESP);
83258945Sroberto		argv[1] = argv[0] + spn + strspn(argv[0] + spn, WHITESP);;
84258945Sroberto		argv[0][spn] = '\0';
85258945Sroberto		if (*argv[1] != '\0')
86258945Sroberto			argc++;
87258945Sroberto
88258945Sroberto#ifdef DEBUG
89258945Sroberto		{
90258945Sroberto			int i;
91258945Sroberto			printf("argc = %d\n", argc);
92258945Sroberto			for (i = 0; i < argc; i++) {
93258945Sroberto				printf("arg[%d] = %s\n", i, argv[i]);
94258945Sroberto			}
95258945Sroberto		}
96258945Sroberto#endif
97258945Sroberto		ddb_main(argc, argv);
98258945Sroberto	}
99258945Sroberto}
100258945Sroberto
101258945Srobertovoid
102ddb_main(int argc, char *argv[])
103{
104
105	if (argc < 1)
106		usage();
107
108	if (strcmp(argv[0], "capture") == 0)
109		ddb_capture(argc, argv);
110	else if (strcmp(argv[0], "script") == 0)
111		ddb_script(argc, argv);
112	else if (strcmp(argv[0], "scripts") == 0)
113		ddb_scripts(argc, argv);
114	else if (strcmp(argv[0], "unscript") == 0)
115		ddb_unscript(argc, argv);
116	else
117		usage();
118}
119
120int
121main(int argc, char *argv[])
122{
123
124	/*
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	 */
128	if (argc == 2 && argv[1][0] == '/' && access(argv[1], R_OK) == 0)
129		ddb_readfile(argv[1]);
130	else
131		ddb_main(argc-1, argv+1);
132	exit(EX_OK);
133}
134