12 /***************************************************************************\
14 ** Function name: getopt() **
15 ** Author: Henry Spencer, UofT **
16 ** Coding date: 84/04/28 **
20 ** Parses argv[] for arguments. **
21 ** Works with Whitesmith's C compiler. **
23 ** Inputs - The number of arguments **
24 ** - The base address of the array of arguments **
25 ** - A string listing the valid options (':' indicates an **
26 ** argument to the preceding option is required, a ';' **
27 ** indicates an argument to the preceding option is optional) **
29 ** Outputs - Returns the next option character, **
30 ** '?' for non '-' arguments **
31 ** or ':' when there is no more arguments. **
33 ** Side Effects + The argument to an option is pointed to by 'optarg' **
35 *****************************************************************************
37 ** REVISION HISTORY: **
39 ** DATE NAME DESCRIPTION **
40 ** YY/MM/DD ------------------ ------------------------------------ **
41 ** 88/10/20 Janick Bergeron Returns '?' on unamed arguments **
42 ** returns '!' on unknown options **
43 ** and 'EOF' only when exhausted. **
44 ** 88/11/18 Janick Bergeron Return ':' when no more arguments **
45 ** 89/08/11 Janick Bergeron Optional optarg when ';' in optstring **
47 \***************************************************************************/
49 char *optarg; /* Global argument pointer. */
52 getopt(int argc, char **argv, char *optstring)
56 static int optind = 0;
57 static char *scan = NULL;
61 if (scan == NULL || *scan == '\0') {
68 optarg = place = argv[optind++];
69 if (place[0] != '-' || place[1] == '\0')
71 if (place[1] == '-' && place[2] == '\0')
77 place = strchr(optstring, c);
78 if (place == NULL || c == ':' || c == ';') {
80 (void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
84 if (*++place == ':') {
96 (void) fprintf(stderr, "%s: %c requires an argument\n",
100 optarg = argv[optind];
104 else if (*place == ';') {
114 if (optind >= argc || *argv[optind] == '-')
117 optarg = argv[optind];
127 print_datum(datum db)
132 for (i = 0; i < db.dsize; i++) {
133 if (isprint((unsigned char)db.dptr[i]))
137 putchar('0' + ((db.dptr[i] >> 6) & 0x07));
138 putchar('0' + ((db.dptr[i] >> 3) & 0x07));
139 putchar('0' + (db.dptr[i] & 0x07));
154 db.dptr = (char *) malloc(strlen(s) * sizeof(char));
156 oops("cannot get memory");
158 for (p = db.dptr; *s != '\0'; p++, db.dsize++, s++) {
168 else if (isdigit((unsigned char)*s)
169 && isdigit((unsigned char)*(s + 1))
170 && isdigit((unsigned char)*(s + 2)))
172 i = (*s++ - '0') << 6;
173 i |= (*s++ - '0') << 3;
196 buf = (char *) malloc((db.dsize + 1) * sizeof(char));
198 oops("cannot get memory");
199 for (p1 = buf, p2 = db.dptr; *p2 != '\0'; *p1++ = *p2++);
205 main(int argc, char **argv)
208 YOW, FETCH, STORE, DELETE, SCAN, REGEXP
216 int st_flag = DBM_INSERT;
225 while ((opt = getopt(argc, argv, "acdfFm:rstvx")) != ':') {
244 if (strcmp(optarg, "r") == 0)
246 else if (strcmp(optarg, "w") == 0)
248 else if (strcmp(optarg, "rw") == 0)
251 fprintf(stderr, "Invalid mode: \"%s\"\n", optarg);
256 st_flag = DBM_REPLACE;
275 comarg[argn++] = optarg;
277 fprintf(stderr, "Too many arguments.\n");
284 if (giveusage || what == YOW || argn < 1) {
285 fprintf(stderr, "Usage: %s database [-m r|w|rw] [-crtx] -a|-d|-f|-F|-s [key [content]]\n", argv[0]);
289 if ((db = dbm_open(comarg[0], flags, 0777)) == NULL) {
290 fprintf(stderr, "Error opening database \"%s\"\n", comarg[0]);
295 key = read_datum(comarg[1]);
297 content = read_datum(comarg[2]);
302 key = dbm_firstkey(db);
304 fprintf(stderr, "Error when fetching first key\n");
307 while (key.dptr != NULL) {
308 content = dbm_fetch(db, key);
310 fprintf(stderr, "Error when fetching ");
317 print_datum(content);
320 fprintf(stderr, "Error when fetching next key\n");
323 key = dbm_nextkey(db);
329 fprintf(stderr, "Missing regular expression.\n");
332 if (re_comp(comarg[1])) {
333 fprintf(stderr, "Invalid regular expression\n");
336 key = dbm_firstkey(db);
338 fprintf(stderr, "Error when fetching first key\n");
341 while (key.dptr != NULL) {
342 if (re_exec(key2s(key))) {
343 content = dbm_fetch(db, key);
345 fprintf(stderr, "Error when fetching ");
352 print_datum(content);
355 fprintf(stderr, "Error when fetching next key\n");
359 key = dbm_nextkey(db);
365 fprintf(stderr, "Missing fetch key.\n");
368 content = dbm_fetch(db, key);
370 fprintf(stderr, "Error when fetching ");
375 if (content.dptr == NULL) {
376 fprintf(stderr, "Cannot find ");
383 print_datum(content);
389 fprintf(stderr, "Missing delete key.\n");
392 if (dbm_delete(db, key) || dbm_error(db)) {
393 fprintf(stderr, "Error when deleting ");
400 printf(": DELETED\n");
406 fprintf(stderr, "Missing key and/or content.\n");
409 if (dbm_store(db, key, content, st_flag) || dbm_error(db)) {
410 fprintf(stderr, "Error when storing ");
418 print_datum(content);
428 fprintf(stderr, "Error closing database \"%s\"\n", comarg[0]);