Get rid of src/libpspp/debug-print.h and all its users. (There were
[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 #include <ctype.h>
22 #include <stdlib.h>
23 #include <libpspp/alloc.h>
24 #include <language/command.h>
25 #include <data/dictionary.h>
26 #include <libpspp/message.h>
27 #include <language/lexer/lexer.h>
28 #include <output/output.h>
29 #include <libpspp/start-date.h>
30 #include <data/variable.h>
31 #include <libpspp/version.h>
32 #include <procedure.h>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 static int get_title (const char *cmd, char **title);
38
39 int
40 cmd_title (void)
41 {
42   return get_title ("TITLE", &outp_title);
43 }
44
45 int
46 cmd_subtitle (void)
47 {
48   return get_title ("SUBTITLE", &outp_subtitle);
49 }
50
51 static int
52 get_title (const char *cmd, char **title)
53 {
54   int c;
55
56   c = lex_look_ahead ();
57   if (c == '"' || c == '\'')
58     {
59       lex_get ();
60       if (!lex_force_string ())
61         return CMD_FAILURE;
62       if (*title)
63         free (*title);
64       *title = xstrdup (ds_c_str (&tokstr));
65       lex_get ();
66       if (token != '.')
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 (NULL));
79       lex_discard_line ();
80       for (cp = *title; *cp; cp++)
81         *cp = toupper ((unsigned char) (*cp));
82       token = '.';
83     }
84   return CMD_SUCCESS;
85 }
86
87 /* Performs the FILE LABEL command. */
88 int
89 cmd_file_label (void)
90 {
91   const char *label;
92
93   label = lex_rest_of_line (NULL);
94   lex_discard_line ();
95   while (isspace ((unsigned char) *label))
96     label++;
97
98   dict_set_label (default_dict, label);
99   token = '.';
100
101   return CMD_SUCCESS;
102 }
103
104 /* Add LINE as a line of document information to default_dict,
105    indented by INDENT spaces. */
106 static void
107 add_document_line (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 (default_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 (default_dict, new_documents);
123
124   free (new_documents);
125 }
126
127 /* Performs the DOCUMENT command. */
128 int
129 cmd_document (void)
130 {
131   /* Add a few header lines for reference. */
132   {
133     char buf[256];
134
135     if (dict_get_documents (default_dict) != NULL)
136       add_document_line ("", 0);
137
138     sprintf (buf, _("Document entered %s by %s:"), get_start_date (), version);
139     add_document_line (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 (&had_dot);
149       lex_discard_line ();
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 (copy_line, 3);
159       free (copy_line);
160
161       lex_get_line ();
162       if (had_dot)
163         break;
164     }
165
166   token = '.';
167   return CMD_SUCCESS;
168 }
169
170 /* Performs the DROP DOCUMENTS command. */
171 int
172 cmd_drop_documents (void)
173 {
174   dict_set_documents (default_dict, NULL);
175
176   return lex_end_of_command ();
177 }