From 0a7d939c9b9d4cce5c40683fb5e67eb32e33b685 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Tue, 9 Jul 2024 01:06:03 -0400 Subject: [PATCH] Adding module unipar.c --- app/Makefile | 12 +- app/{mailled.c => maild.c} | 9 +- lib/Makefile | 5 + lib/subrou.c | 3 +- lib/unipar.c | 253 +++++++++++++++++++++++++++++++++++++ lib/unipar.h | 29 +++++ 6 files changed, 306 insertions(+), 5 deletions(-) rename app/{mailled.c => maild.c} (83%) create mode 100644 lib/unipar.c create mode 100644 lib/unipar.h diff --git a/app/Makefile b/app/Makefile index 32e15b1..6d7ee49 100644 --- a/app/Makefile +++ b/app/Makefile @@ -21,10 +21,10 @@ clean : #Equivalences #-------------------------------------------------------------------- EXE= \ - mailled \ + maild \ SRC= \ - mailled.c \ + maild.c \ #-------------------------------------------------------------------- #definitions @@ -37,9 +37,15 @@ LIBS = $(LIBAI) #-------------------------------------------------------------------- #Dependances #-------------------------------------------------------------------- -mailled : toremake mailled.o +maild : toremake maild.o @ $(LD) $(LDFLAGS) -o ../bin/$@ $@.o $(LIBS) + +maild.o : maild.c \ + ../lib/unipar.h \ + ../lib/subrou.h \ + + toremake: Makefile $(LIBAI) touch toremake - rm -f $(EXE) *.o diff --git a/app/mailled.c b/app/maild.c similarity index 83% rename from app/mailled.c rename to app/maild.c index 7e0f573..a90cfad 100644 --- a/app/mailled.c +++ b/app/maild.c @@ -8,6 +8,7 @@ #include #include "subrou.h" +#include "unipar.h" /* @@ -31,21 +32,27 @@ int main(int argc,char *argv[]) { int status; +ARGTYP *params; int phase; _Bool proceed; status=0; +params=(ARGTYP *)0; phase=0; proceed=true; while (proceed==true) { switch (phase) { case 0 : //initialising process (void) rou_modesubrou(true); + (void) uni_modeunipar(true); break; case 1 : //checking parameters - (void) fprintf(stderr,"JMPDBG Vers='%s'\n",rou_getversion()); + if ((params=par_getparams(argc,argv,"d:hr:v"))==(ARGTYP *)0) { + phase=999; //no need to go further + } break; default : //end of task + (void) uni_modeunipar(false); (void) rou_modesubrou(false); proceed=false; break; diff --git a/lib/Makefile b/lib/Makefile index 911c3b9..f530df6 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -16,6 +16,7 @@ clean : #Equivalences #-------------------------------------------------------------------- OBJS= \ + unipar.o \ subrou.o objs : $(OBJS) @@ -24,6 +25,10 @@ objs : $(OBJS) #-------------------------------------------------------------------- #Dependances +unipar.o: \ + subrou.h \ + unipar.h unipar.c + subrou.o: \ subrou.h subrou.c diff --git a/lib/subrou.c b/lib/subrou.c index 9639ec8..0414c09 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -14,7 +14,7 @@ //version definition #define VERSION "0.1" -#define RELEASE "6" +#define RELEASE "7" //Public variables PUBLIC int debug=0; //debug level @@ -153,6 +153,7 @@ if (mode!=modopen) { switch ((int)mode) { case true : debug=0; + //application started in background mode by default foreground=false; off64_time=(time_t)0; off_date=(time_t)0; diff --git a/lib/unipar.c b/lib/unipar.c new file mode 100644 index 0000000..8ad1745 --- /dev/null +++ b/lib/unipar.c @@ -0,0 +1,253 @@ +// vim: smarttab tabstop=8 shiftwidth=2 expandtab +/********************************************************/ +/* */ +/* Low level subroutine declaration */ +/* to hanle an argv list and extract */ +/* parameters. */ +/* */ +/********************************************************/ +#include +#include +#include +#include + +#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; +} +/* + +*/ +/********************************************************/ +/* */ +/* 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"); +} +/* + +*/ +/********************************************************/ +/* */ +/* 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"); +} +/* + +*/ +/********************************************************/ +/* */ +/* 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;iargc;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 +} diff --git a/lib/unipar.h b/lib/unipar.h new file mode 100644 index 0000000..29530af --- /dev/null +++ b/lib/unipar.h @@ -0,0 +1,29 @@ +// vim: smarttab tabstop=8 shiftwidth=2 expandtab +/********************************************************/ +/* */ +/* Low level subroutine declaration */ +/* to hanle an argv list and extract */ +/* parameters. */ +/* */ +/********************************************************/ +#ifndef UNIPAR +#define UNIPAR + +#include + +//structure of argument +typedef struct { + int argc; //number of main argument + char **argv; //list of argument + }ARGTYP; + +//free allocated memory used by a ARGTYP structure +extern ARGTYP *par_freeparams(ARGTYP *params); + +//allocated memory and parse an argment list +extern ARGTYP *par_getparams(int argc,char * const argv[],const char *optstring); + +//homework to be done before starting/stoping module. +extern int uni_modeunipar(_Bool mode); + +#endif -- 2.47.3