From cc3348c9993227c2efc2f72e6de915da566cae27 Mon Sep 17 00:00:00 2001 From: Thomas Jensen Date: Fri, 19 Mar 1999 17:44:47 +0000 Subject: [PATCH] ... still programming ... --- src/boxes.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 159 insertions(+), 3 deletions(-) diff --git a/src/boxes.c b/src/boxes.c index 5a8f202..34ef585 100644 --- a/src/boxes.c +++ b/src/boxes.c @@ -1,4 +1,27 @@ +/* + * File: boxes.c + * Date created: March 18, 1999 (Thursday, 15:09h) + * Author: Thomas Jensen + * tsjensen@stud.informatik.uni-erlangen.de + * Version: $Id$ + * Language: ANSI C + * Platforms: sunos5/sparc, for now + * World Wide Web: http://home.pages.de/~jensen/boxes/ + * Purpose: Filter to draw boxes around input text. + * Intended for use with vim(1). + * Remarks: This is not a release. + * + * Revision History: + * + * $Log$ + * Revision 1.1 1999/03/18 15:09:17 tsjensen + * Initial revision + * +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + +#include #include #include #include @@ -11,6 +34,8 @@ extern int optind, opterr, optopt; extern FILE *yyin; /* lex input file */ +const char rcsid[] = "$Id$"; + char *yyfilename = NULL; /* file name of config file used */ design_t *designs = NULL; /* available box designs */ @@ -19,6 +44,24 @@ int design_idx = 0; /* anz_designs-1 */ int anz_designs = 0; /* no of designs after parsing */ +char *shape_name[] = { + "NW", "NNW", "N", "NNE", "NE", "ENE", "E", "ESE", + "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW" +}; + +char *ofs_name[] = { + "NW_NNW", "NNW_N", "N_NNE", "NNE_NE", "NE_ENE", "ENE_E", "E_ESE", "ESE_SE", + "SE_SSE", "SSE_S", "S_SSW", "SSW_SW", "SW_WSW", "WSW_W", "W_WNW", "WNW_NW" +}; + +shape_t north_side[SHAPES_PER_SIDE] = { NW, NNW, N, NNE, NE }; /* clockwise */ +shape_t east_side[SHAPES_PER_SIDE] = { NE, ENE, E, ESE, SE }; +shape_t south_side[SHAPES_PER_SIDE] = { SE, SSE, S, SSW, SW }; +shape_t west_side[SHAPES_PER_SIDE] = { SW, WSW, W, WNW, NW }; +shape_t corners[ANZ_CORNERS] = { NW, NE, SE, SW }; +shape_t *sides[] = { north_side, east_side, south_side, west_side }; + + struct { /* command line options */ int l; /* list available designs */ } opt; @@ -44,6 +87,81 @@ int yyerror (const char *fmt, ...) +shape_t *on_side (const shape_t shape1, const shape_t shape2) +/* + * Compute the side that *both* shapes are on. + * + * RETURNS: pointer to a side list on success + * NULL on error (e.g. shape on different sides) + * +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ +{ + int i, j, found; + + for (i=0; i 1) { + switch (i) { + case 0: return north_side; + case 1: return east_side; + case 2: return south_side; + case 3: return west_side; + default: return NULL; + } + } + } + } + + return NULL; +} + + + +int shape_distance (const shape_t s1, const shape_t s2) +/* + * Compute distance between two shapes which are located on the same side + * of the box. E.g. shape_distance(NW,N) == 2. + * + * RETURNS: distance in steps if ok + * -1 on error + * +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ +{ + int i; + int distance = -1; + shape_t *workside = on_side (s1, s2); + + if (!workside) return -1; + if (s1 == s2) return 0; + + for (i=0; i -1) { + ++distance; + break; + } + } + else { + if (distance > -1) + ++distance; + } + } + + if (distance > 0 && distance < SHAPES_PER_SIDE) + return distance; + else + return -1; +} + + + void usage (FILE *st) { fprintf (st, "Usage: %s [options] [infile [outfile]]\n", PROJECT); @@ -129,9 +247,47 @@ int process_commandline (int argc, char *argv[]) } } while (oc != EOF); + /* + * If no config file has been specified, try getting it from other + * locations. (1) contents of BOXES environment variable (2) file + * ~/.boxes. If neither file exists, complain and die. + */ if (yyin == stdin) { - fprintf (stderr, "%s: Could not find config file\n", PROJECT); - return 1; + char *s = getenv ("BOXES"); + if (s) { + f = fopen (s, "r"); + if (f == NULL) { + fprintf (stderr, "%s: Couldn\'t open config file \'%s\' " + "for input (taken from environment).\n", PROJECT, s); + return 1; + } + yyfilename = (char *) strdup (s); + yyin = f; + } + else { + char buf[PATH_MAX]; /* to build file name */ + s = getenv ("HOME"); + if (s) { + strncpy (buf, s, PATH_MAX); + buf[PATH_MAX-1-7] = '\0'; /* ensure space for /.boxes */ + strcat (buf, "/.boxes"); + f = fopen (buf, "r"); + if (f) { + yyfilename = (char *) strdup (buf); + yyin = f; + } + else { + fprintf (stderr, "%s: Could not find config file.\n", + PROJECT); + return 1; + } + } + else { + fprintf (stderr, "%s: Environment variable HOME must point to " + "the user\'s home directory.\n", PROJECT); + return 1; + } + } } return 0; @@ -176,7 +332,7 @@ int main (int argc, char *argv[]) { extern int yyparse(); int rc; /* general return code */ - design_t *tmp; + design_t *tmp = (design_t *) rcsid; /* make rcsid stay */ rc = process_commandline (argc, argv); if (rc == 42) exit (EXIT_SUCCESS);