/usr/bin/chem is in groff 1.21-7.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 | #! /usr/bin/env perl
# chem - a groff preprocessor for producing chemical structure diagrams
# Source file position: <groff-source>/contrib/chem/chem.pl
# Installed position: <prefix>/bin/chem
# Copyright (C) 2006, 2009 Free Software Foundation, Inc.
# Written by Bernd Warken.
# This file is part of `chem', which is part of `groff'.
# `groff' is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# `groff' is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
########################################################################
# settings
########################################################################
my $Program_Version = '0.3.1';
my $Last_Update = '03 Jan 2009';
# this setting of the groff version is only used before make is run,
# otherwise 1.21 will set it.
my $Groff_Version_Preset='1.20preset';
# test on Perl version
require v5.6;
########################################################################
# begin
########################################################################
use warnings;
use strict;
use Math::Trig;
# for catfile()
use File::Spec;
# $Bin is the directory where this script is located
use FindBin;
my $Chem_Name;
my $Groff_Version;
my $File_chem_pic;
my $File_pic_tmac;
BEGIN {
{
my $before_make; # script before run of `make'
{
my $at = '@';
$before_make = 1 if '1.21' eq "${at}VERSION${at}";
}
my %at_at;
if ($before_make) {
my $chem_dir = $FindBin::Bin;
$at_at{'BINDIR'} = $chem_dir;
$at_at{'G'} = '';
$File_chem_pic = File::Spec->catfile($chem_dir, 'chem.pic');
$File_pic_tmac = File::Spec->catfile($chem_dir, '..', 'pic.tmac');
$Groff_Version = '';
$Chem_Name = 'chem';
} else {
$Groff_Version = '1.21';
$at_at{'BINDIR'} = '/usr/bin';
$at_at{'G'} = '';
$at_at{'PICDIR'} = '/usr/share/groff/1.21/pic';
$at_at{'TMACDIR'} = '/usr/share/groff/1.21/tmac';
$File_chem_pic =
File::Spec->catfile($at_at{'PICDIR'}, 'chem.pic');
$File_pic_tmac = File::Spec->catfile($at_at{'TMACDIR'}, 'pic.tmac');
$Chem_Name = $at_at{'G'} . 'chem';
}
}
}
########################################################################
# check the parameters
########################################################################
if (@ARGV) {
# process any FOO=bar switches
# eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
my @filespec = ();
my $dbl_minus;
my $wrong;
foreach (@ARGV) {
next unless $_;
if (/=/) {
# ignore FOO=bar switches
push @filespec, $_ if -f;
next;
}
if ($dbl_minus) {
if (-f $_) {
push @filespec, $_ if -s $_;
} else {
warn "chem: argument $_ is not an existing file.\n";
$wrong = 1;
}
next;
}
if (/^--$/) {
$dbl_minus = 1;
next;
}
if (/^-$/) {
push @filespec, $_;
next;
}
if (/^-h$/ or '--help' =~ /^$_/) {
&usage();
exit 0;
}
if (/^-v$/ or '--version' =~ /^$_/) {
&version();
exit 0;
}
if (-f $_) {
push @filespec, $_ if -s $_;
} else {
$wrong = 1;
if (/^-/) {
warn "chem: wrong option ${_}.\n";
} else {
warn "chem: argument $_ is not an existing file.\n";
}
}
}
if (@filespec) {
@ARGV = @filespec;
} else {
exit 0 if $wrong;
@ARGV = ('-');
}
} else { # @ARGV is empty
@ARGV = ('-') unless @ARGV;
}
########################################################################
# main process
########################################################################
my %Dc = ( 'up' => 0, 'right' => 90, 'down' => 180, 'left' => 270,
'ne' => 45, 'se' => 135, 'sw' => 225, 'nw' => 315,
0 => 'n', 90 => 'e', 180 => 's', 270 => 'w',
30 => 'ne', 45 => 'ne', 60 => 'ne',
120 => 'se', 135 => 'se', 150 => 'se',
210 => 'sw', 225 => 'sw', 240 => 'sw',
300 => 'nw', 315 => 'nw', 330 => 'nw',
);
my $Word_Count;
my @Words;
my $Line_No;
my $Last_Name = '';
# from init()
my $First_Time = 1;
my $Last_Type;
my $Dir; # direction
my %Types = (
'RING' => 'R',
'MOL' => 'M',
'BOND' => 'B',
'OTHER' => 'O' # manifests
);
# from setparams()
my %Params;
# from ring()
my $Nput;
my $Aromatic;
my %Put;
my %Dbl;
my %Labtype;
my %Define = ();
my $File_Name = '';
my $Line = '';
&main();
{
my $is_pic = '';
my $is_chem = '';
my $former_line = '';
##########
# main()
#
sub main {
my $count_minus = 0;
my @stdin = ();
my $stdin = 0;
# for centralizing the pic code
open TMAC, "<$File_pic_tmac" and print <TMAC>;
close TMAC;
foreach (@ARGV) {
$count_minus++ if /^-$/;
}
foreach my $arg (@ARGV) {
&setparams(1.0);
next unless $arg;
$Line_No = 0;
$is_pic = '';
$is_chem = '';
if ($arg eq '-') {
$File_Name = 'standard input';
if ($stdin) {
&main_line($_) foreach @stdin;
} else {
$stdin = 1;
if ($count_minus <= 1) {
while (<STDIN>) {
&main_line($_);
}
} else {
@stdin = ();
while (<STDIN>) {
push @stdin, $_;
&main_line($_);
}
}
}
### main()
} else { # $arg is not -
$File_Name = $arg;
open FILE, "<$arg";
&main_line($_) while <FILE>;
close FILE;
} # if $arg
if ($is_pic) {
printf ".PE\n";
}
}
} # main()
##########
# main_line()
#
sub main_line {
my $line = $_[0];
# $Last_Type = $Types{'OTHER'};
# $Last_Type = '';
my $stack;
$Line_No++;
chomp $line;
$line = $former_line . $line if $former_line;
if ($line =~ /^(.*)\\$/) {
$former_line = $1;
return 1;
} else {
$former_line = '';
}
$Line = $line;
{
@Words = ();
my $s = $line;
$s =~ s/^\s*//;
$s =~ s/\s+$//;
return 1 unless $s;
$s = " $s";
$s =~ s/\s+#.*$// if $is_pic;
return 1 unless $s;
$line = $s;
$line =~ s/^\s*|\s*$//g;
my $bool = 1;
while ($bool) {
$s =~ /^([^"]*)\s("[^"]*"?\S*)(.*)$/;
if (defined $1) {
my $s1 = $1;
my $s2 = $2;
$s = $3;
$s1 =~ s/^\s*|\s*$//g;
push @Words, split(/\s+/, $s1) if $s1;
push @Words, $s2;
}
if ($s !~ /\s"/) {
$s =~ s/^\s*|\s*$//g;
push @Words, split(/\s+/, $s) if $s;
$bool = 0;
}
}
# @Words = split(/\s+/, $s);
return 1 unless @Words;
# foreach my $i (0..$#Words) {
# if ($Words[$i] =~ /^\s*#/) {
# $#Words = $i - 1;
# last;
# }
# }
# return 1 unless @Words;
}
if ($line =~ /^([\.']\s*PS\s*)|([\.']\s*PS\s.+)$/) {
# .PS
unless ($is_pic) {
$is_pic = 'running';
print "$line\n";
}
return 1;
}
### main_line()
if ( $line =~ /^([\.']\s*PE\s*)|([\.']\s*PE\s.+)$/ ) {
# .PE
$is_chem = '';
if ($is_pic) {
$is_pic = '';
print "$line\n";
}
return 1;
}
if ($line =~ /^[\.']\s*cstart\s*$/) {
# line: `.cstart'
if ($is_chem) {
&error("additional `.cstart'; chem is already active.");
return 1;
}
unless ($is_pic) {
&print_ps();
$is_pic = 'by chem';
}
$is_chem = '.cstart';
&init();
return 1;
}
### main_line()
if ($line =~ /^\s*begin\s+chem\s*$/) {
# line: `begin chem'
if ($is_pic) {
if ($is_chem) {
&error("additional `begin chem'; chem is already active.");
return 1;
}
$is_chem = 'begin chem';
&init();
} else {
print "$line\n";
}
return 1;
}
if ($line =~ /^[\.']\s*cend\s*/) {
# line `.cend'
if ($is_chem) {
&error("you end chem with `.cend', but started it with `begin chem'.")
if $is_chem eq 'begin chem';
if ($is_pic eq 'by chem') {
&print_pe();
$is_pic = '';
}
$is_chem = '';
} else {
print "$line\n";
}
return 1;
}
if ($line =~ /^\s*end\s*$/) {
# line: `end'
if ($is_chem) {
&error("you end chem with `end', but started it with `.cstart'.")
if $is_chem eq '.cstart';
if ($is_pic eq 'by chem') {
&print_pe();
$is_pic = '';
}
$is_chem = '';
} else {
print "$line\n";
}
return 1;
}
### main_line()
if (! $is_chem) {
print "$line\n";
return 1;
}
if ($line =~ /^[.']/) {
# groff request line
print "$line\n";
return 1;
}
if ($Words[0] eq 'pic') {
# pic pass-thru
return 1 if $#Words == 0;
my $s = $line;
$s =~ /^\s*pic\s*(.*)$/;
$s = $1;
print "$s\n" if $s;
$Last_Type = $Types{'OTHER'};
$Define{ $Words[2] } = 1 if $#Words >= 2 && $Words[1] eq 'define';
return 1;
}
if ($Words[0] eq 'textht') {
if ($#Words == 0) {
&error("`textht' needs a single argument.");
return 0;
}
&error("only the last argument is taken for `textht', " .
"all others are ignored.")
unless $#Words <= 1 or ($#Words == 2 && $Words[1] =~ /^=/);
$Params{'textht'} = $Words[$#Words];
return 1;
}
### main_line()
if ($Words[0] eq 'cwid') { # character width
if ($#Words == 0) {
&error("`cwid' needs a single argument.");
return 0;
}
&error("only the last argument is taken for `cwid', " .
"all others are ignored.")
unless $#Words <= 1 or ($#Words == 2 && $Words[1] =~ /^=/);
$Params{'cwid'} = $Words[$#Words];
return 1;
}
if ($Words[0] eq 'db') { # bond length
if ($#Words == 0) {
&error("`db' needs a single argument.");
return 0;
}
&error("only the last argument is taken for `db', " .
"all others are ignored.")
unless $#Words <= 1 or ($#Words == 2 && $Words[1] =~ /^=/);
$Params{'db'} = $Words[$#Words];
return 1;
}
if ($Words[0] eq 'size') { # size for all parts of the whole diagram
my $size;
if ($#Words == 0) {
&error("`size' needs a single argument.");
return 0;
}
&error("only the last argument is taken for `size', " .
"all others are ignored.")
unless $#Words <= 1 or ($#Words == 2 && $Words[1] =~ /^=/);
if ($Words[$#Words] <= 4) {
$size = $Words[$#Words];
} else {
$size = $Words[$#Words] / 10;
}
&setparams($size);
return 1;
}
### main_line()
print "\n#", $Line, "\n"; # debugging, etc.
$Last_Name = '';
# $Last_Type = $Types{'OTHER'};
# $Last_Type = '';
if ($Words[0] =~ /^[A-Z].*:$/) {
# label; falls thru after shifting left
my $w = $Words[0];
$Last_Name = $w;
$Last_Name =~ s/:$//;
print "$w";
shift @Words;
if (@Words) {
print " ";
$line =~ s/^\s*$w\s*//;
} else {
print "\n";
return 1;
}
}
if ($Words[0] eq 'define') {
print "$line\n";
$Define{ $Words[1] } = 1 if $#Words >= 1;
$Last_Type = $Types{'OTHER'};
return 1;
}
if ($Words[0] =~ /^[\[\]{}]/) {
print "$line\n";
$Last_Type = $Types{'OTHER'};
return 1;
}
if ($Words[0] =~ /^"/) {
print 'Last: ', $line, "\n";
$Last_Type = $Types{'OTHER'};
return 1;
}
if ($Words[0] =~ /bond/) {
&bond($Words[0]);
return 1;
}
if ($#Words >= 1) {
if ($Words[0] =~ /^(double|triple|front|back)$/ &&
$Words[1] eq 'bond') {
my $w = shift @Words;
$Words[0] = $w . $Words[0];
&bond($Words[0]);
return 1;
}
if ($Words[0] eq 'aromatic') {
my $temp = $Words[0];
$Words[0] = $Words[1] ? $Words[1] : '';
$Words[1] = $temp;
}
}
if ($Words[0] =~ /ring|benz/) {
&ring($Words[0]);
return 1;
}
if ($Words[0] eq 'methyl') {
# left here as an example
$Words[0] = 'CH3';
}
### main_line()
if ($Words[0] =~ /^[A-Z]/) {
&molecule();
return 1;
}
if ($Words[0] eq 'left') {
my %left; # not used
$left{++$stack} = &fields(1, $#Words);
printf (("Last: [\n"));
return 1;
}
if ($Words[0] eq 'right') {
&bracket();
$stack--;
return 1;
}
if ($Words[0] eq 'label') { # prints the vertex numbers in a ring
if ( exists $Labtype{$Words[1]} and
$Labtype{$Words[1]} =~ /^$Types{'RING'}/ ) {
my $v = substr($Labtype{$Words[1]}, 1, 1);
$Words[1] = '' unless $Words[1];
foreach my $i ( 1..$v ) {
printf "\"\\s-3%d\\s0\" at 0.%d<%s.C,%s.V%d>\n", $i, $v + 2,
$Words[1], $Words[1], $i;
}
} else {
&error("$Words[1] is not a ring.");
}
return 1;
}
if ( exists $Define{ $Words[0] } ) {
print $line, "\n";
$Last_Type = $Types{'OTHER'};
return 1;
}
return 1 unless $line;
# print STDERR "# $Line\n";
# &error('This is not a chem command. To include a command for pic, ' .
# "add `pic' as the first word to the command.");
print $line, "\n";
$Last_Type = $Types{'OTHER'};
1;
} # main_line()
}
########################################################################
# functions
########################################################################
##########
# atom(<string>)
#
sub atom {
# convert CH3 to atom(...)
my ($s) = @_;
my ($i, $n, $nsub, $cloc, $nsubc, @s);
if ($s eq "\"\"") {
return $s;
}
$n = length($s);
$nsub = $nsubc = 0;
$cloc = index($s, 'C');
if (! defined($cloc) || $cloc < 0) {
$cloc = 0;
}
@s = split('', $s);
$i = 0;
foreach (@s) {
unless (/[A-Z]/) {
$nsub++;
$nsubc++ if $i < $cloc;
$i++;
}
}
$s =~ s/([0-9]+\.[0-9]+)|([0-9]+)/\\s-3\\d$&\\u\\s+3/g;
if ($s =~ /([^0-9]\.)|(\.[^0-9])/) { # centered dot
$s =~ s/\./\\v#-.3m#.\\v#.3m#/g;
}
sprintf( "atom(\"%s\", %g, %g, %g, %g, %g, %g)",
$s, ($n - $nsub / 2) * $Params{'cwid'}, $Params{'textht'},
($cloc - $nsubc / 2 + 0.5) * $Params{'cwid'}, $Params{'crh'},
$Params{'crw'}, $Params{'dav'}
);
} # atom()
##########
# bond(<type>)
#
sub bond {
my ($type) = @_;
my ($i, $moiety, $from, $leng);
$moiety = '';
for ($i = 1; $i <= $#Words; $i++) {
if ($Words[$i] eq ';') {
&error("a colon `;' must be followed by a space and a single word.")
if $i != $#Words - 1;
$moiety = $Words[$i + 1] if $#Words > $i;
$#Words = $i - 1;
last;
}
}
$leng = $Params{'db'}; # bond length
$from = '';
for ($Word_Count = 1; $Word_Count <= $#Words; ) {
if ($Words[$Word_Count] =~
/(\+|-)?\d+|up|down|right|left|ne|se|nw|sw/) {
$Dir = &cvtdir($Dir);
} elsif ($Words[$Word_Count] =~ /^leng/) {
$leng = $Words[$Word_Count + 1] if $#Words > $Word_Count;
$Word_Count += 2;
} elsif ($Words[$Word_Count] eq 'to') {
$leng = 0;
$from = &fields($Word_Count, $#Words);
last;
} elsif ($Words[$Word_Count] eq 'from') {
$from = &dofrom();
last;
} elsif ($Words[$Word_Count] =~ /^#/) {
$Word_Count = $#Words + 1;
last;
} else {
$from = &fields($Word_Count, $#Words);
last;
}
}
### bond()
if ($from =~ /( to )|^to/) { # said "from ... to ...", so zap length
$leng = 0;
} elsif (! $from) { # no from given at all
$from = 'from Last.' . &leave($Last_Type, $Dir) . ' ' .
&fields($Word_Count, $#Words);
}
printf "Last: %s(%g, %g, %s)\n", $type, $leng, $Dir, $from;
$Last_Type = $Types{'BOND'};
$Labtype{$Last_Name} = $Last_Type if $Last_Name;
if ($moiety) {
@Words = ($moiety);
&molecule();
}
} # bond()
##########
# bracket()
#
sub bracket {
my $t;
printf (("]\n"));
if ($Words[1] && $Words[1] eq ')') {
$t = 'spline';
} else {
$t = 'line';
}
printf "%s from last [].sw+(%g,0) to last [].sw to last [].nw to last " .
"[].nw+(%g,0)\n", $t, $Params{'dbrack'}, $Params{'dbrack'};
printf "%s from last [].se-(%g,0) to last [].se to last [].ne to last " .
"[].ne-(%g,0)\n", $t, $Params{'dbrack'}, $Params{'dbrack'};
if ($Words[2] && $Words[2] eq 'sub') {
printf "\" %s\" ljust at last [].se\n", &fields(3, $#Words);
}
} # bracket()
##########
# corner(<dir>)
#
# Return the corner name next to the given angle.
#
sub corner {
my ($d) = @_;
$Dc{ (45 * int(($d + 22.5) / 45)) % 360 };
} # corner()
##########
# cvtdir(<dir>)
#
# Maps "[pointing] somewhere" to degrees.
#
sub cvtdir {
my ($d) = @_;
if ($Words[$Word_Count] eq 'pointing') {
$Word_Count++;
}
if ($Words[$Word_Count] =~ /^[+\\-]?\d+/) {
return ( $Words[$Word_Count++] % 360 );
} elsif ($Words[$Word_Count] =~ /left|right|up|down|ne|nw|se|sw/) {
return ( $Dc{$Words[$Word_Count++]} % 360 );
} else {
$Word_Count++;
return $d;
}
} # cvtdir()
##########
# dblring(<v>)
#
sub dblring {
my ($v) = @_;
my ($d, $v1, $v2);
# should canonicalize to i,i+1 mod v
$d = $Words[$Word_Count];
for ($Word_Count++; $Word_Count <= $#Words &&
$Words[$Word_Count] =~ /^[1-9]/; $Word_Count++) {
$v1 = substr($Words[$Word_Count], 0, 1);
$v2 = substr($Words[$Word_Count], 2, 1);
if ($v2 == $v1 + 1 || $v1 == $v && $v2 == 1) { # e.g., 2,3 or 5,1
$Dbl{$v1} = $d;
} elsif ($v1 == $v2 + 1 || $v2 == $v && $v1 == 1) { # e.g., 3,2 or 1,5
$Dbl{$v2} = $d;
} else {
&error(sprintf("weird %s bond in\n\t%s", $d, $_));
}
}
} # dblring()
##########
# dofrom()
#
sub dofrom {
my $n;
$Word_Count++; # skip "from"
$n = $Words[$Word_Count];
if (defined $Labtype{$n}) { # "from Thing" => "from Thing.V.s"
return 'from ' . $n . '.' . &leave($Labtype{$n}, $Dir);
}
if ($n =~ /^\.[A-Z]/) { # "from .V" => "from Last.V.s"
return 'from Last' . $n . '.' . &corner($Dir);
}
if ($n =~ /^[A-Z][^.]*\.[A-Z][^.]*$/) { # "from X.V" => "from X.V.s"
return 'from ' . $n . '.' . &corner($Dir);
}
&fields($Word_Count - 1, $#Words);
} # dofrom()
##########
# error(<string>)
#
sub error {
my ($s) = @_;
printf STDERR "chem: error in %s on line %d: %s\n",
$File_Name, $Line_No, $s;
} # error()
##########
# fields(<n1>, <n2>)
#
sub fields {
my ($n1, $n2) = @_;
if ($n1 > $n2) {
return '';
}
my $s = '';
foreach my $i ($n1..$n2) {
if ($Words[$i] =~ /^#/) {
last;
}
$s = $s . $Words[$i] . ' ';
}
$s;
} # fields()
##########
# init()
#
sub init {
if ($First_Time) {
printf "copy \"%s\"\n", $File_chem_pic;
printf "\ttextht = %g; textwid = .1; cwid = %g\n",
$Params{'textht'}, $Params{'cwid'};
printf "\tlineht = %g; linewid = %g\n",
$Params{'lineht'}, $Params{'linewid'};
$First_Time = 0;
}
printf "Last: 0,0\n";
$Last_Type = $Types{'OTHER'};
$Dir = 90;
} # init()
##########
# leave(<last>, <d>)
#
sub leave {
my ($last, $d) = @_;
my ($c, $c1);
# return vertex of $last in direction $d
if ( $last eq $Types{'BOND'} ) {
return 'end';
}
$d %= 360;
if ( $last =~ /^$Types{'RING'}/ ) {
return &ringleave($last, $d);
}
if ( $last eq $Types{'MOL'} ) {
if ($d == 0 || $d == 180) {
$c = 'C';
} elsif ($d > 0 && $d < 180) {
$c = 'R';
} else {
$c = 'L';
}
if (defined $Dc{$d}) {
$c1 = $Dc{$d};
} else {
$c1 = &corner($d);
}
return sprintf('%s.%s', $c, $c1);
}
if ( $last eq $Types{'OTHER'} ) {
return &corner($d);
}
'c';
} # leave()
##########
# makering(<type>, <pt>, <v>)
#
sub makering {
my ($type, $pt, $v) = @_;
my ($i, $j, $a, $r, $rat, $fix, $c1, $c2);
if ($type =~ /flat/) {
$v = 6;
# vertices
;
}
$r = $Params{'ringside'} / (2 * sin(pi / $v));
printf "\tC: 0,0\n";
for ($i = 0; $i <= $v + 1; $i++) {
$a = (($i - 1) / $v * 360 + $pt) / 57.29578; # 57. is $deg
printf "\tV%d: (%g,%g)\n", $i, $r * sin($a), $r * cos($a);
}
if ($type =~ /flat/) {
printf "\tV4: V5; V5: V6\n";
$v = 5;
}
# sides
if ($Nput > 0) {
# hetero ...
for ($i = 1; $i <= $v; $i++) {
$c1 = $c2 = 0;
if ($Put{$i} ne '') {
printf "\tV%d: ellipse invis ht %g wid %g at V%d\n",
$i, $Params{'crh'}, $Params{'crw'}, $i;
printf "\t%s at V%d\n", $Put{$i}, $i;
$c1 = $Params{'cr'};
}
$j = $i + 1;
if ($j > $v) {
$j = 1;
}
### makering()
if ($Put{$j} ne '') {
$c2 = $Params{'cr'};
}
printf "\tline from V%d to V%d chop %g chop %g\n", $i, $j, $c1, $c2;
if ($Dbl{$i} ne '') {
# should check i<j
if ($type =~ /flat/ && $i == 3) {
$rat = 0.75;
$fix = 5;
} else {
$rat = 0.85;
$fix = 1.5;
}
if ($Put{$i} eq '') {
$c1 = 0;
} else {
$c1 = $Params{'cr'} / $fix;
}
if ($Put{$j} eq '') {
$c2 = 0;
} else {
$c2 = $Params{'cr'} / $fix;
}
printf "\tline from %g<C,V%d> to %g<C,V%d> chop %g chop %g\n",
$rat, $i, $rat, $j, $c1, $c2;
if ($Dbl{$i} eq 'triple') {
printf "\tline from %g<C,V%d> to %g<C,V%d> chop %g chop %g\n",
2 - $rat, $i, 2 - $rat, $j, $c1, $c2;
}
}
}
### makering()
} else {
# regular
for ($i = 1; $i <= $v; $i++) {
$j = $i + 1;
if ($j > $v) {
$j = 1;
}
printf "\tline from V%d to V%d\n", $i, $j;
if ($Dbl{$i} ne '') {
# should check i<j
if ($type =~ /flat/ && $i == 3) {
$rat = 0.75;
} else {
$rat = 0.85;
}
printf "\tline from %g<C,V%d> to %g<C,V%d>\n",
$rat, $i, $rat, $j;
if ($Dbl{$i} eq 'triple') {
printf "\tline from %g<C,V%d> to %g<C,V%d>\n",
2 - $rat, $i, 2 - $rat, $j;
}
}
}
}
### makering()
# punt on triple temporarily
# circle
if ($type =~ /benz/ || $Aromatic > 0) {
if ($type =~ /flat/) {
$r *= .4;
} else {
$r *= .5;
}
printf "\tcircle rad %g at 0,0\n", $r;
}
} # makering()
##########
# molecule()
#
sub molecule {
my ($n, $type);
if ($#Words >= 0) {
$n = $Words[0];
if ($n eq 'BP') {
$Words[0] = "\"\" ht 0 wid 0";
$type = $Types{'OTHER'};
} else {
$Words[0] = &atom($n);
$type = $Types{'MOL'};
}
}
$n =~ s/[^A-Za-z0-9]//g; # for stuff like C(OH3): zap non-alnum
if ($#Words < 1) {
printf "Last: %s: %s with .%s at Last.%s\n",
$n, join(' ', @Words), &leave($type, $Dir + 180),
&leave($Last_Type, $Dir);
### molecule()
} else {
if (! $Words[1]) {
printf "Last: %s: %s with .%s at Last.%s\n",
$n, join(' ', @Words), &leave($type, $Dir + 180),
&leave($Last_Type, $Dir);
} elsif ($#Words >= 1 and $Words[1] eq 'below') {
$Words[2] = '' if ! $Words[2];
printf "Last: %s: %s with .n at %s.s\n", $n, $Words[0], $Words[2];
} elsif ($#Words >= 1 and $Words[1] eq 'above') {
$Words[2] = '' if ! $Words[2];
printf "Last: %s: %s with .s at %s.n\n", $n, $Words[0], $Words[2];
} elsif ($#Words >= 2 and $Words[1] eq 'left' && $Words[2] eq 'of') {
$Words[3] = '' if ! $Words[3];
printf "Last: %s: %s with .e at %s.w+(%g,0)\n",
$n, $Words[0], $Words[3], $Params{'dew'};
} elsif ($#Words >= 2 and $Words[1] eq 'right' && $Words[2] eq 'of') {
$Words[3] = '' if ! $Words[3];
printf "Last: %s: %s with .w at %s.e-(%g,0)\n",
$n, $Words[0], $Words[3], $Params{'dew'};
} else {
printf "Last: %s: %s\n", $n, join(' ', @Words);
}
}
$Last_Type = $type;
if ($Last_Name) {
# $Last_Type = '';
$Labtype{$Last_Name} = $Last_Type;
}
$Labtype{$n} = $Last_Type;
} # molecule()
##########
# print_hash(<hash_or_ref>)
#
# print the elements of a hash or hash reference
#
sub print_hash {
my $hr;
my $n = scalar @_;
if ($n == 0) {
print STDERR "empty hash\n;";
return 1;
} elsif ($n == 1) {
if (ref($_[0]) eq 'HASH') {
$hr = $_[0];
} else {
warn 'print_hash(): the argument is not a hash or hash reference;';
return 0;
}
} else {
if ($n % 2) {
warn 'print_hash(): the arguments are not a hash;';
return 0;
} else {
my %h = @_;
$hr = \%h;
}
}
### print_hash()
unless (%$hr) {
print STDERR "empty hash\n";
return 1;
}
print STDERR "hash (ignore the ^ characters):\n";
for my $k (sort keys %$hr) {
my $hk = $hr->{$k};
print STDERR " $k => ";
if (defined $hk) {
print STDERR "^$hk^";
} else {
print STDERR "undef";
}
print STDERR "\n";
}
1;
} # print_hash()
##########
# print_pe()
#
sub print_pe {
print ".PE\n";
} # print_pe()
##########
# print_ps()
#
sub print_ps {
print ".PS\n";
} # print_ps()
##########
# putring(<v>)
#
sub putring {
# collect "put Mol at n"
my ($v) = @_;
my ($m, $mol, $n);
$Word_Count++;
$mol = $Words[$Word_Count++];
if ($Words[$Word_Count] eq 'at') {
$Word_Count++;
}
$n = $Words[$Word_Count];
if ($n !~ /^\d+$/) {
$n =~ s/(\d)+$/$1/;
$n = 0 if $n !~ /^\d+$/;
error('use single digit as argument for "put at"');
}
if ($n >= 1 && $n <= $v) {
$m = $mol;
$m =~ s/[^A-Za-z0-9]//g;
$Put{$n} = $m . ':' . &atom($mol);
} elsif ($n == 0) {
error('argument of "put at" must be a single digit');
} else {
error('argument of "put at" is too large');
}
$Word_Count++;
} # putring()
##########
# ring(<type>)
#
sub ring {
my ($type) = @_;
my ($typeint, $pt, $verts, $i, $other, $fused, $withat);
$pt = 0; # points up by default
if ($type =~ /([1-8])$/) {
$verts = $1;
} elsif ($type =~ /flat/) {
$verts = 5;
} else {
$verts = 6;
}
$fused = $other = '';
for ($i = 1; $i <= $verts; $i++) {
$Put{$i} = $Dbl{$i} = '';
}
$Nput = $Aromatic = $withat = 0;
for ($Word_Count = 1; $Word_Count <= $#Words; ) {
if ($Words[$Word_Count] eq 'pointing') {
$pt = &cvtdir(0);
} elsif ($Words[$Word_Count] eq 'double' ||
$Words[$Word_Count] eq 'triple') {
&dblring($verts);
} elsif ($Words[$Word_Count] =~ /arom/) {
$Aromatic++;
$Word_Count++; # handled later
### ring()
} elsif ($Words[$Word_Count] eq 'put') {
&putring($verts);
$Nput++;
} elsif ($Words[$Word_Count] =~ /^#/) {
$Word_Count = $#Words + 1;
last;
} else {
if ($Words[$Word_Count] eq 'with' || $Words[$Word_Count] eq 'at') {
$withat = 1;
}
$other = $other . ' ' . $Words[$Word_Count];
$Word_Count++;
}
}
$typeint = $Types{'RING'} . $verts . $pt; # RING | verts | dir
if ($withat == 0) {
# join a ring to something
if ( $Last_Type =~ /^$Types{'RING'}/ ) {
# ring to ring
if (substr($typeint, 2) eq substr($Last_Type, 2)) {
# fails if not 6-sided
$fused = 'with .V6 at Last.V2';
}
}
# if all else fails
$fused = sprintf('with .%s at Last.%s',
&leave($typeint, $Dir + 180), &leave($Last_Type, $Dir));
}
printf "Last: [\n";
&makering($type, $pt, $verts);
printf "] %s %s\n", $fused, $other;
$Last_Type = $typeint;
$Labtype{$Last_Name} = $Last_Type if $Last_Name;
} # ring()
##########
# ringleave(<last>, <d>)
#
sub ringleave {
my ($last, $d) = @_;
my ($rd, $verts);
# return vertex of ring in direction d
$verts = substr($last, 1, 1);
$rd = substr($last, 2);
sprintf('V%d.%s', int( (($d - $rd) % 360) / (360 / $verts)) + 1,
&corner($d));
} # ringleave()
##########
# setparams(<scale>)
#
sub setparams {
my ($scale) = @_;
$Params{'lineht'} = $scale * 0.2;
$Params{'linewid'} = $scale * 0.2;
$Params{'textht'} = $scale * 0.16;
$Params{'db'} = $scale * 0.2; # bond length
$Params{'cwid'} = $scale * 0.12; # character width
$Params{'cr'} = $scale * 0.08; # rad of invis circles at ring vertices
$Params{'crh'} = $scale * 0.16; # ht of invis ellipse at ring vertices
$Params{'crw'} = $scale * 0.12; # wid
$Params{'dav'} = $scale * 0.015; # vertical shift up for atoms in atom macro
$Params{'dew'} = $scale * 0.02; # east-west shift for left of/right of
$Params{'ringside'} = $scale * 0.3; # side of all rings
$Params{'dbrack'} = $scale * 0.1; # length of bottom of bracket
} # setparams()
##########
# usage()
#
# Print usage information for --help.
#
sub usage {
print "\n";
&version();
print <<EOF;
Usage: $Chem_Name [option]... [filespec]...
$Chem_Name is a groff preprocessor for producing chemical structure
diagrams. The output suits to the pic preprocessor.
"filespec" is one of
"filename" name of a readable file
"-" for standard input
All available options are
-h --help print this usage message.
-v --version print version information.
EOF
} # usage()
##########
# version()
#
# Get version information from version.sh and print a text with this.
#
sub version {
$Groff_Version = $Groff_Version_Preset unless $Groff_Version;
my $year = $Last_Update;
$year =~ s/^.* //;
print <<EOF;
$Chem_Name $Program_Version of $Last_Update (Perl version)
is part of groff version $Groff_Version.
Copyright (C) $year Free Software Foundation, Inc.
GNU groff and chem come with ABSOLUTELY NO WARRANTY.
You may redistribute copies of groff and its subprograms
under the terms of the GNU General Public License.
EOF
} # version()
1;
### Emacs settings
# Local Variables:
# mode: CPerl
# End:
|