xref: /plan9-contrib/sys/src/9/boot/aux.c (revision 1936bb650459bace06c38a45b60888b47e5cd459)
1 #include <u.h>
2 #include <libc.h>
3 #include <../boot/boot.h>
4 
5 /*
6 int
7 plumb(char *dir, char *dest, int *efd, char *here)
8 {
9 	char buf[128];
10 	char name[128];
11 	int n;
12 
13 	sprint(name, "%s/clone", dir);
14 	efd[0] = open(name, ORDWR);
15 	if(efd[0] < 0)
16 		return -1;
17 	n = read(efd[0], buf, sizeof(buf)-1);
18 	if(n < 0){
19 		close(efd[0]);
20 		return -1;
21 	}
22 	buf[n] = 0;
23 	sprint(name, "%s/%s/data", dir, buf);
24 	if(here){
25 		sprint(buf, "announce %s", here);
26 		if(sendmsg(efd[0], buf) < 0){
27 			close(efd[0]);
28 			return -1;
29 		}
30 	}
31 	sprint(buf, "connect %s", dest);
32 	if(sendmsg(efd[0], buf) < 0){
33 		close(efd[0]);
34 		return -1;
35 	}
36 	efd[1] = open(name, ORDWR);
37 	if(efd[1] < 0){
38 		close(efd[0]);
39 		return -1;
40 	}
41 	return efd[1];
42 }
43 
44 int
45 sendmsg(int fd, char *msg)
46 {
47 	int n;
48 
49 	n = strlen(msg);
50 	if(write(fd, msg, n) != n)
51 		return -1;
52 	return 0;
53 }
54  */
55 
56 void
57 warning(char *s)
58 {
59 	char buf[ERRMAX];
60 
61 	buf[0] = '\0';
62 	errstr(buf, sizeof buf);
63 	fprint(2, "boot: %s: %s\n", s, buf);
64 }
65 
66 void
67 fatal(char *s)
68 {
69 	char buf[ERRMAX];
70 
71 	buf[0] = '\0';
72 	errstr(buf, sizeof buf);
73 	fprint(2, "boot: %s: %s\n", s, buf);
74 	exits(0);
75 }
76 
77 int
78 readfile(char *name, char *buf, int len)
79 {
80 	int f, n;
81 
82 	buf[0] = 0;
83 	f = open(name, OREAD);
84 	if(f < 0)
85 		return -1;
86 	n = read(f, buf, len-1);
87 	if(n >= 0)
88 		buf[n] = 0;
89 	close(f);
90 	return 0;
91 }
92 
93 int
94 writefile(char *name, char *buf, int len)
95 {
96 	int f, n;
97 
98 	f = open(name, OWRITE);
99 	if(f < 0)
100 		return -1;
101 	n = write(f, buf, len);
102 	close(f);
103 	return (n != len) ? -1 : 0;
104 }
105 
106 void
107 setenv(char *name, char *val)
108 {
109 	int f;
110 	char ename[64];
111 
112 	snprint(ename, sizeof ename, "#e/%s", name);
113 	f = create(ename, 1, 0666);
114 	if(f < 0){
115 		fprint(2, "create %s: %r\n", ename);
116 		return;
117 	}
118 	write(f, val, strlen(val));
119 	close(f);
120 }
121 
122 void
123 srvcreate(char *name, int fd)
124 {
125 	char *srvname;
126 	int f;
127 	char buf[64];
128 
129 	srvname = strrchr(name, '/');
130 	if(srvname)
131 		srvname++;
132 	else
133 		srvname = name;
134 
135 	snprint(buf, sizeof buf, "#s/%s", srvname);
136 	f = create(buf, 1, 0666);
137 	if(f < 0)
138 		fatal(buf);
139 	sprint(buf, "%d", fd);
140 	if(write(f, buf, strlen(buf)) != strlen(buf))
141 		fatal("write");
142 	close(f);
143 }
144 
145 void
146 catchint(void *a, char *note)
147 {
148 	USED(a);
149 	if(strcmp(note, "alarm") == 0)
150 		noted(NCONT);
151 	noted(NDFLT);
152 }
153 
154 int
155 outin(char *prompt, char *def, int len)
156 {
157 	int n;
158 	char buf[256];
159 
160 	if(len >= sizeof buf)
161 		len = sizeof(buf)-1;
162 
163 	if(cpuflag){
164 		notify(catchint);
165 		alarm(15*1000);
166 	}
167 	print("%s[%s]: ", prompt, *def ? def : "no default");
168 	memset(buf, 0, sizeof buf);
169 	n = read(0, buf, len);
170 	if(cpuflag){
171 		alarm(0);
172 		notify(0);
173 	}
174 
175 	if(n < 0)
176 		return 1;
177 	if(n > 1){
178 		buf[n-1] = 0;
179 		strcpy(def, buf);
180 	}
181 	return n;
182 }
183