1#! @PERLPATH@
2#
3# Copyright (c) 2010, 2016 Tama Communications Corporation
4#
5# This file is free software; as a special exception the author gives
6# unlimited permission to copy and/or distribute it, with or without
7# modifications, as long as this notice is preserved.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
11# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12#
13use strict;
14use warnings;
15#
16# Set PATH and remove some environment variables to satisfy taint mode.
17# If you installed idutils outside of the PATH, you should add the place
18# to the PATH.
19#
20$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
21delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
22my $script_name = 'completion.cgi';
23my $global_command = '@GLOBALPATH@';
24sub error_and_exit {
25	print STDERR "$script_name: " . $_[0] . "\n";
26	exit 1;
27}
28my $query_string = defined($ENV{'QUERY_STRING'}) ? $ENV{'QUERY_STRING'} : '';
29my @pairs = split (/&/, $query_string);
30my %form;
31foreach (@pairs) {
32	my($name, $value) = split(/=/, $_);
33	$value =~ tr/+/ /;
34	$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C", hex($1))/eg;
35	$form{$name} = $value;
36}
37if (!defined($form{'q'})) {
38	error_and_exit("request value is null.");
39}
40my $q = $form{'q'};
41#
42# Execute a ceremony to satisfy taint mode.
43#
44if ($q =~ /\A(.*)\z/) {
45        $q = $1;
46} else {
47        $q = '';
48}
49my $type = $form{'type'};
50my $flags = '';
51my $limit = 0;
52if ($type eq 'definition' || $type eq 'reference') {
53	$flags = 'c';
54} elsif ($type eq 'symbol') {
55	$flags = 'cs';
56} elsif ($type eq 'path') {
57	$flags = 'cP';
58} elsif ($type eq 'idutils') {
59	$flags = 'cI';
60} elsif ($type eq 'grep') {
61	exit(0);	# Ignored because completion for grep is groundless.
62} else {
63	error_and_exit("invalid type name.");
64}
65if ($form{'icase'}) {
66	$flags .= 'i';
67}
68if ($form{'other'}) {
69	$flags .= 'o';
70}
71if ($form{'limit'}) {
72	$limit = $form{'limit'};
73}
74if (-d "cgi-bin") {
75	# This code avoids the bug of the python built-in web server.
76	chdir("cgi-bin");
77	if ($?) {
78		error_and_exit("Couldn't change 'cgi-bin' directory.");
79	}
80}
81my $gtagsroot = '';
82if (-f "../GTAGSROOT" && open(GTAGSROOT, "../GTAGSROOT")) {
83	$gtagsroot = <GTAGSROOT>;
84	chop($gtagsroot);
85	close(GTAGSROOT);
86} else {
87	$gtagsroot = "../..";
88}
89chdir($gtagsroot);
90print "Content-Type: text/html\n\n";
91if ($^O eq 'MSWin32') {
92	#
93	# This code was commented out, because it may have a security hole in the
94	# future.  To use this code, please uncomment in your own responsibility.
95	#
96	#open(PIPE, $global_command . " -${flags}e \"$q\" |");
97	error_and_exit("Feature not implemented.");
98} else {
99	open(PIPE, "-|") || exec $global_command, '-'.$flags.'e', $q;
100}
101if ($limit > 0) {
102	while (<PIPE>) {
103		print if ($limit-- > 0);
104	}
105} else {
106	print <PIPE>;
107}
108close(PIPE);
109exit(0);
110