summaryrefslogtreecommitdiff
path: root/support/bibtex-gen/bibtex.pl
blob: d14043fe22cc5d9eda4f3d44cf42c6b4343a7ffa (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
#!/usr/bin/perl -w
# /*=========================================================================*
#  *      Title   : BibTeX Database Generator (Interactive script)
#  *      File    : bibtex.pl
#  *
#  *      Author  : Pallav Gupta
#  *      Created : Sat Aug 23 20:36:59 2003
#  *
#  * This source code can be redistributed under the GNU Free Public Licence.
#  *=========================================================================*/

# /* $Id: bibtex.pl,v 1.4 2003/10/09 04:04:27 pgupta Exp $ */

# Usage: bibtex.pl

require Term::ReadKey;
require Term::ANSIColor;
require File::Basename;

use File::Basename;
use Term::ReadKey;
use Term::ANSIColor qw(:constants);
use strict;

# Contains template for the bibtex entries
my $program_dir = dirname($0);
my $template = "$program_dir/bibtex.template";

# Global variables
# Interactive prompt
my $prompt = "bibtex>";
# Map each of the entries to its shortcut command
my %types;
# Hash table to store all the entries
my %entries;

# Main program
&bibtex_header(*STDOUT);
&bibtex_initialize_structures();
&exec_bibtex_script(*STDIN);
exit 0;

# Options interpreter
sub exec_bibtex_script {
  my $fp = shift @_;

  print "\nType 'h(elp)' for help message.\n";
  print "\n$prompt";
  while(<$fp>) {
    chomp($_);

    if (/^$/ || /^\#/) {
      next;
    }
    if (/^q(uit)?/) {
      last;
    }
    elsif (/^c(reate)?\s+(\w+)/) {
      &bibtex_new_entry($2);
    }
    elsif (/^h(elp)?/) {
      &bibtex_help();
    }
    elsif (/^s(how)?\s*(.*)?/) {
      &bibtex_show_entry(*STDOUT, $2);
    }
    elsif (/^d(del)?\s*(.*)?/) {
      &bibtex_del_entry($2);
    }
    elsif (/^m(odify)?\s+(.*)/) {
      &bibtex_modify_entry($2);
    }
    elsif (/^k(eys)?/) {
      &bibtex_show_keys();
    }
    elsif (/^w(rite)?\s+(.*)/) {
      &bibtex_write_to_file(0,$2); # 0 is for no appending
    }
    elsif (/^a(append)?\s+(.*)/) {
      &bibtex_write_to_file(1,$2); # 1 is for appending
    }
    else {
      &bibtex_error("Invalid option '$_'. Type 'h(elp)' for help message.");
    }

    print "\n$prompt";
  }
}

# Here we initialize the fields each type of bibtex entry must have
sub bibtex_initialize_structures {

  die "File $template does not exist. Aborting $0." if (!-e "$template");
  open(IN, "$template") || die "Unable to open file $template";
  print "Reading $template and initializing ...\n";

  while (<IN>) {
    chomp $_;

    next if ($_ eq '');

    # ignore comments that begin with #
    m/^\#|^\s/ and do {
      next;
    };

    m/(.*);\s*(.*);\s*(.*)/ and do {
      my $type = $1;
      my $symb = $3;
      $type =~ s/\s//g;
      $symb =~ s/\s//g;
      my @fields = split /,\s+/,$2;
      unshift @fields, $type;
      print "Read $symb : @fields\n";
      $types{$symb} = \@fields;
      next;
    };

    die "Parse error: unable to parse '$_'\n";
  }

  close(IN);
}

# Create a new entry of type 'type'
sub bibtex_new_entry {
  my $type = shift @_;

  my @entry;
  my %data;

  if (!exists $types{$type}) {
    bibtex_error("I don't know how to process entry of type '$type'.");
    return;
  }

  @entry = @{$types{$type}};
  my $char = &bibtex_readchar("Create entry type '$entry[0]'");
  return if ($char eq 'n');
  $data{"type"} = $type;

  for (my $i = 1; $i <= $#entry; $i++) {
    print "$entry[$i]>";
    my $line = <STDIN>;
    chomp($line);
    $data{$entry[$i]} = $line;
  }

  print "key>";
  my $key = <STDIN>;
  chomp($key);
  $key =~ s/\s//g;
  while (1) {
    if (exists $entries{$key}) { print "An entry with key '$key' already exists. Enter new key>"; }
    elsif ($key eq "") { print "Invalid key '$key'. Enter new key>"; }
    else { last; }
    $key = <STDIN>;
    chomp($key);
    $key =~ s/\s//g;
  }
  $entries{$key} = \%data;
}

# Displays the key of each entry in database
sub bibtex_show_keys {

  if (scalar keys %entries == 0) {
    &bibtex_error("There are no entries.");
    return;
  }

  my $i = 0;
  foreach my $key (keys %entries) {
    print "$key";
    if (($i++)%5 == 0) { print "\n"; }
    else { print ", "; }
  }
}

# Modify an entry with key 'key'
sub bibtex_modify_entry {
  my $key = shift @_;

  if (scalar keys %entries == 0) {
    &bibtex_error("There are no entries.");
    return;
  }

  if (defined $key && $key ne "") {
    chomp ($key);

    if (!exists $entries{$key}) {
      &bibtex_error("No entry found with key $key");
      return;
    }
    else {
      my %data = %{$entries{$key}};
      my @fields = @{$types{$data{"type"}}};

      print "\nModifying entry '$fields[0]'. To keep old value of a field, press <ENTER>\n";

      for (my $i = 1; $i <= $#fields; $i++) {
	print "$fields[$i]: ", RED, "$data{$fields[$i]}\n", RESET;
	print "Enter new value>";
	my $str = <STDIN>;
	chomp($str);
	if ($str ne "") { $data{$fields[$i]} = $str; }
      }
      $entries{$key} = \%data;
    }
    print "\n";
  }
  else {
    &bibtex_error("No key supplied. Aborted!");
    return;
  }
}


# Display all or a single entry
sub bibtex_show_entry {
  my ($fh, $key) = (@_);

  if (scalar keys %entries == 0) {
    &bibtex_error("There are no entries.");
    return;
  }

  if (defined $key && $key ne "") {
    chomp($key);

    if (exists $entries{$key}) {
      &bibtex_print_entry($fh, $key);
    }
    else {
      &bibtex_error("No entry found with key $key");
      return;
    }
  }
  else {
    foreach $key (sort keys %entries) {
      &bibtex_print_entry($fh, $key);
    }
  }
}

# Delete an entry with key 'key'
sub bibtex_del_entry {
  my $key = shift @_;

  if (scalar keys %entries == 0) {
    &bibtex_error("There are no entries.");
    return;
  }

  if (defined $key && $key ne "") {
    chomp ($key);

    if (exists $entries{$key}) { delete $entries{$key}; }
    else {
      &bibtex_error("No entry found with key $key");
      return;
    }
  }
  else {
    my $char = &bibtex_readchar("Delete all entries");
    return if ($char eq 'n');
    %entries = ();
  }
}

# Print an entry with key 'key'
sub bibtex_print_entry {
  my ($fh, $key) = (@_);

  my %entry_data = %{$entries{$key}};
  my @entry_type = @{$types{$entry_data{"type"}}};

  print $fh "\n\@$entry_type[0]\{$key,\n";

  for (my $i = 1; $i <= $#entry_type; $i++) {
    print $fh "$entry_type[$i] = \{$entry_data{$entry_type[$i]}\}";
    print $fh ",\n" if ($i != $#entry_type);
  }
  print $fh "\n};\n";
}

# Write bibtex to file <file>
sub bibtex_write_to_file {
  my ($append, $file) = (@_);

  if (not defined $file || $file eq "") {
    &bibtex_error("Invalid name '$file' for file specified");
    return;
  }

  if (scalar keys %entries == 0) {
    &bibtex_error("There are no entries.");
    return;
  }

  my $t;
  if ($append) { $t = ">>$file"; }
  else { $t = ">$file"; }

  if (!open(OUT, $t)) {
    &bibtex_error("Unable to open $file for writing. Aborted!");
    return;
  }

  &bibtex_show_entry(*OUT, "");
  close(OUT);
}

# Help msg
sub bibtex_help {
  print "\nCommands that are currently supported:\n";
  print CYAN, "h(elp)", RESET, "\t\t\t  - this help\n";
  print CYAN, "c(reate) ";

  my @keys = sort keys %types;
  print "[";
  for (my $i = 0; $i < $#keys; $i++) {
    print "$keys[$i]|";
  }
  print "$keys[$#keys]]", RESET, " - create an entry type\n";

  foreach my $i (@keys) {
    print RED "\t $i", RESET, "\t: @{$types{$i}}[0]\n";
  }

  print CYAN, "d(el) [<key>]", RESET, "\t\t - delete bibtex entry with key <key>\n";
  print "\t\t\t NOTE: not supplying a key will delete everything!\n";
  print CYAN, "s(how) [<key>]", RESET, "\t\t - show bibtex entry with key <key>\n";
  print "\t\t\t NOTE: not supplying a key will show everything!\n";
  print CYAN, "m(odify) <key>", RESET, "\t\t - modify values of entry with key <key>\n";
  print CYAN, "w(rite) <file>", RESET, "\t\t - write bibtex database to file <file> by sorting keys\n";
  print CYAN, "a(ppend) <file>", RESET, "\t\t - append bibtex database to file <file> by sorting keys\n";
  print CYAN, "k(eys)", RESET, "\t\t\t - show all keys in current database\n";
  print CYAN, "q(uit)", RESET, "\t\t\t - quit\n";
}

# Print error messages
sub bibtex_error {
  my @msgs = (@_);

  foreach my $msg (@msgs) {
    print "\nERROR: $msg\n";
  }
}

# Read a char from stdin
sub bibtex_readchar {
  my $msg = shift @_;

  my $char = 'g';
  ReadMode('cbreak');
  while (!($char eq 'y' || $char eq 'n')) {
    print "\n$msg [y|n]?";
    $char = ReadKey(0);
  }
  ReadMode('normal');
  print "\n";
  return $char;
}

# Script header info
sub bibtex_header {
  my $fh = shift @_;

  my $prog_name = "BibTeX Database Generator";
  my $email = "pallav\@ieee.org";
  my $author = "Pallav Gupta";
  my $version = "1.0";
  my $date = `date`;

  print $fh "Starting $prog_name \t\t $date\n";
  print $fh "Author: $author\n";
  print $fh "Version: $version\n";
  print $fh "Report bugs: $email\n";
}