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) || DIO_SAFE_MODE_CHECK(file_name, "wb+")) {
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));
130 + if (!new_php_fd(&f, fd)) {
134 + ZEND_REGISTER_RESOURCE(return_value, f, le_fd);
140 +/* {{{ proto resource dio_fdopen(int fd)
141 + Returns a resource for the specified file descriptor. */
142 +PHP_FUNCTION(dio_fdopen)
148 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &lfd) == FAILURE) {
154 + if ((fcntl(fd, F_GETFL, 0) == -1) && (errno == EBADF)) {
155 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad file descriptor %d", fd);
159 + if (!new_php_fd(&f, fd)) {
163 + ZEND_REGISTER_RESOURCE(return_value, f, le_fd);
168 +/* {{{ proto resource dio_dup(resource fd)
169 + Opens a duplicate of the specified open resource. */
170 +PHP_FUNCTION(dio_dup)
176 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &r_fd) == FAILURE) {
180 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
184 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot duplication file descriptor %d: %s", f->fd, strerror(errno));
188 + if (!new_php_fd(&df, dfd)) {
192 + ZEND_REGISTER_RESOURCE(return_value, df, le_fd);
197 +/* {{{ proto string dio_read(resource fd[, int n])
198 + Read n bytes from fd and return them, if n is not specified, read 1k */
199 +PHP_FUNCTION(dio_read)
207 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &r_fd, &bytes) == FAILURE) {
211 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
214 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0.");
218 + data = emalloc(bytes + 1);
219 + res = read(f->fd, data, bytes);
225 + data = erealloc(data, res + 1);
228 + RETURN_STRINGL(data, res, 0);
232 +/* {{{ proto int dio_write(resource fd, string data[, int len])
233 + Write data to fd with optional truncation at length */
234 +PHP_FUNCTION(dio_write)
240 + long trunc_len = 0;
243 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &r_fd, &data, &data_len, &trunc_len) == FAILURE) {
247 + if (trunc_len < 0 || trunc_len > data_len) {
248 + 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.");
252 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
254 + res = write(f->fd, data, trunc_len ? trunc_len : data_len);
256 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot write data to file descriptor %d: %s", f->fd, strerror(errno));
265 +/* {{{ proto bool dio_truncate(resource fd, int offset)
266 + Truncate file descriptor fd to offset bytes */
267 +PHP_FUNCTION(dio_truncate)
273 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &r_fd, &offset) == FAILURE) {
277 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
279 + if (ftruncate(f->fd, offset) == -1) {
280 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't truncate %d to %ld bytes: %s", f->fd, offset, strerror(errno));
289 +#define ADD_FIELD(f, v) add_assoc_long_ex(return_value, (f), sizeof(f), v);
291 +/* {{{ proto array dio_stat(resource fd)
292 + Get stat information about the file descriptor fd */
293 +PHP_FUNCTION(dio_stat)
299 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &r_fd) == FAILURE) {
303 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
305 + if (fstat(f->fd, &s) == -1) {
306 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot stat %d: %s", f->fd, strerror(errno));
310 + array_init(return_value);
311 + ADD_FIELD("device", s.st_dev);
312 + ADD_FIELD("inode", s.st_ino);
313 + ADD_FIELD("mode", s.st_mode);
314 + ADD_FIELD("nlink", s.st_nlink);
315 + ADD_FIELD("uid", s.st_uid);
316 + ADD_FIELD("gid", s.st_gid);
317 + ADD_FIELD("device_type", s.st_rdev);
318 + ADD_FIELD("size", s.st_size);
320 + ADD_FIELD("block_size", s.st_blksize);
321 + ADD_FIELD("blocks", s.st_blocks);
323 + ADD_FIELD("atime", s.st_atime);
324 + ADD_FIELD("mtime", s.st_mtime);
325 + ADD_FIELD("ctime", s.st_ctime);
329 +/* {{{ proto int dio_seek(resource fd, int pos, int whence)
330 + Seek to pos on fd from whence */
331 +PHP_FUNCTION(dio_seek)
336 + long whence = SEEK_SET;
338 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &r_fd, &offset, &whence) == FAILURE) {
342 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
344 + RETURN_LONG(lseek(f->fd, offset, whence));
350 +/* {{{ proto mixed dio_fcntl(resource fd, int cmd[, mixed arg])
351 + Perform a c library fcntl on fd */
352 +PHP_FUNCTION(dio_fcntl)
359 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|z", &r_fd, &cmd, &arg) == FAILURE) {
363 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
369 + struct flock lk = {0};
373 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be array or int, none given");
376 + if (Z_TYPE_P(arg) == IS_ARRAY) {
378 + if (zend_hash_find(fh, "start", sizeof("start"), (void **) &element) == FAILURE) {
381 + lk.l_start = Z_LVAL_PP(element);
384 + if (zend_hash_find(fh, "length", sizeof("length"), (void **) &element) == FAILURE) {
387 + lk.l_len = Z_LVAL_PP(element);
390 + if (zend_hash_find(fh, "whence", sizeof("whence"), (void **) &element) == FAILURE) {
393 + lk.l_whence = Z_LVAL_PP(element);
396 + if (zend_hash_find(fh, "type", sizeof("type"), (void **) &element) == FAILURE) {
399 + lk.l_type = Z_LVAL_PP(element);
401 + } else if (Z_TYPE_P(arg) == IS_LONG) {
404 + lk.l_whence = SEEK_SET;
405 + lk.l_type = Z_LVAL_P(arg);
407 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be array or int, %s given", zend_zval_type_name(arg));
411 + RETURN_LONG(fcntl(f->fd, cmd, &lk));
415 + struct flock lk = {0};
417 + fcntl(f->fd, cmd, &lk);
419 + array_init(return_value);
420 + add_assoc_long(return_value, "type", lk.l_type);
421 + add_assoc_long(return_value, "whence", lk.l_whence);
422 + add_assoc_long(return_value, "start", lk.l_start);
423 + add_assoc_long(return_value, "length", lk.l_len);
424 + add_assoc_long(return_value, "pid", lk.l_pid);
431 + if (!arg || Z_TYPE_P(arg) != IS_LONG) {
432 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be int");
436 + if (!new_php_fd(&new_f, fcntl(f->fd, cmd, Z_LVAL_P(arg)))) {
439 + ZEND_REGISTER_RESOURCE(return_value, new_f, le_fd);
443 + if (!arg || Z_TYPE_P(arg) != IS_LONG) {
444 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be int");
448 + RETURN_LONG(fcntl(f->fd, cmd, Z_LVAL_P(arg)));
456 +/* {{{ proto mixed dio_tcsetattr(resource fd, array args )
457 + Perform a c library tcsetattr on fd */
458 +PHP_FUNCTION(dio_tcsetattr)
463 + struct termios newtio;
464 + int Baud_Rate, Data_Bits=8, Stop_Bits=1, Parity=0, Flow_Control=1, Is_Canonical=1;
465 + long BAUD,DATABITS,STOPBITS,PARITYON,PARITY;
469 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &r_fd, &arg) == FAILURE) {
473 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
475 + if (Z_TYPE_P(arg) != IS_ARRAY) {
476 + php_error_docref(NULL TSRMLS_CC, E_WARNING,"tcsetattr, third argument should be an associative array");
482 + if (zend_hash_find(fh, "baud", sizeof("baud"), (void **) &element) == FAILURE) {
485 + Baud_Rate = Z_LVAL_PP(element);
488 + if (zend_hash_find(fh, "bits", sizeof("bits"), (void **) &element) == FAILURE) {
491 + Data_Bits = Z_LVAL_PP(element);
494 + if (zend_hash_find(fh, "stop", sizeof("stop"), (void **) &element) == FAILURE) {
497 + Stop_Bits = Z_LVAL_PP(element);
500 + if (zend_hash_find(fh, "parity", sizeof("parity"), (void **) &element) == FAILURE) {
503 + Parity = Z_LVAL_PP(element);
506 + if (zend_hash_find(fh, "flow_control", sizeof("flow_control"), (void **) &element) == FAILURE) {
509 + Flow_Control = Z_LVAL_PP(element);
512 + if (zend_hash_find(fh, "is_canonical", sizeof("is_canonical"), (void **) &element) == FAILURE) {
515 + Is_Canonical = Z_LVAL_PP(element);
518 + /* assign to correct values... */
519 + switch (Baud_Rate) {
566 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid baud rate %d", Baud_Rate);
569 + switch (Data_Bits) {
583 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data bits %d", Data_Bits);
586 + switch (Stop_Bits) {
594 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid stop bits %d", Stop_Bits);
612 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid parity %d", Parity);
616 + memset(&newtio, 0, sizeof(newtio));
617 + tcgetattr(f->fd, &newtio);
619 + if (Is_Canonical) {
620 + newtio.c_iflag = IGNPAR | ICRNL;
621 + newtio.c_oflag = 0;
622 + newtio.c_lflag = ICANON;
624 + cfmakeraw(&newtio);
627 + newtio.c_cflag = BAUD | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
630 + if (Flow_Control) {
631 + newtio.c_cflag |= CRTSCTS;
637 + newtio.c_cc[VMIN] = 1;
638 + newtio.c_cc[VTIME] = 0;
639 + tcflush(f->fd, TCIFLUSH);
640 + tcsetattr(f->fd,TCSANOW,&newtio);
647 +/* {{{ proto void dio_close(resource fd)
648 + Close the file descriptor given by fd */
649 +PHP_FUNCTION(dio_close)
654 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &r_fd) == FAILURE) {
658 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
660 + zend_list_delete(Z_LVAL_P(r_fd));
664 +#define RDIOC(c) REGISTER_LONG_CONSTANT(#c, c, CONST_CS | CONST_PERSISTENT)
666 +/* {{{ dio_init_legacy_defines
667 + * Initialises the legacy PHP defines
669 +static void dio_init_legacy_defines(int module_number TSRMLS_DC) {
720 +ZEND_BEGIN_ARG_INFO_EX(dio_open_args, 0, 0, 2)
721 + ZEND_ARG_INFO(0, filename)
722 + ZEND_ARG_INFO(0, flags)
723 + ZEND_ARG_INFO(0, mode)
726 +ZEND_BEGIN_ARG_INFO_EX(dio_fdopen_args, 0, 0, 1)
727 + ZEND_ARG_INFO(0, fd)
730 +ZEND_BEGIN_ARG_INFO_EX(dio_dup_args, 0, 0, 1)
731 + ZEND_ARG_INFO(0, fd)
735 +ZEND_BEGIN_ARG_INFO_EX(dio_read_args, 0, 0, 1)
736 + ZEND_ARG_INFO(0, fd)
737 + ZEND_ARG_INFO(0, n)
740 +ZEND_BEGIN_ARG_INFO_EX(dio_write_args, 0, 0, 2)
741 + ZEND_ARG_INFO(0, fd)
742 + ZEND_ARG_INFO(0, data)
743 + ZEND_ARG_INFO(0, len)
746 +ZEND_BEGIN_ARG_INFO_EX(dio_stat_args, 0, 0, 1)
747 + ZEND_ARG_INFO(0, fd)
750 +ZEND_BEGIN_ARG_INFO_EX(dio_truncate_args, 0, 0, 2)
751 + ZEND_ARG_INFO(0, fd)
752 + ZEND_ARG_INFO(0, offset)
755 +ZEND_BEGIN_ARG_INFO_EX(dio_seek_args, 0, 0, 3)
756 + ZEND_ARG_INFO(0, fd)
757 + ZEND_ARG_INFO(0, pos)
758 + ZEND_ARG_INFO(0, whence)
761 +ZEND_BEGIN_ARG_INFO_EX(dio_fcntl_args, 0, 0, 2)
762 + ZEND_ARG_INFO(0, fd)
763 + ZEND_ARG_INFO(0, cmd)
764 + ZEND_ARG_INFO(0, arg)
767 +ZEND_BEGIN_ARG_INFO_EX(dio_tcsetattr_args, 0, 0, 2)
768 + ZEND_ARG_INFO(0, fd)
769 + ZEND_ARG_INFO(0, args)
772 +ZEND_BEGIN_ARG_INFO_EX(dio_close_args, 0, 0, 1)
773 + ZEND_ARG_INFO(0, fd)
777 + +----------------------------------------------------------------------+
778 + | END OF DEPRECATED FUNCTIONALITY |
779 + +----------------------------------------------------------------------+
782 +ZEND_BEGIN_ARG_INFO_EX(dio_raw_args, 0, 0, 2)
783 + ZEND_ARG_INFO(0, filename)
784 + ZEND_ARG_INFO(0, mode)
785 + ZEND_ARG_INFO(0, options)
788 +ZEND_BEGIN_ARG_INFO_EX(dio_serial_args, 0, 0, 2)
789 + ZEND_ARG_INFO(0, filename)
790 + ZEND_ARG_INFO(0, mode)
791 + ZEND_ARG_INFO(0, options)
794 +static zend_object_handlers dio_raw_object_handlers;
796 +static zend_function_entry dio_functions[] = {
797 + /* Class functions. */
799 + /* Legacy functions (Deprecated - See dio_legacy.c) */
800 + PHP_FE(dio_open, dio_open_args)
802 + PHP_FE(dio_fdopen, dio_fdopen_args)
803 + PHP_FE(dio_dup, dio_dup_args)
804 + PHP_FE(dio_truncate, dio_truncate_args)
806 + PHP_FE(dio_stat, dio_stat_args)
807 + PHP_FE(dio_seek, dio_seek_args)
809 + PHP_FE(dio_fcntl, dio_fcntl_args)
811 + PHP_FE(dio_read, dio_read_args)
812 + PHP_FE(dio_write, dio_write_args)
813 + PHP_FE(dio_close, dio_close_args)
815 + PHP_FE(dio_tcsetattr, dio_tcsetattr_args)
818 + /* Stream functions */
819 + PHP_FE(dio_raw, dio_raw_args)
820 + PHP_FE(dio_serial, dio_serial_args)
822 + /* End of functions */
826 +zend_module_entry dio_module_entry = {
827 + STANDARD_MODULE_HEADER,
836 + STANDARD_MODULE_PROPERTIES
839 +#ifdef COMPILE_DL_DIO
840 +ZEND_GET_MODULE(dio)
843 +#define DIO_UNDEF_CONST -1
845 +/* {{{ PHP_MINIT_FUNCTION
847 +PHP_MINIT_FUNCTION(dio)
849 + /* Legacy resource destructor. */
850 + le_fd = zend_register_list_destructors_ex(_dio_close_fd, NULL, le_fd_name, module_number);
852 + dio_init_legacy_defines(module_number TSRMLS_CC);
854 + /* Register the stream wrappers */
855 + return (php_register_url_stream_wrapper(DIO_RAW_STREAM_NAME, &php_dio_raw_stream_wrapper TSRMLS_CC) == SUCCESS &&
856 + php_register_url_stream_wrapper(DIO_SERIAL_STREAM_NAME, &php_dio_serial_stream_wrapper TSRMLS_CC) == SUCCESS) ? SUCCESS : FAILURE;
860 +/* {{{ PHP_MSHUTDOWN_FUNCTION
862 +PHP_MSHUTDOWN_FUNCTION(dio)
864 + return (php_unregister_url_stream_wrapper(DIO_RAW_STREAM_NAME TSRMLS_CC) == SUCCESS &&
865 + php_unregister_url_stream_wrapper(DIO_SERIAL_STREAM_NAME TSRMLS_CC) == SUCCESS) ? SUCCESS : FAILURE;
869 +/* {{{ PHP_MINFO_FUNCTION
871 +PHP_MINFO_FUNCTION(dio)
873 + php_info_print_table_start();
874 + php_info_print_table_header(2, "dio support", "enabled");
875 + php_info_print_table_row(2, "version", PHP_DIO_VERSION);
876 + php_info_print_table_end();
882 + * c-basic-offset: 4
885 + * vim600: fdm=marker
886 + * vim: sw=4 ts=4 noet
889 +++ b/ext/dio/dio_common.c
892 + +----------------------------------------------------------------------+
894 + +----------------------------------------------------------------------+
895 + | Copyright (c) 2009 Melanie Rhianna Lewis |
896 + +----------------------------------------------------------------------+
897 + | This source file is subject to version 3.0 of the PHP license, |
898 + | that is bundled with this package in the file LICENSE, and is |
899 + | available through the world-wide-web at the following url: |
900 + | http://www.php.net/license/3_0.txt. |
901 + | If you did not receive a copy of the PHP license and are unable to |
902 + | obtain it through the world-wide-web, please send a note to |
903 + | license@php.net so we can mail you a copy immediately. |
904 + +----------------------------------------------------------------------+
905 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
906 + +----------------------------------------------------------------------+
909 +#ifdef HAVE_CONFIG_H
915 +#include "php_dio.h"
916 +#include "php_dio_common.h"
918 +/* {{{ dio_init_stream_data
919 + * Initialises the command parts of the stream data.
921 +void dio_init_stream_data(php_dio_stream_data *data) {
922 + data->stream_type = DIO_STREAM_TYPE_NONE;
923 + data->end_of_file = 0;
924 +#ifdef DIO_HAS_FILEPERMS
925 + data->has_perms = 0;
929 + data->is_blocking = 1;
930 + data->has_timeout = 0;
931 + data->timeout_sec = 0;
932 + data->timeout_usec = 0;
933 + data->timed_out = 0;
935 + /* Serial options */
936 + data->data_rate = 9600;
937 + data->data_bits = 8;
938 + data->stop_bits = 1;
940 + data->flow_control = 1;
941 + data->canonical = 1;
945 +/* {{{ dio_convert_to_long
946 + * Returns as a long, the value of the zval regardless of its type.
948 +long dio_convert_to_long(zval *val) {
952 + ALLOC_INIT_ZVAL(copyval);
954 + convert_to_long(copyval);
955 + longval = Z_LVAL_P(copyval);
956 + zval_ptr_dtor(©val);
962 +/* {{{ dio_assoc_array_get_basic_options
963 + * Retrieves the basic open option values from an associative array
965 +void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data TSRMLS_DC) {
966 +#if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
968 + HashTable *opthash;
970 + opthash = HASH_OF(options);
973 +#ifdef DIO_HAS_FILEPERMS
974 + /* This is the file mode flags used by open(). */
975 + if (zend_hash_find(opthash, "perms", sizeof("perms"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
976 + data->perms = (int)dio_convert_to_long(*tmpzval);
977 + data->has_perms = 1;
982 + /* This sets the underlying stream to be blocking/non
983 + block (i.e. O_NONBLOCK) */
984 + if (zend_hash_find(opthash, "is_blocking", sizeof("is_blocking"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
985 + data->is_blocking = dio_convert_to_long(*tmpzval) ? 1 : 0;
988 + /* This is the timeout value for reads in seconds. Only one of
989 + timeout_secs or timeout_usecs need be defined to define a timeout. */
990 + if (zend_hash_find(opthash, "timeout_secs", sizeof("timeout_secs"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
991 + data->timeout_sec = dio_convert_to_long(*tmpzval);
994 + /* This is the timeout value for reads in microseconds. Only one of
995 + timeout_secs or timeout_usecs need be defined to define a timeout. */
996 + if (zend_hash_find(opthash, "timeout_usecs", sizeof("timeout_usecs"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
997 + data->timeout_usec = dio_convert_to_long(*tmpzval);
1000 + data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
1005 +/* {{{ dio_assoc_array_get_serial_options
1006 + * Retrieves the serial open option values from an associative array
1008 +void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data TSRMLS_DC) {
1010 + HashTable *opthash;
1012 + opthash = HASH_OF(options);
1014 + if (zend_hash_find(opthash, "data_rate", sizeof("data_rate"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1015 + data->data_rate = dio_convert_to_long(*tmpzval);
1018 + if (zend_hash_find(opthash, "data_bits", sizeof("data_bits"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1019 + data->data_bits = (int)dio_convert_to_long(*tmpzval);
1022 + if (zend_hash_find(opthash, "stop_bits", sizeof("stop_bits"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1023 + data->stop_bits = (int)dio_convert_to_long(*tmpzval);
1026 + if (zend_hash_find(opthash, "parity", sizeof("parity"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1027 + data->parity = (int)dio_convert_to_long(*tmpzval);
1030 + if (zend_hash_find(opthash, "flow_control", sizeof("flow_control"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1031 + data->flow_control = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
1034 + if (zend_hash_find(opthash, "is_canonical", sizeof("is_canonical"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1035 + data->canonical = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
1040 +/* {{{ dio_stream_context_get_raw_options
1041 + * Extracts the option values for dio.raw mode from a context
1043 +void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC) {
1044 +#if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
1048 +#ifdef DIO_HAS_FILEPERMS
1049 + /* This is the file mode flags used by open(). */
1050 + if (php_stream_context_get_option(context, "dio", "perms", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1051 + data->perms = (int)dio_convert_to_long(*tmpzval);
1052 + data->has_perms = 1;
1056 +#ifdef DIO_NONBLOCK
1057 + /* This sets the underlying stream to be blocking/non
1058 + block (i.e. O_NONBLOCK) */
1059 + if (php_stream_context_get_option(context, "dio", "is_blocking", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1060 + data->is_blocking = dio_convert_to_long(*tmpzval) ? 1 : 0;
1063 + /* This is the timeout value for reads in seconds. Only one of
1064 + timeout_secs or timeout_usecs need be defined to define a timeout. */
1065 + if (php_stream_context_get_option(context, "dio", "timeout_secs", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1066 + data->timeout_sec = dio_convert_to_long(*tmpzval);
1069 + /* This is the timeout value for reads in microseconds. Only one of
1070 + timeout_secs or timeout_usecs need be defined to define a timeout. */
1071 + if (php_stream_context_get_option(context, "dio", "timeout_usecs", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1072 + data->timeout_usec = dio_convert_to_long(*tmpzval);
1075 + data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
1080 +/* {{{ dio_stream_context_get_serial_options
1081 + * Extracts the option values for dio.serial mode from a context
1083 +void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC) {
1086 + if (php_stream_context_get_option(context, "dio", "data_rate", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1087 + data->data_rate = dio_convert_to_long(*tmpzval);
1090 + if (php_stream_context_get_option(context, "dio", "data_bits", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1091 + data->data_bits = (int)dio_convert_to_long(*tmpzval);
1094 + if (php_stream_context_get_option(context, "dio", "stop_bits", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1095 + data->stop_bits = (int)dio_convert_to_long(*tmpzval);
1098 + if (php_stream_context_get_option(context, "dio", "parity", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1099 + data->parity = (int)dio_convert_to_long(*tmpzval);
1102 + if (php_stream_context_get_option(context, "dio", "flow_control", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1103 + data->flow_control = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
1106 + if (php_stream_context_get_option(context, "dio", "is_canonical", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1107 + data->canonical = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
1113 + * Local variables:
1114 + * c-basic-offset: 4
1117 + * vim600: fdm=marker
1118 + * vim: sw=4 ts=4 noet
1122 +++ b/ext/dio/dio_posix.c
1125 + +----------------------------------------------------------------------+
1127 + +----------------------------------------------------------------------+
1128 + | Copyright (c) 2009 Melanie Rhianna Lewis |
1129 + +----------------------------------------------------------------------+
1130 + | This source file is subject to version 3.0 of the PHP license, |
1131 + | that is bundled with this package in the file LICENSE, and is |
1132 + | available through the world-wide-web at the following url: |
1133 + | http://www.php.net/license/3_0.txt. |
1134 + | If you did not receive a copy of the PHP license and are unable to |
1135 + | obtain it through the world-wide-web, please send a note to |
1136 + | license@php.net so we can mail you a copy immediately. |
1137 + +----------------------------------------------------------------------+
1138 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
1139 + +----------------------------------------------------------------------+
1142 +#ifdef HAVE_CONFIG_H
1143 +#include "config.h"
1148 +#include "php_dio_common.h"
1150 +/* {{{ dio_stream_mode_to_flags
1151 + * Convert an fopen() mode string to open() flags
1153 +static int dio_stream_mode_to_flags(const char *mode) {
1154 + int flags = 0, ch = 0, bin = 1;
1156 + switch(mode[ch++]) {
1161 + flags = O_TRUNC | O_CREAT;
1164 + flags = O_APPEND | O_CREAT;
1167 + flags = O_EXCL | O_CREAT;
1171 + if (mode[ch] != '+') {
1172 + bin = (mode[ch++] == 'b');
1175 + if (mode[ch] == '+') {
1177 + } else if (flags) {
1178 + flags |= O_WRONLY;
1180 + flags |= O_RDONLY;
1183 +#if defined(_O_TEXT) && defined(O_BINARY)
1185 + flags |= O_BINARY;
1195 +/* {{{ dio_data_rate_to_define
1196 + * Converts a numeric data rate to a termios define
1198 +static int dio_data_rate_to_define(long rate, speed_t *def) {
1278 +/* {{{ dio_data_bits_to_define
1279 + * Converts a number of data bits to a termios define
1281 +static int dio_data_bits_to_define(int data_bits, int *def) {
1284 + switch (data_bits) {
1306 +/* {{{ dio_stop_bits_to_define
1307 + * Converts a number of stop bits to a termios define
1309 +static int dio_stop_bits_to_define(int stop_bits, int *def) {
1312 + switch (stop_bits) {
1328 +/* {{{ dio_parity_to_define
1329 + * Converts a parity type to a termios define
1331 +static int dio_parity_to_define(int parity, int *def) {
1339 + val = PARENB | PARODD;
1353 +/* {{{ dio_create_stream_data
1354 + * Creates an initialised stream data structure. Free with efree().
1356 +php_dio_stream_data * dio_create_stream_data(void) {
1357 + php_dio_posix_stream_data * data = emalloc(sizeof(php_dio_posix_stream_data));
1358 + dio_init_stream_data(&(data->common));
1362 + return (php_dio_stream_data *)data;
1366 +/* {{{ dio_common_write
1367 + * Writes count chars from the buffer to the stream described by the stream data.
1369 +size_t dio_common_write(php_dio_stream_data *data, const char *buf, size_t count) {
1372 + /* Blocking writes can be interrupted by signals etc. If
1373 + * interrupted try again. Not sure about non-blocking
1374 + * writes but it doesn't hurt to check. */
1376 + ret = write(((php_dio_posix_stream_data*)data)->fd, buf, count);
1380 + } while (errno == EINTR);
1385 +#ifdef DIO_NONBLOCK
1386 +/* {{{ dio_timeval_subtract
1387 + * Calculates the difference between two timevals returning the result in the
1388 + * structure pointed to by diffptr. Returns -1 as error if late time is
1389 + * earlier than early time.
1391 +static int dio_timeval_subtract(struct timeval *late, struct timeval *early, struct timeval *diff) {
1392 + struct timeval *tmp;
1394 + /* Handle negatives */
1395 + if (late->tv_sec < early->tv_sec) {
1399 + if ((late->tv_sec == early->tv_sec) && (late->tv_usec < early->tv_usec)) {
1403 + /* Handle any carry. If later usec is smaller than earlier usec simple
1404 + * subtraction will result in negative value. Since usec has a maximum
1405 + * of one second by adding another second before the subtraction the
1406 + * result will always be positive. */
1407 + if (late->tv_usec < early->tv_usec) {
1408 + late->tv_usec += 1000000;
1412 + /* Once adjusted can just subtract values. */
1413 + diff->tv_sec = late->tv_sec - early->tv_sec;
1414 + diff->tv_usec = late->tv_usec - early->tv_usec;
1420 +/* {{{ dio_common_read
1421 + * Reads count chars to the buffer to the stream described by the stream data.
1423 +size_t dio_common_read(php_dio_stream_data *data, const char *buf, size_t count) {
1424 + int fd = ((php_dio_posix_stream_data*)data)->fd;
1425 + size_t ret, total = 0;
1426 + char *ptr = (char*)buf;
1428 + struct timeval timeout, timeouttmp, before, after, diff;
1431 + if (!data->has_timeout) {
1432 + /* Blocking reads can be interrupted by signals etc. If
1433 + * interrupted try again. Not sure about non-blocking
1434 + * reads but it doesn't hurt to check. */
1436 + ret = read(fd, (char*)ptr, count);
1439 + } else if (!ret) {
1440 + data->end_of_file = 1;
1442 + } while ((errno == EINTR) && !data->end_of_file);
1445 +#ifdef DIO_NONBLOCK
1447 + /* Clear timed out flag */
1448 + data->timed_out = 0;
1450 + /* The initial timeout value */
1451 + timeout.tv_sec = data->timeout_sec;
1452 + timeout.tv_usec = data->timeout_usec;
1455 + /* The semantics of select() are that you cannot guarantee
1456 + * that the timeval structure passed in has not been changed by
1457 + * the select call. So you keep a copy. */
1458 + timeouttmp = timeout;
1460 + /* The time before we wait for data. */
1461 + (void) gettimeofday(&before, NULL);
1463 + /* Wait for an event on our file descriptor. */
1465 + FD_SET(fd, &rfds);
1467 + ret = select(fd + 1, &rfds, NULL, NULL, &timeouttmp);
1469 + if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN)) {
1473 + /* We have data to read. */
1474 + if ((ret > 0) && FD_ISSET(fd, &rfds)) {
1475 + ret = read(fd, ptr, count);
1476 + /* Another error */
1477 + if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN)) {
1482 + /* Got data, add it to the buffer. */
1486 + } else if (!ret) {
1487 + /* This should never happen since how can we have
1488 + * data to read at an end of file, but still
1489 + * just in case! */
1490 + data->end_of_file = 1;
1495 + /* If not timed out and not end of file and not all data read
1496 + * calculate how long it took us and loop if we still have time
1497 + * out time left. */
1499 + (void) gettimeofday(&after, NULL);
1501 + /* Diff the timevals */
1502 + (void) dio_timeval_subtract(&after, &before, &diff);
1504 + /* Now adjust the timeout. */
1505 + if (!dio_timeval_subtract(&timeout, &diff, &timeout)) {
1506 + /* If it errors we've run out of time. */
1507 + data->timed_out = 1;
1509 + } else if (!timeout.tv_sec && !(timeout.tv_usec / 1000)) {
1510 + /* Check for rounding issues (millisecond accuracy) */
1511 + data->timed_out = 1;
1515 + } while (count); /* Until time out or end of file or all data read. */
1523 +/* {{{ php_dio_stream_data
1524 + * Closes the php_stream.
1526 +int dio_common_close(php_dio_stream_data *data) {
1527 + if (close(((php_dio_posix_stream_data*)data)->fd) < 0) {
1535 +/* {{{ dio_common_set_option
1536 + * Sets/gets stream options
1538 +int dio_common_set_option(php_dio_stream_data *data, int option, int value, void *ptrparam) {
1539 + int fd = ((php_dio_posix_stream_data*)data)->fd;
1540 + int old_is_blocking;
1544 +#ifdef DIO_NONBLOCK
1545 + case PHP_STREAM_OPTION_READ_TIMEOUT:
1547 + struct timeval *tv = (struct timeval*)ptrparam;
1549 + flags = fcntl(fd, F_GETFL, 0);
1551 + /* A timeout of zero seconds and zero microseconds disables
1552 + any existing timeout. */
1553 + if (tv->tv_sec || tv->tv_usec) {
1554 + data->timeout_sec = tv->tv_sec;
1555 + data->timeout_usec = tv->tv_usec;
1556 + data->has_timeout = -1;
1557 + (void) fcntl(fd, F_SETFL, flags & ~DIO_NONBLOCK);
1559 + data->timeout_sec = 0;
1560 + data->timeout_usec = 0;
1561 + data->has_timeout = 0;
1562 + data->timed_out = 0;
1563 + (void) fcntl(fd, F_SETFL, flags | DIO_NONBLOCK);
1566 + return PHP_STREAM_OPTION_RETURN_OK;
1568 + return PHP_STREAM_OPTION_RETURN_ERR;
1571 + case PHP_STREAM_OPTION_BLOCKING:
1572 + flags = fcntl(fd, F_GETFL, 0);
1574 + flags &= ~DIO_NONBLOCK;
1576 + flags |= DIO_NONBLOCK;
1578 + (void) fcntl(fd, F_SETFL, flags);
1580 + old_is_blocking = data->is_blocking;
1581 + data->is_blocking = value;
1582 + return old_is_blocking ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
1583 +#endif /* O_NONBLOCK */
1593 +/* {{{ dio_raw_open_stream
1594 + * Opens the underlying stream.
1596 +int dio_raw_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
1597 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1598 + pdata->flags = dio_stream_mode_to_flags(mode);
1600 +#ifdef DIO_NONBLOCK
1601 + if (!data->is_blocking || data->has_timeout) {
1602 + pdata->flags |= DIO_NONBLOCK;
1606 + /* Open the file and handle any errors. */
1607 +#ifdef DIO_HAS_FILEPERMS
1608 + if (data->has_perms) {
1609 + pdata->fd = open(filename, pdata->flags, (mode_t)data->perms);
1611 + pdata->fd = open(filename, pdata->flags);
1614 + pdata->fd = open(filename, pdata->flags);
1617 + if (pdata->fd < 0) {
1620 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "File exists!");
1631 +/* {{{ dio_serial_init
1632 + * Initialises the serial settings storing the original settings before hand.
1634 +static int dio_serial_init(php_dio_stream_data *data TSRMLS_DC) {
1635 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1636 + int ret = 0, data_bits_def, stop_bits_def, parity_def;
1637 + struct termios tio;
1640 + if (!dio_data_rate_to_define(data->data_rate, &rate_def)) {
1641 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_rate value (%ld)", data->data_rate);
1645 + if (!dio_data_bits_to_define(data->data_bits, &data_bits_def)) {
1646 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_bits value (%d)", data->data_bits);
1650 + if (!dio_stop_bits_to_define(data->stop_bits, &stop_bits_def)) {
1651 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid stop_bits value (%d)", data->stop_bits);
1655 + if (!dio_parity_to_define(data->parity, &parity_def)) {
1656 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid parity value (%d)", data->parity);
1660 + ret = tcgetattr(pdata->fd, &(pdata->oldtio));
1662 + if ((errno == ENOTTY) || (errno == ENODEV)) {
1663 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a serial port or terminal!");
1668 + ret = tcgetattr(pdata->fd, &tio);
1673 + if (data->canonical) {
1674 + tio.c_iflag = IGNPAR | ICRNL;
1676 + tio.c_lflag = ICANON;
1681 + cfsetispeed(&tio, rate_def);
1682 + cfsetospeed(&tio, rate_def);
1684 + tio.c_cflag &= ~CSIZE;
1685 + tio.c_cflag |= data_bits_def;
1686 + tio.c_cflag &= ~CSTOPB;
1687 + tio.c_cflag |= stop_bits_def;
1688 + tio.c_cflag &= ~(PARENB|PARODD);
1689 + tio.c_cflag |= parity_def;
1692 + tio.c_cflag &= ~(CLOCAL | CRTSCTS);
1694 + tio.c_cflag &= ~CLOCAL;
1696 + if (!data->flow_control) {
1697 + tio.c_cflag |= CLOCAL;
1700 + tio.c_cflag |= CRTSCTS;
1704 + ret = tcsetattr(pdata->fd, TCSANOW, &tio);
1713 +/* {{{ dio_serial_uninit
1714 + * Restores the serial settings back to their original state.
1716 +int dio_serial_uninit(php_dio_stream_data *data) {
1717 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1721 + ret = tcsetattr(pdata->fd, TCSANOW, &(pdata->oldtio));
1722 + } while ((ret < 0) && (errno == EINTR));
1728 +/* {{{ dio_serial_flush
1729 + * Purges the serial buffers of data.
1731 +int dio_serial_purge(php_dio_stream_data *data) {
1732 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1735 + if ((pdata->flags & O_RDWR) == O_RDWR) {
1736 + ret = tcflush(pdata->fd, TCIOFLUSH);
1737 + } else if ((pdata->flags & O_WRONLY) == O_WRONLY) {
1738 + ret = tcflush(pdata->fd, TCOFLUSH);
1739 + } else if ((pdata->flags & O_RDONLY) == O_RDONLY) {
1740 + ret = tcflush(pdata->fd, TCIFLUSH);
1751 +/* {{{ dio_serial_open_stream
1752 + * Opens the underlying stream.
1754 +int dio_serial_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
1755 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1758 + /* We don't want a controlling TTY */
1759 + pdata->flags |= O_NOCTTY;
1762 + if (!dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
1766 + if (!dio_serial_init(data TSRMLS_CC)) {
1776 + * Local variables:
1777 + * c-basic-offset: 4
1780 + * vim600: fdm=marker
1781 + * vim: sw=4 ts=4 noet
1784 +++ b/ext/dio/dio_stream_wrappers.c
1787 + +----------------------------------------------------------------------+
1789 + +----------------------------------------------------------------------+
1790 + | Copyright (c) 2009 Melanie Rhianna Lewis |
1791 + +----------------------------------------------------------------------+
1792 + | This source file is subject to version 3.0 of the PHP license, |
1793 + | that is bundled with this package in the file LICENSE, and is |
1794 + | available through the world-wide-web at the following url: |
1795 + | http://www.php.net/license/3_0.txt. |
1796 + | If you did not receive a copy of the PHP license and are unable to |
1797 + | obtain it through the world-wide-web, please send a note to |
1798 + | license@php.net so we can mail you a copy immediately. |
1799 + +----------------------------------------------------------------------+
1800 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
1801 + +----------------------------------------------------------------------+
1804 +#ifdef HAVE_CONFIG_H
1805 +#include "config.h"
1809 +#include "ext/standard/url.h"
1811 +#include "php_dio.h"
1812 +#include "php_dio_common.h"
1813 +#include "php_dio_stream_wrappers.h"
1816 + +----------------------------------------------------------------------+
1817 + | Raw stream handling |
1818 + +----------------------------------------------------------------------+
1821 +/* {{{ dio_stream_write
1822 + * Write to the stream
1824 +static size_t dio_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
1826 + return dio_common_write((php_dio_stream_data*)stream->abstract, buf, count);
1830 +/* {{{ dio_stream_read
1831 + * Read from the stream
1833 +static size_t dio_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
1835 + php_dio_stream_data* data = (php_dio_stream_data*)stream->abstract;
1836 + size_t bytes = dio_common_read(data, buf, count);
1837 + stream->eof = data->end_of_file;
1843 +/* {{{ dio_stream_flush
1844 + * Flush the stream. For raw streams this does nothing.
1846 +static int dio_stream_flush(php_stream *stream TSRMLS_DC)
1852 +/* {{{ dio_stream_close
1853 + * Close the stream
1855 +static int dio_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
1857 + php_dio_stream_data *abstract = (php_dio_stream_data*)stream->abstract;
1859 + if (!dio_common_close(abstract)) {
1868 +/* {{{ dio_stream_set_option
1869 + * Set the stream options.
1871 +static int dio_stream_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC)
1873 + php_dio_stream_data *abstract = (php_dio_stream_data*)stream->abstract;
1876 + case PHP_STREAM_OPTION_META_DATA_API:
1877 +#ifdef DIO_NONBLOCK
1878 + add_assoc_bool((zval *)ptrparam, "timed_out", abstract->timed_out);
1879 + add_assoc_bool((zval *)ptrparam, "blocked", abstract->is_blocking);
1881 + add_assoc_bool((zval *)ptrparam, "eof", stream->eof);
1882 + return PHP_STREAM_OPTION_RETURN_OK;
1884 +#if PHP_MAJOR_VERSION >= 5
1885 + case PHP_STREAM_OPTION_CHECK_LIVENESS:
1886 + stream->eof = abstract->end_of_file;
1887 + return PHP_STREAM_OPTION_RETURN_OK;
1888 +#endif /* PHP_MAJOR_VERSION >= 5 */
1894 + return dio_common_set_option(abstract, option, value, ptrparam);
1898 +php_stream_ops dio_raw_stream_ops = {
1907 + dio_stream_set_option,
1910 +/* {{{ dio_raw_fopen_wrapper
1911 + * fopen for the dio.raw stream.
1913 +static php_stream *dio_raw_fopen_wrapper(php_stream_wrapper *wrapper,
1914 + char *path, char *mode,
1915 + int options, char **opened_path,
1916 + php_stream_context *context STREAMS_DC TSRMLS_DC) {
1917 + php_dio_stream_data *data;
1918 + php_stream *stream;
1921 + /* Check it was actually for us (not a corrupted function pointer
1923 + if (strncmp(path, DIO_RAW_STREAM_PROTOCOL, sizeof(DIO_RAW_STREAM_PROTOCOL) - 1)) {
1927 + /* Get the actually file system name/path. */
1928 + filename = path + sizeof(DIO_RAW_STREAM_PROTOCOL) - 1;
1930 + /* Check we can actually access it. */
1931 + if (php_check_open_basedir(filename TSRMLS_CC) || DIO_SAFE_MODE_CHECK(filename, mode)) {
1935 + data = dio_create_stream_data();
1936 + data->stream_type = DIO_STREAM_TYPE_RAW;
1938 + /* Parse the context. */
1940 + dio_stream_context_get_basic_options(context, data TSRMLS_CC);
1943 + /* Try and open a raw stream. */
1944 + if (!dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
1948 + /* Create a PHP stream based on raw stream */
1949 + stream = php_stream_alloc(&dio_raw_stream_ops, data, 0, mode);
1951 + (void) dio_common_close(data);
1959 +static php_stream_wrapper_ops dio_raw_stream_wops = {
1960 + dio_raw_fopen_wrapper,
1961 + NULL, /* stream_close */
1963 + NULL, /* stat_url */
1964 + NULL, /* opendir */
1965 + DIO_RAW_STREAM_NAME
1968 +php_stream_wrapper php_dio_raw_stream_wrapper = {
1969 + &dio_raw_stream_wops,
1974 +/* {{{ proto dio_raw(string filename, string mode[, array options])
1975 + * Opens a raw direct IO stream.
1977 +PHP_FUNCTION(dio_raw) {
1978 + zval *options = NULL;
1979 + php_dio_stream_data *data;
1980 + php_stream *stream;
1987 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|z", &filename, &filename_len, &mode, &mode_len, &options) == FAILURE) {
1991 + /* Check the third argument is an array. */
1992 + if (options && (Z_TYPE_P(options) != IS_ARRAY)) {
1996 + /* Check we can actually access the file. */
1997 + if (php_check_open_basedir(filename TSRMLS_CC) || DIO_SAFE_MODE_CHECK(filename, mode)) {
2001 + data = dio_create_stream_data();
2002 + data->stream_type = DIO_STREAM_TYPE_RAW;
2005 + dio_assoc_array_get_basic_options(options, data TSRMLS_CC);
2008 + /* Try and open a raw stream. */
2009 + if (dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
2010 + stream = php_stream_alloc(&dio_raw_stream_ops, data, 0, mode);
2012 + (void) dio_common_close(data);
2018 + php_stream_to_zval(stream, return_value);
2023 + +----------------------------------------------------------------------+
2024 + | Serial stream handling |
2025 + +----------------------------------------------------------------------+
2028 +/* {{{ dio_stream_flush
2029 + * Flush the stream. If the stream is read only, it flushes the read
2030 + * stream, if it is write only it flushes the write, otherwise it flushes
2033 +static int dio_serial_stream_flush(php_stream *stream TSRMLS_DC)
2035 + return dio_serial_purge((php_dio_stream_data*)stream->abstract);
2039 +/* {{{ dio_stream_close
2040 + * Close the stream. Restores the serial settings to their value before
2041 + * the stream was open.
2043 +static int dio_serial_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
2045 + php_dio_stream_data *abstract = (php_dio_stream_data*)stream->abstract;
2047 + if (!dio_serial_uninit(abstract)) {
2051 + if (!dio_common_close(abstract)) {
2060 +php_stream_ops dio_serial_stream_ops = {
2063 + dio_serial_stream_close,
2064 + dio_serial_stream_flush,
2069 + dio_stream_set_option,
2072 +/* {{{ dio_raw_fopen_wrapper
2073 + * fopen for the dio.raw stream.
2075 +static php_stream *dio_serial_fopen_wrapper(php_stream_wrapper *wrapper,
2076 + char *path, char *mode,
2077 + int options, char **opened_path,
2078 + php_stream_context *context STREAMS_DC TSRMLS_DC) {
2079 + php_dio_stream_data *data;
2080 + php_stream *stream;
2083 + /* Check it was actually for us (not a corrupted function pointer
2085 + if (strncmp(path, DIO_SERIAL_STREAM_PROTOCOL, sizeof(DIO_SERIAL_STREAM_PROTOCOL) - 1)) {
2089 + /* Get the actually file system name/path. */
2090 + filename = path + sizeof(DIO_SERIAL_STREAM_PROTOCOL) - 1;
2092 + /* Check we can actually access it. */
2093 + if (php_check_open_basedir(filename TSRMLS_CC) || DIO_SAFE_MODE_CHECK(filename, mode)) {
2097 + data = dio_create_stream_data();
2098 + data->stream_type = DIO_STREAM_TYPE_SERIAL;
2100 + /* Parse the context. */
2102 + dio_stream_context_get_basic_options(context, data TSRMLS_CC);
2103 + dio_stream_context_get_serial_options(context, data TSRMLS_CC);
2106 + /* Try and open a serial stream. */
2107 + if (!dio_serial_open_stream(filename, mode, data TSRMLS_CC)) {
2111 + stream = php_stream_alloc(&dio_serial_stream_ops, data, 0, mode);
2120 +static php_stream_wrapper_ops dio_serial_stream_wops = {
2121 + dio_serial_fopen_wrapper,
2122 + NULL, /* stream_close */
2124 + NULL, /* stat_url */
2125 + NULL, /* opendir */
2126 + DIO_SERIAL_STREAM_NAME
2129 +php_stream_wrapper php_dio_serial_stream_wrapper = {
2130 + &dio_serial_stream_wops,
2135 +/* {{{ proto dio_serial(string filename, string mode[, array options])
2136 + * Opens a serial direct IO stream.
2138 +PHP_FUNCTION(dio_serial) {
2139 + zval *options = NULL;
2140 + php_dio_stream_data *data;
2141 + php_stream *stream;
2148 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|z", &filename, &filename_len, &mode, &mode_len, &options) == FAILURE) {
2152 + /* Check the third argument is an array. */
2153 + if (options && (Z_TYPE_P(options) != IS_ARRAY)) {
2154 + php_error_docref(NULL TSRMLS_CC, E_WARNING,"dio_serial, the third argument should be an array of options");
2158 + /* Check we can actually access the file. */
2159 + if (php_check_open_basedir(filename TSRMLS_CC) || DIO_SAFE_MODE_CHECK(filename, mode)) {
2163 + data = dio_create_stream_data();
2164 + data->stream_type = DIO_STREAM_TYPE_SERIAL;
2167 + dio_assoc_array_get_basic_options(options, data TSRMLS_CC);
2168 + dio_assoc_array_get_serial_options(options, data TSRMLS_CC);
2171 + /* Try and open a serial stream. */
2172 + if (dio_serial_open_stream(filename, mode, data TSRMLS_CC)) {
2173 + stream = php_stream_alloc(&dio_serial_stream_ops, data, 0, mode);
2180 + php_stream_to_zval(stream, return_value);
2185 + * Local variables:
2186 + * c-basic-offset: 4
2189 + * vim600: fdm=marker
2190 + * vim: sw=4 ts=4 noet
2193 +++ b/ext/dio/dio_win32.c
2196 + +----------------------------------------------------------------------+
2198 + +----------------------------------------------------------------------+
2199 + | Copyright (c) 2009 Melanie Rhianna Lewis |
2200 + +----------------------------------------------------------------------+
2201 + | This source file is subject to version 3.0 of the PHP license, |
2202 + | that is bundled with this package in the file LICENSE, and is |
2203 + | available through the world-wide-web at the following url: |
2204 + | http://www.php.net/license/3_0.txt. |
2205 + | If you did not receive a copy of the PHP license and are unable to |
2206 + | obtain it through the world-wide-web, please send a note to |
2207 + | license@php.net so we can mail you a copy immediately. |
2208 + +----------------------------------------------------------------------+
2209 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
2210 + +----------------------------------------------------------------------+
2213 +#ifdef HAVE_CONFIG_H
2214 +#include "config.h"
2219 +#include "php_dio_common.h"
2221 +/* {{{ dio_data_rate_to_define
2222 + * Converts a numeric data rate to a termios define
2224 +static int dio_data_rate_to_define(long rate, DWORD *def) {
2250 + *def = (DWORD)rate;
2256 +/* {{{ dio_data_bits_to_define
2257 + * Converts a number of data bits to a termios define
2259 +static int dio_data_bits_to_define(int data_bits, DWORD *def) {
2260 + switch (data_bits) {
2271 + *def = (DWORD)data_bits;
2276 +/* {{{ dio_stop_bits_to_define
2277 + * Converts a number of stop bits to a termios define
2279 +static int dio_stop_bits_to_define(int stop_bits, DWORD *def) {
2282 + switch (stop_bits) {
2301 +/* {{{ dio_parity_to_define
2302 + * Converts a parity type to a termios define
2304 +static int dio_parity_to_define(int parity, DWORD *def) {
2314 + *def = (DWORD)parity;
2319 +/* {{{ dio_create_stream_data
2320 + * Creates an initialised stream data structure. Free with efree().
2322 +php_dio_stream_data * dio_create_stream_data(void) {
2323 + php_dio_win32_stream_data * data = emalloc(sizeof(php_dio_win32_stream_data));
2324 + memset(data, 0, sizeof(php_dio_win32_stream_data));
2325 + dio_init_stream_data(&(data->common));
2326 + data->handle = INVALID_HANDLE_VALUE;
2327 + data->desired_access = 0;
2328 + data->creation_disposition = 0;
2329 + data->olddcb.DCBlength = sizeof(DCB);
2331 + return (php_dio_stream_data *)data;
2335 +/* {{{ dio_common_write
2336 + * Writes count chars from the buffer to the stream described by the stream data.
2338 +size_t dio_common_write(php_dio_stream_data *data, const char *buf, size_t count) {
2339 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2342 + if (WriteFile(wdata->handle, buf, (DWORD)count, &total, NULL)) {
2343 + return (size_t)total;
2350 +/* {{{ dio_buffer_read
2351 + * Reads any available chars from the canonical buffer.
2353 +static size_t dio_buffer_read(php_dio_win32_stream_data *wdata, const char *buf, size_t count) {
2354 + php_dio_win32_canon_data *canon_data = wdata->canon_data;
2357 + /* Read always follows write. I.e. if read ptr > write ptr buffer has
2358 + wrapped and so we need to copy two blocks of data. */
2359 + if (canon_data->read_pos > canon_data->write_pos) {
2361 + /* Check we actually need to copy both blocks */
2362 + if ((canon_data->size - canon_data->read_pos) > count) {
2364 + /* No we don't. Just copy as much as we were asked for. */
2365 + memcpy((char*)buf,
2366 + &(canon_data->buf[canon_data->read_pos]),
2368 + /* Update the read pointer. */
2369 + canon_data->read_pos += count;
2371 + /* Return the amount read. */
2375 + /* We need to copy both blocks so copy data up to the end of
2377 + total = canon_data->size - canon_data->read_pos;
2378 + memcpy((char*)buf,
2379 + &(canon_data->buf[canon_data->read_pos]),
2381 + canon_data->read_pos = 0;
2384 + /* Now copy the data from the start of the buffer either up
2385 + count or the number of bytes in the buffer. */
2387 + if (canon_data->write_pos > count) {
2388 + memcpy((char*)buf, canon_data->buf, count);
2389 + canon_data->read_pos = count;
2394 + memcpy((char*)buf, canon_data->buf, canon_data->write_pos);
2395 + canon_data->read_pos = canon_data->write_pos;
2396 + total += canon_data->write_pos;
2402 + /* Else if write follows read. This is a simpler case. We just copy
2403 + either all the data buffered or count, which ever is smaller. */
2404 + } else if (canon_data->write_pos > canon_data->read_pos) {
2405 + if ((canon_data->write_pos - canon_data->read_pos) > count) {
2406 + memcpy((char*)buf,
2407 + &(canon_data->buf[canon_data->read_pos]),
2409 + canon_data->read_pos += count;
2413 + total = canon_data->write_pos - canon_data->read_pos;
2414 + memcpy((char*)buf,
2415 + &(canon_data->buf[canon_data->read_pos]),
2417 + canon_data->read_pos += total;
2423 + /* Else we need to read more data from the data port. */
2427 +/* {{{ dio_com_read
2428 + * Read chars from the data port.
2430 +static size_t dio_com_read(php_dio_stream_data *data, const char *buf, size_t count) {
2431 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2432 + DWORD err, total = 0;
2434 + if (ReadFile(wdata->handle, (void*)buf, (DWORD)count, &total, NULL)) {
2437 + return (size_t)total;
2440 + data->end_of_file = 1;
2443 + if (!data->end_of_file) {
2444 + err = GetLastError();
2446 + if (ERROR_HANDLE_EOF == err) {
2447 + data->end_of_file = 1;
2454 +/* {{{ dio_canonical_read
2455 + * Reads chars from the input stream until the internal buffer is full or a new
2456 + * line is reached.
2458 +static size_t dio_canonical_read(php_dio_win32_stream_data *wdata, const char *buf, size_t count) {
2459 + php_dio_win32_canon_data *canon_data = wdata->canon_data;
2463 + /* See if there's any buffered data and copy it. */
2464 + total = dio_buffer_read(wdata, buf, count);
2469 + /* Need to read more data from the data port. Buffer should be empty(er)
2472 + /* Is the buffer full? */
2473 + if (((canon_data->write_pos + 1) % canon_data->size) ==
2474 + canon_data->read_pos) {
2478 + /* Read a byte from the input checking for EOF. */
2479 + if (!dio_com_read((php_dio_stream_data*)wdata, &ch, 1)) {
2483 + /* Translate CR to newlines (same as ICRNL in POSIX) */
2484 + ch = (ch != '\r') ? ch : '\n';
2486 + /* We read a character! So buffer it. */
2487 + canon_data->buf[canon_data->write_pos++] = ch;
2488 + if (canon_data->write_pos >= canon_data->size) {
2489 + canon_data->write_pos = 0;
2492 + /* End of line/input (^D)? */
2493 + } while ((ch != '\n') && (ch != 0x04));
2495 + return dio_buffer_read(wdata, buf, count);
2499 +/* {{{ dio_common_read
2500 + * Reads count chars to the buffer to the stream described by the stream data.
2502 +size_t dio_common_read(php_dio_stream_data *data, const char *buf, size_t count) {
2504 + /* You ask for no bytes you'll get none :-) */
2509 + if (data->canonical) {
2510 + return dio_canonical_read((php_dio_win32_stream_data*)data, buf, count);
2512 + return dio_com_read(data, buf, count);
2517 +/* {{{ php_dio_stream_data
2518 + * Closes the php_stream.
2520 +int dio_common_close(php_dio_stream_data *data) {
2521 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2523 + if (data->canonical) {
2524 + efree(wdata->canon_data);
2527 + if (!CloseHandle(wdata->handle)) {
2535 +/* {{{ dio_common_set_option
2536 + * Sets/gets stream options
2538 +int dio_common_set_option(php_dio_stream_data *data, int option, int value, void *ptrparam) {
2539 + COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 };
2540 + int old_is_blocking = 0;
2542 + /* Can't do timeouts or non blocking with raw windows streams :-( */
2543 + if (DIO_STREAM_TYPE_SERIAL == data->stream_type) {
2545 + case PHP_STREAM_OPTION_BLOCKING:
2546 + old_is_blocking = data->is_blocking;
2547 + data->is_blocking = value ? 1 : 0;
2549 + /* Only change values if we need to change them. */
2550 + if (data->is_blocking != old_is_blocking) {
2551 + /* If we're not blocking but don't have a timeout
2552 + set to return immediately */
2553 + if (!data->is_blocking && !data->has_timeout) {
2554 + cto.ReadIntervalTimeout = MAXDWORD;
2557 + /* If we have a timeout ignore the blocking and set
2558 + the total time in which to read the data */
2559 + if (data->has_timeout) {
2560 + cto.ReadIntervalTimeout = MAXDWORD;
2561 + cto.ReadTotalTimeoutMultiplier = MAXDWORD;
2562 + cto.ReadTotalTimeoutConstant = (data->timeout_usec / 1000) +
2563 + (data->timeout_sec * 1000);
2566 + if (!SetCommTimeouts(((php_dio_win32_stream_data*)data)->handle, &cto)) {
2567 + return PHP_STREAM_OPTION_RETURN_ERR;
2570 + return old_is_blocking ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
2572 + case PHP_STREAM_OPTION_READ_TIMEOUT:
2574 + /* struct timeval is supported with PHP_WIN32 defined. */
2575 + struct timeval *tv = (struct timeval*)ptrparam;
2577 + /* A timeout of zero seconds and zero microseconds disables
2578 + any existing timeout. */
2579 + if (tv->tv_sec || tv->tv_usec) {
2580 + data->timeout_sec = tv->tv_sec;
2581 + data->timeout_usec = tv->tv_usec;
2582 + data->has_timeout = -1;
2584 + cto.ReadIntervalTimeout = MAXDWORD;
2585 + cto.ReadTotalTimeoutMultiplier = MAXDWORD;
2586 + cto.ReadTotalTimeoutConstant = (data->timeout_usec / 1000) +
2587 + (data->timeout_sec * 1000);
2589 + data->timeout_sec = 0;
2590 + data->timeout_usec = 0;
2591 + data->has_timeout = 0;
2592 + data->timed_out = 0;
2594 + /* If we're not blocking but don't have a timeout
2595 + set to return immediately */
2596 + if (!data->is_blocking) {
2597 + cto.ReadIntervalTimeout = MAXDWORD;
2601 + if (!SetCommTimeouts(((php_dio_win32_stream_data*)data)->handle, &cto)) {
2602 + return PHP_STREAM_OPTION_RETURN_ERR;
2604 + return PHP_STREAM_OPTION_RETURN_OK;
2607 + return PHP_STREAM_OPTION_RETURN_ERR;
2619 +/* {{{ dio_raw_open_stream
2620 + * Opens the underlying stream.
2622 +int dio_raw_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
2623 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2628 + wdata->creation_disposition = OPEN_EXISTING;
2631 + wdata->creation_disposition = TRUNCATE_EXISTING;
2634 + wdata->creation_disposition = OPEN_ALWAYS;
2637 + wdata->creation_disposition = CREATE_NEW;
2642 + if (*mode && (*mode != '+')) {
2646 + if (*mode && (*mode == '+')) {
2647 + wdata->desired_access = GENERIC_READ | GENERIC_WRITE;
2648 + } else if (OPEN_EXISTING == wdata->creation_disposition) {
2649 + wdata->desired_access = GENERIC_READ;
2651 + wdata->desired_access = GENERIC_WRITE;
2654 + wdata->handle = CreateFile(filename, wdata->desired_access, 0,
2655 + NULL, wdata->creation_disposition, FILE_ATTRIBUTE_NORMAL, NULL);
2656 + if (INVALID_HANDLE_VALUE == wdata->handle) {
2657 + err = GetLastError();
2659 + case ERROR_FILE_EXISTS:
2660 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "File exists!");
2663 + case ERROR_FILE_NOT_FOUND:
2664 + /* ERROR_FILE_NOT_FOUND with TRUNCATE_EXISTING means that
2665 + * the file doesn't exist so now try to create it. */
2666 + if (TRUNCATE_EXISTING == wdata->creation_disposition) {
2667 + wdata->handle = CreateFile(filename, wdata->desired_access, 0,
2668 + NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2669 + if (INVALID_HANDLE_VALUE == wdata->handle) {
2670 + err = GetLastError();
2683 + /* If canonical allocate the canonical buffer. */
2684 + if (data->canonical) {
2685 + wdata->canon_data = emalloc(sizeof(php_dio_win32_canon_data));
2686 + memset(wdata->canon_data, 0, sizeof(php_dio_win32_canon_data));
2687 + wdata->canon_data->size = DIO_WIN32_CANON_BUF_SIZE;
2694 +/* {{{ dio_serial_init
2695 + * Initialises the serial port
2697 +static int dio_serial_init(php_dio_stream_data *data TSRMLS_DC) {
2698 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2699 + DWORD err, rate_def, data_bits_def, stop_bits_def, parity_def;
2702 + if (!dio_data_rate_to_define(data->data_rate, &rate_def)) {
2703 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_rate value (%d) (%d)", data->data_rate, __LINE__);
2707 + if (!dio_data_bits_to_define(data->data_bits, &data_bits_def)) {
2708 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_bits value (%d)", data->data_bits);
2712 + if (!dio_stop_bits_to_define(data->stop_bits, &stop_bits_def)) {
2713 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid stop_bits value (%d)", data->stop_bits);
2717 + if (!dio_parity_to_define(data->parity, &parity_def)) {
2718 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid parity value (%d)", data->parity);
2722 + if (!GetCommState(wdata->handle, &(wdata->olddcb))) {
2723 + err = GetLastError();
2724 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "GetCommState() failed! (%d)", err);
2728 + /* Init the DCB structure */
2729 + memset(&dcb, 0, sizeof(DCB));
2730 + dcb.DCBlength = sizeof(DCB);
2732 + /* Set the communication parameters */
2734 + dcb.BaudRate = rate_def;
2735 + dcb.ByteSize = (BYTE)data_bits_def;
2736 + dcb.StopBits = (BYTE)stop_bits_def;
2737 + dcb.Parity = (BYTE)parity_def;
2739 + /* Set the control line parameters */
2740 + dcb.fDtrControl = DTR_CONTROL_DISABLE;
2741 + dcb.fDsrSensitivity = FALSE;
2742 + dcb.fOutxDsrFlow = FALSE;
2743 + dcb.fTXContinueOnXoff = FALSE;
2744 + dcb.fOutX = FALSE;
2746 + dcb.fErrorChar = FALSE;
2747 + dcb.fNull = FALSE;
2748 + dcb.fAbortOnError = FALSE;
2750 + /* Hardware flow control */
2751 + if (data->flow_control) {
2752 + dcb.fOutxCtsFlow = TRUE;
2753 + dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
2755 + dcb.fOutxCtsFlow = FALSE;
2756 + dcb.fRtsControl = RTS_CONTROL_DISABLE;
2759 + if (!SetCommState(wdata->handle, &dcb)) {
2768 +/* {{{ dio_serial_uninit
2769 + * Restores the serial settings back to their original state.
2771 +int dio_serial_uninit(php_dio_stream_data *data) {
2772 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2774 + if (!SetCommState(wdata->handle, &(wdata->olddcb))) {
2782 +/* {{{ dio_serial_flush
2783 + * Purges the serial buffers of data.
2785 +int dio_serial_purge(php_dio_stream_data *data) {
2786 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2789 + /* Purge the canonical buffer if required */
2790 + if (data->canonical && ((wdata->desired_access & GENERIC_READ) == GENERIC_READ)) {
2791 + wdata->canon_data->read_pos = 0;
2792 + wdata->canon_data->write_pos = 0;
2795 + /* Purge the com port */
2796 + if ((wdata->desired_access & (GENERIC_READ|GENERIC_WRITE)) == (GENERIC_READ|GENERIC_WRITE)) {
2797 + ret = PurgeComm(wdata->handle, PURGE_RXCLEAR|PURGE_TXCLEAR);
2798 + } else if ((wdata->desired_access & GENERIC_WRITE) == GENERIC_WRITE) {
2799 + ret = PurgeComm(wdata->handle, PURGE_TXCLEAR);
2800 + } else if ((wdata->desired_access & GENERIC_READ) == GENERIC_READ) {
2801 + ret = PurgeComm(wdata->handle, PURGE_RXCLEAR);
2808 +/* {{{ dio_serial_open_stream
2809 + * Opens the underlying stream.
2811 +int dio_serial_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
2812 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2813 + COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 };
2816 + if (!dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
2820 + if (!GetCommTimeouts(wdata->handle, &(wdata->oldcto))) {
2821 + err = GetLastError();
2822 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "SetCommTimeouts() failed! (%d) Not a comm port?", err);
2823 + CloseHandle(wdata->handle);
2827 + /* If we're not blocking but don't have a timeout
2828 + set to return immediately */
2829 + if (!data->is_blocking && !data->has_timeout) {
2830 + cto.ReadIntervalTimeout = MAXDWORD;
2833 + /* If we have a timeout ignore the blocking and set
2834 + the total time in which to read the data */
2835 + if (data->has_timeout) {
2836 + cto.ReadIntervalTimeout = MAXDWORD;
2837 + cto.ReadTotalTimeoutMultiplier = MAXDWORD;
2838 + cto.ReadTotalTimeoutConstant = (data->timeout_usec / 1000) +
2839 + (data->timeout_sec * 1000);
2842 + if (!SetCommTimeouts(wdata->handle, &cto)) {
2843 + CloseHandle(wdata->handle);
2847 + if (!dio_serial_init(data TSRMLS_CC)) {
2848 + CloseHandle(wdata->handle);
2857 + * Local variables:
2858 + * c-basic-offset: 4
2861 + * vim600: fdm=marker
2862 + * vim: sw=4 ts=4 noet
2865 +++ b/ext/dio/php_dio.h
2868 + +----------------------------------------------------------------------+
2870 + +----------------------------------------------------------------------+
2871 + | Copyright (c) 1997-2004 The PHP Group |
2872 + +----------------------------------------------------------------------+
2873 + | This source file is subject to version 3.0 of the PHP license, |
2874 + | that is bundled with this package in the file LICENSE, and is |
2875 + | available through the world-wide-web at the following url: |
2876 + | http://www.php.net/license/3_0.txt. |
2877 + | If you did not receive a copy of the PHP license and are unable to |
2878 + | obtain it through the world-wide-web, please send a note to |
2879 + | license@php.net so we can mail you a copy immediately. |
2880 + +----------------------------------------------------------------------+
2887 +#include "php_dio_common.h"
2888 +#include "php_dio_stream_wrappers.h"
2890 +extern zend_module_entry dio_module_entry;
2891 +#define phpext_dio_ptr &dio_module_entry
2893 +#define PHP_DIO_VERSION "0.0.4RC4"
2895 +/* Standard module functions. */
2896 +PHP_MINIT_FUNCTION(dio);
2897 +PHP_MSHUTDOWN_FUNCTION(dio);
2898 +PHP_RINIT_FUNCTION(dio);
2899 +PHP_RSHUTDOWN_FUNCTION(dio);
2900 +PHP_MINFO_FUNCTION(dio);
2902 +/* Legacy functions. */
2903 +PHP_FUNCTION(dio_open);
2904 +PHP_FUNCTION(dio_truncate);
2905 +PHP_FUNCTION(dio_stat);
2906 +PHP_FUNCTION(dio_seek);
2907 +PHP_FUNCTION(dio_read);
2908 +PHP_FUNCTION(dio_write);
2909 +PHP_FUNCTION(dio_fcntl);
2910 +PHP_FUNCTION(dio_close);
2911 +PHP_FUNCTION(dio_tcsetattr);
2921 + * Local variables:
2923 + * c-basic-offset: 4
2924 + * indent-tabs-mode: t
2928 +++ b/ext/dio/php_dio_common.h
2931 + +----------------------------------------------------------------------+
2933 + +----------------------------------------------------------------------+
2934 + | Copyright (c) 2009 Melanie Rhianna Lewis |
2935 + +----------------------------------------------------------------------+
2936 + | This source file is subject to version 3.0 of the PHP license, |
2937 + | that is bundled with this package in the file LICENSE, and is |
2938 + | available through the world-wide-web at the following url: |
2939 + | http://www.php.net/license/3_0.txt. |
2940 + | If you did not receive a copy of the PHP license and are unable to |
2941 + | obtain it through the world-wide-web, please send a note to |
2942 + | license@php.net so we can mail you a copy immediately. |
2943 + +----------------------------------------------------------------------+
2944 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
2945 + +----------------------------------------------------------------------+
2948 +#ifndef PHP_DIO_COMMON_H_
2949 +#define PHP_DIO_COMMON_H_
2952 +#define PHP_DIO_API __declspec(dllexport)
2954 +#define PHP_DIO_API
2958 +#include "php_dio_win32.h"
2960 +#include "php_dio_posix.h"
2963 +#define DIO_STREAM_TYPE_NONE 0
2964 +#define DIO_STREAM_TYPE_RAW 1
2965 +#define DIO_STREAM_TYPE_SERIAL 2
2967 +long dio_convert_to_long(zval *val);
2969 +php_dio_stream_data * dio_create_stream_data(void);
2971 +void dio_init_stream_data(php_dio_stream_data *data);
2973 +void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data TSRMLS_DC);
2975 +void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data TSRMLS_DC);
2977 +void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC);
2979 +void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC);
2981 +size_t dio_common_write(php_dio_stream_data *data, const char *buf, size_t count);
2983 +size_t dio_common_read(php_dio_stream_data *data, const char *buf, size_t count);
2985 +int dio_common_close(php_dio_stream_data *data);
2987 +int dio_common_set_option(php_dio_stream_data *data, int option, int value, void *ptrparam);
2989 +int dio_raw_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC);
2991 +int dio_serial_uninit(php_dio_stream_data *data);
2993 +int dio_serial_purge(php_dio_stream_data *data);
2995 +int dio_serial_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC);
2997 +#endif /* PHP_DIO_COMMON_H_ */
3000 + * Local variables:
3001 + * c-basic-offset: 4
3004 + * vim600: fdm=marker
3005 + * vim: sw=4 ts=4 noet
3008 +++ b/ext/dio/php_dio_common_data.h
3011 + +----------------------------------------------------------------------+
3013 + +----------------------------------------------------------------------+
3014 + | Copyright (c) 2009 Melanie Rhianna Lewis |
3015 + +----------------------------------------------------------------------+
3016 + | This source file is subject to version 3.0 of the PHP license, |
3017 + | that is bundled with this package in the file LICENSE, and is |
3018 + | available through the world-wide-web at the following url: |
3019 + | http://www.php.net/license/3_0.txt. |
3020 + | If you did not receive a copy of the PHP license and are unable to |
3021 + | obtain it through the world-wide-web, please send a note to |
3022 + | license@php.net so we can mail you a copy immediately. |
3023 + +----------------------------------------------------------------------+
3024 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
3025 + +----------------------------------------------------------------------+
3028 +#ifndef PHP_DIO_COMMON_DATA_H_
3029 +#define PHP_DIO_COMMON_DATA_H_
3031 +/* This is the data structure 'base class'. It is common data fields used
3032 + * by all versions of DIO.
3034 +typedef struct _php_dio_stream_data {
3037 + /* Stream options */
3039 +#ifdef DIO_HAS_FILEPERMS
3043 +#ifdef DIO_NONBLOCK
3047 + long timeout_usec;
3050 + /* Serial options */
3057 +} php_dio_stream_data ;
3059 +#endif /* PHP_DIO_COMMON_DATA_H_ */
3062 + * Local variables:
3063 + * c-basic-offset: 4
3066 + * vim600: fdm=marker
3067 + * vim: sw=4 ts=4 noet
3070 +++ b/ext/dio/php_dio_posix.h
3073 + +----------------------------------------------------------------------+
3075 + +----------------------------------------------------------------------+
3076 + | Copyright (c) 2009 Melanie Rhianna Lewis |
3077 + +----------------------------------------------------------------------+
3078 + | This source file is subject to version 3.0 of the PHP license, |
3079 + | that is bundled with this package in the file LICENSE, and is |
3080 + | available through the world-wide-web at the following url: |
3081 + | http://www.php.net/license/3_0.txt. |
3082 + | If you did not receive a copy of the PHP license and are unable to |
3083 + | obtain it through the world-wide-web, please send a note to |
3084 + | license@php.net so we can mail you a copy immediately. |
3085 + +----------------------------------------------------------------------+
3086 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
3087 + +----------------------------------------------------------------------+
3090 +#ifndef PHP_DIO_POSIX_H_
3091 +#define PHP_DIO_POSIX_H_
3093 +#include <sys/stat.h>
3094 +#include <sys/types.h>
3095 +#include <sys/select.h>
3096 +#include <sys/time.h>
3098 +#ifdef HAVE_UNISTD_H
3099 +#include <unistd.h>
3103 +#include <termios.h>
3107 + * Detect if we can support non blocking IO.
3110 +#define DIO_NONBLOCK O_NONBLOCK
3113 +#define DIO_NONBLOCK O_NDELAY
3118 + * POSIXy platforms have file permissions
3120 +#define DIO_HAS_FILEPERMS
3122 +#include "php_dio_common_data.h"
3124 +typedef struct _php_dio_posix_stream_data {
3125 + php_dio_stream_data common;
3128 + /* Serial options */
3129 + struct termios oldtio;
3130 +} php_dio_posix_stream_data ;
3132 +#endif /* PHP_DIO_POSIX_H_ */
3135 + * Local variables:
3136 + * c-basic-offset: 4
3139 + * vim600: fdm=marker
3140 + * vim: sw=4 ts=4 noet
3143 +++ b/ext/dio/php_dio_stream_wrappers.h
3146 + +----------------------------------------------------------------------+
3148 + +----------------------------------------------------------------------+
3149 + | Copyright (c) 2009 Melanie Rhianna Lewis |
3150 + +----------------------------------------------------------------------+
3151 + | This source file is subject to version 3.0 of the PHP license, |
3152 + | that is bundled with this package in the file LICENSE, and is |
3153 + | available through the world-wide-web at the following url: |
3154 + | http://www.php.net/license/3_0.txt. |
3155 + | If you did not receive a copy of the PHP license and are unable to |
3156 + | obtain it through the world-wide-web, please send a note to |
3157 + | license@php.net so we can mail you a copy immediately. |
3158 + +----------------------------------------------------------------------+
3159 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
3160 + +----------------------------------------------------------------------+
3163 +#ifndef PHP_DIO_STREAM_WRAPPERS_H_
3164 +#define PHP_DIO_STREAM_WRAPPERS_H_
3166 +#define DIO_RAW_STREAM_NAME "dio.raw"
3167 +#define DIO_RAW_STREAM_PROTOCOL "dio.raw://"
3168 +#define DIO_SERIAL_STREAM_NAME "dio.serial"
3169 +#define DIO_SERIAL_STREAM_PROTOCOL "dio.serial://"
3171 +/* To support PHP 5.4 and later */
3172 +#if PHP_VERSION_ID < 50399
3173 +#define DIO_SAFE_MODE_CHECK(f, m) (PG(safe_mode) && !php_checkuid(f, m, CHECKUID_CHECK_MODE_PARAM))
3175 +#define DIO_SAFE_MODE_CHECK(f, m) (0)
3178 +extern php_stream_wrapper php_dio_raw_stream_wrapper;
3180 +PHP_FUNCTION(dio_raw);
3182 +extern php_stream_wrapper php_dio_serial_stream_wrapper;
3184 +PHP_FUNCTION(dio_serial);
3186 +#endif /* PHP_DIO_STREAM_WRAPPERS_H_ */
3189 + * Local variables:
3190 + * c-basic-offset: 4
3193 + * vim600: fdm=marker
3194 + * vim: sw=4 ts=4 noet
3197 +++ b/ext/dio/php_dio_win32.h
3200 + +----------------------------------------------------------------------+
3202 + +----------------------------------------------------------------------+
3203 + | Copyright (c) 2009 Melanie Rhianna Lewis |
3204 + +----------------------------------------------------------------------+
3205 + | This source file is subject to version 3.0 of the PHP license, |
3206 + | that is bundled with this package in the file LICENSE, and is |
3207 + | available through the world-wide-web at the following url: |
3208 + | http://www.php.net/license/3_0.txt. |
3209 + | If you did not receive a copy of the PHP license and are unable to |
3210 + | obtain it through the world-wide-web, please send a note to |
3211 + | license@php.net so we can mail you a copy immediately. |
3212 + +----------------------------------------------------------------------+
3213 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
3214 + +----------------------------------------------------------------------+
3217 +#ifndef PHP_DIO_WIN32_H_
3218 +#define PHP_DIO_WIN32_H_
3220 +#include <windows.h>
3222 +/* Windows platform can do non blocking. */
3223 +#define DIO_NONBLOCK
3225 +#include "php_dio_common_data.h"
3227 +#define DIO_WIN32_CANON_BUF_SIZE 8192
3229 +/* This is the buffer information when reading in canonical mode. Data is
3230 + read right up to either buffer being full or a newline being read. Excess
3231 + data will be retained in the buffer until the next read. */
3232 +typedef struct _php_dio_win32_canon_data {
3236 + char buf[DIO_WIN32_CANON_BUF_SIZE];
3238 +} php_dio_win32_canon_data;
3240 +typedef struct _php_dio_win32_stream_data {
3241 + php_dio_stream_data common;
3243 + DWORD desired_access;
3244 + DWORD creation_disposition;
3246 + COMMTIMEOUTS oldcto;
3247 + php_dio_win32_canon_data *canon_data;
3249 +} php_dio_win32_stream_data ;
3251 +#endif /* PHP_DIO_WIN32_H_ */
3254 + * Local variables:
3255 + * c-basic-offset: 4
3258 + * vim600: fdm=marker
3259 + * vim: sw=4 ts=4 noet
3262 +++ b/ext/dio/tests/001.phpt
3265 +Test dio legacy open
3267 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3270 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3273 + $filename = "/dev/null";
3275 + $filename = "NUL";
3278 + $f = dio_open($filename,O_RDONLY);
3280 + echo "Legacy open passed";
3282 + echo "Legacy open failed";
3288 +++ b/ext/dio/tests/dio_raw_stream_001.phpt
3291 +Test dio raw stream open
3293 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3296 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3299 + $filename = "dio.raw:///dev/null";
3301 + $filename = "dio.raw://NUL";
3304 + $f = fopen($filename, "r+");
3306 + echo "Raw open passed";
3309 + echo "Raw open failed";
3315 +++ b/ext/dio/tests/dio_raw_stream_002.phpt
3318 +Test dio raw stream close
3320 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3323 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3326 + $filename = "dio.raw:///dev/null";
3328 + $filename = "dio.raw://NUL";
3331 + $f = fopen($filename, "r+");
3334 + echo "Raw close passed";
3336 + echo "Raw close failed";
3339 + echo "Raw open failed";
3345 +++ b/ext/dio/tests/dio_raw_stream_003.phpt
3348 +Test dio raw stream write
3350 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3353 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3356 + $filename = "dio.raw:///dev/null";
3358 + $filename = "dio.raw://NUL";
3361 + $f = fopen($filename, "r+");
3363 + $data = str_repeat("+", 2048);
3364 + if (fwrite($f, $data)) {
3365 + echo "Raw write passed";
3367 + echo "Raw write failed";
3371 + echo "Raw open failed";
3377 +++ b/ext/dio/tests/dio_raw_stream_004.phpt
3383 + if (!extension_loaded('dio')) print 'skip';
3387 + // Create a temp file with some content to read
3389 + // Create the temp file name
3390 + if (!function_exists('sys_get_temp_dir')) {
3391 + if (!($tmpdir = getenv('TEMP'))) {
3395 + $tmpdir = sys_get_temp_dir();
3397 + $filename = tempnam($tmpdir, 'dio_raw_stream_004.tmp');
3399 + // Create the temp file
3400 + $tf = fopen($filename, "w");
3402 + fwrite($tf, str_repeat('*', 2048));
3405 + echo "Can\'t create temp file";
3408 + $f = fopen('dio.raw://' . $filename, "r");
3410 + $data = fread($f, 2048);
3411 + if ($data && (strlen($data) == 2048)) {
3412 + echo "Raw read passed";
3414 + echo "Raw read failed";
3418 + echo "Raw open failed";
3421 + unlink($filename);
3426 +++ b/ext/dio/tests/dio_raw_stream_005.phpt
3432 + if (!extension_loaded('dio')) print 'skip';
3436 + // Create a temp file with some content to read
3438 + // Create the temp file name
3439 + if (!function_exists('sys_get_temp_dir')) {
3440 + if (!($tmpdir = getenv('TEMP'))) {
3444 + $tmpdir = sys_get_temp_dir();
3446 + $filename = tempnam($tmpdir, 'dio_raw_stream_005.tmp');
3448 + // Create the temp file
3449 + $tf = fopen($filename, "w");
3453 + echo "Can\'t create temp file";
3456 + $f = fopen('dio.raw://' . $filename, "r");
3458 + $data = fread($f, 2048);
3460 + echo "Raw feof passed";
3462 + echo "Raw feof failed";
3466 + echo "Raw open failed";
3469 + unlink($filename);
3474 +++ b/ext/dio/tests/dio_raw_stream_006.phpt
3480 + if (!extension_loaded('dio')) print 'skip';
3484 + // Create a temp file with some content to read
3486 + // Create the temp file name
3487 + if (!function_exists('sys_get_temp_dir')) {
3488 + if (!($tmpdir = getenv('TEMP'))) {
3492 + $tmpdir = sys_get_temp_dir();
3494 + $filename = tempnam($tmpdir, 'diotest');
3496 + // Create the temp file
3497 + $tf = fopen($filename, "w");
3499 + fwrite($tf, str_repeat('*', 2048));
3502 + echo "Can\'t create temp file";
3505 + $f = fopen('dio.raw://' . $filename, "r");
3507 + $data = fread($f, 1024);
3508 + if (stream_set_blocking($f, false)) {
3509 + echo "Raw set blocking passed";
3511 + echo "Raw set blocking failed";
3515 + echo "Raw open failed";
3518 + unlink($filename);
3521 +Raw set blocking passed
3523 +++ b/ext/dio/tests/dio_raw_stream_007.phpt
3526 +Test dio_raw() call
3528 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3531 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3534 + $filename = "/dev/null";
3536 + $filename = "NUL";
3539 + $f = dio_raw($filename, "r+");
3541 + echo "dio_raw passed";