1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2009 Melanie Rhianna Lewis                             |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.0 of the PHP license,       |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt.                                 |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Melanie Rhianna Lewis <cyberspice@php.net>                   |
16    +----------------------------------------------------------------------+
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 
25 #include "php_dio.h"
26 #include "php_dio_common.h"
27 
28 /* {{{ dio_init_stream_data
29  * Initialises the command parts of the stream data.
30  */
dio_init_stream_data(php_dio_stream_data * data)31 void dio_init_stream_data(php_dio_stream_data *data) {
32 	data->stream_type = DIO_STREAM_TYPE_NONE;
33 	data->end_of_file = 0;
34 #ifdef DIO_HAS_FILEPERMS
35 	data->has_perms = 0;
36 	data->perms = 0;
37 #endif
38 #ifdef DIO_NONBLOCK
39 	data->is_blocking = 1;
40 	data->has_timeout = 0;
41 	data->timeout_sec = 0;
42 	data->timeout_usec = 0;
43 	data->timed_out = 0;
44 #endif
45 	/* Serial options */
46 	data->data_rate = 9600;
47 	data->data_bits = 8;
48 	data->stop_bits = 1;
49 	data->parity = 0;
50 	data->flow_control = 1;
51 	data->canonical = 1;
52 }
53 /* }}} */
54 
55 
56 /* {{{ dio_assoc_array_get_basic_options
57  * Retrieves the basic open option values from an associative array
58  */
dio_assoc_array_get_basic_options(zval * options,php_dio_stream_data * data)59 void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data) {
60 #if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
61 	zval      *tmpzval;
62 	HashTable *opthash;
63 
64 	opthash = HASH_OF(options);
65 #endif
66 
67 #ifdef DIO_HAS_FILEPERMS
68 	/* This is the file mode flags used by open(). */
69 	if ((tmpzval = zend_hash_str_find(opthash, "perms", sizeof("perms") -1)) != NULL) {
70 		data->perms = (int)zval_get_long(tmpzval);
71 		data->has_perms = 1;
72 	}
73 #endif
74 
75 #ifdef DIO_NONBLOCK
76 	/* This sets the underlying stream to be blocking/non
77 	   block (i.e. O_NONBLOCK) */
78 	if ((tmpzval = zend_hash_str_find(opthash, "is_blocking", sizeof("is_blocking") -1)) != NULL) {
79 		data->is_blocking = zval_get_long(tmpzval) ? 1 : 0;
80 	}
81 
82 	/* This is the timeout value for reads in seconds.  Only one of
83 	   timeout_secs or timeout_usecs need be defined to define a timeout. */
84 	if ((tmpzval = zend_hash_str_find(opthash, "timeout_secs", sizeof("timeout_secs") -1)) != NULL) {
85 		data->timeout_sec = zval_get_long(tmpzval);
86 	}
87 
88 	/* This is the timeout value for reads in microseconds.  Only one of
89 	   timeout_secs or timeout_usecs need be defined to define a timeout. */
90 	if ((tmpzval = zend_hash_str_find(opthash, "timeout_usecs", sizeof("timeout_usecs") -1)) != NULL) {
91 		data->timeout_usec = zval_get_long(tmpzval);
92 	}
93 
94 	data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
95 #endif
96 }
97 /* }}} */
98 
99 /* {{{ dio_assoc_array_get_serial_options
100  * Retrieves the serial open option values from an associative array
101  */
dio_assoc_array_get_serial_options(zval * options,php_dio_stream_data * data)102 void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data) {
103 	zval *tmpzval;
104 	HashTable *opthash;
105 
106 	opthash = HASH_OF(options);
107 
108 	if ((tmpzval = zend_hash_str_find(opthash, "data_rate", sizeof("data_rate") -1)) != NULL) {
109 		data->data_rate = zval_get_long(tmpzval);
110 	}
111 
112 	if ((tmpzval = zend_hash_str_find(opthash, "data_bits", sizeof("data_bits") -1)) != NULL) {
113 		data->data_bits = (int)zval_get_long(tmpzval);
114 	}
115 
116 	if ((tmpzval = zend_hash_str_find(opthash, "stop_bits", sizeof("stop_bits") -1)) != NULL) {
117 		data->stop_bits = (int)zval_get_long(tmpzval);
118 	}
119 
120 	if ((tmpzval = zend_hash_str_find(opthash, "parity", sizeof("parity") -1)) != NULL) {
121 		data->parity = (int)zval_get_long(tmpzval);
122 	}
123 
124 	if ((tmpzval = zend_hash_str_find(opthash, "flow_control", sizeof("flow_control") -1)) != NULL) {
125 		data->flow_control = (int)(zval_get_long(tmpzval) ? 1 : 0);
126 	}
127 
128 	if ((tmpzval = zend_hash_str_find(opthash, "is_canonical", sizeof("is_canonical") -1)) != NULL) {
129 		data->canonical = (int)(zval_get_long(tmpzval) ? 1 : 0);
130 	}
131 }
132 /* }}} */
133 
134 /* {{{ dio_stream_context_get_raw_options
135  * Extracts the option values for dio.raw mode from a context
136  */
dio_stream_context_get_basic_options(php_stream_context * context,php_dio_stream_data * data)137 void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_stream_data *data) {
138 #if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
139 	zval *tmpzval;
140 #endif
141 
142 #ifdef DIO_HAS_FILEPERMS
143 	/* This is the file mode flags used by open(). */
144 	if ((tmpzval = php_stream_context_get_option(context, "dio", "perms")) != NULL) {
145 		data->perms = (int)zval_get_long(tmpzval);
146 		data->has_perms = 1;
147 	}
148 #endif
149 
150 #ifdef DIO_NONBLOCK
151 	/* This sets the underlying stream to be blocking/non
152 	   block (i.e. O_NONBLOCK) */
153 	if ((tmpzval = php_stream_context_get_option(context, "dio", "is_blocking")) != NULL) {
154 		data->is_blocking = zval_get_long(tmpzval) ? 1 : 0;
155 	}
156 
157 	/* This is the timeout value for reads in seconds.  Only one of
158 	   timeout_secs or timeout_usecs need be defined to define a timeout. */
159 	if ((tmpzval = php_stream_context_get_option(context, "dio", "timeout_secs")) != NULL) {
160 		data->timeout_sec = zval_get_long(tmpzval);
161 	}
162 
163 	/* This is the timeout value for reads in microseconds.  Only one of
164 	   timeout_secs or timeout_usecs need be defined to define a timeout. */
165 	if ((tmpzval = php_stream_context_get_option(context, "dio", "timeout_usecs")) != NULL) {
166 		data->timeout_usec = zval_get_long(tmpzval);
167 	}
168 
169 	data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
170 #endif
171 }
172 /* }}} */
173 
174 /* {{{ dio_stream_context_get_serial_options
175  * Extracts the option values for dio.serial mode from a context
176  */
dio_stream_context_get_serial_options(php_stream_context * context,php_dio_stream_data * data)177 void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_stream_data *data) {
178 	zval *tmpzval;
179 
180 	if ((tmpzval = php_stream_context_get_option(context, "dio", "data_rate")) != NULL) {
181 		data->data_rate = zval_get_long(tmpzval);
182 	}
183 
184 	if ((tmpzval = php_stream_context_get_option(context, "dio", "data_bits")) != NULL) {
185 		data->data_bits = (int)zval_get_long(tmpzval);
186 	}
187 
188 	if ((tmpzval = php_stream_context_get_option(context, "dio", "stop_bits")) != NULL) {
189 		data->stop_bits = (int)zval_get_long(tmpzval);
190 	}
191 
192 	if ((tmpzval = php_stream_context_get_option(context, "dio", "parity")) != NULL) {
193 		data->parity = (int)zval_get_long(tmpzval);
194 	}
195 
196 	if ((tmpzval = php_stream_context_get_option(context, "dio", "flow_control")) != NULL) {
197 		data->flow_control = (int)(zval_get_long(tmpzval) ? 1 : 0);
198 	}
199 
200 	if ((tmpzval = php_stream_context_get_option(context, "dio", "is_canonical")) != NULL) {
201 		data->canonical = (int)(zval_get_long(tmpzval) ? 1 : 0);
202 	}
203 }
204 /* }}} */
205 
206 /*
207  * Local variables:
208  * c-basic-offset: 4
209  * tab-width: 4
210  * End:
211  * vim600: fdm=marker
212  * vim: sw=4 ts=4 noet
213  */
214 
215