d7943126601f80881d62085173e3affd3d26fcfc
[pspp-builds.git] / src / language / utilities / 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
22 #include <ctype.h>
23 #include <stdlib.h>
24
25 #include <data/dictionary.h>
26 #include <data/procedure.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/lexer/lexer.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/message.h>
32 #include <libpspp/start-date.h>
33 #include <libpspp/version.h>
34 #include <output/output.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 static int get_title (struct lexer *, const char *cmd, char **title);
40
41 int
42 cmd_title (struct lexer *lexer, struct dataset *ds UNUSED)
43 {
44   return get_title (lexer, "TITLE", &outp_title);
45 }
46
47 int
48 cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED)
49 {
50   return get_title (lexer, "SUBTITLE", &outp_subtitle);
51 }
52
53 static int
54 get_title (struct lexer *lexer, const char *cmd, char **title)
55 {
56   int c;
57
58   c = lex_look_ahead (lexer);
59   if (c == '"' || c == '\'')
60     {
61       lex_get (lexer);
62       if (!lex_force_string (lexer))
63         return CMD_FAILURE;
64       if (*title)
65         free (*title);
66       *title = ds_xstrdup (lex_tokstr (lexer));
67       lex_get (lexer);
68       if (lex_token (lexer) != '.')
69         {
70           msg (SE, _("%s: `.' expected after string."), cmd);
71           return CMD_FAILURE;
72         }
73     }
74   else
75     {
76       char *cp;
77
78       if (*title)
79         free (*title);
80       *title = xstrdup (lex_rest_of_line (lexer, NULL));
81       lex_discard_line (lexer);
82       for (cp = *title; *cp; cp++)
83         *cp = toupper ((unsigned char) (*cp));
84     }
85   return CMD_SUCCESS;
86 }
87
88 /* Performs the FILE LABEL command. */
89 int
90 cmd_file_label (struct lexer *lexer, struct dataset *ds)
91 {
92   const char *label;
93
94   label = lex_rest_of_line (lexer, NULL);
95   lex_discard_line (lexer);
96   while (isspace ((unsigned char) *label))
97     label++;
98
99   dict_set_label (dataset_dict (ds), label);
100
101   return CMD_SUCCESS;
102 }
103
104 /* Add LINE as a line of document information to dictionary
105    indented by INDENT spaces. */
106 static void
107 add_document_line (struct dictionary *dict, 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 (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   buf_copy_str_rpad (new_documents + old_len + indent, 80 - indent, line);
120   new_documents[old_len + 80] = '\0';
121
122   dict_set_documents (dict, new_documents);
123
124   free (new_documents);
125 }
126
127 /* Performs the DOCUMENT command. */
128 int
129 cmd_document (struct lexer *lexer, struct dataset *ds)
130 {
131   struct dictionary *dict = dataset_dict (ds);
132   /* Add a few header lines for reference. */
133   {
134     char buf[256];
135
136     if (dict && dict_get_documents (dict))
137       add_document_line (dict, "", 0);
138
139     sprintf (buf, _("Document entered %s by %s:"), get_start_date (), version);
140     add_document_line (dict, buf, 1);
141   }
142
143   for (;;)
144     {
145       int had_dot;
146       const char *orig_line;
147       char *copy_line;
148
149       orig_line = lex_rest_of_line (lexer, &had_dot);
150       lex_discard_line (lexer);
151       while (isspace ((unsigned char) *orig_line))
152         orig_line++;
153
154       copy_line = xmalloc (strlen (orig_line) + 2);
155       strcpy (copy_line, orig_line);
156       if (had_dot)
157         strcat (copy_line, ".");
158
159       add_document_line (dict, copy_line, 3);
160       free (copy_line);
161
162       lex_get_line (lexer);
163       if (had_dot)
164         break;
165     }
166
167   return CMD_SUCCESS;
168 }
169
170 /* Performs the DROP DOCUMENTS command. */
171 int
172 cmd_drop_documents (struct lexer *lexer, struct dataset *ds)
173 {
174   dict_set_documents (dataset_dict (ds), NULL);
175
176   return lex_end_of_command (lexer);
177 }