]> git.draconx.ca Git - gob-dx.git/blobdiff - src/main.c
Release 1.0.12
[gob-dx.git] / src / main.c
index 9e3d4bc58456ea8705b255ae5132e3e1914bb07a..526408b6f1921d6e13f710dc97ff8ff9f5ac37ae 100644 (file)
@@ -102,6 +102,14 @@ gboolean no_self_alias = FALSE;
 gboolean no_kill_underscores = FALSE;
 gboolean always_private_struct = FALSE;
 
+gboolean use_m4 = FALSE; /* preprocess sources with m4 */
+gboolean use_m4_clean = FALSE; /* preprocess sources with m4, no m4 flags */
+char *m4_commandline = NULL;
+#define M4_INCLUDE_DIR  PKGDATADIR "/m4"
+#define M4_BASE_FILENAME "gobm4.m4"
+#define M4_FLAGS "-P -s -I" M4_INCLUDE_DIR  " -DGOBM4_GOB_VERSION=" VERSION " " M4_BASE_FILENAME
+#define M4_COMMANDLINE "m4"
+
 int method_unique_id = 1;
 
 static void
@@ -298,6 +306,16 @@ make_method_gnu_aliases(Class *c)
                                out_printf(out, "#define %s() "
                                           "%s_%s()\n", m->id,
                                           funcbase, get_real_id(m->id));
+
+                       /* for compatibility with gob2 */
+                       if(m->args != NULL)
+                               out_printf(out, "#define self_%s(args...) "
+                                          "%s_%s(args)\n", m->id,
+                                          funcbase, get_real_id(m->id));
+                       else
+                               out_printf(out, "#define self_%s() "
+                                          "%s_%s()\n", m->id,
+                                          funcbase, get_real_id(m->id));
                }
        }
 }
@@ -332,6 +350,13 @@ make_method_nongnu_aliases(Class *c)
                        out_printf(out, " = %s_%s;\n", funcbase,
                                   get_real_id(m->id));
 
+                       /* for compatibility with gob2 */
+                       print_method(out, "static ", "(* const self_", "", ") ",
+                                    "", "",
+                                    m, FALSE, TRUE, FALSE);
+                       out_printf(out, " = %s_%s;\n", funcbase,
+                                  get_real_id(m->id));
+
                        local_made_aliases = TRUE;
                }
        }
@@ -2829,6 +2854,12 @@ print_help(void)
                "\t--always-private-struct Always create a private pointer "
                                          "in\n"
                "\t                        the object structure\n"
+               "\t--m4                    Preprocess source with m4. "
+                                         "Following args will\n"
+               "\t                        be passed to m4\n"
+               "\t--m4-dir                Print directory that will be "
+                                         "searched for m4\n"
+               "\t                        files\n"
                "\t--no-write,-n           Don't write output files, just "
                                          "check syntax\n"
                "\t--no-lines              Don't print '#line' to output\n"
@@ -2845,11 +2876,43 @@ parse_options(int argc, char *argv[])
        int i;
        int got_file = FALSE;
        int no_opts = FALSE;
+       int m4_opts = FALSE; /* if we are just passing on args to m4 */
 
        filename = NULL;
 
        for(i = 1 ; i < argc; i++) {
-               if(no_opts ||
+               if(m4_opts) {
+                       char *new_commandline;
+                       g_assert(m4_commandline!=NULL);
+
+                       /* check whether this one looks like the filename */
+                       if((!strcmp(argv[i],"-") || argv[i][0] != '-') 
+                          && !got_file) {
+                               const gchar *m4_flags=use_m4_clean?"":M4_FLAGS;
+                               filename = argv[i];
+                               got_file = TRUE;
+                               
+                               /* insert flags before the filename */
+                               new_commandline=g_strconcat(m4_commandline,
+                                                           " ",
+                                                           m4_flags, 
+                                                           " ",
+                                                           argv[i],
+                                                           NULL);
+                       }
+
+                       /* just an ordinary option */
+                       else                      
+                         new_commandline=g_strconcat(m4_commandline,
+                                                     " ",
+                                                     argv[i],
+                                                     NULL);
+
+                       /* free old commandline */
+                       g_free(m4_commandline);
+                       m4_commandline=new_commandline;
+
+               } else if(no_opts ||
                   argv[i][0] != '-') {
                        /*must be a file*/
                        if(got_file) {
@@ -2893,6 +2956,19 @@ parse_options(int argc, char *argv[])
                        no_kill_underscores = TRUE;
                } else if(strcmp(argv[i], "--always-private-struct")==0) {
                        always_private_struct = TRUE;
+               } else if(strcmp(argv[i], "--m4-dir")==0) {
+                       printf("%s\n",M4_INCLUDE_DIR);
+                       exit(0);
+               } else if(strcmp(argv[i], "--m4")==0) {
+                       use_m4 = TRUE;
+                       use_m4_clean=FALSE;
+                       m4_opts = TRUE;
+                       m4_commandline=g_strdup(M4_COMMANDLINE);
+               } else if(strcmp(argv[i], "--m4-clean")==0) {
+                       use_m4 = TRUE;
+                       use_m4_clean=TRUE;
+                       m4_opts = TRUE;
+                       m4_commandline=g_strdup(M4_COMMANDLINE);
                } else if(strcmp(argv[i], "--")==0) {
                        /*further arguments are files*/
                        no_opts = TRUE;
@@ -2926,6 +3002,19 @@ parse_options(int argc, char *argv[])
                        }
                }
        }
+
+#if 0
+       /* if we are using m4, and got no filename, append m4 flags now */
+       if(!got_file && use_m4 && !use_m4_clean) {
+               char *new_commandline;
+               new_commandline=g_strconcat(m4_commandline,
+                                           " ",
+                                           M4_FLAGS,
+                                           NULL);
+               g_free(m4_commandline);
+               m4_commandline=new_commandline;
+       }
+#endif
 }
 
 /* this is a somewhat ugly hack, but it appears to work */
@@ -2964,14 +3053,23 @@ main(int argc, char *argv[])
 {
        parse_options(argc, argv);
        
-       if(filename) {
+       if(use_m4) {
+               yyin = popen(m4_commandline, "r");
+               if(!yyin) {
+                       fprintf(stderr, "Error: can't open pipe from '%s'\n",
+                               m4_commandline);
+                       exit(1);
+               }
+       } else if(filename) {
                yyin = fopen(filename, "r");
                if(!yyin) {
                        fprintf(stderr, "Error: can't open file '%s'\n",
                                filename);
                        exit(1);
                }
-       } else
+       }
+
+       if(filename==NULL)
                filename = "stdin";
 
        /* This is where parsing is done */
@@ -2979,6 +3077,11 @@ main(int argc, char *argv[])
        if(yyparse() != 0)
                g_error("Parsing errors, quitting");
 
+       /* close input file */
+       if(use_m4) pclose(yyin);
+       else fclose(yyin);
+       yyin=stdin;
+
        if( ! class)
                error_print(GOB_ERROR, 0, " no class defined");