A significant part of the KolibriOS operating system (the kernel and most of the drivers and programs) is written in assembly language. However, programs in high-level languages ​​are also available. Today we will talk about one of them.
Before creating the Shell command line program, I already had experience writing programs for KolibriOS. Basically, these were the games -
Piton ,
Donkey , the port of the ZX Spectrum emulator, which I called the
e80 , the ports of the console tags and the port of the virtual interlocutor
Eliza . I had fun as I could.

')
In addition to toys, there were attempts to write something more useful. For example, the
cObj utility (“see .obj”) allowed viewing the list of exported dynamic libraries, i.e. COFF file with global variable EXPORTS or _EXPORTS, functions. In fact, this is the most stripped-down Dependency Walker. The cobj program was written in assembler, and the size of the executable file compressed using the kpack compressor is 256 bytes. Therefore, as it seems to me now, this program fits perfectly into the concept of Colibrios.

Why did I devote a whole paragraph to a completely different program, which will be discussed further? There are two reasons. First: cObj - turned out to be a wonderful tool for researching the library for working with the console (console.obj). A wonderful additional tool, as the library is well documented, like many things in KolibriOS. The second reason is that I am a lazy person, and programming in assembler for me is just as much entertainment as spending time playing computer games, and not achieving a certain result. Low-level optimization of the code associated with projects at the main place of work is not counted. All the programs listed in the article, except for cobi, of course, and the port of Eliza, are written in the great and mighty C.
Toys toys (pancake, and now I write them!), But I wanted to write something useful not only for software developers. And since I lacked an advanced command line, I decided to implement it.
The question may arise: “Wasn’t there a command line in KolibriOS ?!”. Yes, the command line existed. Written in assembly language, the program was called CMD, and, in principle, speaking objectively, was quite functional.

However, subjectively, I saw many shortcomings in it (such as the fine print, for example, or the inability to scroll the window). Therefore, the goal was set - "I will write my command line, with scripts and console applications."

July 2008. He opened the topic
“Very functional shell” on the KolibriOS forum. The first message started like this: “I decided to write a functional shell on C using console.obj.” The message ended with the words “So far I made the function of separating the command and parameters, and also implemented the help commands (without parameters), ver and exit.”
The file shell.zip attached to the first post (now downloaded more than 180 times) contained the binary of this craft with the size of 1.3 kilobytes.
The very next day I posted a version with the added commands exit, ls, pwd, ps, kill, help (the last command - with and without parameters).
At first, updates were quite frequent, then they appeared less and less. This is not to say that I have lost interest in my development. It just didn't always have time to program “just for fun”.
The version history of the Shell program starts at 0.01. Yes, exactly 0.01. In version 0.1, it became possible to launch programs (someone would have thought that this possibility would not be realized immediately?). Starting from version 0.3, Shell could run scripts of its own format. Also in this version there is a history of commands, working, including, and with pseudonyms (aliases).
Beginning with version 0.4.2, other members of the KolibriOS community have joined the project. So, diamond (the author of many programs for Hummingbirds and the developer of the console.obj library) implemented the correct completion of the program when closing the window and optimized functions for speed, Pterox added some useful commands,
Leency did support relative paths in the scripts. In addition, it is not surprising that not without the help of
CleverMouse .
Soon, with the release of Hummingbird 0.7.5.0 at the end of January 2009, the Shell program replaced the old command line CMD.
What are the possibilities of this “almost very functional shell”?
To date, the current version of the program 0.7.4. Yes, it is far to 1.0, which means you should not expect much. However, 25 commands are implemented (about, alias, cd, clear, cp, date, echo, exit, free, help, history, kill, ls, mkdir, more, ps, pwd, reboot, rm, rmdir, shutdown, sleep, touch, uptime, ver), there is support for scripts and console applications, command history, command alias system, processing of control characters and ESC sequences, the first steps have been taken in supporting the system-wide clipboard. The program can be compiled with messages either in Russian or in English.

"Under the hood," Shell has
only about 80 kilobytes of C source code , of which 18 are libraries similar to libc, as well as a library with API Hummingbird functions. Using your own libc (besides it in ColibriOS there are at least two more implementations) allowed to ease the assembly of the program and slightly reduce the size of the output file. By the way, the MinGW compiler is required to build Shell, although the Shell version from diamond has been designed for MSVC. I tried to make the source of the program as transparent as possible, i.e. understandable even to novice programmers (That's exactly why they have so few comments! The code is clear anyway! I'm kidding). Shell source code is divided into modules, and each of the internal commands is rendered into separate files, which theoretically should simplify the search for the necessary sections.
From the capabilities of the program move on to the principles of work. If the interactive mode, I think, everything is clear (enter a command, get the answer, and so on until you get tired), then there are nuances in writing scripts and console applications for Shell.
Scripts can contain any commands that work interactively. Moreover, when launched, Shell searches the settings directory (usually / sys / settings /), and then in the directory from which it is launched, the “.shell” file (without quotes) and if it finds it, executes it. Usually it has an about command and an offer to type help for help. But, of course, you can write your own sequence of commands.

In scripts, it is possible to use comments starting with a pound (#).
The first line of the script (see the screenshot above) must be “#SHS”. It means “SHellScript”, and it is for her that Shell determines whether a script can be executed.
Shell console applications are normal ColibriOS applications for which you do not even need to initialize the console window. True, the developer of such an application will have to take care of the correct initialization of the interface with the Shell.
Information is exchanged between Shell and console applications through a named area. After its launch, the console application should first create a named region named pid-SHELL, where pid is a process identifier without capital zeros, for example: 6, 42 or 204. The first byte of the region is a command (that is, a maximum of 255 commands that enough), then the data (however, they may be absent).
List of implemented commands:
- SC_OK 0 do nothing
- SC_EXIT 1 output
- SC_PUTC 2 display character
- SC_PUTS 3 display string
- SC_GETC 4 read character from keyboard
- SC_GETS 5 read a string from the keyboard
- SC_CLS 6 clear screen
A console application can be written in any programming language whose compiler can produce code for ColibriOS. On the SVN-server KolibriOS in the Shell directory you can see two examples of console applications - in C and in assembly language (and it is noteworthy that the last example appeared much later than the first).
A few more features that the developer should know:
- The console program for Shell should take care of rational use of CPU time.
- The Shell console program should take care of closing the named area itself.
Those. in principle, a few simple rules, and the rest is obvious, especially after analyzing the source code of the example (2 kilobytes in C and 3 kilobytes in assembler). Is it difficult, for example, the main function from the example?
void kol_main() { char string[256]; sc_init(); sc_cls(); sc_puts("This is a test console application for Shell\n\r"); sc_puts("Type a string (255 symbols max): "); sc_gets(string); sc_puts("You typed:\n\r"); sc_puts(string); sc_puts("Press any key: "); string[0] = sc_getc(); sc_puts("\n\rYou pressed: "); sc_putc(string[0]); sc_exit(); }
Despite the simplicity of the interface, in KolibriOS there is not a single console program for Shell! Scripts, however, are also not very common, but still sometimes used.
In general, you can talk about the Shell program for a long time. Especially about its further development. After all, the fact that there are today does not suit many. Users require the implementation of new commands, without specifying, however, what. Yes, Shell is very far from BusyBox. But if you consider that Shell is designed for a non-UNIX-like system, and the standard libc is not used, then it should be clear that the implementation of each new function is a kind of experiment. And there can be many more such experiments. Therefore, I hope that by version 1.0 of Shell it will be possible to call it a “very functional shell”.