1 /*-
2  * Copyright (c) 2013 Nathan Whitehorn
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  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <errno.h>
31 #include <libutil.h>
32 #include <inttypes.h>
33 
34 #include <libgeom.h>
35 #include <dialog.h>
36 #include <dlg_keys.h>
37 
38 #include "partedit.h"
39 
40 static struct gprovider *
41 provider_for_name(struct gmesh *mesh, const char *name)
42 {
43 	struct gclass *classp;
44 	struct gprovider *pp = NULL;
45 	struct ggeom *gp;
46 
47 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
48 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
49 			if (LIST_EMPTY(&gp->lg_provider))
50 				continue;
51 
52 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
53 				if (strcmp(pp->lg_name, name) == 0)
54 					break;
55 
56 			if (pp != NULL) break;
57 		}
58 
59 		if (pp != NULL) break;
60 	}
61 
62 	return (pp);
63 }
64 
65 static int
66 part_config(char *disk, const char *scheme, char *config)
67 {
68 	char *partition, *ap, *size = NULL, *type = NULL, *mount = NULL;
69 	struct gclass *classp;
70 	struct gmesh mesh;
71 	struct ggeom *gpart = NULL;
72 	int error;
73 
74 	if (scheme == NULL)
75 		scheme = default_scheme();
76 
77 	error = geom_gettree(&mesh);
78 	if (provider_for_name(&mesh, disk) == NULL) {
79 		fprintf(stderr, "GEOM provider %s not found\n", disk);
80 		geom_deletetree(&mesh);
81 		return (-1);
82 	}
83 
84 	/* Remove any existing partitioning and create new scheme */
85 	LIST_FOREACH(classp, &mesh.lg_class, lg_class)
86 		if (strcmp(classp->lg_name, "PART") == 0)
87 			break;
88         if (classp != NULL) {
89 		LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
90 		if (strcmp(gpart->lg_name, disk) == 0)
91 			break;
92 	}
93 	if (gpart != NULL)
94 		gpart_destroy(gpart);
95 	gpart_partition(disk, scheme);
96 
97 	if (strcmp(scheme, "PC98") == 0 || strcmp(scheme, "MBR") == 0) {
98 		struct gmesh submesh;
99 		geom_gettree(&submesh);
100 		gpart_create(provider_for_name(&submesh, disk),
101 		    "freebsd", NULL, NULL, &disk, 0);
102 		geom_deletetree(&submesh);
103 	} else {
104 		disk= strdup(disk);
105 	}
106 
107 	geom_deletetree(&mesh);
108 	error = geom_gettree(&mesh);
109 
110 	/* Create partitions */
111 	if (config == NULL) {
112 		wizard_makeparts(&mesh, disk, "ufs", 0);
113 		goto finished;
114 	}
115 
116 	while ((partition = strsep(&config, ",")) != NULL) {
117 		while ((ap = strsep(&partition, " \t\n")) != NULL) {
118 			if (*ap == '\0')
119 				continue;
120 			if (size == NULL)
121 				size = ap;
122 			else if (type == NULL)
123 				type = ap;
124 			else if (mount == NULL)
125 				mount = ap;
126 		}
127 		if (size == NULL)
128 			continue;
129 		if (strcmp(size, "auto") == 0)
130 			size = NULL;
131 		gpart_create(provider_for_name(&mesh, disk), type, size, mount,
132 		    NULL, 0);
133 		geom_deletetree(&mesh);
134 		error = geom_gettree(&mesh);
135 		size = type = mount = NULL;
136 	}
137 
138 finished:
139 	geom_deletetree(&mesh);
140 	free(disk);
141 
142 	return (0);
143 }
144 
145 static
146 int parse_disk_config(char *input)
147 {
148 	char *ap;
149 	char *disk = NULL, *scheme = NULL, *partconfig = NULL;
150 
151 	while (input != NULL && *input != 0) {
152 		if (isspace(*input)) {
153 			input++;
154 			continue;
155 		}
156 
157 		switch(*input) {
158 		case '{':
159 			input++;
160 			partconfig = strchr(input, '}');
161 			if (partconfig == NULL) {
162 				fprintf(stderr, "Malformed partition setup "
163 				    "string: %s\n", input);
164 				return (1);
165 			}
166 			*partconfig = '\0';
167 			ap = partconfig+1;
168 			partconfig = input;
169 			input = ap;
170 			break;
171 		default:
172 			if (disk == NULL)
173 				disk = strsep(&input, " \t\n");
174 			else if (scheme == NULL)
175 				scheme = strsep(&input, " \t\n");
176 			else {
177 				fprintf(stderr, "Unknown directive: %s\n",
178 				    strsep(&input, " \t\n"));
179 				return (1);
180 			}
181 		}
182 	} while (input != NULL && *input != 0);
183 
184 	if (disk != NULL)
185 		return (part_config(disk, scheme, partconfig));
186 
187 	return (0);
188 }
189 
190 int
191 scripted_editor(int argc, const char **argv)
192 {
193 	char *token;
194 	int i, error = 0, len = 0;
195 
196 	for (i = 1; i < argc; i++)
197 		len += strlen(argv[i]) + 1;
198 	char inputbuf[len], *input = inputbuf;
199 	strcpy(input, argv[1]);
200 	for (i = 2; i < argc; i++) {
201 		strcat(input, " ");
202 		strcat(input, argv[i]);
203 	}
204 
205 	while ((token = strsep(&input, ";")) != NULL) {
206 		error = parse_disk_config(token);
207 		if (error != 0)
208 			return (error);
209 	}
210 
211 	return (0);
212 }
213 
214