[svn-commits] tilghman: branch tilghman/issue16461 r271334 - in /team/tilghman/issue16461: ...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Jun 18 11:26:06 CDT 2010


Author: tilghman
Date: Fri Jun 18 11:26:02 2010
New Revision: 271334

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=271334
Log:
Commit what we have so far

Added:
    team/tilghman/issue16461/tests/test_func_file.c   (with props)
Modified:
    team/tilghman/issue16461/   (props changed)
    team/tilghman/issue16461/funcs/func_env.c

Propchange: team/tilghman/issue16461/
------------------------------------------------------------------------------
    automerge = *

Propchange: team/tilghman/issue16461/
------------------------------------------------------------------------------
    automerge-email = tilghman at mail.jeffandtilghman.com

Propchange: team/tilghman/issue16461/
------------------------------------------------------------------------------
    svnmerge-integrated = /trunk:1-271330

Modified: team/tilghman/issue16461/funcs/func_env.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/issue16461/funcs/func_env.c?view=diff&rev=271334&r1=271333&r2=271334
==============================================================================
--- team/tilghman/issue16461/funcs/func_env.c (original)
+++ team/tilghman/issue16461/funcs/func_env.c Fri Jun 18 11:26:02 2010
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2006, Digium, Inc.
+ * Copyright (C) 1999 - 2010, Digium, Inc.
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
@@ -25,13 +25,14 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <sys/stat.h>
+#include <sys/stat.h>   /* stat(2) */
 
 #include "asterisk/module.h"
 #include "asterisk/channel.h"
 #include "asterisk/pbx.h"
 #include "asterisk/utils.h"
 #include "asterisk/app.h"
+#include "asterisk/file.h"
 
 /*** DOCUMENTATION
 	<function name="ENV" language="en_US">
@@ -70,26 +71,150 @@
 	</function>
 	<function name="FILE" language="en_US">
 		<synopsis>
-			Obtains the contents of a file.
+			Read or write text file.
+		</synopsis>
+		<syntax>
+			<parameter name="filename" required="true">
+				<para>File to write or modify.</para>
+			</argument>
+			</parameter>
+			<parameter name="offset">
+				<para>Maybe specified as any number. If negative, <replaceable>offset</replaceable> specifies the number
+				of bytes back from the end of the file.</para>
+			</parameter>
+			<parameter name="length">
+				<para>If specified, will limit the length of the data read to that size. If negative,
+				trims <replaceable>length</replaceable> bytes from the end of the file.</para>
+			</parameter>
+			<parameter name="options">
+				<optionlist>
+					<option name="l">
+						<para>Line mode:  offset and length are assumed to be measured in lines, instead of byte offsets.</para>
+					</option>
+					<option name="a">
+						<para>In write mode only, the append option is used to append to the end of the file, instead of overwriting the existing file.</para>
+					</option>
+				</optionlist>
+			</parameter>
+			<parameter name="format">
+				<para>The <replaceable>format</replaceable> parameter may be used to delimit the type of line terminators in line mode.</para>
+				<optionlist>
+					<option name="u">
+						<para>Unix newline format.</para>
+					</option>
+					<option name="d">
+						<para>DOS newline format.</para>
+					</option>
+					<option name="m">
+						<para>Macintosh newline format.</para>
+					</option>
+				</optionlist>
+			</parameter>
+		</syntax>
+		<description>
+			<para>Read and write text file in character and line mode.</para>
+			<para>Examples :</para>
+			<para/>
+			<para>Read mode (byte):</para>
+			<para>    ;reads the entire content of the file.</para>
+			<para>    Set(foo=${FILE(/tmp/test.txt)})</para>
+			<para>    ;reads from the 11th byte to the end of the file (i.e. skips the first 10).</para>
+			<para>    Set(foo=${FILE(/tmp/test.txt,10)})</para>
+			<para>    ;reads from the 11th to 20th byte in the file (i.e. skip the first 10, then read 10 bytes).</para>
+			<para>    Set(foo=${FILE(/tmp/test.txt,10,10)})</para>
+			<para/>
+			<para>Read mode (line):</para>
+			<para>    ; reads the 3rd line of the file.</para>
+			<para>    Set(foo=${FILE(/tmp/test.txt,3,1,l)})</para>
+			<para>    ; reads the 3rd and 4th lines of the file.</para>
+			<para>    Set(foo=${FILE(/tmp/test.txt,3,2,l)})</para>
+			<para>    ; reads from the third line to the end of the file.</para>
+			<para>    Set(foo=${FILE(/tmp/test.txt,3,,l)})</para>
+			<para>    ; reads the last three lines of the file.</para>
+			<para>    Set(foo=${FILE(/tmp/test.txt,-3,,l)})</para>
+			<para>    ; reads the 3rd line of a DOS-formatted file.</para>
+			<para>    Set(foo=${FILE(/tmp/test.txt,3,1,l,d)})</para>
+			<para/>
+			<para>Write mode (byte):</para>
+			<para>    ; truncate the file and write "bar"</para>
+			<para>    Set(FILE(/tmp/test.txt)=bar)</para>
+			<para>    ; Append "bar"</para>
+			<para>    Set(FILE(/tmp/test.txt,,,a)=bar)</para>
+			<para>    ; Replace the first byte with "bar" (replaces 1 character with 3)</para>
+			<para>    Set(FILE(/tmp/test.txt,0,1)=bar)</para>
+			<para>    ; Replace 10 bytes beginning at the 21st byte of the file with "bar"</para>
+			<para>    Set(FILE(/tmp/test.txt,20,10)=bar)</para>
+			<para>    ; Replace all bytes from the 21st with "bar"</para>
+			<para>    Set(FILE(/tmp/test.txt,20)=bar)</para>
+			<para>    ; Insert "bar" after the 4th character</para>
+			<para>    Set(FILE(/tmp/test.txt,4,0)=bar)</para>
+			<para/>
+			<para>Write mode (line):</para>
+			<para>    ; Replace the first line of the file with "bar"</para>
+			<para>    Set(FILE(/tmp/foo.txt,0,1,l)=bar)</para>
+			<para>    ; Replace the last line of the file with "bar"</para>
+			<para>    Set(FILE(/tmp/foo.txt,-1,,l)=bar)</para>
+			<para>    ; Append "bar" to the file with a newline</para>
+			<para>    Set(FILE(/tmp/foo.txt,,,al)=bar)</para>
+		</description>
+		<see-also>
+			<ref type="function">FILE_COUNT_LINE</ref>
+			<ref type="function">FILE_FORMAT</ref>
+		</see-also>
+	</function>
+	<function name="FILE_COUNT_LINE" language="en_US">
+		<synopsis>
+			Obtains the number of lines of a text file.
 		</synopsis>
 		<syntax>
 			<parameter name="filename" required="true" />
-			<parameter name="offset" required="true">
-				<para>Maybe specified as any number. If negative, <replaceable>offset</replaceable> specifies the number
-				of bytes back from the end of the file.</para>
-			</parameter>
-			<parameter name="length" required="true">
-				<para>If specified, will limit the length of the data read to that size. If negative,
-				trims <replaceable>length</replaceable> bytes from the end of the file.</para>
+			<parameter name="format">
+				<para>Format may be one of the following:</para>
+				<optionlist>
+					<option name="u">
+						<para>Unix newline format.</para>
+					</option>
+					<option name="d">
+						<para>DOS newline format.</para>
+					</option>
+					<option name="m">
+						<para>Macintosh newline format.</para>
+					</option>
+				</optionlist>
+				<note><para>If not specified, an attempt will be made to determine the newline format type.</para></note>
 			</parameter>
 		</syntax>
 		<description>
+			<para>Returns the number of lines, or <literal>-1</literal> on error.</para>
 		</description>
+		<see-also>
+			<ref type="function">FILE</ref>
+			<ref type="function">FILE_FORMAT</ref>
+		</see-also>
+	</function>
+	<function name="FILE_FORMAT" language="en_US">
+		<synopsis>
+			Return the newline format of a text file.
+		</synopsis>
+		<syntax>
+			<parameter name="filename" required="true" />
+		</syntax>
+		<description>
+			<para>Return the line terminator type:</para>
+			<para>'u' - Unix "\n" format</para>
+			<para>'d' - DOS "\r\n" format</para>
+			<para>'m' - Macintosh "\r" format</para>
+			<para>'x' - Cannot be determined</para>
+		</description>
+		<see-also>
+			<ref type="function">FILE</ref>
+			<ref type="function">FILE_COUNT_LINE</ref>
+		</see-also>
 	</function>
  ***/
 
 static int env_read(struct ast_channel *chan, const char *cmd, char *data,
-		    char *buf, size_t len)
+			char *buf, size_t len)
 {
 	char *ret = NULL;
 
@@ -105,7 +230,7 @@
 }
 
 static int env_write(struct ast_channel *chan, const char *cmd, char *data,
-		     const char *value)
+			 const char *value)
 {
 	if (!ast_strlen_zero(data) && strncmp(data, "AST_", 4)) {
 		if (!ast_strlen_zero(value)) {
@@ -119,7 +244,7 @@
 }
 
 static int stat_read(struct ast_channel *chan, const char *cmd, char *data,
-		     char *buf, size_t len)
+			 char *buf, size_t len)
 {
 	char *action;
 	struct stat s;
@@ -161,66 +286,761 @@
 	return 0;
 }
 
-static int file_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+enum file_format {
+	FF_UNKNOWN = -1,
+	FF_UNIX,
+	FF_DOS,
+	FF_MAC,
+};
+
+static int64_t count_lines(const char *filename, enum file_format newline_format)
+{
+	int count = 0;
+	char fbuf[4096];
+	FILE *ff;
+
+	if (!(ff = fopen(filename, "r"))) {
+		ast_log(LOG_ERROR, "Unable to open '%s': %s\n", filename, strerror(errno));
+		return -1;
+	}
+
+	while (fgets(fbuf, sizeof(fbuf), ff)) {
+		char *next = fbuf, *first_cr = NULL, *first_nl = NULL;
+
+		/* Must do it this way, because if the fileformat is FF_MAC, then Unix
+		 * assumptions about line-format will not come into play. */
+		while (next) {
+			if (newline_format == FF_DOS || newline_format == FF_MAC || newline_format == FF_UNKNOWN) {
+				first_cr = strchr(next, '\r');
+			}
+			if (newline_format == FF_UNIX || newline_format == FF_UNKNOWN) {
+				first_nl = strchr(next, '\n');
+			}
+
+			/* No terminators found in buffer */
+			if (!first_cr && !first_nl) {
+				break;
+			}
+
+			if (newline_format == FF_UNKNOWN) {
+				if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
+					if (first_nl && first_nl == first_cr + 1) {
+						newline_format = FF_DOS;
+					} else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
+						/* Get it on the next pass */
+						fseek(ff, -1, SEEK_CUR);
+						break;
+					} else {
+						newline_format = FF_MAC;
+						first_nl = NULL;
+					}
+				} else {
+					newline_format = FF_UNIX;
+					first_cr = NULL;
+				}
+				/* Jump down into next section */
+			}
+
+			if (newline_format == FF_DOS) {
+				if (first_nl && first_cr && first_nl == first_cr + 1) {
+					next = first_nl + 1;
+					count++;
+				} else if (first_cr == &fbuf[sizeof(fbuf) - 2]) {
+					/* Get it on the next pass */
+					fseek(ff, -1, SEEK_CUR);
+					break;
+				}
+			} else if (newline_format == FF_MAC) {
+				if (first_cr) {
+					next = first_cr + 1;
+					count++;
+				}
+			} else if (newline_format == FF_UNIX) {
+				if (first_nl) {
+					next = first_nl + 1;
+					count++;
+				}
+			}
+		}
+	}
+	fclose(ff);
+
+	return count;
+}
+
+static int file_count_line(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+	enum file_format newline_format = FF_UNKNOWN;
+	int64_t count;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(filename);
+		AST_APP_ARG(format);
+	);
+
+	AST_STANDARD_APP_ARGS(args, data);
+	if (args.argc > 1) {
+		if (tolower(args.format[0]) == 'd') {
+			newline_format = FF_DOS;
+		} else if (tolower(args.format[0]) == 'm') {
+			newline_format = FF_MAC;
+		} else if (tolower(args.format[0]) == 'u') {
+			newline_format = FF_UNIX;
+		}
+	}
+
+	count = count_lines(args.filename, newline_format);
+	ast_str_set(buf, len, "%" PRId64, count);
+	return 0;
+}
+
+#define LINE_COUNTER(cptr, term, counter) \
+	if (*cptr == '\n' && term == FF_UNIX) { \
+		counter++; \
+	} else if (*cptr == '\n' && term == FF_DOS && dos_state == 0) { \
+		dos_state = 1; \
+	} else if (*cptr == '\r' && term == FF_DOS && dos_state == 1) { \
+		dos_state = 0; \
+		counter++; \
+	} else if (*cptr == '\r' && term == FF_MAC) { \
+		counter++; \
+	} else if (term == FF_DOS) { \
+		dos_state = 0; \
+	}
+
+static enum file_format file2format(const char *filename)
+{
+	FILE *ff;
+	char fbuf[4096];
+	char *first_cr, *first_nl;
+	enum file_format newline_format = FF_UNKNOWN;
+
+	if (!(ff = fopen(filename, "r"))) {
+		ast_log(LOG_ERROR, "Cannot open '%s': %s\n", filename, strerror(errno));
+		return -1;
+	}
+
+	while (fgets(fbuf, sizeof(fbuf), ff)) {
+		first_cr = strchr(fbuf, '\r');
+		first_nl = strchr(fbuf, '\n');
+
+		if (!first_cr && !first_nl) {
+			continue;
+		}
+
+		if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
+
+			if (first_nl && first_nl == first_cr + 1) {
+				newline_format = FF_DOS;
+			} else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
+				/* Edge case: get it on the next pass */
+				fseek(ff, -1, SEEK_CUR);
+				continue;
+			} else {
+				newline_format = FF_MAC;
+			}
+		} else {
+			newline_format = FF_UNIX;
+		}
+		break;
+	}
+	fclose(ff);
+	return newline_format;
+}
+
+static int file_format(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+	enum file_format newline_format = file2format(data);
+	ast_str_set(buf, len, "%c", newline_format == FF_UNIX ? 'u' : newline_format == FF_DOS ? 'd' : newline_format == FF_MAC ? 'm' : 'x');
+	return 0;
+}
+
+static int file_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+	FILE *ff;
+	int64_t offset = 0, length = LLONG_MAX;
+	enum file_format format = FF_UNKNOWN;
+	char fbuf[4096];
+	int64_t flength, i; /* iterator needs to be signed, so it can go negative and terminate the loop */
+	int64_t offset_offset = -1, length_offset = -1;
+	char dos_state = 0;
+	size_t readlen;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(filename);
+		AST_APP_ARG(offset);
+		AST_APP_ARG(length);
+		AST_APP_ARG(options);
+		AST_APP_ARG(fileformat);
+	);
+
+	AST_STANDARD_APP_ARGS(args, data);
+
+	if (args.argc > 1) {
+		sscanf(args.offset, "%" SCNd64, &offset);
+	}
+	if (args.argc > 2) {
+		sscanf(args.length, "%" SCNd64, &length);
+	}
+
+	if (args.argc < 4 || !strchr(args.options, 'l')) {
+		/* Character-based mode */
+		off_t off_i;
+
+	   	if (!(ff = fopen(args.filename, "r"))) {
+			ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", args.filename, strerror(errno));
+			return 0;
+		}
+
+		fseeko(ff, 0, SEEK_END);
+		flength = ftello(ff);
+
+		if (offset < 0) {
+			fseeko(ff, offset, SEEK_END);
+			offset = ftello(ff);
+		}
+		if (length < 0) {
+			fseek(ff, length, SEEK_END);
+			if ((length = ftello(ff) - offset) < 0) {
+				/* Eliminates all results */
+				return -1;
+			}
+		}
+
+		ast_str_reset(*buf);
+
+		fseeko(ff, offset, SEEK_SET);
+		for (off_i = ftello(ff); off_i < flength && off_i <= offset + length; off_i += sizeof(fbuf)) {
+			/* Calculate if we need to retrieve just a portion of the file in memory */
+			size_t toappend = sizeof(fbuf);
+
+			if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
+				ast_log(LOG_ERROR, "Short read?!!\n");
+				break;
+			}
+
+			/* Don't go past the length requested */
+			if (off_i + toappend > offset + length) {
+				toappend = length - off_i;
+			}
+
+			ast_str_append_substr(buf, len, fbuf, toappend);
+		}
+		ast_str_append_substr(buf, len, "", 1);
+
+		return 0;
+	}
+
+	/* Line-based read */
+	if (args.argc == 5) {
+		if (tolower(args.fileformat[0]) == 'd') {
+			format = FF_DOS;
+		} else if (tolower(args.fileformat[0]) == 'm') {
+			format = FF_MAC;
+		} else if (tolower(args.fileformat[0]) == 'u') {
+			format = FF_UNIX;
+		}
+	}
+
+	if (format == FF_UNKNOWN) {
+		if ((format = file2format(args.filename)) == FF_UNKNOWN) {
+			ast_log(LOG_WARNING, "'%s' is not a line-based file\n", args.filename);
+			return -1;
+		}
+	}
+
+	if (offset < 0 && length <= offset) {
+		/* Length eliminates all content */
+		return -1;
+	}
+
+	if (!(ff = fopen(args.filename, "r"))) {
+		ast_log(LOG_ERROR, "Cannot open '%s': %s\n", args.filename, strerror(errno));
+		return -1;
+	}
+
+	if (fseek(ff, 0, SEEK_END)) {
+		ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
+		fclose(ff);
+		return -1;
+	}
+	flength = ftello(ff);
+
+	/* For negative offset and/or negative length */
+	if (offset < 0 || length < 0) {
+		int64_t count = 0;
+		for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
+			size_t end;
+			char *pos;
+			if (fseeko(ff, i, SEEK_SET)) {
+				ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
+			}
+			end = fread(fbuf, 1, sizeof(fbuf), ff);
+			fbuf[end] = '\0';
+			for (pos = fbuf + sizeof(buf) - 1; pos > fbuf - 1; pos--) {
+				LINE_COUNTER(pos, format, count);
+
+				if (length < 0 && count * -1 == length) {
+					length_offset = i + (pos - fbuf) + 1;
+				} else if (count * -1 == offset) {
+					/* Found our initial offset.  We're done with reverse motion! */
+					if (format == FF_DOS) {
+						offset_offset = i + (pos - fbuf) + 2;
+					} else {
+						offset_offset = i + (pos - fbuf) + 1;
+					}
+					break;
+				}
+			}
+			if (offset_offset >= 0) {
+				break;
+			}
+		}
+	}
+
+	/* Positve line offset */
+	if (offset > 0) {
+		int64_t count = 0;
+		fseek(ff, 0, SEEK_SET);
+		for (i = 0; i < flength; i += sizeof(fbuf)) {
+			char *pos;
+			for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
+				LINE_COUNTER(pos, format, count);
+
+				if (count == offset) {
+					offset_offset = i + (pos - fbuf);
+					break;
+				}
+			}
+			if (offset_offset >= 0) {
+				break;
+			}
+		}
+	}
+
+	if (offset_offset < 0) {
+		ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
+		fclose(ff);
+		return -1;
+	}
+
+	ast_str_reset(*buf);
+	fseeko(ff, SEEK_SET, offset_offset);
+
+	/* If we have both offset_offset and length_offset, then grabbing the
+	 * buffer is simply a matter of just retrieving the file and adding it
+	 * to buf.  Otherwise, we need to run byte-by-byte forward until the
+	 * length is complete. */
+	if (length_offset >= 0) {
+		for (i = offset_offset; i < length_offset; i += sizeof(fbuf)) {
+			if ((readlen = fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf))) {
+				/* This is expected on the last time through the loop */
+				memset(&fbuf[readlen], 0, sizeof(fbuf) - readlen);
+			}
+			ast_str_append_substr(buf, len, fbuf, i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf));
+		}
+	} else {
+		/* Positive line offset */
+		int64_t current_length = 0;
+		char dos_state = 0;
+		for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
+			char *pos;
+			if ((readlen = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
+				memset(&fbuf[readlen], 0, sizeof(fbuf) - readlen);
+			}
+			for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
+				LINE_COUNTER(pos, format, current_length);
+
+				if (current_length == length) {
+					length_offset = i + (pos - fbuf) + 1;
+					break;
+				}
+			}
+			ast_str_append_substr(buf, len, fbuf, length_offset >= 0 ? i - length_offset : sizeof(fbuf));
+
+			if (length_offset >= 0) {
+				break;
+			}
+		}
+	}
+
+	return 0;
+}
+
+const char *format2term(enum file_format f) __attribute__((const));
+const char *format2term(enum file_format f)
+{
+	const char *term[] = { "", "\n", "\r\n", "\r" };
+	return term[f + 1];
+}
+
+static int file_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
 {
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(filename);
 		AST_APP_ARG(offset);
 		AST_APP_ARG(length);
+		AST_APP_ARG(options);
+		AST_APP_ARG(format);
 	);
-	int offset = 0, length, res = 0;
-	char *contents;
-	size_t contents_len;
+	int64_t offset = 0, length = LLONG_MAX;
+	off_t flength, vlength;
+	size_t foplen;
+	FILE *ff;
 
 	AST_STANDARD_APP_ARGS(args, data);
+
 	if (args.argc > 1) {
-		offset = atoi(args.offset);
-	}
-
+		sscanf(args.offset, "%" SCNd64, &offset);
+	}
 	if (args.argc > 2) {
-		/* The +1/-1 in this code section is to accomodate for the terminating NULL. */
-		if ((length = atoi(args.length) + 1) > len) {
-			ast_log(LOG_WARNING, "Length %d is greater than the max (%d).  Truncating output.\n", length - 1, (int)len - 1);
-			length = len;
+		sscanf(args.length, "%" SCNd64, &length);
+	}
+
+	vlength = strlen(value);
+
+	if (args.argc < 4 || !strchr(args.options, 'l')) {
+		/* Character-based mode */
+
+		if (args.argc == 1) {
+			if (!(ff = fopen(args.filename, "w"))) {
+		   		ast_log(LOG_WARNING, "Cannot open file '%s' for writing: %s\n", args.filename, strerror(errno));
+		   		return 0;
+		   	}
+			if (fwrite(value, 1, vlength, ff) < vlength) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+    		fclose(ff);
+		   	return 0;
+		} else if (args.argc > 3 && strchr(args.options, 'a')) {
+			/* Append mode */
+			if (!(ff = fopen(args.filename, "a"))) {
+    			ast_log(LOG_WARNING, "Cannot open file '%s' for appending: %s\n", args.filename, strerror(errno));
+    			return 0;
+			}
+			if (fwrite(value, 1, vlength, ff) < vlength) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+			fclose(ff);
+			return 0;
+		}
+
+		if (!(ff = fopen(args.filename, "r+"))) {
+    		ast_log(LOG_WARNING, "Cannot open file '%s' for modification: %s\n", args.filename, strerror(errno));
+    		return 0;
+		}
+		fseeko(ff, 0, SEEK_END);
+		flength = ftello(ff);
+
+		if (length < 0) {
+			length += flength;
+			if (length < 0) {
+				ast_log(LOG_ERROR, "Length '%s' exceeds the file length.  No data will be written.\n", args.length);
+				fclose(ff);
+				return -1;
+			}
+		}
+
+		if (offset < 0) {
+			fseeko(ff, offset, SEEK_END);
+		}
+
+		if (length == vlength) {
+			/* Simplest case, a straight replace */
+			if (fwrite(value, 1, vlength, ff) < vlength) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+			fclose(ff);
+		} else if (length > vlength) {
+			/* More complex -- need to close a gap */
+			char fbuf[4096];
+			if (fwrite(value, 1, vlength, ff) < vlength) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+			while (ftello(ff) < flength) {
+				fseeko(ff, length - vlength, SEEK_CUR);
+				if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+					ast_log(LOG_ERROR, "Short read?!!\n");
+				}
+				fseeko(ff, vlength - length, SEEK_CUR);
+				if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+					ast_log(LOG_ERROR, "Short write?!!\n");
+				}
+			}
+			fflush(ff);
+			if (ftruncate(fileno(ff), flength - (length - vlength))) {
+				ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
+			}
+			fclose(ff);
+		} else {
+			/* Most complex -- need to open a gap */
+			char fbuf[4096];
+			off_t lastwritten;
+			fseeko(ff, vlength - length, SEEK_END);
+			while (offset + sizeof(fbuf) < ftello(ff)) {
+				fseeko(ff, -1 * (sizeof(fbuf) + (vlength - length)), SEEK_CUR);
+				if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+					ast_log(LOG_ERROR, "Short read?!!\n");
+					fclose(ff);
+					return -1;
+				}
+				fseeko(ff, vlength - length, SEEK_CUR);
+				if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+					ast_log(LOG_ERROR, "Short write?!!\n");
+					fclose(ff);
+					return -1;
+				}
+			}
+			/* Note the location of this last buffer -- we must not overwrite this position. */
+			lastwritten = ftello(ff) - sizeof(fbuf);
+			fseek(ff, offset + length, SEEK_SET);
+			/* Doesn't matter how much we read -- just need to restrict the write */
+			if (fread(fbuf, 1, sizeof(fbuf), ff)) { }
+			fseek(ff, offset, SEEK_SET);
+			/* Write out the value, then write just up until where we last moved some data */
+			if (fwrite(value, 1, vlength, ff) < vlength) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			} else if (fwrite(fbuf, 1, (foplen = lastwritten - ftello(ff)), ff) < foplen) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+			fclose(ff);
 		}
 	} else {
-		length = len;
-	}
-
-	if (!(contents = ast_read_textfile(args.filename))) {
-		return -1;
-	}
-
-	do {
-		contents_len = strlen(contents);
-		if (offset > contents_len) {
-			res = -1;
-			break;
-		}
-
-		if (offset >= 0) {
-			if (length < 0) {
-				if (contents_len - offset + length < 0) {
-					/* Nothing left after trimming */
-					res = -1;
-					break;
-				}
-				ast_copy_string(buf, &contents[offset], contents_len + length);
+		enum file_format newline_format = FF_UNKNOWN;
+
+		/* Line mode */
+		if (args.argc == 5) {
+			if (tolower(args.format[0]) == 'u') {
+				newline_format = FF_UNIX;
+			} else if (tolower(args.format[0]) == 'm') {
+				newline_format = FF_MAC;
+			} else if (tolower(args.format[0]) == 'd') {
+				newline_format = FF_DOS;
+			}
+		}
+		if (newline_format == FF_UNKNOWN && (newline_format = file2format(args.filename)) == FF_UNKNOWN) {
+			ast_log(LOG_ERROR, "File '%s' not in line format\n", args.filename);
+			return -1;
+		}
+
+		if (offset == 0 && length == LLONG_MAX) {
+			if (!(ff = fopen(args.filename, "w"))) {
+				ast_log(LOG_ERROR, "Unable to open '%s' for writing: %s\n", args.filename, strerror(errno));
+				return -1;
+			}
+			if (fwrite(value, 1, vlength, ff) < vlength) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			} else if (fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+			fclose(ff);
+		} else if (strchr(args.options, 'a')) {
+			if (!(ff = fopen(args.filename, "a"))) {
+				ast_log(LOG_ERROR, "Unable to open '%s' for appending: %s\n", args.filename, strerror(errno));
+				return -1;
+			}
+			if (fwrite(value, 1, vlength, ff) < vlength) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			} else if (fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+			fclose(ff);
+		} else {
+			int64_t offset_offset = -1, length_offset = -1, flength, i, current_length = 0;
+			char dos_state = 0, fbuf[4096];
+
+			if (offset < 0 && length < offset) {
+				/* Nonsense! */
+				ast_log(LOG_ERROR, "Length cannot specify a position prior to the offset\n");
+				return -1;
+			}
+
+			if (!(ff = fopen(args.filename, "r+"))) {
+				ast_log(LOG_ERROR, "Cannot open '%s' for modification: %s\n", args.filename, strerror(errno));
+				return -1;
+			}
+
+			if (fseek(ff, 0, SEEK_END)) {
+				ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
+				fclose(ff);
+				return -1;
+			}
+			flength = ftello(ff);
+
+			/* For negative offset and/or negative length */
+			if (offset < 0 || length < 0) {
+				int64_t count = 0;
+				for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
+					size_t end;
+					char *pos;
+					if (fseeko(ff, i, SEEK_SET)) {
+						ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
+					}
+					if ((end = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
+						memset(fbuf + end, 0, sizeof(fbuf) - end);
+					}
+					for (pos = fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
+						LINE_COUNTER(pos, newline_format, count);
+
+						if (length < 0 && count * -1 == length) {
+							length_offset = i + (pos - fbuf) + 1;
+						} else if (count * -1 == offset) {
+							/* Found our initial offset.  We're done with reverse motion! */
+							if (newline_format == FF_DOS) {
+								offset_offset = i + (pos - fbuf) + 2;
+							} else {
+								offset_offset = i + (pos - fbuf) + 1;
+							}
+							break;
+						}
+					}
+					if (offset_offset >= 0) {
+						break;
+					}
+				}
+			}
+
+			/* Positve line offset */
+			if (offset > 0) {
+				int64_t count = 0;
+				fseek(ff, 0, SEEK_SET);
+				for (i = 0; i < flength; i += sizeof(fbuf)) {
+					size_t end;
+					char *pos;
+					if ((end = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
+						memset(fbuf + end, 0, sizeof(fbuf) - end);
+					}
+					for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
+						LINE_COUNTER(pos, newline_format, count);
+		
+						if (count == offset) {
+							offset_offset = i + (pos - fbuf);
+							break;
+						}
+					}
+					if (offset_offset >= 0) {
+						break;
+					}
+				}
+			}
+		
+			if (offset_offset < 0) {
+				ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
+				fclose(ff);
+				return -1;
+			}
+
+			if (length == 0) {
+				length_offset = offset_offset;
+			} else if (length == LLONG_MAX) {
+				length_offset = flength;
+			}
+
+			/* Positive line length */
+			if (length_offset < 0) {
+				fseeko(ff, SEEK_SET, offset_offset);
+				for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
+					size_t end;
+					char *pos;
+					if ((end = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
+						memset(fbuf + end, 0, sizeof(fbuf) - end);
+					}
+					for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
+						LINE_COUNTER(pos, newline_format, current_length);
+
+						if (current_length == length) {
+							length_offset = i + (pos - fbuf) + 1;
+							break;
+						}
+					}
+					if (length_offset >= 0) {
+						break;
+					}
+				}
+				if (length_offset < 0) {
+					/* Exceeds length of file */
+					length_offset = flength;
+				}
+			}
+
+			/* Have offset_offset and length_offset now */
+			if (length_offset - offset_offset == vlength + strlen(format2term(newline_format))) {
+				/* Simple case - replacement of text inline */
+				fseeko(ff, offset_offset, SEEK_SET);
+				if (fwrite(value, 1, vlength, ff) < vlength) {
+					ast_log(LOG_ERROR, "Short write?!!\n");
+				} else if (fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
+					ast_log(LOG_ERROR, "Short write?!!\n");
+				}
+				fclose(ff);
+			} else if (length_offset - offset_offset > vlength + strlen(format2term(newline_format))) {
+				/* More complex case - need to shorten file */
+				fseeko(ff, offset_offset, SEEK_SET);
+				if (fwrite(value, 1, vlength, ff) < vlength) {
+					ast_log(LOG_ERROR, "Short write?!!\n");
+					fclose(ff);
+					return -1;
+				}
+				while (ftello(ff) < flength) {
+					fseek(ff, length_offset - offset_offset, SEEK_CUR);
+					if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+						ast_log(LOG_ERROR, "Short read?!!\n");
+						fclose(ff);
+						return -1;
+					}
+					fseek(ff, offset_offset - length_offset, SEEK_CUR);
+					if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+						ast_log(LOG_ERROR, "Short write?!!\n");
+						fclose(ff);
+						return -1;
+					}
+				}
+				fflush(ff);
+				if (ftruncate(fileno(ff), flength - (length_offset - offset_offset - vlength))) {
+					ast_log(LOG_ERROR, "Truncation of file failed: %s\n", strerror(errno));
+				}
+				fclose(ff);
 			} else {
-				ast_copy_string(buf, &contents[offset], length);
-			}
-		} else {
-			if (offset * -1 > contents_len) {
-				ast_log(LOG_WARNING, "Offset is larger than the file size.\n");
-				offset = contents_len * -1;
-			}
-			ast_copy_string(buf, &contents[contents_len + offset], length);
-		}
-	} while (0);
-
-	ast_free(contents);
-
-	return res;
+				/* Most complex case - need to lengthen file */
+				off_t lastwritten;
+				int64_t origlen = length_offset - offset_offset;
+				fseeko(ff, vlength - origlen, SEEK_END);
+				while (offset_offset + sizeof(fbuf) < ftello(ff)) {
+					fseeko(ff, -1 * (sizeof(fbuf) + (vlength - origlen)), SEEK_CUR);
+					if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+						ast_log(LOG_ERROR, "Short read?!!\n");
+						fclose(ff);
+						return -1;
+					}
+					fseeko(ff, vlength - origlen, SEEK_CUR);
+					if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+						ast_log(LOG_ERROR, "Short write?!!\n");
+						fclose(ff);
+						return -1;
+					}
+				}
+				lastwritten = ftello(ff) - sizeof(fbuf);
+				fseek(ff, length_offset, SEEK_SET);
+				if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+					ast_log(LOG_ERROR, "Short read?!!\n");
+					fclose(ff);
+					return -1;
+				}
+				fseek(ff, offset_offset, SEEK_SET);
+				if (fwrite(value, 1, vlength, ff) < vlength) {
+					ast_log(LOG_ERROR, "Short write?!!\n");
+					fclose(ff);
+					return -1;
+				}
+				if (fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
+					ast_log(LOG_ERROR, "Short write?!!\n");
+				} else if (fwrite(fbuf, 1, (foplen = lastwritten - ftello(ff)), ff) < foplen) {
+					ast_log(LOG_ERROR, "Short write?!!\n");
+				}
+				fclose(ff);
+			}
+		}
+	}
+
+	return 0;
 }
 
 static struct ast_custom_function env_function = {
@@ -237,12 +1057,20 @@
 
 static struct ast_custom_function file_function = {
 	.name = "FILE",
-	.read = file_read
-	/*
-	 * Some enterprising programmer could probably add write functionality
-	 * to FILE(), although I'm not sure how useful it would be.  Hence why
-	 * it's called FILE and not READFILE (like the app was).
-	 */
+	.read2 = file_read,
+	.write = file_write,
+};
+
+static struct ast_custom_function file_count_line_function = {
+	.name = "FILE_COUNT_LINE",
+	.read2 = file_count_line,
+	.read_max = 12,
+};
+
+static struct ast_custom_function file_format_function = {
+	.name = "FILE_FORMAT",
+	.read2 = file_format,
+	.read_max = 2,
 };
 
 static int unload_module(void)
@@ -252,6 +1080,8 @@
 	res |= ast_custom_function_unregister(&env_function);
 	res |= ast_custom_function_unregister(&stat_function);
 	res |= ast_custom_function_unregister(&file_function);
+	res |= ast_custom_function_unregister(&file_count_line_function);
+	res |= ast_custom_function_unregister(&file_format_function);
 
 	return res;
 }
@@ -263,6 +1093,8 @@
 	res |= ast_custom_function_register(&env_function);
 	res |= ast_custom_function_register(&stat_function);
 	res |= ast_custom_function_register(&file_function);
+	res |= ast_custom_function_register(&file_count_line_function);
+	res |= ast_custom_function_register(&file_format_function);
 
 	return res;
 }

Added: team/tilghman/issue16461/tests/test_func_file.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/issue16461/tests/test_func_file.c?view=auto&rev=271334
==============================================================================
--- team/tilghman/issue16461/tests/test_func_file.c (added)
+++ team/tilghman/issue16461/tests/test_func_file.c Fri Jun 18 11:26:02 2010
@@ -1,0 +1,250 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2010, Digium, Inc.
+ *
+ * Tilghman Lesher <tlesher AT digium DOT com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Function FILE tests
+ *
+ * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
+ *
+ * \ingroup tests
+ */
+
+/*** MODULEINFO
+	<depend>TEST_FRAMEWORK</depend>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/utils.h"
+#include "asterisk/app.h"
+#include "asterisk/module.h"
+#include "asterisk/test.h"
+#include "asterisk/pbx.h"
+
+static struct {
+	const char *contents;
+	const char *args;
+	const char *value;
+} read_tests[] = {
+	/* 4 different ways of specifying the first character */
+	{ "123456789", "0,1", "1" },
+	{ "123456789", "0,-8", "1" },
+	{ "123456789", "-9,1", "1" },
+	{ "123456789", "-9,-8", "1" },
+	/* Does 0-length work? */
+	{ "123456789", "0,0", "" },
+	{ "123456789", "-9,0", "" },
+	{ "123456789", "-9,-9", "" },
+	/* Does negative length work? */
+	{ "123456789", "5,-6", "" },
+	{ "123456789", "-5,-6", "" },
+	/* No length */
+	{ "123456789", "-5", "56789" },
+	{ "123456789", "4", "56789" },
+	/* Line mode, 4 ways of specifying the first character */
+	{ "123\n456\n789\n", "0,1,l", "123\n" },
+	{ "123\n456\n789\n", "-3,1,l", "123\n" },
+	{ "123\n456\n789\n", "0,-2,l", "123\n" },
+	{ "123\n456\n789\n", "-3,-2,l", "123\n" },
+	/* Line mode, 0-length */
+	{ "123\n456\n789\n", "0,0,l", "" },
+	{ "123\n456\n789\n", "-3,0,l", "" },
+	{ "123\n456\n789\n", "-3,-3,l", "" },
+	/* Line mode, negative length */
+	{ "123\n456\n789\n", "2,-2,l", "" },
+	{ "123\n456\n789\n", "-2,-3,l", "" },
+	/* No length */
+	{ "123\n456\n789\n", "1,,l", "456\n789\n" },
+	{ "123\n456\n789\n", "-2,,l", "456\n789\n" },
+};
+
+static struct {
+	const char *contents;
+	const char *args;
+	const char *value;
+	const char *contents2;
+} write_tests[] = {
+	/* Single character replace */
+	{ "123456789", "0,1", "a", "a23456789" },
+	{ "123456789", "-9,1", "a", "a23456789" },
+	{ "123456789", "0,-8", "a", "a23456789" },
+	{ "123456789", "-9,-8", "a", "a23456789" },
+	{ "123456789", "5,1", "b", "12345b789" },
+	{ "123456789", "-4,1", "b", "12345b789" },
+	{ "123456789", "5,-3", "b", "12345b789" },
+	{ "123456789", "-4,-3", "b", "12345b789" },
+	/* Replace 2 characters with 1 */
+	{ "123456789", "0,2", "c", "c3456789" },
+	{ "123456789", "-9,2", "c", "c3456789" },
+	{ "123456789", "0,-7", "c", "c3456789" },
+	{ "123456789", "-9,-7", "c", "c3456789" },
+	{ "123456789", "4,2", "d", "1234d789" },
+	{ "123456789", "-5,2", "d", "1234d789" },
+	{ "123456789", "4,-3", "d", "1234d789" },
+	{ "123456789", "-5,-3", "d", "1234d789" },
+	/* Truncate file */
+	{ "123456789", "5", "e", "12345e" },
+	{ "123456789", "5", "", "12345" },
+	{ "123456789", "-4", "e", "12345e" },
+	{ "123456789", "-4", "", "12345" },
+	/* Replace 1 character with 2 */
+	{ "123456789", "0,1", "fg", "fg23456789" },
+	{ "123456789", "0,-8", "fg", "fg23456789" },
+	{ "123456789", "-9,1", "fg", "fg23456789" },
+	{ "123456789", "-9,-8", "fg", "fg23456789" },
+};
+
+AST_TEST_DEFINE(test_func_file)
+{
+	int res = AST_TEST_PASS;
+	int i;
+	char dir[] = "/tmp/test_func_file.XXXXXX";
+	char file[80], expression[256];
+	struct ast_str *buf;
+	char fbuf[256];
+	FILE *fh;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "func_file";
+		info->category = "funcs/func_env";
+		info->summary = "Verify behavior of the FILE() dialplan function";
+		info->description =
+			"Verifies that the examples of the FILE() dialplan function documentation work as described.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	if (!mkdtemp(dir)) {
+		ast_test_status_update(test, "Cannot create temporary directory: %s\n", strerror(errno));
+		return AST_TEST_FAIL;
+	}
+
+	if (!(buf = ast_str_create(16))) {
+		rmdir(dir);
+		return AST_TEST_FAIL;
+	}
+
+	snprintf(file, sizeof(file), "%s/test.txt", dir);
+
+	for (i = 0; i < ARRAY_LEN(read_tests); i++) {
+		if (!(fh = fopen(file, "w"))) {
+			ast_test_status_update(test, "Cannot open test file: %s\n", strerror(errno));
+			unlink(file);
+			rmdir(dir);
+			return AST_TEST_FAIL;
+		}
+
+		if (fwrite(read_tests[i].contents, 1, strlen(read_tests[i].contents), fh) < strlen(read_tests[i].contents)) {
+			ast_test_status_update(test, "Cannot write initial values into test file: %s\n", strerror(errno));
+			fclose(fh);
+			unlink(file);
+			rmdir(dir);
+			return AST_TEST_FAIL;
+		}
+
+		fclose(fh);
+
+		snprintf(expression, sizeof(expression), "${FILE(%s,%s)}", file, read_tests[i].args);
+		ast_str_substitute_variables(&buf, 0, NULL, expression);
+
+		if (strcmp(ast_str_buffer(buf), read_tests[i].value)) {
+			if (strchr(read_tests[i].value, '\n')) {
+				ast_test_status_update(test, "Expression '${FILE(...,%s)}' did not produce the expected value\n", read_tests[i].args);
+			} else {
+				ast_test_status_update(test, "Expression '${FILE(...,%s)}' did not produce ('%s') the expected value ('%s')\n",
+					read_tests[i].args, ast_str_buffer(buf), read_tests[i].value);
+			}
+			res = AST_TEST_FAIL;
+		}
+	}
+
+	for (i = 0; i < ARRAY_LEN(write_tests); i++) {
+		if (!(fh = fopen(file, "w"))) {

[... 81 lines stripped ...]



More information about the svn-commits mailing list