]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Adding module unipar.c
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Tue, 9 Jul 2024 05:06:03 +0000 (01:06 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Tue, 9 Jul 2024 05:06:03 +0000 (01:06 -0400)
app/Makefile
app/maild.c [moved from app/mailled.c with 83% similarity]
lib/Makefile
lib/subrou.c
lib/unipar.c [new file with mode: 0644]
lib/unipar.h [new file with mode: 0644]

index 32e15b1e8ec64396c19e6a5ae77ab3b1643a8947..6d7ee495036cbc5ff9b1094bac3d024e4f55d898 100644 (file)
@@ -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
similarity index 83%
rename from app/mailled.c
rename to app/maild.c
index 7e0f573efef38ed27790570dd02690d59db066c0..a90cfad1beeb9ab09861bd85f0fcb643ff03640d 100644 (file)
@@ -8,6 +8,7 @@
 #include       <stdio.h>
 
 #include       "subrou.h"
+#include       "unipar.h"
 
 /*
 \f
@@ -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;
index 911c3b9a19f480fc7912967988db8062c11e072f..f530df6df3d72e488a70368727609c7e6be7eb77 100644 (file)
@@ -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
 
index 9639ec8e5b4ecf4da2f4a2d7e80bd92d9d17a607..0414c097900e3a544025d582ba3f1f4c3968586b 100644 (file)
@@ -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 (file)
index 0000000..8ad1745
--- /dev/null
@@ -0,0 +1,253 @@
+// 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
+}
diff --git a/lib/unipar.h b/lib/unipar.h
new file mode 100644 (file)
index 0000000..29530af
--- /dev/null
@@ -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        <stdbool.h>
+
+//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