[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gobolinux-commits
Subject:    [gobolinux-commits] tools/Scripts src/Makefile
From:       Michael Homer <michael () e ! geek ! nz>
Date:       2008-02-26 6:34:36
Message-ID: E1JTtOe-0000vD-Fi () cvs ! savannah ! gnu ! org
[Download RAW message or body]

CVSROOT:	/sources/goboscripts
Module name:	tools
Changes by:	Michael Homer <mwh>	08/02/26 06:34:36

Modified files:
	Scripts/src    : Makefile 
Added files:
	Scripts/src    : CommandNotFound.c 
Removed files:
	Scripts/bin    : CommandNotFound 

Log message:
	C replacement implementation of CNF, about 100x as fast.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/tools/Scripts/bin/CommandNotFound?cvsroot=goboscripts&r1=1.4&r2=0
 http://cvs.savannah.gnu.org/viewcvs/tools/Scripts/src/Makefile?cvsroot=goboscripts&r1=1.17&r2=1.18
 http://cvs.savannah.gnu.org/viewcvs/tools/Scripts/src/CommandNotFound.c?cvsroot=goboscripts&rev=1.1


Patches:
Index: src/Makefile
===================================================================
RCS file: /sources/goboscripts/tools/Scripts/src/Makefile,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -b -r1.17 -r1.18
--- src/Makefile	20 Feb 2008 20:40:50 -0000	1.17
+++ src/Makefile	26 Feb 2008 06:34:36 -0000	1.18
@@ -12,7 +12,7 @@
    RM_EXE=-rm -f *.exe
 endif
 
-dynamic_exec = SuperUserName IsExecutable usleep LinkOrExpandAll List
+dynamic_exec = SuperUserName IsExecutable usleep LinkOrExpandAll List \
CommandNotFound  static_exec = RescueSymlinkProgram
 
 all: $(dynamic_exec) $(static_exec)

Index: src/CommandNotFound.c
===================================================================
RCS file: src/CommandNotFound.c
diff -N src/CommandNotFound.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/CommandNotFound.c	26 Feb 2008 06:34:36 -0000	1.1
@@ -0,0 +1,85 @@
+// Copyright (C) 2008 Michael Homer <=mwh>
+// This program 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.
+// 
+// This program 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.
+
+#include <stdio.h>
+#include <sys/stat.h>
+#include <string.h>
+
+// Arbitrary, but much larger than we need here
+#define MAX_ITERATIONS 30
+#define BUFLEN 512
+#define DATAFILE "/Programs/Scripts/Current/Data/CommandNotFound.data"
+
+void multiprogrammessage(char * executable, char * program, char * program2) {
+	printf("The program '%s' is not currently installed.\nIt is available in the \
following packages:\n", executable); +	printf(" %s, %s", program, program2);
+	while (program2 = strtok(NULL, " "))
+		printf(", %s", program2); // The last program will include the newline in it, no \
need to be explicit +	printf("You can install one of these by typing (for example):\n \
InstallPackage %s\nor\n Compile %s\n", program, program); +}
+
+void singleprogrammessage(char * executable, char * program) {
+	printf("The program '%s' is not currently installed.\nYou can install it by \
typing:\n", executable); +	printf(" InstallPackage %sor\n Compile %s", program, \
program); +}
+
+int foundexecutable(char * executable, char * target) {
+	// As the message output is different for multiple-program entries, read
+	// to check whether there is more than one and pass the data on accordingly
+	char * program = strtok(NULL, " ");
+	char * program2 = strtok(NULL, " ");
+	if (NULL != program2)
+		multiprogrammessage(executable, program, program2);
+	else
+		singleprogrammessage(executable, program);
+	return 0;
+}
+
+int binsearch(FILE * fp, char * target, int lo, int hi, char * last, int depth) {
+	int mid = lo + (hi-lo)/2;
+	char entry [BUFLEN];
+	char * executable;
+	if ((depth > MAX_ITERATIONS) || (mid == lo))
+		return depth; // No infinite loops when we're not getting anywhere
+	// Jump to our current midpoint
+	fseek(fp, mid, SEEK_SET);
+	// We're probably in the middle of a line, so discard it, then use the next
+	fgets(entry, BUFLEN, fp);
+	mid -= strlen(entry); // For edge cases where we always end up in the middle
+	fgets(entry, BUFLEN, fp);
+	executable = strtok(entry, " ");
+	// Terminate if we're at the same entry we were at last time
+	if (last && (strcmp(last, executable) == 0))
+		return depth;
+	int cmpval = strcmp(executable, target);
+	if (0 == cmpval)
+		return foundexecutable(executable, target);
+	else if (0 > cmpval)
+		return binsearch(fp, target, mid, hi, executable, depth + 1);
+	else if (0 < cmpval)
+		return binsearch(fp, target, lo, mid, executable, depth + 1);
+}
+
+int main(int argc, char **argv) {
+	FILE * fp;
+	if (argc < 2)
+		return 1;
+	// Stat for the filesize to initialise the binary search
+	struct stat st;
+	stat(DATAFILE, &st);
+	
+	if (!(fp = fopen(DATAFILE, "r")))
+		return 1; // If file doesn't exist, fail silently
+	if (binsearch(fp, argv[1], 0, st.st_size, NULL, 0))
+		printf("The program '%s' is not currently installed, and no known package contains \
it.\n", argv[1]); +	fclose(fp);
+	return 0;
+}

Index: bin/CommandNotFound
===================================================================
RCS file: bin/CommandNotFound
diff -N bin/CommandNotFound
--- bin/CommandNotFound	25 Feb 2008 06:33:36 -0000	1.4
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,47 +0,0 @@
-#!/usr/bin/env python
-# Copyright (C) 2008 Michael Homer <=mwh>
-# This program 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.
-#
-# This program 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.
-
-import sys, os
-import textwrap
-
-# Limited to real Gobo for now; importing getGoboVariable takes too long.
-path = '/Programs/Scripts/Current/Data/CommandNotFound.py'
-def log(s):
-	"""Output word-wrapped text"""
-	sys.stdout.write('\n'.join(textwrap.wrap(s, 80))+'\n')
-
-# 'commands' is a dict of executable=>(package, ...)
-commands = dict()
-if os.path.exists(path) and os.path.getsize(path) > 0:
-	commands = eval(open(path).read())
-
-# The command to search for is the first argument
-cmd = sys.argv[1]
-
-if cmd in commands:
-	packages = commands[cmd]
-	# Occasionally executables are in more than one program. In those cases, give a \
                list of all those programs,
-	# and a suggestion of how to install them. Otherwise, just the suggestion.
-	if len(packages)>1:
-		log('The program "%s" is not currently installed. It is available in the following \
                packages:'%(cmd))
-		log(' ' + ', '.join(packages))
-		log('You can install one of these by typing (for example):')
-		log(' InstallPackage %s'%(packages[0]))
-		log('or')
-		log(' Compile %s'%(packages[0]))
-	else:
-		log('The program "%s" is not currently installed. You can install it by \
                typing:'%(cmd))
-		log(' InstallPackage %s'%(packages[0]))
-		log('or')
-		log(' Compile %s'%(packages[0]))
-else: 
-	log('The program "%s" is not currently installed, and there is no known package \
containing it.'%(cmd)) _______________________________________________
gobolinux-commits mailing list
gobolinux-commits@lists.gobolinux.org
http://lists.gobolinux.org/mailman/listinfo/gobolinux-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic