BRL-CAD
cmd.c
Go to the documentation of this file.
1 /* C M D . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1987-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @addtogroup ray */
21 /** @{ */
22 /** @file librt/cmd.c
23  *
24  * Read and parse a viewpoint-control command stream.
25  *
26  * This module is intended to be common to all programs which read
27  * this type of command stream; the routines to handle the various
28  * keywords should go in application-specific modules.
29  *
30  */
31 /** @} */
32 
33 #include "common.h"
34 
35 #include <ctype.h>
36 #include <string.h>
37 #include "bio.h"
38 
39 #include "vmath.h"
40 #include "raytrace.h"
41 
42 char *
43 rt_read_cmd(register FILE *fp)
44 {
45  register int c;
46  register char *buf;
47  register int curpos;
48  register int curlen;
49 
50  curpos = 0;
51  curlen = 400;
52  buf = (char *)bu_malloc(curlen, "rt_read_cmd command buffer");
53 
54  do {
55  c = fgetc(fp);
56  if (c == EOF) {
57  c = '\0';
58  } else if (c == '#') {
59  /* All comments run to the end of the line */
60  while ((c = fgetc(fp)) != EOF && c != '\n')
61  ;
62  continue;
63  } else if (c == '\n') {
64  c = ' ';
65  } else if (c == ';') {
66  c = '\0';
67  } else if (c == '\\') {
68  /* Backslash takes next character literally.
69  * EOF detection here is not a problem, next
70  * pass will detect it.
71  */
72  c = fgetc(fp);
73  }
74  if (c != '\0' && curpos == 0 && isspace(c)) {
75  /* Dispose of leading white space.
76  * Necessary to slurp up what newlines turn into.
77  */
78  continue;
79  }
80  if (curpos >= curlen) {
81  curlen *= 2;
82  buf = (char *)bu_realloc(buf, curlen, "rt_read_cmd command buffer");
83  }
84  buf[curpos++] = c;
85  } while (c != '\0');
86  if (curpos <= 1) {
87  bu_free(buf, "rt_read_cmd command buffer (EOF)");
88  return (char *)0; /* EOF */
89  }
90  return buf; /* OK */
91 }
92 
93 
94 #define MAXWORDS 4096 /* Max # of args per command */
95 
96 
97 int
98 rt_do_cmd(struct rt_i *rtip, const char *ilp, register const struct command_tab *tp)
99 /* FUTURE: for globbing */
100 
101 
102 {
103  register int nwords; /* number of words seen */
104  char *cmd_args[MAXWORDS+1]; /* array of ptrs to args */
105  char *lp;
106  int retval;
107 
108  if (rtip)
109  RT_CK_RTI(rtip);
110 
111  lp = bu_strdup(ilp);
112 
113  nwords = bu_argv_from_string(cmd_args, MAXWORDS, lp);
114  if (nwords <= 0)
115  return 0; /* No command to process */
116 
117 
118  for (; tp->ct_cmd != (char *)0; tp++) {
119  if (cmd_args[0][0] != tp->ct_cmd[0] ||
120  /* the length of "n" is not significant, just needs to be big enough */
121  bu_strncmp(cmd_args[0], tp->ct_cmd, MAXWORDS) != 0)
122  continue;
123  if ((nwords >= tp->ct_min)
124  && ((tp->ct_max < 0) || (nwords <= tp->ct_max)))
125  {
126  retval = tp->ct_func(nwords, (const char **)cmd_args);
127  bu_free(lp, "rt_do_cmd lp");
128  return retval;
129  }
130  bu_log("rt_do_cmd Usage: %s %s\n\t%s\n",
131  tp->ct_cmd, tp->ct_parms, tp->ct_comment);
132  bu_free(lp, "rt_do_cmd lp");
133  return -1; /* ERROR */
134  }
135  bu_log("rt_do_cmd(%s): command not found\n", cmd_args[0]);
136  bu_free(lp, "rt_do_cmd lp");
137  return -1; /* ERROR */
138 }
139 
140 
141 /*
142  * Local Variables:
143  * mode: C
144  * tab-width: 8
145  * indent-tabs-mode: t
146  * c-file-style: "stroustrup"
147  * End:
148  * ex: shiftwidth=4 tabstop=8
149  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int(* ct_func)(const int, const char **)
Definition: raytrace.h:1891
#define RT_CK_RTI(_p)
Definition: raytrace.h:1833
const char * ct_parms
Definition: raytrace.h:1889
int rt_do_cmd(struct rt_i *rtip, const char *ilp, register const struct command_tab *tp)
Definition: cmd.c:98
int ct_max
max number of words in cmd
Definition: raytrace.h:1893
Header file for the BRL-CAD common definitions.
const char * ct_comment
Definition: raytrace.h:1890
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
int bu_strncmp(const char *string1, const char *string2, size_t n)
Definition: str.c:191
void * bu_realloc(void *ptr, size_t siz, const char *str)
char * rt_read_cmd(register FILE *fp)
Definition: cmd.c:43
int ct_min
min number of words in cmd
Definition: raytrace.h:1892
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
size_t bu_argv_from_string(char *argv[], size_t lim, char *lp)
Definition: argv.c:32
const char * ct_cmd
Definition: raytrace.h:1888
#define MAXWORDS
Definition: cmd.c:94
#define bu_strdup(s)
Definition: str.h:71