home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-07-23 | 28.5 KB | 1,266 lines |
- _A SOURCE CODE GENERATOR FOR C_
- by Karl Vogel
-
-
- [LISTING ONE]
-
- /* File "new.h" */
-
- #ifndef lint
- static char *new_h_rcsid =
- "$Header: new.h,v 1.5 91/03/29 19:23:08 vogel Exp $";
-
- static char *new_h_source =
- "$Source: /d/cdc/vogel/source/new/RCS/new.h,v $";
-
- #endif
-
- /*
- * NAME:
- * new.h
- *
- * SYNOPSIS:
- * #include "new.h"
- *
- * DESCRIPTION:
- * Holds the data structure for the command line options, plus
- * general definitions.
- *
- * AUTHOR:
- * Karl Vogel
- *
- * BUGS:
- * None noticed.
- *
- * REVISIONS:
- *
- * $Log: new.h,v $
- * Revision 1.5 91/03/29 19:23:08 vogel
- * 1. Renamed the directory holding the templates.
- *
- * Revision 1.4 91/03/15 16:56:33 vogel
- * 1. Ran indent on the code and reformatted the comment header.
- *
- * Revision 1.3 90/07/03 14:21:20 vogel
- * 1. Added a new field: extension of the program to be created. This is also
- * in "macro.h", but it can be most easily set from examining the command
- * line options.
- *
- * Revision 1.2 90/07/03 12:07:06 vogel
- * 1. Added a flag to the OPTIONS structure which is TRUE if the created file
- * is to be edited, FALSE otherwise.
- *
- * Revision 1.1 90/06/29 15:32:04 vogel
- * Initial revision
- *
- */
-
- #include <stdio.h>
-
- #define YES 1
- #define NO 0
-
- /* Error codes. */
- #define OK 0
- #define EMALLOC 1
-
- /* Set directory holding templates, and prefix of each template file. */
- #define DIRECTORY "/d/cdc/vogel/source/new/"
- #define PREFIX "template."
-
- /* Set up the options structure. */
- struct options_data
- {
- char **files; /* NULL-terminated array of C files
- * to be created. */
- char *template; /* Full name of the desired
- * template file. */
- char exten[5]; /* Extension of the program to be
- * created. */
- int edit; /* True if the created program is to
- * be edited. */
- int help; /* True if help is needed. */
- };
-
- typedef struct options_data OPTIONS;
-
-
-
- [LISTING TWO]
-
-
- /* File "macro.h" */
-
- #ifndef lint
- static char *macro_h_rcsid =
- "$Header: macro.h,v 1.2 91/03/15 16:56:28 vogel Exp $";
-
- static char *macro_h_source =
- "$Source: /d/cdc/vogel/source/new/RCS/macro.h,v $";
-
- #endif
-
- /*
- * NAME:
- * macro.h
- *
- * SYNOPSIS:
- * #include "macro.h"
- *
- * DESCRIPTION:
- * Header file for macro substitutions.
- *
- * AUTHOR:
- * Karl Vogel
- *
- * BUGS:
- * Any problems or suggestions.
- *
- * REVISIONS:
- *
- * $Log: macro.h,v $
- * Revision 1.2 91/03/15 16:56:28 vogel
- * 1. Ran indent on the code and reformatted the comment header.
- *
- * Revision 1.1 90/06/29 17:40:15 vogel
- * Initial revision
- *
- */
-
- #define BUFFER 512
-
- struct macro_data
- {
- char args[BUFFER]; /* Arguments for the function: full
- * declarations separated by
- * semicolons and newlines. Replaces
- * $a in a template. */
- char body[BUFFER]; /* Body of the function. Comes under
- * the first comment after the
- * variable declarations. Includes
- * tabs and newlines. Replaces $b in
- * a template. */
- char exten[5]; /* Extension of the created file,
- * usually ".c". Replaces $e in a
- * template. */
- char functions[BUFFER]; /* Internal function
- * declarations. Comes just
- * after the start of the
- * function itself. Includes
- * tabs and newlines.
- * Replaces $f in a template. */
- char globals[BUFFER];/* External global variables. Comes
- * just before the start of the
- * function itself. Includes tabs
- * and newlines. Replaces $g in a
- * template. */
- char include[BUFFER];/* External include files. Comes
- * just before the start of the
- * function itself. Includes tabs
- * and newlines. Replaces $i in a
- * template. */
- char alist[BUFFER]; /* Short form of the argument list,
- * consisting of just the variable
- * names separated by commas.
- * Replaces $l in a template. */
- char name[64]; /* Name of the function being
- * created. May or may not be the
- * same as the filename. Replaces $n
- * in a template. */
- char define[BUFFER]; /* Holds preprocessor #define
- * statements separated by newlines.
- * Replaces $p in a template. */
- char rev[16]; /* Current revision level of the
- * created function. Replaces $r in
- * a template. */
- char type[32]; /* Type of function created, i.e.
- * char * or int. Replaces $t in a
- * template. */
- char usage[BUFFER]; /* Comes right after the first use of
- * the function name, and tells you
- * what the function is supposed to
- * do. Just above the Usage section
- * in the intro comments. This is
- * the only "smart" macro: won't let
- * you put too many words on a line,
- * and starts each line with a
- * comment indicator. Replaces $u in
- * a template. */
- char vars[BUFFER]; /* Holds internally-declared
- * variables, separated by tabs,
- * semicolons, and newlines. Replaces
- * $v in a template. */
- char userid[16]; /* Holds the author's userid.
- * Replaces $w in a template. */
- char username[BUFFER]; /* Holds the author's full
- * name. Replaces $x in a
- * template. */
- char token; /* Holds the character to be used as
- * a token indicator. Shown as a
- * dollar sign in this file. */
- int year; /* Current year. Replaces $y in a
- * template. */
- int month; /* Current month. Replaces $c in a
- * template. */
- int day; /* Current day. Replaces $d in a
- * template. */
- int hour; /* Current hour. Replaces $h in a
- * template. */
- int minute; /* Current minute. Replaces $m in a
- * template. */
- int second; /* Current second. Replaces $s in a
- * template. */
- };
- typedef struct macro_data MACRO;
-
-
-
-
- [LISTING THREE]
-
- /* File "new.c" */
-
- #ifndef lint
- static char *new_c_rcsid =
- "$Header: new.c,v 1.6 91/03/15 16:55:42 vogel Exp $";
-
- static char *new_c_source =
- "$Source: /d/cdc/vogel/source/new/RCS/new.c,v $";
-
- #endif
-
- /*
- * NAME:
- * new
- *
- * SYNOPSIS:
- * new [-c] [-h] [-mx] file [file ...]
- *
- * DESCRIPTION:
- * Creates and optionally edits one or more new C programs.
- *
- * "new" makes extensive use of templates to create new C files.
- * A template file has the name "template.x", where 'x' is the string
- * which follows '-m' in the program arguments.
- *
- * You can create other templates by adding template files to the
- * template directory specified in the header file "new.h".
- *
- * OPTIONS:
- * "-c" creates a new program but does NOT edit it.
- *
- * "-h" prints help information if improper or no arguments at all
- * are given.
- *
- * "-mx" creates a program of type 'x', where x is one of the following:
- * d - function driver.
- * f - normal function (default).
- * h - header file.
- * i - program to handle simple I/O.
- * m - main routine with arguments.
- * o - function to handle command line arguments.
- * s - stub (placeholder) function.
- *
- * "file" is one or more C files to be created. A suffix of ".c" will
- * be appended if it isn't there already. If the file already
- * exists, you will be asked if you want to overwrite it.
- *
- * AUTHOR:
- * Karl Vogel
- *
- * BUGS:
- * None noticed.
- *
- * REVISIONS:
- *
- * $Log: new.c,v $
- * Revision 1.6 91/03/15 16:55:42 vogel
- * 1. Ran indent on the code and reformatted the comment header.
- *
- * Revision 1.5 90/09/24 16:42:03 vogel
- * 1. Corrected a typo in the comments.
- *
- * Revision 1.4 90/07/03 14:20:23 vogel
- * 1. Copied the desired file extension into the MACRO structure.
- *
- * Revision 1.3 90/07/03 12:05:23 vogel
- * 1. Added "nextfile" variable, to hold the next file to be created.
- * 2. Got rid of debugging print statements for the macros structure.
- * 3. Added code to create and optionally edit each file in turn.
- *
- * Revision 1.2 90/06/29 17:40:04 vogel
- * 1. Added header file and definitions for the macro substitution strings.
- * 2. Added code to print help if needed.
- *
- * Revision 1.1 90/06/29 15:27:52 vogel
- * Initial revision
- *
- */
-
- #include "new.h"
- #include "macro.h"
- #include <strings.h>
- #include <sys/param.h>
-
- main (argc, argv)
- int argc;
- char **argv;
- {
-
- /* Variables. */
- MACRO *macros;
- MACRO macbuffer;
-
- OPTIONS *options;
- OPTIONS opbuffer;
-
- char nextfile[MAXPATHLEN];
-
- int k;
- int status;
-
- /* Process command line options. If they aren't OK, offer help and exit. */
- options = &opbuffer;
- status = GetOptions (argc, argv, options);
-
- if (status == EMALLOC)
- {
- fprintf (stderr, "Severe memory error -- please call the ");
- fprintf (stderr, "System Administrator.\n");
- exit (1);
- }
-
- if (options->help)
- {
- (void) Help ();
- exit (1);
- }
-
- /* Set up the substitution macros. */
- macros = &macbuffer;
- SetMacros (macros);
- (void) strcpy (macros->exten, options->exten);
-
- /* Create and optionally edit each file in turn. */
- for (k = 0; options->files[k]; ++k)
- {
- (void) strcpy (nextfile, options->files[k]);
-
- if (CreateCode (macros, options->template, nextfile) == 0)
- if (options->edit)
- EditCode (nextfile);
- }
-
- exit (0);
- }
-
-
-
-
- [LISTING FOUR]
-
- /* File "CreateCode.c" */
-
- #ifndef lint
- static char *CreateCode_c_rcsid =
- "$Header: CreateCode.c,v 1.2 91/03/15 16:55:25 vogel Exp $";
-
- static char *CreateCode_c_source =
- "$Source: /d/cdc/vogel/source/new/RCS/CreateCode.c,v $";
-
- #endif
-
- /*
- * NAME:
- * CreateCode
- *
- * SYNOPSIS:
- * #include "macro.h"
- *
- * int CreateCode (macros, template, nextfile)
- * MACRO * macros;
- * char * template;
- * char * nextfile;
- *
- * DESCRIPTION:
- * Accepts the substitution structure, the input file, and the new C
- * file to be created. Reads the template file, fills it in with the
- * contents of the substitution structure, and writes the results to
- * the new C file.
- *
- * A function value of 0 is returned if everything is OK.
- * A function value of 1 is returned if the template could not be read.
- * A function value of 2 is returned if the output could not be written.
- *
- * ARGUMENTS:
- * "macros" is the structure holding the macro substitution values.
- * "template" is the name of the input template file.
- * "nextfile" is the name of the next output file.
- *
- * AUTHOR:
- * Karl Vogel
- *
- * BUGS:
- * None noticed.
- *
- * REVISIONS:
- *
- * $Log: CreateCode.c,v $
- * Revision 1.2 91/03/15 16:55:25 vogel
- * 1. Ran indent on the code and reformatted the comment header.
- *
- * Revision 1.1 90/07/03 11:59:45 vogel
- * Initial revision
- *
- */
-
- #include "macro.h"
- #include <stdio.h>
- #include <strings.h>
- #include <sys/file.h>
-
- #define SMALLBUF 20
-
- int CreateCode (macros, template, nextfile)
- MACRO *macros;
- char *template;
- char *nextfile;
- {
-
- /* Variables. */
-
- FILE *in;
- FILE *out;
-
- char *base;
- char *dot;
- char last[SMALLBUF];
- char prompt[SMALLBUF];
-
- int length;
- int status;
-
- /* If filename already has default extension specified in MACRO structure, zap
- * it so we can store the function name in the MACRO structure. */
- status = 0;
- in = (FILE *) NULL;
- out = (FILE *) NULL;
-
- last[0] = '.';
- last[1] = '\0';
-
- (void) strcat (last, macros->exten);
- length = strlen (last);
-
- if (dot = rindex (nextfile, '.'))
- if (strcmp (dot, last) == 0)
- *dot = '\0';
-
- if (base = rindex (nextfile, '/'))
- base++;
- else
- base = nextfile;
-
- (void) strcpy (macros->name, base);
- (void) strcat (nextfile, last);
-
- /* If output file already exists, make sure that user wants to overwrite it. */
- if (access (nextfile, F_OK) == 0)
- {
- printf ("File \"%s\" exists. Overwrite? ", nextfile);
- fflush (stdout);
- fgets (prompt, SMALLBUF, stdin);
-
- if (prompt[0] != 'y' && prompt[0] != 'Y')
- {
- printf ("\"%s\" not overwritten.\n", nextfile);
- goto done;
- }
- }
-
- /* Open the input template file. */
- if ((in = fopen (template, "r")) == (FILE *) NULL)
- {
- printf ("Unable to open template file \"%s\"\n", template);
- status = 1;
- goto done;
- }
-
- /* Open the output C file. */
- if ((out = fopen (nextfile, "w")) == (FILE *) NULL)
- {
- printf ("Unable to open C file \"%s\"\n", nextfile);
- status = 2;
- goto done;
- }
-
- /* Do the macro substitution, clean up, and return. */
- ReplaceMacros (in, macros, out);
- done:
- if (in)
- (void) fclose (in);
- if (out)
- (void) fclose (out);
- return (status);
- }
-
-
- [LISTING FIVE]
-
- /* File "EditCode.c" */
-
- #ifndef lint
- static char *EditCode_c_rcsid =
- "$Header: EditCode.c,v 1.2 91/03/15 16:55:28 vogel Exp $";
-
- static char *EditCode_c_source =
- "$Source: /d/cdc/vogel/source/new/RCS/EditCode.c,v $";
-
- #endif
-
- /*
- * NAME:
- * EditCode
- *
- * SYNOPSIS:
- * int EditCode (nextfile)
- * char * nextfile;
- *
- * DESCRIPTION:
- * Edits the new C file using either "cvi" or whatever the user has in
- * his "EDITOR" environment variable.
- *
- * ARGUMENTS:
- * "nextfile" is the new C file.
- *
- * AUTHOR:
- * Karl Vogel
- *
- * BUGS:
- * None noticed.
- *
- * REVISIONS:
- *
- * $Log: EditCode.c,v $
- * Revision 1.2 91/03/15 16:55:28 vogel
- * 1. Ran indent on the code and reformatted the comment header.
- *
- * Revision 1.1 90/07/03 12:00:13 vogel
- * Initial revision
- */
-
- #include <stdio.h>
-
- int EditCode (nextfile)
- char *nextfile;
- {
-
- /* Functions. */
- char *getenv ();
-
- /* Variables. */
- char *editor;
- char cmd[BUFSIZ];
-
- int status;
-
- /* Create and execute the edit command. */
- if (editor = getenv ("EDITOR"))
- (void) strcpy (cmd, editor);
- else
- (void) strcpy (cmd, "cvi");
- (void) strcat (cmd, " ");
- (void) strcat (cmd, nextfile);
- (void) system (cmd);
-
- return (0);
- }
-
-
- [LISTING SIX]
-
- /* File "GetOptions.c" */
-
- #ifndef lint
- static char *GetOptions_c_rcsid =
- "$Header: GetOptions.c,v 1.4 91/03/15 16:55:31 vogel Exp $";
-
- static char *GetOptions_c_source =
- "$Source: /d/cdc/vogel/source/new/RCS/GetOptions.c,v $";
-
- #endif
-
- /*
- * NAME:
- * GetOptions
- *
- * SYNOPSIS:
- * #include "new.h"
- *
- * int GetOptions (argc, argv, options)
- * int argc;
- * char ** argv;
- * OPTIONS * options;
- *
- * DESCRIPTION:
- * Accepts the command line arguments and a pointer to a structure which
- * holds the options, and parses the arguments into the structure.
- *
- * ARGUMENTS:
- * "argc" and "argv" are the normal C program variables holding the
- * argument list.
- *
- * "options" is the structure meant to hold the options.
- *
- * AUTHOR:
- * Karl Vogel
- *
- * BUGS:
- * Any problems or suggestions.
- *
- * REVISIONS:
- *
- * $Log: GetOptions.c,v $
- * Revision 1.4 91/03/15 16:55:31 vogel
- * 1. Ran indent on the code and reformatted the comment header.
- *
- * Revision 1.3 90/07/03 14:19:49 vogel
- * 1. Added code to set up the file extension based on the command line options.
- * "new -mh" gives you a file with name ending in ".h", anything else gives
- * you a file with name ending in ".c".
- *
- * Revision 1.2 90/07/03 12:01:01 vogel
- * 1. Added new option '-c'.
- * 2. Added code to make sure that default template is properly set up.
- *
- * Revision 1.1 90/06/29 15:28:13 vogel
- * Initial revision
- *
- */
-
- #include "new.h"
- #include <strings.h>
-
- #define SMALL 10
-
- int GetOptions (argc, argv, options)
- int argc;
- char **argv;
- OPTIONS *options;
- {
-
- /* Functions. */
- char *malloc ();
-
- /* Variables. */
- char *next;
- char extension[SMALL + 1];
-
- int count;
- int flags;
- int k;
- int status;
-
- unsigned int length;
-
- /* Initialize options to defaults. */
- options->files = (char **) NULL;
- options->template = (char *) NULL;
- options->edit = YES;
- options->help = NO;
-
- (void) strcpy (extension, "f");
-
- count = 0;
- status = OK;
-
- /* Handle the flags, and count the files. */
- for (k = 1; k < argc; ++k)
- {
- next = argv[k];
-
- if (*next == '-')
- {
- next++;
-
- if (*next == 'm')
- {
- (void) strncpy (extension, next + 1, SMALL);
- extension[SMALL] = '\0';
- }
-
- else
- if (*next == 'c')
- options->edit = NO;
-
- else
- {
- options->help = YES;
- goto done;
- }
- }
-
- else /* option does not start with a dash */
- count++;
- }
-
- /* If no files were specified, set help flag. Otherwise, store filenames. */
- if (count)
- {
- length = (unsigned) ((count + 1) * (sizeof (char *)));
- if (options->files = (char **) malloc (length))
- {
- for (k = 1, count = 0; k < argc; ++k)
- if (*argv[k] != '-')
- options->files[count++] = argv[k];
- options->files[count] = (char *) NULL;
- }
- else
- {
- status = EMALLOC;
- goto done;
- }
- }
- else
- {
- options->help = YES;
- goto done;
- }
-
- /* Set up the file extension. */
- if (strcmp (extension, "h") == 0)
- options->exten[0] = 'h';
- else
- options->exten[0] = 'c';
-
- options->exten[1] = '\0';
-
- /* Set up the template pathname. */
- length = (unsigned) (strlen (DIRECTORY) + strlen (PREFIX) +
- strlen (extension) + 1);
- if (options->template = malloc (length))
- (void) sprintf (options->template, "%s%s%s", DIRECTORY,
- PREFIX, extension);
- else
- status = EMALLOC;
- done:
- return (status);
- }
-
-
-
-
- [LISTING SEVEN]
-
- /* File "Help.c" */
-
- #ifndef lint
- static char *Help_c_rcsid =
- "$Header: Help.c,v 1.4 91/03/15 16:55:33 vogel Exp $";
-
- static char *Help_c_source =
- "$Source: /d/cdc/vogel/source/new/RCS/Help.c,v $";
-
- #endif
-
- /*
- * NAME:
- * Help
- *
- * SYNOPSIS:
- * int Help ()
- *
- * DESCRIPTION:
- * Prints help information to stdout.
- *
- * ARGUMENTS:
- * None.
- *
- * AUTHOR:
- * Karl Vogel
- *
- * BUGS:
- * None noticed.
- *
- * REVISIONS:
- *
- * $Log: Help.c,v $
- * Revision 1.4 91/03/15 16:55:33 vogel
- * 1. Ran indent on the code and reformatted the comment header.
- *
- * Revision 1.3 90/09/24 16:40:47 vogel
- * 1. Corrected a typo.
- *
- * Revision 1.2 90/07/03 12:01:42 vogel
- * 1. Changed print strings to reflect new '-c' option.
- *
- * Revision 1.1 90/06/29 17:40:46 vogel
- * Initial revision
- *
- */
-
- #include <stdio.h>
-
- int Help ()
- {
-
- /* Variables. */
- int k;
-
- static char *array[] =
- {
- "",
- "\tnew:\t\tcreates and optionally edits one or more new C",
- "\t\t\tprograms.",
- "",
- "\t\tUsage:\tnew [-c] [-h] [-mx] file [file ...]",
- "",
- "\t\tWhere:\t\"-c\"\tcreates a new program but does NOT edit it.",
- "",
- "\t\t\t\"-h\"\tprints a help file. Help is also given if",
- "\t\t\t\tno arguments at all are given.",
- "",
- "\t\t\t\"-mx\"\tcreates a program of type 'x', where x is",
- "\t\t\t\tone of the following:",
- "\t",
- "\t\t\t\td - function driver.",
- "\t\t\t\tf - normal function (default).",
- "\t\t\t\th - header file.",
- "\t\t\t\ti - program to handle simple I/O.",
- "\t\t\t\tm - main routine with arguments.",
- "\t\t\t\to - function to handle command line arguments.",
- "\t\t\t\ts - stub (placeholder) function.",
- "",
- "\t\t\t\"file\"\tis one or more C files to be created. A",
- "\t\t\t\tsuffix of \".c\" will be appended if it isn't",
- "\t\t\t\tthere already.",
- "\t",
- "\t\t\t\tIf the file already exists, you will be asked",
- "\t\t\t\tif you want to overwrite it.",
- "",
- "",
- "\t\t\"new\" makes extensive use of templates to create new C files.",
- "\t\tA template file has the name \"template.x\", where 'x' is the",
- "\t\tstring which follows '-m' in the program arguments.",
- "\t",
- "\t\tYou can create other templates by adding template files to",
- "\t\tthe template directory specified in the header file \"new.h\".",
- (char *) NULL
- };
-
- /* Write the help information. */
- for (k = 0; array[k]; ++k)
- puts (array[k]);
- return (0);
- }
-
-
-
- [LISTING EIGHT]
-
- /* File "OutString.c" */
-
- #ifndef lint
- static char *OutString_c_rcsid =
- "$Header: OutString.c,v 1.2 91/03/15 16:55:35 vogel Exp $";
-
- static char *OutString_c_source =
- "$Source: /d/cdc/vogel/source/new/RCS/OutString.c,v $";
-
- #endif
-
- /*
- * NAME:
- * OutString
- *
- * SYNOPSIS:
- * #include "new.h"
- * #include "macro.h"
- *
- * int OutString (macros, current, string)
- * MACRO * macros;
- * char current;
- * char * string;
- *
- * DESCRIPTION:
- * Decides which macro string to return based on the current token
- * character.
- *
- *
- * ARGUMENTS:
- * "macros" holds the current macro replacement values.
- *
- * "current" is the value of the token character following a dollar sign.
- *
- * "string" is the replacement value of that token.
- *
- * AUTHOR:
- * Karl Vogel
- *
- * BUGS:
- * None noticed.
- *
- * REVISIONS:
- *
- * $Log: OutString.c,v $
- * Revision 1.2 91/03/15 16:55:35 vogel
- * 1. Ran indent on the code and reformatted the comment header.
- *
- * Revision 1.1 90/07/03 12:02:28 vogel
- * Initial revision
- *
- */
-
- #include "new.h"
- #include "macro.h"
- #include <stdio.h>
- #include <ctype.h>
- #include <strings.h>
-
- int OutString (macros, current, string)
- MACRO *macros;
- char current;
- char *string;
- {
-
- /* Variables. */
- char *blank;
- char *s;
- char *t;
- char temp[BUFFER];
-
- int col;
- int first;
- int length;
- int status;
-
- /* Do the simple string replacements, and numeric conversion. */
- switch (current)
- {
- case 'a':
- (void) strcpy (string, macros->args);
- break;
- case 'b':
- (void) strcpy (string, macros->body);
- break;
- case 'c':
- (void) sprintf (string, "%2.2d", macros->month);
- break;
- case 'd':
- (void) sprintf (string, "%2.2d", macros->day);
- break;
- case 'e':
- (void) strcpy (string, macros->exten);
- break;
- case 'f':
- (void) strcpy (string, macros->functions);
- break;
- case 'g':
- (void) strcpy (string, macros->globals);
- break;
- case 'h':
- (void) sprintf (string, "%2.2d", macros->hour);
- break;
- case 'i':
- break;
- case 'l':
- (void) strcpy (string, macros->alist);
- break;
- case 'm':
- (void) sprintf (string, "%2.2d", macros->minute);
- break;
- case 'n':
- (void) strcpy (string, macros->name);
- break;
- case 'p':
- (void) strcpy (string, macros->define);
- break;
- case 'r':
- (void) strcpy (string, macros->rev);
- break;
- case 's':
- (void) sprintf (string, "%2.2d", macros->second);
- break;
- case 't':
- (void) strcpy (string, macros->type);
- break;
- case 'u':
- break;
- case 'v':
- (void) strcpy (string, macros->vars);
- break;
- case 'w':
- (void) strcpy (string, macros->userid);
- break;
- case 'x':
- (void) strcpy (string, macros->username);
- break;
- case 'y':
- (void) sprintf (string, "%2.2d", macros->year);
- break;
- default:
- *string = '\0';
- break;
- }
-
- /* Handle "Usage" string. Write no more than 50 characters/line, indented
- *3 tab spaces in. */
- if (current == 'u')
- {
- (void) strcpy (temp, macros->usage);
- t = temp;
- *string = '\0';
- first = YES;
- while (length = strlen (t))
- {
- if (!first)
- (void) strcat (string, "\n * \t\t\t");
-
- first = NO;
- if (length <= 50)
- {
- (void) strcat (string, t);
- break;
- }
- else
- {
- blank = (char *) NULL;
- col = 25;
- for (s = t; *s && col < 75; ++s, ++col)
- if (isspace (*s))
- blank = s;
- if (blank)
- {
- *blank = '\0';
- (void) strcat (string, t);
- t = blank + 1;
- }
- else
- {
- (void) strcat (string, t);
- break;
- }
- }
- }
- }
-
- /* Handle the "include" strings. */
- if (current == 'i')
- {
- for (s = string, t = macros->include; *t; ++s, ++t)
- {
- if (*t == ' ')
- *s = '\t';
- else
- *s = *t;
- }
- *s = '\0';
- }
- return (0);
- }
-
-
-
- [LISTING NINE]
-
- /* File "ReplaceMacros.c" */
-
- #ifndef lint
- static char *ReplaceMacros_c_rcsid =
- "$Header: ReplaceMacros.c,v 1.2 91/03/15 16:55:37 vogel Exp $";
-
- static char *ReplaceMacros_c_source =
- "$Source: /d/cdc/vogel/source/new/RCS/ReplaceMacros.c,v $";
-
- #endif
-
- /*
- * NAME:
- * ReplaceMacros
- *
- * SYNOPSIS:
- * #include "macro.h"
- *
- * int ReplaceMacros (in, macros, out)
- * FILE * in;
- * MACRO * macros;
- * FILE * out;
- *
- * DESCRIPTION:
- * Accepts the macro structure holding the variables to be replaced,
- * an input filepointer, and an output filepointer. Does token
- * substitution from input to output.
- *
- * ARGUMENTS:
- * Described above.
- *
- * AUTHOR:
- * Karl Vogel
- *
- * BUGS:
- * None noticed.
- *
- * REVISIONS:
- *
- * $Log: ReplaceMacros.c,v $
- * Revision 1.2 91/03/15 16:55:37 vogel
- * 1. Ran indent on the code and reformatted the comment header.
- *
- * Revision 1.1 90/07/03 12:02:42 vogel
- * Initial revision
- *
- */
-
- #include "macro.h"
- #include <stdio.h>
- #include <ctype.h>
- #include <strings.h>
-
- int ReplaceMacros (in, macros, out)
- FILE *in;
- MACRO *macros;
- FILE *out;
- {
-
- /* Variables. */
- char *s;
- char string[BUFFER];
- char current;
- char previous;
-
- /* Start the main loop which looks ahead one character. */
- previous = getc (in);
- if (previous == EOF)
- return (0);
-
- /* Decide what to do if we get a token character. */
- while ((current = getc (in)) != EOF)
- {
- if (previous == macros->token)
- {
- if (index ("abcdefghilmnprstuvwxy", current))
- {
- OutString (macros, current, string);
- for (s = string; *s; ++s)
- putc (*s, out);
- previous = getc (in);
- }
- else
- {
- putc (previous, out);
- previous = current;
- }
- }
- else
- {
- putc (previous, out);
- previous = current;
- }
- }
-
- /* Don't forget to write the last character. */
- putc (previous, out);
- return (0);
- }
-
-
-
- [LISTING TEN]
-
- /* File "SetMacros.c" */
-
- #ifndef lint
- static char *SetMacros_c_rcsid =
- "$Header: SetMacros.c,v 1.4 91/03/15 16:55:40 vogel Exp $";
-
- static char *SetMacros_c_source =
- "$Source: /d/cdc/vogel/source/new/RCS/SetMacros.c,v $";
-
- #endif
-
- #include "macro.h"
- #include <stdio.h>
- #include <strings.h>
- #include <pwd.h>
- #include <sys/time.h>
-
- int SetMacros (macros)
- MACRO *macros;
- {
-
- /* Functions. */
- char *getenv ();
- struct passwd *getpwuid ();
-
- /* Variables. */
- char *s;
- int offset;
- long clock;
- struct passwd *ptr;
- struct tm *now;
-
- /* Set up the internal C stuff. The "name" entry will be set later. */
- macros->args[0] = '\0';
- macros->body[0] = '\0';
- macros->functions[0] = '\0';
- macros->globals[0] = '\0';
- macros->alist[0] = '\0';
- macros->name[0] = '\0';
- macros->define[0] = '\0';
- macros->usage[0] = '\0';
- macros->vars[0] = '\0';
-
- (void) strcpy (macros->exten, "c");
- (void) strcpy (macros->rev, "1.1");
- (void) strcpy (macros->type, "int");
-
- (void) strcpy (macros->include,
- "#include <stdio.h>\n#include <ctype.h>");
-
- /* Set up userid and full name of program author. See if we can get the
- * information from the environment. If that fails, look it up from userid
- * and passwd file. */
- macros->userid[0] = '\0';
- macros->username[0] = '\0';
-
- if (s = getenv ("USER"))
- (void) strcpy (macros->userid, s);
- if (s = getenv ("USERNAME"))
- (void) strcpy (macros->username, s);
- if (strlen (macros->userid) == 0 || strlen (macros->username) == 0)
- {
- if (ptr = getpwuid (getuid ()))
- {
- (void) strcpy (macros->userid, ptr->pw_name);
- if (strncmp (ptr->pw_gecos, "Civ ", 4) == 0)
- offset = 4;
- else
- offset = 0;
- (void) strcpy (macros->username,
- ptr->pw_gecos + offset);
- if (s = index (macros->username, ';'))
- *s = '\0';
- }
- else
- {
- (void) strcpy (macros->userid, "unknown");
- (void) strcpy (macros->username, "unknown");
- }
- }
-
- /* Set the character which we will recognize as the start of a token. */
- macros->token = '$';
-
- /* Set up the current date and time. */
- (void) time (&clock);
- now = localtime (&clock);
- macros->second = now->tm_sec;
- macros->minute = now->tm_min;
- macros->hour = now->tm_hour;
- macros->day = now->tm_mday;
- macros->month = now->tm_mon + 1;
- macros->year = now->tm_year;
-
- return (0);
- }