1bdb0e9a0db6487467d51aa958452cf91ef13637
[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/message.h>
28 #include <libpspp/start-date.h>
29 #include <libpspp/version.h>
30 #include <output/output.h>
31
32 #include "xalloc.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 static int get_title (struct lexer *, const char *cmd, char **title);
38
39 int
40 cmd_title (struct lexer *lexer, struct dataset *ds UNUSED)
41 {
42   return get_title (lexer, "TITLE", &outp_title);
43 }
44
45 int
46 cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED)
47 {
48   return get_title (lexer, "SUBTITLE", &outp_subtitle);
49 }
50
51 static int
52 get_title (struct lexer *lexer, const char *cmd, char **title)
53 {
54   int c;
55
56   c = lex_look_ahead (lexer);
57   if (c == '"' || c == '\'')
58     {
59       lex_get (lexer);
60       if (!lex_force_string (lexer))
61         return CMD_FAILURE;
62       if (*title)
63         free (*title);
64       *title = ds_xstrdup (lex_tokstr (lexer));
65       lex_get (lexer);
66       if (lex_token (lexer) != '.')
67         {
68           msg (SE, _("%s: `.' expected after string."), cmd);
69           return CMD_FAILURE;
70         }
71     }
72   else
73     {
74       char *cp;
75
76       if (*title)
77         free (*title);
78       *title = xstrdup (lex_rest_of_line (lexer));
79       lex_discard_line (lexer);
80       for (cp = *title; *cp; cp++)
81         *cp = toupper ((unsigned char) (*cp));
82     }
83   return CMD_SUCCESS;
84 }
85
86 /* Performs the FILE LABEL command. */
87 int
88 cmd_file_label (struct lexer *lexer, struct dataset *ds)
89 {
90   const char *label;
91
92   label = lex_rest_of_line (lexer);
93   lex_discard_line (lexer);
94   while (isspace ((unsigned char) *label))
95     label++;
96
97   dict_set_label (dataset_dict (ds), label);
98
99   return CMD_SUCCESS;
100 }
101
102 /* Add entry date line to DICT's documents. */
103 static void
104 add_document_trailer (struct dictionary *dict)
105 {
106   char buf[64];
107
108   sprintf (buf, _("   (Entered %s)"), get_start_date ());
109   dict_add_document_line (dict, buf);
110 }
111
112 /* Performs the DOCUMENT command. */
113 int
114 cmd_document (struct lexer *lexer, struct dataset *ds)
115 {
116   struct dictionary *dict = dataset_dict (ds);
117   struct string line = DS_EMPTY_INITIALIZER;
118   bool end_dot;
119
120   do
121     {
122       end_dot = lex_end_dot (lexer);
123       ds_assign_string (&line, lex_entire_line_ds (lexer));
124       if (end_dot)
125         ds_put_char (&line, '.');
126       dict_add_document_line (dict, ds_cstr (&line));
127
128       lex_discard_line (lexer);
129       lex_get_line (lexer);
130     }
131   while (!end_dot);
132
133   add_document_trailer (dict);
134   ds_destroy (&line);
135
136   return CMD_SUCCESS;
137 }
138
139 /* Performs the DROP DOCUMENTS command. */
140 int
141 cmd_drop_documents (struct lexer *lexer, struct dataset *ds)
142 {
143   dict_clear_documents (dataset_dict (ds));
144
145   return lex_end_of_command (lexer);
146 }
147
148
149 /* Performs the ADD DOCUMENTS command. */
150 int
151 cmd_add_documents (struct lexer *lexer, struct dataset *ds)
152 {
153   struct dictionary *dict = dataset_dict (ds);
154
155   if ( ! lex_force_string (lexer) )
156     return CMD_FAILURE;
157
158   while ( lex_is_string (lexer))
159     {
160       dict_add_document_line (dict, ds_cstr (lex_tokstr (lexer)));
161       lex_get (lexer);
162     }
163
164   add_document_trailer (dict);
165
166   return lex_end_of_command (lexer) ;
167 }