Subject: syntax improvement: expect options before the filename <p>start at documentation

From: "[email-masked]" <jan.nijtmans>
Date: Wed, 24 Oct 2012 21:22:30 +0000

---
 doc/Load.3         |  3 ++-
 doc/load.n         | 15 ++++++++++++---
 generic/tclLoad.c  | 29 ++++++++++++++---------------
 tests/load.test    | 18 +++++++++---------
 unix/tclLoadDl.c   |  2 +-
 unix/tclLoadNext.c |  2 +-
 6 files changed, 39 insertions(+), 30 deletions(-)

diff --git a/doc/Load.3 b/doc/Load.3
index c088f32..9602b77 100644
--- a/doc/Load.3
+++ b/doc/Load.3
@@ -31,7 +31,8 @@ Array of names of symbols to be resolved during the load of the library, or
 NULL if no symbols are to be resolved. If an array is given, the last entry in
 the array must be NULL.
 .AP int flags in
-Reserved for future expansion. Must be 0.
+The value should normally be 0, but \fITCL_LOAD_GLOBALfR or \fITCL_LOAD_LAZYfR
+or a combination of those two is allowed as well.
 .AP void *procPtrs out
 Points to an array that will hold the addresses of the functions described in
 the \fIsymbols\fR argument. Should be NULL if no symbols are to be resolved.
diff --git a/doc/load.n b/doc/load.n
index c32cb65..7f7c624 100644
--- a/doc/load.n
+++ b/doc/load.n
@@ -11,11 +11,11 @@
 .SH NAME
 load \- Load machine code and initialize new commands
 .SH SYNOPSIS
-\fBload \fIfileName\fR
+\fBload\fR ?\fB\-global\fR? ?\fB\-lazy\fR? ?\fB\-\-\fR? \fIfileName\fR
 .br
-\fBload \fIfileName packageName\fR
+\fBload ?\fB\-global\fR? ?\fB\-lazy\fR? ?\fB\-\-\fR? \fIfileName packageName\fR
 .br
-\fBload \fIfileName packageName interp\fR
+\fBload ?\fB\-global\fR? ?\fB\-lazy\fR? ?\fB\-\-\fR? \fIfileName packageName interp\fR
 .BE
 .SH DESCRIPTION
 .PP
@@ -104,6 +104,15 @@ Otherwise, the \fBload\fR command searches for a dynamically loaded
 package by that name, and uses it if it is found.  If several
 different files have been \fBload\fRed with different versions of
 the package, Tcl picks the file that was loaded first.
+.PP
+If \fB\-global\fR is specified preceding the filename, all symbols
+found in the shared library are exported for global use by other
+libraries. The option \fB\-lazy\fR delays the actual loading of
+symbols until their first actual use. The options may be abbreviated.
+The option \fB\-\-\fR indicates the end of the options, and should
+be used if you wish to use a filename which starts with \fB\-\fR.
+On platforms which do not support the \fB\-global\fR or \fB\-lazy\fR
+options, the options still exist but have no effect.
 .SH "PORTABILITY ISSUES"
 .TP
 \fBWindows\fR\0\0\0\0\0
diff --git a/generic/tclLoad.c b/generic/tclLoad.c
index f8186d5..22cfc65 100644
--- a/generic/tclLoad.c
+++ b/generic/tclLoad.c
@@ -135,38 +135,37 @@ Tcl_LoadObjCmd(
     int index, flags = 0;
     Tcl_Obj *const *savedobjv = objv;
     static const char *const options[] = {
-	"-global",		"-lazy",	NULL
+	"-global",		"-lazy",		"--",	NULL
     };
     enum options {
-	LOAD_GLOBAL,	LOAD_LAZY
+	LOAD_GLOBAL,	LOAD_LAZY,	LOAD_LAST
     };
 
     while (objc > 2) {
-	if (TclGetString(objv[2])[0] != '-') {
+	if (TclGetString(objv[1])[0] != '-') {
 	    break;
 	}
-	if (Tcl_GetIndexFromObj(interp, objv[2], options, "option", 0,
+	if (Tcl_GetIndexFromObj(interp, objv[1], options, "option", 0,
 		&index) != TCL_OK) {
 	    return TCL_ERROR;
 	}
 	++objv; --objc;
-	switch ((enum options) index) {
-	case LOAD_GLOBAL:
+	if (LOAD_GLOBAL == (enum options) index) {
 	    flags |= 1;
-	    break;
-	case LOAD_LAZY:
+	} else if (LOAD_LAZY == (enum options) index) {
 	    flags |= 2;
-	    break;
+	} else {
+		break;
 	}
     }
     if ((objc < 2) || (objc > 4)) {
-	Tcl_WrongNumArgs(interp, 1, savedobjv, "fileName ?-global? ?-lazy? ?packageName? ?interp?");
+	Tcl_WrongNumArgs(interp, 1, savedobjv, "?-global? ?-lazy? ?--? fileName ?packageName? ?interp?");
 	return TCL_ERROR;
     }
-    if (Tcl_FSConvertToPathType(interp, savedobjv[1]) != TCL_OK) {
+    if (Tcl_FSConvertToPathType(interp, objv[1]) != TCL_OK) {
 	return TCL_ERROR;
     }
-    fullFileName = Tcl_GetString(savedobjv[1]);
+    fullFileName = Tcl_GetString(objv[1]);
 
     Tcl_DStringInit(&pkgName);
     Tcl_DStringInit(&initName);
@@ -323,7 +322,7 @@ Tcl_LoadObjCmd(
 		 * that.
 		 */
 
-		splitPtr = Tcl_FSSplitPath(savedobjv[1], &pElements);
+		splitPtr = Tcl_FSSplitPath(objv[1], &pElements);
 		Tcl_ListObjIndex(NULL, splitPtr, pElements -1, &pkgGuessPtr);
 		pkgGuess = Tcl_GetString(pkgGuessPtr);
 		if ((pkgGuess[0] == 'l') && (pkgGuess[1] == 'i')
@@ -391,7 +390,7 @@ Tcl_LoadObjCmd(
 	symbols[1] = NULL;
 
 	Tcl_MutexLock(&packageMutex);
-	code = Tcl_LoadFile(interp, savedobjv[1], symbols, flags, &initProc,
+	code = Tcl_LoadFile(interp, objv[1], symbols, flags, &initProc,
 		&loadHandle);
 	Tcl_MutexUnlock(&packageMutex);
 	if (code != TCL_OK) {
@@ -417,7 +416,7 @@ Tcl_LoadObjCmd(
 	pkgPtr->unloadProc	   = (Tcl_PackageUnloadProc *)
 		Tcl_FindSymbol(interp, loadHandle,
 			Tcl_DStringValue(&unloadName));
-	pkgPtr->safeUnloadProc	   = (Tcl_PackageUnloadProc *) 
+	pkgPtr->safeUnloadProc	   = (Tcl_PackageUnloadProc *)
 		Tcl_FindSymbol(interp, loadHandle,
 			Tcl_DStringValue(&safeUnloadName));
 	pkgPtr->interpRefCount	   = 0;
diff --git a/tests/load.test b/tests/load.test
index 8bd2291..19303ce 100644
--- a/tests/load.test
+++ b/tests/load.test
@@ -47,35 +47,35 @@ testConstraint testsimplefilesystem \
 
 test load-1.1 {basic errors} {} {
     list [catch {load} msg] $msg
-} "1 {wrong \# args: should be \"load fileName ?-global? ?-lazy? ?packageName? ?interp?\"}"
+} "1 {wrong \# args: should be \"load ?-global? ?-lazy? ?--? fileName ?packageName? ?interp?\"}"
 test load-1.2 {basic errors} {} {
     list [catch {load a b c d} msg] $msg
-} "1 {wrong \# args: should be \"load fileName ?-global? ?-lazy? ?packageName? ?interp?\"}"
+} "1 {wrong \# args: should be \"load ?-global? ?-lazy? ?--? fileName ?packageName? ?interp?\"}"
 test load-1.3 {basic errors} {} {
     list [catch {load a b foobar} msg] $msg
 } {1 {could not find interpreter "foobar"}}
 test load-1.4 {basic errors} {} {
-    list [catch {load {} -global} msg] $msg
+    list [catch {load -global {}} msg] $msg
 } {1 {must specify either file name or package name}}
 test load-1.5 {basic errors} {} {
-    list [catch {load {} -lazy {}} msg] $msg
+    list [catch {load -lazy {} {}} msg] $msg
 } {1 {must specify either file name or package name}}
 test load-1.6 {basic errors} {} {
     list [catch {load {} Unknown} msg] $msg
 } {1 {package "Unknown" isn't loaded statically}}
 test load-1.7 {basic errors} {} {
-    list [catch {load foo -abc} msg] $msg
-} "1 {bad option \"-abc\": must be -global or -lazy}"
+    list [catch {load -abc foo} msg] $msg
+} "1 {bad option \"-abc\": must be -global, -lazy, or --}"
 
 test load-2.1 {basic loading, with guess for package name} \
 	[list $dll $loaded] {
-    load [file join $testDir pkga$ext] -global
+    load -global [file join $testDir pkga$ext]
     list [pkga_eq abc def] [lsort [info commands pkga_*]]
 } {0 {pkga_eq pkga_quote}}
 interp create -safe child
 test load-2.2 {loading into a safe interpreter, with package name conversion} \
 	[list $dll $loaded] {
-    load [file join $testDir pkgb$ext] -lazy pKgB child
+    load -lazy [file join $testDir pkgb$ext] pKgB child
     list [child eval pkgb_sub 44 13] [catch {child eval pkgb_unsafe} msg] $msg \
 	    [catch {pkgb_sub 12 10} msg2] $msg2
 } {31 1 {invalid command name "pkgb_unsafe"} 1 {invalid command name "pkgb_sub"}}
@@ -129,7 +129,7 @@ test load-5.1 {file name not specified and no static package: pick default} \
 	[list $dll $loaded] {
     catch {interp delete x}
     interp create x
-    load [file join $testDir pkga$ext] -global pkga
+    load -global [file join $testDir pkga$ext] pkga
     load {} pkga x
     set result [info loaded x]
     interp delete x
diff --git a/unix/tclLoadDl.c b/unix/tclLoadDl.c
index 9c021e1..267067f 100644
--- a/unix/tclLoadDl.c
+++ b/unix/tclLoadDl.c
@@ -164,7 +164,7 @@ FindSymbol(
     const char *native;		/* Name of the library to be loaded, in
 				 * system encoding */
     Tcl_DString newName, ds;	/* Buffers for converting the name to
-				 * system encoding and prepending an 
+				 * system encoding and prepending an
 				 * underscore*/
     void *handle = (void *) loadHandle->clientData;
 				/* Native handle to the loaded library */
diff --git a/unix/tclLoadNext.c b/unix/tclLoadNext.c
index f5911f8..484b1d6 100644
--- a/unix/tclLoadNext.c
+++ b/unix/tclLoadNext.c
@@ -134,7 +134,7 @@ FindSymbol(
     const char *symbol)
 {
     Tcl_PackageInitProc *proc = NULL;
- 
+
    if (symbol) {
 	char sym[strlen(symbol) + 2];
 
-- 
cgit v0.12