/* aprogram to add a suffix to all words in 
 * stdin, and write to stdout
 $Log: not supported by cvs2svn $
 Revision 1.1  1996/06/23 14:23:17  emery
 First installation of affix.c

 */

#include "mdb.h"
#include "scan.h"
#include "match_string.h"
#include <stdio.h>

static char *USAGE="affix -prefix=<string> -suffix=<string>\n\
Affixes a prefix or a suffix or both to strings read from stdin.\n\
Results are written to stdout";


#define CLO_PREFIX 0
#define CLO_SUFFIX 1
#define COMMANDLINE_OPTIONS 2
char *commandline_option[COMMANDLINE_OPTIONS] = {
    "prefix",
    "suffix"
        };

int main(int argc, char **argv){
    long i_arg;
    char *prefix,*suffix,word[1024];
    SCANNED_ARG *s_arg;
    

    argc = scanargs(&s_arg, argc, argv);
    if (argc==1) 
        bomb(NULL,  USAGE);

    prefix=suffix=NULL;
    for (i_arg=1; i_arg<argc; i_arg++) {
        if (s_arg[i_arg].arg_type==OPTION) {
            switch (match_string(s_arg[i_arg].list[0], commandline_option, COMMANDLINE_OPTIONS, UNIQUE_MATCH)) {
              case CLO_PREFIX:
                if(!(prefix=s_arg[i_arg].list[1]))
                    bomb("(affix) no string given for -prefix",USAGE);
                break;
              case CLO_SUFFIX:
                if(!(suffix=s_arg[i_arg].list[1]))
                    bomb("(affix) no string given for -suffix",USAGE);
                break;
              default:
                bomb("(affix) unrecognized option given", USAGE );
                break;
                }
            }
        }    
    while(EOF!=scanf("%s",word)){
        if(prefix)
            fputs(prefix,stdout);
        fputs(word,stdout);
        if(suffix)
            fputs(suffix,stdout);
        printf(" ");
        }
    printf("\n");
    return(0);
    }

