whitespace squelching in netsource.c
[jack.git] / tools / ipload.c
blob4ce93698d91a5b3e71951723706c575917edc435
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <unistd.h>
6 #include <getopt.h>
7 #include <jack/jack.h>
8 #include <jack/intclient.h>
10 jack_client_t *client;
11 jack_intclient_t intclient;
12 char *client_name;
13 char *intclient_name;
14 char *load_name;
15 char *load_init = NULL;
16 char *server_name = NULL;
17 int wait_opt = 0;
19 void
20 signal_handler (int sig)
22 jack_status_t status;
24 fprintf (stderr, "signal received, unloading...");
25 status = jack_internal_client_unload (client, intclient);
26 if (status & JackFailure)
27 fprintf (stderr, "(failed), status = 0x%2.0x\n", status);
28 else
29 fprintf (stderr, "(succeeded)\n");
30 jack_client_close (client);
31 exit (0);
34 void
35 show_usage ()
37 fprintf (stderr, "usage: %s [ options ] client-name [ load-name "
38 "[ init-string]]\n\noptions:\n", client_name);
39 fprintf (stderr,
40 "\t-h, --help \t\t print help message\n"
41 "\t-i, --init string\t initialize string\n"
42 "\t-s, --server name\t select JACK server\n"
43 "\t-w, --wait \t\t wait for signal, then unload\n"
44 "\n"
48 int
49 parse_args (int argc, char *argv[])
51 int c;
52 int option_index = 0;
53 char *short_options = "hi:s:w";
54 struct option long_options[] = {
55 { "help", 0, 0, 'h' },
56 { "init", required_argument, 0, 'i' },
57 { "server", required_argument, 0, 's' },
58 { "wait", 0, 0, 'w' },
59 { 0, 0, 0, 0 }
62 client_name = strrchr(argv[0], '/');
63 if (client_name == NULL) {
64 client_name = argv[0];
65 } else {
66 client_name++;
69 while ((c = getopt_long (argc, argv, short_options, long_options,
70 &option_index)) >= 0) {
71 switch (c) {
72 case 'i':
73 load_init = optarg;
74 break;
75 case 's':
76 server_name = optarg;
77 break;
78 case 'w':
79 wait_opt = 1;
80 break;
81 case 'h':
82 default:
83 show_usage ();
84 return 1;
88 if (optind == argc) { /* no positional args? */
89 show_usage ();
90 return 1;
92 if (optind < argc)
93 load_name = intclient_name = argv[optind++];
95 if (optind < argc)
96 load_name = argv[optind++];
98 if (optind < argc)
99 load_init = argv[optind++];
101 //fprintf (stderr, "client-name = `%s', load-name = `%s', "
102 // "load-init = `%s', wait = %d\n",
103 // intclient_name, load_name, load_init, wait_opt);
105 return 0; /* args OK */
109 main (int argc, char *argv[])
111 jack_status_t status;
113 /* parse and validate command arguments */
114 if (parse_args (argc, argv))
115 exit (1); /* invalid command line */
117 /* first, become a JACK client */
118 client = jack_client_open (client_name, JackServerName,
119 &status, server_name);
120 if (client == NULL) {
121 fprintf (stderr, "jack_client_open() failed, "
122 "status = 0x%2.0x\n", status);
123 if (status & JackServerFailed) {
124 fprintf (stderr, "Unable to connect to JACK server\n");
126 exit (1);
128 if (status & JackServerStarted) {
129 fprintf (stderr, "JACK server started\n");
131 if (status & JackNameNotUnique) {
132 client_name = jack_get_client_name(client);
133 fprintf (stderr, "unique name `%s' assigned\n", client_name);
136 /* then, load the internal client */
137 intclient = jack_internal_client_load (client, intclient_name,
138 (JackLoadName|JackLoadInit),
139 &status, load_name, load_init);
140 if (status & JackFailure) {
141 fprintf (stderr, "could not load %s, status = 0x%2.0x\n",
142 load_name, status);
143 return 2;
145 if (status & JackNameNotUnique) {
146 intclient_name =
147 jack_get_internal_client_name (client, intclient);
148 fprintf (stderr, "unique internal client name `%s' assigned\n",
149 intclient_name);
152 fprintf (stdout, "%s is running.\n", load_name);
154 if (wait_opt) {
155 /* define a signal handler to unload the client, then
156 * wait for it to exit */
157 signal (SIGQUIT, signal_handler);
158 signal (SIGTERM, signal_handler);
159 signal (SIGHUP, signal_handler);
160 signal (SIGINT, signal_handler);
162 while (1) {
163 sleep (1);
167 return 0;