/***********************************************************************
Copyright ?2007 LGC Wireless, Inc. All rights reserved
File Name: 		snmp_mib_api.c
Description:	API for SNMP MIB parser
Version:		v9.0.0
Author:			Roy Jiang
Create Date:	2007-6-19

History:
2007-6-9	v9.0.0	Create
***********************************************************************/
#include <stdio.h>
#include <sys/stat.h>
#include "snmp_mib.h"
#include "snmp_mib_api.h"

const char *mib_dir = "./mib";
const char *index_file = "index";

extern struct tree *tree_head;
extern struct module *module_head;
extern void new_module(const char *name, const char *file);
extern struct tree *read_module(const char *name);
extern int get_token(FILE * fp, char *token, int maxtlen);
extern void print_subtree(FILE * f, struct tree *tree, int count);
extern struct tree *find_tree_node(const char *name, int modid);
extern void free_tree(struct tree *Tree);

//return 0 on success
int init_mib()
{    
	//open dir and check mib files in the dir
	{
		FILE           *ip, *fp;
	    char            tmpstr[300];
	    int             count = 0;
	    char            token[MAXTOKEN];
	    char 			newline;
	    struct stat     dir_stat, idx_stat;
	    char            tmpstr1[300];
	    
		printf("Scanning directory %s\n", mib_dir);

		snprintf(token, sizeof(token), "%s/%s", mib_dir, index_file);
	    token[ sizeof(token)-1 ] = 0;
	    if (stat(token, &idx_stat) == 0 && stat(mib_dir, &dir_stat) == 0) {
	        if ((ip = fopen(token, "r")) != NULL) {
	            while (fscanf(ip, "%256s%c", tmpstr, &newline) == 2) 
			    {
					/*
					 * If an overflow of the token or tmpstr buffers has been
					 * found log a message and break out of the while loop,
					 * thus the rest of the file tokens will be ignored.
					 */				 
					if (newline != '\n') {
						printf("add_mibdir: strings scanned in from %s/%s " \
						    "are too large.  count = %d\n ", mib_dir,
						    ".index", count);
						break;
				    }

				    snprintf(tmpstr1, sizeof(tmpstr1), "%s/%s", mib_dir, tmpstr);
		            tmpstr1[ sizeof(tmpstr1)-1 ] = 0;
		            fp = fopen(tmpstr1, "r");
				    if (fp == NULL) {
				    	printf("Can not open mib file: %s\n", tmpstr1);
				        continue;
				    }
				    get_token(fp, token, MAXTOKEN);
				    fclose(fp);
		            new_module(token, tmpstr1);
		            count++;
				}
				fclose(ip);
			} 
	        else
	            printf("Can't read index\n");
	    } 
	    else
	        printf("No index\n");
	}
	
	//read all mibs
	{
		struct module	*mp;
		for (mp = module_head; mp; mp = mp->next)
	        if (mp->no_imports == -1)
	            read_module(mp->name);
	}

#ifdef _TEST_
{
	//for debug
	// print_subtree(stdout, tree_head, 0);
}
#endif

    return 0;
}

struct tree *getLGCroot()
{
	return find_tree_node("lgcNS", -1);
}

void close_mib()
{
	free_tree(tree_head);
	return;
}

//test----------------------------------------------------------------------------
#ifdef _TEST_
int main(int argc, char *argv[])
{
	init_mib();

    return 0;
}
#endif
//end of test---------------------------------------------------------------------

