schaefer@ogcvax.UUCP (Barton E. Schaefer) (02/25/88)
One small drawbacks of the "iface" interface package (recently posted to comp.sources.unix) is the requirement that the addcom() calls be made in command-sorted order. The author suggests that the interested user write an insertion sort to add commands in another order if desired, but why write your own sort when qsort(3) is available? The following patch (to comc.c, iface.c, iface.h, and init.c) allows commands to be added in any order. addcom() detects when an unsorted addition is made and sets a flag; getcom() tests that flag and does a qsort() if necessary before looking up the command. If a getcom() is done after each addcom(), this is not very efficient, but if several addcom()s are done between getcom()s it probably does better than an insertion sort. If you want the old behavior, just compile with "-DNOSORT". This patch does NOT include the get_string() bug fix (see the x-referenced article in comp.sources.bugs). *** comc.c.old Wed Feb 24 14:47:38 1988 --- comc.c Wed Feb 24 14:24:05 1988 *************** *** 1,3 **** --- 1,9 ---- + #include <stdio.h> + #include "iface.h" + #ifndef NOSORT + int pstrcmp(); /* forward declaration */ + #endif + /* getcom: get a command (character string) from standard input getcom takes a list of character strings in the same format *************** *** 21,28 **** ^U clears the cmd string and restarts completion */ - #include <stdio.h> - getcom(clist,ncmd,prompt) char **clist; int ncmd; --- 27,32 ---- *************** *** 36,41 **** --- 40,51 ---- cmd[0] = '\0'; while (!match) { + #ifndef NOSORT + if (sortcmd) { + qsort(cmds,ncmds,sizeof(char *),pstrcmp); + sortcmd = 0; + } + #endif c = getchar(); switch(c) { *************** *** 216,218 **** --- 226,235 ---- } } + #ifndef NOSORT + int pstrcmp(ps1,ps2) + char **ps1, **ps2; + { + return strcmp(*ps1,*ps2); + } + #endif *** iface.c.old Wed Feb 24 14:47:52 1988 --- iface.c Wed Feb 24 14:13:47 1988 *************** *** 9,14 **** --- 9,17 ---- char *cmds[256], *syms[NSYM], lastkey; int (*cptr[256])(), binding[128][4]; int oldflags, ncmds, nsyms; + #ifndef NOSORT + int sortcmd = 0; + #endif struct ltchars termc; struct sgttyb iobasic; *** iface.h.old Wed Feb 24 14:47:55 1988 --- iface.h Wed Feb 24 14:12:35 1988 *************** *** 8,13 **** --- 8,16 ---- extern char *cmds[256], *syms[NSYM], lastkey; extern int (*cptr[256])(), binding[128][4]; extern int oldflags, ncmds, nsyms; + #ifndef NOSORT + extern int sortcmd; + #endif extern struct ltchars termc; extern struct sgttyb iobasic; *** init.c.old Wed Feb 24 14:48:05 1988 --- init.c Wed Feb 24 14:42:08 1988 *************** *** 55,62 **** --- 55,66 ---- int ref; if (strcmp(str,cmds[ncmds-1]) < 0) { + #ifdef NOSORT printf("# Yuck! initial command %s is out of place!\n",str); quit(); + #else + sortcmd = 1; + #endif } cmds[ncmds] = malloc((unsigned)(strlen(str)+1)); -- Bart Schaefer CSNET: schaefer@cse.ogc.edu UUCP: ...{tektronix,verdix}!ogcvax!schaefer "You can lead a yak to water, but you can't teach an old dog to make a silk purse out of a pig in a poke." -- Opus