--- /dev/null
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/* */
+/* Low level subroutine declaration */
+/* to hanle an argv list and extract */
+/* parameters. */
+/* */
+/********************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "subrou.h"
+#include "unipar.h"
+
+static _Bool modopen; //module open/close status
+/*
+^L
+*/
+/********************************************************/
+/* */
+/* */
+/* Procedure to init the argument list */
+/* */
+/********************************************************/
+static ARGTYP *initparams()
+
+{
+ARGTYP *params;
+
+params=(ARGTYP *)calloc(1,sizeof(ARGTYP));
+params->argv=(char **)0;
+return params;
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Display aid,aidhud usage parameter */
+/* */
+/********************************************************/
+static void usage_aid(char *name)
+
+{
+(void) fprintf(stderr,"%s-%s\n",name,rou_getversion());
+(void) fprintf(stderr,"usage:\n ");
+(void) fprintf(stderr,"%s\t"
+ "[-d debug] "
+ "[-h] "
+ "[-r root] "
+ "[-v] "
+ "\n",name);
+(void) fprintf(stderr,"\twhere:\n");
+(void) fprintf(stderr,"\t\t-d level\t: debug level [1-10]\n");
+(void) fprintf(stderr,"\t\t-h\t\t: print this help message\n");
+(void) fprintf(stderr,"\t\t-r root\t\t: root working directory\n");
+(void) fprintf(stderr,"\t\t-v\t\t: Print program version number\n");
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Display aidobj usage parameter */
+/* */
+/********************************************************/
+static void usage_obj(char *name)
+
+{
+(void) fprintf(stderr,"%s-%s\n",name,rou_getversion());
+(void) fprintf(stderr,"usage:\n ");
+(void) fprintf(stderr,"%s\t"
+ "[-d debug] "
+ "[-h] "
+ "[-r root] "
+ "[-v] "
+ "\n",name);
+(void) fprintf(stderr,"\twhere:\n");
+(void) fprintf(stderr,"\t\t-d level\t: debug level [1-10]\n");
+(void) fprintf(stderr,"\t\t-h\t\t: print this help message\n");
+(void) fprintf(stderr,"\t\t-r root\t\t: root working directory\n");
+(void) fprintf(stderr,"\t\t-v\t\t: Print program version number\n");
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Display a list of parameters available according*/
+/* the name used to call the backd program */
+/* */
+/********************************************************/
+static int usage(char *name)
+
+{
+static char *prg[]={
+ "aid",
+ "aidhud",
+ "aidobj",
+ (char *)0
+ };
+int status;
+int seq;
+char *shortname;
+
+status=0;
+if ((shortname=strrchr(name,'/'))==(char *)0)
+ shortname=name;
+else
+ shortname++;
+for (seq=0;prg[seq]!=(char *)0;seq++) {
+ if (strcmp(prg[seq],shortname)==0)
+ break;
+ }
+switch (seq) {
+ case 0 : //aid
+ case 1 : //aidhud
+ (void) usage_aid(name);
+ break;
+ case 2 : //aidobj
+ (void) usage_obj(name);
+ break;
+ default :
+ (void) usage_aid(name); //unexpected default!
+ break;
+ }
+return status;
+}
+/*
+^L
+*/
+/********************************************************/
+/* */
+/* Procedure to free memory used by an arg */
+/* list */
+/* */
+/********************************************************/
+ARGTYP *par_freeparams(ARGTYP *params)
+
+{
+if (params!=(ARGTYP *)0) {
+ if (params->argv!=(char **)0) {
+ char **ptr;
+
+ ptr=params->argv;
+ while (*ptr!=(char *)0) {
+ (void) free(*ptr);
+ ptr++;
+ }
+ (void) free(params->argv);
+ }
+ (void) free(params);
+ params=(ARGTYP *)0;
+ }
+return params;
+}
+/*
+^L
+*/
+/********************************************************/
+/* */
+/* Procedure to extract argument list */
+/* but cherry-pick only the one allowed by */
+/* sequence optstring. */
+/* on error return an null argtype */
+/* */
+/********************************************************/
+ARGTYP *par_getparams(int argc,char * const argv[],const char *optstring)
+
+{
+ARGTYP *params;
+int c;
+
+params=initparams();
+opterr=0; //no error message from getopt library routine
+while (((c=getopt(argc,argv,optstring))!=EOF)&&(params!=(ARGTYP *)0)) {
+ switch(c) {
+ case 'd' : //debug level
+ debug=atoi(optarg);
+ (void) rou_alert(1,"debug level is now '%d'",debug);
+ break;
+ case 'f' : //foreground mode
+ foreground=true;
+ break;
+ case 'h' : //requestion program help
+ (void) usage(argv[0]);
+ params=par_freeparams(params);
+ break;
+ case 'r' :
+ if (rootdir!=(char *)0)
+ (void) free(rootdir);
+ rootdir=strdup(optarg);
+ break;
+ case 'v' :
+ (void) fprintf(stderr,"%s Version <%s>\n",argv[0],rou_getversion());
+ (void) exit(0); //just display version
+ break;
+ default :
+ (void) usage(argv[0]);
+ (void) rou_alert(0,"\"%s\" unexpected argument designator",argv[optind-1]);
+ params=par_freeparams(params);
+ break;
+ }
+ }
+if ((params!=(ARGTYP *)0)&&(argc>optind)) {
+ int i;
+
+ params->argc=argc-optind;
+ params->argv=(char **)calloc(params->argc+1,sizeof(char *));
+ for (i=0;i<params->argc;i++) {
+ params->argv[i]=strdup(argv[optind+i]);
+ }
+ }
+return params;
+}
+/*
+^L
+*/
+/********************************************************/
+/* */
+/* Procedure to "open/close" module and do */
+/* homework purpose */
+/* return zero if everything right */
+/* */
+/********************************************************/
+int uni_modeunipar(_Bool mode)
+
+{
+#define OPEP "unipar.c:uni_modeunipar"
+
+int status;
+
+status=0;
+if (mode!=modopen) {
+ (void) rou_modesubrou(mode);
+ switch ((int)mode) {
+ case true :
+ break;
+ case false :
+ break;
+ default :
+ (void) fprintf(stderr,"Calling %s with wrong mode='%d' (Bug?!):",
+ OPEP,(int)mode);
+ status=-1;
+ break;
+ }
+ modopen=mode;
+ }
+return status;
+#undef OPEP
+}