home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************
- * File: searchCmnds.c
- *
- * Author: Rhett "Jonzy" Jones
- * jonzy@cc.utah.edu
- *
- * Date: May 13, 1993
- *
- * Modified: May 23, 1993 by Rhett "Jonzy" Jones.
- * Commented out the pathway stuff with #ifdef DO_PATHWAY
- * because this isn't ready yet.
- * Merged this routine into jughead.
- * Modified StillNeed2Parse() such that "?" gets mapped to
- * "?help".
- *
- * At the request of doylej@liberty.uc.wlu.edu gave support
- * for the "range=start-stop what" special command.
- *
- * Description: This module takes care of some special commands for use
- * by the jughead search engine. A special command is defined
- * as one of the following:
- * ?all what
- * ?help [what]
- * ?limit=n what
- * ?version [what]
- * ?range=start-stop what
- * ?pathway what NOT supported yet.
- * where 'what' is a standard search string, anything enclosed
- * in square brackets is optional, and all special commands must
- * be preceeded with '?'.
- *
- * The pathway command is currently not supported.
- *
- * Routines: static short StillNeed2Parse(char *string,short *theCommand,
- * long *limit,long *rangeStart,
- * long *rangeEnd);
- * static int HandleSpecialCommand(char *string,
- * short theCommand);
- * char *SpecialCommand(char *what2find,long *limit);
- *
- * Bugs: No known bugs.
- *
- * Copyright: Copyright 1993, University of Utah Computer Center.
- * This source may be freely distributed as long as this copyright
- * notice remains intact, and is in no way used for any monetary
- * gain, by any institution, business, person, or persons.
- *
- ****************************************************************************/
-
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
-
- #define NIL (void *)0
-
- #define RANGEERROR -4 /* Error with range usage. */
- #define PATHWAYERROR -3 /* Error with pathway usage. */
- #define USAGERROR -2 /* Generic usage error. */
- #define LIMITERROR -1 /* Error with limit usage. */
-
- /* Various jughead commands followed by syntax. */
- #define ALLCMND 1 /* ?all what */
- #define HELPCMND 2 /* ?help [what] */
- #define LIMITCMND 3 /* ?limit=n what */
- #define VERSIONCMND 4 /* ?version [what] */
- #define RANGECMND 5 /* ?range=start-end what */
- #ifdef DO_PATHWAY
- #define PATHWAYCMND 5 /* ?pathway what */
- #endif
-
- /* This variable lists the special commands for use by the jughead search engine. */
- static char *specialCmnds[] = { "all", /* Return all entries in excess of 1024. */
- "help", /* Return a link to a help file. */
- "limit", /* Return at most the first n entries. */
- "version", /* Return the version of jughead. */
- "range", /* Return a range of items.*/
- #ifdef DO_PATHWAY
- "pathway", /* Return the pathway for a selection. */
- #endif
- 0 };
- #ifdef DO_PATHWAY
- char *pathString; /* The selStr to find the path of. */
- #endif
-
- extern int SendString(); /* defined in "sockets.c". */
-
- /*****************************************************************************
- * StillNeed2Parse determines if we have a special command and if so assigns
- * the appropriate command to 'theCommand', which includes any command errors
- * encountered. This routine returns true if there is still information to
- * parse, and false otherwise.
- ****************************************************************************/
- static short StillNeed2Parse(string,theCommand,limit,rangeStart,rangeEnd)
- char *string; /* The line of text given. */
- short *theCommand; /* Do we have a special command? */
- long *limit, /* The number of items to return. */
- *rangeStart, /* The start of a range. */
- *rangeEnd; /* The end of a range. */
- { char *str, /* Position in 'string'. */
- **s, /* The special commands. */
- theSpecialCmnd[12]; /* The special command the user issued. */
- short i; /* A loop counter. */
-
- /* Initialize some variables. */
- *rangeStart = *rangeEnd = *theCommand = 0;
- theSpecialCmnd[0] = '\0';
-
- /* Skip any whitespace. */
- for (str = string; *str && isspace(*str); str++);
-
- if (*str == '?') /* Looks like a special command. */
- {
- /* Get a copy of the special command. */
- if (!strcmp(str,"?")) /* Treat it like ?help. */
- strcpy(theSpecialCmnd,"help");
- else
- {
- for (++str, i = 0; i < 12 && isalpha(*str); i++, str++)
- theSpecialCmnd[i] = *str;
- theSpecialCmnd[i] = '\0';
- }
-
- /* Locate the special command we found. */
- for (*theCommand = 1, s = specialCmnds; *s; s++, ++*theCommand)
- if (!strcmp(*s,theSpecialCmnd))
- break;
-
- switch (*theCommand)
- {
- case ALLCMND:
- *limit = -1;
- break;
- case HELPCMND:
- break;
- case LIMITCMND:
- *limit = 0;
- if (*str == '=' && isdigit(*++str))
- for (*limit = 0; isdigit(*str); str++)
- *limit = *limit * 10 + *str - '0';
- else
- *theCommand = LIMITERROR;
- break;
- case VERSIONCMND:
- break;
- case RANGECMND:
- if (*str == '=' && isdigit(*++str))
- {
- for (*rangeStart = 0; isdigit(*str); str++)
- *rangeStart = *rangeStart * 10 + *str - '0';
- if (*str == '-')
- for (++str, *rangeEnd = 0; isdigit(*str); str++)
- *rangeEnd = *rangeEnd * 10 + *str - '0';
- else
- {
- *rangeStart = *rangeEnd = 0;
- *theCommand = RANGEERROR;
- }
- }
- else
- *theCommand = RANGEERROR;
- break;
- #ifdef DO_PATHWAY
- case PATHWAYCMND:
- if (!*str)
- {
- *theCommand = PATHWAYERROR;
- break;
- }
- if (*str) /* Skip any whitespace. */
- for (++str; isspace(*str); str++);
-
- /* Extract the pathString. */
- if (*str == '"')
- {
- for (pathString = ++str; *str && *str != '"'; str++);
- if (*str == '"')
- *str++ = '\0';
- else
- {
- *theCommand = PATHWAYERROR;
- break;
- }
- }
- else
- {
- for (pathString = str; *str && !isspace(*str); str++);
- *str = '\0';
- }
- break;
- #endif
- default: /* Oops we're in deep shit. */
- *theCommand = USAGERROR;
- theSpecialCmnd[0] = '\0';
- break;
- }
-
- /* Shft the special command out from 'string'. */
- if (*str)
- for (++str; isspace(*str); str++); /* Skip any whitespace. */
- for (i = 0; *str; i++, str++)
- string[i] = *str;
- string[i] = '\0';
- }
-
- return(*string);
-
- } /* StillNeed2Parse */
-
- /*****************************************************************************
- * HandleSpecialCommand returns true if 'theCommand' is an error or we
- * discover an error, and send the information back to the client following
- * gopher protocol. Otherwise this routine returns false and sets some
- * variables or accomplishes the special command.
- ****************************************************************************/
- static int HandleSpecialCommand(string,theCommand)
- char *string; /* The remainder of the input line. */
- short theCommand; /* The special command or error. */
- { int error = 0; /* Did we get an error? */
- char help[1024]; /* Line of text adhering to gopher protocol. */
-
- switch (theCommand)
- {
- case RANGEERROR:
- sprintf(help,"0range usage error%s",JUGHEADHELP);
- error = SendString(help);
- break;
- case PATHWAYERROR:
- sprintf(help,"0pathway usage error%s",JUGHEADHELP);
- error = SendString(help);
- break;
- case USAGERROR:
- sprintf(help,"0jughead usage error%s",JUGHEADHELP);
- error = SendString(help);
- break;
- case LIMITERROR:
- sprintf(help,"0limit usage error%s",JUGHEADHELP);
- error = SendString(help);
- break;
- case ALLCMND:
- if (!*string)
- {
- sprintf(help,"0all usage error%s",JUGHEADHELP);
- error = SendString(help);
- }
- break;
- case HELPCMND:
- sprintf(help,"0jughead help%s",JUGHEADHELP);
- SendString(help);
- break;
- case LIMITCMND:
- if (!*string)
- {
- sprintf(help,"0limit usage error%s",JUGHEADHELP);
- error = SendString(help);
- }
- break;
- case VERSIONCMND:
- sprintf(help,"0This version of jughead is %s%s",VERSION,JUGHEADHELP);
- SendString(help);
- break;
- case RANGECMND:
- break;
- #ifdef DO_PATHWAY
- case PATHWAYCMND:
- /* exec pathway on pathString. */
- break;
- #endif
- }
- return(error);
-
- } /* HandleSpecialCommand */
-
- /*****************************************************************************
- * SpecialCommand handles any special command the jughead search engine
- * might get, and returns nil if there is nothing to parse out, and the
- * string to continue parsing if there is any.
- ****************************************************************************/
- char *SpecialCommand(what2find,limit,rangeStart,rangeEnd)
- char *what2find; /* The input line. */
- long *limit, /* The maximum number of items to return. */
- *rangeStart, /* The start of a range. */
- *rangeEnd; /* The end of a range. */
- { short theCommand; /* Do we have a special command? */
-
- if (StillNeed2Parse(what2find,&theCommand,limit,rangeStart,rangeEnd))
- {
- if (theCommand)
- if (HandleSpecialCommand(what2find,theCommand))
- return((char *)NIL);
- }
- else if (theCommand)
- {
- (void)HandleSpecialCommand(what2find,theCommand);
- return((char *)NIL);
- }
-
- return(what2find);
-
- } /* SpecialCommand */
-