11 /***************************************************************************\
13 ** Function name: getopt() **
14 ** Author: Henry Spencer, UofT **
15 ** Coding date: 84/04/28 **
19 ** Parses argv[] for arguments. **
20 ** Works with Whitesmith's C compiler. **
22 ** Inputs - The number of arguments **
23 ** - The base address of the array of arguments **
24 ** - A string listing the valid options (':' indicates an **
25 ** argument to the preceding option is required, a ';' **
26 ** indicates an argument to the preceding option is optional) **
28 ** Outputs - Returns the next option character, **
29 ** '?' for non '-' arguments **
30 ** or ':' when there is no more arguments. **
32 ** Side Effects + The argument to an option is pointed to by 'optarg' **
34 *****************************************************************************
36 ** REVISION HISTORY: **
38 ** DATE NAME DESCRIPTION **
39 ** YY/MM/DD ------------------ ------------------------------------ **
40 ** 88/10/20 Janick Bergeron Returns '?' on unamed arguments **
41 ** returns '!' on unknown options **
42 ** and 'EOF' only when exhausted. **
43 ** 88/11/18 Janick Bergeron Return ':' when no more arguments **
44 ** 89/08/11 Janick Bergeron Optional optarg when ';' in optstring **
46 \***************************************************************************/
48 char *optarg; /* Global argument pointer. */
55 getopt(int argc, char **argv, char *optstring)
60 static int optind = 0;
61 static char *scan = NULL;
65 if (scan == NULL || *scan == '\0') {
72 optarg = place = argv[optind++];
73 if (place[0] != '-' || place[1] == '\0')
75 if (place[1] == '-' && place[2] == '\0')
81 place = index(optstring, c);
82 if (place == NULL || c == ':' || c == ';') {
84 (void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
88 if (*++place == ':') {
100 (void) fprintf(stderr, "%s: %c requires an argument\n",
104 optarg = argv[optind];
108 else if (*place == ';') {
118 if (optind >= argc || *argv[optind] == '-')
121 optarg = argv[optind];
131 print_datum(datum db)
136 for (i = 0; i < db.dsize; i++) {
137 if (isprint((unsigned char)db.dptr[i]))
141 putchar('0' + ((db.dptr[i] >> 6) & 0x07));
142 putchar('0' + ((db.dptr[i] >> 3) & 0x07));
143 putchar('0' + (db.dptr[i] & 0x07));
158 db.dptr = (char *) malloc(strlen(s) * sizeof(char));
160 oops("cannot get memory");
162 for (p = db.dptr; *s != '\0'; p++, db.dsize++, s++) {
172 else if (isdigit((unsigned char)*s)
173 && isdigit((unsigned char)*(s + 1))
174 && isdigit((unsigned char)*(s + 2)))
176 i = (*s++ - '0') << 6;
177 i |= (*s++ - '0') << 3;
200 buf = (char *) malloc((db.dsize + 1) * sizeof(char));
202 oops("cannot get memory");
203 for (p1 = buf, p2 = db.dptr; *p2 != '\0'; *p1++ = *p2++);
209 main(int argc, char **argv)
212 YOW, FETCH, STORE, DELETE, SCAN, REGEXP
220 int st_flag = DBM_INSERT;
229 while ((opt = getopt(argc, argv, "acdfFm:rstvx")) != ':') {
248 if (strcmp(optarg, "r") == 0)
250 else if (strcmp(optarg, "w") == 0)
252 else if (strcmp(optarg, "rw") == 0)
255 fprintf(stderr, "Invalid mode: \"%s\"\n", optarg);
260 st_flag = DBM_REPLACE;
279 comarg[argn++] = optarg;
281 fprintf(stderr, "Too many arguments.\n");
288 if (giveusage || what == YOW || argn < 1) {
289 fprintf(stderr, "Usage: %s databse [-m r|w|rw] [-crtx] -a|-d|-f|-F|-s [key [content]]\n", argv[0]);
293 if ((db = dbm_open(comarg[0], flags, 0777)) == NULL) {
294 fprintf(stderr, "Error opening database \"%s\"\n", comarg[0]);
299 key = read_datum(comarg[1]);
301 content = read_datum(comarg[2]);
306 key = dbm_firstkey(db);
308 fprintf(stderr, "Error when fetching first key\n");
311 while (key.dptr != NULL) {
312 content = dbm_fetch(db, key);
314 fprintf(stderr, "Error when fetching ");
321 print_datum(content);
324 fprintf(stderr, "Error when fetching next key\n");
327 key = dbm_nextkey(db);
333 fprintf(stderr, "Missing regular expression.\n");
336 if (re_comp(comarg[1])) {
337 fprintf(stderr, "Invalid regular expression\n");
340 key = dbm_firstkey(db);
342 fprintf(stderr, "Error when fetching first key\n");
345 while (key.dptr != NULL) {
346 if (re_exec(key2s(key))) {
347 content = dbm_fetch(db, key);
349 fprintf(stderr, "Error when fetching ");
356 print_datum(content);
359 fprintf(stderr, "Error when fetching next key\n");
363 key = dbm_nextkey(db);
369 fprintf(stderr, "Missing fetch key.\n");
372 content = dbm_fetch(db, key);
374 fprintf(stderr, "Error when fetching ");
379 if (content.dptr == NULL) {
380 fprintf(stderr, "Cannot find ");
387 print_datum(content);
393 fprintf(stderr, "Missing delete key.\n");
396 if (dbm_delete(db, key) || dbm_error(db)) {
397 fprintf(stderr, "Error when deleting ");
404 printf(": DELETED\n");
410 fprintf(stderr, "Missing key and/or content.\n");
413 if (dbm_store(db, key, content, st_flag) || dbm_error(db)) {
414 fprintf(stderr, "Error when storing ");
422 print_datum(content);
432 fprintf(stderr, "Error closing database \"%s\"\n", comarg[0]);