File: core_output.c

package info (click to toggle)
shntool 3.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,220 kB
  • ctags: 1,118
  • sloc: ansic: 8,515; sh: 3,761; makefile: 36
file content (193 lines) | stat: -rw-r--r-- 3,619 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
/*  core_output.c - generic output functions
 *  Copyright (C) 2000-2009  Jason Jordan <shnutils@freeshell.org>
 *
 *  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
 *  of the License, 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <string.h>
#include <stdarg.h>
#include "shntool.h"

CVSID("$Id: core_output.c,v 1.36 2009/03/11 17:18:01 jason Exp $")

static char msgbuf[BUF_SIZE];
static char errbuf[BUF_SIZE];

void st_output(char *msg, ...)
{
  va_list args;

  va_start(args,msg);

  st_vsnprintf(msgbuf,BUF_SIZE,msg,args);

  fprintf(stdout,"%s",msgbuf);

  va_end(args);
}

void st_info(char *msg, ...)
{
  va_list args;

  if (st_priv.suppress_stderr)
    return;

  va_start(args,msg);

  st_vsnprintf(errbuf,BUF_SIZE,msg,args);

  fprintf(stderr,"%s",errbuf);

  va_end(args);
}

static void print_prefix()
{
  if (st_priv.is_aliased || NULL == st_priv.progmode)
    fprintf(stderr,"%s: ",st_priv.progname);
  else
    fprintf(stderr,"%s [%s]: ",st_priv.progname,st_priv.progmode);
}

static void print_msgtype(char *msgtype,int line)
{
  int i;

  if (0 == line) {
    fprintf(stderr,"%s",msgtype);
  }
  else {
    for (i=0;i<strlen(msgtype);i++)
      fprintf(stderr," ");
  }
}

static void print_lines(char *msgtype,char *msg)
{
  int line = 0;
  char *head, *tail;

  head = tail = msgbuf;
  while (*head) {
    if ('\n' == *head) {
      *head = 0;

      print_prefix();
      print_msgtype(msgtype,line);
      fprintf(stderr,"%s\n",tail);

      tail = head + 1;
      line++;
    }
    head++;
  }

  print_prefix();
  print_msgtype(msgtype,line);
  fprintf(stderr,"%s\n",tail);
}

void st_error(char *msg, ...)
{
  va_list args;

  va_start(args,msg);

  st_vsnprintf(msgbuf,BUF_SIZE,msg,args);

  print_lines("error: ",msgbuf);

  va_end(args);

  exit(ST_EXIT_ERROR);
}

void st_help(char *msg, ...)
{
  va_list args;

  va_start(args,msg);

  st_vsnprintf(msgbuf,BUF_SIZE,msg,args);

  print_lines("error: ",msgbuf);

  va_end(args);

  print_prefix();
  fprintf(stderr,"\n");
  print_prefix();
  fprintf(stderr,"type '%s -h' for help\n",st_priv.fullprogname);

  exit(ST_EXIT_ERROR);
}

void st_warning(char *msg, ...)
{
  va_list args;

  if (st_priv.suppress_warnings || st_priv.suppress_stderr)
    return;

  va_start(args,msg);

  st_vsnprintf(msgbuf,BUF_SIZE,msg,args);

  print_lines("warning: ",msgbuf);

  va_end(args);
}

void st_debug_internal(int level,char *msg,va_list args)
{
  char debugprefix[16];

  if (level > st_priv.debug_level)
    return;

  st_vsnprintf(msgbuf,BUF_SIZE,msg,args);

  st_snprintf(debugprefix,16,"debug%d: ",level);

  print_lines(debugprefix,msgbuf);
}

void st_debug1(char *msg, ...)
{
  va_list args;

  va_start(args,msg);
  st_debug_internal(1,msg,args);
  va_end(args);
}

void st_debug2(char *msg, ...)
{
  va_list args;

  va_start(args,msg);
  st_debug_internal(2,msg,args);
  va_end(args);
}

void st_debug3(char *msg, ...)
{
  va_list args;

  va_start(args,msg);
  st_debug_internal(3,msg,args);
  va_end(args);
}