verbs.c
pdirt/drv/compiler/verbs.c · 4.4 KB
From the archive pDirt-1_tar.gz
Download the archive « Back to the archive contents
/******************************************************************************
** Module : verbs.c
** Author : Peter Eussen
** Description: Generates the files 'verbs' which is loaded on boot time, and
** verbs.h which is needed to compile the mud. The source file
** is verbs.src, which is in the same format as the old one
** <verb>[=<verb>]
** One entry per line. The [=<verb>] is used to specify aliases
** for one command.
****************************************************************************/
#include <stdlib.h>
#include <time.h>
#include "gen.h"
#include "lexer.h"
#include "hash.h"
#include "config.h"
#define MAX_VERBLEN 20
typedef struct _verb_record
{ int verbcode;
char name[MAX_VERBLEN];
struct _verb_record *next;
} verb_record;
int generate_verb_files(char *fn,char *dir)
{ char fullpathfn[120];
FILE *fp;
token_record *this_token;
verb_record *root = NULL;
verb_record *current = NULL;
verb_record *lastptr = NULL;
int num_verbs = 0, vcode = 0;
sprintf(fullpathfn,"%s/%s",dir,fn);
/* First we read in the verbs file
*/
if ((fp = xfopen(fullpathfn)) == NULL)
{ printf("File not found.\n");
return -1;
}
this_token = get_token(fp,'\n',False);
while (this_token->token_type >= 0)
{ current = memAlloc(verb_record,1,REC_CHAR);
strcpy(current->name,lowercase(this_token->the_string));
num_verbs++;
this_token = get_token(fp,'\n',False);
/* Check if next token is an equal sign, if so, then this command is an
* alias of another command and we need to find it. Otherwise we just
* assign a new number to this verb.
*/
if (this_token->token_type != T_EQUALS)
{ current->verbcode = ++vcode;
}
else
{ verb_record *tmpptr=root;
this_token = get_token(fp,'\n',False);
lowercase(this_token->the_string);
while (tmpptr != NULL && strcmp(this_token->the_string,tmpptr->name) != 0)
tmpptr = tmpptr->next;
if (tmpptr == NULL)
current->verbcode = ++vcode;
else
current->verbcode = tmpptr->verbcode;
this_token = get_token(fp,'\n',False);
}
current->next = NULL;
if (root == NULL)
root = lastptr = current;
else
{ lastptr->next = current;
lastptr = current;
}
}
fclose(fp);
if (this_token->token_type == -1)
{ FILE *verb_include;
FILE *verb_data;
time_t t = time(0);
verb_record *tmpptr=root;
if ((verb_data = safe_open("verbs",request_char_value("DATA_DIR"),"w")) == NULL)
{ printf("Error opening verbs file, terminating verb generation.\n");
return -1;
}
if ((verb_include = safe_open("verbs.h",request_char_value("INCLUDE_DIR"),"w")) == NULL)
{ printf("Error opening verbs.h file, terminating verb generation.\n");
return -1;
}
fprintf(verb_include,
"/****************************************************************************\n"
" ** VERBS INCLUDE - Automatically generated by mudcompiler with option -verbs\n"
" ** DO NOT make any changes here, as they will go away!!!\n"
" ** last updated: %s"
" ****************************************************************************/\n"
"#ifndef PDIRT_VERBS_H_\n"
"#define PDIRT_VERBS_H_\n\n",
ctime(&t));
fprintf(verb_data,"%d\n",num_verbs);
while (tmpptr != NULL)
{ fprintf(verb_data,"%s %d\n",tmpptr->name,tmpptr->verbcode);
uppercase(tmpptr->name);
fprintf(verb_include,"#define VERB_%-20.20s\t %d\n",tmpptr->name,tmpptr->verbcode);
tmpptr = tmpptr->next;
}
fprintf(verb_include,"\n#endif\n");
fclose(verb_data);
fclose(verb_include);
tmpptr = root;
while (tmpptr != NULL)
{ lastptr = tmpptr->next;
xfree(tmpptr,REC_CHAR);
tmpptr = lastptr;
}
printf("Parsed %d verbs.\n",num_verbs);
return 0;
}
return -1;
}