### Analysis of the Signature Code by Wunaike
Quoted from the signature of Wunaike on the China DOS Union forum
This signature's code is applied in WinNT series systems (such as WinXP). Copy and paste the code content into the "Start - Run" dialog box, and you can see the signature of Brother Wunaike displayed character by character. The code is only 132B long, but it contains many batch programming skills, some of which I only gradually noticed later. Now I will divide this code into sections and briefly hint at the important skills used, to entertain readers. Also, please allow Brother Wunaike, the original author, and other batch processing experts to criticize and make corrections.
1. %ComSpec% /c
The purpose of using it is to call internal commands such as set / for / pause in the Run dialog box. Because they are supported by the main program of the command line environment in NT series operating systems - the "internal" of cmd, and can only be used within the "internal" of cmd. Cmd is full name cmd.exe, located in the %SystemRoot%\system32\ path, and its complete path is indicated by the environment variable %ComSpec%. Because it can only run in the NT series environment, in fact, it is approximately equivalent to cmd /c. But because %ComSpec% contains the path where cmd.exe is located, it can also be used in special system environments where the location of cmd.exe is special or the %path% variable is missing. Detailed content can be found by using cmd /? in the command line environment.
Because "Run" is outside cmd, only "external commands" with independent executable files can be called, and the cmd "external command" acts as a "mouthpiece" for internal commands, passing a string of "internal commands" as the command line parameters of cmd, and passing them to the internal of cmd for interpretation and execution by the option switch /c. Generally, a space is inserted between /c and the subsequent attached command string to increase the readability of the code; Brother Wunaike omits the space, which is the embodiment of the requirements he mentioned of "trying to make the characters as short as possible" and "trying to be obscure and hard to read". And if used in the command line environment and cmd is used instead of %ComSpec%, the space between cmd and /c can also be omitted. These usages of omitting spaces are sometimes not simply for the purpose of streamlining the code, but to deal with the relatively weak string analysis algorithms of some commands or programs - sometimes we need to treat the entire command line as a string, and many command line programs will use spaces as the end mark of a string, such as the (set) part in the for command. Of course, this kind of usage that is compatible with both spaces and no spaces reflects its flexible characteristics of being inclusive on the one hand, and also causes many troubles for many command line extension programs on the other hand. This can only be said that "a sword has two edges".
In addition, "external commands" can also be used after /c, such as the ping that appears later. When it cannot be found in %path%, the path where it is located needs to be specified.
2. set,=何奈无── 。何奈可无是原,事奈无做人奈无
This section saves the signature string to be displayed in reverse order in an environment variable. The use of the variable name "," has multiple meanings here. In addition to the principle of "two tries", it also acts as a separator between set and its parameters. This is another example of the flexibility of the command line - it allows the variable name to use non-alphanumeric characters, and also allows this character to be interpreted in other ways. Similarly, the "/" in cmd/c mentioned above, as well as many characters such as "=" and ";" are also like this. I have discussed this in old posts before, which are not easy to find. Those who are interested can try using cmd with some special characters by themselves.
The purpose of reversing the signature string is naturally also "trying to be obscure and hard to understand", which echoes with (22;-1;0) in the subsequent for.
The two special characters before and after the signature string are the bell characters in the ASCII character set, whose ASCII code is 07, and they belong to the control area characters in the ASCII code table. Because most control area characters have special meanings, it is difficult to input this character with "Notepad". You can use the DOS editor EDIT. At that time, first type Ctrl+P to turn on the control character display (that is, temporarily turn off its escape characteristics), and then type Ctrl+G (tip: G is in the 7th position in the alphabet. For detailed content, please refer to the relevant information of the ASCII code table on the Internet), and you can get this character. Of course, you can also use a hexadecimal editor such as UltraEdit to directly type its ASCII code to input it. Because the display glyphs of ASCII control area characters are not uniformly agreed upon, it looks like a dot in the command line environment, and most Windows fonts can no longer display it normally. But because the ASCII standard is absorbed as a basic plane in the Unicode standard, NT systems that support Unicode will still correctly recognize and interpret this special character that was popular in the old DOS era.
3. &for,/l,%i,in,(22,-1,0)do,
"&" is a command separator, used to separate multiple commands in a command line. Cmd.exe runs the first command, and then runs the second command. Because "&" has the function of connecting multiple commands, it is also called a "command connector". set is the first command it connects, and for is the second command. It should be noted that there is also an "&" before the subsequent ping statement. Although it is also a command separator, it belongs to different levels. The former separates set and for in %ComSpec% /c, and the latter separates call and ping in for. Similar separators also include |, &&, ||, etc. For detailed content, please refer to the "Command Prompt" section in Windows Help.
The following is the control part of for, which realizes a reverse loop from 22 to 0, replaces the variable with %i, and will appear in the subsequent loop body statement block, and is replaced with the 23 digital strings from 22 to 0; for detailed content, you can use for /? in the command line environment to find out. The other commas in (22,-1,0) can be replaced with spaces, or other available parameter separators (such as ";", "=", etc., note the difference from the command separator). Such usage is still the embodiment of "trying to be obscure and hard to understand".
4. @call,set/p= %,:~%i,1%<nul
This is the first line of code in the for statement block. The purpose is to take out the corresponding character from the environment variable %,% according to the replacement variable %i, and combined with the control part of for, that is, to realize the reverse character-by-character display of the signature string. The role of @ is to prohibit the subsequent call statement from echoing in the command line. Because the for statement will create a new command running environment to execute multiple statements in the loop body, and command line echoing is on in this new environment. And call realizes the secondary replacement of the command line escape character %: in the new environment created by for, %i is replaced with the 23 digital strings from 22 to 0 in turn, and in the new environment created again by call, %,:~22,1% to %,:~0,1% are replaced with each character in the signature string in turn.
As for how the command line analyzes the multiple escape characters % that appear and the replacement variables, environment variables, and command line parameters mixed in them, this mainly depends on the two principles of "left is prior to right" and "replaceable is prior to non-replaceable", plus the delayed replacement characteristic of environment variables. Please readers experience it by themselves slowly.
Thus, this line of code is finally replaced with "set/p= (a certain signature character)<nul". The function of /p is to output the signature character as a prompt for asking the value of the environment variable. And this environment variable has no name, so no environment variable will be saved. As for why not use echo to display the character, it is because set/p will not display a new line after displaying the string, so that all signature characters can be displayed in one line instead of one column; the space after = can be omitted, it is displayed in front of each character, so it will increase the character spacing and improve the display effect; <nul is responsible for satisfying the input that set/p is waiting for. < redirects the input device of set/p from the standard console (CON, usually keyboard + screen) to the null device (NUL). Although it is not a actually existing hardware device, but only a software concept, it will "swallow" all input streams and output streams pointing to it. Here, the input requirement of set/p is also "swallowed", so it will not stop to wait for the user's input.
5. &ping/n 1 127.1>nul
& has been explained in section 3. ping is an external command used in the network environment to send an "Internet Control Message Protocol (ICMP)" echo request message to the specified IP address. For detailed content, please refer to the ping command section in Windows Help; here it is used to pause for a short time to achieve the effect of character-by-character display. Of course, its pause time is relatively short and not very fixed. It is recommended to change the number 1 after n to 2, so that the pause time will be approximately equal to 1 second. For detailed content, please refer to . >nul is similar to <nul in section 5, except that at this time NUL will be used as the output device, "swallowing" all the output information generated by the ping command.
Postscript
The original intention of this article originated from Brother maya0su's suggestion in . I was going to modify a command line version of the batch code based on Brother Wunaike's signature, which can display a specified text character by character in a more general method. At first, I thought that with the signature code as a foreshadowing, a little modification should be able to realize it. But as the modification went deeper, I found some more troublesome problems (mainly the compatibility problem of escape characters), so I started to study the signature code carefully, and discovered some details that I didn't notice before and some wrong understandings of some codes . This is the reason why this original article came out.
In fact, the most important skill in the signature code is the secondary replacement of escape characters, and I originally mistakenly understood it as delayed replacement. This is also one of the important reasons for the emergence of this article. However, when I tried to explain the mechanism in detail, I found that I could only go deep but not explain it in a simple way. Many contents are related to the complex characteristics of command line interpretation. There are no official documents or other public materials to refer to and prove, and my own experience is inevitably incomplete. In order to avoid misleading readers, I have to use a sentence "experience by yourself" to be vaguely passed, which is a bit embarrassing to say. I sincerely ask for the understanding of the majority of readers!
References
A Heterogeneous Batch Programming - Clock (Clock)
http://www.cn-dos.net/forum/viewthread.php?tid=8905#pid54227
A Little Talk about Batch Processing Parameters
http://www.cn-dos.net/forum/viewthread.php?tid=17785
About the Delay of "set & echo" Variable Replacement
http://www.cn-dos.net/forum/viewthread.php?tid=18050
[ Last edited by willsort on 2006-1-22 at 11:34 ]
☆Start\Run (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
Quoted from the signature of Wunaike on the China DOS Union forum
This signature's code is applied in WinNT series systems (such as WinXP). Copy and paste the code content into the "Start - Run" dialog box, and you can see the signature of Brother Wunaike displayed character by character. The code is only 132B long, but it contains many batch programming skills, some of which I only gradually noticed later. Now I will divide this code into sections and briefly hint at the important skills used, to entertain readers. Also, please allow Brother Wunaike, the original author, and other batch processing experts to criticize and make corrections.
1. %ComSpec% /c
The purpose of using it is to call internal commands such as set / for / pause in the Run dialog box. Because they are supported by the main program of the command line environment in NT series operating systems - the "internal" of cmd, and can only be used within the "internal" of cmd. Cmd is full name cmd.exe, located in the %SystemRoot%\system32\ path, and its complete path is indicated by the environment variable %ComSpec%. Because it can only run in the NT series environment, in fact, it is approximately equivalent to cmd /c. But because %ComSpec% contains the path where cmd.exe is located, it can also be used in special system environments where the location of cmd.exe is special or the %path% variable is missing. Detailed content can be found by using cmd /? in the command line environment.
Because "Run" is outside cmd, only "external commands" with independent executable files can be called, and the cmd "external command" acts as a "mouthpiece" for internal commands, passing a string of "internal commands" as the command line parameters of cmd, and passing them to the internal of cmd for interpretation and execution by the option switch /c. Generally, a space is inserted between /c and the subsequent attached command string to increase the readability of the code; Brother Wunaike omits the space, which is the embodiment of the requirements he mentioned of "trying to make the characters as short as possible" and "trying to be obscure and hard to read". And if used in the command line environment and cmd is used instead of %ComSpec%, the space between cmd and /c can also be omitted. These usages of omitting spaces are sometimes not simply for the purpose of streamlining the code, but to deal with the relatively weak string analysis algorithms of some commands or programs - sometimes we need to treat the entire command line as a string, and many command line programs will use spaces as the end mark of a string, such as the (set) part in the for command. Of course, this kind of usage that is compatible with both spaces and no spaces reflects its flexible characteristics of being inclusive on the one hand, and also causes many troubles for many command line extension programs on the other hand. This can only be said that "a sword has two edges".
In addition, "external commands" can also be used after /c, such as the ping that appears later. When it cannot be found in %path%, the path where it is located needs to be specified.
2. set,=何奈无── 。何奈可无是原,事奈无做人奈无
This section saves the signature string to be displayed in reverse order in an environment variable. The use of the variable name "," has multiple meanings here. In addition to the principle of "two tries", it also acts as a separator between set and its parameters. This is another example of the flexibility of the command line - it allows the variable name to use non-alphanumeric characters, and also allows this character to be interpreted in other ways. Similarly, the "/" in cmd/c mentioned above, as well as many characters such as "=" and ";" are also like this. I have discussed this in old posts before, which are not easy to find. Those who are interested can try using cmd with some special characters by themselves.
The purpose of reversing the signature string is naturally also "trying to be obscure and hard to understand", which echoes with (22;-1;0) in the subsequent for.
The two special characters before and after the signature string are the bell characters in the ASCII character set, whose ASCII code is 07, and they belong to the control area characters in the ASCII code table. Because most control area characters have special meanings, it is difficult to input this character with "Notepad". You can use the DOS editor EDIT. At that time, first type Ctrl+P to turn on the control character display (that is, temporarily turn off its escape characteristics), and then type Ctrl+G (tip: G is in the 7th position in the alphabet. For detailed content, please refer to the relevant information of the ASCII code table on the Internet), and you can get this character. Of course, you can also use a hexadecimal editor such as UltraEdit to directly type its ASCII code to input it. Because the display glyphs of ASCII control area characters are not uniformly agreed upon, it looks like a dot in the command line environment, and most Windows fonts can no longer display it normally. But because the ASCII standard is absorbed as a basic plane in the Unicode standard, NT systems that support Unicode will still correctly recognize and interpret this special character that was popular in the old DOS era.
3. &for,/l,%i,in,(22,-1,0)do,
"&" is a command separator, used to separate multiple commands in a command line. Cmd.exe runs the first command, and then runs the second command. Because "&" has the function of connecting multiple commands, it is also called a "command connector". set is the first command it connects, and for is the second command. It should be noted that there is also an "&" before the subsequent ping statement. Although it is also a command separator, it belongs to different levels. The former separates set and for in %ComSpec% /c, and the latter separates call and ping in for. Similar separators also include |, &&, ||, etc. For detailed content, please refer to the "Command Prompt" section in Windows Help.
The following is the control part of for, which realizes a reverse loop from 22 to 0, replaces the variable with %i, and will appear in the subsequent loop body statement block, and is replaced with the 23 digital strings from 22 to 0; for detailed content, you can use for /? in the command line environment to find out. The other commas in (22,-1,0) can be replaced with spaces, or other available parameter separators (such as ";", "=", etc., note the difference from the command separator). Such usage is still the embodiment of "trying to be obscure and hard to understand".
4. @call,set/p= %,:~%i,1%<nul
This is the first line of code in the for statement block. The purpose is to take out the corresponding character from the environment variable %,% according to the replacement variable %i, and combined with the control part of for, that is, to realize the reverse character-by-character display of the signature string. The role of @ is to prohibit the subsequent call statement from echoing in the command line. Because the for statement will create a new command running environment to execute multiple statements in the loop body, and command line echoing is on in this new environment. And call realizes the secondary replacement of the command line escape character %: in the new environment created by for, %i is replaced with the 23 digital strings from 22 to 0 in turn, and in the new environment created again by call, %,:~22,1% to %,:~0,1% are replaced with each character in the signature string in turn.
As for how the command line analyzes the multiple escape characters % that appear and the replacement variables, environment variables, and command line parameters mixed in them, this mainly depends on the two principles of "left is prior to right" and "replaceable is prior to non-replaceable", plus the delayed replacement characteristic of environment variables. Please readers experience it by themselves slowly.
Thus, this line of code is finally replaced with "set/p= (a certain signature character)<nul". The function of /p is to output the signature character as a prompt for asking the value of the environment variable. And this environment variable has no name, so no environment variable will be saved. As for why not use echo to display the character, it is because set/p will not display a new line after displaying the string, so that all signature characters can be displayed in one line instead of one column; the space after = can be omitted, it is displayed in front of each character, so it will increase the character spacing and improve the display effect; <nul is responsible for satisfying the input that set/p is waiting for. < redirects the input device of set/p from the standard console (CON, usually keyboard + screen) to the null device (NUL). Although it is not a actually existing hardware device, but only a software concept, it will "swallow" all input streams and output streams pointing to it. Here, the input requirement of set/p is also "swallowed", so it will not stop to wait for the user's input.
5. &ping/n 1 127.1>nul
& has been explained in section 3. ping is an external command used in the network environment to send an "Internet Control Message Protocol (ICMP)" echo request message to the specified IP address. For detailed content, please refer to the ping command section in Windows Help; here it is used to pause for a short time to achieve the effect of character-by-character display. Of course, its pause time is relatively short and not very fixed. It is recommended to change the number 1 after n to 2, so that the pause time will be approximately equal to 1 second. For detailed content, please refer to . >nul is similar to <nul in section 5, except that at this time NUL will be used as the output device, "swallowing" all the output information generated by the ping command.
Postscript
The original intention of this article originated from Brother maya0su's suggestion in . I was going to modify a command line version of the batch code based on Brother Wunaike's signature, which can display a specified text character by character in a more general method. At first, I thought that with the signature code as a foreshadowing, a little modification should be able to realize it. But as the modification went deeper, I found some more troublesome problems (mainly the compatibility problem of escape characters), so I started to study the signature code carefully, and discovered some details that I didn't notice before and some wrong understandings of some codes . This is the reason why this original article came out.
In fact, the most important skill in the signature code is the secondary replacement of escape characters, and I originally mistakenly understood it as delayed replacement. This is also one of the important reasons for the emergence of this article. However, when I tried to explain the mechanism in detail, I found that I could only go deep but not explain it in a simple way. Many contents are related to the complex characteristics of command line interpretation. There are no official documents or other public materials to refer to and prove, and my own experience is inevitably incomplete. In order to avoid misleading readers, I have to use a sentence "experience by yourself" to be vaguely passed, which is a bit embarrassing to say. I sincerely ask for the understanding of the majority of readers!
References
A Heterogeneous Batch Programming - Clock (Clock)
http://www.cn-dos.net/forum/viewthread.php?tid=8905#pid54227
A Little Talk about Batch Processing Parameters
http://www.cn-dos.net/forum/viewthread.php?tid=17785
About the Delay of "set & echo" Variable Replacement
http://www.cn-dos.net/forum/viewthread.php?tid=18050
[ Last edited by willsort on 2006-1-22 at 11:34 ]
Recent Ratings for This Post
( 8 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| redtek | +10 | 2006-11-06 22:39 |
| hxuan999 | +1 | 2006-11-23 05:39 |
| HUNRYBECKY | +2 | 2007-02-04 03:04 |
| mtiankong | +2 | 2007-04-21 01:28 |
| xiaoyao1987 | +2 | 2007-04-22 03:34 |
| luowei144 | +2 | 2007-05-13 22:16 |
| zqdarkday | +2 | 2007-11-16 16:54 |
| dthao | +4 | 2007-11-16 18:17 |
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!

DigestII
