trunk: changeset 38

Sat, 19 Jan 2002 19:14:36 +0100

author
tuomov
date
Sat, 19 Jan 2002 19:14:36 +0100
changeset 35
5a71d53d0228
parent 34
828f3afd5c76
child 36
63cd573ffbcf

trunk: changeset 38
Warning callbacks (thanks to Lukas Schroeder). libtu now depends on
the asprintf functions and one implementation is included in
snprintf_2.2/.

Makefile file | annotate | diff | comparison | revisions
README file | annotate | diff | comparison | revisions
include/libtu/optparser.h file | annotate | diff | comparison | revisions
include/libtu/output.h file | annotate | diff | comparison | revisions
include/libtu/parser.h file | annotate | diff | comparison | revisions
include/libtu/tokenizer.h file | annotate | diff | comparison | revisions
output.c file | annotate | diff | comparison | revisions
parser.c file | annotate | diff | comparison | revisions
snprintf_2.2/INSTALL file | annotate | diff | comparison | revisions
snprintf_2.2/LICENSE.txt file | annotate | diff | comparison | revisions
snprintf_2.2/Makefile.unused file | annotate | diff | comparison | revisions
snprintf_2.2/README file | annotate | diff | comparison | revisions
snprintf_2.2/README.html file | annotate | diff | comparison | revisions
snprintf_2.2/snprintf-orig.c file | annotate | diff | comparison | revisions
snprintf_2.2/snprintf.c file | annotate | diff | comparison | revisions
snprintf_2.2/snprintf.h file | annotate | diff | comparison | revisions
snprintf_2.2/test.c file | annotate | diff | comparison | revisions
tokenizer.c file | annotate | diff | comparison | revisions
--- a/Makefile	Sun Apr 01 01:43:46 2001 +0200
+++ b/Makefile	Sat Jan 19 19:14:36 2002 +0100
@@ -16,7 +16,7 @@
 DEFINES += -DLIBTU_NO_ERRMSG
 else
 ifndef HAS_SYSTEM_ASPRINTF
-OBJS += ../snprintf_2.2/snprintf.o
+OBJS += snprintf_2.2/snprintf.o
 else
 DEFINES += -DHAS_SYSTEM_ASPRINTF
 endif
--- a/README	Sun Apr 01 01:43:46 2001 +0200
+++ b/README	Sat Jan 19 19:14:36 2002 +0100
@@ -24,8 +24,8 @@
 ---
 
 Libtu needs the functions asprintf and vasprintf. These do not
-exist on most platforms. One implementation is available at
-<http://www.ijs.si/software/snprintf/>, which is used by default
-and is supposed to be found at ../snprintf_2.2. To use the
-system's versions, if available, modify system.mk.
+exist on most platforms. One implementation by Mark Martinec
+is included in snprintf_2.2/ and is used by default. To use the
+system's versions of these functions, if available, modify
+system.mk.
 
--- a/include/libtu/optparser.h	Sun Apr 01 01:43:46 2001 +0200
+++ b/include/libtu/optparser.h	Sat Jan 19 19:14:36 2002 +0100
@@ -57,7 +57,6 @@
 	const char *about;
 } OptParserCommonInfo;
 
-#define END_OPTPARSEROPTS {0, NULL, 0, NULL, NULL}
 
 enum{
 	OPT_ID_END=0,
--- a/include/libtu/output.h	Sun Apr 01 01:43:46 2001 +0200
+++ b/include/libtu/output.h	Sat Jan 19 19:14:36 2002 +0100
@@ -12,6 +12,8 @@
 
 #include "types.h"
 
+typedef void WarnHandler(const char *);
+extern WarnHandler *set_warn_handler(WarnHandler *handler);
 
 extern void verbose(const char *p, ...);
 extern void verbose_v(const char *p, va_list args);
--- a/include/libtu/parser.h	Sun Apr 01 01:43:46 2001 +0200
+++ b/include/libtu/parser.h	Sat Jan 19 19:14:36 2002 +0100
@@ -35,7 +35,6 @@
 	struct _ConfOpt *opts;
 } ConfOpt;
 
-#define END_CONFOPTS {NULL, NULL, NULL, NULL}
 
 extern bool parse_config_tokz(Tokenizer *tokz, const ConfOpt *options);
 extern bool parse_config(const char *fname, const ConfOpt *options, int flags);
--- a/include/libtu/tokenizer.h	Sun Apr 01 01:43:46 2001 +0200
+++ b/include/libtu/tokenizer.h	Sat Jan 19 19:14:36 2002 +0100
@@ -123,7 +123,9 @@
 	TOKZ_IGNORE_NEXTLINE=0x1,
 	TOKZ_READ_COMMENTS=0x2,
 	TOKZ_PARSER_INDENT_MODE=0x04,
-	TOKZ_ERROR_TOLERANT=0x8
+	TOKZ_ERROR_TOLERANT=0x8,
+	TOKZ_READ_FROM_BUFFER=0x10,
+	TOKZ_DEFAULT_OPTION=0x20
 };
 
 
@@ -163,6 +165,12 @@
 	Token ungettok;
 } Tokenizer_FInfo;
 
+typedef struct _Tokenizer_Buffer{
+		char *data;
+		int len;
+		int pos;
+} Tokenizer_Buffer;
+	
 typedef struct _Tokenizer{
 	FILE *file;
 	char *name;
@@ -170,6 +178,8 @@
 	int ungetc;
 	Token ungettok;
 	
+	Tokenizer_Buffer buffer;
+
 	int flags;
 	const struct _ConfOpt **optstack;
 	int nest_lvl;
@@ -184,6 +194,7 @@
 
 extern Tokenizer *tokz_open(const char *fname);
 extern Tokenizer *tokz_open_file(FILE *file, const char *fname);
+extern Tokenizer *tokz_prepare_buffer(char *buffer, int len);
 extern void tokz_close(Tokenizer *tokz);
 extern bool tokz_get_token(Tokenizer *tokz, Token *tok);
 extern void tokz_unget_token(Tokenizer *tokz, Token *tok);
--- a/output.c	Sun Apr 01 01:43:46 2001 +0200
+++ b/output.c	Sat Jan 19 19:14:36 2002 +0100
@@ -15,7 +15,7 @@
 #include <libtu/output.h>
 #include <libtu/util.h>
 
-#if !defined(LIBTU_NO_ERRMSG) && !defined(HAS_SYSTEM_ASPRINTF)
+#if !defined(HAS_SYSTEM_ASPRINTF)
 #include "../snprintf_2.2/snprintf.h"
 #endif
 
@@ -26,11 +26,14 @@
 static bool verbose_mode=FALSE;
 static int verbose_indent_lvl=0;
 static bool progname_enable=TRUE;
+static WarnHandler *current_warn_handler=NULL;
 
 #define INDENTATOR_LENGTH 4
 
 static char indentator[]={' ', ' ', ' ', ' '};
 
+static void do_dispatch_message(const char *message);
+
 
 void verbose(const char *p, ...)
 {
@@ -99,11 +102,29 @@
 /* warn
  */
 
+
+static void fallback_warn()
+{
+	put_prog_name();
+	fprintf(stderr, "Oops. Error string compilation failed: %s",
+			strerror(errno));
+}
+	
+	
 #define CALL_V(NAME, ARGS) \
-	va_list args; va_start(args, p); NAME ARGS; va_end(args);
+	do { va_list args; va_start(args, p); NAME ARGS; va_end(args); } while(0)
 
+#define DO_DISPATCH(NAME, ARGS) \
+	do{ \
+		char *msg; \
+		if((msg=NAME ARGS)!=NULL){ \
+			do_dispatch_message(msg); \
+			free(msg);\
+		}else{ \
+			fallback_warn(); \
+		} \
+	}while(0)
 
-#ifndef LIBTU_NO_ERRMSG
 
 void libtu_asprintf(char **ret, const char *p, ...)
 {
@@ -116,7 +137,6 @@
 	vasprintf(ret, p, args);
 }
 
-#endif
 
 void warn(const char *p, ...)
 {
@@ -144,69 +164,39 @@
 
 void warn_v(const char *p, va_list args)
 {
-	put_prog_name();
-	vfprintf(stderr, p, args);
-	putc('\n', stderr);
+	DO_DISPATCH(errmsg_v, (p, args));
 }
 
 
 void warn_obj_line_v(const char *obj, int line, const char *p, va_list args)
 {
-	put_prog_name();
-	if(obj!=NULL){
-		if(line>0)
-			fprintf(stderr, TR("%s:%d: "), obj, line);
-		else		
-			fprintf(stderr, "%s: ", obj);
-	}else{
-		if(line>0)
-			fprintf(stderr, TR("%d: "), line);
-	}
-	vfprintf(stderr, p, args);
-	putc('\n', stderr);
+	DO_DISPATCH(errmsg_obj_line_v, (obj, line, p, args));
 }
 
 
 void warn_err()
 {
-	put_prog_name();
-	fprintf(stderr, "%s\n", strerror(errno));
+	DO_DISPATCH(errmsg_err, ());
 }
 
 
 void warn_err_obj(const char *obj)
 {
-	put_prog_name();
-	if(obj!=NULL)
-		fprintf(stderr, "%s: %s\n", obj, strerror(errno));
-	else
-		fprintf(stderr, "%s\n", strerror(errno));
+	DO_DISPATCH(errmsg_err_obj, (obj));
 }
 
 void warn_err_obj_line(const char *obj, int line)
 {
-	put_prog_name();
-	if(obj!=NULL){
-		if(line>0)
-			fprintf(stderr, TR("%s:%d: %s\n"), obj, line, strerror(errno));
-		else
-			fprintf(stderr, "%s: %s\n", obj, strerror(errno));
-	}else{
-		if(line>0)
-			fprintf(stderr, TR("%d: %s\n"), line, strerror(errno));
-		else
-			fprintf(stderr, TR("%s\n"), strerror(errno));
-	}
-
+	DO_DISPATCH(errmsg_err_obj_line, (obj, line));
 }
 
 
 /* errmsg
  */
-#ifndef LIBTU_NO_ERRMSG
 
 #define CALL_V_RET(NAME, ARGS) \
-	char *ret; va_list args; va_start(args, p); ret=NAME ARGS; va_end(args); return ret;
+	char *ret; va_list args; va_start(args, p); ret=NAME ARGS; \
+	va_end(args); return ret;
 
 
 char* errmsg(const char *p, ...)
@@ -244,6 +234,7 @@
 char *errmsg_obj_line_v(const char *obj, int line, const char *p, va_list args)
 {
 	char *res1=NULL, *res2, *res3;
+	
 	if(obj!=NULL){
 		if(line>0)
 			asprintf(&res1, TR("%s:%d: "), obj, line);
@@ -253,7 +244,7 @@
 		if(line>0)
 			asprintf(&res1, TR("%d: "), line);
 	}
-	asprintf(&res2, p, args);
+	vasprintf(&res2, p, args);
 	if(res1!=NULL){
 		if(res2==NULL)
 			return NULL;
@@ -302,20 +293,21 @@
 	return res;
 }
 
-#endif /* LIBTU_NO_ERRMSG */
-
 
 /* die
  */
 
+
 void die(const char *p, ...)
 {
+	set_warn_handler(NULL);
 	CALL_V(die_v, (p, args));
 }
 
 
 void die_v(const char *p, va_list args)
 {
+	set_warn_handler(NULL);
 	warn_v(p, args);
 	exit(EXIT_FAILURE);
 }
@@ -323,18 +315,21 @@
 
 void die_obj(const char *obj, const char *p, ...)
 {
+	set_warn_handler(NULL);
 	CALL_V(die_obj_v, (obj, p, args));
 }
 
 
 void die_obj_line(const char *obj, int line, const char *p, ...)
 {
+	set_warn_handler(NULL);
 	CALL_V(die_obj_line_v, (obj, line, p, args));
 }
 
 
 void die_obj_v(const char *obj, const char *p, va_list args)
 {
+	set_warn_handler(NULL);
 	warn_obj_v(obj, p, args);
 	exit(EXIT_FAILURE);
 }
@@ -342,6 +337,7 @@
 
 void die_obj_line_v(const char *obj, int line, const char *p, va_list args)
 {
+	set_warn_handler(NULL);
 	warn_obj_line_v(obj, line, p, args);
 	exit(EXIT_FAILURE);
 }
@@ -349,6 +345,7 @@
 
 void die_err()
 {
+	set_warn_handler(NULL);
 	warn_err();
 	exit(EXIT_FAILURE);
 }
@@ -356,6 +353,7 @@
 
 void die_err_obj(const char *obj)
 {
+	set_warn_handler(NULL);
 	warn_err_obj(obj);
 	exit(EXIT_FAILURE);
 }
@@ -363,6 +361,32 @@
 
 void die_err_obj_line(const char *obj, int line)
 {
+	set_warn_handler(NULL);
 	warn_err_obj_line(obj, line);
 	exit(EXIT_FAILURE);
 }
+
+
+static void default_warn_handler(const char *message)
+{
+	put_prog_name();
+	fprintf(stderr, "%s", message);
+	putc('\n', stderr);
+}
+
+
+static void do_dispatch_message(const char *message)
+{
+	if(current_warn_handler!=NULL)
+		current_warn_handler(message);
+	else
+		default_warn_handler(message);
+}
+
+
+WarnHandler *set_warn_handler(WarnHandler *handler)
+{
+	WarnHandler *old=current_warn_handler;
+	current_warn_handler=handler;
+	return old;
+}
--- a/parser.c	Sun Apr 01 01:43:46 2001 +0200
+++ b/parser.c	Sat Jan 19 19:14:36 2002 +0100
@@ -216,6 +216,7 @@
 	int init_nest_lvl;
 	bool had_error;
 	int errornest=0;
+	bool is_default=FALSE;
 
 	/* Allocate tokz->optstack if it does not yet exist (if it does,
 	 * we have been called from an option handler)
@@ -283,11 +284,15 @@
 								  TOK_IDENT_VAL(tokens+0));
 			if(options==NULL)
 				options=lookup_option(common_opts, TOK_IDENT_VAL(tokens+0));
+			if(options==NULL && (tokz->flags&TOKZ_DEFAULT_OPTION)){
+				options=lookup_option(tokz->optstack[tokz->nest_lvl], "#default");
+				is_default=(options!=NULL);
+			}
 
 			if(options==NULL){
 				had_error=TRUE;
 				tokz_warn_error(tokz, tokens->line, E_TOKZ_UNKNOWN_OPTION);
-			}else{			
+			}else if(!is_default) {			
 				had_error=!check_args(tokz, tokens, ntokens, options->argfmt);
 			}
 			
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snprintf_2.2/INSTALL	Sat Jan 19 19:14:36 2002 +0100
@@ -0,0 +1,24 @@
+HOW TO INSTALL - manually:
+
+  1. Read the description of macros that control the bahaviour
+     of the program at the beginning of the file snprintf.c,
+     change the definitions in snprintf.c or in Makefile if necessary.
+
+  2. make
+
+  3. move the file snprintf.o where your programs will find it
+
+
+
+HOW TO INSTALL - with autoconf:
+
+Contributed by Caolan McNamara <Caolan.McNamara@ul.ie>:
+
+  Though it might be overkill for snprintf I also have
+  an autoconf and automaked version which works out the need
+  for long long support and makes snprintf optionally into
+  a dynamic library on libtool supported platforms.
+
+  1. cd with_autoconf
+
+  2. follow instructions in the file INSTALL there.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snprintf_2.2/LICENSE.txt	Sat Jan 19 19:14:36 2002 +0100
@@ -0,0 +1,121 @@
+The Frontier Artistic License Version 1.0
+Derived from the Artistic License at OpenSource.org.
+Submitted to OpenSource.org for Open Source Initiative certification.
+   
+Preamble
+
+The intent of this document is to state the conditions under which a
+Package may be copied, such that the Copyright Holder maintains some
+semblance of artistic control over the development of the package,
+while giving the users of the package the right to use and distribute
+the Package in a more-or-less customary fashion, plus the right to
+make reasonable modifications.
+   
+Definitions
+
+  "Package" refers to the script, suite, file, or collection of
+  scripts, suites, and/or files distributed by the Copyright Holder,
+  and to derivatives of that Package created through textual modification.
+
+  "Standard Version" refers to such a Package if it has not been
+  modified, or has been modified in accordance with the wishes of
+  the Copyright Holder.
+
+  "Copyright Holder" is whoever is named in the copyright statement
+  or statements for the package.
+
+  "You" is you, if you're thinking about copying or distributing
+  this Package.
+
+  "Reasonable copying fee" is whatever you can justify on the basis
+  of media cost, duplication charges, time of people involved, and
+  so on. (You will not be required to justify it to the Copyright
+  Holder, but only to the computing community at large as a market
+  that must bear the fee.)
+
+  "Freely Available" means that no fee is charged for the item
+  itself, though there may be fees involved in handling the item.
+  It also means that recipients of the item may redistribute it under
+  the same conditions they received it.
+       
+
+Terms
+
+1. You may make and give away verbatim copies of the source form of
+the Standard Version of this Package without restriction, provided
+that you duplicate all of the original copyright notices and
+associated disclaimers.
+   
+2. You may apply bug fixes, portability fixes, and other modifications
+derived from the Public Domain or from the Copyright Holder. A Package
+modified in such a way shall still be considered the Standard Version.
+   
+3. You may otherwise modify your copy of this Package in any way,
+provided that you insert a prominent notice in each changed script,
+suite, or file stating how and when you changed that script, suite,
+or file, and provided that you do at least ONE of the following:
+   
+  a) Use the modified Package only within your corporation or
+  organization, or retain the modified Package solely for personal use.
+     
+  b) Place your modifications in the Public Domain or otherwise make
+  them Freely Available, such as by posting said modifications to Usenet
+  or an equivalent medium, or placing the modifications on a major archive
+  site such as ftp.uu.net, or by allowing the Copyright Holder to include
+  your modifications in the Standard Version of the Package.
+     
+  c) Rename any non-standard executables so the names do not conflict
+  with standard executables, which must also be provided, and provide
+  a separate manual page (or equivalent) for each non-standard executable
+  that clearly documents how it differs from the Standard Version.
+     
+  d) Make other distribution arrangements with the Copyright Holder.
+     
+4. You may distribute the programs of this Package in object code or
+executable form, provided that you do at least ONE of the following:
+   
+  a) Distribute a Standard Version of the executables and library
+  files, together with instructions (in the manual page or
+  equivalent) on where to get the Standard Version.
+     
+  b) Accompany the distribution with the machine-readable source of
+  the Package with your modifications.
+     
+  c) Accompany any non-standard executables with their corresponding
+  Standard Version executables, give the non-standard executables
+  non-standard names, and clearly document the differences in manual
+  pages (or equivalent), together with instructions on where to get
+  the Standard Version.
+     
+  d) Make other distribution arrangements with the Copyright Holder.
+     
+5. You may charge a reasonable copying fee for any distribution of
+this Package. You may charge any fee you choose for support of this
+Package. You may not charge a fee for this Package itself. However,
+you may distribute this Package in aggregate with other (possibly
+commercial) programs as part of a larger (possibly commercial)
+software distribution provided that you do not advertise this Package
+as a product of your own.
+   
+6. The scripts and library files supplied as input to or produced as
+output from the programs of this Package do not automatically fall
+under the copyright of this Package, but belong to whomever generated
+them, and may be sold commercially, and may be aggregated with this
+Package.
+   
+7. Scripts, suites, or programs supplied by you that depend on or
+otherwise make use of this Package shall not be considered part of
+this Package.
+   
+8. The name of the Copyright Holder may not be used to endorse or
+promote products derived from this software without specific prior
+written permission.
+   
+9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+   
+                        The End
+
+
+http://www.spinwardstars.com/frontier/fal.html
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snprintf_2.2/Makefile.unused	Sat Jan 19 19:14:36 2002 +0100
@@ -0,0 +1,43 @@
+# Make sure you include -DHAVE_SNPRINTF in CFLAGS if your system
+# does have snprintf!
+
+# If you need (long long int) support and you sprintf supports it,
+# define -DSNPRINTF_LONGLONG_SUPPORT
+
+CC      = gcc
+
+CFLAGS  = -DPREFER_PORTABLE_SNPRINTF -O3 \
+	  -Wall -Wpointer-arith -Wwrite-strings \
+	  -Wcast-qual -Wcast-align -Waggregate-return \
+	  -Wmissing-prototypes -Wmissing-declarations \
+	  -Wshadow -Wstrict-prototypes
+
+#	  -DNEED_ASPRINTF -DNEED_ASNPRINTF -DNEED_VASPRINTF -DNEED_VASNPRINTF
+#	  -DNEED_SNPRINTF_ONLY
+
+# Digital Unix: native compiler usually produces better code than gcc
+#CC     = cc
+#CFLAGS = -DPREFER_PORTABLE_SNPRINTF -O4 -std1 -arch host
+
+# Recommend to leave COMPATIBILITY empty for normal use.
+# Should be set for bug compatibility when running tests
+# too keep them less chatty.
+COMPATIBILITY =
+
+#COMPATIBILITY = -DSOLARIS_BUG_COMPATIBLE
+#COMPATIBILITY = -DHPUX_BUG_COMPATIBLE
+#COMPATIBILITY = -DDIGITAL_UNIX_BUG_COMPATIBLE
+#COMPATIBILITY = -DPERL_BUG_COMPATIBLE