PDA

View Full Version : codeform version 1.2.0 #2



dwks
03-23-2007, 05:34 PM
Codeform, a syntax highlighter [intended] for C and C++.

Codeform's main page: http://dwks.theprogrammingsite.com/myprogs/codeform.htm
Direct download link: http://dwks.theprogrammingsite.com/myprogs/down/codeform.zip

"clip" download link (Windows clipboard utilities): http://dwks.theprogrammingsite.com/myprogs/down/clip.zip

Since the other threads were sadly lost in the recent presumed board restore from a backup, I'm starting another codeform thread. With good reason. Here is a list of the things I have done in the past two days:

(Incomplete) Rules files for highlighting other rules files and phpbb were added. From names.txt:

cfrule codeform rules files
...
phpbb (PhpBB) BBCode (just like vbb but with [color=black] around everything)
The static variables in add_rule() were removed into struct rules_t (as struct prevrule_t pr). There are now no global or static variables in codeform, though there is a rather complex structure that is passed to most functions (at least in part). :) Oh well.
[bugfix] (related to the static variables) A rare memory leak occured when the last rule already existed:

=keyword
int:[:]
int:[[:]]
It was fixed by freeing prevrule_t from main() with a call to the new function free_prevrule().
Several spelling mistakes in the source file were fixed. Some undoubtable remain. :)
[bugfix] Many keywords starting with the same characters would cause some to be ignored under certain conditions due to a problem with find_rule_new():

=keyword
x:[:]
do:*:*
done:*:*
double:*:*
doubles:*:*
doubled:*:*
doubling:*:*
[bugfix] A variable directly following an undefined variable would be ignored, due to an oversight in find_var_replace():

=keyword
int:$(formbb)$(keyworddarkc)$(formba):$(forma)
[bugfix] A memory leak of at least BUFSIZ characters per input file occured. (The leak was the longest line in the file as a multiple of BUFSIZ, rounded up. So if BUFSIZ was 512 and there was a line in the file 530 characters long, 1024 characters would have been leaked.) It was fixed by adding free(line) into read_file().
[bugfix] A memory leak occured for every parameter passed to the program. free_argument() was calling free() instead of free_strings().
[bugfix] When reading rules files with DOS-style newlines under Linux, repeated sections ("*") were taken as literal "*"s. (This was manifest with codeform online.) chomp_newline() was added which chomps '\r's as well as '\n's.
[bugfix] One byte too many was being allocated for every variable name in add_var9).
One call to strlen() was eliminated by making remove_escapes() cooperate with shrink_string(). :)

There are still some things to fix, however: Valgrind still shows a few hundred bytes leaked for very complex rules files (down from several thousand, thanks to all of the memory leak bug fixes). (It's the first time I've ever used Valgrind; it's amazing.) I'm pretty sure there's something up with the previous-rule code in add_rule() et al.

HOWTO.txt is sort of out of date.

I want to add Perl support (a rather large undertaking, don't expect it within days ;) ).

Also see the TODO list for other things that I still need to do:

/*-------------------------------------------*\
| TODO list for future versions of codeform |
\*-------------------------------------------*/

Short-term
- ? Optimise rule searching by looking in the previous position first
- Add support for "\n" and "\param" in specific comments ("" and /*! */)
- Make a "function" rule: \w+\s+\(
- ? Don't print closing tags for same-coloured nested comments
- Provide a default rules directory that is searched first
- Make "escaped newline" (\) character specifiable
- Don't count #es in #defines as new comments;
don't allow nestcoms to include themselves
[done] - Allow multiple prev pointers!
[done] - free static variables in add_rule()

Long-term
- Make input, output and styles rules files separate

Distant long-term
- Support indent-style code beautifying

I haven't forgotten about the Win32 functions, don't worry, but the lists are gone now so it will take me a while . . . .

Codeform online still isn't working. Any ideas? How can I run a program as a restricted user from a priviledged Perl script?

kryptkat
03-23-2007, 05:56 PM
Would you like me to put the win api list back up?

dwks
03-23-2007, 06:14 PM
Sure. Or you could create the codeform rules files yourself if you're feeling up to it. :D

Perhaps something like this:

=keyword

keyworddarkb=
keyworddarka=

auto:$(keyworddarkb):$(keyworddarka)
bool:*:*
break:*:*
case:*:*
...
In fact, I have a Perl script for you:


#!/usr/bin/perl

print "=keyword\n\nkeyworddarkb=\nkeyworddarka=\n\n";
chomp($l = <>);
print "$l:\$(keyworddarkb):\$(keyworddarka)\n";

while(<>) {
chomp;
print "$_:*:*\n";
}

Note: it's untested. Okay, now I've edited the code and tested it. It works pretty well. It assumes an input file with one keyword per line.

kryptkat
03-23-2007, 06:20 PM
=keyword

keyworddarkb=
keyworddarka=

auto:$(keyworddarkb):$(keyworddarka)
bool:*:*
break:*:*
case:*:*
...


i have no clue what that is or does.... <i know it is perl but....>



#!/usr/bin/perl

print "=keyword\n\nkeyworddarkb=\nkeyworddarka=\n\n";
$l = <>;
print "$l:$(keyworddarkb):$(keyworddarka)\n"

while(<>) {
print "$_:*:*\n";
}


have no perl compiler....

dwks
03-23-2007, 06:24 PM
I'll run it for you then. (It's a Perl interpreter BTW.) Post the list (as an attachment!) if you have it handy. If not, I'll start looking . . . .

kryptkat
03-23-2007, 06:36 PM
put it all on one file.txt



note to self.... dx9 and gdi need to add

dwks
03-23-2007, 06:56 PM
Here it is . . . generated by this:

#!/usr/bin/perl

print "=keyword\n\nkeyworddarkb=\nkeyworddarka=\n\n";
chomp($l = <>);
$l =~ s/\s*\(?\s*$//;
print "$l:\$(keyworddarkb):\$(keyworddarka)\n";

while($l = <>) {
chomp($l);
$l =~ s/\(?\s*$//;
if($l =~ /\/\*(.*)\*\// || $l =~ /(. Functions)/) {
print "# $1\n";
}
elsif($l ne '') {
print "$l:*:*\n";
}
}
I call it rules/winapi on my machine. It's used like so:

$ ./codeform -f rules/cpp_vbb_1 -f rules/winapi ...
It's untested BTW. Let me know if you have problems with it. The latest upload of codeform segfaults quite often for no apparent reason . . . not a good sign. :)

Stupid attachments. You can't have a file with no extension . . . remove the .txt.

twomers
03-23-2007, 07:02 PM
Might be worth putting in some documentation about the clipboard programs too so people can use your code formatter along with your clipboard thing.

kryptkat
03-24-2007, 05:24 AM
tested it. it hangs. let it run all night just to see if it was slow or not working. 9pm to 7:30am is enough time to know that it crashed.

first thought colins on math functions
second thought two rule files
third thought comments in file.
fourth thought perl script messed up
fifth thought multiple references of the same functions

manually edit win api file retry with manually made rule file.

other thoughts as to why it crashed?


how large of .c file can it handle?

kryptkat
03-24-2007, 10:26 AM
care to try that again?

here is the list sorted with the duplicates removed and the comments removed. it is ready for a perl script to turn it to a winapi rule file.

note there is gdi in there hbitmap and bitblit i think all of it .

dwks
03-24-2007, 01:52 PM
first thought colins on math functions
second thought two rule files
third thought comments in file.
fourth thought perl script messed up
fifth thought multiple references of the same functions
Colons in function names would definitely mess codeform up -- they need to be escaped.
Comments in file beginning with '#' are fine -- "/**/" comments are not. I think I got rid of them though.
The Perl script may well have messed up. :)
Multiple functions of the same name would cause the duplicates to be ignored (only the first one counts).



how large of .c file can it handle?

At least 64 KB -- that's the largest I've tested -- but I know for a fact that its line length is theoretically unlimited (5,000,000 character lines on an old computer). If you find a larger source file, feel free to test it. There's no reason why a larger file would crash it however.

I expect the problems you encountered were due to a bug in the codeform I uploaded . . . I've re-uploaded it again, having done the following changes last night (it was a late night...):
Changes
The line in add_var()

vars->data[pos]->from = malloc(eq - str);
becomes

vars->data[pos]->from = malloc(eq - str + 1);
Despite the reassuring comment, the previous version did have a buffer overflow and I suspect that was what was causing the segmentation faults.

free_onerule()'s free() goes inside the if statement -- might make codeform very slightly faster ;)

Added

&& x <= ruledata->data[y]->data.number
to free_onerule()'s inner if statement -- it might do something on rare occasions.

(minor change) In add_rule, the return value of the call to add_allocated_rule() is saved and pr->freep is set to it.

Fixed yet another memory leak due to a bug in add_rule().
This line is added to free_dup_onerule() to get rid of a small memory leak when keywords of the same name were added:

free(rule->data.len);

Re-written free_rulecdat() completely to get rid of a serious memory leak to look like this:


void free_rulecdat(const struct rules_t *rules) {
enum type_t x;
size_t y;

for(x = 0; x < TYPES; x ++) {
if(rules->cdat[x]) {
for(y = 0; y < rules->cdat[x]->data.number; y ++) {
free(rules->cdat[x]->data.data[y]);
}

free(rules->cdat[x]->data.data);
free(rules->cdat[x]->data.len);
free(rules->cdat[x]);
}
}
}

I did a few other things too, not worth mentioning here.

Valgrind now reports no memory leaks for codeform! Even for rules/c_1_css. But it does report "invalid read"s, whatever that is. (Enlighten me?)

I will indeed re-create the winapi file and test it this time (I'm on the right computer). I'll report back in a few minutes.

dwks
03-24-2007, 02:06 PM
Okay, download the latest version; rules/winapi is included in it. I tested it with this command line and it worked:

C:\>codeform -f rules\_html -f rules\c_1ext -f rules\winapi rules\winapi
It increased the size of the .ZIP by a bit. :)

For nicer colours try

C:\>codeform -f rules\_html -f rules\c_1 -f rules\winapi rules\winapi
Here's a sample (with _vbb of course instead of _html):

=keyword

keyworddarkb=
keyworddarka=

AbnormalTermination:$(keyworddarkb):$(keyworddarka )
AbortDoc:*:*
AbortPath:*:*
AbortPrinter:*:*
AbortProc:*:*
ABORTPROC:*:*
AbortSystemShutdown:*:*
accept:*:*
AcceptEx:*:*
AccessNtmsLibraryDoor:*:*
ACMDRIVERENUMCB:*:*
ACMDRIVERPROC:*:*
ACMFILTERCHOOSEHOOKPROC:*:*
ACMFILTERENUMCB:*:*
ACMFILTERTAGENUMCB:*:*
ACMFORMATCHOOSEHOOKPROC:*:*
ACMFORMATENUMCB:*:*
ACMFORMATTAGENUMCB:*:*
...etc...


The executable included in the .ZIP is not compiled with my normal compiler but it should still work.

The linux executable included is ancient. If you're using linux just compile it yourself. Type "make clean" and then "make". ;)

kryptkat
03-24-2007, 03:16 PM
If you find a larger source file, feel free to test it.

<very big grin>

was looking over your changes....good to know. i do not have linux.

the winapi file is large and i hope it will add to the progs popularity. it has helped fix a few progs already. if i had realized you were going to run it through a prog to make the rule file i would have plucked cleaned stuffed and baked the file in the first place. thank you.

dwks
03-24-2007, 04:05 PM
I used another Perl program that basically takes codeform.c and copies it 100 times into another file. Here are the results:

timeit: 14285 ms

Volume in drive [edited]
Volume Serial Number is [edited]

Directory of [edited]

24/03/2007 03:59 PM 7,098,100 bigfile
24/03/2007 03:59 PM 20,343,374 bigfile.htm
2 File(s) 27,441,474 bytes
0 Dir(s) 56,410,112 bytes free
Not bad . . . :) A 7MB file was processed in 14.3 seconds (admittedly, it was in the background but this is a really fast computer).


<very big grin>

was looking over your changes....good to know. i do not have linux.

the winapi file is large and i hope it will add to the progs popularity. it has helped fix a few progs already. if i had realized you were going to run it through a prog to make the rule file i would have plucked cleaned stuffed and baked the file in the first place. thank you.
Thank you for the list. :) All I did was write a program to parse it and tested and uploaded it . . . you did all of the work.

Sorry that the change list is not very legible. I wrote it as I was coding and never intended to put it on the internet or anything.

zacs7
03-31-2007, 04:29 AM
DWKS, perhaps look into the <pre></pre> tags and keep tabs?, Rather than converting them to spaces...?

Think about it, its an extra 3 bytes per tab... It all adds up. Not to mention it makes the highlighted source easier to "cut & paste" while keeping its original formatting.

Anywho just thought i'd comment.

dwks
03-31-2007, 02:09 PM
DWKS, perhaps look into the <pre></pre> tags and keep tabs?, Rather than converting them to spaces...?

Think about it, its an extra 3 bytes per tab... It all adds up. Not to mention it makes the highlighted source easier to "cut & paste" while keeping its original formatting.

Anywho just thought i'd comment.
Actually right now codeform prints all whitespace verbatim into the output file. Any tabs, spaces, or newlines in your source file will also be in the output file.

The HTML output format does indeed use <pre></pre> tags. (See the files rules/_html.)

If you wanted to have tabs converted to spaces or vice versa you could either do it yourself with a separate program -- it's quite easy; I think I have at least five programs that do it -- or create rules that look like this:

=keyword
<tab>::<space><space><space><space>:# converts tabs to spaces
<space><space><space><space>:<tab>:# converts spaces to tabs
Note that I don't know if that will work; keywords containing spaces might not match properly. (And codeform isn't on this computer so I can't test it.) It should however and if it doesn't just give me a shout.

Comments are much appreciated. :)

zacs7
03-31-2007, 05:13 PM
You should make a CGI version to perhaps? :)

dwks
03-31-2007, 05:58 PM
Guess what? I was doing just that. If you'd signed up before the board crashed (http://cboard.cprogramming.com/showthread.php?t=87796), I could link you to the thread, but alas it is gone. I wrote a Perl script which ran codeform on the remote server. It worked perfectly, but codeform was running as a priviledged user, opening up tons of security holes. Unfortunately there doesn't seem to be a user called nobody on that server. So I guess I could host codeform version 1.2.0 online, as it is called, but would it be a good idea? . . . .

My only idea was to restrict the input file to, say, 32KB, to reduce the risk of buffer overruns. Codeform, as I mentioned, does not have any memory leaks when run through Valgrind, but it does have lots of "Invalid read"s. I don't know what those are but I'm guessing they're related to buffer overruns. Once I fix those I think I will host codeform online. We'll see.

If you have a server that will run Perl scripts that can execute other programs which may or may not have buffer overruns, let me know. ;)

zacs7
03-31-2007, 06:14 PM
I did sign-up before the board crashed :( I lost my account...

Anywho, as you mentioned in the other thread perhaps link to standard-c functions ?

And maybe something for the future: use anchors for user declared functions so you can 'skip' to their declarations, eg



<pre>

void Foo(void);

int main(void)
{
<a href="#foo">Foo</a>();
return 0;
}

<a name="foo"></a>
void Foo(void)
{
return;
}

</pre>


Sorry, I don't currently have access to a server :(, specially not one with, DuN-DUN-DUN PERL!:P

dwks
03-31-2007, 06:23 PM
I did sign-up before the board crashed :frown: I lost my account...
That's too bad. All I lost was 300 posts. You lost your join date etc . . .


Anywho, as you mentioned in the other thread perhaps link to standard-c functions ?

Actually I just did that really quickly . . . here's the gist of it.

linkfuncbb=
linkfunca=
linkfuncurl=http://www.opengroup.org/pubs/online/7908799/xsh/

=keyword
atoi:$(linkfuncbb)$(linkfuncurl)atoi$(linkfuncba): $(linkfunca)
printf:$(linkfuncbb)$(linkfuncurl)printf$(linkfunc ba):$(linkfunca)

(Highlighted with codeform. ;) ) I still need to find a list of C functions, though. (Feel free to find one. :P) It's used like so:

$ codeform -f rules/c_1_vbb -f rules/clinkfunc


And maybe something for the future: use anchors for user declared functions so you can 'skip' to their declarations, eg

<pre>

void Foo(void);

int main(void)
{
<a href="#foo">Foo</a>();
return 0;
}

<a name="foo"></a>
void Foo(void)
{
return;
}

</pre>
Unfortunately codeform is currently a single-pass parser. Doing something like that would be impossible. I plan to change it however.


Sorry, I don't currently have access to a server , specially not one with, DuN-DUN-DUN PERL!:P
I can find tons of free web hosts that support Perl, but none of them like me executing codeform from within the Perl script . . . not surprisingly.

zacs7
03-31-2007, 06:33 PM
I created a IRC bot a while ago, in the process I made a list of most of the standard C functions....

See the attachment, I know it doesn't contain URLS, but its a start - and it does explain what each does. Perhaps you could add tooltips for the standard funcs instead (so it doesn't rely on the internet)

dwks
03-31-2007, 06:44 PM
Thanks for the list. I'll transform it into some codeform files as soon as I can (probably by tomorrow night). One to turn "printf" into

$(clinkfuncbb)$(clinkfuncurl)printf$(clinkfuncba)p rintf$(clinkfunca)
or

printf
and one for

<a alt="printf(f, ...) is equivalent to fprintf(stdout, f, ...)">printf</a>
perhaps with a link too. I may re-create the alts though. That printf one isn't too helpful. :)

zacs7
04-01-2007, 04:13 AM
yes well :P, I didn't write the descriptions they're from the web... :)

dwks
04-02-2007, 12:01 PM
Sorry, I haven't finished the clinkfunc rules (as they are to be called) yet. My estimates are always off . . . six months before I first released codeform I said "codeform should be finished within two weeks or maybe three."

I have implemented doubling-the-allocated-memory-size. It hasn't been uploaded yet though. (It's a simple two line change to get_string().)

I have attached the latest version of codeform online's Perl script in case anyone's interested. I attached the wrong file. :rolleyes: I'm also attaching the Perl script now.

Perspective
04-02-2007, 12:38 PM
how about linking standard (or common lib) functions to online definitions (like online man pages or something).

kryptkat
04-02-2007, 01:53 PM
note. i did put math.h functions names in winapi list. they are included just the names.

note 2. if anyone wants dx9 list. i may start on it sometime.

dwks
04-02-2007, 02:52 PM
how about linking standard (or common lib) functions to online definitions (like online man pages or something).
That's exactly what this clinkfunc will do. In theory. Here's an example of what it should look like: http://cboard.cprogramming.com/showpost.php?p=629967&postcount=8


note. i did put math.h functions names in winapi list. they are included just the names.
Okay, good to know.

dwks
04-02-2007, 05:02 PM
I've decided to risk it and host codeform version 1.2.0 online. Perhaps it should have a new thread -- but too late now. :)

Codeform version 1.2.0 online: http://dwks.theprogrammingsite.com/myprogs/down/codeform_online/cfonline.htm

Tell me how you like it. If you find any bugs please let me know. Just as before, input files are limited to 32KB in size. The Windows API rules are also supported (only for style 1).

I may not keep codeform online up and running forever, but if I stop hosting it I'll let everyone know here.

The Perl script is attached, with .txt added.

whiteflags
04-02-2007, 05:37 PM
> If you find any bugs please let me know.
The browser will parse #include <header> as #include, and template stuff will also act up,
which is wrong. At least let your users knw this is the case to avoid a surprise.

Other than that, you've done a nifty job so far.

dwks
04-03-2007, 11:46 AM
What do you mean?

#include <stdio.h>

template <typename t>
It works pretty well for me. Post the code you messed it up with. That's how it's supposed to look BTW. If you were thinking it should look like

#include <stdio.h>
well, that wasn't how it was designed. twomers wanted the same thing however. It's relatively easy to implement; just get rid of the "#" comment and add these keywords:

=keyword
#include::
#define:...etc...
I may add an option in codeform online to do that.


Other than that, you've done a nifty job so far.
Thank you. :)

dwks
04-03-2007, 11:57 AM
I've uploaded codeform again. The only changes are:

get_string() doubles the allocated memory when it runs out of space, speeding up codeform for very long source code lines.
clinkfunc, cdescfunc, and cdesclinkfunc rules files have been added. They only work with HTML so far like so:

C:\>codeform -f rules\c_1_css -f rules\cdesclinkfunc codeform.c
But you can easily modify them for use with BBCode. Just change the top ten lines where variables are initialized.


I've also created Perl versions of my clipboard programs. The first package is pclip.zip: http://dwks.theprogrammingsite.com/myprogs/down/pclip.zip

It contains pclipset.pl, pclipget.pl, and pclipclr.pl, which set, get, and clear the clipboard. The whole archive is 510 bytes and this is the most complicated of the three:

use Win32::Clipboard;

my $CLIP = Win32::Clipboard();

my @data = <STDIN>;

$CLIP->Set(join('', @data));

They work better than my C ones; they never add mysterious characters to the end or chop off the last character.

There's also pclipcf2.zip: http://dwks.theprogrammingsite.com/myprogs/down/pclipcf2.zip

It's really neat. You run it in the background from codeform's install directory. Then whenever you put something into the clipboard that starts with "//cf", it runs codeform on the clipboard (after removing the "//cf") and puts the results in the clipboard. So you just write some code like

//cf
#include <stdio.h>

int main(int argc, char *argv[]) {
printf("Hello, World!\n");
return 0;
}
(without code tags), copy it into the clipboard, and viola! your clipboard contains this (with code tags):


#include <stdio.h>

int main(int argc, char *argv[]) {
printf("Hello, World!\n");
return 0;
}
It's amazing. Sort of annoying if you need to copy text starting with "//cf", but that shouldn't happen too often. It takes virtually no CPU time, except when you modify the clipboard. It must be run from codeform's install directory -- i.e. the same directory as codeform.exe. It only works on Windows. And don't worry, if the clipboard doesn't start with //cf, it leaves it alone. :) Let me know how it works. It doesn't seem to work too well with Windows XP . . .

For non-Perl gurus:

C:\whatever\codeform>perl pclipcf.pl


pclipcf update
There's a new version of pclipcf, version 3. It works with Windows XP even. :)
http://dwks.theprogrammingsite.com/myprogs/down/pclipcf3.zip

twomers
04-03-2007, 12:32 PM
That's pretty neat! so \\cf your code, copy it and paste it and it should work without me having to encode it manually? :) You're a king of being nice to lazy people!

dwks
04-03-2007, 12:48 PM
Yeah, well, I fall into that category too you know. :)

The latest version of pclipcf, version 3, works on Windows XP. See my edit.

In case anyone wants to get the Perl interpreter for Windows, here's where: http://www.activestate.com/Products/ActivePerl/

whiteflags
04-03-2007, 01:48 PM
> Post the code you messed it up with.
No.

> It works pretty well for me.
That's because you're still spitting out BB code. HTML treats < and > very differently, and that's what I mean. You see, before posting BB code, the scripts that this board works with will replace all those < > with some &#60 special character jargon. So you don't need to worry about it. But in a web page, #include <header> will be parsed by browsers as #include ; template<class T> will also probably just be template .

Try it yourself and see.

dwks
04-03-2007, 02:12 PM
Right, see, I didn't know what the settings you were using were or anything.

Codeform actually does replace "<" with "&lt;", as you can see from viewing the source of the output:

<font color="green">#include &lt;stdio.h&gt;</font>
But I guess I have to replace "<" with "&amp;lt;" for HTML. Thanks for the bug report. I'll fix it as soon as I can.

I never really thought the online version would be used for anything but BBCode so I guess when I tacked on HTML support I forgot about that. :)

It looks like I haven't uploaded the right pclipcf3.zip . . . rats, it's not on this computer. I'll upload it as soon as I can. Or you can modify pclipcf2.pl; just comment out the line

exec("perl $0");
changing it into

#exec("perl $0");
and create a new script to run the other one in an infinite loop:


for(;;) {
system("perl pclipcf2_modified.pl");
}

twomers
04-03-2007, 02:23 PM
But ... what happens if someone creates a batch file called perl and it does bad things, dwks?

dwks
04-04-2007, 11:29 AM
I would put the full path in, but it's not standard on Windows systems and the Windows version of perl doesn't support shebangs (#!/usr/bin/perl). Perhaps there's a variable that contains the path to the Perl interpreter, like $(MAKE) in Makefiles. I'll look into it. It's $^X (http://perldoc.perl.org/perlvar.html). So you could use

exec("$^X $0");
Looks really cryptic, eh? :) Maybe put a comment in too.

Besides, I just wrote that program in half an hour.

Version 2 (with the exec) works on Windows 98. For Windows XP, just remove the exec line and it should work. I'll try to make a platform-independent version soon.

But just think of the possibilities for that code! If you've ever used KDE, you know the value of kclip, which holds previous clipboard values. You can copy something, copy something else, and then click on kclip and choose some previous content of the clipboard to store in the clipboard. I could write something like that for Windows. Not that it hasn't already been done: http://ditto-cp.sourceforge.net/

Or I could write a version that, whenever a file (image) is put into the clipboard, the image is resized to 800x600 . . . useful for digital cameras that have really bad reading software (like mine). I could even make something like this:

/b: bold
/c: code
/blue: [color=blue]
for use on CBoard, or RTF; I could make a backup program -- copy the files you want backed up, tell this program to do something, and it will copy them . . . the possibilities are endless.

dwks
04-08-2007, 10:00 PM
I have not done much with codeform proper, but I've rewritten pclipcf again; this version is called pclipcf4. I've tested it on Windows 98 and XP and it works on both. Here's the download link (it's 1172 bytes): http://dwks.theprogrammingsite.com/myprogs/down/pclipcf4.zip

See the post 7 posts up for details about pclipcf:

It's really neat. You run it in the background [...] Then whenever you put something into the clipboard that starts with ["[cf]"], it runs codeform on the clipboard (after removing the ["[cf]"]) and puts the results in the clipboard.

pclipcf4 is broken up into functions. It exists in one script, so there's no problems with exec. And it even has a comment at the top explaining what it does. :)

# pclipcf4.pl by DWK
# Perl script to run codeform with the clipboard for input and output
#
# Usage: pclipcf4.pl [-s start-sequence] [path-to-codeform [arguments]]
#
# If the clipboard starts with the start sequence, executes codeform on the
# clipboard's contents and puts the output back into the clipboard.
# Otherwise, does nothing.
#
# The start sequence is by default "[cf]".
#
# This script is Windows-only and not thread-safe!
# In other words, don't run multiple instances of it.
# It takes no CPU except when the clipboard is changed (on the OSes tested).

As you can see, the default starting sequence has been changed to "[cf]", but you can change it with a command-line argument. As well, this script can be run from anywhere -- you just pass it the location of codeform's install directory. Finally, you can tell it what arguments to pass codeform. (The default is "rules/cpp_1_vbb".)

So, here's how you run it from outside the codeform directory:

C:\pclipcf4>perl pclipcf4.pl c:\codeform

Note that you have to quit it with CTRL-C. Under Windows XP this causes an illegal operation. However, this is beyond my control as it happens even with this simple script:

use Win32::Clipboard;

my $clip = Win32::Clipboard();

for(;;) {
$clip->WaitForChange();
}
Just ignore it. It's supposed to be quitting anyway. :)

Also, while codeform is executing, pclipcf4 clears the clipboard. So if you've highlighting a really large chunk of code (like codeform's source, over 2000 lines), and you press CTRL-C-V and nothing happens (that's what happens with an empty clipboard), wait a few milliseconds and press CTRL-V again.

dwks
04-13-2007, 01:00 PM
I've uploaded codeform again. There are no changes to the actual program, but I added a subdirectory called exts/ which contains 50KB (>25 files) of extensions to codeform. (The .ZIP is now 227208 bytes, 222KB.)

I'll copy exts/README.txt here:

This directory, exts/, contains several extensions to codeform. Perl is
required to run many of them. You can get Perl for Windows at:
http://activestate.com/

codeform.pl is used to host codeform online at
http://dwks.theprogrammingsite.com/myprogs/down/codeform_online/cfonline.htm

The rest of the files are related to clipboard extensions. You can run them by
executing the batch files or directly from the command line, with your own
options.

Here are the files in exts/ and a brief description:
asyncdown.c C source code for asyncdown
asyncdown.exe Returns true if a given key is held down
clip_inout_any.bat Codeforms the data from and to the clipboard
clip_inout_vbb.bat The same, but rules\cpp_1_vbb is added to the args
clip_shift.bat* Codeforms the clipboard when CTRL-SHIFT-C is pressed
clip_start.bat* Codeforms the clipboard when it starts with "[cf]"
clip_start_any.bat* The same, but when it starts with the first argument
clipclr.c C source code for clipclr
clipclr.exe Clears the clipboard
clipget.c C source code for clipget
clipget.exe Prints the contents of the clipboard (like pclipget.pl)
clipset.c C source code for clipset
clipset.exe Stores its input into the clipboard (like pclipset.pl)
cliptee.c C source code for cliptee
cliptee.exe Stores input into the clipboard, but also prints the data
codeform.pl* Perl script for codeform online
pclipcf.pl* Codeforms the clipboard for multiple conditions
pclipcf4.pl* (old) Codeforms the clipboard when it starts with a string
pclipclr.pl* Clears the clipboard
pclipget.pl* Prints the contents of the clipboard to the screen
pclipset.pl* Sets the clipboard to the data it is passed (from stdin)
pcliptee.pl* Stores input into the clipboard, but also prints the data
presdir.exe CDs somewhere, executes a command, then CDs back
presdir.c C source code for presdir
README.txt This readme file
shiftdown.c C source code for shiftdown
shiftdown.exe Returns true if either shift key is currently held down

Programs marked with an asterisk (*) require Perl to run.


Usually, unless you want something unusual, you execute one of the batch files.

clip_inout_any.bat: This uses the clipboard for input and output to codeform, but you need to pass it some arguments because it doesn't add any. Execute this from the command line only.
clip_inout_vbb.bat: The same, but rules\cpp_1_vbb is added to the arguments. You can run this from Windows. I have a shortcut to it. :)
clip_shift.bat: [perl] *NEW* When you press CTRL-SHIFT-C, the clipboard is codeformed; CTRL-C, it isn't. See below.
clip_start.bat: Codeforms the clipboard when it starts with "[cf]". Identical to pclipcf4.pl (which is still included if you want to use it). Run this from Windows.
clip_start_any.bat: The same, but you specify what to use instead of "[cf]" for the first argument. Run this from a command line.

What's really neat is clip_shift.bat. When it is running, if you press CTRL-SHIFT-C instead of CTRL-C, the clipboard is codeformed. No more typing an extra four characters for "[cf]"! We're lazier than that! Just press shift! :D Unfortunately it doesn't work on Windows 98, though it does work on Windows XP. I can't imagine why; here's its code:

#include <windows.h>

int main(void) {
return GetAsyncKeyState(VK_SHIFT) & 0x8000 ? 1 : 0;
}

Also, some programs trap CTRL-SHIFT-C. If that is the case, you could modify that code, or use asyncshift to trap both shifts, or use notepad. :)

I also fixed a bug in the C clipboard programs. They now work just as well as the Perl ones.

I also added cliptee, which is like clipset but it also prints the clipboard contents to the screen.

I posted that exactly on the hour! That doesn't happen too often . . . 1/60 of the time in fact. :)

Vote for code colouring! :D http://cboard.cprogramming.com/showthread.php?p=633909

dwks
04-18-2007, 10:45 AM
Codeform online has moved. The new location of cfonline.htm is at this shorter address: http://dwks.theprogrammingsite.com/myprogs/cfonline.htm

The same file at the old location (http://dwks.theprogrammingsite.com/myprogs/down/codeform_online/cfonline.htm) can still be accessed, but I may get rid of it soon.

I don't suppose anyone really cares, but I've fixed citizen's bug. I've also created C versions of all of the Perl scripts included in codeform.zip (except for the codeform online script, codeform.pl), but I haven't added them to the .ZIP because right now you have to do something like this:

whenkey -p "clipget | startseq -p "clipget | stripchars 4 |\
presdir .. codeform rules\cpp_1_vbb exts\clipset" [cf]" ctrl c
Except you can't have double quotes inside double quotes on Windows, of course; you need multiple batch files. But if someone wants them, let me know. I'll add them to the .ZIP once they work properly (well, almost anyway).

I've also enabled cdescfunc, clinkfunc, and cdesclinkfunc, though since you can't specify codeform's arguments, they only work with HTML and CSS. Also, don't try checking more than one of those cfunc checkboxes. :)

KONI
04-18-2007, 12:14 PM
Could I ask where you got your grammars from? I intend to write a real-time code colorer and syntax analyzer with JavaScript but I had problems finding the lexical and syntactical grammar.

So I was wondering if there was an official grammar somewhere ?

brewbuck
04-18-2007, 12:26 PM
Could I ask where you got your grammars from? I intend to write a real-time code colorer and syntax analyzer with JavaScript but I had problems finding the lexical and syntactical grammar.

There are grammars out there, but they're old. Obviously, there is an official grammar of the C language, but it varies between language levels (pre-C90, C90, C99, etc.)

I think implementing a complete grammar for the C language would be overkill. It might even work against you, since people edit code in real-time and the code goes through intermediate states where it is not even valid C. You wouldn't want your code colorer to oscillate wildly as people type stuff.

Most code coloring software is based on heuristics. There is some basic grammar processing going on, but it's mostly at a lexical level. So really, all you need is a lexer, not a full blown parser. You wouldn't have to code it to standard -- just code it so that it highlights things how you want them highlighted.

dwks
04-18-2007, 01:10 PM
I didn't use any grammer at all, believe it or not. Codeform just looks for keywords based on whether the characters before and after the keyword are non-alphanumeric or not. :) It works reasonably well, but of course I can't easily tell the difference between parentheses for function calls and parentheses for expressions.