xref: /freebsd/sbin/ddb/ddb.c (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
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