output: Factor common code for command name tracking out of all drivers.
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 30 Nov 2018 05:04:55 +0000 (21:04 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 25 Dec 2018 19:30:28 +0000 (11:30 -0800)
This reduces redundancy.

src/output/ascii.c
src/output/cairo.c
src/output/csv.c
src/output/driver-provider.h
src/output/driver.c
src/output/html.c
src/output/journal.c
src/output/message-item.c
src/output/message-item.h
src/output/msglog.c
src/output/odt.c

index c6a8ee51de427636fe9d67d449a665d9f85eb326..bb776d0c2a0b3b9ef10b5aa2296cccbfed216222 100644 (file)
@@ -171,7 +171,6 @@ struct ascii_driver
     const ucs4_t *box;          /* Line & box drawing characters. */
 
     /* Internal state. */
-    char *command_name;
     struct file_handle *handle;
     FILE *file;                 /* Output file. */
     bool error;                 /* Output error? */
@@ -250,7 +249,6 @@ ascii_create (struct  file_handle *fh, enum settings_output_devices device_type,
                     NULL_SENTINEL);
   a->box = box == BOX_ASCII ? ascii_box_chars : unicode_box_chars;
 
-  a->command_name = NULL;
   a->file = NULL;
   a->error = false;
   a->lines = NULL;
@@ -334,7 +332,6 @@ ascii_destroy (struct output_driver *driver)
   if (a->file != NULL)
     fn_close (a->handle, a->file);
   fh_unref (a->handle);
-  free (a->command_name);
   free (a->chart_file_name);
   for (i = 0; i < a->allocated_lines; i++)
     u8_line_destroy (&a->lines[i]);
@@ -428,8 +425,6 @@ ascii_submit (struct output_driver *driver,
 {
   struct ascii_driver *a = ascii_driver_cast (driver);
 
-  output_driver_track_current_command (output_item, &a->command_name);
-
   if (a->error)
     return;
 
@@ -487,7 +482,7 @@ ascii_submit (struct output_driver *driver,
     {
       const struct message_item *message_item = to_message_item (output_item);
       const struct msg *msg = message_item_get_msg (message_item);
-      char *s = msg_to_string (msg, a->command_name);
+      char *s = msg_to_string (msg, message_item->command_name);
       ascii_output_text (a, s);
       free (s);
     }
index 3b87aeb64dbde07710167fe7e0fdb6ad1c9e0a5c..745c16e1eb0f4df915736d4f857dfe09e127c5e7 100644 (file)
@@ -524,7 +524,6 @@ xr_destroy (struct output_driver *driver)
       cairo_destroy (xr->cairo);
     }
 
-  free (xr->command_name);
   for (i = 0; i < XR_N_FONTS; i++)
     {
       struct xr_font *font = &xr->fonts[i];
@@ -552,8 +551,6 @@ xr_submit (struct output_driver *driver, const struct output_item *output_item)
 {
   struct xr_driver *xr = xr_driver_cast (driver);
 
-  output_driver_track_current_command (output_item, &xr->command_name);
-
   xr_driver_output_item (xr, output_item);
   while (xr_driver_need_new_page (xr))
     {
@@ -1588,7 +1585,7 @@ xr_render_message (struct xr_driver *xr,
   struct xr_render_fsm *fsm;
   char *s;
 
-  s = msg_to_string (msg, xr->command_name);
+  s = msg_to_string (msg, message_item->command_name);
   fsm = xr_create_text_renderer (xr, s);
   free (s);
 
index d463a3f6de3546b9be239c173c15b305673d1475..ef49759566d9b27829a2d8011d271c149e9ef6f9 100644 (file)
@@ -51,7 +51,6 @@ struct csv_driver
     bool captions;              /* Print table captions? */
 
     struct file_handle *handle;
-    char *command_name;         /* Current command. */
     FILE *file;                 /* Output file. */
     int n_items;                /* Number of items output so far. */
   };
@@ -177,8 +176,6 @@ csv_submit (struct output_driver *driver,
 {
   struct csv_driver *csv = csv_driver_cast (driver);
 
-  output_driver_track_current_command (output_item, &csv->command_name);
-
   if (is_table_item (output_item))
     {
       struct table_item *table_item = to_table_item (output_item);
@@ -322,7 +319,7 @@ csv_submit (struct output_driver *driver,
     {
       const struct message_item *message_item = to_message_item (output_item);
       const struct msg *msg = message_item_get_msg (message_item);
-      char *s = msg_to_string (msg, csv->command_name);
+      char *s = msg_to_string (msg, message_item->command_name);
       csv_put_separator (csv);
       csv_output_field (csv, s);
       free (s);
index aa70bb454adc691a830d0164b5d77dbe15158977..31503f2175a1ee57c481c8cd3293d02f6bd9fda4 100644 (file)
@@ -73,9 +73,7 @@ struct output_driver_class
     void (*flush) (struct output_driver *driver);
   };
 
-/* Useful for output driver implementation. */
-void output_driver_track_current_command (const struct output_item *, char **);
-\f
+
 /* An abstract way for the output subsystem to create an output driver. */
 struct output_driver_factory
   {
index bb5aa3a8b9e2ec7cf0256dd44e96a7f3a40dee95..653aa8d9c30031e86d271ce6d7734738aad9024e 100644 (file)
@@ -48,6 +48,7 @@ struct output_engine
   {
     struct llx_list drivers;       /* Contains "struct output_driver"s. */
     struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */
+    char *command_name;            /* Name of command being processed. */
   };
 
 static const struct output_driver_factory *factories[];
@@ -75,6 +76,7 @@ output_engine_push (void)
   e = &engine_stack[n_stack++];
   llx_init (&e->drivers);
   ds_init_empty (&e->deferred_syntax);
+  e->command_name = NULL;
 }
 
 void
@@ -90,6 +92,7 @@ output_engine_pop (void)
       output_driver_destroy (d);
     }
   ds_destroy (&e->deferred_syntax);
+  free (e->command_name);
 }
 
 void
@@ -174,6 +177,33 @@ output_submit (struct output_item *item)
     }
 
   flush_deferred_syntax (e);
+
+  if (is_text_item (item))
+    {
+      const struct text_item *text_item = to_text_item (item);
+      const char *text = text_item_get_text (text_item);
+      enum text_item_type type = text_item_get_type (text_item);
+
+      if (type == TEXT_ITEM_COMMAND_OPEN)
+        {
+          free (e->command_name);
+          e->command_name = xstrdup (text);
+        }
+      else if (type == TEXT_ITEM_COMMAND_CLOSE)
+        {
+          free (e->command_name);
+          e->command_name = NULL;
+        }
+    }
+  else if (is_message_item (item))
+    {
+      struct message_item *message_item = to_message_item (item);
+      free (message_item->command_name);
+      message_item->command_name = (e->command_name
+                                    ? xstrdup (e->command_name)
+                                    : NULL);
+    }
+
   output_submit__ (e, item);
 }
 
@@ -262,31 +292,6 @@ output_driver_is_registered (const struct output_driver *driver)
   return output_driver_get_engine (driver) != NULL;
 }
 \f
-/* Useful functions for output driver implementation. */
-
-void
-output_driver_track_current_command (const struct output_item *output_item,
-                                     char **command_namep)
-{
-  if (is_text_item (output_item))
-    {
-      const struct text_item *item = to_text_item (output_item);
-      const char *text = text_item_get_text (item);
-      enum text_item_type type = text_item_get_type (item);
-
-      if (type == TEXT_ITEM_COMMAND_OPEN)
-        {
-          free (*command_namep);
-          *command_namep = xstrdup (text);
-        }
-      else if (type == TEXT_ITEM_COMMAND_CLOSE)
-        {
-          free (*command_namep);
-          *command_namep = NULL;
-        }
-    }
-}
-\f
 extern const struct output_driver_factory txt_driver_factory;
 extern const struct output_driver_factory list_driver_factory;
 extern const struct output_driver_factory html_driver_factory;
index 6e5181d7da0340134942749643f05d7cede98c3b..e79cd8afea34180dfa279dc2ff6adfc0f98aa38f 100644 (file)
@@ -55,7 +55,6 @@ struct html_driver
     struct file_handle *handle;
     char *chart_file_name;
 
-    char *command_name;
     FILE *file;
     size_t chart_cnt;
 
@@ -221,7 +220,6 @@ html_destroy (struct output_driver *driver)
     }
   free (html->chart_file_name);
   fh_unref (html->handle);
-  free (html->command_name);
   free (html);
 }
 
@@ -231,8 +229,6 @@ html_submit (struct output_driver *driver,
 {
   struct html_driver *html = html_driver_cast (driver);
 
-  output_driver_track_current_command (output_item, &html->command_name);
-
   if (is_table_item (output_item))
     {
       struct table_item *table_item = to_table_item (output_item);
@@ -320,7 +316,7 @@ html_submit (struct output_driver *driver,
     {
       const struct message_item *message_item = to_message_item (output_item);
       const struct msg *msg = message_item_get_msg (message_item);
-      char *s = msg_to_string (msg, html->command_name);
+      char *s = msg_to_string (msg, message_item->command_name);
       print_title_tag (html->file, "P", s);
       free (s);
     }
index d4ad5a75fbefcb3480cf208982208efe417b6591..5ace0db2164f08206dd9daf6df96da83263e44b9 100644 (file)
@@ -41,7 +41,6 @@ struct journal_driver
   {
     struct output_driver driver;
     FILE *file;
-    char *command_name;
 
     /* Name of journal file. */
     char *file_name;
@@ -80,10 +79,7 @@ journal_destroy (struct output_driver *driver)
   struct journal_driver *j = journal_driver_cast (driver);
 
   if ( !j->destroyed)
-    {
-      journal_close ();
-      free (j->command_name);
-    }
+    journal_close ();
 
   j->destroyed = true;
 }
@@ -107,8 +103,6 @@ journal_submit (struct output_driver *driver, const struct output_item *item)
 {
   struct journal_driver *j = journal_driver_cast (driver);
 
-  output_driver_track_current_command (item, &j->command_name);
-
   if (is_text_item (item))
     {
       const struct text_item *text_item = to_text_item (item);
@@ -121,7 +115,7 @@ journal_submit (struct output_driver *driver, const struct output_item *item)
     {
       const struct message_item *message_item = to_message_item (item);
       const struct msg *msg = message_item_get_msg (message_item);
-      char *s = msg_to_string (msg, j->command_name);
+      char *s = msg_to_string (msg, message_item->command_name);
       journal_output (j, s);
       free (s);
     }
@@ -145,7 +139,6 @@ journal_init (void)
   output_driver_init (&journal.driver, &journal_class, "journal",
                      SETTINGS_DEVICE_UNFILTERED);
   journal.file = NULL;
-  journal.command_name = NULL;
 
   /* Register journal driver. */
   output_driver_register (&journal.driver);
index feb1d32c02eed54e13cb66eba6c869c429447f2b..a44784fe2eac99b7b8291290366682fdcc83367e 100644 (file)
@@ -34,6 +34,7 @@ message_item_create (const struct msg *msg)
   item = xmalloc (sizeof *msg);
   output_item_init (&item->output_item, &message_item_class);
   item->msg = msg_dup (msg);
+  item->command_name = NULL;
 
   return item;
 }
@@ -49,6 +50,7 @@ message_item_destroy (struct output_item *output_item)
 {
   struct message_item *item = to_message_item (output_item);
   msg_destroy (item->msg);
+  free (item->command_name);
   free (item);
 }
 
index 88450f1adee4cc91276e649853794eaab767fe95..8f0befe350b91a3f8bb17d10b626cc13678e8646 100644 (file)
@@ -36,6 +36,7 @@ struct message_item
   {
     struct output_item output_item;
     struct msg *msg;
+    char *command_name;
   };
 
 struct message_item *message_item_create (const struct msg *);
index 0d0329571045daa79b48190e3a22b1600250f9c7..9b606b4f18f1bde7cfdb478eac1bb3985334c7cb 100644 (file)
@@ -42,7 +42,6 @@ struct msglog_driver
     struct output_driver driver;
     FILE *file;
     struct file_handle *handle;
-    char *command_name;
   };
 
 static const struct output_driver_class msglog_class;
@@ -78,7 +77,6 @@ msglog_create (const char *file_name)
   ml->handle = handle;
   output_driver_init (&ml->driver, &msglog_class, file_name, type);
   ml->file = file;
-  ml->command_name = NULL;
 
   output_driver_register (&ml->driver);
 
@@ -91,7 +89,6 @@ msglog_destroy (struct output_driver *driver)
   struct msglog_driver *ml = msglog_driver_cast (driver);
 
   fn_close (ml->handle, ml->file);
-  free (ml->command_name);
   fh_unref (ml->handle);
   free (ml);
 }
@@ -101,13 +98,11 @@ msglog_submit (struct output_driver *driver, const struct output_item *item)
 {
   struct msglog_driver *ml = msglog_driver_cast (driver);
 
-  output_driver_track_current_command (item, &ml->command_name);
-
   if (is_message_item (item))
     {
       const struct message_item *message_item = to_message_item (item);
       const struct msg *msg = message_item_get_msg (message_item);
-      char *s = msg_to_string (msg, ml->command_name);
+      char *s = msg_to_string (msg, message_item->command_name);
       fprintf (ml->file, "%s\n", s);
       free (s);
     }
index 63c10054f9411528a49e63f79ec8e3362af68ba4..1d11594a152aeadcbace51f18855b1da02cdb373 100644 (file)
@@ -70,9 +70,6 @@ struct odt_driver
   /* Number of tables so far. */
   int table_num;
 
-  /* Name of current command. */
-  char *command_name;
-
   /* Number of footnotes so far. */
   int n_footnotes;
 };
@@ -387,7 +384,6 @@ odt_destroy (struct output_driver *driver)
     }
 
   free (odt->file_name);
-  free (odt->command_name);
   free (odt);
 }
 
@@ -587,8 +583,6 @@ odt_submit (struct output_driver *driver,
 {
   struct odt_driver *odt = odt_driver_cast (driver);
 
-  output_driver_track_current_command (output_item, &odt->command_name);
-
   if (is_table_item (output_item))
     write_table (odt, to_table_item (output_item));
   else if (is_text_item (output_item))
@@ -602,7 +596,7 @@ odt_submit (struct output_driver *driver,
     {
       const struct message_item *message_item = to_message_item (output_item);
       const struct msg *msg = message_item_get_msg (message_item);
-      char *s = msg_to_string (msg, odt->command_name);
+      char *s = msg_to_string (msg, message_item->command_name);
       odt_output_text (odt, s);
       free (s);
     }