Adopt use of gnulib for portability.
[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 "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 #include "debug-print.h"
38
39 static int get_title (const char *cmd, char **title);
40
41 int
42 cmd_title (void)
43 {
44   return get_title ("TITLE", &outp_title);
45 }
46
47 int
48 cmd_subtitle (void)
49 {
50   return get_title ("SUBTITLE", &outp_subtitle);
51 }
52
53 static int
54 get_title (const char *cmd, char **title)
55 {
56   int c;
57
58   c = lex_look_ahead ();
59   debug_printf ((_("%s before: %s\n"), cmd, *title ? *title : _("<none>")));
60   if (c == '"' || c == '\'')
61     {
62       lex_get ();
63       if (!lex_force_string ())
64         return CMD_FAILURE;
65       if (*title)
66         free (*title);
67       *title = xstrdup (ds_c_str (&tokstr));
68       lex_get ();
69       if (token != '.')
70         {
71           msg (SE, _("%s: `.' expected after string."), cmd);
72           return CMD_FAILURE;
73         }
74     }
75   else
76     {
77       char *cp;
78
79       if (*title)
80         free (*title);
81       *title = xstrdup (lex_rest_of_line (NULL));
82       lex_discard_line ();
83       for (cp = *title; *cp; cp++)
84         *cp = toupper ((unsigned char) (*cp));
85       token = '.';
86     }
87   debug_printf ((_("%s after: %s\n"), cmd, *title));
88   return CMD_SUCCESS;
89 }
90
91 /* Performs the FILE LABEL command. */
92 int
93 cmd_file_label (void)
94 {
95   const char *label;
96
97   label = lex_rest_of_line (NULL);
98   lex_discard_line ();
99   while (isspace ((unsigned char) *label))
100     label++;
101
102   dict_set_label (default_dict, label);
103   token = '.';
104
105   return CMD_SUCCESS;
106 }
107
108 /* Add LINE as a line of document information to default_dict,
109    indented by INDENT spaces. */
110 static void
111 add_document_line (const char *line, int indent)
112 {
113   const char *old_documents;
114   size_t old_len;
115   char *new_documents;
116
117   old_documents = dict_get_documents (default_dict);
118   old_len = old_documents != NULL ? strlen (old_documents) : 0;
119   new_documents = xmalloc (old_len + 81);
120
121   memcpy (new_documents, old_documents, old_len);
122   memset (new_documents + old_len, ' ', indent);
123   buf_copy_str_rpad (new_documents + old_len + indent, 80 - indent, line);
124   new_documents[old_len + 80] = '\0';
125
126   dict_set_documents (default_dict, new_documents);
127
128   free (new_documents);
129 }
130
131 /* Performs the DOCUMENT command. */
132 int
133 cmd_document (void)
134 {
135   /* Add a few header lines for reference. */
136   {
137     char buf[256];
138     struct tm *tmp = localtime (&last_vfm_invocation);
139
140     if (dict_get_documents (default_dict) != NULL)
141       add_document_line ("", 0);
142
143     sprintf (buf, _("Document entered %s %02d:%02d:%02d by %s (%s):"),
144              curdate, tmp->tm_hour, tmp->tm_min, tmp->tm_sec, version,
145              host_system);
146     add_document_line (buf, 1);
147   }
148
149   for (;;)
150     {
151       int had_dot;
152       const char *orig_line;
153       char *copy_line;
154
155       orig_line = lex_rest_of_line (&had_dot);
156       lex_discard_line ();
157       while (isspace ((unsigned char) *orig_line))
158         orig_line++;
159
160       copy_line = xmalloc (strlen (orig_line) + 2);
161       strcpy (copy_line, orig_line);
162       if (had_dot)
163         strcat (copy_line, ".");
164
165       add_document_line (copy_line, 3);
166       free (copy_line);
167
168       lex_get_line ();
169       if (had_dot)
170         break;
171     }
172
173   token = '.';
174   return CMD_SUCCESS;
175 }
176
177 /* Performs the DROP DOCUMENTS command. */
178 int
179 cmd_drop_documents (void)
180 {
181   dict_set_documents (default_dict, NULL);
182
183   return lex_end_of_command ();
184 }