2 +++ b/ext/dio/config.m4
5 +dnl $Id: config.m4 291957 2009-12-10 17:13:14Z cyberspice $
8 +PHP_ARG_ENABLE(dio, whether to enable direct I/O support,
9 +[ --enable-dio Enable direct I/O support])
11 +if test "$PHP_DIO" != "no"; then
12 + PHP_NEW_EXTENSION(dio, dio.c dio_common.c dio_posix.c dio_stream_wrappers.c, $ext_shared)
18 + +----------------------------------------------------------------------+
20 + +----------------------------------------------------------------------+
21 + | Copyright (c) 1997-2009 The PHP Group |
22 + +----------------------------------------------------------------------+
23 + | This source file is subject to version 3.0 of the PHP license, |
24 + | that is bundled with this package in the file LICENSE, and is |
25 + | available through the world-wide-web at the following url: |
26 + | http://www.php.net/license/3_0.txt. |
27 + | If you did not receive a copy of the PHP license and are unable to |
28 + | obtain it through the world-wide-web, please send a note to |
29 + | license@php.net so we can mail you a copy immediately. |
30 + +----------------------------------------------------------------------+
31 + | Author: Sterling Hughes <sterling@php.net> |
32 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
33 + +----------------------------------------------------------------------+
42 +#include "ext/standard/info.h"
45 +#include "php_dio_stream_wrappers.h"
47 +#include <sys/stat.h>
48 +#include <sys/types.h>
59 +/* e.g. IRIX does not have CRTSCTS */
62 +# define CRTSCTS CNEW_RTSCTS
65 +# endif /* CNEW_RTSCTS */
66 +#endif /* !CRTSCTS */
69 + +----------------------------------------------------------------------+
70 + | DEPRECATED FUNCTIONALITY |
71 + +----------------------------------------------------------------------+
72 + | The functions below are from the earlier DIO versions. They will |
73 + | continue to be maintained but not extended. It is thoroughly |
74 + | recommended that you should use either the stream wrappers or the |
75 + | DIO classes in new code. - Melanie |
76 + +----------------------------------------------------------------------+
79 +#define le_fd_name "Direct I/O File Descriptor"
82 +static int new_php_fd(php_fd_t **f, int fd)
84 + if (!(*f = malloc(sizeof(php_fd_t)))) {
91 +static void _dio_close_fd(zend_rsrc_list_entry *rsrc TSRMLS_DC)
93 + php_fd_t *f = (php_fd_t *) rsrc->ptr;
100 +/* {{{ proto resource dio_open(string filename, int flags[, int mode])
101 + Open a new filename with specified permissions of flags and creation permissions of mode */
102 +PHP_FUNCTION(dio_open)
106 + int file_name_length;
111 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", &file_name, &file_name_length, &flags, &mode) == FAILURE) {
115 + if (php_check_open_basedir(file_name TSRMLS_CC) || (PG(safe_mode) && !php_checkuid(file_name, "wb+", CHECKUID_CHECK_MODE_PARAM))) {
119 + if (ZEND_NUM_ARGS() == 3) {
120 + fd = open(file_name, flags, mode);
122 + fd = open(file_name, flags);
126 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot open file %s with flags %ld and permissions %ld: %s", file_name, flags, mode, strerror(errno));
131 + if (!new_php_fd(&f, fd)) {
134 + ZEND_REGISTER_RESOURCE(return_value, f, le_fd);
138 +/* {{{ proto string dio_read(resource fd[, int n])
139 + Read n bytes from fd and return them, if n is not specified, read 1k */
140 +PHP_FUNCTION(dio_read)
148 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &r_fd, &bytes) == FAILURE) {
152 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
155 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0.");
159 + data = emalloc(bytes + 1);
160 + res = read(f->fd, data, bytes);
166 + data = erealloc(data, res + 1);
169 + RETURN_STRINGL(data, res, 0);
173 +/* {{{ proto int dio_write(resource fd, string data[, int len])
174 + Write data to fd with optional truncation at length */
175 +PHP_FUNCTION(dio_write)
181 + long trunc_len = 0;
184 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &r_fd, &data, &data_len, &trunc_len) == FAILURE) {
188 + if (trunc_len < 0 || trunc_len > data_len) {
189 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "length must be greater or equal to zero and less then the length of the specified string.");
193 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
195 + res = write(f->fd, data, trunc_len ? trunc_len : data_len);
197 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot write data to file descriptor %d, %s", f->fd, strerror(errno));
206 +/* {{{ proto bool dio_truncate(resource fd, int offset)
207 + Truncate file descriptor fd to offset bytes */
208 +PHP_FUNCTION(dio_truncate)
214 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &r_fd, &offset) == FAILURE) {
218 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
220 + if (ftruncate(f->fd, offset) == -1) {
221 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't truncate %d to %ld bytes: %s", f->fd, offset, strerror(errno));
230 +#define ADD_FIELD(f, v) add_assoc_long_ex(return_value, (f), sizeof(f), v);
232 +/* {{{ proto array dio_stat(resource fd)
233 + Get stat information about the file descriptor fd */
234 +PHP_FUNCTION(dio_stat)
240 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &r_fd) == FAILURE) {
244 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
246 + if (fstat(f->fd, &s) == -1) {
247 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot stat %d: %s", f->fd, strerror(errno));
251 + array_init(return_value);
252 + ADD_FIELD("device", s.st_dev);
253 + ADD_FIELD("inode", s.st_ino);
254 + ADD_FIELD("mode", s.st_mode);
255 + ADD_FIELD("nlink", s.st_nlink);
256 + ADD_FIELD("uid", s.st_uid);
257 + ADD_FIELD("gid", s.st_gid);
258 + ADD_FIELD("device_type", s.st_rdev);
259 + ADD_FIELD("size", s.st_size);
261 + ADD_FIELD("block_size", s.st_blksize);
262 + ADD_FIELD("blocks", s.st_blocks);
264 + ADD_FIELD("atime", s.st_atime);
265 + ADD_FIELD("mtime", s.st_mtime);
266 + ADD_FIELD("ctime", s.st_ctime);
270 +/* {{{ proto int dio_seek(resource fd, int pos, int whence)
271 + Seek to pos on fd from whence */
272 +PHP_FUNCTION(dio_seek)
277 + long whence = SEEK_SET;
279 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &r_fd, &offset, &whence) == FAILURE) {
283 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
285 + RETURN_LONG(lseek(f->fd, offset, whence));
291 +/* {{{ proto mixed dio_fcntl(resource fd, int cmd[, mixed arg])
292 + Perform a c library fcntl on fd */
293 +PHP_FUNCTION(dio_fcntl)
300 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|z", &r_fd, &cmd, &arg) == FAILURE) {
304 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
310 + struct flock lk = {0};
314 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be array or int, none given");
317 + if (Z_TYPE_P(arg) == IS_ARRAY) {
319 + if (zend_hash_find(fh, "start", sizeof("start"), (void **) &element) == FAILURE) {
322 + lk.l_start = Z_LVAL_PP(element);
325 + if (zend_hash_find(fh, "length", sizeof("length"), (void **) &element) == FAILURE) {
328 + lk.l_len = Z_LVAL_PP(element);
331 + if (zend_hash_find(fh, "whence", sizeof("whence"), (void **) &element) == FAILURE) {
334 + lk.l_whence = Z_LVAL_PP(element);
337 + if (zend_hash_find(fh, "type", sizeof("type"), (void **) &element) == FAILURE) {
340 + lk.l_type = Z_LVAL_PP(element);
342 + } else if (Z_TYPE_P(arg) == IS_LONG) {
345 + lk.l_whence = SEEK_SET;
346 + lk.l_type = Z_LVAL_P(arg);
348 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be array or int, %s given", zend_zval_type_name(arg));
352 + RETURN_LONG(fcntl(f->fd, cmd, &lk));
356 + struct flock lk = {0};
358 + fcntl(f->fd, cmd, &lk);
360 + array_init(return_value);
361 + add_assoc_long(return_value, "type", lk.l_type);
362 + add_assoc_long(return_value, "whence", lk.l_whence);
363 + add_assoc_long(return_value, "start", lk.l_start);
364 + add_assoc_long(return_value, "length", lk.l_len);
365 + add_assoc_long(return_value, "pid", lk.l_pid);
372 + if (!arg || Z_TYPE_P(arg) != IS_LONG) {
373 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be int");
377 + if (!new_php_fd(&new_f, fcntl(f->fd, cmd, Z_LVAL_P(arg)))) {
380 + ZEND_REGISTER_RESOURCE(return_value, new_f, le_fd);
384 + if (!arg || Z_TYPE_P(arg) != IS_LONG) {
385 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be int");
389 + RETURN_LONG(fcntl(f->fd, cmd, Z_LVAL_P(arg)));
397 +/* {{{ proto mixed dio_tcsetattr(resource fd, array args )
398 + Perform a c library tcsetattr on fd */
399 +PHP_FUNCTION(dio_tcsetattr)
404 + struct termios newtio;
405 + int Baud_Rate, Data_Bits=8, Stop_Bits=1, Parity=0, Flow_Control=1, Is_Canonical=1;
406 + long BAUD,DATABITS,STOPBITS,PARITYON,PARITY;
410 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &r_fd, &arg) == FAILURE) {
414 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
416 + if (Z_TYPE_P(arg) != IS_ARRAY) {
417 + php_error_docref(NULL TSRMLS_CC, E_WARNING,"tcsetattr, third argument should be an associative array");
423 + if (zend_hash_find(fh, "baud", sizeof("baud"), (void **) &element) == FAILURE) {
426 + Baud_Rate = Z_LVAL_PP(element);
429 + if (zend_hash_find(fh, "bits", sizeof("bits"), (void **) &element) == FAILURE) {
432 + Data_Bits = Z_LVAL_PP(element);
435 + if (zend_hash_find(fh, "stop", sizeof("stop"), (void **) &element) == FAILURE) {
438 + Stop_Bits = Z_LVAL_PP(element);
441 + if (zend_hash_find(fh, "parity", sizeof("parity"), (void **) &element) == FAILURE) {
444 + Parity = Z_LVAL_PP(element);
447 + if (zend_hash_find(fh, "flow_control", sizeof("flow_control"), (void **) &element) == FAILURE) {
450 + Flow_Control = Z_LVAL_PP(element);
453 + if (zend_hash_find(fh, "is_canonical", sizeof("is_canonical"), (void **) &element) == FAILURE) {
456 + Is_Canonical = Z_LVAL_PP(element);
459 + /* assign to correct values... */
460 + switch (Baud_Rate) {
507 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid baud rate %d", Baud_Rate);
510 + switch (Data_Bits) {
524 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data bits %d", Data_Bits);
527 + switch (Stop_Bits) {
535 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid stop bits %d", Stop_Bits);
553 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid parity %d", Parity);
557 + memset(&newtio, 0, sizeof(newtio));
558 + tcgetattr(f->fd, &newtio);
560 + if (Is_Canonical) {
561 + newtio.c_iflag = IGNPAR | ICRNL;
562 + newtio.c_oflag = 0;
563 + newtio.c_lflag = ICANON;
565 + cfmakeraw(&newtio);
568 + newtio.c_cflag = BAUD | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
571 + if (Flow_Control) {
572 + newtio.c_cflag |= CRTSCTS;
578 + newtio.c_cc[VMIN] = 1;
579 + newtio.c_cc[VTIME] = 0;
580 + tcflush(f->fd, TCIFLUSH);
581 + tcsetattr(f->fd,TCSANOW,&newtio);
588 +/* {{{ proto void dio_close(resource fd)
589 + Close the file descriptor given by fd */
590 +PHP_FUNCTION(dio_close)
595 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &r_fd) == FAILURE) {
599 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
601 + zend_list_delete(Z_LVAL_P(r_fd));
605 +#define RDIOC(c) REGISTER_LONG_CONSTANT(#c, c, CONST_CS | CONST_PERSISTENT)
607 +/* {{{ dio_init_legacy_defines
608 + * Initialises the legacy PHP defines
610 +static void dio_init_legacy_defines(int module_number TSRMLS_DC) {
661 +ZEND_BEGIN_ARG_INFO_EX(dio_open_args, 0, 0, 2)
662 + ZEND_ARG_INFO(0, filename)
663 + ZEND_ARG_INFO(0, flags)
664 + ZEND_ARG_INFO(0, mode)
667 +ZEND_BEGIN_ARG_INFO_EX(dio_read_args, 0, 0, 1)
668 + ZEND_ARG_INFO(0, fd)
669 + ZEND_ARG_INFO(0, n)
672 +ZEND_BEGIN_ARG_INFO_EX(dio_write_args, 0, 0, 2)
673 + ZEND_ARG_INFO(0, fd)
674 + ZEND_ARG_INFO(0, data)
675 + ZEND_ARG_INFO(0, len)
678 +ZEND_BEGIN_ARG_INFO_EX(dio_stat_args, 0, 0, 1)
679 + ZEND_ARG_INFO(0, fd)
682 +ZEND_BEGIN_ARG_INFO_EX(dio_truncate_args, 0, 0, 2)
683 + ZEND_ARG_INFO(0, fd)
684 + ZEND_ARG_INFO(0, offset)
687 +ZEND_BEGIN_ARG_INFO_EX(dio_seek_args, 0, 0, 3)
688 + ZEND_ARG_INFO(0, fd)
689 + ZEND_ARG_INFO(0, pos)
690 + ZEND_ARG_INFO(0, whence)
693 +ZEND_BEGIN_ARG_INFO_EX(dio_fcntl_args, 0, 0, 2)
694 + ZEND_ARG_INFO(0, fd)
695 + ZEND_ARG_INFO(0, cmd)
696 + ZEND_ARG_INFO(0, arg)
699 +ZEND_BEGIN_ARG_INFO_EX(dio_tcsetattr_args, 0, 0, 2)
700 + ZEND_ARG_INFO(0, fd)
701 + ZEND_ARG_INFO(0, args)
704 +ZEND_BEGIN_ARG_INFO_EX(dio_close_args, 0, 0, 1)
705 + ZEND_ARG_INFO(0, fd)
709 + +----------------------------------------------------------------------+
710 + | END OF DEPRECATED FUNCTIONALITY |
711 + +----------------------------------------------------------------------+
714 +ZEND_BEGIN_ARG_INFO_EX(dio_raw_args, 0, 0, 2)
715 + ZEND_ARG_INFO(0, filename)
716 + ZEND_ARG_INFO(0, mode)
717 + ZEND_ARG_INFO(0, options)
720 +ZEND_BEGIN_ARG_INFO_EX(dio_serial_args, 0, 0, 2)
721 + ZEND_ARG_INFO(0, filename)
722 + ZEND_ARG_INFO(0, mode)
723 + ZEND_ARG_INFO(0, options)
726 +static zend_object_handlers dio_raw_object_handlers;
728 +static function_entry dio_functions[] = {
729 + /* Class functions. */
731 + /* Legacy functions (Deprecated - See dio_legacy.c) */
732 + PHP_FE(dio_open, dio_open_args)
734 + PHP_FE(dio_truncate, dio_truncate_args)
736 + PHP_FE(dio_stat, dio_stat_args)
737 + PHP_FE(dio_seek, dio_seek_args)
739 + PHP_FE(dio_fcntl, dio_fcntl_args)
741 + PHP_FE(dio_read, dio_read_args)
742 + PHP_FE(dio_write, dio_write_args)
743 + PHP_FE(dio_close, dio_close_args)
745 + PHP_FE(dio_tcsetattr, dio_tcsetattr_args)
748 + /* Stream functions */
749 + PHP_FE(dio_raw, dio_raw_args)
750 + PHP_FE(dio_serial, dio_serial_args)
752 + /* End of functions */
756 +zend_module_entry dio_module_entry = {
757 + STANDARD_MODULE_HEADER,
766 + STANDARD_MODULE_PROPERTIES
769 +#ifdef COMPILE_DL_DIO
770 +ZEND_GET_MODULE(dio)
773 +#define DIO_UNDEF_CONST -1
775 +/* {{{ PHP_MINIT_FUNCTION
777 +PHP_MINIT_FUNCTION(dio)
779 + /* Legacy resource destructor. */
780 + le_fd = zend_register_list_destructors_ex(_dio_close_fd, NULL, le_fd_name, module_number);
782 + dio_init_legacy_defines(module_number TSRMLS_CC);
784 + /* Register the stream wrappers */
785 + return (php_register_url_stream_wrapper(DIO_RAW_STREAM_NAME, &php_dio_raw_stream_wrapper TSRMLS_CC) == SUCCESS &&
786 + php_register_url_stream_wrapper(DIO_SERIAL_STREAM_NAME, &php_dio_serial_stream_wrapper TSRMLS_CC) == SUCCESS) ? SUCCESS : FAILURE;
790 +/* {{{ PHP_MSHUTDOWN_FUNCTION
792 +PHP_MSHUTDOWN_FUNCTION(dio)
794 + return (php_unregister_url_stream_wrapper(DIO_RAW_STREAM_NAME TSRMLS_CC) == SUCCESS &&
795 + php_unregister_url_stream_wrapper(DIO_SERIAL_STREAM_NAME TSRMLS_CC) == SUCCESS) ? SUCCESS : FAILURE;
799 +/* {{{ PHP_MINFO_FUNCTION
801 +PHP_MINFO_FUNCTION(dio)
803 + php_info_print_table_start();
804 + php_info_print_table_header(2, "dio support", "enabled");
805 + php_info_print_table_row(2, "version", PHP_DIO_VERSION);
806 + php_info_print_table_end();
812 + * c-basic-offset: 4
815 + * vim600: fdm=marker
816 + * vim: sw=4 ts=4 noet
819 +++ b/ext/dio/dio_common.c
822 + +----------------------------------------------------------------------+
824 + +----------------------------------------------------------------------+
825 + | Copyright (c) 2009 Melanie Rhianna Lewis |
826 + +----------------------------------------------------------------------+
827 + | This source file is subject to version 3.0 of the PHP license, |
828 + | that is bundled with this package in the file LICENSE, and is |
829 + | available through the world-wide-web at the following url: |
830 + | http://www.php.net/license/3_0.txt. |
831 + | If you did not receive a copy of the PHP license and are unable to |
832 + | obtain it through the world-wide-web, please send a note to |
833 + | license@php.net so we can mail you a copy immediately. |
834 + +----------------------------------------------------------------------+
835 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
836 + +----------------------------------------------------------------------+
839 +#ifdef HAVE_CONFIG_H
845 +#include "php_dio.h"
846 +#include "php_dio_common.h"
848 +/* {{{ dio_init_stream_data
849 + * Initialises the command parts of the stream data.
851 +void dio_init_stream_data(php_dio_stream_data *data) {
852 + data->stream_type = DIO_STREAM_TYPE_NONE;
853 + data->end_of_file = 0;
854 +#ifdef DIO_HAS_FILEPERMS
855 + data->has_perms = 0;
859 + data->is_blocking = 1;
860 + data->has_timeout = 0;
861 + data->timeout_sec = 0;
862 + data->timeout_usec = 0;
863 + data->timed_out = 0;
865 + /* Serial options */
866 + data->data_rate = 9600;
867 + data->data_bits = 8;
868 + data->stop_bits = 1;
870 + data->flow_control = 1;
871 + data->canonical = 1;
875 +/* {{{ dio_convert_to_long
876 + * Returns as a long, the value of the zval regardless of its type.
878 +long dio_convert_to_long(zval *val) {
882 + ALLOC_INIT_ZVAL(copyval);
884 + convert_to_long(copyval);
885 + longval = Z_LVAL_P(copyval);
886 + zval_ptr_dtor(©val);
892 +/* {{{ dio_assoc_array_get_basic_options
893 + * Retrieves the basic open option values from an associative array
895 +void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data TSRMLS_DC) {
896 +#if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
898 + HashTable *opthash;
900 + opthash = HASH_OF(options);
903 +#ifdef DIO_HAS_FILEPERMS
904 + /* This is the file mode flags used by open(). */
905 + if (zend_hash_find(opthash, "perms", sizeof("perms"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
906 + data->perms = (int)dio_convert_to_long(*tmpzval);
907 + data->has_perms = 1;
912 + /* This sets the underlying stream to be blocking/non
913 + block (i.e. O_NONBLOCK) */
914 + if (zend_hash_find(opthash, "is_blocking", sizeof("is_blocking"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
915 + data->is_blocking = dio_convert_to_long(*tmpzval) ? 1 : 0;
918 + /* This is the timeout value for reads in seconds. Only one of
919 + timeout_secs or timeout_usecs need be defined to define a timeout. */
920 + if (zend_hash_find(opthash, "timeout_secs", sizeof("timeout_secs"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
921 + data->timeout_sec = dio_convert_to_long(*tmpzval);
924 + /* This is the timeout value for reads in microseconds. Only one of
925 + timeout_secs or timeout_usecs need be defined to define a timeout. */
926 + if (zend_hash_find(opthash, "timeout_usecs", sizeof("timeout_usecs"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
927 + data->timeout_usec = dio_convert_to_long(*tmpzval);
930 + data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
935 +/* {{{ dio_assoc_array_get_serial_options
936 + * Retrieves the serial open option values from an associative array
938 +void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data TSRMLS_DC) {
940 + HashTable *opthash;
942 + opthash = HASH_OF(options);
944 + if (zend_hash_find(opthash, "data_rate", sizeof("data_rate"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
945 + data->data_rate = dio_convert_to_long(*tmpzval);
948 + if (zend_hash_find(opthash, "data_bits", sizeof("data_bits"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
949 + data->data_bits = (int)dio_convert_to_long(*tmpzval);
952 + if (zend_hash_find(opthash, "stop_bits", sizeof("stop_bits"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
953 + data->stop_bits = (int)dio_convert_to_long(*tmpzval);
956 + if (zend_hash_find(opthash, "parity", sizeof("parity"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
957 + data->parity = (int)dio_convert_to_long(*tmpzval);
960 + if (zend_hash_find(opthash, "flow_control", sizeof("flow_control"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
961 + data->flow_control = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
964 + if (zend_hash_find(opthash, "is_canonical", sizeof("is_canonical"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
965 + data->canonical = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
970 +/* {{{ dio_stream_context_get_raw_options
971 + * Extracts the option values for dio.raw mode from a context
973 +void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC) {
974 +#if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
978 +#ifdef DIO_HAS_FILEPERMS
979 + /* This is the file mode flags used by open(). */
980 + if (php_stream_context_get_option(context, "dio", "perms", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
981 + data->perms = (int)dio_convert_to_long(*tmpzval);
982 + data->has_perms = 1;
987 + /* This sets the underlying stream to be blocking/non
988 + block (i.e. O_NONBLOCK) */
989 + if (php_stream_context_get_option(context, "dio", "is_blocking", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
990 + data->is_blocking = dio_convert_to_long(*tmpzval) ? 1 : 0;
993 + /* This is the timeout value for reads in seconds. Only one of
994 + timeout_secs or timeout_usecs need be defined to define a timeout. */
995 + if (php_stream_context_get_option(context, "dio", "timeout_secs", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
996 + data->timeout_sec = dio_convert_to_long(*tmpzval);
999 + /* This is the timeout value for reads in microseconds. Only one of
1000 + timeout_secs or timeout_usecs need be defined to define a timeout. */
1001 + if (php_stream_context_get_option(context, "dio", "timeout_usecs", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1002 + data->timeout_usec = dio_convert_to_long(*tmpzval);
1005 + data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
1010 +/* {{{ dio_stream_context_get_serial_options
1011 + * Extracts the option values for dio.serial mode from a context
1013 +void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC) {
1016 + if (php_stream_context_get_option(context, "dio", "data_rate", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1017 + data->data_rate = dio_convert_to_long(*tmpzval);
1020 + if (php_stream_context_get_option(context, "dio", "data_bits", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1021 + data->data_bits = (int)dio_convert_to_long(*tmpzval);
1024 + if (php_stream_context_get_option(context, "dio", "stop_bits", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1025 + data->stop_bits = (int)dio_convert_to_long(*tmpzval);
1028 + if (php_stream_context_get_option(context, "dio", "parity", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1029 + data->parity = (int)dio_convert_to_long(*tmpzval);
1032 + if (php_stream_context_get_option(context, "dio", "flow_control", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1033 + data->flow_control = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
1036 + if (php_stream_context_get_option(context, "dio", "is_canonical", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1037 + data->canonical = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
1043 + * Local variables:
1044 + * c-basic-offset: 4
1047 + * vim600: fdm=marker
1048 + * vim: sw=4 ts=4 noet
1052 +++ b/ext/dio/dio_posix.c
1055 + +----------------------------------------------------------------------+
1057 + +----------------------------------------------------------------------+
1058 + | Copyright (c) 2009 Melanie Rhianna Lewis |
1059 + +----------------------------------------------------------------------+
1060 + | This source file is subject to version 3.0 of the PHP license, |
1061 + | that is bundled with this package in the file LICENSE, and is |
1062 + | available through the world-wide-web at the following url: |
1063 + | http://www.php.net/license/3_0.txt. |
1064 + | If you did not receive a copy of the PHP license and are unable to |
1065 + | obtain it through the world-wide-web, please send a note to |
1066 + | license@php.net so we can mail you a copy immediately. |
1067 + +----------------------------------------------------------------------+
1068 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
1069 + +----------------------------------------------------------------------+
1072 +#ifdef HAVE_CONFIG_H
1073 +#include "config.h"
1078 +#include "php_dio_common.h"
1080 +/* {{{ dio_stream_mode_to_flags
1081 + * Convert an fopen() mode string to open() flags
1083 +static int dio_stream_mode_to_flags(const char *mode) {
1084 + int flags = 0, ch = 0, bin = 1;
1086 + switch(mode[ch++]) {
1091 + flags = O_TRUNC | O_CREAT;
1094 + flags = O_APPEND | O_CREAT;
1097 + flags = O_EXCL | O_CREAT;
1101 + if (mode[ch] != '+') {
1102 + bin = (mode[ch++] == 'b');
1105 + if (mode[ch] == '+') {
1107 + } else if (flags) {
1108 + flags |= O_WRONLY;
1110 + flags |= O_RDONLY;
1113 +#if defined(_O_TEXT) && defined(O_BINARY)
1115 + flags |= O_BINARY;
1125 +/* {{{ dio_data_rate_to_define
1126 + * Converts a numeric data rate to a termios define
1128 +static int dio_data_rate_to_define(long rate, speed_t *def) {
1208 +/* {{{ dio_data_bits_to_define
1209 + * Converts a number of data bits to a termios define
1211 +static int dio_data_bits_to_define(int data_bits, int *def) {
1214 + switch (data_bits) {
1236 +/* {{{ dio_stop_bits_to_define
1237 + * Converts a number of stop bits to a termios define
1239 +static int dio_stop_bits_to_define(int stop_bits, int *def) {
1242 + switch (stop_bits) {
1258 +/* {{{ dio_parity_to_define
1259 + * Converts a parity type to a termios define
1261 +static int dio_parity_to_define(int parity, int *def) {
1269 + val = PARENB | PARODD;
1283 +/* {{{ dio_create_stream_data
1284 + * Creates an initialised stream data structure. Free with efree().
1286 +php_dio_stream_data * dio_create_stream_data(void) {
1287 + php_dio_posix_stream_data * data = emalloc(sizeof(php_dio_posix_stream_data));
1288 + dio_init_stream_data(&(data->common));
1292 + return (php_dio_stream_data *)data;
1296 +/* {{{ dio_common_write
1297 + * Writes count chars from the buffer to the stream described by the stream data.
1299 +size_t dio_common_write(php_dio_stream_data *data, const char *buf, size_t count) {
1302 + /* Blocking writes can be interrupted by signals etc. If
1303 + * interrupted try again. Not sure about non-blocking
1304 + * writes but it doesn't hurt to check. */
1306 + ret = write(((php_dio_posix_stream_data*)data)->fd, buf, count);
1310 + } while (errno == EINTR);
1315 +#ifdef DIO_NONBLOCK
1316 +/* {{{ dio_timeval_subtract
1317 + * Calculates the difference between two timevals returning the result in the
1318 + * structure pointed to by diffptr. Returns -1 as error if late time is
1319 + * earlier than early time.
1321 +static int dio_timeval_subtract(struct timeval *late, struct timeval *early, struct timeval *diff) {
1322 + struct timeval *tmp;
1324 + /* Handle negatives */
1325 + if (late->tv_sec < early->tv_sec) {
1329 + if ((late->tv_sec == early->tv_sec) && (late->tv_usec < early->tv_usec)) {
1333 + /* Handle any carry. If later usec is smaller than earlier usec simple
1334 + * subtraction will result in negative value. Since usec has a maximum
1335 + * of one second by adding another second before the subtraction the
1336 + * result will always be positive. */
1337 + if (late->tv_usec < early->tv_usec) {
1338 + late->tv_usec += 1000000;
1342 + /* Once adjusted can just subtract values. */
1343 + diff->tv_sec = late->tv_sec - early->tv_sec;
1344 + diff->tv_usec = late->tv_usec - early->tv_usec;
1350 +/* {{{ dio_common_read
1351 + * Reads count chars to the buffer to the stream described by the stream data.
1353 +size_t dio_common_read(php_dio_stream_data *data, const char *buf, size_t count) {
1354 + int fd = ((php_dio_posix_stream_data*)data)->fd;
1355 + size_t ret, total = 0;
1356 + char *ptr = (char*)buf;
1358 + struct timeval timeout, timeouttmp, before, after, diff;
1361 + if (!data->has_timeout) {
1362 + /* Blocking reads can be interrupted by signals etc. If
1363 + * interrupted try again. Not sure about non-blocking
1364 + * reads but it doesn't hurt to check. */
1366 + ret = read(fd, (char*)ptr, count);
1369 + } else if (!ret) {
1370 + data->end_of_file = 1;
1372 + } while ((errno == EINTR) && !data->end_of_file);
1375 +#ifdef DIO_NONBLOCK
1377 + /* Clear timed out flag */
1378 + data->timed_out = 0;
1380 + /* The initial timeout value */
1381 + timeout.tv_sec = data->timeout_sec;
1382 + timeout.tv_usec = data->timeout_usec;
1385 + /* The semantics of select() are that you cannot guarantee
1386 + * that the timeval structure passed in has not been changed by
1387 + * the select call. So you keep a copy. */
1388 + timeouttmp = timeout;
1390 + /* The time before we wait for data. */
1391 + (void) gettimeofday(&before, NULL);
1393 + /* Wait for an event on our file descriptor. */
1395 + FD_SET(fd, &rfds);
1397 + ret = select(fd + 1, &rfds, NULL, NULL, &timeouttmp);
1399 + if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN)) {
1403 + /* We have data to read. */
1404 + if ((ret > 0) && FD_ISSET(fd, &rfds)) {
1405 + ret = read(fd, ptr, count);
1406 + /* Another error */
1407 + if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN)) {
1412 + /* Got data, add it to the buffer. */
1416 + } else if (!ret) {
1417 + /* This should never happen since how can we have
1418 + * data to read at an end of file, but still
1419 + * just in case! */
1420 + data->end_of_file = 1;
1425 + /* If not timed out and not end of file and not all data read
1426 + * calculate how long it took us and loop if we still have time
1427 + * out time left. */
1429 + (void) gettimeofday(&after, NULL);
1431 + /* Diff the timevals */
1432 + (void) dio_timeval_subtract(&after, &before, &diff);
1434 + /* Now adjust the timeout. */
1435 + if (!dio_timeval_subtract(&timeout, &diff, &timeout)) {
1436 + /* If it errors we've run out of time. */
1437 + data->timed_out = 1;
1439 + } else if (!timeout.tv_sec && !(timeout.tv_usec / 1000)) {
1440 + /* Check for rounding issues (millisecond accuracy) */
1441 + data->timed_out = 1;
1445 + } while (count); /* Until time out or end of file or all data read. */
1453 +/* {{{ php_dio_stream_data
1454 + * Closes the php_stream.
1456 +int dio_common_close(php_dio_stream_data *data) {
1457 + if (close(((php_dio_posix_stream_data*)data)->fd) < 0) {
1465 +/* {{{ dio_common_set_option
1466 + * Sets/gets stream options
1468 +int dio_common_set_option(php_dio_stream_data *data, int option, int value, void *ptrparam) {
1469 + int fd = ((php_dio_posix_stream_data*)data)->fd;
1470 + int old_is_blocking;
1474 +#ifdef DIO_NONBLOCK
1475 + case PHP_STREAM_OPTION_READ_TIMEOUT:
1477 + struct timeval *tv = (struct timeval*)ptrparam;
1479 + flags = fcntl(fd, F_GETFL, 0);
1481 + /* A timeout of zero seconds and zero microseconds disables
1482 + any existing timeout. */
1483 + if (tv->tv_sec || tv->tv_usec) {
1484 + data->timeout_sec = tv->tv_sec;
1485 + data->timeout_usec = tv->tv_usec;
1486 + data->has_timeout = -1;
1487 + (void) fcntl(fd, F_SETFL, flags & ~DIO_NONBLOCK);
1489 + data->timeout_sec = 0;
1490 + data->timeout_usec = 0;
1491 + data->has_timeout = 0;
1492 + data->timed_out = 0;
1493 + (void) fcntl(fd, F_SETFL, flags | DIO_NONBLOCK);
1496 + return PHP_STREAM_OPTION_RETURN_OK;
1498 + return PHP_STREAM_OPTION_RETURN_ERR;
1501 + case PHP_STREAM_OPTION_BLOCKING:
1502 + flags = fcntl(fd, F_GETFL, 0);
1504 + flags &= ~DIO_NONBLOCK;
1506 + flags |= DIO_NONBLOCK;
1508 + (void) fcntl(fd, F_SETFL, flags);
1510 + old_is_blocking = data->is_blocking;
1511 + data->is_blocking = value;
1512 + return old_is_blocking ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
1513 +#endif /* O_NONBLOCK */
1523 +/* {{{ dio_raw_open_stream
1524 + * Opens the underlying stream.
1526 +int dio_raw_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
1527 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1528 + pdata->flags = dio_stream_mode_to_flags(mode);
1530 +#ifdef DIO_NONBLOCK
1531 + if (!data->is_blocking || data->has_timeout) {
1532 + pdata->flags |= DIO_NONBLOCK;
1536 + /* Open the file and handle any errors. */
1537 +#ifdef DIO_HAS_FILEPERMS
1538 + if (data->has_perms) {
1539 + pdata->fd = open(filename, pdata->flags, (mode_t)data->perms);
1541 + pdata->fd = open(filename, pdata->flags);
1544 + pdata->fd = open(filename, pdata->flags);
1547 + if (pdata->fd < 0) {
1550 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "File exists!");
1561 +/* {{{ dio_serial_init
1562 + * Initialises the serial settings storing the original settings before hand.
1564 +static int dio_serial_init(php_dio_stream_data *data TSRMLS_DC) {
1565 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1566 + int ret = 0, data_bits_def, stop_bits_def, parity_def;
1567 + struct termios tio;
1570 + if (!dio_data_rate_to_define(data->data_rate, &rate_def)) {
1571 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_rate value (%ld)", data->data_rate);
1575 + if (!dio_data_bits_to_define(data->data_bits, &data_bits_def)) {
1576 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_bits value (%d)", data->data_bits);
1580 + if (!dio_stop_bits_to_define(data->stop_bits, &stop_bits_def)) {
1581 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid stop_bits value (%d)", data->stop_bits);
1585 + if (!dio_parity_to_define(data->parity, &parity_def)) {
1586 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid parity value (%d)", data->parity);
1590 + ret = tcgetattr(pdata->fd, &(pdata->oldtio));
1592 + if ((errno == ENOTTY) || (errno == ENODEV)) {
1593 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a serial port or terminal!");
1598 + ret = tcgetattr(pdata->fd, &tio);
1603 + if (data->canonical) {
1604 + tio.c_iflag = IGNPAR | ICRNL;
1606 + tio.c_lflag = ICANON;
1611 + cfsetispeed(&tio, rate_def);
1612 + cfsetospeed(&tio, rate_def);
1614 + tio.c_cflag &= ~CSIZE;
1615 + tio.c_cflag |= data_bits_def;
1616 + tio.c_cflag &= ~CSTOPB;
1617 + tio.c_cflag |= stop_bits_def;
1618 + tio.c_cflag &= ~(PARENB|PARODD);
1619 + tio.c_cflag |= parity_def;
1622 + tio.c_cflag &= ~(CLOCAL | CRTSCTS);
1624 + tio.c_cflag &= ~CLOCAL;
1626 + if (!data->flow_control) {
1627 + tio.c_cflag |= CLOCAL;
1630 + tio.c_cflag |= CRTSCTS;
1634 + ret = tcsetattr(pdata->fd, TCSANOW, &tio);
1643 +/* {{{ dio_serial_uninit
1644 + * Restores the serial settings back to their original state.
1646 +int dio_serial_uninit(php_dio_stream_data *data) {
1647 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1651 + ret = tcsetattr(pdata->fd, TCSANOW, &(pdata->oldtio));
1652 + } while ((ret < 0) && (errno == EINTR));
1658 +/* {{{ dio_serial_flush
1659 + * Purges the serial buffers of data.
1661 +int dio_serial_purge(php_dio_stream_data *data) {
1662 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1665 + if ((pdata->flags & O_RDWR) == O_RDWR) {
1666 + ret = tcflush(pdata->fd, TCIOFLUSH);
1667 + } else if ((pdata->flags & O_WRONLY) == O_WRONLY) {
1668 + ret = tcflush(pdata->fd, TCOFLUSH);
1669 + } else if ((pdata->flags & O_RDONLY) == O_RDONLY) {
1670 + ret = tcflush(pdata->fd, TCIFLUSH);
1681 +/* {{{ dio_serial_open_stream
1682 + * Opens the underlying stream.
1684 +int dio_serial_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
1685 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1688 + /* We don't want a controlling TTY */
1689 + pdata->flags |= O_NOCTTY;
1692 + if (!dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
1696 + if (!dio_serial_init(data TSRMLS_CC)) {
1706 + * Local variables:
1707 + * c-basic-offset: 4
1710 + * vim600: fdm=marker
1711 + * vim: sw=4 ts=4 noet
1714 +++ b/ext/dio/dio_stream_wrappers.c
1717 + +----------------------------------------------------------------------+
1719 + +----------------------------------------------------------------------+
1720 + | Copyright (c) 2009 Melanie Rhianna Lewis |
1721 + +----------------------------------------------------------------------+
1722 + | This source file is subject to version 3.0 of the PHP license, |
1723 + | that is bundled with this package in the file LICENSE, and is |
1724 + | available through the world-wide-web at the following url: |
1725 + | http://www.php.net/license/3_0.txt. |
1726 + | If you did not receive a copy of the PHP license and are unable to |
1727 + | obtain it through the world-wide-web, please send a note to |
1728 + | license@php.net so we can mail you a copy immediately. |
1729 + +----------------------------------------------------------------------+
1730 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
1731 + +----------------------------------------------------------------------+
1734 +#ifdef HAVE_CONFIG_H
1735 +#include "config.h"
1739 +#include "ext/standard/url.h"
1741 +#include "php_dio.h"
1742 +#include "php_dio_common.h"
1743 +#include "php_dio_stream_wrappers.h"
1746 + +----------------------------------------------------------------------+
1747 + | Raw stream handling |
1748 + +----------------------------------------------------------------------+
1751 +/* {{{ dio_stream_write
1752 + * Write to the stream
1754 +static size_t dio_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
1756 + return dio_common_write((php_dio_stream_data*)stream->abstract, buf, count);
1760 +/* {{{ dio_stream_read
1761 + * Read from the stream
1763 +static size_t dio_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
1765 + php_dio_stream_data* data = (php_dio_stream_data*)stream->abstract;
1766 + size_t bytes = dio_common_read(data, buf, count);
1767 + stream->eof = data->end_of_file;
1773 +/* {{{ dio_stream_flush
1774 + * Flush the stream. For raw streams this does nothing.
1776 +static int dio_stream_flush(php_stream *stream TSRMLS_DC)
1782 +/* {{{ dio_stream_close
1783 + * Close the stream
1785 +static int dio_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
1787 + php_dio_stream_data *abstract = (php_dio_stream_data*)stream->abstract;
1789 + if (!dio_common_close(abstract)) {
1798 +/* {{{ dio_stream_set_option
1799 + * Set the stream options.
1801 +static int dio_stream_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC)
1803 + php_dio_stream_data *abstract = (php_dio_stream_data*)stream->abstract;
1806 + case PHP_STREAM_OPTION_META_DATA_API:
1807 +#ifdef DIO_NONBLOCK
1808 + add_assoc_bool((zval *)ptrparam, "timed_out", abstract->timed_out);
1809 + add_assoc_bool((zval *)ptrparam, "blocked", abstract->is_blocking);
1811 + add_assoc_bool((zval *)ptrparam, "eof", stream->eof);
1812 + return PHP_STREAM_OPTION_RETURN_OK;
1814 +#if PHP_MAJOR_VERSION >= 5
1815 + case PHP_STREAM_OPTION_CHECK_LIVENESS:
1816 + stream->eof = abstract->end_of_file;
1817 + return PHP_STREAM_OPTION_RETURN_OK;
1818 +#endif /* PHP_MAJOR_VERSION >= 5 */
1824 + return dio_common_set_option(abstract, option, value, ptrparam);
1828 +php_stream_ops dio_raw_stream_ops = {
1837 + dio_stream_set_option,
1840 +/* {{{ dio_raw_fopen_wrapper
1841 + * fopen for the dio.raw stream.
1843 +static php_stream *dio_raw_fopen_wrapper(php_stream_wrapper *wrapper,
1844 + char *path, char *mode,
1845 + int options, char **opened_path,
1846 + php_stream_context *context STREAMS_DC TSRMLS_DC) {
1847 + php_dio_stream_data *data;
1848 + php_stream *stream;
1851 + /* Check it was actually for us (not a corrupted function pointer
1853 + if (strncmp(path, DIO_RAW_STREAM_PROTOCOL, sizeof(DIO_RAW_STREAM_PROTOCOL) - 1)) {
1857 + /* Get the actually file system name/path. */
1858 + filename = path + sizeof(DIO_RAW_STREAM_PROTOCOL) - 1;
1860 + /* Check we can actually access it. */
1861 + if (php_check_open_basedir(filename TSRMLS_CC) ||
1862 + (PG(safe_mode) && !php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
1866 + data = dio_create_stream_data();
1867 + data->stream_type = DIO_STREAM_TYPE_RAW;
1869 + /* Parse the context. */
1871 + dio_stream_context_get_basic_options(context, data TSRMLS_CC);
1874 + /* Try and open a raw stream. */
1875 + if (!dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
1879 + /* Create a PHP stream based on raw stream */
1880 + stream = php_stream_alloc(&dio_raw_stream_ops, data, 0, mode);
1882 + (void) dio_common_close(data);
1890 +static php_stream_wrapper_ops dio_raw_stream_wops = {
1891 + dio_raw_fopen_wrapper,
1892 + NULL, /* stream_close */
1894 + NULL, /* stat_url */
1895 + NULL, /* opendir */
1896 + DIO_RAW_STREAM_NAME
1899 +php_stream_wrapper php_dio_raw_stream_wrapper = {
1900 + &dio_raw_stream_wops,
1905 +/* {{{ proto dio_raw(string filename, string mode[, array options])
1906 + * Opens a raw direct IO stream.
1908 +PHP_FUNCTION(dio_raw) {
1909 + zval *options = NULL;
1910 + php_dio_stream_data *data;
1911 + php_stream *stream;
1918 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|z", &filename, &filename_len, &mode, &mode_len, &options) == FAILURE) {
1922 + /* Check the third argument is an array. */
1923 + if (options && (Z_TYPE_P(options) != IS_ARRAY)) {
1927 + /* Check we can actually access the file. */
1928 + if (php_check_open_basedir(filename TSRMLS_CC) ||
1929 + (PG(safe_mode) && !php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
1933 + data = dio_create_stream_data();
1934 + data->stream_type = DIO_STREAM_TYPE_RAW;
1937 + dio_assoc_array_get_basic_options(options, data TSRMLS_CC);
1940 + /* Try and open a raw stream. */
1941 + if (dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
1942 + stream = php_stream_alloc(&dio_raw_stream_ops, data, 0, mode);
1944 + (void) dio_common_close(data);
1950 + php_stream_to_zval(stream, return_value);
1955 + +----------------------------------------------------------------------+
1956 + | Serial stream handling |
1957 + +----------------------------------------------------------------------+
1960 +/* {{{ dio_stream_flush
1961 + * Flush the stream. If the stream is read only, it flushes the read
1962 + * stream, if it is write only it flushes the write, otherwise it flushes
1965 +static int dio_serial_stream_flush(php_stream *stream TSRMLS_DC)
1967 + return dio_serial_purge((php_dio_stream_data*)stream->abstract);
1971 +/* {{{ dio_stream_close
1972 + * Close the stream. Restores the serial settings to their value before
1973 + * the stream was open.
1975 +static int dio_serial_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
1977 + php_dio_stream_data *abstract = (php_dio_stream_data*)stream->abstract;
1979 + if (!dio_serial_uninit(abstract)) {
1983 + if (!dio_common_close(abstract)) {
1992 +php_stream_ops dio_serial_stream_ops = {
1995 + dio_serial_stream_close,
1996 + dio_serial_stream_flush,
2001 + dio_stream_set_option,
2004 +/* {{{ dio_raw_fopen_wrapper
2005 + * fopen for the dio.raw stream.
2007 +static php_stream *dio_serial_fopen_wrapper(php_stream_wrapper *wrapper,
2008 + char *path, char *mode,
2009 + int options, char **opened_path,
2010 + php_stream_context *context STREAMS_DC TSRMLS_DC) {
2011 + php_dio_stream_data *data;
2012 + php_stream *stream;
2015 + /* Check it was actually for us (not a corrupted function pointer
2017 + if (strncmp(path, DIO_SERIAL_STREAM_PROTOCOL, sizeof(DIO_SERIAL_STREAM_PROTOCOL) - 1)) {
2021 + /* Get the actually file system name/path. */
2022 + filename = path + sizeof(DIO_SERIAL_STREAM_PROTOCOL) - 1;
2024 + /* Check we can actually access it. */
2025 + if (php_check_open_basedir(filename TSRMLS_CC) ||
2026 + (PG(safe_mode) && !php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
2030 + data = dio_create_stream_data();
2031 + data->stream_type = DIO_STREAM_TYPE_SERIAL;
2033 + /* Parse the context. */
2035 + dio_stream_context_get_basic_options(context, data TSRMLS_CC);
2036 + dio_stream_context_get_serial_options(context, data TSRMLS_CC);
2039 + /* Try and open a serial stream. */
2040 + if (!dio_serial_open_stream(filename, mode, data TSRMLS_CC)) {
2044 + stream = php_stream_alloc(&dio_serial_stream_ops, data, 0, mode);
2053 +static php_stream_wrapper_ops dio_serial_stream_wops = {
2054 + dio_serial_fopen_wrapper,
2055 + NULL, /* stream_close */
2057 + NULL, /* stat_url */
2058 + NULL, /* opendir */
2059 + DIO_SERIAL_STREAM_NAME
2062 +php_stream_wrapper php_dio_serial_stream_wrapper = {
2063 + &dio_serial_stream_wops,
2068 +/* {{{ proto dio_serial(string filename, string mode[, array options])
2069 + * Opens a serial direct IO stream.
2071 +PHP_FUNCTION(dio_serial) {
2072 + zval *options = NULL;
2073 + php_dio_stream_data *data;
2074 + php_stream *stream;
2081 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|z", &filename, &filename_len, &mode, &mode_len, &options) == FAILURE) {
2085 + /* Check the third argument is an array. */
2086 + if (options && (Z_TYPE_P(options) != IS_ARRAY)) {
2087 + php_error_docref(NULL TSRMLS_CC, E_WARNING,"dio_serial, the third argument should be an array of options");
2091 + /* Check we can actually access the file. */
2092 + if (php_check_open_basedir(filename TSRMLS_CC) ||
2093 + (PG(safe_mode) && !php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
2097 + data = dio_create_stream_data();
2098 + data->stream_type = DIO_STREAM_TYPE_SERIAL;
2101 + dio_assoc_array_get_basic_options(options, data TSRMLS_CC);
2102 + dio_assoc_array_get_serial_options(options, data TSRMLS_CC);
2105 + /* Try and open a serial stream. */
2106 + if (dio_serial_open_stream(filename, mode, data TSRMLS_CC)) {
2107 + stream = php_stream_alloc(&dio_serial_stream_ops, data, 0, mode);
2114 + php_stream_to_zval(stream, return_value);
2119 + * Local variables:
2120 + * c-basic-offset: 4
2123 + * vim600: fdm=marker
2124 + * vim: sw=4 ts=4 noet
2127 +++ b/ext/dio/dio_win32.c
2130 + +----------------------------------------------------------------------+
2132 + +----------------------------------------------------------------------+
2133 + | Copyright (c) 2009 Melanie Rhianna Lewis |
2134 + +----------------------------------------------------------------------+
2135 + | This source file is subject to version 3.0 of the PHP license, |
2136 + | that is bundled with this package in the file LICENSE, and is |
2137 + | available through the world-wide-web at the following url: |
2138 + | http://www.php.net/license/3_0.txt. |
2139 + | If you did not receive a copy of the PHP license and are unable to |
2140 + | obtain it through the world-wide-web, please send a note to |
2141 + | license@php.net so we can mail you a copy immediately. |
2142 + +----------------------------------------------------------------------+
2143 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
2144 + +----------------------------------------------------------------------+
2147 +#ifdef HAVE_CONFIG_H
2148 +#include "config.h"
2153 +#include "php_dio_common.h"
2155 +/* {{{ dio_data_rate_to_define
2156 + * Converts a numeric data rate to a termios define
2158 +static int dio_data_rate_to_define(long rate, DWORD *def) {
2184 + *def = (DWORD)rate;
2190 +/* {{{ dio_data_bits_to_define
2191 + * Converts a number of data bits to a termios define
2193 +static int dio_data_bits_to_define(int data_bits, DWORD *def) {
2194 + switch (data_bits) {
2205 + *def = (DWORD)data_bits;
2210 +/* {{{ dio_stop_bits_to_define
2211 + * Converts a number of stop bits to a termios define
2213 +static int dio_stop_bits_to_define(int stop_bits, DWORD *def) {
2216 + switch (stop_bits) {
2235 +/* {{{ dio_parity_to_define
2236 + * Converts a parity type to a termios define
2238 +static int dio_parity_to_define(int parity, DWORD *def) {
2248 + *def = (DWORD)parity;
2253 +/* {{{ dio_create_stream_data
2254 + * Creates an initialised stream data structure. Free with efree().
2256 +php_dio_stream_data * dio_create_stream_data(void) {
2257 + php_dio_win32_stream_data * data = emalloc(sizeof(php_dio_win32_stream_data));
2258 + memset(data, 0, sizeof(php_dio_win32_stream_data));
2259 + dio_init_stream_data(&(data->common));
2260 + data->handle = INVALID_HANDLE_VALUE;
2261 + data->desired_access = 0;
2262 + data->creation_disposition = 0;
2263 + data->olddcb.DCBlength = sizeof(DCB);
2265 + return (php_dio_stream_data *)data;
2269 +/* {{{ dio_common_write
2270 + * Writes count chars from the buffer to the stream described by the stream data.
2272 +size_t dio_common_write(php_dio_stream_data *data, const char *buf, size_t count) {
2273 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2276 + if (WriteFile(wdata->handle, buf, (DWORD)count, &total, NULL)) {
2277 + return (size_t)total;
2284 +/* {{{ dio_buffer_read
2285 + * Reads any available chars from the canonical buffer.
2287 +static size_t dio_buffer_read(php_dio_win32_stream_data *wdata, const char *buf, size_t count) {
2288 + php_dio_win32_canon_data *canon_data = wdata->canon_data;
2291 + /* Read always follows write. I.e. if read ptr > write ptr buffer has
2292 + wrapped and so we need to copy two blocks of data. */
2293 + if (canon_data->read_pos > canon_data->write_pos) {
2295 + /* Check we actually need to copy both blocks */
2296 + if ((canon_data->size - canon_data->read_pos) > count) {
2298 + /* No we don't. Just copy as much as we were asked for. */
2299 + memcpy((char*)buf,
2300 + &(canon_data->buf[canon_data->read_pos]),
2302 + /* Update the read pointer. */
2303 + canon_data->read_pos += count;
2305 + /* Return the amount read. */
2309 + /* We need to copy both blocks so copy data up to the end of
2311 + total = canon_data->size - canon_data->read_pos;
2312 + memcpy((char*)buf,
2313 + &(canon_data->buf[canon_data->read_pos]),
2315 + canon_data->read_pos = 0;
2318 + /* Now copy the data from the start of the buffer either up
2319 + count or the number of bytes in the buffer. */
2321 + if (canon_data->write_pos > count) {
2322 + memcpy((char*)buf, canon_data->buf, count);
2323 + canon_data->read_pos = count;
2328 + memcpy((char*)buf, canon_data->buf, canon_data->write_pos);
2329 + canon_data->read_pos = canon_data->write_pos;
2330 + total += canon_data->write_pos;
2336 + /* Else if write follows read. This is a simpler case. We just copy
2337 + either all the data buffered or count, which ever is smaller. */
2338 + } else if (canon_data->write_pos > canon_data->read_pos) {
2339 + if ((canon_data->write_pos - canon_data->read_pos) > count) {
2340 + memcpy((char*)buf,
2341 + &(canon_data->buf[canon_data->read_pos]),
2343 + canon_data->read_pos += count;
2347 + total = canon_data->write_pos - canon_data->read_pos;
2348 + memcpy((char*)buf,
2349 + &(canon_data->buf[canon_data->read_pos]),
2351 + canon_data->read_pos += total;
2357 + /* Else we need to read more data from the data port. */
2361 +/* {{{ dio_com_read
2362 + * Read chars from the data port.
2364 +static size_t dio_com_read(php_dio_stream_data *data, const char *buf, size_t count) {
2365 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2366 + DWORD err, total = 0;
2368 + if (ReadFile(wdata->handle, (void*)buf, (DWORD)count, &total, NULL)) {
2371 + return (size_t)total;
2374 + data->end_of_file = 1;
2377 + if (!data->end_of_file) {
2378 + err = GetLastError();
2380 + if (ERROR_HANDLE_EOF == err) {
2381 + data->end_of_file = 1;
2388 +/* {{{ dio_canonical_read
2389 + * Reads chars from the input stream until the internal buffer is full or a new
2390 + * line is reached.
2392 +static size_t dio_canonical_read(php_dio_win32_stream_data *wdata, const char *buf, size_t count) {
2393 + php_dio_win32_canon_data *canon_data = wdata->canon_data;
2397 + /* See if there's any buffered data and copy it. */
2398 + total = dio_buffer_read(wdata, buf, count);
2403 + /* Need to read more data from the data port. Buffer should be empty(er)
2406 + /* Is the buffer full? */
2407 + if (((canon_data->write_pos + 1) % canon_data->size) ==
2408 + canon_data->read_pos) {
2412 + /* Read a byte from the input checking for EOF. */
2413 + if (!dio_com_read((php_dio_stream_data*)wdata, &ch, 1)) {
2417 + /* Translate CR to newlines (same as ICRNL in POSIX) */
2418 + ch = (ch != '\r') ? ch : '\n';
2420 + /* We read a character! So buffer it. */
2421 + canon_data->buf[canon_data->write_pos++] = ch;
2422 + if (canon_data->write_pos >= canon_data->size) {
2423 + canon_data->write_pos = 0;
2426 + /* End of line/input (^D)? */
2427 + } while ((ch != '\n') && (ch != 0x04));
2429 + return dio_buffer_read(wdata, buf, count);
2433 +/* {{{ dio_common_read
2434 + * Reads count chars to the buffer to the stream described by the stream data.
2436 +size_t dio_common_read(php_dio_stream_data *data, const char *buf, size_t count) {
2438 + /* You ask for no bytes you'll get none :-) */
2443 + if (data->canonical) {
2444 + return dio_canonical_read((php_dio_win32_stream_data*)data, buf, count);
2446 + return dio_com_read(data, buf, count);
2451 +/* {{{ php_dio_stream_data
2452 + * Closes the php_stream.
2454 +int dio_common_close(php_dio_stream_data *data) {
2455 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2457 + if (data->canonical) {
2458 + efree(wdata->canon_data);
2461 + if (!CloseHandle(wdata->handle)) {
2469 +/* {{{ dio_common_set_option
2470 + * Sets/gets stream options
2472 +int dio_common_set_option(php_dio_stream_data *data, int option, int value, void *ptrparam) {
2473 + COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 };
2474 + int old_is_blocking = 0;
2476 + /* Can't do timeouts or non blocking with raw windows streams :-( */
2477 + if (DIO_STREAM_TYPE_SERIAL == data->stream_type) {
2479 + case PHP_STREAM_OPTION_BLOCKING:
2480 + old_is_blocking = data->is_blocking;
2481 + data->is_blocking = value ? 1 : 0;
2483 + /* Only change values if we need to change them. */
2484 + if (data->is_blocking != old_is_blocking) {
2485 + /* If we're not blocking but don't have a timeout
2486 + set to return immediately */
2487 + if (!data->is_blocking && !data->has_timeout) {
2488 + cto.ReadIntervalTimeout = MAXDWORD;
2491 + /* If we have a timeout ignore the blocking and set
2492 + the total time in which to read the data */
2493 + if (data->has_timeout) {
2494 + cto.ReadIntervalTimeout = MAXDWORD;
2495 + cto.ReadTotalTimeoutMultiplier = MAXDWORD;
2496 + cto.ReadTotalTimeoutConstant = (data->timeout_usec / 1000) +
2497 + (data->timeout_sec * 1000);
2500 + if (!SetCommTimeouts(((php_dio_win32_stream_data*)data)->handle, &cto)) {
2501 + return PHP_STREAM_OPTION_RETURN_ERR;
2504 + return old_is_blocking ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
2506 + case PHP_STREAM_OPTION_READ_TIMEOUT:
2508 + /* struct timeval is supported with PHP_WIN32 defined. */
2509 + struct timeval *tv = (struct timeval*)ptrparam;
2511 + /* A timeout of zero seconds and zero microseconds disables
2512 + any existing timeout. */
2513 + if (tv->tv_sec || tv->tv_usec) {
2514 + data->timeout_sec = tv->tv_sec;
2515 + data->timeout_usec = tv->tv_usec;
2516 + data->has_timeout = -1;
2518 + cto.ReadIntervalTimeout = MAXDWORD;
2519 + cto.ReadTotalTimeoutMultiplier = MAXDWORD;
2520 + cto.ReadTotalTimeoutConstant = (data->timeout_usec / 1000) +
2521 + (data->timeout_sec * 1000);
2523 + data->timeout_sec = 0;
2524 + data->timeout_usec = 0;
2525 + data->has_timeout = 0;
2526 + data->timed_out = 0;
2528 + /* If we're not blocking but don't have a timeout
2529 + set to return immediately */
2530 + if (!data->is_blocking) {
2531 + cto.ReadIntervalTimeout = MAXDWORD;
2535 + if (!SetCommTimeouts(((php_dio_win32_stream_data*)data)->handle, &cto)) {
2536 + return PHP_STREAM_OPTION_RETURN_ERR;
2538 + return PHP_STREAM_OPTION_RETURN_OK;
2541 + return PHP_STREAM_OPTION_RETURN_ERR;
2553 +/* {{{ dio_raw_open_stream
2554 + * Opens the underlying stream.
2556 +int dio_raw_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
2557 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2562 + wdata->creation_disposition = OPEN_EXISTING;
2565 + wdata->creation_disposition = TRUNCATE_EXISTING;
2568 + wdata->creation_disposition = OPEN_ALWAYS;
2571 + wdata->creation_disposition = CREATE_NEW;
2576 + if (*mode && (*mode != '+')) {
2580 + if (*mode && (*mode == '+')) {
2581 + wdata->desired_access = GENERIC_READ | GENERIC_WRITE;
2582 + } else if (OPEN_EXISTING == wdata->creation_disposition) {
2583 + wdata->desired_access = GENERIC_READ;
2585 + wdata->desired_access = GENERIC_WRITE;
2588 + wdata->handle = CreateFile(filename, wdata->desired_access, 0,
2589 + NULL, wdata->creation_disposition, FILE_ATTRIBUTE_NORMAL, NULL);
2590 + if (INVALID_HANDLE_VALUE == wdata->handle) {
2591 + err = GetLastError();
2593 + case ERROR_FILE_EXISTS:
2594 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "File exists!");
2597 + case ERROR_FILE_NOT_FOUND:
2598 + /* ERROR_FILE_NOT_FOUND with TRUNCATE_EXISTING means that
2599 + * the file doesn't exist so now try to create it. */
2600 + if (TRUNCATE_EXISTING == wdata->creation_disposition) {
2601 + wdata->handle = CreateFile(filename, wdata->desired_access, 0,
2602 + NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2603 + if (INVALID_HANDLE_VALUE == wdata->handle) {
2604 + err = GetLastError();
2617 + /* If canonical allocate the canonical buffer. */
2618 + if (data->canonical) {
2619 + wdata->canon_data = emalloc(sizeof(php_dio_win32_canon_data));
2620 + memset(wdata->canon_data, 0, sizeof(php_dio_win32_canon_data));
2621 + wdata->canon_data->size = DIO_WIN32_CANON_BUF_SIZE;
2628 +/* {{{ dio_serial_init
2629 + * Initialises the serial port
2631 +static int dio_serial_init(php_dio_stream_data *data TSRMLS_DC) {
2632 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2633 + DWORD err, rate_def, data_bits_def, stop_bits_def, parity_def;
2636 + if (!dio_data_rate_to_define(data->data_rate, &rate_def)) {
2637 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_rate value (%d) (%d)", data->data_rate, __LINE__);
2641 + if (!dio_data_bits_to_define(data->data_bits, &data_bits_def)) {
2642 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_bits value (%d)", data->data_bits);
2646 + if (!dio_stop_bits_to_define(data->stop_bits, &stop_bits_def)) {
2647 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid stop_bits value (%d)", data->stop_bits);
2651 + if (!dio_parity_to_define(data->parity, &parity_def)) {
2652 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid parity value (%d)", data->parity);
2656 + if (!GetCommState(wdata->handle, &(wdata->olddcb))) {
2657 + err = GetLastError();
2658 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "GetCommState() failed! (%d)", err);
2662 + /* Init the DCB structure */
2663 + memset(&dcb, 0, sizeof(DCB));
2664 + dcb.DCBlength = sizeof(DCB);
2666 + /* Set the communication parameters */
2668 + dcb.BaudRate = rate_def;
2669 + dcb.ByteSize = (BYTE)data_bits_def;
2670 + dcb.StopBits = (BYTE)stop_bits_def;
2671 + dcb.Parity = (BYTE)parity_def;
2673 + /* Set the control line parameters */
2674 + dcb.fDtrControl = DTR_CONTROL_DISABLE;
2675 + dcb.fDsrSensitivity = FALSE;
2676 + dcb.fOutxDsrFlow = FALSE;
2677 + dcb.fTXContinueOnXoff = FALSE;
2678 + dcb.fOutX = FALSE;
2680 + dcb.fErrorChar = FALSE;
2681 + dcb.fNull = FALSE;
2682 + dcb.fAbortOnError = FALSE;
2684 + /* Hardware flow control */
2685 + if (data->flow_control) {
2686 + dcb.fOutxCtsFlow = TRUE;
2687 + dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
2689 + dcb.fOutxCtsFlow = FALSE;
2690 + dcb.fRtsControl = RTS_CONTROL_DISABLE;
2693 + if (!SetCommState(wdata->handle, &dcb)) {
2702 +/* {{{ dio_serial_uninit
2703 + * Restores the serial settings back to their original state.
2705 +int dio_serial_uninit(php_dio_stream_data *data) {
2706 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2708 + if (!SetCommState(wdata->handle, &(wdata->olddcb))) {
2716 +/* {{{ dio_serial_flush
2717 + * Purges the serial buffers of data.
2719 +int dio_serial_purge(php_dio_stream_data *data) {
2720 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2723 + /* Purge the canonical buffer if required */
2724 + if (data->canonical && ((wdata->desired_access & GENERIC_READ) == GENERIC_READ)) {
2725 + wdata->canon_data->read_pos = 0;
2726 + wdata->canon_data->write_pos = 0;
2729 + /* Purge the com port */
2730 + if ((wdata->desired_access & (GENERIC_READ|GENERIC_WRITE)) == (GENERIC_READ|GENERIC_WRITE)) {
2731 + ret = PurgeComm(wdata->handle, PURGE_RXCLEAR|PURGE_TXCLEAR);
2732 + } else if ((wdata->desired_access & GENERIC_WRITE) == GENERIC_WRITE) {
2733 + ret = PurgeComm(wdata->handle, PURGE_TXCLEAR);
2734 + } else if ((wdata->desired_access & GENERIC_READ) == GENERIC_READ) {
2735 + ret = PurgeComm(wdata->handle, PURGE_RXCLEAR);
2742 +/* {{{ dio_serial_open_stream
2743 + * Opens the underlying stream.
2745 +int dio_serial_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
2746 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2747 + COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 };
2750 + if (!dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
2754 + if (!GetCommTimeouts(wdata->handle, &(wdata->oldcto))) {
2755 + err = GetLastError();
2756 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "SetCommTimeouts() failed! (%d) Not a comm port?", err);
2757 + CloseHandle(wdata->handle);
2761 + /* If we're not blocking but don't have a timeout
2762 + set to return immediately */
2763 + if (!data->is_blocking && !data->has_timeout) {
2764 + cto.ReadIntervalTimeout = MAXDWORD;
2767 + /* If we have a timeout ignore the blocking and set
2768 + the total time in which to read the data */
2769 + if (data->has_timeout) {
2770 + cto.ReadIntervalTimeout = MAXDWORD;
2771 + cto.ReadTotalTimeoutMultiplier = MAXDWORD;
2772 + cto.ReadTotalTimeoutConstant = (data->timeout_usec / 1000) +
2773 + (data->timeout_sec * 1000);
2776 + if (!SetCommTimeouts(wdata->handle, &cto)) {
2777 + CloseHandle(wdata->handle);
2781 + if (!dio_serial_init(data TSRMLS_CC)) {
2782 + CloseHandle(wdata->handle);
2791 + * Local variables:
2792 + * c-basic-offset: 4
2795 + * vim600: fdm=marker
2796 + * vim: sw=4 ts=4 noet
2799 +++ b/ext/dio/php_dio.h
2802 + +----------------------------------------------------------------------+
2804 + +----------------------------------------------------------------------+
2805 + | Copyright (c) 1997-2004 The PHP Group |
2806 + +----------------------------------------------------------------------+
2807 + | This source file is subject to version 3.0 of the PHP license, |
2808 + | that is bundled with this package in the file LICENSE, and is |
2809 + | available through the world-wide-web at the following url: |
2810 + | http://www.php.net/license/3_0.txt. |
2811 + | If you did not receive a copy of the PHP license and are unable to |
2812 + | obtain it through the world-wide-web, please send a note to |
2813 + | license@php.net so we can mail you a copy immediately. |
2814 + +----------------------------------------------------------------------+
2821 +#include "php_dio_common.h"
2822 +#include "php_dio_stream_wrappers.h"
2824 +extern zend_module_entry dio_module_entry;
2825 +#define phpext_dio_ptr &dio_module_entry
2827 +#define PHP_DIO_VERSION "0.0.4RC4"
2829 +/* Standard module functions. */
2830 +PHP_MINIT_FUNCTION(dio);
2831 +PHP_MSHUTDOWN_FUNCTION(dio);
2832 +PHP_RINIT_FUNCTION(dio);
2833 +PHP_RSHUTDOWN_FUNCTION(dio);
2834 +PHP_MINFO_FUNCTION(dio);
2836 +/* Legacy functions. */
2837 +PHP_FUNCTION(dio_open);
2838 +PHP_FUNCTION(dio_truncate);
2839 +PHP_FUNCTION(dio_stat);
2840 +PHP_FUNCTION(dio_seek);
2841 +PHP_FUNCTION(dio_read);
2842 +PHP_FUNCTION(dio_write);
2843 +PHP_FUNCTION(dio_fcntl);
2844 +PHP_FUNCTION(dio_close);
2845 +PHP_FUNCTION(dio_tcsetattr);
2855 + * Local variables:
2857 + * c-basic-offset: 4
2858 + * indent-tabs-mode: t
2862 +++ b/ext/dio/php_dio_common.h
2865 + +----------------------------------------------------------------------+
2867 + +----------------------------------------------------------------------+
2868 + | Copyright (c) 2009 Melanie Rhianna Lewis |
2869 + +----------------------------------------------------------------------+
2870 + | This source file is subject to version 3.0 of the PHP license, |
2871 + | that is bundled with this package in the file LICENSE, and is |
2872 + | available through the world-wide-web at the following url: |
2873 + | http://www.php.net/license/3_0.txt. |
2874 + | If you did not receive a copy of the PHP license and are unable to |
2875 + | obtain it through the world-wide-web, please send a note to |
2876 + | license@php.net so we can mail you a copy immediately. |
2877 + +----------------------------------------------------------------------+
2878 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
2879 + +----------------------------------------------------------------------+
2882 +#ifndef PHP_DIO_COMMON_H_
2883 +#define PHP_DIO_COMMON_H_
2886 +#define PHP_DIO_API __declspec(dllexport)
2888 +#define PHP_DIO_API
2892 +#include "php_dio_win32.h"
2894 +#include "php_dio_posix.h"
2897 +#define DIO_STREAM_TYPE_NONE 0
2898 +#define DIO_STREAM_TYPE_RAW 1
2899 +#define DIO_STREAM_TYPE_SERIAL 2
2901 +long dio_convert_to_long(zval *val);
2903 +php_dio_stream_data * dio_create_stream_data(void);
2905 +void dio_init_stream_data(php_dio_stream_data *data);
2907 +void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data TSRMLS_DC);
2909 +void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data TSRMLS_DC);
2911 +void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC);
2913 +void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC);
2915 +size_t dio_common_write(php_dio_stream_data *data, const char *buf, size_t count);
2917 +size_t dio_common_read(php_dio_stream_data *data, const char *buf, size_t count);
2919 +int dio_common_close(php_dio_stream_data *data);
2921 +int dio_common_set_option(php_dio_stream_data *data, int option, int value, void *ptrparam);
2923 +int dio_raw_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC);
2925 +int dio_serial_uninit(php_dio_stream_data *data);
2927 +int dio_serial_purge(php_dio_stream_data *data);
2929 +int dio_serial_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC);
2931 +#endif /* PHP_DIO_COMMON_H_ */
2934 + * Local variables:
2935 + * c-basic-offset: 4
2938 + * vim600: fdm=marker
2939 + * vim: sw=4 ts=4 noet
2942 +++ b/ext/dio/php_dio_common_data.h
2945 + +----------------------------------------------------------------------+
2947 + +----------------------------------------------------------------------+
2948 + | Copyright (c) 2009 Melanie Rhianna Lewis |
2949 + +----------------------------------------------------------------------+
2950 + | This source file is subject to version 3.0 of the PHP license, |
2951 + | that is bundled with this package in the file LICENSE, and is |
2952 + | available through the world-wide-web at the following url: |
2953 + | http://www.php.net/license/3_0.txt. |
2954 + | If you did not receive a copy of the PHP license and are unable to |
2955 + | obtain it through the world-wide-web, please send a note to |
2956 + | license@php.net so we can mail you a copy immediately. |
2957 + +----------------------------------------------------------------------+
2958 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
2959 + +----------------------------------------------------------------------+
2962 +#ifndef PHP_DIO_COMMON_DATA_H_
2963 +#define PHP_DIO_COMMON_DATA_H_
2965 +/* This is the data structure 'base class'. It is common data fields used
2966 + * by all versions of DIO.
2968 +typedef struct _php_dio_stream_data {
2971 + /* Stream options */
2973 +#ifdef DIO_HAS_FILEPERMS
2977 +#ifdef DIO_NONBLOCK
2981 + long timeout_usec;
2984 + /* Serial options */
2991 +} php_dio_stream_data ;
2993 +#endif /* PHP_DIO_COMMON_DATA_H_ */
2996 + * Local variables:
2997 + * c-basic-offset: 4
3000 + * vim600: fdm=marker
3001 + * vim: sw=4 ts=4 noet
3004 +++ b/ext/dio/php_dio_posix.h
3007 + +----------------------------------------------------------------------+
3009 + +----------------------------------------------------------------------+
3010 + | Copyright (c) 2009 Melanie Rhianna Lewis |
3011 + +----------------------------------------------------------------------+
3012 + | This source file is subject to version 3.0 of the PHP license, |
3013 + | that is bundled with this package in the file LICENSE, and is |
3014 + | available through the world-wide-web at the following url: |
3015 + | http://www.php.net/license/3_0.txt. |
3016 + | If you did not receive a copy of the PHP license and are unable to |
3017 + | obtain it through the world-wide-web, please send a note to |
3018 + | license@php.net so we can mail you a copy immediately. |
3019 + +----------------------------------------------------------------------+
3020 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
3021 + +----------------------------------------------------------------------+
3024 +#ifndef PHP_DIO_POSIX_H_
3025 +#define PHP_DIO_POSIX_H_
3027 +#include <sys/stat.h>
3028 +#include <sys/types.h>
3029 +#include <sys/select.h>
3030 +#include <sys/time.h>
3032 +#ifdef HAVE_UNISTD_H
3033 +#include <unistd.h>
3037 +#include <termios.h>
3041 + * Detect if we can support non blocking IO.
3044 +#define DIO_NONBLOCK O_NONBLOCK
3047 +#define DIO_NONBLOCK O_NDELAY
3052 + * POSIXy platforms have file permissions
3054 +#define DIO_HAS_FILEPERMS
3056 +#include "php_dio_common_data.h"
3058 +typedef struct _php_dio_posix_stream_data {
3059 + php_dio_stream_data common;
3062 + /* Serial options */
3063 + struct termios oldtio;
3064 +} php_dio_posix_stream_data ;
3066 +#endif /* PHP_DIO_POSIX_H_ */
3069 + * Local variables:
3070 + * c-basic-offset: 4
3073 + * vim600: fdm=marker
3074 + * vim: sw=4 ts=4 noet
3077 +++ b/ext/dio/php_dio_stream_wrappers.h
3080 + +----------------------------------------------------------------------+
3082 + +----------------------------------------------------------------------+
3083 + | Copyright (c) 2009 Melanie Rhianna Lewis |
3084 + +----------------------------------------------------------------------+
3085 + | This source file is subject to version 3.0 of the PHP license, |
3086 + | that is bundled with this package in the file LICENSE, and is |
3087 + | available through the world-wide-web at the following url: |
3088 + | http://www.php.net/license/3_0.txt. |
3089 + | If you did not receive a copy of the PHP license and are unable to |
3090 + | obtain it through the world-wide-web, please send a note to |
3091 + | license@php.net so we can mail you a copy immediately. |
3092 + +----------------------------------------------------------------------+
3093 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
3094 + +----------------------------------------------------------------------+
3097 +#ifndef PHP_DIO_STREAM_WRAPPERS_H_
3098 +#define PHP_DIO_STREAM_WRAPPERS_H_
3100 +#define DIO_RAW_STREAM_NAME "dio.raw"
3101 +#define DIO_RAW_STREAM_PROTOCOL "dio.raw://"
3102 +#define DIO_SERIAL_STREAM_NAME "dio.serial"
3103 +#define DIO_SERIAL_STREAM_PROTOCOL "dio.serial://"
3105 +extern php_stream_wrapper php_dio_raw_stream_wrapper;
3107 +PHP_FUNCTION(dio_raw);
3109 +extern php_stream_wrapper php_dio_serial_stream_wrapper;
3111 +PHP_FUNCTION(dio_serial);
3113 +#endif /* PHP_DIO_STREAM_WRAPPERS_H_ */
3116 + * Local variables:
3117 + * c-basic-offset: 4
3120 + * vim600: fdm=marker
3121 + * vim: sw=4 ts=4 noet
3124 +++ b/ext/dio/php_dio_win32.h
3127 + +----------------------------------------------------------------------+
3129 + +----------------------------------------------------------------------+
3130 + | Copyright (c) 2009 Melanie Rhianna Lewis |
3131 + +----------------------------------------------------------------------+
3132 + | This source file is subject to version 3.0 of the PHP license, |
3133 + | that is bundled with this package in the file LICENSE, and is |
3134 + | available through the world-wide-web at the following url: |
3135 + | http://www.php.net/license/3_0.txt. |
3136 + | If you did not receive a copy of the PHP license and are unable to |
3137 + | obtain it through the world-wide-web, please send a note to |
3138 + | license@php.net so we can mail you a copy immediately. |
3139 + +----------------------------------------------------------------------+
3140 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
3141 + +----------------------------------------------------------------------+
3144 +#ifndef PHP_DIO_WIN32_H_
3145 +#define PHP_DIO_WIN32_H_
3147 +#include <windows.h>
3149 +/* Windows platform can do non blocking. */
3150 +#define DIO_NONBLOCK
3152 +#include "php_dio_common_data.h"
3154 +#define DIO_WIN32_CANON_BUF_SIZE 8192
3156 +/* This is the buffer information when reading in canonical mode. Data is
3157 + read right up to either buffer being full or a newline being read. Excess
3158 + data will be retained in the buffer until the next read. */
3159 +typedef struct _php_dio_win32_canon_data {
3163 + char buf[DIO_WIN32_CANON_BUF_SIZE];
3165 +} php_dio_win32_canon_data;
3167 +typedef struct _php_dio_win32_stream_data {
3168 + php_dio_stream_data common;
3170 + DWORD desired_access;
3171 + DWORD creation_disposition;
3173 + COMMTIMEOUTS oldcto;
3174 + php_dio_win32_canon_data *canon_data;
3176 +} php_dio_win32_stream_data ;
3178 +#endif /* PHP_DIO_WIN32_H_ */
3181 + * Local variables:
3182 + * c-basic-offset: 4
3185 + * vim600: fdm=marker
3186 + * vim: sw=4 ts=4 noet
3189 +++ b/ext/dio/tests/001.phpt
3192 +Test dio legacy open
3194 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3197 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3200 + $filename = "/dev/null";
3202 + $filename = "NUL";
3205 + $f = dio_open($filename,O_RDONLY);
3207 + echo "Legacy open passed";
3209 + echo "Legacy open failed";
3215 +++ b/ext/dio/tests/dio_raw_stream_001.phpt
3218 +Test dio raw stream open
3220 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3223 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3226 + $filename = "dio.raw:///dev/null";
3228 + $filename = "dio.raw://NUL";
3231 + $f = fopen($filename, "r+");
3233 + echo "Raw open passed";
3236 + echo "Raw open failed";
3242 +++ b/ext/dio/tests/dio_raw_stream_002.phpt
3245 +Test dio raw stream close
3247 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3250 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3253 + $filename = "dio.raw:///dev/null";
3255 + $filename = "dio.raw://NUL";
3258 + $f = fopen($filename, "r+");
3261 + echo "Raw close passed";
3263 + echo "Raw close failed";
3266 + echo "Raw open failed";
3272 +++ b/ext/dio/tests/dio_raw_stream_003.phpt
3275 +Test dio raw stream write
3277 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3280 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3283 + $filename = "dio.raw:///dev/null";
3285 + $filename = "dio.raw://NUL";
3288 + $f = fopen($filename, "r+");
3290 + $data = str_repeat("+", 2048);
3291 + if (fwrite($f, $data)) {
3292 + echo "Raw write passed";
3294 + echo "Raw write failed";
3298 + echo "Raw open failed";
3304 +++ b/ext/dio/tests/dio_raw_stream_004.phpt
3310 + if (!extension_loaded('dio')) print 'skip';
3314 + // Create a temp file with some content to read
3316 + // Create the temp file name
3317 + if (!function_exists('sys_get_temp_dir')) {
3318 + if (!($tmpdir = getenv('TEMP'))) {
3322 + $tmpdir = sys_get_temp_dir();
3324 + $filename = tempnam($tmpdir, 'dio_raw_stream_004.tmp');
3326 + // Create the temp file
3327 + $tf = fopen($filename, "w");
3329 + fwrite($tf, str_repeat('*', 2048));
3332 + echo "Can\'t create temp file";
3335 + $f = fopen('dio.raw://' . $filename, "r");
3337 + $data = fread($f, 2048);
3338 + if ($data && (strlen($data) == 2048)) {
3339 + echo "Raw read passed";
3341 + echo "Raw read failed";
3345 + echo "Raw open failed";
3348 + unlink($filename);
3353 +++ b/ext/dio/tests/dio_raw_stream_005.phpt
3359 + if (!extension_loaded('dio')) print 'skip';
3363 + // Create a temp file with some content to read
3365 + // Create the temp file name
3366 + if (!function_exists('sys_get_temp_dir')) {
3367 + if (!($tmpdir = getenv('TEMP'))) {
3371 + $tmpdir = sys_get_temp_dir();
3373 + $filename = tempnam($tmpdir, 'dio_raw_stream_005.tmp');
3375 + // Create the temp file
3376 + $tf = fopen($filename, "w");
3380 + echo "Can\'t create temp file";
3383 + $f = fopen('dio.raw://' . $filename, "r");
3385 + $data = fread($f, 2048);
3387 + echo "Raw feof passed";
3389 + echo "Raw feof failed";
3393 + echo "Raw open failed";
3396 + unlink($filename);
3401 +++ b/ext/dio/tests/dio_raw_stream_006.phpt
3407 + if (!extension_loaded('dio')) print 'skip';
3411 + // Create a temp file with some content to read
3413 + // Create the temp file name
3414 + if (!function_exists('sys_get_temp_dir')) {
3415 + if (!($tmpdir = getenv('TEMP'))) {
3419 + $tmpdir = sys_get_temp_dir();
3421 + $filename = tempnam($tmpdir, 'diotest');
3423 + // Create the temp file
3424 + $tf = fopen($filename, "w");
3426 + fwrite($tf, str_repeat('*', 2048));
3429 + echo "Can\'t create temp file";
3432 + $f = fopen('dio.raw://' . $filename, "r");
3434 + $data = fread($f, 1024);
3435 + if (stream_set_blocking($f, false)) {
3436 + echo "Raw set blocking passed";
3438 + echo "Raw set blocking failed";
3442 + echo "Raw open failed";
3445 + unlink($filename);
3448 +Raw set blocking passed
3450 +++ b/ext/dio/tests/dio_raw_stream_007.phpt
3453 +Test dio_raw() call
3455 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3458 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3461 + $filename = "/dev/null";
3463 + $filename = "NUL";
3466 + $f = dio_raw($filename, "r+");
3468 + echo "dio_raw passed";