/*  parse_path_info		WJS  Aug 97				*/
/*    This program accepts a string formatted like the PATH_INFO	*/
/*  environment variable.  It will take the string from the command	*/
/*  line invoking the program.  If there is nothing on the command	*/
/*  line, it will use PATH_INFO environment variable.			*/
/*    The program also accepts an optional -nonewline switch con-	*/
/*  trolling whether or not program output is followed by a newline	*/
/*  character.								*/
/*    The PATH_INFO environment variable contains 4 fields.  This	*/
/*  program outputs these 4 fields, followed by a newline, to stdout.	*/
/*  The newline does not appear if the -nonewline switch is specified.	*/
/*  It labels the first 3, but does NOT label the 4th.  At present, the	*/
/*  4th already contains labelled information, so the effect will be to	*/
/*  output a string containing an indefinite amount of labelled		*/
/*  information... unless the format of the 4th field changes!  At some	*/
/*  future time, routines will be written to parse the 4th field and	*/
/*  remove this potential problem					*/
/*    A labelled field is of the form					*/
/*		label=field						*/
/*  The labelled fields are separated by commas.  The assumption is	*/
/*  that no field contains a comma or an equal sign.  The labels are	*/
/*  object, protocol, and level.  Empty fields are output (eg, the	*/
/*  the output string can contain "label=")				*/
/*    The use of equal signs and commas is tied to the assumed format	*/
/*  of the 4th field, again in hopeful anticipation...			*/
/*    The program exits with a 250 (or whatever is defined in error_	*/
/*  exit_defn.h) if a bad switch is specified, if more than 1 input	*/
/*  string is specified, if the PATH_INFO environment variable is	*/
/*  improperlyformatted or if there is trouble getting dynamic memory;	*/
/*  0 else.  A partial result string may be output in error situations,	*/
/*  followed by ***ERROR***						*/
/*									*/
/*    Example:  if PATH_INFO has the value				*/
/*	//gb1.whoi.edu/test.html0{dir=xx,info=yy}			*/
/*    parse_path_info will write the string				*/
/*	object=//gb1.whoi.edu/test,protocol=html,level=0,dir=xx,info=yy	*/
/*									*/
/*    See the path_info_routines.c file for more information		*/

#define PARSE_PATH_INFO_ID "parse_path_info version 1.1a 18 Jul 1999"
/*  18 Jul 99  v 1.1a			WJS				*/
/*	Allow -no as a synonym for -nonewline				*/
/*	    [Begin 1.1a]						*/
/*  20 Apr 99  v 1.1			WJS				*/
/*	-nonewline switch						*/
/*	New error exit status						*/
/*	    [Needs path_info_routines.c v 1.2]				*/
/*	    [Needs path_info_routines.h v 1.3]				*/
/*	    [Begin 1.1]							*/
/*  29 Aug 97  v 1.0			WJS				*/
/*	Look for input on command line before using PATH_INFO		*/
/*  13 Aug 97  v 1.0			WJS				*/
/*	    [Needs path_info_routines.c v 1.2; .h v 1.1]		*/
/*	    [Begin 1.0]							*/

  /*  Force version string into object files				*/
char *parse_path_info_id = PARSE_PATH_INFO_ID;

#include "path_info_routines.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

  /*  path_info_routines						*/
char *get_object();
char *get_protocol();
int get_level();
char *get_options();

void print_it(s,t,terminator)
char *s,*t,*terminator;
{
  if (s != NULL) printf ("%s%c",s,PATH_INFO_OPTIONS_DEFN_CHAR);

  if (t == NULL) {
    printf ("***ERROR***\n");
    exit(ERROR_EXIT_STATUS);	/* defined in error_exit_defn.h		*/
  } else
    printf ("%s%s",t,terminator);

  return;
}

main(argc,argv)
int argc;
char *argv[];
{
  int level;
  char c[2] = {PATH_INFO_OPTIONS_SEPARATOR_CHAR, '\0'};
  char *ptr;
  char *input = NULL;
  char output_terminator[2] = "\n";
  int i;

  for (i = 1; i < argc; i++)
    if (*argv[i] == '-')
      if ( (strcmp(argv[i],"-no") == 0)	      ||
	   (strcmp(argv[i],"-nonewline") == 0)  )
	output_terminator[0] = '\0';
      else {
	printf ("***ERROR*** illegal switch: %s\n",argv[i]);
	exit(ERROR_EXIT_STATUS);	/* defined in error_exit_defn.h	*/
      }
    else {
      if (input != NULL) {
	printf ("***ERROR*** > 1 input param: %s %s\n",input,argv[i]);
	exit(ERROR_EXIT_STATUS);	/* defined in error_exit_defn.h	*/
      }
      input = argv[i];
    }

  print_it("object",get_object(input),c);

  print_it("protocol",get_protocol(input),c);

    /*  Look ahead, & don't put out trailing comma if options field 	*/
    /*  is empty							*/
  ptr = get_options(input);
  if (ptr != NULL)
    if (*ptr == '\0') *c = '\0';

  level = get_level(input);
  if (level == LEVEL_NOT_SPECIFIED) print_it("level","",c);
  else if (level == LEVEL_ERROR) print_it("level",NULL,c);
  else printf("level%c%d%s",PATH_INFO_OPTIONS_DEFN_CHAR,level,c);

  print_it(NULL,ptr,output_terminator);

  exit(0);
}
