Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / language / utilities / title.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <stdlib.h>
21
22 #include <data/dictionary.h>
23 #include <data/procedure.h>
24 #include <data/variable.h>
25 #include <language/command.h>
26 #include <language/lexer/lexer.h>
27 #include <libpspp/alloc.h>
28 #include <libpspp/message.h>
29 #include <libpspp/start-date.h>
30 #include <libpspp/version.h>
31 #include <output/output.h>
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 static int get_title (struct lexer *, const char *cmd, char **title);
37
38 int
39 cmd_title (struct lexer *lexer, struct dataset *ds UNUSED)
40 {
41   return get_title (lexer, "TITLE", &outp_title);
42 }
43
44 int
45 cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED)
46 {
47   return get_title (lexer, "SUBTITLE", &outp_subtitle);
48 }
49
50 static int
51 get_title (struct lexer *lexer, const char *cmd, char **title)
52 {
53   int c;
54
55   c = lex_look_ahead (lexer);
56   if (c == '"' || c == '\'')
57     {
58       lex_get (lexer);
59       if (!lex_force_string (lexer))
60         return CMD_FAILURE;
61       if (*title)
62         free (*title);
63       *title = ds_xstrdup (lex_tokstr (lexer));
64       lex_get (lexer);
65       if (lex_token (lexer) != '.')
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 (lexer));
78       lex_discard_line (lexer);
79       for (cp = *title; *cp; cp++)
80         *cp = toupper ((unsigned char) (*cp));
81     }
82   return CMD_SUCCESS;
83 }
84
85 /* Performs the FILE LABEL command. */
86 int
87 cmd_file_label (struct lexer *lexer, struct dataset *ds)
88 {
89   const char *label;
90
91   label = lex_rest_of_line (lexer);
92   lex_discard_line (lexer);
93   while (isspace ((unsigned char) *label))
94     label++;
95
96   dict_set_label (dataset_dict (ds), label);
97
98   return CMD_SUCCESS;
99 }
100
101 /* Add entry date line to DICT's documents. */
102 static void
103 add_document_trailer (struct dictionary *dict)
104 {
105   char buf[64];
106
107   sprintf (buf, _("   (Entered %s)"), get_start_date ());
108   dict_add_document_line (dict, buf);
109 }
110
111 /* Performs the DOCUMENT command. */
112 int
113 cmd_document (struct lexer *lexer, struct dataset *ds)
114 {
115   struct dictionary *dict = dataset_dict (ds);
116   struct string line = DS_EMPTY_INITIALIZER;
117   bool end_dot;
118
119   do
120     {
121       end_dot = lex_end_dot (lexer);
122       ds_assign_string (&line, lex_entire_line_ds (lexer));
123       if (end_dot)
124         ds_put_char (&line, '.');
125       dict_add_document_line (dict, ds_cstr (&line));
126
127       lex_discard_line (lexer);
128       lex_get_line (lexer);
129     }
130   while (!end_dot);
131
132   add_document_trailer (dict);
133
134   return CMD_SUCCESS;
135 }
136
137 /* Performs the DROP DOCUMENTS command. */
138 int
139 cmd_drop_documents (struct lexer *lexer, struct dataset *ds)
140 {
141   dict_clear_documents (dataset_dict (ds));
142
143   return lex_end_of_command (lexer);
144 }
145
146
147 /* Performs the ADD DOCUMENTS command. */
148 int
149 cmd_add_documents (struct lexer *lexer, struct dataset *ds)
150 {
151   struct dictionary *dict = dataset_dict (ds);
152
153   if ( ! lex_force_string (lexer) )
154     return CMD_FAILURE;
155
156   while ( lex_is_string (lexer))
157     {
158       dict_add_document_line (dict, ds_cstr (lex_tokstr (lexer)));
159       lex_get (lexer);
160     }
161
162   add_document_trailer (dict);
163
164   return lex_end_of_command (lexer) ;
165 }