int main(int argc, char **argv) { dbc_t *dbc; char *filename = NULL; /* default: stdin */ int c; #if 0 extern int yydebug; yydebug = 1; #endif verbose_flag = 0; while (1) { static struct option long_options[] = { /* These options set a flag. */ {"verbose", no_argument, &verbose_flag, 1}, {"brief", no_argument, &verbose_flag, 0}, /* These options don't set a flag. We distinguish them by their indices. */ {"dbc", required_argument, 0, 'd'}, {"networks",no_argument, &networks_flag, 'N'}, {"nodes" ,no_argument, &nodes_flag, 'n'}, {"envvars" ,no_argument, &envvars_flag, 'e'}, {"messages",no_argument, &messages_flag, 'm'}, {"signals" ,no_argument, &signals_flag, 's'}, {"valtables" ,no_argument, &valtables_flag, 't'}, {"help", no_argument, NULL, 'h'}, {0, 0, 0, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; c = getopt_long (argc, argv, "d:Nnemst", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) break; switch (c) { case 0: /* If this option set a flag, do nothing else now. */ if (long_options[option_index].flag != 0) break; printf ("option %s", long_options[option_index].name); if (optarg) printf (" with arg %s", optarg); printf ("\n"); break; case 'd': filename = optarg; break; case 'N': networks_flag = 1; break; case 'n': nodes_flag = 1; break; case 'e': envvars_flag = 1; break; case 'm': messages_flag = 1; break; case 's': signals_flag = 1; break; case 't': valtables_flag = 1; break; case 'h': help(); exit(0); break; case '?': /* getopt_long already printed an error message. */ fprintf(stderr, "Typ `dbcls --help' for more information\n"); abort(); break; default: abort(); break; } } if(NULL != (dbc = dbc_read_file(filename))) { if(networks_flag) show_network(dbc); if(messages_flag) show_message_list(dbc->message_list); if(signals_flag) show_signals(dbc); if(nodes_flag) show_nodes(dbc); if(envvars_flag) show_envvars(dbc->envvar_list); if(valtables_flag) show_valtable_list(dbc->valtable_list); dbc_free(dbc); } return 0; }
int main(int argc, char *argv[], char *envp[]) { int i, pid, bytesread, ret; char buf[256]; /* The following are for blocking certain signals */ sigset_t intrmask, oldmask; /* The following variables are for the select() function */ fd_set readmask, writemask, exceptmask; struct timeval timeout; /* contains tv_sec and tv_usec member variables */ printf("\nCurrent Signal Mask: "); show_signals(); /* Block SIGCHLD and SIGQUIT by first zeroing out the masks, then adding * SIGCHLD and SIGQUIT to the intrmask and then calling sigprocmask to * block those signals */ sigemptyset(&intrmask); sigemptyset(&oldmask); sigaddset(&intrmask, SIGCHLD); sigaddset(&intrmask, SIGQUIT); sigprocmask(SIG_BLOCK, &intrmask, &oldmask); /* Create a signal handler for CTRL-C which is SIGINT */ signal(SIGINT, ctrlc_handler); printf("\nNew Signal Mask: "); show_signals(); /* Enter select() loop. This will wait for activity on specified file * descriptors or timeout if no activity is detected. select() watches for: * Read activity (readmask) * Write activity (writemask) * Exceptions (exceptmask) * select() is a blocking function. You will remain at the select() function * call until there is activity or the function times out. */ pid = getpid(); printf("\nProcess ID: %d\n", pid); printf("\nEnter a string at the prompt or wait for timeout.\n"); printf("Enter just a period on a line by itself to exit.\n\n"); printf("Try entering CTRL-C, CTRL-D and other signals at the prompt.\n"); printf("Also open a second terminal and try sending signals via kill, such as:\n"); printf(" kill -SIGCHLD %d\n", pid); printf(" kill -SIGQUIT %d\n", pid); printf(" kill -SIGINT %d\n", pid); printf(" kill -SIGHUP %d\n", pid); printf(" kill -SIGTERM %d\n", pid); printf("NOTE: Some of the above will terminate the program. Restart to try more.\n"); printf("\n"); while(1) { printf("enter a line> "); fflush(stdout); bzero(buf, sizeof(buf)); /* Clear out the buffer */ /* The first step to using select is to zero out all the file descriptors * in each mask */ FD_ZERO(&readmask); FD_ZERO(&writemask); FD_ZERO(&exceptmask); /* The second step is to set all the file descriptors you wish to watch * for each mask. Recall that the file descriptor for stdin is 0 and the * file descriptor for stdout is 1. For a socket, the file descriptor is * the integer returned by the socket() function */ FD_SET(0, &readmask); /* activiate stdin read */ FD_SET(0, &exceptmask); /* activiate stdin exception */ /* The third step is to set the timeout values. This is how long select() * will wait for activity before giving up and returning */ timeout.tv_sec = SELECT_TIMEOUT; /* seconds */ timeout.tv_usec = 0; /* microseconds */ /* Now we are ready to call select(). We will wait here until something * happens or the function times out */ ret = select(ULIMIT, &readmask, &writemask, &exceptmask, &timeout); /* Now process the return value from timeout to see what happened */ if(ret == 0) /* timeout */ { printf(" timeout!\n"); } else if(ret < 0) /* error or signal interupt */ { if(errno == EINTR) { printf("Signal interupted select!\n"); } else { printf("Exception on select(), exiting loop...\n"); break; } } /* Check if there was an exception on stdin */ else if(FD_ISSET(0, &exceptmask)) { printf("Exception on select(), exiting loop...\n"); break; } /* Check if there's data on stdin waiting to be read */ else if(FD_ISSET(0, &readmask)) { bytesread = read(0, buf, sizeof(buf)); /* Check for the period to end the connection and also convert any * newlines into null characters */ for(i = 0 ; i < (sizeof(buf) - 1) ; i++) { if( (buf[i] == '\n') || (buf[i] == '\0') ) break; } if(buf[i] == '\n') buf[i] = '\0'; /* get rid of newline */ if(buf[0] == '.') /* end of stream */ { printf("Exiting select() loop at user's request...\n"); break; /* Exit out of loop */ } else { printf("You typed: %s\n", buf); } } fflush(stdout); } if(ctrlc_hit == 0) { printf("Please rerun the program and remember to hit CTRL-C at least once.\n"); } return 0; }