xref: /freebsd/sbin/ddb/ddb.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
1 /*-
2  * Copyright (c) 2007 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <err.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sysexits.h>
35 #include <unistd.h>
36 
37 #include "ddb.h"
38 
39 void ddb_readfile(char *file);
40 void ddb_main(int argc, char *argv[]);
41 
42 void
43 usage(void)
44 {
45 
46 	fprintf(stderr, "usage: ddb capture [-M core] [-N system] print\n");
47 	fprintf(stderr, "       ddb capture [-M core] [-N system] status\n");
48 	fprintf(stderr, "       ddb script scriptname\n");
49 	fprintf(stderr, "       ddb script scriptname=script\n");
50 	fprintf(stderr, "       ddb scripts\n");
51 	fprintf(stderr, "       ddb unscript scriptname\n");
52 	fprintf(stderr, "       ddb pathname\n");
53 	exit(EX_USAGE);
54 }
55 
56 void
57 ddb_readfile(char *filename)
58 {
59 	char    buf[BUFSIZ];
60 	FILE*	f;
61 
62 	if ((f = fopen(filename, "r")) == NULL)
63 		err(EX_UNAVAILABLE, "fopen: %s", filename);
64 
65 #define WHITESP		" \t"
66 #define MAXARG	 	2
67 	while (fgets(buf, BUFSIZ, f)) {
68 		int argc = 0;
69 		char *argv[MAXARG];
70 		size_t spn;
71 
72 		spn = strlen(buf);
73 		if (buf[spn-1] == '\n')
74 			buf[spn-1] = '\0';
75 
76 		spn = strspn(buf, WHITESP);
77 		argv[0] = buf + spn;
78 		if (*argv[0] == '#' || *argv[0] == '\0')
79 			continue;
80 		argc++;
81 
82 		spn = strcspn(argv[0], WHITESP);
83 		argv[1] = argv[0] + spn + strspn(argv[0] + spn, WHITESP);;
84 		argv[0][spn] = '\0';
85 		if (*argv[1] != '\0')
86 			argc++;
87 
88 #ifdef DEBUG
89 		{
90 			int i;
91 			printf("argc = %d\n", argc);
92 			for (i = 0; i < argc; i++) {
93 				printf("arg[%d] = %s\n", i, argv[i]);
94 			}
95 		}
96 #endif
97 		ddb_main(argc, argv);
98 	}
99 }
100 
101 void
102 ddb_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 
120 int
121 main(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