Shell - console Just for fun

    A significant part of the KolibriOS operating system (the kernel and most drivers and programs) are written in assembly language. However, programs in high-level languages ​​also exist. Today we will talk about one of them.

    Before creating the Shell command-line program, I already had experience writing programs for Hummingbirds. Basically, these were games - Piton , Donkey , the port of the ZX Spectrum emulator, which I named e80 , ports of console tags and the port of Eliza's virtual interlocutor . I had fun as I could.



    In addition to toys, there were attempts to write something more useful. So, for example, the cObj utility(“See .obj”) allowed you to view the list of exported dynamic library, ie COFF file with the global variable EXPORTS or _EXPORTS functions. In fact, this is the most truncated Dependency Walker. The cObj program was written in assembler, and the size of the executable compressed with the kpack compressor is 256 bytes. Therefore, as it seems to me now, this program fits perfectly into the concept of Hummingbird.



    Why did I devote a whole paragraph to the wrong program, which will be discussed later? 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, since the library is well documented, like many things in KolibriOS. The second reason is that I am a lazy person, and assembly language for me is just as much fun as spending time on computer games, rather than achieving a certain result. Low-level optimization of code associated with projects at the main place of work does not count. All the programs listed in the article, except for cObj, of course, and the port of Eliza, are written in the great and powerful C.

    Toys with toys (damn it, and now I am writing them!), But I wanted to write something useful, not only for program developers. And since I lacked an advanced command line, I decided to implement it.

    The question may arise: "Is there a command line in Hummingbird OS ?!". Yes, the command line existed. The program written in assembly language was called CMD, and, in principle, if to speak objectively, it was quite functional.



    However, subjectively, I saw many shortcomings in it (such as a small 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 forum of Hummingbird OS. The first message began as follows: “I decided to write a functional shell in C using console.obj.” The message ended with the words “While I made the function of separating the command and parameters, and also implemented the help commands (so far without parameters), ver and exit.”

    Attached to the first message, the shell.zip file (now downloaded more than 180 times) contained the binary of this craft, 1.3 kilobytes in size.

    The very next day I posted the version with the added commands exit, ls, pwd, ps, kill, help (the last command - with and without parameters).

    Initially, updates were quite frequent, then they appeared less and less. This is not to say that I have lost interest in my development. There was simply not always time left for programming “just for fun”.

    Version history of the Shell program starts with 0.01. Yes, exactly 0.01. In version 0.1, it became possible to run programs (who would have thought that such an opportunity would not be realized right away?). Starting with version 0.3, Shell could run scripts of its own format. Also in this version there was a history of teams, working, including, with aliases.

    Starting with version 0.4.2, other members of the Hummingbird OS community joined the development of the project. So, diamond (the author of many programs for Hummingbirds and the developer of the console.obj library) implemented the correct termination of the program when closing the window and optimized functions for speed, Pterox added some useful commands, Leencymade support for relative paths in scripts. In addition, it is not surprising that CleverMouse was not without help .

    Soon, with the release of Hummingbird OS 0.7.5.0 at the end of January 2009, Shell replaced the old CMD command line.
    What are the possibilities of this "almost very functional shell"?

    To date, the current version of the program is 0.7.4. Yes, it’s far to 1.0, which means that 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, a system of command aliases, processing of control characters and ESC sequences, the first steps have been taken to support the system-wide clipboard. The program can be compiled with messages in either Russian or English.



    Shell has only about 80 kilobytes of C source code under the hood, of which 18 is a library similar to libc, as well as a library with the functions of the Hummingbird OS API. Using your own libc (besides it there are at least two other implementations in Hummingbird OS) made it easier to build the program and slightly reduce the size of the output file. By the way, the MinGW compiler is required to build Shell, although the Diamond version of Shell was designed for MSVC. I tried to make the source code of the program as transparent as possible, i.e. understandable even to novice programmers (which is precisely why there are so few comments in them! The code is understandable! I’m joking). Shell source code is divided into modules, and each of the internal commands is placed in separate files, which theoretically should simplify the search for the necessary sections.

    From the capabilities of the program, we turn to the principles of work. If with interactive mode, I think everything is clear (we enter a command, we get an answer, and so on, until we get bored), then there are nuances in writing scripts and console applications for Shell.

    Scripts can contain any commands that work interactively. Moreover, at startup Shell searches in the settings directory (usually / sys / settings /), and then in the directory where it is launched, the “.shell” file (without quotes) and, if it finds it, executes it. Usually it contains the about command and a suggestion 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 from it that Shell determines whether the script can be executed.

    Console applications for Shell are ordinary Hummingbird OS 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 Shell.

    Information exchange between Shell and console applications is carried out through the named area. The console application, after its launch, first of all should create a named area with the name pid-SHELL, where pid is the identifier of the process without uppercase zeros, for example: 6, 42 or 204. The first byte of the area is a command (i.e. a maximum of 255 commands, which quite enough), then come 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 a character from the keyboard
    • SC_GETS 5 read a line from the keyboard
    • SC_CLS 6 clear screen

    The console application can be written in any programming language, the compiler of which can produce code for Hummingbird OS. On the KolibriOS OS SVN server 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 a developer should know:
    • The console program for Shell itself must take care of the rational use of CPU time.
    • The console program for Shell must take care of closing the named area itself.

    Those. in principle, a few simple rules, and the rest is obvious, especially after parsing the source code of the example (2 kilobytes in C and 3 kilobytes in assembler). Is it complicated, 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, there is not a single console program for Shell in KolibriOS yet! Scripts, however, are also not very common, but are still sometimes used.

    In general, you can talk about the Shell program for quite some time. Especially about its further development. After all, the fact that there are today does not suit many. Users require the implementation of new teams, without specifying, however, which ones. Yes, Shell is very far from BusyBox. But given that Shell is intended for a non-UNIX-like system, and the standard libc is not used, 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 Shell can be safely called a “very functional shell” by version 1.0.

    Also popular now: