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