📄 mkhfcts.c
字号:
/****************************************************************//* *//* Name: mkhfcts.c *//* *//* Project: NeuroBasic, utility program *//* *//* Survey: Creates a .c and a .h file as interface for *//* user functions on the host (host functions). *//* *//* Author: Urs Mueller *//* Electronics Laboratory, ETH Zuerich *//* Switzerland *//* *//* Created: September 12, 1994 *//* Modified: September 20, 1994 (um) *//* *//****************************************************************/#include <ctype.h>#include <stdlib.h>#include <stdio.h>#include <string.h>/* macrso *//**********/#define LINE_MAX 511 /* max. length of text lines */#define FCT_MAX 1024 /* maximum #host functions */#define TRUE 1#define FALSE 0/* type definitions *//********************//* global variables *//********************/char line_buf[LINE_MAX+1];char *fct_names[FCT_MAX];size_t nfunctions;static int is_space(char *pch, FILE *fpt)/*=====================================*//* Like library function isspace() but skips comments of the C source code.*/{ char c2; if (isspace(*pch)) return TRUE; if (*pch != '/') return FALSE; *pch = fgetc(fpt); if (*pch != '*') /* is it a comment? */ { /* no */ ungetc(*pch, fpt); return FALSE; } else { /* skip the comment */ *pch = fgetc(fpt); do { c2 = *pch; *pch = fgetc(fpt); } while ((*pch != EOF) && (c2 != '*' || *pch != '/')); *pch = ' '; /* replace comment by a real space */ return TRUE; }} /* end of is_space() */static void skip_space(char *pch, FILE *fpt)/*========================================*//* skip white spaces and comments */{ while (is_space(pch, fpt)) *pch = fgetc(fpt);} /* end of skip_space() */static void init_fct_list(void)/*===========================*/{ size_t i; for (i = 0; i < FCT_MAX; i++) fct_names[i] = NULL; nfunctions = 0;} /* end of init_fct_list() */static void release_fct_list()/*==========================*/{ size_t i; for (i = 0; i < FCT_MAX; i++) if (fct_names[i] != NULL) free(fct_names[i]); nfunctions = 0;} /* end of release_fct_list() */static char *str_upper(char *str)/*=============================*/{ char *s; for (s = str; *s != 0; s++) *s = toupper(*s); return str;} /* end of str_upper() */static void write_file_header(FILE *fpt)/*====================================*/{ fprintf(fpt, "/*\n"); fprintf(fpt, " This file was created by mkhobjs. "); fprintf(fpt, "Do not modify it!\n"); fprintf(fpt, "*/\n\n\n");} /* end of write_file_header() */static int scan_inputfile(char *filename)/*=====================================*/{ char ch; FILE *fpt; size_t lb; fpt = fopen(filename, "r"); if (fpt == NULL) /* no error message if file doesn't exist */ return EXIT_SUCCESS; ch = fgetc(fpt); while (ch != EOF) { /*===== looking for return type =====*/ lb = 0; skip_space(&ch, fpt); while (ch != EOF && !isspace(ch)) { if (lb < LINE_MAX) line_buf[lb++] = ch; ch = fgetc(fpt); } line_buf[lb] = 0; if (ch == EOF) break; if (strcmp(line_buf, "void") != 0) { printf("ERROR of mkhfcts: Return value must be \"void\" "); printf("in file \"%s\".\n", filename); return EXIT_FAILURE; } /*===== looking for function name =====*/ lb = 0; skip_space(&ch, fpt); while (ch != EOF && !isspace(ch) && ch != '(') { if (lb < LINE_MAX) line_buf[lb++] = ch; ch = fgetc(fpt); } line_buf[lb] = 0; skip_space(&ch, fpt); if (ch != '(') { printf("ERROR of mkhfcts: missing '(' "); printf("in file \"%s\".\n", filename); return EXIT_FAILURE; } if (nfunctions >= FCT_MAX) { printf("ERROR of mkhfcts: too many host functions. "); printf("(%ld allowed).\n", (long)FCT_MAX); return EXIT_FAILURE; } fct_names[nfunctions] = (char*)malloc(strlen(line_buf)); if (fct_names[nfunctions] == NULL) { printf("ERROR of mkhfcts: out of memory during scanning "); printf("file \"%s\".\n", filename); return EXIT_FAILURE; } strcpy(fct_names[nfunctions], line_buf); nfunctions++; /*===== looking for arguments =====*/ lb = 0; ch = fgetc(fpt); skip_space(&ch, fpt); while (ch != EOF && !isspace(ch) && ch != '(') { if (lb < LINE_MAX) line_buf[lb++] = ch; ch = fgetc(fpt); } line_buf[lb] = 0; if (strcmp(line_buf, "message_t") != 0) { printf("ERROR of mkhfcts: argument not of type \"message_t\" "); printf("in function \"%s()\" of file \"%s\".\n", line_buf, filename); return EXIT_FAILURE; } while (ch != ')' && ch != EOF) ch = fgetc(fpt); ch = fgetc(fpt); if (ch != ';') { printf("ERROR of mkhfcts: missing ')' or ';'"); printf("in file \"%s\".\n", filename); return EXIT_FAILURE; } ch = fgetc(fpt); } if (ferror(fpt)) { printf("ERROR of mkhfcts: Read error on input file \"%s\".n", filename); return EXIT_FAILURE; } fclose(fpt); return EXIT_SUCCESS;} /* end of scan_inputfile() */static int create_h(char *filename)/*===============================*/{ FILE *fpt; size_t i; fpt = fopen(filename, "w"); if (fpt == NULL) { printf("ERROR of mkhfcts: Cannot create output file \"%s\".\n", filename); return EXIT_FAILURE; } write_file_header(fpt); fprintf(fpt, "#include \"common.h\"\n"); fprintf(fpt, "\n\n"); for (i = 0; i < nfunctions; i++) fprintf(fpt, "void %s(message_t msg);\n", fct_names[i]); fprintf(fpt, "\n\n"); fprintf(fpt, "typedef enum\n{"); for (i = 0; i < nfunctions; i++) { strcpy(line_buf, fct_names[i]); str_upper(line_buf); fprintf(fpt, " %s,\n", line_buf); } fprintf(fpt, " HOST_FCT_MAX\n} host_fct_macros_t;\n"); if (ferror(fpt)) { printf("ERROR of mkhfcts: Write error on output file \"%s\".n", filename); return EXIT_FAILURE; } fclose(fpt); return EXIT_SUCCESS;} /* end of create_h() */static int create_c(char *filename)/*===============================*/{ size_t i; FILE *fpt; fpt = fopen(filename, "w"); if (fpt == NULL) { printf("ERROR of mkhfcts: Cannot create output file \"%s\".\n", filename); return EXIT_FAILURE; } write_file_header(fpt); fprintf(fpt, "#include <stdio.h>\n"); fprintf(fpt, "#include <m_host.h>\n"); fprintf(fpt, "#include \"allhfcts.h\"\n"); fprintf(fpt, "#include \"basic.h\"\n"); fprintf(fpt, "\n\n"); fprintf(fpt, "host_fct_t host_fcts[] =\n{\n"); for (i = 0; i < nfunctions; i++) fprintf(fpt, " %s,\n", fct_names[i]); fprintf(fpt, " NULL\n};\n"); if (ferror(fpt)) { printf("ERROR of mkhfcts: Write error on output file \"%s\".n", filename); return EXIT_FAILURE; } fclose(fpt); return EXIT_SUCCESS;} /* end of create_c() */int main(int argc, char *argv[])/******************************/{ size_t i; if (argc < 4) { printf("Usage: mkhfcts <input1> <input2> ... <.c-output> "); printf("<.h-output>\n"); return EXIT_FAILURE; } init_fct_list(); for (i = 1; i < argc-2; i++) { if (scan_inputfile(argv[i]) != EXIT_SUCCESS) return EXIT_FAILURE; } if (create_c(argv[argc-2]) != EXIT_SUCCESS) return EXIT_FAILURE; if (create_h(argv[argc-1]) != EXIT_SUCCESS) return EXIT_FAILURE; release_fct_list(); return EXIT_SUCCESS;} /* end of main() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -