summaryrefslogtreecommitdiff
path: root/navit/speech/cmdline/speech_cmdline.c
blob: 71f862215611dcbd5fe09390cd9626ad89d7ae10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <stdlib.h>
#include <glib.h>
#include "config.h"
#include "item.h"
#include "plugin.h"
#include "speech.h"
#ifdef HAVE_API_WIN32_BASE
#include <windows.h>
#include "util.h"
#endif
#ifdef USE_EXEC
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#endif

struct speech_priv {
	char *cmdline;
};

static int 
speechd_say(struct speech_priv *this, const char *text)
{
#ifdef USE_EXEC
	if (!fork()) {
		char *cmdline=g_strdup_printf(this->cmdline, text);
                int argcmax=10;
                char *argv[argcmax];
                int argc=0;
                char *pos=cmdline,end;
                while (*pos && argc < argcmax-1) {
                        end=' ';
                        if (*pos == '\'' || *pos == '\"') {
                                end=*pos++;
                        }
                        argv[argc]=pos;
                        while (*pos && *pos != end)
                                pos++;
                        if (*pos)
                                *pos++='\0';
                        while (*pos == ' ')
                                pos++;
                        if (strcmp(argv[argc], "2>/dev/null") && strcmp(argv[argc],">/dev/null") && strcmp(argv[argc],"&"))
                                argc++;
                }
                argv[argc++]=NULL;
                execvp(argv[0], argv);
                exit(1);
        }
	return 0;
#else
#ifdef HAVE_API_WIN32_BASE
	char *cmdline,*p;
	PROCESS_INFORMATION pr;
	LPCWSTR cmd,arg;
	int ret;


	cmdline=g_strdup_printf(this->cmdline, text);
	p=cmdline;
	while (*p != ' ' && *p != '\0')
		p++;
	if (*p == ' ')
		*p++='\0';
	cmd = newSysString(cmdline);
	arg = newSysString(p);
	ret=!CreateProcess(cmd, arg, NULL, NULL, 0, CREATE_NEW_CONSOLE, NULL, NULL, NULL, &pr);
	g_free(cmd);
	g_free(arg);
	return ret;
#else
	char *cmdline;

	cmdline=g_strdup_printf(this->cmdline, text);
	return system(cmdline);
#endif
#endif
}

static void 
speechd_destroy(struct speech_priv *this) {
	g_free(this->cmdline);
	g_free(this);
}

static struct speech_methods speechd_meth = {
	speechd_destroy,
	speechd_say,
};

static struct speech_priv *
speechd_new(struct speech_methods *meth, struct attr **attrs, struct attr *parent) {
	struct speech_priv *this;
	struct attr *data;
	data=attr_search(attrs, NULL, attr_data);
	if (! data)
		return NULL;
	this=g_new(struct speech_priv,1);
	this->cmdline=g_strdup(data->u.str);
	*meth=speechd_meth;
	return this;
}


void
plugin_init(void)
{
	plugin_register_speech_type("cmdline", speechd_new);
}