xref: /freebsd/sbin/ddb/ddb.c (revision 7431dfd4580e850375fe5478d92ec770344db098)
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 	fclose(f);
100 }
101 
102 void
103 ddb_main(int argc, char *argv[])
104 {
105 
106 	if (argc < 1)
107 		usage();
108 
109 	if (strcmp(argv[0], "capture") == 0)
110 		ddb_capture(argc, argv);
111 	else if (strcmp(argv[0], "script") == 0)
112 		ddb_script(argc, argv);
113 	else if (strcmp(argv[0], "scripts") == 0)
114 		ddb_scripts(argc, argv);
115 	else if (strcmp(argv[0], "unscript") == 0)
116 		ddb_unscript(argc, argv);
117 	else
118 		usage();
119 }
120 
121 int
122 main(int argc, char *argv[])
123 {
124 
125 	/*
126 	 * If we've only got one argument and it's an absolute path to a file,
127 	 * interpret as a file to be read in.
128 	 */
129 	if (argc == 2 && argv[1][0] == '/' && access(argv[1], R_OK) == 0)
130 		ddb_readfile(argv[1]);
131 	else
132 		ddb_main(argc-1, argv+1);
133 	exit(EX_OK);
134 }
135