Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I print to console in C when command syntax is wrong? 1

Status
Not open for further replies.

KotiChennayya

Programmer
Oct 3, 2003
15
0
0
IN
Hi All,
I have a command iseq for comparing 2 numbers.
I am using it in a script & i will be calling the script in Tcl_EvalFile().I will be checking whether all the parameters are present.If not I want to print error message to std output.Here I dont want to use printf().I want to simulate the senario exactly like tclsh whenever invalid arguments are issued.for ex:
tclsh>join
wrong # args: should be "join list ?joinString?"
This kind message should appear for iseq command when I use it in script.How can I do this?
interp->result,Tcl_SetObjResult,Tcl_WrongNumArgs will store it in result object of interp,but wont display it on std output.
Is there any way to do this?
Thanks in advance
Koti
 
I guess I'm a little bit confused as to what you need based on your description.

For starters, with procedures defined with Tcl code, the Tcl interpreter automatically handles checking whether you've provided the proper number of arguments (unless you've created a procedure with a variable number of arguments using the args parameter). So, if you attempt to call your Tcl procedure from C code and don't provide it with the right number of arguments, you'll get a TCL_ERROR return code from your Tcl_EvalFile() call, with the error message stored in the return object/string. You can then do anything you want with it, including printing it out to [tt]stdout[/tt] (or [tt]stderr[/tt], as I'd prefer in this case).

On the other hand, if you've got a procedure defined as Tcl code, rather than C code, and it accepts a variable number of arguments, then you'll have to do your own check to verify that you've got enough arguments. If not, the standard way of signalling the type of situation you've described is by raising an error condition. (That's what join is doing in your example.) You can raise an error condition in Tcl code by executing the error command, providing it with your error message as an argument. For example:

Code:
proc iseq {args} {
  if {[llength $args] < 2} {
    error &quot;Wrong # args: should be &quot;iseq val val ?val...?&quot;
  }
  # Remainder of procedure
}

Once again, your Tcl_EvalFile() call would return a TCL_ERROR return code if iseq didn't get enough arguments, with the error message stored in the return object/string.

Or, am I just completely missing what you're asking for?

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
This is perfectly fine.But my doubt is not that.I will parse through the arguments & if any argument is missing I have to inform to the user.I will be parsing arguments in C not in TCL script.
So if I say just
%load mydll.dll
%set a 2
%set b 3
%iseq $a
&quot;wrong # args: should be &quot;iseq intval intval&quot;
%iseq $a $b
TRUE
%
The error message&quot;wrong # args: should be &quot;iseq intval intval&quot; I can see on console.But when I do Tcl_EvalFile() this error message wont be printed on console.When iseq command is exicuted, it will call corresponding C function.Inside this C function I will parse cmd line
arguments.So here if the arguments are wrong I will return TCL_ERROR & set the interp->result = &quot;wrong # args: should be &quot;iseq intval intval&quot;.How can I print this message without using printf when I evaluate the file containg above commands?Is it possible or I have to do it only using printf?
Thanks in advance.
Koti


 
That's right, it's a feature of the tclsh interpreter to print the error message to the console (I believe on the [tt]stderr[/tt] channel, but I'm not positive). If you're embedding a Tcl interpreter in your own custom C/C++ code, then you're responsible for reporting error conditions like that. So when your Tcl_EvalFile() call returns a TCL_ERROR return code, you'll need to retrieve result object/string and handle it however you want to. You could either printf it, or you could Tcl_Eval() an appropriate puts command. But otherwise, nothing will show up on the console unless you cause it to appear there.

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top