C programming from shell

C programming from shell

am 29.09.2007 18:12:12 von Outspan

Hi,

I'd like to give C programming from shell a try. I thought about
vim+gcc, so i added these lines to ~/.vimrc:

syntax on
set et
set sw=4
set smarttab
set nocp incsearch
set cinoptions=:0,p0,t0
set cinwords=if,else,while,do,for,switch,case
set formatoptions=tcqr
set cindent

but my question is: isn't there a way to integrate vim and gdb (watches,
breakpoints etc on the shell)? If I got it right, both GdbVim and Clewn
do what I'm looking for, but just on the GUI version of vim. How do you
program from shell? Suggestions?

thanks

Re: C programming from shell

am 29.09.2007 18:38:08 von Stephane CHAZELAS

2007-09-29, 18:12(+02), Outspan:
> Hi,
>
> I'd like to give C programming from shell a try. I thought about
> vim+gcc, so i added these lines to ~/.vimrc:
>
> syntax on
> set et
> set sw=4
> set smarttab
> set nocp incsearch
> set cinoptions=:0,p0,t0
> set cinwords=if,else,while,do,for,switch,case
> set formatoptions=tcqr
> set cindent
>
> but my question is: isn't there a way to integrate vim and gdb (watches,
> breakpoints etc on the shell)? If I got it right, both GdbVim and Clewn
> do what I'm looking for, but just on the GUI version of vim. How do you
> program from shell? Suggestions?
[...]

There is a plugin for C in vim, so you don't need to do all the
above by hand.

As for integrating vim with gdb, there's
http://clewn.sourceforge.net/ (which I've not tried).

But you can also use gdb -f and a filter in GNU screen that
intercepts the output of gdb and sends intructions to vim in
another screen window (or window pane).

Before realising it could be done with screen filters, I once
had come up with
http://www.cygwin.com/ml/gdb-patches/2003-03/msg00471.html

Here is an example of a filter that can be used in screen:

/*
GNU screen filter that detects the line informations ouput by gdb
when invoked with gdb -f (for emacs mode initially).

When such an information is detected, the $GDB_SCRIPT command is run
with that information as first argument:

"source FILENAME:LINE:CHARACTER:MIDDLE:ADDR"

where FILENAME is an absolute file name indicating which source file,
LINE is the line number within that file (where 1 is the first line
in the file), CHARACTER is the character position within the file
(where 0 is the first character in the file) (for most debug formats
this will necessarily point to the beginning of a line), MIDDLE is
`middle' if ADDR is in the middle of the line, or `beg' if ADDR is at
the beginning of the line, and ADDR is the address in the target
program associated with the source which is being displayed. ADDR is
in the form `0x' followed by one or more lowercase hex digits (note
that this does not depend on the language).

A script example could be:

#! /usr/xpg4/bin/sh -
set -f
IFS=:
set -- $1
screen -X msgminwait 0
screen -X at VIM stuff ":e +/\\\\%$2l. $1
"
screen -X msgminwait 1

"at VIM" implies that the window where vim is running is named "VIM"
(use A in screen to name a window).
*/

#include
#include
#include
#define BUF_SIZE 2048
#define DEFAULT_SCRIPT "parse-gdb.sh"
int main() {
char c, *p, *cmd, *home, *line_end;
char line[BUF_SIZE];

/* the script to be run when an information line is detected from gdb
* output is in $GDB_SCRIPT or in $HOME/ if
* $GDB_SCRIPT is not defined */
if ((cmd = getenv("GDB_SCRIPT")) == NULL) {
home = getenv("HOME");
if (home == NULL) {
home = "";
}
cmd = malloc(strlen(home) + 1 + strlen(DEFAULT_SCRIPT) + 1);
if (cmd == NULL) {
perror("malloc");
exit(1);
}
sprintf(cmd, "%s/%s", home, DEFAULT_SCRIPT);
}

line_end = line + sizeof(line) - 1;
while (read(0, &c, 1) > 0) {
if (c == '\032') { /* ^Z */
if (read(0, &c, 1) <= 0) break;
if (c == '\032') {
for (
p = line;
p < line_end && read(0, &c, 1) > 0 && c != '\n';
p++
) {
*p = c;
}
*p = '\0';

if (fork() == 0) {
execl(cmd, cmd, line, NULL);
perror("can't run GDB_SCRIPT");
return 1;
}
continue;
} else write(1, "\032", 1);
}
write(1,&c,1);
}
return 0;
}




--
Stéphane

Re: C programming from shell

am 30.09.2007 03:11:56 von Maxwell Lol

Outspan writes:

> Hi,
>
> I'd like to give C programming from shell a try. I thought about
> vim+gcc, so i added these lines to ~/.vimrc:

As I recall, (in original vi) I liked to use
set sm (show match)

as you typed a '}', vi would quickly move the cursor to the matching
'{', and back again. It helped you keep track of nesting.


Also there is
set ai (for auto indent.)
this you may or may not like.

Re: C programming from shell

am 30.09.2007 09:46:29 von Stephane CHAZELAS

2007-09-29, 21:11(-04), Maxwell Lol:
[...]
>> I'd like to give C programming from shell a try. I thought about
>> vim+gcc, so i added these lines to ~/.vimrc:
>
> As I recall, (in original vi) I liked to use
> set sm (show match)
>
> as you typed a '}', vi would quickly move the cursor to the matching
> '{', and back again. It helped you keep track of nesting.
>
>
> Also there is
> set ai (for auto indent.)
> this you may or may not like.

Things have evolved in vim since then. vim has many extensions
for the programmer and especially the C programmer:

- ctags integration (also in vi)
- cscope integration
- looking through #includes for definitions of symbols
- [[, ]], to jump from function to function
- see also *, # to find the next occurrence of the word under
the cursor
- :find, the preview window, :che, completion (^P, ^N, ^Xf...),
the jump history, K...
- syntax highlighting, automatic indentation...

And its help system is very handy.

--
Stéphane