FreeBSD/sbin/atm/atmconfig/main.c
/*
 * Function to print help. The help argument is in argv[0] here.
 */
Function to print help. The help argument is in argv[0] here.
Function to print help . The help argument is in argv [ 0 ] here .
static void
help_func(int argc, char *argv[])
{
	struct help_file hfile;
	struct help_pos match, last_match;
	const char *line;
	char key[100];
	int level;
	int i, has_sub_topics;
	memset(&hfile, 0, sizeof(hfile));
	memset(&match, 0, sizeof(match));
	memset(&last_match, 0, sizeof(last_match));
	if (argc == 0) {
		if ((argv[0] = strdup("intro")) == NULL)
			err(1, NULL);
		argc = 1;
	}
	optind = 0;
	match.pos = -1;
	last_match.pos = -1;
	for (;;) {
		if ((line = help_next_line(&hfile)) == NULL) {
			level = 999;
			goto stop;
		}
		if (line[0] != '^' || line[1] == '^')
			continue;
		if (sscanf(line + 1, "%d%99s", &level, key) != 2)
			errx(1, "error in help file '%s'", line);
		if (level < optind) {
  stop:
			if (match.pos == -1) {
				goto not_found;
			}
			help_file_seek(&hfile, &match);
			last_match = match;
			memset(&match, 0, sizeof(match));
			match.pos = -1;
			if (++optind >= argc)
				break;
		}
		if (level == optind) {
			if (substr(argv[optind], key)) {
				if (match.pos != -1) {
					printf("Ambiguous topic.");
					goto list_topics;
				}
				help_file_tell(&hfile, &match);
			}
		}
	}
	for (;;) {
		if ((line = help_next_line(&hfile)) == NULL)
			break;
		if (line[0] == '#')
			continue;
		if (line[0] == '^') {
			if (line[1] == '^')
				continue;
			break;
		}
		if (strncmp(line, "$MAIN", 5) == 0) {
			help_get_0topics(&hfile);
			continue;
		}
		printf("%s", line);
	}
	exit(0);
  not_found:
	printf("Topic not found.");
  list_topics:
	printf(" Use one of:\natmconfig help");
	for (i = 0; i < optind; i++)
		printf(" %s", argv[i]);
	printf(" [");
	if (last_match.pos == -1)
		help_file_rewind(&hfile);
	else
		help_file_seek(&hfile, &last_match);
	has_sub_topics = 0;
	while ((line = help_next_line(&hfile)) != NULL) {
		if (line[0] == '#' || line[0] != '^' || line[1] == '^')
			continue;
		if (sscanf(line + 1, "%d%99s", &level, key) != 2)
			errx(1, "error in help file '%s'", line);
		if (level < optind)
			break;
		if (level == optind) {
			has_sub_topics = 1;
			printf(" %s", key);
		}
	}
	printf(" ].");
	if (!has_sub_topics)
		printf(" No sub-topics found.");
	printf("\n");
	exit(1);
}
static void help_func ( int argc , char * argv [ ] ) { struct help_file hfile ; struct help_pos match , last_match ; const char * line ; char key [ 100 ] ; int level ; int i , has_sub_topics ; memset ( & hfile , 0 , sizeof ( hfile ) ) ; memset ( & match , 0 , sizeof ( match ) ) ; memset ( & last_match , 0 , sizeof ( last_match ) ) ; if ( argc == 0 ) { if ( ( argv [ 0 ] = strdup ( " " ) ) == NULL ) err ( 1 , NULL ) ; argc = 1 ; } optind = 0 ; match . pos = -1 ; last_match . pos = -1 ; for ( ; ; ) { if ( ( line = help_next_line ( & hfile ) ) == NULL ) { level = 999 ; goto stop ; } if ( line [ 0 ] != ' ' || line [ 1 ] == ' ' ) continue ; if ( sscanf ( line + 1 , " " , & level , key ) != 2 ) errx ( 1 , " " , line ) ; if ( level < optind ) { stop : if ( match . pos == -1 ) { goto not_found ; } help_file_seek ( & hfile , & match ) ; last_match = match ; memset ( & match , 0 , sizeof ( match ) ) ; match . pos = -1 ; if ( ++ optind >= argc ) break ; } if ( level == optind ) { if ( substr ( argv [ optind ] , key ) ) { if ( match . pos != -1 ) { printf ( " " ) ; goto list_topics ; } help_file_tell ( & hfile , & match ) ; } } } for ( ; ; ) { if ( ( line = help_next_line ( & hfile ) ) == NULL ) break ; if ( line [ 0 ] == ' ' ) continue ; if ( line [ 0 ] == ' ' ) { if ( line [ 1 ] == ' ' ) continue ; break ; } if ( strncmp ( line , " " , 5 ) == 0 ) { help_get_0topics ( & hfile ) ; continue ; } printf ( " " , line ) ; } exit ( 0 ) ; not_found : printf ( " " ) ; list_topics : printf ( " \n " ) ; for ( i = 0 ; i < optind ; i ++ ) printf ( " " , argv [ i ] ) ; printf ( " " ) ; if ( last_match . pos == -1 ) help_file_rewind ( & hfile ) ; else help_file_seek ( & hfile , & last_match ) ; has_sub_topics = 0 ; while ( ( line = help_next_line ( & hfile ) ) != NULL ) { if ( line [ 0 ] == ' ' || line [ 0 ] != ' ' || line [ 1 ] == ' ' ) continue ; if ( sscanf ( line + 1 , " " , & level , key ) != 2 ) errx ( 1 , " " , line ) ; if ( level < optind ) break ; if ( level == optind ) { has_sub_topics = 1 ; printf ( " " , key ) ; } } printf ( " " ) ; if ( ! has_sub_topics ) printf ( " " ) ; printf ( " \n " ) ; exit ( 1 ) ; }
Function to print help.
Function to print help .
/* only 'help' - show intro */ /* read next line */ /* EOF */ /* next higher level entry - stop this level */ /* not found */ /* go back to the match */ /* go to next key */ /* before breaking above we have seeked back to the matching point */ /* list all the keys at this level */ /* go back to start of help */