1 /* =========================================================================
2 zsp - description
3
4 Copyright (c) the Contributors as noted in the AUTHORS file.
5 This file is part of CZMQ, the high-level C binding for 0MQ:
6 http://czmq.zeromq.org.
7
8 This Source Code Form is subject to the terms of the Mozilla Public
9 License, v. 2.0. If a copy of the MPL was not distributed with this
10 file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 =========================================================================
12 */
13
14 /*
15 @header
16 zsp - ZSubProc testing tool
17 @discuss
18 Command line tool to support tests for zsubproc class
19 @end
20 */
21
22 #include "czmq_classes.h"
23
main(int argc,char * argv[])24 int main (int argc, char *argv [])
25 {
26 int argn;
27
28 bool verbose = false;
29 bool use_stdin = false;
30 bool use_stderr = false;
31 bool use_stdout = false;
32 bool abrt = false;
33 int quit = 0;
34
35 char *message = NULL;
36
37 for (argn = 1; argn < argc; argn++) {
38 if (streq (argv [argn], "--help")
39 || streq (argv [argn], "-h")) {
40 puts ("zsp [options] ...");
41 #if ! defined (__WINDOWS__)
42 puts (" --stdin / -i read input from stdin");
43 #endif
44 puts (" --stderr / -e output on stderr");
45 puts (" --stdout / -o output on stdout");
46 puts (" --abrt / -a crash with SIGABRT on start");
47 puts (" --verbose / -v verbose mode");
48 puts (" --quit / -q X quit after X seconds");
49 puts (" --help / -h this information");
50 return 0;
51 }
52 else
53 if (streq (argv [argn], "--stderr")
54 || streq (argv [argn], "-e"))
55 use_stderr = true;
56 else
57 if (streq (argv [argn], "--stdout")
58 || streq (argv [argn], "-o"))
59 use_stdout = true;
60 else
61 if (streq (argv [argn], "--stdin")
62 || streq (argv [argn], "-i"))
63 use_stdin = true;
64 else
65 if (streq (argv [argn], "--verbose")
66 || streq (argv [argn], "-v"))
67 verbose = true;
68 else
69 if (streq (argv [argn], "--abrt")
70 || streq (argv [argn], "-a"))
71 abrt = true;
72 else
73 if (streq (argv [argn], "--quit")
74 || streq (argv [argn], "-q")) {
75 quit = atoi (argv [argn + 1]) * 1000;
76 ++argn;
77 }
78 else
79 if (argv [argn][0] == '-') {
80 printf ("Unknown option: %s\n", argv [argn]);
81 return 1;
82 }
83 else {
84 message = argv[argn];
85 }
86 }
87
88 zsys_init ();
89 if (verbose)
90 zsys_debug ("argn=%d, argc=%d", argn, argc);
91
92 message = "Lorem ipsum";
93 if (getenv ("ZSP_MESSAGE"))
94 message = getenv ("ZSP_MESSAGE");
95
96 zfile_t *stdinf = NULL;
97
98 if (use_stdin) {
99 stdinf = zfile_new ("/dev", "stdin");
100 int r = zfile_input (stdinf);
101 assert (r == 0);
102 }
103
104 if (abrt) {
105 if (verbose)
106 zsys_info ("Going to abort myself");
107 #if defined (__WINDOWS__)
108 assert (false); // TODO: how to do kill myelf on Windows?
109 #else
110 kill (getpid (), SIGABRT);
111 #endif
112 }
113
114 // Insert main code here
115 int64_t start = zclock_mono ();
116 while (!zsys_interrupted) {
117 #if ! defined (__WINDOWS__)
118 if (use_stdin) {
119 const char *line = zfile_readln (stdinf);
120
121 if (!line && errno != 0) {
122 zsys_error ("Error reading stdin: %s", strerror (errno));
123 exit (EXIT_FAILURE);
124 }
125
126 if (zfile_eof (stdinf) || !line)
127 break;
128
129 message = (char*) line;
130 }
131 #endif
132 if (use_stderr)
133 fprintf (stderr, "%s\n", message);
134 if (use_stdout)
135 fprintf (stdout, "%s\n", message);
136
137 zclock_sleep (50);
138 if (quit && zclock_mono () - start > quit) break;
139 }
140
141 zfile_destroy (&stdinf);
142
143 return 0;
144 }
145