Reworked compatibility computation to take program version into account.
[gedcom-parse.git] / gedcom / gedcom.y
index 395db23646f78b877c851977832f9e86ad3a25f3..0564018e888f004ec6f70c99ece3ab97f543aaf1 100644 (file)
@@ -157,13 +157,18 @@ int  gedcom_high_level_debug = 0;
 Gedcom_err_mech error_mechanism = IMMED_FAIL;
 Gedcom_val_struct val1;
 Gedcom_val_struct val2; 
-char line_item_buf[MAXGEDCLINELEN * UTF_FACTOR + 1];
-char *line_item_buf_ptr;
+
+void cleanup_line_item_buffer();
+struct safe_buffer line_item_buffer = { NULL, 0, NULL, 0,
+                                       cleanup_line_item_buffer };
 
 void cleanup_concat_buffer(); 
-struct safe_buffer concat_buffer  = { NULL, 0, cleanup_concat_buffer }; 
+struct safe_buffer concat_buffer = { NULL, 0, NULL, 0, cleanup_concat_buffer };
 
+void cleanup_usertag_buffer();
+struct safe_buffer usertag_buffer = { NULL, 0, NULL, 0,
+                                     cleanup_usertag_buffer};
 /* These are defined at the bottom of the file */ 
 void push_countarray(int level);
 void set_parenttag(const char* tag);
@@ -175,9 +180,6 @@ int  count_tag(int tag);
 int  check_occurrence(int tag);
 void clean_up(); 
 
-#define CLEAR_BUFFER(BUF)                                                     \
-     memset(BUF, 0, sizeof(BUF));
 #define HANDLE_ERROR                                                          \
      { if (error_mechanism == IMMED_FAIL) {                                   \
         clean_up(); YYABORT;                                                 \
@@ -212,7 +214,9 @@ void clean_up();
                      #TAG, parenttag);                                       \
          HANDLE_ERROR;                                                        \
        }                                                                      \
-     } 
+     }
+#define CHK_COND(TAG)                                                         \
+     check_occurrence(TAG_##TAG)
 #define POP                                                                   \
      { pop_countarray();                                                      \
        --count_level;                                                         \
@@ -446,22 +450,24 @@ head_sect    : OPEN DELIM TAG_HEAD
                                         NULL, GEDCOM_MAKE_NULL(val2));
                 START(HEAD, $1, $<ctxt>$) }
                head_subs
-               { if (compat_mode(C_FTREE)) {
-                  CHECK3(SOUR, GEDC, CHAR);
-                  compat_generate_submitter_link($<ctxt>4);
-                }
-                else if (compat_mode(C_LIFELINES)) {
-                  CHECK1(SOUR);
+               { if (compat_mode(C_NO_SUBMITTER) && ! CHK_COND(SUBM))
                   compat_generate_submitter_link($<ctxt>4);
+                else CHK(SUBM);
+
+                if (compat_mode(C_NO_GEDC) && ! CHK_COND(GEDC))
                   compat_generate_gedcom($<ctxt>4);
+                else CHK(GEDC);
+
+                if (compat_mode(C_NO_CHAR) && ! CHK_COND(CHAR)) {
                   if (compat_generate_char($<ctxt>4)) HANDLE_ERROR;
-                }
-                else
-                  CHECK4(SOUR, SUBM, GEDC, CHAR)
+                }
+                else CHK(CHAR);
+
+                CHECK1(SOUR);
               }
                CLOSE
                { end_record(REC_HEAD, $<ctxt>4, GEDCOM_MAKE_NULL(val1));
-                if (compat_mode(C_FTREE | C_LIFELINES))
+                if (compat_mode(C_NO_SUBMITTER))
                   compat_generate_submitter();
               }
              ;
@@ -473,7 +479,7 @@ head_subs    : /* empty */
 head_sub     : head_sour_sect  { OCCUR2(SOUR, 1, 1) }
              | head_dest_sect  { OCCUR2(DEST, 0, 1) }
              | head_date_sect  { OCCUR2(DATE, 0, 1) }
-             | head_time_sect  { if (!compat_mode(C_LIFELINES))
+             | head_time_sect  { if (!compat_mode(C_HEAD_TIME))
                                  INVALID_TAG("TIME");
                                 OCCUR2(TIME, 0, 1) }
              | head_subm_sect  { OCCUR2(SUBM, 1, 1) }
@@ -490,7 +496,7 @@ head_sub     : head_sour_sect  { OCCUR2(SOUR, 1, 1) }
 
 /* HEAD.SOUR */
 head_sour_sect : OPEN DELIM TAG_SOUR mand_line_item 
-                 { set_compatibility($4);
+                 { set_compatibility_program($4);
                   $<ctxt>$ = start_element(ELT_HEAD_SOUR, PARENT,
                                            $1, $3, $4,
                                            GEDCOM_MAKE_STRING(val1, $4));
@@ -499,7 +505,8 @@ head_sour_sect : OPEN DELIM TAG_SOUR mand_line_item
                  head_sour_subs
                  { CHECK0 }
                 CLOSE
-                 { end_element(ELT_HEAD_SOUR, PARENT, $<ctxt>5,
+                 { compute_compatibility();
+                  end_element(ELT_HEAD_SOUR, PARENT, $<ctxt>5,
                               GEDCOM_MAKE_NULL(val1)); }
                ;
 
@@ -515,7 +522,8 @@ head_sour_sub : head_sour_vers_sect  { OCCUR2(VERS, 0, 1) }
               ;
 
 head_sour_vers_sect : OPEN DELIM TAG_VERS mand_line_item
-                      { $<ctxt>$ = start_element(ELT_HEAD_SOUR_VERS, PARENT,
+                      { set_compatibility_version($4);
+                       $<ctxt>$ = start_element(ELT_HEAD_SOUR_VERS, PARENT,
                                                 $1, $3, $4,
                                                 GEDCOM_MAKE_STRING(val1, $4));
                        START(VERS, $1, $<ctxt>$)
@@ -670,7 +678,7 @@ head_date_time_sect : OPEN DELIM TAG_TIME mand_line_item
                      }
                     ;
 
-/* HEAD.TIME (Only for 'Lifelines' compatibility) */
+/* HEAD.TIME (Only for compatibility) */
 /* Just ignore the time... */
 head_time_sect : OPEN DELIM TAG_TIME opt_line_item CLOSE
                  { gedcom_warning(_("Header change time lost in the compatibility"));
@@ -1049,7 +1057,10 @@ indiv_rec   : OPEN DELIM POINTER DELIM TAG_INDI
               indi_subs
              { CHECK0 }
               CLOSE
-              { end_record(REC_INDI, $<ctxt>6, GEDCOM_MAKE_NULL(val1)); }
+              { end_record(REC_INDI, $<ctxt>6, GEDCOM_MAKE_NULL(val1));
+               if (compat_mode(C_NO_SLGC_FAMC))
+                 compat_generate_slgc_famc_fam();
+             }
             ;
 
 indi_subs   : /* empty */
@@ -1076,7 +1087,7 @@ indi_sub    : indi_resn_sect  { OCCUR2(RESN, 0, 1) }
             | indi_afn_sect  /* 0:M */
             | ident_struc_sub  /* 0:1 */
             | change_date_sub  /* 0:1 */
-           | ftree_addr_sect { if (!compat_mode(C_FTREE))
+           | indi_addr_sect { if (!compat_mode(C_INDI_ADDR))
                                  INVALID_TAG("ADDR");
                              }
            | no_std_sub
@@ -1214,23 +1225,23 @@ indi_afn_sect  : OPEN DELIM TAG_AFN mand_line_item
                 }
                ;
 
-/* INDI.ADDR (Only for 'ftree' compatibility) */
-ftree_addr_sect : OPEN DELIM TAG_ADDR opt_line_item
-                  { if (compat_mode(C_FTREE)) {
+/* INDI.ADDR (Only for compatibility) */
+indi_addr_sect : OPEN DELIM TAG_ADDR opt_line_item
+                  { if (compat_mode(C_INDI_ADDR)) {
                      Gedcom_ctxt par = compat_generate_resi_start(PARENT);
                      START(RESI, $1, par);
                      $<ctxt>$
                        = start_element(ELT_SUB_ADDR,
                                        par, $1 + 1, $3, $4,
                                        GEDCOM_MAKE_NULL_OR_STRING(val2, $4));
-                     START(ADDR, $1, $<ctxt>$);
+                     START(ADDR, $1 + 1, $<ctxt>$);
                    }
                  else { START(ADDR, $1, NULL) }
                  }
                   ftree_addr_subs
                   { CHECK0 }
                   CLOSE
-                  { if (compat_mode(C_FTREE)) {
+                  { if (compat_mode(C_INDI_ADDR)) {
                      Gedcom_ctxt par = PARENT;
                      end_element(ELT_SUB_ADDR, par, $<ctxt>5,
                                  GEDCOM_MAKE_NULL(val1));
@@ -1331,7 +1342,6 @@ obje_blob_sect : OPEN DELIM TAG_BLOB
                                            PARENT, $1, $3, NULL,
                                            GEDCOM_MAKE_NULL(val1));
                   reset_buffer(&concat_buffer);
-                  safe_buf_append(&concat_buffer, "");
                   START(BLOB, $1, $<ctxt>$)              
                 }
                  obje_blob_subs
@@ -1406,7 +1416,7 @@ note_rec    : OPEN DELIM POINTER DELIM TAG_NOTE note_line_item
             ;
 
 note_line_item : /* empty */
-                   { if (!compat_mode(C_FTREE)) {
+                   { if (!compat_mode(C_NOTE_NO_VALUE)) {
                       gedcom_error(_("Missing value")); YYERROR;
                     }
                     else {
@@ -1994,7 +2004,7 @@ addr_cont_sect : OPEN DELIM TAG_CONT mand_line_item
                  { $<ctxt>$ = start_element(ELT_SUB_ADDR_CONT,
                                            PARENT, $1, $3, $4, 
                                            GEDCOM_MAKE_STRING(val1, $4));
-                  safe_buf_append(&concat_buffer, "\n");
+                  SAFE_BUF_ADDCHAR(&concat_buffer, '\n');
                   safe_buf_append(&concat_buffer, $4);
                   START(CONT, $1, $<ctxt>$)               
                  }               
@@ -2272,7 +2282,7 @@ cont_sect : OPEN DELIM TAG_CONT mand_line_item
             { $<ctxt>$ = start_element(ELT_SUB_CONT,
                                       PARENT, $1, $3, $4, 
                                       GEDCOM_MAKE_STRING(val1, $4));
-             safe_buf_append(&concat_buffer, "\n");
+             SAFE_BUF_ADDCHAR(&concat_buffer, '\n');
              safe_buf_append(&concat_buffer, $4);
              START(CONT, $1, $<ctxt>$)  
             }  
@@ -2886,7 +2896,11 @@ lio_slgc_sect : OPEN DELIM TAG_SLGC
                  START(SLGC, $1, $<ctxt>$) 
                 }
                 lio_slgc_subs
-               { CHECK1(FAMC) }
+                { if (compat_mode(C_NO_SLGC_FAMC) && ! CHK_COND(FAMC))
+                   compat_generate_slgc_famc_link($<ctxt>4);
+                 else CHK(FAMC);
+                 CHECK0;
+               }
                 CLOSE 
                 { end_element(ELT_SUB_LIO_SLGC, PARENT, $<ctxt>4, 
                              GEDCOM_MAKE_NULL(val1));
@@ -3632,9 +3646,15 @@ no_std_rec  : user_rec /* 0:M */
 
 user_rec    : OPEN DELIM opt_xref USERTAG
               { if ($4.string[0] != '_') {
-                 gedcom_error(_("Undefined tag (and not a valid user tag): %s"),
-                              $4);
-                 YYERROR;
+                 if (compat_mode(C_551_TAGS)
+                     && compat_check_551_tag($4.string, &usertag_buffer)) {
+                   $4.string = get_buf_string(&usertag_buffer);
+                 }
+                 else {                  
+                   gedcom_error(_("Undefined tag (and not a valid user tag): %s"),
+                                $4);
+                   YYERROR;
+                 }
                }
              }
               opt_value
@@ -3656,9 +3676,15 @@ user_rec    : OPEN DELIM opt_xref USERTAG
             ;
 user_sect   : OPEN DELIM opt_xref USERTAG
               { if ($4.string[0] != '_') {
-                 gedcom_error(_("Undefined tag (and not a valid user tag): %s"),
-                              $4);
-                 YYERROR;
+                 if (compat_mode(C_551_TAGS)
+                     && compat_check_551_tag($4.string, &usertag_buffer)) {
+                   $4.string = get_buf_string(&usertag_buffer);
+                 }
+                 else {
+                   gedcom_error(_("Undefined tag (and not a valid user tag): %s"),
+                                $4);
+                   YYERROR;
+                 }
                }
              }
               opt_value
@@ -3698,9 +3724,8 @@ mand_pointer : /* empty */ { gedcom_error(_("Missing pointer")); YYERROR; }
              ;
 
 mand_line_item : /* empty */
-                 { if (compat_mode(C_LIFELINES)) {
-                     /* Lifelines tends to not care about mandatory values */
-                    gedcom_debug_print("==Val: ==");
+                 { if (compat_mode(C_NO_REQUIRED_VALUES)) {
+                     gedcom_debug_print("==Val: ==");
                     $$ = "";
                   }
                   else {
@@ -3717,38 +3742,36 @@ opt_line_item : /* empty */     { $$ = NULL; }
               ;
 
 line_item   : anychar  { size_t i;
-                        CLEAR_BUFFER(line_item_buf);
-                        line_item_buf_ptr = line_item_buf;
-                        /* The following also takes care of '@@' */
+                         reset_buffer(&line_item_buffer); 
+                        /* The following also takes care of '@@' */
                         if (!strncmp($1, "@@", 3))
-                          *line_item_buf_ptr++ = '@';
+                          SAFE_BUF_ADDCHAR(&line_item_buffer, '@')
                         else
                           for (i=0; i < strlen($1); i++)
-                            *line_item_buf_ptr++ = $1[i];
-                        $$ = line_item_buf;
+                            SAFE_BUF_ADDCHAR(&line_item_buffer, $1[i])
+                        $$ = get_buf_string(&line_item_buffer);
                        }
             | ESCAPE   { size_t i;
-                         CLEAR_BUFFER(line_item_buf);
-                        line_item_buf_ptr = line_item_buf;
-                        for (i=0; i < strlen($1); i++)
-                          *line_item_buf_ptr++ = $1[i];
-                        $$ = line_item_buf;
+                         reset_buffer(&line_item_buffer); 
+                        for (i=0; i < strlen($1); i++)
+                          SAFE_BUF_ADDCHAR(&line_item_buffer, $1[i])
+                        $$ = get_buf_string(&line_item_buffer);
                       }
             | line_item anychar
                   { size_t i;
                    /* The following also takes care of '@@' */
                    if (!strncmp($2, "@@", 3))
-                     *line_item_buf_ptr++ = '@';
+                     SAFE_BUF_ADDCHAR(&line_item_buffer, '@')
                    else
                      for (i=0; i < strlen($2); i++)
-                       *line_item_buf_ptr++ = $2[i];
-                   $$ = line_item_buf;
+                       SAFE_BUF_ADDCHAR(&line_item_buffer, $2[i])
+                   $$ = get_buf_string(&line_item_buffer);
                  }
             | line_item ESCAPE
                   { size_t i;
                    for (i=0; i < strlen($2); i++)
-                     *line_item_buf_ptr++ = $2[i];
-                   $$ = line_item_buf;
+                     SAFE_BUF_ADDCHAR(&line_item_buffer, $2[i])
+                   $$ = get_buf_string(&line_item_buffer);
                  }
             | line_item error anychar { HANDLE_ERROR; }
             | line_item error ESCAPE  { HANDLE_ERROR; }
@@ -4029,6 +4052,16 @@ void cleanup_concat_buffer()
   cleanup_buffer(&concat_buffer);
 }
 
+void cleanup_line_item_buffer()
+{
+  cleanup_buffer(&line_item_buffer);
+}
+
+void cleanup_usertag_buffer()
+{
+  cleanup_buffer(&usertag_buffer);
+}
+
 /* Enabling debug mode */
 /* level 0: no debugging */
 /* level 1: only internal */