1 /*
2  * $Id: info.c,v 1.8 2000/08/10 21:02:50 danny Exp $
3  *
4  * Copyright � 1993 Free Software Foundation, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this software; see the file COPYING.  If not, write to
18  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #ifdef	WITH_DMALLOC
26 #include <dmalloc.h>
27 #endif
28 
29 #include "global.h"
30 #include "info.h"
31 #include "hash.h"
32 #include "cmd.h"
33 #include <stdarg.h>
34 
35 struct hash_control * info_buffers;
36 
37 
38 void
init_info(void)39 init_info (void)
40 {
41   info_buffers = hash_new();
42 }
43 
44 struct info_buffer *
find_info(char * name)45 find_info (char * name)
46 {
47   return (struct info_buffer *)hash_find (info_buffers, name);
48 }
49 
50 
51 struct info_buffer *
find_or_make_info(char * name)52 find_or_make_info (char * name)
53 {
54   struct info_buffer * buf =
55     (struct info_buffer *)hash_find (info_buffers, name);
56   if (buf)
57     return buf;
58 
59   buf = ((struct info_buffer *)
60 	 ck_malloc (sizeof (struct info_buffer) + strlen(name) + 1));
61   buf->name = (char *)buf + sizeof (*buf);
62   strcpy (buf->name, name);
63   buf->len = 0;
64   buf->text = 0;
65   hash_insert (info_buffers, buf->name, buf);
66   return buf;
67 }
68 
69 void
clear_info(struct info_buffer * buf)70 clear_info (struct info_buffer * buf)
71 {
72   if (buf->text)
73     {
74       int x;
75       int stop = buf->len;
76       for (x = 0; x < stop; ++x)
77 	ck_free (buf->text[x]);
78       ck_free (buf->text);
79     }
80   buf->text = 0;
81   buf->len = 0;
82 }
83 
84 /* This appears to be a source of trouble in the help
85  * system.  Every time help is requested for non-standard
86  * instructions (macros and the like), Oleo dumps core.
87  * It would have been useful to see some sort of note here
88  * re what this is meant to do exactly ... ?
89  * --FB
90  */
91 
92 void
print_info(struct info_buffer * buf,char * format,...)93 print_info (struct info_buffer * buf, char * format, ...)
94 {
95   va_list ap;
96   char txt[1000];
97   int len;			/* Length of the new line */
98 
99   va_start (ap, format);
100   vsprintf (txt, format, ap);
101   va_end (ap);
102   len = strlen (txt);
103 
104   ++buf->len;			/* Number of lines in the buffer */
105   buf->text = (char **)ck_remalloc (buf->text, buf->len * sizeof (char *));
106   buf->text[buf->len - 1] = (char *)ck_malloc (len + 1);
107   bcopy (txt, buf->text[buf->len - 1], len + 1);
108 }
109 
110 
111 
112 
113 /* A generic buffer for the use informational commands like show-options */
114 static struct info_buffer * the_text_buf;
115 
116 void
io_text_start(void)117 io_text_start (void)
118 {
119   the_text_buf = find_or_make_info ("_text");
120   clear_info (the_text_buf);
121 }
122 
123 
124 
125 void
io_text_line(char * format,...)126 io_text_line (char * format, ...)
127 {
128   va_list ap;
129   char txt[1000];
130   int len;			/* Length of the new line */
131 
132   va_start (ap, format);
133   vsprintf (txt, format, ap);
134   va_end (ap);
135   len = strlen (txt);
136 
137   if (the_text_buf == NULL)
138 	return;
139 
140   ++the_text_buf->len;		/* Number of lines in the the_text_buf */
141   the_text_buf->text =
142     (char **)ck_remalloc (the_text_buf->text,
143 			  the_text_buf->len * sizeof (char *));
144   the_text_buf->text[the_text_buf->len - 1] = (char *)ck_malloc (len + 1);
145   bcopy (txt, the_text_buf->text[the_text_buf->len - 1], len + 1);
146 }
147 
148 void
io_text_finish(void)149 io_text_finish (void)
150 {
151   run_string_as_macro ("{view-info _text}");
152 }
153 
154