Problems with using cl.ukey in case statement
Petr Skoda wrote on Jan 12, 2010
Hi Mike,
several years ago we have discussed the interactive prompting using cl.ukey in loop here:
http://iraf.net/phpBB2/viewtopic.php?p=136735
I have written some script using it but today I have realized by a mistake in input that in fact it does not work correctly. After more detailed investigation it seems that there is something strange about the handling of the special characters in the solution you have suggested.
I have extracted the core of the logic in following test program
For most letters it works correctly but it does not react to '?' as expected - as it is in ASCII table before 'a' so it is trapped as well as ENTER (\015) and more other codes (it is not desired effect anyway).
But when I change the trap condition for characters lower (in ASCII code position) then 'a' the system prints an error when entering
ENTER (\015) and SPACE (\040)
After some experiments it seems that the critical boundary is the '\' character - but it may not be put in the test at line 6 (complaining
immediately :
If giving the ']' character (next higher than backslash)
it works - it prints
(for ENTER)
or
for SPACE bar
If giving the first lower char that backslash - the '[' - it starts to complain
So it is clean that the condition on line 6 does not trap the special characters and it somehow intrudes the second part after else
It does not help even the condition
Can you explain this strange behaviour ? Is there some solution for trapping fully the control characters (like ENTER SPACE) ?
Is there some function in IRAF that would allow to make the scancode number from the ukey input - so I could compare just integer numbers without the strange side effects ?
Thanks
Petr Skoda
several years ago we have discussed the interactive prompting using cl.ukey in loop here:
http://iraf.net/phpBB2/viewtopic.php?p=136735
I have written some script using it but today I have realized by a mistake in input that in fact it does not work correctly. After more detailed investigation it seems that there is something strange about the handling of the special characters in the solution you have suggested.
I have extracted the core of the logic in following test program
1 procedure testukey.cl
2
3 begin
4 string ch
5 for (ch = "c"; ch != "z"; ch = cl.ukey) {
6 if (ch < "a") {
7 print (ch)
8 print ("got a newline")
9 } else {
10 print (ch)
11 switch (ch) {
12 case "?": print ("help for commands here...")
13 case "h": print ("imheader (imname, long+)")
14 case "e": print ("imexamine (imname, 1)")
15 case "d": print ("display (imname, 1, fill+)")
16 case "n": break # next image
17 case "q": goto cleanup
18 case "s": print ("imstat (imname)")
19 }
20 printf ("Command? ")
21 }
22 }
23 print('finishing loop')
24 cleanup:
25 printf("\nfinished \n-------------\n")
26 end
For most letters it works correctly but it does not react to '?' as expected - as it is in ASCII table before 'a' so it is trapped as well as ENTER (\015) and more other codes (it is not desired effect anyway).
But when I change the trap condition for characters lower (in ASCII code position) then 'a' the system prints an error when entering
ENTER (\015) and SPACE (\040)
After some experiments it seems that the critical boundary is the '\' character - but it may not be put in the test at line 6 (complaining
immediately :
ERROR on line 6: Newline while processing string
testukey ()
If giving the ']' character (next higher than backslash)
it works - it prints
\015
got a newline
(for ENTER)
or
\040
got a newline
for SPACE bar
If giving the first lower char that backslash - the '[' - it starts to complain
c
Command? \015
ERROR on line 26: Illegal switch value.
testukey ()
So it is clean that the condition on line 6 does not trap the special characters and it somehow intrudes the second part after else
It does not help even the condition
if (ch =="\015")
Can you explain this strange behaviour ? Is there some solution for trapping fully the control characters (like ENTER SPACE) ?
Is there some function in IRAF that would allow to make the scancode number from the ukey input - so I could compare just integer numbers without the strange side effects ?
Thanks
Petr Skoda
Mike Fitzpatrick wrote on Jan 12, 2010
The thing to remember is that the backslash is used as an escape, and so it needs to be escaped itself. Its a kludge, but try something like
-Mike
begin
char ch
while (yes) {
ch = cl.ukey
if (substr(ch,1,4) == "\\015") {
print ("do newline")
} else if (substr(ch,1,4) == "\\040") {
print ("do spacebar")
} else {
switch (ch) {
case "q": break
case "a": print ("got an 'a'")
case "?": print ("do help")
case "\\": print ("do backslash")
}
}
}
end
-Mike
Petr Skoda wrote on Jan 12, 2010
Thanks Mike!
It works nicely for me - I did not realize the representation of a scancode is really a normal string - I was thinking about a special treatment of control codes...
Petr Skoda
It works nicely for me - I did not realize the representation of a scancode is really a normal string - I was thinking about a special treatment of control codes...
Petr Skoda
Last post on Jan 12, 2010