]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/netbsd-tests/lib/libcurses/director/director.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / netbsd-tests / lib / libcurses / director / director.c
1 /*      $NetBSD: director.c,v 1.10 2012/06/03 23:19:11 joerg Exp $      */
2
3 /*-
4  * Copyright 2009 Brett Lymn <blymn@NetBSD.org>
5  *
6  * All rights reserved.
7  *
8  * This code has been donated to The NetBSD Foundation by the Author.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software withough specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *
30  */
31
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <ctype.h>
38 #include <termios.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <util.h>
44 #include <err.h>
45 #include "returns.h"
46
47 void yyparse(void);
48 #define DEF_TERMPATH "."
49 #define DEF_TERM "atf"
50 #define DEF_SLAVE "./slave"
51
52 const char *def_check_path = "./"; /* default check path */
53 const char *def_include_path = "./"; /* default include path */
54
55 extern size_t nvars;    /* In testlang_conf.y */
56 saved_data_t  saved_output;     /* In testlang_conf.y */
57 int cmdpipe[2];         /* command pipe between director and slave */
58 int slvpipe[2];         /* reply pipe back from slave */
59 int master;             /* pty to the slave */
60 int verbose;            /* control verbosity of tests */
61 const char *check_path; /* path to prepend to check files for output
62                            validation */
63 const char *include_path;       /* path to prepend to include files */
64 char *cur_file;         /* name of file currently being read */
65
66 void init_parse_variables(int); /* in testlang_parse.y */
67
68 /*
69  * Handle the slave exiting unexpectedly, try to recover the exit message
70  * and print it out.
71  */
72 static void
73 slave_died(int param)
74 {
75         char last_words[256];
76         size_t count;
77
78         fprintf(stderr, "ERROR: Slave has exited\n");
79         if (saved_output.count > 0) {
80                 fprintf(stderr, "output from slave: ");
81                 for (count = 0; count < saved_output.count; count ++) {
82                         if (isprint((unsigned char)saved_output.data[count]))
83                             fprintf(stderr, "%c", saved_output.data[count]);
84                 }
85                 fprintf(stderr, "\n");
86         }
87
88         if ((count = read(master, &last_words, 255)) > 0) {
89                 last_words[count] = '\0';
90                 fprintf(stderr, "slave exited with message \"%s\"\n",
91                         last_words);
92         }
93
94         exit(2);
95 }
96
97
98 static void
99 usage(void)
100 {
101         fprintf(stderr, "Usage: %s [-v] [-I include-path] [-C check-path] "
102             "[-T terminfo-file] [-s pathtoslave] [-t term] "
103             "commandfile\n", getprogname());
104         fprintf(stderr, " where:\n");
105         fprintf(stderr, "    -v enables verbose test output\n");
106         fprintf(stderr, "    -T is a directory containing the terminfo.cdb "
107             "file, or a file holding the terminfo description n");
108         fprintf(stderr, "    -s is the path to the slave executable\n");
109         fprintf(stderr, "    -t is value to set TERM to for the test\n");
110         fprintf(stderr, "    -I is the directory to include files\n");
111         fprintf(stderr, "    -C is the directory for config files\n");
112         fprintf(stderr, "    commandfile is a file of test directives\n");
113         exit(1);
114 }
115
116
117 int
118 main(int argc, char *argv[])
119 {
120         extern char *optarg;
121         extern int optind;
122         const char *termpath, *term, *slave;
123         int ch;
124         pid_t slave_pid;
125         extern FILE *yyin;
126         char *arg1, *arg2, *arg3, *arg4;
127         struct termios term_attr;
128         struct stat st;
129
130         termpath = term = slave = NULL;
131         verbose = 0;
132
133         while ((ch = getopt(argc, argv, "vC:I:p:s:t:T:")) != -1) {
134                 switch(ch) {
135                 case 'I':
136                         include_path = optarg;
137                         break;
138                 case 'C':
139                         check_path = optarg;
140                         break;
141                 case 'T':
142                         termpath = optarg;
143                         break;
144                 case 'p':
145                         termpath = optarg;
146                         break;
147                 case 's':
148                         slave = optarg;
149                         break;
150                 case 't':
151                         term = optarg;
152                         break;
153                 case 'v':
154                         verbose = 1;
155                         break;
156                 case '?':
157                 default:
158                         usage();
159                         break;
160                 }
161         }
162
163         argc -= optind;
164         argv += optind;
165         if (argc < 1)
166                 usage();
167
168         if (termpath == NULL)
169                 termpath = DEF_TERMPATH;
170
171         if (slave == NULL)
172                 slave = DEF_SLAVE;
173
174         if (term == NULL)
175                 term = DEF_TERM;
176
177         if (check_path == NULL)
178                 check_path = getenv("CHECK_PATH");
179         if ((check_path == NULL) || (check_path[0] == '\0')) {
180                 warn("$CHECK_PATH not set, defaulting to %s", def_check_path);
181                 check_path = def_check_path;
182         }
183
184         if (include_path == NULL)
185                 include_path = getenv("INCLUDE_PATH");
186         if ((include_path == NULL) || (include_path[0] == '\0')) {
187                 warn("$INCLUDE_PATH not set, defaulting to %s",
188                         def_include_path);
189                 include_path = def_include_path;
190         }
191
192         signal(SIGCHLD, slave_died);
193
194         if (setenv("TERM", term, 1) != 0)
195                 err(2, "Failed to set TERM variable");
196
197         if (stat(termpath, &st) == -1)
198                 err(1, "Cannot stat %s", termpath);
199
200         if (S_ISDIR(st.st_mode)) {
201                 char tinfo[MAXPATHLEN];
202                 int l = snprintf(tinfo, sizeof(tinfo), "%s/%s", termpath,
203                     "terminfo.cdb");
204                 if (stat(tinfo, &st) == -1)
205                         err(1, "Cannot stat `%s'", tinfo);
206                 if (l >= 4)
207                         tinfo[l - 4] = '\0';
208                 if (setenv("TERMINFO", tinfo, 1) != 0)
209                         err(1, "Failed to set TERMINFO variable");
210         } else {
211                 int fd;
212                 char *tinfo;
213                 if ((fd = open(termpath, O_RDONLY)) == -1)
214                         err(1, "Cannot open `%s'", termpath);
215                 if ((tinfo = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE,
216                         fd, 0)) == MAP_FAILED)
217                         err(1, "Cannot map `%s'", termpath);
218                 if (setenv("TERMINFO", tinfo, 1) != 0)
219                         err(1, "Failed to set TERMINFO variable");
220                 close(fd);
221                 munmap(tinfo, (size_t)st.st_size);
222         }
223
224         if (pipe(cmdpipe) < 0)
225                 err(1, "Command pipe creation failed");
226
227         if (pipe(slvpipe) < 0)
228                 err(1, "Slave pipe creation failed");
229
230         /*
231          * Create default termios settings for later use
232          */
233         memset(&term_attr, 0, sizeof(term_attr));
234         term_attr.c_iflag = TTYDEF_IFLAG;
235         term_attr.c_oflag = TTYDEF_OFLAG;
236         term_attr.c_cflag = TTYDEF_CFLAG;
237         term_attr.c_lflag = TTYDEF_LFLAG;
238         cfsetspeed(&term_attr, TTYDEF_SPEED);
239         term_attr.c_cc[VERASE] = '\b';
240         term_attr.c_cc[VKILL] = '\025'; /* ^U */
241
242         if ((slave_pid = forkpty(&master, NULL, &term_attr, NULL)) < 0)
243                 err(1, "Fork of pty for slave failed\n");
244
245         if (slave_pid == 0) {
246                 /* slave side, just exec the slave process */
247                 if (asprintf(&arg1, "%d", cmdpipe[0]) < 0)
248                         err(1, "arg1 conversion failed");
249
250                 if (asprintf(&arg2, "%d", cmdpipe[1]) < 0)
251                         err(1, "arg2 conversion failed");
252
253                 if (asprintf(&arg3, "%d", slvpipe[0]) < 0)
254                         err(1, "arg3 conversion failed");
255
256                 if (asprintf(&arg4, "%d", slvpipe[1]) < 0)
257                         err(1, "arg4 conversion failed");
258
259                 if (execl(slave, slave, arg1, arg2, arg3, arg4, NULL) < 0)
260                         err(1, "Exec of slave %s failed", slave);
261
262                 /* NOT REACHED */
263         }
264
265         fcntl(master, F_SETFL, O_NONBLOCK);
266
267         if ((yyin = fopen(argv[0], "r")) == NULL)
268                 err(1, "Cannot open command file %s", argv[0]);
269
270         if ((cur_file = strdup(argv[0])) == NULL)
271                 err(2, "Failed to alloc memory for test file name");
272
273         init_parse_variables(1);
274
275         yyparse();
276         fclose(yyin);
277
278         exit(0);
279 }