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