| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "mew.h" |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
#include <ctype.h> |
| 16 |
#include <stdarg.h> |
| 17 |
|
| 18 |
#if !HAVE_MEMMOVE |
| 19 |
#define memmove(d, s, n) bcopy((s), (d), (n)) |
| 20 |
#endif |
| 21 |
|
| 22 |
char *warn_prog; |
| 23 |
|
| 24 |
public char * |
| 25 |
getprognm(char *filename) |
| 26 |
{ |
| 27 |
char *p, *q; |
| 28 |
|
| 29 |
if ((p = strrchr(filename, FILESEP)) != NULL) |
| 30 |
p++; |
| 31 |
else |
| 32 |
p = filename; |
| 33 |
|
| 34 |
for (q = p; *q; q++) { |
| 35 |
*q = tolower(*q & 0xff); |
| 36 |
} |
| 37 |
return p; |
| 38 |
} |
| 39 |
|
| 40 |
public void |
| 41 |
warning(const char *fmt, ...) |
| 42 |
{ |
| 43 |
va_list ap; |
| 44 |
|
| 45 |
if (warn_prog != NULL) |
| 46 |
fprintf(stderr, "%s: ", warn_prog); |
| 47 |
va_start(ap, fmt); |
| 48 |
if (fmt != NULL) |
| 49 |
vfprintf(stderr, fmt, ap); |
| 50 |
va_end(ap); |
| 51 |
fprintf(stderr, "\n"); |
| 52 |
} |
| 53 |
|
| 54 |
public void |
| 55 |
warn_exit(const char *fmt, ...) |
| 56 |
{ |
| 57 |
va_list ap; |
| 58 |
|
| 59 |
if (warn_prog != NULL) |
| 60 |
fprintf(stderr, "%s: ", warn_prog); |
| 61 |
va_start(ap, fmt); |
| 62 |
if (fmt != NULL) |
| 63 |
vfprintf(stderr, fmt, ap); |
| 64 |
va_end(ap); |
| 65 |
fprintf(stderr, "\n"); |
| 66 |
|
| 67 |
exit(EXIT_FAILURE); |
| 68 |
} |
| 69 |
|
| 70 |
#ifndef HAVE_SNPRINTF |
| 71 |
public int |
| 72 |
snprintf(char *buf, int len, const char *fmt, ...) |
| 73 |
{ |
| 74 |
int ret; |
| 75 |
va_list ap; |
| 76 |
|
| 77 |
va_start(ap, fmt); |
| 78 |
ret = vsprintf(buf, fmt, ap); |
| 79 |
va_end(ap); |
| 80 |
return ret; |
| 81 |
} |
| 82 |
#endif |
| 83 |
|
| 84 |
#ifndef HAVE_FGETLN |
| 85 |
private char* |
| 86 |
fgetln(FILE *fp, size_t *len) |
| 87 |
{ |
| 88 |
static size_t bufsize = 0; |
| 89 |
static char *buf = NULL; |
| 90 |
char tmp[BUFSIZ]; |
| 91 |
size_t readlen; |
| 92 |
|
| 93 |
if (buf == NULL) { |
| 94 |
bufsize = BUFSIZ; |
| 95 |
MALLOC(buf, bufsize); |
| 96 |
} |
| 97 |
|
| 98 |
*len = 0; |
| 99 |
while (1) { |
| 100 |
if (fgets(tmp, BUFSIZ, fp) == NULL) |
| 101 |
return NULL; |
| 102 |
readlen = strlen(tmp); |
| 103 |
|
| 104 |
if (*len + readlen > bufsize) { |
| 105 |
bufsize += BUFSIZ; |
| 106 |
REALLOC(buf, bufsize); |
| 107 |
} |
| 108 |
memmove(&buf[*len], tmp, readlen); |
| 109 |
*len += readlen; |
| 110 |
if (readlen < BUFSIZ - 1 || tmp[BUFSIZ - 2] == '\n') |
| 111 |
break; |
| 112 |
} |
| 113 |
return buf; |
| 114 |
} |
| 115 |
#endif |
| 116 |
|
| 117 |
char* |
| 118 |
Getline(FILE* fp) |
| 119 |
{ |
| 120 |
char *s, *t; |
| 121 |
size_t len; |
| 122 |
if ((t = fgetln(fp, &len)) == NULL) |
| 123 |
return NULL; |
| 124 |
MALLOC(s, len+1); |
| 125 |
memmove(s, t, len); |
| 126 |
s[len] = '\0'; |
| 127 |
return s; |
| 128 |
} |
| 129 |
|
| 130 |
int Optind = 1; |
| 131 |
char *Optarg = NULL; |
| 132 |
|
| 133 |
public int |
| 134 |
Getopt(int argc, char *argv[], const char *fmt) |
| 135 |
{ |
| 136 |
char *p, *q, c; |
| 137 |
|
| 138 |
Optarg = NULL; |
| 139 |
if (Optind >= argc) |
| 140 |
return EOF; |
| 141 |
p = argv[Optind]; |
| 142 |
|
| 143 |
if (*p++ != '-') |
| 144 |
return EOF; |
| 145 |
c = *p; |
| 146 |
if (c == NUL) |
| 147 |
return EOF; |
| 148 |
if (*(p + 1) != NUL) |
| 149 |
warn_exit("unknown long option '-%s'.", p); |
| 150 |
if ((q = strchr(fmt, c)) == NULL) |
| 151 |
warn_exit("unknown option '-%c'.", c); |
| 152 |
if (++q != NULL && *q == ':') { |
| 153 |
if (++Optind >= argc) |
| 154 |
warn_exit("no parameter for '-%c'.", c); |
| 155 |
Optarg = argv[Optind]; |
| 156 |
} |
| 157 |
Optind++; |
| 158 |
return c; |
| 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 |
|