summaryrefslogtreecommitdiff
path: root/Master/bin/i386-openbsd/vpe
blob: 9eb52b69e3cf7902cda9f8c9d92d09dd5e82924d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $argv:q'
  if 0;
use strict;
#
# vpe.pl
#
# Copyright (C) 2000 Heiko Oberdiek.
#
# This program may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either version 1.2
# of this license or (at your option) any later version.
# The latest version of this license is in
#   http://www.latex-project.org/lppl.txt
# and version 1.2 or later is part of all distributions of LaTeX
# version 1999/12/01 or later.
#
# See file "vpe.txt" for a list of files that belong to this project.
#
# This file "vpe.pl" may be renamed to "vpe"
# for installation purposes.
#
my $file        = "vpe.pl";
my $program     = uc($&) if $file =~ /^\w+/;
my $version     = "0.1";
my $date        = "2000/09/15";
my $author      = "Heiko Oberdiek";
my $copyright   = "Copyright (c) 2000 by $author.";
#
# History:
#   2000/09/15 v0.1: First release.
#

### program identification
my $title = "$program $version, $date - $copyright\n";

### editor call
# %F: file name
# %L: line number
my $EditorCall = "xterm -e joe +%d %s";
$EditorCall = $ENV{TEXEDIT} if $ENV{TEXEDIT};
$EditorCall = $ENV{VPE} if $ENV{VPE};

### error strings
my $Error = "!!! Error:"; # error prefix

my $usage = <<"END_OF_USAGE";
${title}
Depending on the name of the script `vpe' works in four modes:

* [vpe] Syntax: vpe <pdf file>[.pdf]
  The pdf file is scanned for actions created by VTeX,
  that start an editor with the source file at the
  specified line under Windows.
  A symbol link is made from the source file name
  extended by the extension `.vpe' to this script.
  The line number is encoded in the path and the
  action is changed to start this script.

* [sty] Internal for vpe.sty:
  Syntax: vpe --sty [--progname=...] <latex file> <vpe file>

* [system] Internal for vpe.sty:
  Syntax: vpe --system <vpe file>

* [launch] Syntax: <source file name>.vpe
  The script decodes the line number in the path of
  the source file name and starts an editor with
  this file at that line number.

Options:
  --help:     print usage.
  --verbose:  print additional informations during running.
  --force:    force symbol links
  --delete:   delete symbol links
  --sty:      internal for `vpe.sty' (get absolute file name and lines)
  --system:   internal for `vpe.sty' (get system info linux or win)
  --progname: latex, pdflatex, elatex, pdfelatex
END_OF_USAGE

### options
$::opt_verbose = 0;
$::opt_help = 0;
$::opt_force = 0;
$::opt_delete = 0;
$::opt_sty = 0;
$::opt_system = 0;
$::opt_progname = "latex";
use Getopt::Long;
GetOptions(
  "help!",
  "verbose!",
  "force!",
  "delete!",
  "sty!",
  "system!",
  "progname=s",
) or die $usage;
if ($::opt_help) {
  die $usage;
}

if ($::opt_sty and $::opt_system) {
  die "$usage" .
      "$Error Options --sty and --system cannot used together!\n";
}

###################
### launch mode ###
###################
if ($0 =~ /\.vpe/) {

  @ARGV == 0 or
    die "$usage$Error Too many arguments [launch mode]!\n";

  my $file = "";
  $0 =~ m|(^[\./]+\./)(.+)\.vpe$| or
    die "$Error Cannot extract line number ($0)!\n";
  my $str = $1;
  $file = "/$2";

  my $line = "";
  while ($str ne "") {
    $str =~ m|^(/*)(\.?)\./(.*)$| or
      die "$Error Parse error!\n";
    $line .= length($1) + (($2 eq ".") ? 5 : 0);
    $str = $3;
  }

  my $callstr = $EditorCall;
  $EditorCall =~ s/%s/$file/;
  $EditorCall =~ s/%d/$line/;
  print "File: $file, line: $line\n" if $::opt_verbose;
  exec($EditorCall);
  exit 1;
}

###
### used by both sty and system mode:
###
my $system = "linux";
$system = "" if $^O =~ /os2/i;
$system = "" if $^O =~ /mac/i;
$system = "win" if $^O =~ /dos/i;
$system = "win" if $^O =~ /win/i;

################
### sty mode ###
################
if ($::opt_sty) {

  @ARGV == 2 or
    die "$usage$Error Wrong arguments [sty mode]!\n";

  my $vpefile = $ARGV[1];
  print "VPE file: $vpefile\n" if $::opt_verbose;

  my $file = `kpsewhich -progname=$::opt_progname $ARGV[0]`;
  chomp $file;
  if (!($file =~ m|^/| or $file =~ m|^\w:|)) {
    use Cwd;
    $file = cwd() . "/" . $file;
    $file =~ s|/[^/]+/\.\./|/|g;
    $file =~ s|/\./|/|g;
  }
  -f $file or
    die "$Error Cannot find file `$file' [sty mode]!\n";
  print "File: $file\n" if $::opt_verbose;

  my $last = 0;
  if (open(IN, $file)) {
    while (<IN>) {
      $last++;
    }
  }
  if ($last == 0) {
    $last = 10000;
  }
  print "Last line: $last\n" if $::opt_verbose;

  open(OUT, ">>$vpefile") or
    die "$Error Cannot open file `$vpefile`!\n";
  print OUT "\\vpeentry{$file}{$last}\n";

  exit 1;
}

###################
### system mode ###
###################
if ($::opt_system) {

  @ARGV == 1 or
    die "$usage$Error Wrong arguments [system mode]!\n";

  my $vpefile = $ARGV[0];
  print "VPE file: $vpefile\n" if $::opt_verbose;

  open(OUT, ">>$vpefile") or
    die "$Error Cannot open file `$vpefile`!\n";
  print OUT "\\vpesystem{$system}\n";

  exit 1;
}

################
### vpe mode ###
################
if (@ARGV < 1) {
  die "$usage$Error Missing pdf file [vpe mode]!\n";
}
if (@ARGV > 2) {
  die "$usage$Error Too many arguments [vpe mode]!\n";
}

my $pdffile = $ARGV[0];
if (!-f $pdffile) {
  my $name = $pdffile;
  $pdffile .= ".pdf";
  -f $pdffile or
    die "$Error File `$name' not found [vpe mode]!\n";
}

open(IN, "+<$pdffile") or
  die "$Error Cannot open `$pdffile' [vpe mode]!\n";
binmode(IN);
my %symlinks = ();
while (<IN>) {

  if (m|/F\([\./]*(/.*)\.vpe\)|) {
    $symlinks{$1} = 1;
    next;
  }

  my $do = 0;
  my ($action, $file, $line, $type);
  # my $color; # only for debugging

  if (m|^
        /A \s* << \s*
          /Type \s* /Action \s*
          /S \s* /Launch \s*
          /Win \s* << \s*
            /F \s* \(aftcomp.exe\) \s*
            /P \s* \("(.*)\" \s+ \d+\-(\d+)\) \s*
          >> \s*
        >> \s*
        $
       |x
  ) {
    $action = $_;
    $file = $1;
    $line = $2;
    # $color = "/C[0 0 1]"; # only for debugging
    $type = "aftcomp";
    $do = 1;
  }

  if (m|^
        /A \s* << \s*
          /Type \s* /Action \s*
          /S \s* /Launch \s*
          /Win \s* << \s*
            /F \s* \(repos.exe\) \s*
            /P \s* \(
              "(.*)" \s*
              "(\d+)" \s*
              "(.*)" \s*
              "(\d+)"
            \) \s*
          >> \s*
        >> \s*
        $
       |x
  ) {
    $action = $_;
    if ($3 eq "") {
      $file = $1;
      $line = $2;
    }
    else {
      # ???
      $file = $1;
      $line = $2;
    }
    # $color = "/C[1 0 0]"; # only for debugging
    $type = "repos";
    $do = 1;
  }

  if ($do) {
    my $length = length($action);
    print "* File: $file, line: $line, type: $type\n" if $::opt_verbose;

    if (!($file =~ m|^/|)) {
      print STDERR "$Error File `$file' lacks of absolute path!\n";
      next;
    }

    if (!$::opt_delete) {
      if ($line <= 0) {
        $line = 1;
      }

      my $newaction = "";
      {
        my $digit = substr($line, 0, 1);
        if ($digit <= 5) {
          $newaction .= "/" x $digit;
        }
        else {
          $newaction .= "/" x ($digit - 5) . ".";
        }
        $newaction .= "./";
        my $rest = $line;
        while (($rest = substr($rest, 1)) ne "") {
          $digit = substr($rest, 0, 1);
          if ($digit < 5) {
            $newaction .= "/" x $digit;
          }
          else {
            $newaction .= "/" x ($digit - 5) . ".";
          }
          $newaction .= "./";
        }
      }
      $newaction .= substr($file, 1) . ".vpe";
      $newaction = # $color . # only for debugging
                   "/A<</Type/Action/S/Launch/F($newaction)>>";
      $newaction .= " " x ($length - length($newaction) - 1);
      if (length($newaction) > $length) {
         print STDERR "$Error Action too long!\n";
         next;
      }

      seek(IN, -length($action), 1);
      print IN $newaction;
    }

    $symlinks{$file} = 1;
  }
}

if (keys(%symlinks)) {

  my $this = $0;
  if (!$::opt_delete) {
    if (!-f $0 or !-x $0) {
      $this = `which $0`;
      ($this ne "") or die "$Error Cannot find this script!\n";
    }
    if (!($this =~ m|^/|)) {
      use Cwd;
      $this = cwd() . "/" . $this;
    }
    $this =~ s|/[^/]+/\.\./|/|g;
    $this =~ s|/\./|/|g;
  }

  if ($::opt_delete) {
    print "Delete symlinks:\n";
  }
  else {
    if ($::opt_force) {
      print "Forced symlinks to $this:\n";
    }
    else {
      print "Symlinks to $this:\n";
    }
  }

  foreach (keys(%symlinks)) {
    my $sym = $_ . ".vpe";
    print "  $sym [";

    if ($::opt_delete) {
      if (!-l $sym) {
        print "ok, not existing]\n";
        next;
      }
      unlink($sym);
      if (!-l $sym) {
        print "ok, deleted]\n";
        next;
      }
      print "failed]\n";
      next;
    }
    if ($::opt_force) {
      if (-l $sym) {
        unlink($sym);
        if (-l $sym) {
          print "deletion failed]\n";
          next;
        }
        if (symlink($this, $sym)) {
          print "ok, deleted and created]\n";
          next;
        }
        print "deleted, creation failed]\n";
        next;
      }
      if (symlink($this, $sym)) {
        print "ok, created]\n";
        next;
      }
      print "creation failed]\n";
      next;
    }
    if (-f $sym) {
      print "exists]\n";
      next;
    }
    if (symlink($this, $sym)) {
      print "ok, created]\n";
      next;
    }
    print "failed]\n";
    next;
  }
}
__END__