Skip to contentSkip to conversation

tcc->tc comments

LARTC raptor 2 participants 2 messages Sep 26, 2002

lore
Share & tools
lore

raptor

author
#0 lore raw

hi,

is there a way so that I can write something like that :

htb {
class ( rate 10kbps, ceil 20kbps, comment "this is a comment") { }
}

and later in the tcc-output I get something like this :

#this is a comment
tc ...blah.. classid X:Y ....

And similar thing for filters/classifiers ?!
The idea is to be able read easly if I have alot of classes and classifiers.

thanx alot in advance
_______________________________________________
LARTC mailing list / [email protected]
http://mailman.ds9a.nl/mailman/listinfo/lartc HOWTO: http://lartc.org/

#1 lore raw
Quoted 1 line · raptor

is there a way so that I can write something like that :

Hmm, looks like a good use for pragmas with tc output ;-) After applying the attached quick and dirty patch, this

htb (pragma "#the_outer_qdisc") {
    class (rate 10kbps,ceil 20kbps,pragma "#the_first_class") {
        fifo(pragma "#the_inner_qdisc");
    }
} 

yields this:

# the_outer_qdisc
tc qdisc add dev eth0 handle 1:0 root htb
# the_first_class
tc class add dev eth0 parent 1:0 classid 1:1 htb rate 1250bps ceil 2500bps
# the_inner_qdisc
tc qdisc add dev eth0 handle 2:0 parent 1:1 pfifo

Note that you can't include spaces in pragams, but all other non-blank characters are fine.

Quoted 1 line

And similar thing for filters/classifiers ?!

Works there too, but only if using "on". When using "if", tcc may transform the expression quite heavily, and there's no way for associating output items with input items (in the general case).

But a better way to do all this is to use tcc's location map to extract the comments directly from the source, as demonstrated by the Perl script in the second attachment.

Input example:

/* this is the comment
for HTB */

htb {
    // another comment, this
    // time for a class
    class (rate 10kbps,ceil 20kbps) {
        // last but not least, a comment for the FIFO
        fifo;
    }
}

Not that, when invoking com.pl, the name of the input file must be the last command-line argument.

- Werner

Signature · 3 lines
  _________________________________________________________________________
 / Werner Almesberger, Buenos Aires, Argentina         [email protected] /
/_http://www.almesberger.net/____________________________________________/
tc.c renamed from tc.c.orig +14 -2
@@ -237,6 +237,18 @@
 
 void tc_pragma(const PARAM *params)
 {
-    if (prm_present(params,&prm_pragma))
-	yywarn("\"pragma\" parameter ignored by \"tc\" target");
+    const DATA_LIST *pragma;
+
+    if (!prm_present(params,&prm_pragma)) return;
+    for (pragma = prm_data(params,&prm_pragma).u.list; pragma;
+      pragma = pragma->next) {
+	const char *s;
+
+	s = pragma->ref->u.string;
+	if (*s != '#') {
+	    yywarn("\"pragma\" parameter ignored by \"tc\" target");
+	    return;
+	}
+	printf("# %s\n",s+1);
+    }
 }

#!/usr/bin/perl

sub emit

{
    return unless $in =~ /\n$_[0] /s;
    ($s = "\n$`") =~ s/\n\d+ /\n/gs;
    return unless
      $s =~ m#/\*\s*(([^*]|\*[^/])*)\*/[^\n]*$|//\s*(([^\n]*\n\s*//)*[^\n]*)$#s;
    ($s = defined $1 ? $1 : $3) =~ s/\n\s*/\n# /sg;
    $s =~ s#\s*//\s*# #;
    print "# $s\n" || die "stdout: $!";
}

open(FILE,$ARGV[$#ARGV]) || die "$ARGV[$#ARGV]: $!";

while (<FILE>) {
    $in .= "$. $_";
}

close FILE;
print "out: $in\n";
open(PIPE,"tcc -l $$.out ".join(" ",@ARGV)."|") || die "tcc: $!"; @out = <PIPE>;
close PIPE;
exit $? if $?;
open(FILE,"$$.out") || die "$$.out: $!";

for (<FILE>) {
    $q{$1} = $2 if /^qdisc [^:]+:(\S+) .* (\d+)$/;
    $c{$1} = $2 if /^class [^:]+:(\S+) .* (\d+)$/;
}
close FILE;
unlink "$$.out" || warn "unlink $$.out: $!";
for (@out) {
    &emit($q{$1}) if /^tc qdisc .* handle (\d+):0/;
    &emit($c{$1}) if /^tc class .* classid (\d+:\d+)/;
    print $_ || die "stdout: $!";
}