vpac[Vim-Perl-Asm-C/C++] A Programmer's Aid

By Mike Lear

 

Contents

1.Introduction 2.Overview 3.Install Instructions 4.Sample Code 4.1.Listing: 4.2.Function Keys: 4.3.Example Build: 4.4.The VM Function: 5.Download Package 6.Conclusion

1.Introduction

As a Linux and Vim user I like to write programs and sub routines in various programming languages. I developed vpac as a series of programming aids for the following languages. [Perl, gcc, g++ and asm] using Vim as the focalpoint. vpac and its related files allow you to compile,debug and run programs all within one Vim session. Another feature is the ability to comment/uncomment existing code with a single keystoke. vpac works like a global makefile in that you can build several small files into one executable file. It detects the programming language that you are using and selects the appropriate compiler/assember, vpac can carry out mixed language builds.[c/c++ and asm] It is easy to use, the commands are all run from the Function keys. vpac comes as a tar-gzipped package which includes various "Howto"s and all of the source code.

Back to Contents

2.Overview

The vpac allows you to compile, edit, debug or run a program all inside the Vim shell. It selects the correct [compiler-assembler-linker-debugger] by examining the file extension of the source code. By pressing various function keys you can choose to compile your program. Activate the Quickfix window at the bottom of your source code window. Scroll up and down the error list and edit(fix) any errors. You can also run the finished program in Vim with or without command line arguments. Pressing another function key loads up one of the debuggers[perl,gdb,ald]. If the program you are editing is assembly, you can switch between using gdb or ald debugger. The CTRL X and Y keys allow you to comment-uncomment sections of code.[Useful when debugging]. The comments for Perl,C++,C and ASM are all different.

			Perl comments  -->  #
			C++  comments  -->  //
			C    comments  -->  /* ... */
			Asm  comments  -->  ;

So the correct type can be selected by use of vpac. Read the VM function further down in this document. You can compile several files together using the vpac, and just like Make it will skip files that are up to date. vpac uses the [F8] key to show all the compile-link options. This works in a similar fashion to the make -n option. ie: If all files are upto date nothing is displayed.

Back to Contents

3.Installing

Before you download and install this program your computer should have installed and running the following programs. Perl, Gcc, G++, Gdb, Nasm and Ald. Most Linux versions usually have the C/C++ and Perl installed. For Nasm and Ald visit the Nasm Homepage, there you will find links to the download sites. vpac was written and tested using RedHat 9 and gcc version 3.2.3. Most modern Linux builds incorporate gcc, so if you are using an earlier version you might need to upgrade to this version. (Red Hat Linux 3.2.3-49) vpac will work without Nasm and Ald but it's useful for writing sub routines.

Install Example:


  User jack would download and install to:
	/home/jack/some_dir/
  then
	gzip -df vpac-0.2.tar.gz
	tar xvf vpac-0.2.tar
	cd vpac-0.2
  Next read the README and ./doc/howto for more information on installing.
  Having read the install proceed to:
	./configure
	make install
  This will create 3 small sub directories under
	/home/jack
	~/etc ~/bin and ~/man
  In your $HOME/.bash_profile file add the bin directory.
	PATH=$PATH:$HOME/bin
	export PATH
  Open a new window and at the prompt type in vpact [ret]. If it replies
  with OFF. Then your Path is setup correctly for vpac.
  That's assuming you are using a bash shell.
  If you run man vp at a command prompt the man page details for vpac
  will be displayed.
  Finally make a backup of your .vimrc file
  Then copy vpac-0.2/VIMRC to /home/jack/.vimrc
  That concludes the install process.

Back to Contents

4.Sample Code

During the course of writing vpac I found I needed to take a list of the source files and examine the age of each file against any header,macro or include files that were entered during the build files prompt. I therefore would have to save the source files age prior to opening it and then restore the file age back after testing it against the header file. I also wanted to flag each individual source file with its extension type. The command boolstrstr is a locally written function which compares strings. Just like a needle in a haystack. I used the actime and modtime functions from utime.h to restore the original file times. The switch at the end of this code flags the file type(not shown). I have not included all the error checking ect in this snippet of code. You can view the actual code in the opt.cc file which is part of the vpac package. But the main function of the code is listed below.

Back to Contents

4.1.Listing:

  1. int Options::find_h_files(string & sourcestr,string & headerstr) { unsigned int dot;
  2. string filename,headername,ext,srchstr;
  3. std::istringstream instr(sourcestr);
  4. while(instr >> filename){
  5. std::istringstream hinstr(headerstr);
  6. while(hinstr >> headername) {
  7. if(stat(filename.c_str(),&statbuf) < 0) { continue; }
  8. fin.open(filename.c_str());
  9. if(!fin.is_open()) { sys_err("Open File Error"); }
  10. while (getline(fin,srchstr)) {
  11. if(boolstrstr(srchstr,headername) == 0) { ans = 1; break;}
  12. } fin.cVP(); fin.close();
  13. timebuf.actime = statbuf.st_atime;
  14. timebuf.modtime = statbuf.st_mtime;
  15. dot = filename.find(".");
  16. if(dot==string::npos) { sys_err("Unsuitable file type"); }
  17. ext.assign(filename,dot,filename.size());
  18. dot = file_types.find(ext);
  19. switch(dot) {

This is only a small section of the code used, and does not show all of the error checking ect which would have made the code more difficult to follow.

Back to Contents

4.2.Function Keys:

Function keys used in vpac:

[F12] Debug Mode

Debug mode will place your program in gdb,perldb or ald debug depending on the name of the file you are working on. If you are using nasm [assembly language] you can select with the [F8] key the GDB or ALD debuggers.

[F11] Execute Program

Run the program in the vim window. If the finished program requires command line arguments again the [F3] key allows these to be entered prior to running.

[F10] Make Mode

Build command this selects the compiler/assemberinterpreter dependent on the program filetype entered. Any errors during the build are placed in the Quickfix window. This functions acts like a Makefile in that any files that are upto date are not re-compiled. Once again the [F3] key allows you to enter additional files for the build process.

[F9] Save File

This is the normal Vim save file feature. The [F10] key uses the Vim autowrite feature to save the work prior to a build also.

[F8] View Options

Pressing [F8] to the ON position allows with the aid of the [F10] key a means of viewing the operations that the [F10] key would run under normal use. It is very similar to the make -n option. It will only show you the files and commands that need to be run if you were doing a build. Like the Makefile if all the files in question are upto date then there will be no output. For normal use [F8] should be in the OFF position. [F8] is also used to switch between ald and gdb debuggers whilst debugging assemble lanuguage.

[F7] Close Quickfix Window

This key closes the Quickfix window.

[F6] Open Quickfix Window

This key pops the Quickfix window up at the bottom of your current window.

[F5] Scroll Down Mode

Scroll down the Quikfix error window. This allows you with the aid of the [F4] key to fix syntax errors.

[F4] Scroll Up Mode

Scroll up the Quikfix error window. Whilst in the Quickfix windows you can use the cursor to obtain the hex and ascii values of any character. Move the cursor to the position of the character and the values will appear at the right of the status line.

[F3] Toggle Modes

[F3] is used with the previous keys to toggle options ON or OFF it displays its condition every time it is used. An example being if you wanted to build more than one file pressing the [F3] ON will, once the [F10] is pressed bring up a prompt saying Enter build files:. Whereas if [F3] is OFF then the prompt would not appear and the build would be for a single file.i

[F2] Exit vpac

This key is the standard Vim quit command. Note it does NOT save your work.

[F1] Not Used

Not used by vpac

[Ctrl X] Comment Code

Using this key in the command mode allows you to quickly comment out lines of code. It works across whitespace boundries. The comment type is dependent on the file type you are programming.

[Ctrl Y] Uncomment Code

This key is the opposite function to the previous key. It will uncomment the code previously set with the Ctrl-X key. It works across whitespace boundries. Further information on the use of these two control keys and how to select the different comment types can be found in the howto documents that come with the vpac package.

Back to Contents

4.3.Example Build:

Example Mixed Language Build:

Imagine that you have the following files in a subdirectory along with any associated local header files. secure.c ezshell.c view.asm runp.c crypt.o Normally you would either write a Makefile or compile/assemble each file then link the resulting object files to create the executable secure. This is where vpac can assist you in the build process. First you must cd to the subdirectory where the files are listed. vpac will search only the current directory for all of the build source files. vpac examines each file in turn, calling the appropriate compiler-assembler to build the object files. Just like Make, they will only be built if they are not up to date. Compiler-assembler options are stored in small scripts in the $HOME/etc directory. You don't normally need to touch these files unless you are adding or removing options. Read up on the use of these script files in the howto.txt that comes with the vpac package. Back to the Example Compile: In a window run. vi secure.c or vm secure.c if you also wish to set commenting to c style at the same time. With the file loaded in the window press the [F3] key. Each time you press the key it will display ON or OFF. In the OFF position if you pressed the [F10] key it would compile your program and not ask you for the other files to build. So the [F3] OFF mode is only used to build single stand- alone files. Ensure that the [F3] is ON. Now press the [F8] key also make sure this key is ON. (we are just checking the build options first) NOTE: When the [F8] Key is in the ON mode then only files that require updating will be displayed. Like "make -n" it only displays the options and does not run them. If all files are up to date then nothing is displayed. To use press [F8] to the ON position then press the [F10] key. For normal file building the [F8] key should be in the OFF position. A prompt will appear [Enter build files:] At this prompt, enter the other four file names, each filename seperated by a space. Enter build files: ezshell.c crypt.o view.asm runp.c press [enter]. The build options will be displayed.

   :!vpac secure.c  2>&1|tee  mikes_2065
   Enter build files: ezshell.c  view.asm  runp.c
     gcc -c  -Wall -Werror -O2 -c -g ezshell.c 2>&1|mlfilter|tee
     nasm -g -i/usr/include/ASM/ -f elf view.asm 2>&1|mlfilter|tee
     gcc -c  -Wall -Werror -O2 -c -g runp.c    2>&1|mlfilter|tee
     gcc -c  -Wall -Werror -O2 -c -g secure.c  2>&1|mlfilter|tee
     gcc -o secure secure.o ezshell.o crypt.o view.o runp.o  -lstdc

A quick note about locally written header or macro files.(.h,.inc or .mac) If for example your program secure.c had a locally written header file called rc4.h. You can optionally include this file at the build files prompt. vpac will examine the file age of any include-macro or header files and will update the associated file[s]. In this case if rc4.h is more recent than secure.c or secure.o it will re-compile the secure.c file. All locally written header files must be in the current directory along with all source files.

To make changes to the compile-assemble or link options. Read the howto.txt that comes with this package. Also further information can be found in the actual files in your $HOME/etc subdirectory. Once you are satisfied with the build options, press the [F8]key OFF and then press [F10]. Re-enter the build filenames at the prompt and press the enter key. The files will be built in the sequence shown by the [F8] key, finally linking to form the secure executable file. The [F11] key enables you to run the completed program. Any Command line arguments to secure may be entered with the [F3] key ON. If the [F3] is OFF, [F11] will not ask you for arguments and will just run the program. If the file secure.c failed its build with syntax errors press the [F6] to bring up the Vim(Quickfix) window which is set at 6 lines. You can adjust the window size in the .vimrc file. By pressing [F5] and [F4] keys you can scroll through the errors in your code. As you scroll the cursor will jump to the line where the compiler complained of a syntax error. Re-edit any errors and press the [F7] then [F10] key again and repeat the previous operations. [F7 closes Quickfix window]. Continue to compile and check the Quickfix window until all syntax errors are fixed. Whilst in the error mode the status line displays the ascii decimal and hex codes of the character at the current cursor position. Having fixed the syntax errors and run the resulting executable. It might still fail with an exception error. Time to step through your code and locate the bug. The [F12] is used to enter debug mode. All of the debugger commands can used in this mode. like load - set args - step ect. The [F8] is used in assembly language debugging to select either the ald or gdb debuggers. There is a file called gdbinit in the $HOME/etc directory which in [S]step mode in assembly displays the main processors registers and 6 lines of the program counter. You can edit this file to your own preferences.

You can comment out sections of code with the use of the [Ctrl-X] key. This is useful whilst testing and debugging. The [Ctrl-Y] key will remove the comments without effecting the format(layout) of your program. Select the correct type of comment to use by starting your program with the vm alias command.

Notes on the Enter Build Files command: If you were compiling a single file and by mistake the Enter Build Files: prompt appears ([F3] was on). Press the enter key and the compile will continue to build. vpac will ignore empty parameters to the build files command. The command will also accept object files with the .o extension. You can intermix object,asm and c/c++ files along with locally written header,macro or include files on the command line. Always use the F8 key to view the way that vpac will build your files, sometimes the order of files entered has to be changed so that they build without errors. If you have problems with a build. Try using a normal Makefile to ensure that they can be built. If a Makefile also fails then vpac certainly will. Entering a filename that cannot be built by C/C++ or Asm will cause the build to fail. Do not enter a path and filename, as in /nasm/view.asm this will fail because vpac expects all the source files including any header files that you have written to be in the current directory.

   So to recap. 
	The alias vm is used to set comment type.
	[Ctrl A] also sets the comment type.
	[F3][F8][F10] used to check on the options.
	[F3][F10] build commands.
	[F4][F5][F6][F7] quickfix commands.
	[F11][F3] run commands.
	[F12][F8] debug commands.
	Ctrl-X Ctrl-Y comment-uncomment commands.
	[F2][F9] exit save commands.

Finally a tip for linking assembly with c++. Normally the c++ compiler will struggle to link in an assembly routine that isn't properly defined. So to make sure it finds your asm routine (function) first in the assembly function declare it as Global. Then in your c++ program where it will be used place the following just below your namespace line.

// c++ program:
 using namespace std;
	extern "C" {
		int crypt(void); // external asm function
	}
...
...	
int main(int argc,char *argv[]) {
// Then in main call the nasm crypt function
	...
	crypt();
	...

Then at the start of your asm program:(crypt.asm) make the function crypt global just above section .text

 global crypt
 section .text 
 %define stdin 0
 %define stdout 1
 ...

 ;;; then instead of _start:
 ;;; _start:	push	ebp
 ;;;	mov	 ebp,esp
 ;;; use the function name. In this case crypt

 crypt:	push	ebp  ;;; start of crypt asm function
	mov	 ebp,esp
This will cause the c++ compiler to switch to the C compiler for the asm function crypt. It will then link in sucessfully with your c++ program. In practice when using vpac nasm would build the crypt object file then the c++/c compiler would carry out the linking.

Back to Contents

4.4.The VM Function:

Whilst testing a stubborn bug it is useful at times to quickly comment out sections of code to eliminate it from the problem. This is where vm comes to your assistance. During the vpac installation a small csh script called vip.csh was placed in your $HOME/bin subdirectory. This file along with the executable vpacm(stands for vpac commenter) allows you to use the Ctrl-X and Ctrl-Y keys to comment out or uncomment your code. Vm is just an alias for the vip script and is used to set the comment type to the main source file type. The mappings are as follows :-

	vm filetype.c        maps to /*                 */
	vm filetype.cc       maps to //
	vm filetype.asm      maps to ;;;
	vm filetype.pl       maps to ##
Before using these maps you must edit your $HOME/.bashrc file and enter the following line.

	 alias vm='vip $1'
Now when starting a vim session instead of entering vi somefile.ext use vm somefile.ext, where ext can be [c,cc,asm or pl] This will map the the Ctrl-X and Ctrl-Y keys to the correct type of comments for that file. vpacm uses a small buffer area set aside in the .vimrc file to store the new mappings. This buffer is overwritten by vpacm each time you select a different mapping. The buffer is exactly 83 bytes in size. The Ctrl-X and Ctrl-Y keys do not change the format-[layout] of your program and will comment across white space [blank lines]. Further information on vm vip.csh and vpacm can be found in the howto.txt that comes with this package.

Back to Contents

5.Download Package

You can download the vpac package here

IMPORTANT Download and build as a user. NOT as root!. The executables in this package make use of shells, they have been designed to work in a users environment and running Vim with shells as root is a security risk. Vpac detects the uid(users id) and will not work if uid=0(root).

Back to Contents

6.Conclusion

vpac acts similar to a Makefile. The difference being in that a Makefile works to a fixed set of rules. Whereas vpac uses a series of single line scripts stored in the $HOME/etc/ directory. This allows vpac to work globally in any sub-directory. You can check the various options using the [F8] and [F10] keys. vpac makes the programming cycle easier by the fact that it selects the correct compiler/assember to run your programs. All of the main programming commands are controlled by the use of function keys, this saves you leaving a Vim session to debug or run the program. I have been using various prototypes of vpac over the past 3 years or so without any problems. I actually built and tested this version of vpac using version 1 of vpac. It is ideal for writing-compiling several files, but for the larger projects then a normal Makefile should be used. Makefiles are, and always will be far better to use when you have many files and executables to build, vpac is meant for speeding up the Edit-Compile-Debug cycle. I hope you find it a useful set of tools to add to your collection.

Mike Lear.

Back to Contents