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