checkin of 0.3.0
[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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include "alloc.h"
24 #include "command.h"
25 #include "error.h"
26 #include "lexer.h"
27 #include "main.h"
28 #include "output.h"
29 #include "var.h"
30 #include "version.h"
31 #include "vfm.h"
32
33 #undef DEBUGGING
34 /*#define DEBUGGING 1 */
35 #include "debug-print.h"
36
37 static int get_title (const char *cmd, char **title);
38
39 int
40 cmd_title (void)
41 {
42   return get_title ("TITLE", &outp_title);
43 }
44
45 int
46 cmd_subtitle (void)
47 {
48   return get_title ("SUBTITLE", &outp_subtitle);
49 }
50
51 static int
52 get_title (const char *cmd, char **title)
53 {
54   int c;
55
56   c = lex_look_ahead ();
57   debug_printf ((_("%s before: %s\n"), cmd, *title ? *title : _("<none>")));
58   if (c == '"' || c == '\'')
59     {
60       lex_get ();
61       if (!lex_force_string ())
62         return CMD_FAILURE;
63       if (*title)
64         free (*title);
65       *title = xstrdup (ds_value (&tokstr));
66       lex_get ();
67       if (token != '.')
68         {
69           msg (SE, _("%s: `.' expected after string."), cmd);
70           return CMD_FAILURE;
71         }
72     }
73   else
74     {
75       char *cp;
76
77       if (*title)
78         free (*title);
79       *title = xstrdup (lex_rest_of_line (NULL));
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   char *label;
93
94   label = lex_rest_of_line (NULL);
95   while (isspace ((unsigned char) *label))
96     label++;
97
98   free (default_dict.label);
99   default_dict.label = xstrdup (label);
100   if (strlen (default_dict.label) > 60)
101     default_dict.label[60] = 0;
102   token = '.';
103
104   return CMD_SUCCESS;
105 }
106
107 /* Add LINE as a line of document information to default_dict,
108    indented by INDENT spaces. */
109 static void
110 add_document_line (const char *line, int indent)
111 {
112   char *doc;
113
114   default_dict.n_documents++;
115   default_dict.documents = xrealloc (default_dict.documents,
116                                      80 * default_dict.n_documents);
117   doc = &default_dict.documents[80 * (default_dict.n_documents - 1)];
118   memset (doc, ' ', indent);
119   st_bare_pad_copy (&doc[indent], line, 80 - indent);
120 }
121
122 /* Performs the DOCUMENT command. */
123 int
124 cmd_document (void)
125 {
126   /* Add a few header lines for reference. */
127   {
128     char buf[256];
129     struct tm *tmp = localtime (&last_vfm_invocation);
130
131     if (default_dict.n_documents)
132       add_document_line ("", 0);
133
134     sprintf (buf, _("Document entered %s %02d:%02d:%02d by %s (%s):"),
135              curdate, tmp->tm_hour, tmp->tm_min, tmp->tm_sec, version,
136              host_system);
137     add_document_line (buf, 1);
138   }
139
140   for (;;)
141     {
142       int had_dot;
143       char *line;
144
145       line = lex_rest_of_line (&had_dot);
146       while (isspace ((unsigned char) *line))
147         line++;
148
149       if (had_dot)
150         {
151           char *cp = strchr (line, 0);
152           *cp++ = '.';
153           *cp = 0;
154         }
155
156       add_document_line (line, 3);
157
158       lex_get_line ();
159       if (had_dot)
160         break;
161     }
162
163   token = '.';
164   return CMD_SUCCESS;
165 }
166
167 /* Performs the DROP DOCUMENTS command. */
168 int
169 cmd_drop_documents (void)
170 {
171   lex_match_id ("DROP");
172   lex_match_id ("DOCUMENTS");
173
174   free (default_dict.documents);
175   default_dict.documents = NULL;
176   default_dict.n_documents = 0;
177
178   return lex_end_of_command ();
179 }