Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[pspp-builds.git] / src / language / utilities / title.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <ctype.h>
22 #include <stdlib.h>
23
24 #include <data/dictionary.h>
25 #include <data/procedure.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <libpspp/alloc.h>
30 #include <libpspp/message.h>
31 #include <libpspp/start-date.h>
32 #include <libpspp/version.h>
33 #include <output/output.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 static int get_title (struct lexer *, const char *cmd, char **title);
39
40 int
41 cmd_title (struct lexer *lexer, struct dataset *ds UNUSED)
42 {
43   return get_title (lexer, "TITLE", &outp_title);
44 }
45
46 int
47 cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED)
48 {
49   return get_title (lexer, "SUBTITLE", &outp_subtitle);
50 }
51
52 static int
53 get_title (struct lexer *lexer, const char *cmd, char **title)
54 {
55   int c;
56
57   c = lex_look_ahead (lexer);
58   if (c == '"' || c == '\'')
59     {
60       lex_get (lexer);
61       if (!lex_force_string (lexer))
62         return CMD_FAILURE;
63       if (*title)
64         free (*title);
65       *title = ds_xstrdup (lex_tokstr (lexer));
66       lex_get (lexer);
67       if (lex_token (lexer) != '.')
68         {
69           msg (SE, _("%s: `.' expected after string."), cmd);
70           return CMD_FAILURE;
71         }
72     }
73   else
74     {
75       char *cp;
76
77       if (*title)
78         free (*title);
79       *title = xstrdup (lex_rest_of_line (lexer, NULL));
80       lex_discard_line (lexer);
81       for (cp = *title; *cp; cp++)
82         *cp = toupper ((unsigned char) (*cp));
83     }
84   return CMD_SUCCESS;
85 }
86
87 /* Performs the FILE LABEL command. */
88 int
89 cmd_file_label (struct lexer *lexer, struct dataset *ds)
90 {
91   const char *label;
92
93   label = lex_rest_of_line (lexer, NULL);
94   lex_discard_line (lexer);
95   while (isspace ((unsigned char) *label))
96     label++;
97
98   dict_set_label (dataset_dict (ds), label);
99
100   return CMD_SUCCESS;
101 }
102
103 /* Add LINE as a line of document information to dictionary
104    indented by INDENT spaces. */
105 static void
106 add_document_line (struct dictionary *dict, const char *line, int indent)
107 {
108   const char *old_documents;
109   size_t old_len;
110   char *new_documents;
111
112   old_documents = dict_get_documents (dict);
113   old_len = old_documents != NULL ? strlen (old_documents) : 0;
114   new_documents = xmalloc (old_len + 81);
115
116   memcpy (new_documents, old_documents, old_len);
117   memset (new_documents + old_len, ' ', indent);
118   buf_copy_str_rpad (new_documents + old_len + indent, 80 - indent, line);
119   new_documents[old_len + 80] = '\0';
120
121   dict_set_documents (dict, new_documents);
122
123   free (new_documents);
124 }
125
126 /* Performs the DOCUMENT command. */
127 int
128 cmd_document (struct lexer *lexer, struct dataset *ds)
129 {
130   struct dictionary *dict = dataset_dict (ds);
131   /* Add a few header lines for reference. */
132   {
133     char buf[256];
134
135     if (dict && dict_get_documents (dict))
136       add_document_line (dict, "", 0);
137
138     sprintf (buf, _("Document entered %s by %s:"), get_start_date (), version);
139     add_document_line (dict, buf, 1);
140   }
141
142   for (;;)
143     {
144       int had_dot;
145       const char *orig_line;
146       char *copy_line;
147
148       orig_line = lex_rest_of_line (lexer, &had_dot);
149       lex_discard_line (lexer);
150       while (isspace ((unsigned char) *orig_line))
151         orig_line++;
152
153       copy_line = xmalloc (strlen (orig_line) + 2);
154       strcpy (copy_line, orig_line);
155       if (had_dot)
156         strcat (copy_line, ".");
157
158       add_document_line (dict, copy_line, 3);
159       free (copy_line);
160
161       lex_get_line (lexer);
162       if (had_dot)
163         break;
164     }
165
166   return CMD_SUCCESS;
167 }
168
169 /* Performs the DROP DOCUMENTS command. */
170 int
171 cmd_drop_documents (struct lexer *lexer, struct dataset *ds)
172 {
173   dict_set_documents (dataset_dict (ds), NULL);
174
175   return lex_end_of_command (lexer);
176 }