C grunt work - how to automate with BBEdit?

39 views
Skip to first unread message

Sam Hathaway

unread,
Jan 28, 2020, 3:45:41 PM1/28/20
to BBEdit Talk

There are two tasks I end up doing manually in C a lot and I’d like to not.

Thing one: putting guards around header files.

Any module.h should start with:

#ifndef MODULE_H
#define MODULE_H

and end with:

#endif /* !MODULE_H */

I usually get a few minutes into writing the header file and then remember that I need have an include guard. I’d like to be able to hit a key or select a menu item in order to add it. The name of the guard should be derived from the file name.

Thing two: maintaining function prototypes in header files.

Given a file.c, prototypes for all non-static functions should exist in the counterpart file.h.

So given file.c:

void file_init(int a, int b)
{
    /* stuff here */
}

static void helper(void)
{
    /* more stuff */
}

void file_scan(int a, int c)
{
    /* and again */
}

The corresponding file.h should look contain:

void file_init(int a, int b);
void file_scan(int a, int c);

Again, I’d like to, from a C source file, hit a key or select a menu item to add prototypes in the corresponding C header file. Bonus points if the command creates the header file if it doesn’t exist yet, and adds the appropriate include guard. More bonus points if it can update existing prototypes if I change the return type or parameter list.

Does that all make sense?

I imagine AppleScript would be involved.

Don’t feel like you have to write this for me, but if you already have tools to do this (or part of this) please point me in their direction.

Thanks for any ideas!
-sam

Steve deRosier

unread,
Jan 28, 2020, 3:54:52 PM1/28/20
to bbedit
For the include file guards, I'd suggest either looking at the
Stationary or Clippings features. Both should easily do what you want
to varying degrees.

- Steve
> --
> This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
> ---
> You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/75A92463-3871-4AD6-8546-F8DEFF1DCA2B%40munkynet.org.

GP

unread,
Jan 29, 2020, 8:46:09 AM1/29/20
to BBEdit Talk


On Tuesday, January 28, 2020 at 12:45:41 PM UTC-8, Sam Hathaway wrote:

There are two tasks I end up doing manually in C a lot and I’d like to not.

Thing one: putting guards around header files.

 

[snip]

 

Thing two: maintaining function prototypes in header files.


[snip] 

Look at the "Filters and Scripts" section of BBEdit's User Manual.  Amongst other environment variables, BBEdit sets BB_DOC_NAME and BB_DOC_PATH. With those you can easily check for the existence of the counterpart header and if it doesn't exist create one with the appropriate include file guards.

If it was me, I would use Perl for the scripting language since that allows you to use the same regular expression as you would use in BBEdit to search for C function headers in your .c file(s). How easily that will be depends largely upon how clean or ugly you write C code. (Without testing it, I think you can reject static function headings using a negative lookahead/lookbehind on static in the function heading.) I don't know if it is still the case, but BBEdit at one time shipped with a C function header grep pattern in the find/search dialogs.  You could use that as a starting point to work up to a regular expression that will find just the function headings in a .c file that you need/want to generation function prototypes for. 

I also wouldn't get too fancy with the prototype updating. Todays machines have cycles to spare so just regenerate the whole header contents anew, compare old with new, and then replace the old with the new if there is a difference between the two. Exactly how fine grained the replacing needs to be depends upon how you structure your header files and what else (e.g., comments and other non-function declarations) you put in the files. 

Reply all
Reply to author
Forward
0 new messages