stage4/generate_c/generate_c.cc
changeset 1060 01c48180af05
parent 1059 aef63aa7ef2e
child 1061 f762b43c8fad
equal deleted inserted replaced
1059:aef63aa7ef2e 1060:01c48180af05
   176 /***********************************************************************/
   176 /***********************************************************************/
   177 
   177 
   178 
   178 
   179 static int generate_line_directives__ = 0;
   179 static int generate_line_directives__ = 0;
   180 static int generate_pou_filepairs__   = 0;
   180 static int generate_pou_filepairs__   = 0;
   181 static int generate_plc_state_backup_fuctions__ = 1;
   181 static int generate_plc_state_backup_fuctions__ = 0;
   182 
   182 
   183 #ifdef __unix__
   183 #ifdef __unix__
   184 /* Parse command line options passed from main.c !! */
   184 /* Parse command line options passed from main.c !! */
   185 #include <stdlib.h> // for getsybopt()
   185 #include <stdlib.h> // for getsubopt()
   186 int  stage4_parse_options(char *options) {
   186 int  stage4_parse_options(char *options) {
   187   enum {                    LINE_OPT = 0            ,  SEPTFILE_OPT              /*, SOME_OTHER_OPT, YET_ANOTHER_OPT */};
   187   enum {LINE_OPT = 0,  
   188   char *const token[] = { /*[LINE_OPT]=*/(char *)"l",/*SEPTFILE_OPT*/(char *)"p" /*, SOME_OTHER_OPT, ...             */, NULL };
   188         SEPTFILE_OPT,
       
   189         BACKUP_OPT    /* option to generate function to backup and restore internal PLC state */
       
   190         /*, SOME_OTHER_OPT, YET_ANOTHER_OPT */};
       
   191   char *const token[] = {
       
   192         /*       LINE_OPT*/(char *)"l",
       
   193         /*   SEPTFILE_OPT*/(char *)"p",
       
   194         /*     BACKUP_OPT*/(char *)"b",
       
   195         /* SOME_OTHER_OPT, ...             */
       
   196         NULL };
   189   /* unfortunately, the above commented out syntax for array initialization is valid in C, but not in C++ */
   197   /* unfortunately, the above commented out syntax for array initialization is valid in C, but not in C++ */
   190   
   198   
   191   char *subopts = options;
   199   char *subopts = options;
   192   char *value;
   200   char *value;
   193   int opt;
   201   int opt;
   194 
   202 
   195   while (*subopts != '\0') {
   203   while (*subopts != '\0') {
   196     switch (getsubopt(&subopts, token, &value)) {
   204     switch (getsubopt(&subopts, token, &value)) {
   197       case     LINE_OPT: generate_line_directives__  = 1; break;
   205       case     LINE_OPT: generate_line_directives__            = 1; break;
   198       case SEPTFILE_OPT: generate_pou_filepairs__    = 1; break;
   206       case SEPTFILE_OPT: generate_pou_filepairs__              = 1; break;
       
   207       case   BACKUP_OPT: generate_plc_state_backup_fuctions__  = 1; break;
   199       default          : fprintf(stderr, "Unrecognized option: -O %s\n", value); return -1; break;
   208       default          : fprintf(stderr, "Unrecognized option: -O %s\n", value); return -1; break;
   200      }
   209      }
   201   }     
   210   }     
   202   return 0;
   211   return 0;
   203 }
   212 }
   205 
   214 
   206 void stage4_print_options(void) {
   215 void stage4_print_options(void) {
   207   printf("          (options must be separated by commas. Example: 'l,w,x')\n"); 
   216   printf("          (options must be separated by commas. Example: 'l,w,x')\n"); 
   208   printf("      l : insert '#line' directives in generated C code.\n"); 
   217   printf("      l : insert '#line' directives in generated C code.\n"); 
   209   printf("      p : place each POU in a separate pair of files (<pou_name>.c, <pou_name>.h).\n"); 
   218   printf("      p : place each POU in a separate pair of files (<pou_name>.c, <pou_name>.h).\n"); 
       
   219   printf("      b : generate functions to backup and restore internal PLC state.\n"); 
   210 }
   220 }
   211 #else /* not __unix__ */
   221 #else /* not __unix__ */
   212 /* getsubopt isn't supported with mingw, 
   222 /* getsubopt isn't supported with mingw, 
   213  *  then stage4 options aren't available on windows*/
   223  *  then stage4 options aren't available on windows*/
   214 void stage4_print_options(void) {}
   224 void stage4_print_options(void) {}