~ubuntu-branches/ubuntu/jaunty/alex/jaunty

« back to all changes in this revision

Viewing changes to glafp-utils/genargs/genargs.pl

  • Committer: Bazaar Package Importer
  • Author(s): Ian Lynagh (wibble)
  • Date: 2003-10-01 12:31:01 UTC
  • Revision ID: james.westby@ubuntu.com-20031001123101-yquo14mvjqh3e0sk
Tags: upstream-2.0
ImportĀ upstreamĀ versionĀ 2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
my $quote_open = 0;
 
3
my $quote_char = '';
 
4
my $accum = "";
 
5
my $once = 1;
 
6
my $c;
 
7
 
 
8
# This program generates a partial Haskell list of Strings from
 
9
# words passed via stdin suitable for use in package.conf, e.g.:
 
10
#
 
11
#   foo bar   --> "foo", "bar"
 
12
#   "foo bar" --> "foo bar"
 
13
#   foo\"bar  --> "foo\"bar"
 
14
#
 
15
# Invoking genargs.pl with -comma will print an initial comma if
 
16
# there's anything to print at all.
 
17
#
 
18
# Sample application in a Makefile:
 
19
#  HSIFIED_EXTRA_LD_OPTS= `echo "$(EXTRA_LD_OPTS)" | $(PERL) genargs.pl`
 
20
#  PACKAGE_CPP_OPTS += -DHSIFIED_EXTRA_LD_OPTS="$(HSIFIED_EXTRA_LD_OPTS)"
 
21
 
 
22
sub printaccum {
 
23
  if ($once) {
 
24
    if ($ARGV[0] eq "-comma") {
 
25
      print ", ";
 
26
    }
 
27
  } else {
 
28
    print ", ";
 
29
  }
 
30
  $once=0;
 
31
  print '"';
 
32
  print $accum;
 
33
  print '"';
 
34
}
 
35
 
 
36
while ($c = getc) {
 
37
  if ($quote_open) {
 
38
    if ($c eq $quote_char) {
 
39
      $quote_open = 0;
 
40
    } elsif ($c eq '"') {
 
41
      $accum .= '\"';
 
42
    } else {
 
43
      $accum .= $c;
 
44
    }
 
45
  } else {
 
46
    if (($c eq ' ') || ($c eq "\n")) {
 
47
      if (!($accum eq "")) {
 
48
        printaccum;
 
49
        $accum = "";
 
50
      }
 
51
    } elsif ($c eq "\\") {
 
52
      $accum .= $c;
 
53
      $c = getc;
 
54
      $accum .= $c;
 
55
    } elsif (($c eq '"') || ($c eq "\'")) {
 
56
      $quote_open = 1;
 
57
      $quote_char = $c;
 
58
    } else {
 
59
      $accum .= $c
 
60
    }
 
61
  }
 
62
}