e8d95bf0e58a26c7cc02c578428f9bc4d01cb60f
[pspp-builds.git] / src / title.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include "alloc.h"
24 #include "command.h"
25 #include "dictionary.h"
26 #include "error.h"
27 #include "lexer.h"
28 #include "main.h"
29 #include "output.h"
30 #include "var.h"
31 #include "version.h"
32 #include "vfm.h"
33
34 #include "debug-print.h"
35
36 static int get_title (const char *cmd, char **title);
37
38 int
39 cmd_title (void)
40 {
41   return get_title ("TITLE", &outp_title);
42 }
43
44 int
45 cmd_subtitle (void)
46 {
47   return get_title ("SUBTITLE", &outp_subtitle);
48 }
49
50 static int
51 get_title (const char *cmd, char **title)
52 {
53   int c;
54
55   c = lex_look_ahead ();
56   debug_printf ((_("%s before: %s\n"), cmd, *title ? *title : _("<none>")));
57   if (c == '"' || c == '\'')
58     {
59       lex_get ();
60       if (!lex_force_string ())
61         return CMD_FAILURE;
62       if (*title)
63         free (*title);
64       *title = xstrdup (ds_c_str (&tokstr));
65       lex_get ();
66       if (token != '.')
67         {
68           msg (SE, _("%s: `.' expected after string."), cmd);
69           return CMD_FAILURE;
70         }
71     }
72   else
73     {
74       char *cp;
75
76       if (*title)
77         free (*title);
78       *title = xstrdup (lex_rest_of_line (NULL));
79       lex_discard_line ();
80       for (cp = *title; *cp; cp++)
81         *cp = toupper ((unsigned char) (*cp));
82       token = '.';
83     }
84   debug_printf ((_("%s after: %s\n"), cmd, *title));
85   return CMD_SUCCESS;
86 }
87
88 /* Performs the FILE LABEL command. */
89 int
90 cmd_file_label (void)
91 {
92   const char *label;
93
94   label = lex_rest_of_line (NULL);
95   lex_discard_line ();
96   while (isspace ((unsigned char) *label))
97     label++;
98
99   dict_set_label (default_dict, label);
100   token = '.';
101
102   return CMD_SUCCESS;
103 }
104
105 /* Add LINE as a line of document information to default_dict,
106    indented by INDENT spaces. */
107 static void
108 add_document_line (const char *line, int indent)
109 {
110   const char *old_documents;
111   size_t old_len;
112   char *new_documents;
113
114   old_documents = dict_get_documents (default_dict);
115   old_len = old_documents != NULL ? strlen (old_documents) : 0;
116   new_documents = xmalloc (old_len + 81);
117
118   memcpy (new_documents, old_documents, old_len);
119   memset (new_documents + old_len, ' ', indent);
120   buf_copy_str_rpad (new_documents + old_len + indent, 80 - indent, line);
121   new_documents[old_len + 80] = '\0';
122
123   dict_set_documents (default_dict, new_documents);
124
125   free (new_documents);
126 }
127
128 /* Performs the DOCUMENT command. */
129 int
130 cmd_document (void)
131 {
132   /* Add a few header lines for reference. */
133   {
134     char buf[256];
135     struct tm *tmp = localtime (&last_vfm_invocation);
136
137     if (dict_get_documents (default_dict) != NULL)
138       add_document_line ("", 0);
139
140     sprintf (buf, _("Document entered %s %02d:%02d:%02d by %s (%s):"),
141              curdate, tmp->tm_hour, tmp->tm_min, tmp->tm_sec, version,
142              host_system);
143     add_document_line (buf, 1);
144   }
145
146   for (;;)
147     {
148       int had_dot;
149       const char *orig_line;
150       char *copy_line;
151
152       orig_line = lex_rest_of_line (&had_dot);
153       lex_discard_line ();
154       while (isspace ((unsigned char) *orig_line))
155         orig_line++;
156
157       copy_line = xmalloc (strlen (orig_line) + 2);
158       strcpy (copy_line, orig_line);
159       if (had_dot)
160         strcat (copy_line, ".");
161
162       add_document_line (copy_line, 3);
163       free (copy_line);
164
165       lex_get_line ();
166       if (had_dot)
167         break;
168     }
169
170   token = '.';
171   return CMD_SUCCESS;
172 }
173
174 /* Performs the DROP DOCUMENTS command. */
175 int
176 cmd_drop_documents (void)
177 {
178   dict_set_documents (default_dict, NULL);
179
180   return lex_end_of_command ();
181 }