Mass Compile Cobol code in IBM i (aka OS/400, iSeries) - shell

I am new to COBOL and AS/400 IBM iSeries world, and am interested in best practices used in this community.
How do people perform batch compilation of all COBOL members, or multiple members at once?
I see that AS/400 source files are organized in libraries, file
objects, and members. How do you detect which file members are COBOL src
code to be compiled? Can you get the member file type (which should be CBL
in this case) via some qshell command?
Thanks,
Akku

Common PDM manual method:
Probably the simplest and most widely used method would be to use PDM (Program Development Manager) in a "green-screen" (5250-emulation) session. This allows you to manually select every program you wish to compile. It may not be quite the answer you were looking for, but it may be the most widely used, due to its simple approach, and leaving decisions in the developer's hands.
People commonly use the Start PDM command STRPDM, which provides a menu where you can select an option to work with lists of Libraries, Objects (Files), or Members. (Personally, I prefer to use the corresponding commands directly, WRKLIBPDM, WRKOBJPDM, or WRKMBRPDM.) At each of these levels you can filter the list by pressing F17 (shift F5).
F18 (shift F6) allows you to set the option to Compile in Batch. This means that each individual compile will be submitted to a job queue, to compile in its own job. You can also specify which job description you would like to use, which will determine which job queue the jobs are placed on. Some job queues may be single threaded, while others might run multiple jobs at once. You can custom-define your own PDM options with F16.
If you chose to start at the library level, you can enter option 12 next to each library you wish to work with its objects (source files).
At the object level, you would want to view only objects of type *FILE, and attribute 'PF-SRC' (or concievably 'PF38-SRC'). You can then enter option 12 beside any source file you wish to work with its members.
At the member level, you might want to filter to type *CBL* because (depending on how things have been done on your system) COBOL members could be CBL, CBLLE, SQLCBL, SQLCBLE, or even System/38 or /36 variants. Type option 14 (or a custom-defined option) next to each member you wish to compile. You can repeat an option down the list with F13 (shift F1).
This method uses manual selection, and does not automatically select ALL of your COBOL programs to be compliled. But it does allow you to submit large numbers of compiles at a time, and uses programmer discretion to determine which members to select, and what options to use.

Many (if not most) developers on IBM i are generally not very familiar with qshell. Most of us write automation scripts in CL. A few renegades like myself may also use REXX, but sadly this is rare. It's not too often that we would want to re-compile all programs in the system. Generally we only compile programs that we are working with, or select only those affected by some file change.
Compiling everything might not be a simple problem. Remember some libraries or source files might simply be archival copies of old source, which you might not really want to compile, or that might not compile successfully anymore. You would want to distinguish which members are COBOL copybooks, rather than programs. With ILE, you would want to distinguish which members should be compiled as programs, modules, or service programs. You may need to compile modules before compiling programs that bind with them. Those modules might not necessarily have been written in COBOL, or COBOL modules might be bound into ILE programs in other languages, perhaps CL or RPG.
So how would a full system recompile be automated in a CL program? You could get a list of all source files on they system with DSPOBJD *ALL/*ALL *FILE OUTPUT(*FILE) OUTFILE( ___ ). The output file contains a file attribute column to distinguish which objects are source files. Your CL program could read this, and for each source file, it could generate a file of detailed member information with DSPFD &lib/&file TYPE(*MBR) OUTPUT(*FILE) OUTFILE( ___ ). That file contains member type information, which could help you determine which members were COBOL. From there you could RTVOBJD to figure out whether it was a program, module, and/or service program.
You may also need to know the options for how individual programs, modules, or service programs were compiled. I often solve this by creating a source file, which I generally call BUILD, with a member for each object that needs special handling. This member could be CL, but I often use REXX. In fact I might be tempted to do the whole thing in REXX for its power as a dynamic interpreted language. But that's just me.

Related

Using gmake to build large system

I'm working on trying to fix/redo the makefile(s) for a legacy system, and I keep banging my head against some things. This is a huge software system, consisting of numerous executables, shared objects, and libraries, using C, Fortran, and a couple of other compilers (such as Motif's UIL compiler). I'm trying to avoid recursive make and I would prefer "one makefile to build them all" rather than the existing "one makefile per executable/.so/.a" idea. (We call each executable, shared object, library, et al a "task," so permit me to use that term as I go forward.)
The system is scattered across tons of different source subdirectories, with includes usually in one of 6 main directories, though not always.
What would be the best way to deal with the plethora of source & target directories? I would prefer including a Task.mk file for each task that would provide the task-specific details, but target-specific variables don't give me enough control. For example, they don't allow me to change the source & target directories easily, at least from what I've tried.
Some things I cannot (i.e., not allowed to) do include:
Change the structure of the project. There's too much that would need to be changed everywhere to pull that off.
Use a different make. The official configuration (which our customer has guaranteed, so we don't need to deal with unknown configurations) uses gmake 3.81, period.

Processing gcov data files for tracing purposes

I'm trying to create a tool similar to TraceGL, but for C-type languages:
As you can see, the tool above highlights code flows that were not executed in red.
In terms of building this tool for Objective-C, for example, I know that gcov (and libprofile_rt in clang) output data files that can help determine how many times a given line of code has been executed. However, would the gcov data files be able to tell me when a given line of code occurred during a program's execution?
For example, if line X is called during code paths A and B, would I be able to ascertain from the gcov that code paths A and B called line X given line X alone?
As far as I know, GCOV instrumentation data only tells that some point in the code was executed (and maybe how many times). But there is no relationship between the code points that are instrumented.
It sounds like what you want is to determine paths through the code. To do that, you either need to do static analysis of the code (requiring a full up C parser, name resolver, flow analyzer), or you need to couple the dynamic instrumentation points together in execution order.
The first requires you find machinery capable of processing C in all of its glory; you don't want to repeat that yourself. GCC, Clang, our DMS Toolkit are choices. I know the GCC and Clang do pretty serious analysis; I'm pretty sure you could find at least intraprocedural control flow analysis; I know that DMS can do this. You'd have to customize GCC and Clang to extract this data. You'd have to configure DMS to extract this data; configuration is easier than customization because it is a design property rather than a "custom" action. YMMV.
Then, using the GCOV data, you could determine the flows between the GCOV data points. It isn't clear to me that this buys you anything beyond what you already get with just the static control flow analysis, unless your goal is to exhibit execution traces.
To do this dynamically, what you could do is force each data collection point in the instrumented code to note that it is the most recent point encountered; before doing that, it would record the most recent point encountered before it was. This would produce in effect a chain of references between points which would match the control flow. This has two problems from your point of view, I think: a) you'd have to modify GCOV or some other tool to insert this different kind of instrumentation, b) you have to worry about what and how you record "predecessors" when a data collection point gets hit more than once.
gcov (or lcov) is one option. It does produce most of the information you are looking for, though how often those files are updated depends on how often __gcov_flush() is called. It's not really intended to be real time, and does not include all of the information you are looking for (notably, the 'when'). There is a short summary of the gcov data format here and in the header file here. lcov data is described here.
For what you are looking for DTrace should be able to provide all of the information you need, and in real time. For Objective-C on Apple platforms there are dtrace probes for the runtime which allow you to trace pretty much anything. There are a number of useful guides and examples out there for learning about dtrace and how to write scripts. Brendan Gregg provides some really great examples. Big Nerd Ranch has done a series of articles on it.

Extending functionality of existing program I don't have source for

I'm working on a third-party program that aggregates data from a bunch of different, existing Windows programs. Each program has a mechanism for exporting the data via the GUI. The most brain-dead approach would have me generate extracts by using AutoIt or some other GUI manipulation program to generate the extractions via the GUI. The problem with this is that people might be interacting with the computer when, suddenly, some automated program takes over. That's no good. What I really want to do is somehow have a program run once a day and silently (i.e. without popping up any GUIs) export the data from each program.
My research is telling me that I need to hook each application (assume these applications are always running) and inject a custom DLL to trigger each export. Am I remotely close to being on the right track? I'm a fairly experienced software dev, but I don't know a whole lot about reverse engineering or hooking. Any advice or direction would be greatly appreciated.
Edit: I'm trying to manage the availability of a certain type of professional. Their schedules are stored in proprietary systems. With their permission, I want to install an app on their system that extracts their schedule from whichever system they are using and uploads the information to a central server so that I can present that information to potential clients.
I am aware of four ways of extracting the information you want, both with their advantages and disadvantages. Before you do anything, you need to be aware that any solution you create is not guaranteed and in fact very unlikely to continue working should the target application ever update. The reason is that in each case, you are relying on an implementation detail instead of a pre-defined interface through which to export your data.
Hooking the GUI
The first way is to hook the GUI as you have suggested. What you are doing in this case is simply reading off from what an actual user would see. This is in general easier, since you are hooking the WinAPI which is clearly defined. One danger is that what the program displays is inconsistent or incomplete in comparison to the internal data it is supposed to be representing.
Typically, there are two common ways to perform WinAPI hooking:
DLL Injection. You create a DLL which you load into the other program's virtual address space. This means that you have read/write access (writable access can be gained with VirtualProtect) to the target's entire memory. From here you can trampoline the functions which are called to set UI information. For example, to check if a window has changed its text, you might trampoline the SetWindowText function. Note every control has different interfaces used to set what they are displaying. In this case, you are hooking the functions called by the code to set the display.
SetWindowsHookEx. Under the covers, this works similarly to DLL injection and in this case is really just another method for you to extend/subvert the control flow of messages received by controls. What you want to do in this case is hook the window procedures of each child control. For example, when an item is added to a ComboBox, it would receive a CB_ADDSTRING message. In this case, you are hooking the messages that are received when the display changes.
One caveat with this approach is that it will only work if the target is using or extending WinAPI controls.
Reading from the GUI
Instead of hooking the GUI, you can alternatively use WinAPI to read directly from the target windows. However, in some cases this may not be allowed. There is not much to do in this case but to try and see if it works. This may in fact be the easiest approach. Typically, you will send messages such as WM_GETTEXT to query the target window for what it is currently displaying. To do this, you will need to obtain the exact window hierarchy containing the control you are interested in. For example, say you want to read an edit control, you will need to see what parent window/s are above it in the window hierarchy in order to obtain its window handle.
Reading from memory (Advanced)
This approach is by far the most complicated but if you are able to fully reverse engineer the target program, it is the most likely to get you consistent data. This approach works by you reading the memory from the target process. This technique is very commonly used in game hacking to add 'functionality' and to observe the internal state of the game.
Consider that as well as storing information in the GUI, programs often hold their own internal model of all the data. This is especially true when the controls used are virtual and simply query subsets of the data to be displayed. This is an example of a situation where the first two approaches would not be of much use. This data is often held in some sort of abstract data type such as a list or perhaps even an array. The trick is to find this list in memory and read the values off directly. This can be done externally with ReadProcessMemory or internally through DLL injection again. The difficulty lies mainly in two prerequisites:
Firstly, you must be able to reliably locate these data structures. The problem with this is that code is not guaranteed to be in the same place, especially with features such as ASLR. Colloquially, this is sometimes referred to as code-shifting. ASLR can be defeated by using the offset from a module base and dynamically getting the module base address with functions such as GetModuleHandle. As well as ASLR, a reason that this occurs is due to dynamic memory allocation (e.g. through malloc). In such cases, you will need to find a heap address storing the pointer (which would for example be the return of malloc), dereference that and find your list. That pointer would be prone to ASLR and instead of a pointer, it might be a double-pointer, triple-pointer, etc.
The second problem you face is that it would be rare for each list item to be a primitive type. For example, instead of a list of character arrays (strings), it is likely that you will be faced with a list of objects. You would need to further reverse engineer each object type and understand internal layouts (at least be able to determine offsets of primitive values you are interested in in terms of its offset from the object base). More advanced methods revolve around actually reverse engineering the vtable of objects and calling their 'API'.
You might notice that I am not able to give information here which is specific. The reason is that by its nature, using this method requires an intimate understanding of the target's internals and as such, the specifics are defined only by how the target has been programmed. Unless you have knowledge and experience of reverse engineering, it is unlikely you would want to go down this route.
Hooking the target's internal API (Advanced)
As with the above solution, instead of digging for data structures, you dig for the internal API. I briefly covered this with when discussing vtables earlier. Instead of doing this, you would be attempting to find internal APIs that are called when the GUI is modified. Typically, when a view/UI is modified, instead of directly calling the WinAPI to update it, a program will have its own wrapper function which it calls which in turn calls the WinAPI. You simply need to find this function and hook it. Again this is possible, but requires reverse engineering skills. You may find that you discover functions which you want to call yourself. In this case, as well as being able to locate the location of the function, you have to reverse engineer the parameters it takes, its calling convention and you will need to ensure calling the function has no side effects.
I would consider this approach to be advanced. It can certainly be done and is another common technique used in game hacking to observe internal states and to manipulate a target's behaviour, but is difficult!
The first two methods are well suited for reading data from WinAPI programs and are by far easier. The two latter methods allow greater flexibility. With enough work, you are able to read anything and everything encapsulated by the target but requires a lot of skill.
Another point of concern which may or may not relate to your case is how easy it will be to update your solution to work should the target every be updated. With the first two methods, it is more likely no changes or small changes have to be made. With the second two methods, even a small change in source code can cause a relocation of the offsets you are relying upon. One method of dealing with this is to use byte signatures to dynamically generate the offsets. I wrote another answer some time ago which addresses how this is done.
What I have written is only a brief summary of the various techniques that can be used for what you want to achieve. I may have missed approaches, but these are the most common ones I know of and have experience with. Since these are large topics in themselves, I would advise you ask a new question if you want to obtain more detail about any particular one. Note that in all of the approaches I have discussed, none of them suffer from any interaction which is visible to the outside world so you would have no problem with anything popping up. It would be, as you describe, 'silent'.
This is relevant information about detouring/trampolining which I have lifted from a previous answer I wrote:
If you are looking for ways that programs detour execution of other
processes, it is usually through one of two means:
Dynamic (Runtime) Detouring - This is the more common method and is what is used by libraries such as Microsoft Detours. Here is a
relevant paper where the first few bytes of a function are overwritten
to unconditionally branch to the instrumentation.
(Static) Binary Rewriting - This is a much less common method for rootkits, but is used by research projects. It allows detouring to be
performed by statically analysing and overwriting a binary. An old
(not publicly available) package for Windows that performs this is
Etch. This paper gives a high-level view of how it works
conceptually.
Although Detours demonstrates one method of dynamic detouring, there
are countless methods used in the industry, especially in the reverse
engineering and hacking arenas. These include the IAT and breakpoint
methods I mentioned above. To 'point you in the right direction' for
these, you should look at 'research' performed in the fields of
research projects and reverse engineering.

Are there any good reference implementations available for command line implementations for embedded systems?

I am aware that this is nothing new and has been done several times. But I am looking for some reference implementation (or even just reference design) as a "best practices guide". We have a real-time embedded environment and the idea is to be able to use a "debug shell" in order to invoke some commands. Example: "SomeDevice print reg xyz" will request the SomeDevice sub-system to print the value of the register named xyz.
I have a small set of routines that is essentially made up of 3 functions and a lookup table:
a function that gathers a command line - it's simple; there's no command line history or anything, just the ability to backspace or press escape to discard the whole thing. But if I thought fancier editing capabilities were needed, it wouldn't be too hard to add them here.
a function that parses a line of text argc/argv style (see Parse string into argv/argc for some ideas on this)
a function that takes the first arg on the parsed command line and looks it up in a table of commands & function pointers to determine which function to call for the command, so the command handlers just need to match the prototype:
int command_handler( int argc, char* argv[]);
Then that function is called with the appropriate argc/argv parameters.
Actually, the lookup table also has pointers to basic help text for each command, and if the command is followed by '-?' or '/?' that bit of help text is displayed. Also, if 'help' is used for a command, the command table is dumped (possible only a subset if a parameter is passed to the 'help' command).
Sorry, I can't post the actual source - but it's pretty simple and straight forward to implement, and functional enough for pretty much all the command line handling needs I've had for embedded systems development.
You might bristle at this response, but many years ago we did something like this for a large-scale embedded telecom system using lex/yacc (nowadays I guess it would be flex/bison, this was literally 20 years ago).
Define your grammar, define ranges for parameters, etc... and then let lex/yacc generate the code.
There is a bit of a learning curve, as opposed to rolling a 1-off custom implementation, but then you can extend the grammar, add new commands & parameters, change ranges, etc... extremely quickly.
You could check out libcli. It emulates Cisco's CLI and apparently also includes a telnet server. That might be more than you are looking for, but it might still be useful as a reference.
If your needs are quite basic, a debug menu which accepts simple keystrokes, rather than a command shell, is one way of doing this.
For registers and RAM, you could have a sub-menu which just does a memory dump on demand.
Likewise, to enable or disable individual features, you can control them via keystrokes from the main menu or sub-menus.
One way of implementing this is via a simple state machine. Each screen has a corresponding state which waits for a keystroke, and then changes state and/or updates the screen as required.
vxWorks includes a command shell, that embeds the symbol table and implements a C expression evaluator so that you can call functions, evaluate expressions, and access global symbols at runtime. The expression evaluator supports integer and string constants.
When I worked on a project that migrated from vxWorks to embOS, I implemented the same functionality. Embedding the symbol table required a bit of gymnastics since it does not exist until after linking. I used a post-build step to parse the output of the GNU nm tool for create a symbol table as a separate load module. In an earlier version I did not embed the symbol table at all, but rather created a host-shell program that ran on the development host where the symbol table resided, and communicated with a debug stub on the target that could perform function calls to arbitrary addresses and read/write arbitrary memory. This approach is better suited to memory constrained devices, but you have to be careful that the symbol table you are using and the code on the target are for the same build. Again that was an idea I borrowed from vxWorks, which supports both teh target and host based shell with the same functionality. For the host shell vxWorks checksums the code to ensure the symbol table matches; in my case it was a manual (and error prone) process, which is why I implemented the embedded symbol table.
Although initially I only implemented memory read/write and function call capability I later added an expression evaluator based on the algorithm (but not the code) described here. Then after that I added simple scripting capabilities in the form of if-else, while, and procedure call constructs (using a very simple non-C syntax). So if you wanted new functionality or test, you could either write a new function, or create a script (if performance was not an issue), so the functions were rather like 'built-ins' to the scripting language.
To perform the arbitrary function calls, I used a function pointer typedef that took an arbitrarily large (24) number of arguments, then using the symbol table, you find the function address, cast it to the function pointer type, and pass it the real arguments, plus enough dummy arguments to make up the expected number and thus create a suitable (if wasteful) maintain stack frame.
On other systems I have implemented a Forth threaded interpreter, which is a very simple language to implement, but has a less than user friendly syntax perhaps. You could equally embed an existing solution such as Lua or Ch.
For a small lightweight thing you could use forth. Its easy to get going ( forth kernels are SMALL)
look at figForth, LINa and GnuForth.
Disclaimer: I don't Forth, but openboot and the PCI bus do, and I;ve used them and they work really well.
Alternative UI's
Deploy a web sever on your embedded device instead. Even serial will work with SLIP and the UI can be reasonably complex ( or even serve up a JAR and get really really complex.
If you really need a CLI, then you can point at a link and get a telnet.
One alternative is to use a very simple binary protocol to transfer the data you need, and then make a user interface on the PC, using e.g. Python or whatever is your favourite development tool.
The advantage is that it minimises the code in the embedded device, and shifts as much of it as possible to the PC side. That's good because:
It uses up less embedded code space—much of the code is on the PC instead.
In many cases it's easier to develop a given functionality on the PC, with the PC's greater tools and resources.
It gives you more interface options. You can use just a command line interface if you want. Or, you could go for a GUI, with graphs, data logging, whatever fancy stuff you might want.
It gives you flexibility. Embedded code is harder to upgrade than PC code. You can change and improve your PC-based tool whenever you want, without having to make any changes to the embedded device.
If you want to look at variables—If your PC tool is able to read the ELF file generated by the linker, then it can find out a variable's location from the symbol table. Even better, read the DWARF debug data and know the variable's type as well. Then all you need is a "read-memory" protocol message on the embedded device to get the data, and the PC does the decoding and displaying.

How do multiple languages interact in one project?

I heard some people program in multiple languages in one project. I can't imagine how the languages interact with each other.
I mean there is no Java method like
myProgram.callCfunction(parameters);
never happens or am I wrong?
Having multiple languages in one project is actually quite common, however the principles behind are not always simple.
In the simple case, different languages are compiled to the same code. For example, C and C++ code typically is compiled into machine assembler or C# and VB.Net is compiled into IL (the language understood by the .NET runtime).
It gets more difficult if the languages/compilers use a differnt type system. There can be many different ways, basic data types such as integer, float and doubles are represented internally, and there is even more ways to represent strings. When passing types around between the different languages it must be sure that both sides interpret the type the same or - if not - the types are correctly mapped. This sort of type mapping is also known as marshalling.
Classic examples of interoperability between different program languages are (mostly from the Windows world):
The various languages available for the .NET platfrom. This includes C#, VB.Net, J#, IronRuby, F#, XSLT and many other less popular languages.
Native COM components written in C++ or VB can be used with a huge variety of languages: VBScript, VB, all .NET languages, Java
Win32 api functions can be called from .NET or VB
IPC (inter process communication)
Corba, probably the most comprehensive (and most complex) approach
Web services and other service-oriented architectures, probably the most modern approach
Generally, any decently sized web project will use about five languages: HTML, CSS, Javascript, some kind of server-side “getting things done” language (ASP, JSP, CGI scripts with Perl, PHP, etc.), and some variant of SQL for database connectivity.
(This is, of course, hand-waving away the argument about whether or not HTML and CSS count as programming languages – I’m the “they are, but just not Turing-complete languages” camp, but that’s a whole other thread.)
Some examples of how all those work together:
If you’re going the best-practices route, the structure of a web page is in HTML, and the instructions for how to display it are in CSS – which could be in the same file, but don’t have to be. The CSS contains a bunch of classes, which the HTML refers to, and it’s up to the browser to figure out how to click them together.
Taking all that a step further, any javascript scripts on that page can alter any of the HTML/CSS that is present (change contents of HTML entities, swap out one CSS class for another, change the behavior of the CSS, and so on.) It does this via something called the Document Object Model, which is essentially a language and platform-independent API to manipulate HTML pages in an object-like manner (at which point I’ll back away slowly and just provide a link to the relevant wiki article.)
But then, where does all the HTML / CSS / Javascript come from? That’s what the server-side language does. In the simplest form, the serer-side language is a program that returns a giant string holding an HTML page as its output. This, obviously, can get much more complex: HTML forms and query string parameters can be used as input for our server side program, and then you have the whole AJAX thing where the javascript gets to send data directly to the server language as well. You can also get fancy where the server language can customize the HTML, CSS, and Javascript that gets spit out – essentially, you have a program in one language writing a program in another language.
The Server-side language to SQL connection works much the same. There are a lot of ways to make it both more complex and safer, but the simplest way is for your server language to dynamically build a string with a SQL command in it, hand that to the database via some kind of connector, and get back a result set. (This is a case where you really do have a function that boils down to someValue = database.executeThisSQLCommand( SQLString ). )
So to wrap this up, different languages in this case either communicate by actually writing programs in each other, or by handing data around in very simple easy to parse formats that everybody can understand. (Strings, mainly.)
Multiple languages in use is called "interoperability" or "interop" for short.
Your example is wrong. Java can call C functions.
The language provides a mechanism for interoperability.
In the case of .NET, languages are compiled into IL as part of the CLI. Thus any .NET language can interop (call methods defined by) modules defined in any other .NET language.
As an example:
I can define a method in C#
static void Hello(){ Console.WriteLine("Hello World");}
And I can call it from Python (IronPython)
Hello()
And get the expected output.
Generally speaking, some languages interop better than others, especially if the language authors specifically made interop a feature of the language.
Multiple languages can interact with:
Piped input/output (ANY language can do this
because input and output must by necessity be implemented in every non-toy
language)
Having code in one language compile to a native library
while the other supports calling native code.
Communicating over a loopback network connection. You can
run into difficulties with firewall interference this way.
Databases. These can be thought of as a "universal" data
storage format, and hence can be accessed by most languages
with database extensions. This generally
requires one program to finish operation before the next program
can access the database. In addition, all 'communications' are
generally written to disk.
If the languages involved run on the same runtime (i.e. .NET,
JVM), then you generally can pass object data from one language
directly to the other with little impedence.
In almost every case, you have to convert any communication to a common
format before it can be exchanged (the exception is languages on the
same runtime). This is why multiple languages are rarely used in one
project.
I work on a large enterprise project which is comprised of (at the last count) about 8 languages. The majority of the communication is via an enterprise level message bus which contains bindings for multiple languages to tap into and pass data back and forth. It's called tibco.
There are many different ways you can use different languages in one project
There are two main categories that come to mind:
Using Different languages together to build one application. For example using Java to build the GUI and using JNI to access C API (so answering your question you can call C functions from Java ;))
Using different languages together in the one project if they are not part of the same application. For example. I am currently working on an iPhone app that has uses a large amount of text. I am currently using three languages: Python (to work with the original sources of the text), SQL (to put the results of the python app in a format easily accessible from the iPhone sqlite3 API) and Objective C to build the actual app. Even though the final product will only be Objective C, I've used two other languages to get to the final product.
You could have an app where the bulk of the work is done in Java, but there might be some portion of it, like maybe a data parser or something is written in Python or what have you. Almost two separate apps really, perhaps the parser is just doing some work on files and then your main app in Java is using them for something. If someone were to ask me what I used in this project I'd say "Java and Python."
There are various ways that multiple languages can be used in one project. Some examples:
You can write a DLL in, say, C, and then use that library from, say, a VB program.
You could write a server program in, say C++, and have lots of different language implementations of the client.
A web project often uses lots of languages; for example a server program, written in, say, Java (a programming language), which fetches data from a database using SQL (a query language), sends the result to the browser in HTML (a markup language), which the user can interact with using Javascript (a scripting language)...
Badly. If there is no urgent need, stick to a single language. You're increasing dependencies and complexity. But when you have existing code providing interesting functionality, it can be easier to glue it together than to recreate it.
It depends on the type of project. If you want to experiment, you can set up a web project, in .NET, and change the language on a page by page basis. It does not work as you show in your pseudocode, but it is multiple languages. Of course, the actual code directory must be a single language.
There are a couple of ways in which code in languages can interact directly. As long as the data being passed between the code is in the right format, at the bits and bytes level, then there is no reason why different languages can't interop. This approach is used in traditional windows DLL development. Even on different platforms, if you can get the format correct (look at big/little endian if interested) it will work as long as your linker (not compiler) knows how to join code together.
Beyond that there are numerous other ways in which languages can talk to each other. In the .Net world code is compiled down to IL code, which is the same for every language, in this way C#, VB.Net are all the same under the hood and can call/work with each other seamlessly.
Just to add to the list of examples, it's fairly common to optimize Python code in C or C++ or write a C library to bind another library to Python.

Resources