File: console.c

package info (click to toggle)
freeciv 1.9.0-2.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 11,004 kB
  • ctags: 6,284
  • sloc: ansic: 65,037; makefile: 634; sh: 418; sed: 93
file content (204 lines) | stat: -rw-r--r-- 6,132 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/********************************************************************** 
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "fcintl.h"
#include "log.h"

#include "civserver.h"

#include "console.h"

static int console_show_prompt=0;
static int console_prompt_is_showing=0;
static int console_rfcstyle=0;

/************************************************************************
Function to handle log messages.
This must match the log_callback_fn typedef signature.
************************************************************************/
static void con_handle_log(int level, char *message)
{
  if(console_rfcstyle) {
    con_write(C_LOG_BASE+level, "%s", message);
  } else {
    con_write(C_LOG_BASE+level, "%d: %s", level, message);
  }
}

/************************************************************************
Print the prompt if it is not the last thing printed.
************************************************************************/
static void con_update_prompt(void)
{
  if (console_prompt_is_showing || !console_show_prompt)
    return;
    
  con_dump(C_READY,"> ");
  con_flush();
  console_prompt_is_showing = 1;
}

/************************************************************************
Initialize logging via console.
************************************************************************/
void con_log_init(char *log_filename, int log_level)
{
  log_init(log_filename, log_level, (log_filename ? NULL : con_handle_log));
  logdebug_suppress_warning;
}

/************************************************************************
Write to console without line-break, don't print prompt.
************************************************************************/
int con_dump(int i, char *message, ...)
{
  static char buf[512];
  va_list args;
  
  va_start(args, message);
  vsprintf(buf, message, args);
  va_end(args);

  if(console_prompt_is_showing) {
    printf("\n");
  }
  if ((console_rfcstyle) && (i >= 0)) {
    printf("%.3d %s", i, buf);
  } else {
    printf("%s", buf);
  }
  console_prompt_is_showing = 0;
  return strlen(buf);
}

/************************************************************************
Write to console and add line-break, and show prompt if required.
************************************************************************/
void con_write(int i, char *message, ...)
{
  static char buf[512];
  va_list args;
  
  va_start(args, message);
  vsprintf(buf, message, args);
  va_end(args);

  con_puts(i, buf);
}

/************************************************************************
Write to console and add line-break, and show prompt if required.
Same as con_write, but without the format string stuff.
The real reason for this is because __attribute__ complained
with con_write(C_COMMENT,"") of "warning: zero-length format string";
this allows con_puts(C_COMMENT,"");
************************************************************************/
void con_puts(int i, char *str)
{
  if(console_prompt_is_showing) {
    printf("\n");
  }
  if ((console_rfcstyle) && (i >= 0)) {
    printf("%.3d %s\n", i, str);
  } else {
    printf("%s\n", str);
  }
  console_prompt_is_showing = 0;
  con_update_prompt();
}

/************************************************************************
For rfc-specific information only.
Adds line-break, and shows prompt if required.
************************************************************************/
void con_rfconly(int i, char *message, ...)
{
  static char buf[512];
  va_list args;
  
  va_start(args, message);
  vsprintf(buf, message, args);
  va_end(args);
  
  if ((console_rfcstyle) && (i >= 0))
    printf("%.3d %s\n", i, buf);
  console_prompt_is_showing = 0;
  con_update_prompt();
}

/************************************************************************
Ensure timely update. 
************************************************************************/
void con_flush(void)
{
  fflush(stdout);
}

/************************************************************************
Set style.
************************************************************************/
void con_set_style( int i )
{
  console_rfcstyle = i;
  if (console_rfcstyle) 
    con_puts(C_OK, _("Ok. RFC-style set."));
  else
    con_puts(C_OK, _("Ok. Standard style set."));
}

/************************************************************************
Returns rfc-style.
************************************************************************/
int con_get_style(void)
{
  return console_rfcstyle;
}

/************************************************************************
Make sure a prompt is printed, and re-printed after every message.
************************************************************************/
void con_prompt_on(void)
{
  static int first = 1;
  if (first) {
    con_puts(C_COMMENT, "");
    con_puts(C_COMMENT, _("Get a list of the available commands with 'help'."));
    first = 0;
  }
  console_show_prompt=1;
  con_update_prompt();
}

/************************************************************************
Do not print a prompt after log messages.
************************************************************************/
void con_prompt_off(void)
{
  console_show_prompt=0;
}

/************************************************************************
User pressed enter: will need a new prompt 
************************************************************************/
void con_prompt_enter(void)
{
  console_prompt_is_showing = 0;
}