📄 output.c
字号:
/******************************************************************************* * * O U T P U T . C * --------------- * * Description: * Generates the C-code for CForms. * * Included functions: * output - Does the job * * Revision: * Ver Date By Reason * --- ---- -- ------ * 1.00 900627 Lars Berntzon Created * ******************************************************************************/#ifndef lintstatic volatile char sccs_id[] = "@(#) output.c,v 1.14 1993/07/20 23:58:14 lasse Exp";#endif#include "config.h"#include "token.h"#include "comp.h"#include "../patchlevel.h"static char module[200];static char ofname[202];/****************************************************************** * O U T P U T * ----------- * Description: * Write out the file. * * Arguments: * none * * Return: * none * * Global variables: * filename - Name of input file. * list - Struct of lists of all things. * ******************************************************************/void output(void){ struct picture *pp; struct field *fp; struct event *ep; struct stmt *sp; struct literal *lp; struct ccode *cp; int n_fields, n_events, n_literals, n_pictures; FILE *out; char *p; /* * Make module name. */ if ((p = strrchr(filename, '/')) != NULL) p++; else p = filename; strncpy(module, p, sizeof module); if (p = strrchr(module, '.')) { *p = 0; } /* * Make output filename. */ strncpy(ofname, filename, sizeof ofname); if ((p = strrchr(ofname, '.')) != NULL) *p = 0; strcat(ofname, ".c"); /* * Open output file. */ if ((out = fopen(ofname, "w")) == NULL) { fprintf(stderr, "failed to open output file.\n"); cleanup(0); } /* * G e n e r a t e H e a d e r. */ fprintf(out, "/* Generated by C-Forms version %s */\n\n", PATCHLEVEL); fprintf(out, "#include <stdio.h>\n\n"); fprintf(out, "#include <curses.h>\n"); fprintf(out, "#include <cforms.h>\n\n"); /* * D e c l a r a t i o n o f t h i s m o d u l e. */ fprintf(out, "extern struct module _cf_module_%s;\n\n", module); /* * C r e a t e C - c o d e s. */ debug("writing c-codes..."); if (list.ccode) fprintf(out, " /* C - c o d e s */\n"); for(cp = list.ccode; cp != NULL; cp = NEXT_CCODE(cp)) { stmt_write(cp->stmt, out); fprintf(out, "\n\n"); } /* * C r e a t e p i c t u r e s. */ debug("writing pictures..."); if (list.picture) fprintf(out, " /* P i c t u r e s */\n\n"); for(pp = list.picture; pp != NULL; pp = NEXT_PICTURE(pp)) { /* * E v e n t s f o r a l l f i e l d s. */ n_fields = 0; n_literals = 0; debug("writing events for fields..."); for(fp = pp->field; fp != NULL; fp = NEXT_FIELD(fp)) { /* Event routines for field */ for(ep = fp->event; ep != NULL; ep = NEXT_EVENT(ep)) { if (ep->stmt) { fprintf(out, "static void event_%s_%s_%s(int event_code)\n{\n", pp->link.name, fp->link.name, ep->link.name); stmt_write(ep->stmt, out); fprintf(out, "\n}\n"); } } /* Event table for field */ if (fp->event) { fprintf(out, "static struct event etab_%s_%s[] = {\n", pp->link.name, fp->link.name); for(ep = fp->event; ep != NULL; ep = NEXT_EVENT(ep)) { fprintf(out, " %s, %s, %d, ", ep->type, ep->code, ep->global); if (ep->stmt) { fprintf(out, "event_%s_%s_%s", pp->link.name, fp->link.name, ep->link.name); } else { fprintf(out, "NULL"); } fprintf(out, "%s\n", NEXT_EVENT(ep) ? "," : ""); } fprintf(out, "};\n\n"); } /* * F i e l d d a t a a r r a y. */ if (fp->value) { fprintf(out, "static char field_%s_%s[%d] = \"%s\";\n\n", pp->link.name, fp->link.name, fp->len + 1, fp->value); } else { fprintf(out, "static char field_%s_%s[%d];\n\n", pp->link.name, fp->link.name, fp->len + 1); } fprintf(out, "/***************************************************/\n"); } /* * T a b l e o f a l l f i e l d s. */ debug("writing table of fields..."); if (pp->field) { fprintf(out, "static struct field ftab_%s[] = {\n", pp->link.name); for(fp = pp->field; fp != NULL; n_fields++, fp = NEXT_FIELD(fp)) { fprintf(out, " {\"%s\", ", fp->link.name); fprintf(out, "%s, %d, ", fp->type, fp->len); fprintf(out, "field_%s_%s, ", pp->link.name, fp->link.name); /* * Calculated centered field. */ if (ISCENTER(fp->pos.x)) { fprintf(out, "%d, ", fp->pos.x - CF_CENTER + pp->viewport->size.x / 2 - fp->len / 2); } else if (ISMAX(fp->pos.x)) { fprintf(out, "%d, ", fp->pos.x - CF_MAX + pp->viewport->size.x - 1 - fp->len); } else { fprintf(out, "%d, ", fp->pos.x); } if (ISCENTER(fp->pos.y)) { fprintf(out, "%d, ", fp->pos.y - CF_CENTER + pp->viewport->size.y / 2); } else if (ISMAX(fp->pos.y)) { fprintf(out, "%d, ", fp->pos.y - CF_MAX + pp->viewport->size.y - 1); } else { fprintf(out, "%d, ", fp->pos.y); } fprintf(out, "\"%s\", \"%s\", ", fp->lvalue ? fp->lvalue : "", fp->rvalue ? fp->rvalue : ""); if (fp->event) { fprintf(out, "etab_%s_%s", pp->link.name, fp->link.name); } else { fprintf(out, "NULL"); } for(n_events = 0, ep = fp->event; ep != NULL; n_events++, ep = NEXT_EVENT(ep)) ; fprintf(out, ", %d, %d", n_events, fp->flags); fprintf(out, ", 0"); /* Modified flag */ fprintf(out, ", NULL"); /* Modified flag */ fprintf(out, "}%s", NEXT_FIELD(fp) ? ",\n" : "\n"); } fprintf(out, "};\n\n"); } /* * T a b l e o f l i t e r a l s. */ debug("writing table of literals..."); if (pp->literal) { fprintf(out, "static struct literal ltab_%s[] = {\n", pp->link.name); for(lp = pp->literal; lp != NULL; n_literals++, lp = NEXT_LITERAL(lp)) { if (ISCENTER(lp->pos.x)) { fprintf(out, " %d, ", lp->pos.x - CF_CENTER + pp->viewport->size.x / 2 - (int)strlen(lp->link.name) / 2); } else if (ISMAX(lp->pos.x)) { fprintf(out, " %d, ", lp->pos.x - CF_MAX + pp->viewport->size.x - 1 - strlen(lp->link.name)); } else { fprintf(out, " %d, ", lp->pos.x); } if (ISCENTER(lp->pos.y)) { fprintf(out, "%d, ", lp->pos.y - CF_CENTER + pp->viewport->size.y / 2); } else if (ISMAX(lp->pos.y)) { fprintf(out, "%d, ", lp->pos.y - CF_MAX + pp->viewport->size.y - 1); } else { fprintf(out, "%d, ", lp->pos.y); } fprintf(out, "\"%s\", %d%s\n", lp->link.name, lp->flags, NEXT_LITERAL(lp) ? "," : ""); } fprintf(out, "};\n\n"); } /* * E v e n t s f o r p i c t u r e. */ debug("writing events for pictures..."); for(ep = pp->event; ep != NULL; ep = NEXT_EVENT(ep)) { if (ep->stmt) { fprintf(out, "static void event_%s_%s(int event_code)\n{\n", pp->link.name, ep->link.name); stmt_write(ep->stmt, out); fprintf(out, "\n}\n\n"); } } n_events = 0; if(pp->event) { fprintf(out, "static struct event etab_%s[] = {\n", pp->link.name); for(ep = pp->event; ep != NULL; n_events++, ep = NEXT_EVENT(ep)) { fprintf(out, " %s, %s, %d, ", ep->type, ep->code, ep->global); if (ep->stmt) { fprintf(out, "event_%s_%s", pp->link.name, ep->link.name); } else { fprintf(out, "NULL"); } fprintf(out, "%s\n", NEXT_EVENT(ep) ? "," : ""); } fprintf(out, "};\n\n"); } } /* * T a b l e o f p i c t u r e s. */ debug("writing table of pictures..."); if(list.picture) { fprintf(out, "static struct picture ptab[] = {\n"); } n_pictures = 0; for(pp = list.picture; pp != NULL; n_pictures++, pp = NEXT_PICTURE(pp)) { fprintf(out, " {\"%s\", ", pp->link.name); /* Fields */ for(n_fields = 0, fp = pp->field; fp; fp = NEXT_FIELD(fp)) { n_fields++; } if (pp->field) { fprintf(out, "ftab_%s, %d, ", pp->link.name, n_fields); } else fprintf(out, "NULL, 0, "); /* Literals */ for(n_literals = 0, lp = pp->literal; lp; lp = NEXT_LITERAL(lp)) { n_literals++; } if (pp->literal) { fprintf(out, "ltab_%s, %d, ", pp->link.name, n_literals); } else fprintf(out, "NULL, 0, "); /* Events */ for(n_events = 0, ep = pp->event; ep; ep = NEXT_EVENT(ep)) { n_events++; } if (pp->event) { fprintf(out, "etab_%s, %d", pp->link.name, n_events); } else fprintf(out, "NULL, 0"); fprintf(out, ", %d,%d, %d,%d", pp->viewport->pos.x,pp->viewport->pos.y, pp->viewport->size.x, pp->viewport->size.y); fprintf(out, ", &_cf_module_%s, ", module); fprintf(out, "%d}", pp->flags); if (NEXT_PICTURE(pp)) { fprintf(out, ","); } fprintf(out, "\n"); } if(list.picture) { fprintf(out, "};\n\n"); } /* * Event functions for module. */ debug("writing events for module..."); for(ep = list.event; ep != NULL; ep = NEXT_EVENT(ep)) { if (ep->stmt) { fprintf(out, "static void event_%s(int event_code)\n{\n", ep->link.name); stmt_write(ep->stmt, out); fprintf(out, "\n}\n\n"); } } n_events = 0; if(list.event) { fprintf(out, "static struct event etab[] = {\n"); for(ep = list.event; ep != NULL; n_events++, ep = NEXT_EVENT(ep)) { fprintf(out, " %s, %s, %d, ", ep->type, ep->code, ep->global); if (ep->stmt) { fprintf(out, "event_%s", ep->link.name); } else { fprintf(out, "NULL"); } fprintf(out, "%s\n", NEXT_EVENT(ep) ? "," : ""); } fprintf(out, "};\n\n"); } debug("writing module..."); fprintf(out, "struct module _cf_module_%s = { ", module); if (list.picture) fprintf(out, "ptab, %d, ", n_pictures); else fprintf(out, "NULL, 0, "); if (list.event) fprintf(out, "etab, %d", n_events); else fprintf(out, "NULL, 0"); fprintf(out, "};\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -