- NOT_REACHED ();
- }
-}
-
-/* Writes the headers. Some of them might be vertical; most are
- probably horizontal. */
-static void
-write_header (struct outp_driver *d)
-{
- struct list_ext *prc = d->prc;
-
- if (!prc->header_rows)
- return;
-
- if (n_lines_remaining (d) < prc->header_rows + 1)
- {
- outp_eject_page (d);
- assert (n_lines_remaining (d) >= prc->header_rows + 1);
- }
-
- /* Design the header. */
- if (!prc->header)
- {
- size_t i;
- size_t x;
-
- /* Allocate, initialize header. */
- prc->header = xnmalloc (prc->header_rows, sizeof *prc->header);
- {
- int w = n_chars_width (d);
- for (i = 0; i < prc->header_rows; i++)
- {
- prc->header[i] = xmalloc (w + 1);
- memset (prc->header[i], ' ', w);
- }
- }
-
- /* Put in vertical names. */
- for (i = x = 0; i < prc->n_vertical; i++)
- {
- struct variable *v = cmd.v_variables[i];
- const char *name = var_get_name (v);
- size_t name_len = strlen (name);
- const struct fmt_spec *print = var_get_print_format (v);
- size_t j;
-
- memset (&prc->header[prc->header_rows - 1][x], '-', print->w);
- x += print->w - 1;
- for (j = 0; j < name_len; j++)
- prc->header[name_len - j - 1][x] = name[j];
- x += 2;
- }
-
- /* Put in horizontal names. */
- for (; i < cmd.n_variables; i++)
- {
- struct variable *v = cmd.v_variables[i];
- const char *name = var_get_name (v);
- size_t name_len = strlen (name);
- const struct fmt_spec *print = var_get_print_format (v);
-
- memset (&prc->header[prc->header_rows - 1][x], '-',
- MAX (print->w, (int) name_len));
- if ((int) name_len < print->w)
- x += print->w - name_len;
- memcpy (&prc->header[0][x], name, name_len);
- x += name_len + 1;
- }
-
- /* Add null bytes. */
- for (i = 0; i < prc->header_rows; i++)
- {
- for (x = n_chars_width (d); x >= 1; x--)
- if (prc->header[i][x - 1] != ' ')
- {
- prc->header[i][x] = 0;
- break;
- }
- assert (x);
- }
- }
-
- /* Write out the header, in back-to-front order except for the last line. */
- if (prc->header_rows >= 2)
- {
- size_t i;
-
- for (i = prc->header_rows - 1; i-- != 0; )
- write_line (d, prc->header[i]);
- }
- write_line (d, prc->header[prc->header_rows - 1]);
-}
-
-
-/* Frees up all the memory we've allocated. */
-static void
-clean_up (void)
-{
- struct outp_driver *d;
-
- for (d = outp_drivers (NULL); d; d = outp_drivers (d))
- if (d->class->special == 0)
- {
- struct list_ext *prc = d->prc;
- size_t i;
-
- if (prc->header)
- {
- for (i = 0; i < prc->header_rows; i++)
- free (prc->header[i]);
- free (prc->header);
- }
- free (prc);
- }
- else if (d->class == &html_class)
- {
- if (d->page_open)
- {
- struct html_driver_ext *x = d->ext;
-
- fputs ("</TABLE>\n", x->file);
- }
- }
- else
- NOT_REACHED ();
-
- free (cmd.v_variables);
-}
-
-/* Writes string STRING at the current position. If the text would
- fall off the side of the page, then advance to the next line,
- indenting by amount INDENT. */
-static void
-write_varname (struct outp_driver *d, char *string, int indent)
-{
- struct outp_text text;
- int width;
-
- if (d->cp_x + outp_string_width (d, string, OUTP_FIXED) > d->width)
- {
- d->cp_y += d->font_height;
- if (d->cp_y + d->font_height > d->length)
- outp_eject_page (d);
- d->cp_x = indent;
- }
-
- text.font = OUTP_FIXED;
- text.justification = OUTP_LEFT;
- text.string = ss_cstr (string);
- text.x = d->cp_x;
- text.y = d->cp_y;
- text.h = text.v = INT_MAX;
- d->class->text_draw (d, &text);
- d->class->text_metrics (d, &text, &width, NULL);
- d->cp_x += width;
-}
-
-/* When we can't fit all the values across the page, we write out all
- the variable names just once. This is where we do it. */
-static void
-write_fallback_headers (struct outp_driver *d)
-{
- const int max_width = n_chars_width(d) - 10;
-
- int index = 0;
- int width = 0;
- int line_number = 0;
-
- const char *Line = _("Line");
- char *leader = local_alloc (strlen (Line)
- + INT_STRLEN_BOUND (line_number) + 1 + 1);
-
- while (index < cmd.n_variables)
- {
- struct outp_text text;
- int leader_width;
-
- /* Ensure that there is enough room for a line of text. */
- if (d->cp_y + d->font_height > d->length)
- outp_eject_page (d);
-
- /* The leader is a string like `Line 1: '. Write the leader. */
- sprintf (leader, "%s %d:", Line, ++line_number);
- text.font = OUTP_FIXED;
- text.justification = OUTP_LEFT;
- text.string = ss_cstr (leader);
- text.x = 0;
- text.y = d->cp_y;
- text.h = text.v = INT_MAX;
- d->class->text_draw (d, &text);
- d->class->text_metrics (d, &text, &leader_width, NULL);
- d->cp_x = leader_width;
-
- goto entry;
- do
- {
- width++;
-
- entry:
- {
- int var_width = var_get_print_format (cmd.v_variables[index])->w;
- if (width + var_width > max_width && width != 0)
- {
- width = 0;
- d->cp_x = 0;
- d->cp_y += d->font_height;
- break;
- }
- width += var_width;
- }
-
- {
- char varname[LONG_NAME_LEN + 2];
- snprintf (varname, sizeof varname,
- " %s", var_get_name (cmd.v_variables[index]));
- write_varname (d, varname, leader_width);
- }
- }
- while (++index < cmd.n_variables);
-
- }
- d->cp_x = 0;
- d->cp_y += d->font_height;
-
- local_free (leader);
-}
-
-/* There are three possible layouts for the LIST procedure:
-
- 1. If the values and their variables' name fit across the page,
- then they are listed across the page in that way.
-
- 2. If the values can fit across the page, but not the variable
- names, then as many variable names as necessary are printed
- vertically to compensate.
-
- 3. If not even the values can fit across the page, the variable
- names are listed just once, at the beginning, in a compact format,
- and the values are listed with a variable name label at the
- beginning of each line for easier reference.