parse_imports(FILE * fp)
{
    register int    type;
    char            token[MAXTOKEN];
    char            modbuf[256];
#define MAX_IMPORTS	256
    struct module_import import_list[MAX_IMPORTS];
    int             this_module;
    struct module  *mp;

    int             import_count = 0;   /* Total number of imported descriptors */
    int             i = 0, old_i;       /* index of first import from each module */

    type = get_token(fp, token, MAXTOKEN);

    /*
     * Parse the IMPORTS clause
     */
    while (type != SEMI && type != ENDOFFILE) {
        if (type == LABEL) {
            if (import_count == MAX_IMPORTS) {
                print_error("Too many imported symbols", token, type);
                do {
                    type = get_token(fp, token, MAXTOKEN);
                } while (type != SEMI && type != ENDOFFILE);
                return;
            }
            import_list[import_count++].label = strdup(token);
        } else if (type == FROM) {
            type = get_token(fp, token, MAXTOKEN);
            if (import_count == i) {    /* All imports are handled internally */
                type = get_token(fp, token, MAXTOKEN);
                continue;
            }
            this_module = which_module(token);

            for (old_i = i; i < import_count; ++i)
                import_list[i].modid = this_module;

            /*
             * Recursively read any pre-requisite modules
             */
            if (read_module_internal(token) == MODULE_NOT_FOUND) {
		int found = 0;
                for (; old_i < import_count; ++old_i) {
                    found += read_import_replacements(token, &import_list[old_i]);
                }
		if (!found)
		    print_module_not_found(token);
            }
        }
        type = get_token(fp, token, MAXTOKEN);
    }

    /* Initialize modid in case the module name was missing. */
    for (; i < import_count; ++i)
        import_list[i].modid = -1;

    /*
     * Save the import information
     *   in the global module table
     */
    for (mp = module_head; mp; mp = mp->next)
        if (mp->modid == current_module) {
            if (import_count == 0)
                return;
            if (mp->imports && (mp->imports != root_imports)) {
                /*
                 * this can happen if all modules are in one source file. 
                 */
                for (i = 0; i < mp->no_imports; ++i) {
                    DEBUGMSGTL(("parse-mibs",
                                "#### freeing Module %d '%s' %d\n",
                                mp->modid, mp->imports[i].label,
                                mp->imports[i].modid));
                    free((char *) mp->imports[i].label);
                }
                free((char *) mp->imports);
            }
            mp->imports = (struct module_import *)
                calloc(import_count, sizeof(struct module_import));
            if (mp->imports == NULL)
                return;
            for (i = 0; i < import_count; ++i) {
                mp->imports[i].label = import_list[i].label;
                mp->imports[i].modid = import_list[i].modid;
                DEBUGMSGTL(("parse-mibs",
                            "#### adding Module %d '%s' %d\n", mp->modid,
                            mp->imports[i].label, mp->imports[i].modid));
            }
            mp->no_imports = import_count;
            return;
        }

    /*
     * Shouldn't get this far
     */
    print_module_not_found(module_name(current_module, modbuf));
    return;
}
parse_imports(FILE * fp)
{
    register int    type;
    char            token[MAXTOKEN];
    char            modbuf[256];
#define MAX_IMPORTS	256
    struct module_import import_list[MAX_IMPORTS];
    int             this_module;
    struct module  *mp;

    int             import_count = 0;    
    int             i = 0, old_i;        

    type = get_token(fp, token, MAXTOKEN);

     
    while (type != SEMI && type != ENDOFFILE) {
        if (type == LABEL) {
            if (import_count == MAX_IMPORTS) {
                print_error("Too many imported symbols", token, type);
                do {
                    type = get_token(fp, token, MAXTOKEN);
                } while (type != SEMI && type != ENDOFFILE);
                return;
            }
            import_list[import_count++].label = strdup(token);
        } else if (type == FROM) {
            type = get_token(fp, token, MAXTOKEN);
            if (import_count == i) {     
                type = get_token(fp, token, MAXTOKEN);
                continue;
            }
            this_module = which_module(token);

            for (old_i = i; i < import_count; ++i)
                import_list[i].modid = this_module;

             
            if (read_module_internal(token) == MODULE_NOT_FOUND) {
		int found = 0;
                for (; old_i < import_count; ++old_i) {
                    found += read_import_replacements(token, &import_list[old_i]);
                }
		if (!found)
		    print_module_not_found(token);
            }
        }
        type = get_token(fp, token, MAXTOKEN);
    }

     
    for (; i < import_count; ++i)
        import_list[i].modid = -1;

     
    for (mp = module_head; mp; mp = mp->next)
        if (mp->modid == current_module) {
            if (import_count == 0)
                return;
            if (mp->imports && (mp->imports != root_imports)) {
                 
                for (i = 0; i < mp->no_imports; ++i) {
                    DEBUGMSGTL(("parse-mibs",
                                "#### freeing Module %d '%s' %d\n",
                                mp->modid, mp->imports[i].label,
                                mp->imports[i].modid));
                    free((char *) mp->imports[i].label);
                }
                free((char *) mp->imports);
            }
            mp->imports = (struct module_import *)
                calloc(import_count, sizeof(struct module_import));
            if (mp->imports == NULL)
                return;
            for (i = 0; i < import_count; ++i) {
                mp->imports[i].label = import_list[i].label;
                mp->imports[i].modid = import_list[i].modid;
                DEBUGMSGTL(("parse-mibs",
                            "#### adding Module %d '%s' %d\n", mp->modid,
                            mp->imports[i].label, mp->imports[i].modid));
            }
            mp->no_imports = import_count;
            return;
        }

     
    print_module_not_found(module_name(current_module, modbuf));
    return;
}