[go: up one dir, main page]

Menu

[014e0a]: / src / shmcat.c  Maximize  Restore  History

Download this file

273 lines (244 with data), 8.3 kB

  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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* shmcat: Dump Shared Memory Segments and more
* (C) 2012, 2014, 2016 by Stefan Gast
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* shmcat.c: shmcat main program
*/
#include "config.h"
#include <stdio.h>
#include <locale.h>
#include <unistd.h>
#include "translate.h"
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include "status.h"
#include "filedumper.h"
#include "printtext.h"
#include "shmdumper.h"
enum
{
EXITCODE_OK = 0,
EXITCODE_ERROR = 1,
EXITCODE_FATAL = 2,
EXITCODE_USAGE = 10
};
static void usage(const char *progname)
{
printf(_("Usage: %s [OPTION]... [OPERAND]...\n"), progname);
puts(_("Print files, shared memory segments and/or the given text\n"
"to standard output.\n"));
puts(_("Following OPTIONs are supported:\n"));
#ifdef HAVE_GETOPT_LONG
puts(_("-h, --help display this help and exit.\n"
"-? same as -h.\n"
"-S, --stop stop immediately if there is an error with an operand.\n"
"-V, --version output version information and exit.\n"));
#else /* !HAVE_GETOPT_LONG */
puts(_("-h display this help and exit.\n"
"-? same as -h.\n"
"-S stop immediately if there is an error with an operand.\n"
"-V output version information and exit.\n"));
#endif /* HAVE_GETOPT_LONG */
puts(_("An OPERAND may be one of:\n"));
#ifdef HAVE_GETOPT_LONG
puts(_("-f, --file=filename dump contents of the given file.\n"
"-i, --stdin dump standard input.\n"
"-M, --shmkey=key dump the System V shared memory segment with the\n"
" given key.\n"
"-m, --shmid=id dump the System V shared memory segment with the\n"
" given id.\n"
"-n, --newline add a line feed."));
#ifdef ENABLE_POSIX_SHM
puts(_("-p, --posix-shm=name dump the POSIX shared memory segment with the\n"
" given name."));
#endif /* ENABLE_POSIX_SHM */
puts(_("-t, --text=text print the given text.\n"));
#else /* !HAVE_GETOPT_LONG */
puts(_("-f filename dump contents of the given file.\n"
"-i dump standard input.\n"
"-M key dump the System V shared memory segment with the given key.\n"
"-m id dump the System V shared memory segment with the given id.\n"
"-n add a line feed."));
#ifdef ENABLE_POSIX_SHM
puts(_("-p name dump the POSIX shared memory segment with the given name."));
#endif /* ENABLE_POSIX_SHM */
puts(_("-t text print the given text.\n"));
#endif /* HAVE_GETOPT_LONG */
puts(_("If no operand is given, read standard input."));
}
static void show_version(void)
{
printf("%s (", PACKAGE_STRING);
#ifdef ENABLE_BIG_BUFFER
printf("big-buffer");
#else /* !ENABLE_BIG_BUFFER */
printf("small-buffer");
#endif /* ENABLE_BIG_BUFFER */
#ifdef ENABLE_POSIX_SHM
printf(", posix-shm");
#endif /* ENABLE_POSIX_SHM */
puts(")");
puts(_("Copyright (C) 2012, 2014, 2016 by Stefan Gast"));
puts(_("This is free software. You may redistribute copies of it under the terms of\n"
"the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"));
/* TRANSLATORS: The placeholder indicates the bug-reporting address
for this package. Please add _another line_ saying
"Report translation bugs to <...>\n" with the address for translation
bugs (typically your translation team's web or email address). */
printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
}
int main(int argc, char **argv)
{
#ifdef ENABLE_POSIX_SHM
const char *optstr = "hSVf:iM:m:np:t:";
#else
const char *optstr = "hSVf:iM:m:nt:";
#endif
#if defined HAVE_GETOPT_LONG && defined HAVE_GETOPT_H
static const struct option long_options[] =
{
{ "help", no_argument, NULL, 'h' },
{ "stop", no_argument, NULL, 'S' },
{ "version", no_argument, NULL, 'V' },
{ "file", required_argument, NULL, 'f' },
{ "stdin", no_argument, NULL, 'i' },
{ "shmkey", required_argument, NULL, 'M' },
{ "shmid", required_argument, NULL, 'm' },
{ "newline", no_argument, NULL, 'n' },
#ifdef ENABLE_POSIX_SHM
{ "posix-shm", required_argument, NULL, 'p' },
#endif
{ "text", required_argument, NULL, 't' },
{ NULL, 0, NULL, 0 }
};
#endif
int opt;
int stop = 0;
ShmcatStatus status = SHMCAT_NOTHING_DONE;
ShmcatStatus dumperStatus;
/* Initialize i18n support */
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
/* Do a first run of getopt/getopt_long to check the command line for
* validity and to handle help or version info options. */
opterr = 0;
optind = 1;
#if defined HAVE_GETOPT_LONG && defined HAVE_GETOPT_H
while((opt = getopt_long(argc, argv, optstr, long_options, NULL)) != -1)
#else
while((opt = getopt(argc, argv, optstr)) != -1)
#endif
{
switch(opt)
{
case 'h':
usage(argv[0]);
return EXITCODE_OK;
case 'S':
stop = 1;
break;
case 'V':
show_version();
return EXITCODE_OK;
case '?':
if(optopt == '?')
{
usage(argv[0]);
return EXITCODE_OK;
}
else
{
fprintf(stderr, _("%s: Wrong usage, try \"%s -h\" for more information!\n"), argv[0], argv[0]);
return EXITCODE_USAGE;
}
}
}
if(argc != optind)
{
fprintf(stderr, _("%s: Wrong usage, try \"%s -h\" for more information!\n"), argv[0], argv[0]);
return EXITCODE_USAGE;
}
/* Second getopt run does the actual work */
optind = 1;
#if defined HAVE_GETOPT_LONG && defined HAVE_GETOPT_H
while((opt = getopt_long(argc, argv, optstr, long_options, NULL)) != -1 &&
status != SHMCAT_PANIC && !(stop && status == SHMCAT_ERROR))
#else
while((opt = getopt(argc, argv, optstr)) != -1 &&
status != SHMCAT_PANIC && !(stop && status == SHMCAT_ERROR))
#endif
{
switch(opt)
{
case 'f':
dumperStatus = dumpFilename(optarg, argv[0]);
break;
case 'i':
dumperStatus = dumpFd(STDIN_FILENO, NULL, argv[0]);
break;
case 'M':
dumperStatus = dumpShmKey(optarg, argv[0]);
break;
case 'm':
dumperStatus = dumpShmId(optarg, argv[0]);
break;
case 'n':
dumperStatus = printText("\n", argv[0]);
break;
#ifdef ENABLE_POSIX_SHM
case 'p':
dumperStatus = dumpPosixShm(optarg, argv[0]);
break;
#endif
case 't':
dumperStatus = printText(optarg, argv[0]);
break;
case 'S':
/* Handled above in the first loop, no need to do anything here. */
break;
default:
/* Notify the user someting is wrong, but do not handle this
* as a fatal error. */
fprintf(stderr, _("%s: BUG: Unhandled command line option: %c\n"), argv[0], opt);
dumperStatus = SHMCAT_ERROR;
break;
}
/* The ShmcatStatus enum is ordered ascendingly, so we just have to
* increase our status variable if the current run of the loop led
* to a higher status value. */
if(status < dumperStatus)
status = dumperStatus;
}
/* If we haven't done anything until here, just dump stdin. */
if(status == SHMCAT_NOTHING_DONE)
status = dumpFd(STDIN_FILENO, NULL, argv[0]);
switch(status)
{
case SHMCAT_OK:
return EXITCODE_OK;
case SHMCAT_ERROR:
return EXITCODE_ERROR;
case SHMCAT_PANIC:
return EXITCODE_FATAL;
default:
fprintf(stderr, _("%s: BUG: One of the program routines returned an invalid value (%i)!\n"), argv[0], (int)status);
return EXITCODE_ERROR;
}
}