]> Pileus Git - ~andy/gtk/blob - examples/extract.awk
New files to automagically extract code examples from the tutorial.
[~andy/gtk] / examples / extract.awk
1 # extract - extract C source files from GTK Tutorial
2 # Copyright (C) Tony Gale 1998
3 # Contact: gale@gtk.org
4 #
5 # Command Switches:
6 # -c : Just do checking rather than output files
7 # -f <filename> : Extract a specific file
8 # -d : Extract files to current directory
9  
10 BEGIN {in_example=0; check=0; spec_example=""; do_output=0; flatten=0
11        for (i=0 ; i < ARGC ; i++) {
12         if ( ARGV[i] == "-c" ) {
13           check = 1;
14           ARGV[i]="";
15         } else if ( ARGV[i] == "-f" ) {
16           spec_example=ARGV[i+1];
17           ARGV[i]="";
18           ARGV[i+1]="";
19           if ( length(spec_example) == 0 ) {
20             print "usage: -f <filename>";
21             exit;
22           }
23         } else if ( ARGV[i] == "-d" ) {
24           flatten = 1;
25           ARGV[i]="";
26         }
27        }
28       }
29
30 $2 == "example-start" && in_example == 1 { printf("\nERROR: nested example at line %d\n", NR) > "/dev/stderr";
31                                            exit}
32
33 $2 == "example-start"                    { in_example=1 }
34
35 $2 == "example-start" && check == 0 \
36   { if ( (spec_example == "") || (spec_example == $4) ) {
37     if ( flatten == 0 ) {
38       file_name = sprintf("%s/%s",$3, $4);
39       command = sprintf("mkdir -p %s", $3);
40       system(command);
41     } else {
42       file_name = $4;
43     }
44     do_output=1;
45   }
46   }
47
48 in_example==1 && check==0 && do_output==1 { gsub(/&amp;/, "\\&", $0);
49                                             print $0 >file_name } 
50
51 $2 == "example-end" && in_example == 0    { printf("\nERROR: multiple ends at line %d\n", NR) > "/dev/stderr";
52                                             exit}
53 $2 == "example-end"                       { in_example=0; do_output=0 }
54
55
56 END {}
57