PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
args.c File Reference
#include "c.h"
#include <ctype.h>
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "indent_globs.h"
#include "indent.h"
Include dependency graph for args.c:

Go to the source code of this file.

Data Structures

struct  pro
 

Macros

#define INDENT_VERSION   "2.1.2"
 
#define PRO_SPECIAL   1 /* special case */
 
#define PRO_BOOL   2 /* boolean */
 
#define PRO_INT   3 /* integer */
 
#define ON   1 /* turn it on */
 
#define OFF   0 /* turn it off */
 
#define IGN   1 /* ignore it */
 
#define CLI   2 /* case label indent (float) */
 
#define STDIN   3 /* use stdin */
 
#define KEY   4 /* type (keyword) */
 
#define KEY_FILE   5 /* only used for args */
 
#define VERSION   6 /* only used for args */
 

Functions

static void scan_profile (FILE *f)
 
void add_typedefs_from_file (const char *str)
 
void set_profile (const char *profile_name)
 
static const char * eqin (const char *s1, const char *s2)
 
void set_defaults (void)
 
void set_option (char *arg)
 

Variables

const char * option_source = "?"
 
struct pro pro []
 

Macro Definition Documentation

◆ CLI

#define CLI   2 /* case label indent (float) */

Definition at line 67 of file args.c.

◆ IGN

#define IGN   1 /* ignore it */

Definition at line 66 of file args.c.

◆ INDENT_VERSION

#define INDENT_VERSION   "2.1.2"

Definition at line 54 of file args.c.

◆ KEY

#define KEY   4 /* type (keyword) */

Definition at line 69 of file args.c.

◆ KEY_FILE

#define KEY_FILE   5 /* only used for args */

Definition at line 73 of file args.c.

◆ OFF

#define OFF   0 /* turn it off */

Definition at line 63 of file args.c.

◆ ON

#define ON   1 /* turn it on */

Definition at line 62 of file args.c.

◆ PRO_BOOL

#define PRO_BOOL   2 /* boolean */

Definition at line 58 of file args.c.

◆ PRO_INT

#define PRO_INT   3 /* integer */

Definition at line 59 of file args.c.

◆ PRO_SPECIAL

#define PRO_SPECIAL   1 /* special case */

Definition at line 57 of file args.c.

◆ STDIN

#define STDIN   3 /* use stdin */

Definition at line 68 of file args.c.

◆ VERSION

#define VERSION   6 /* only used for args */

Definition at line 74 of file args.c.

Function Documentation

◆ add_typedefs_from_file()

void add_typedefs_from_file ( const char *  str)

Definition at line 335 of file args.c.

336{
337 FILE *file;
338 char line[BUFSIZ];
339
340 if ((file = fopen(str, "r")) == NULL) {
341 fprintf(stderr, "indent: cannot open file %s\n", str);
342 exit(1);
343 }
344 while ((fgets(line, BUFSIZ, file)) != NULL) {
345 /* Remove trailing whitespace */
346 line[strcspn(line, " \t\n\r")] = '\0';
347 add_typename(line);
348 }
349 fclose(file);
350}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
const char * str
void add_typename(const char *)
Definition: lexi.c:687
exit(1)

References add_typename(), exit(), fprintf, and str.

Referenced by set_option().

◆ eqin()

static const char * eqin ( const char *  s1,
const char *  s2 
)
static

Definition at line 233 of file args.c.

234{
235 while (*s1) {
236 if (*s1++ != *s2++)
237 return (NULL);
238 }
239 return (s2);
240}
char * s1
char * s2

References s1, and s2.

Referenced by set_option().

◆ scan_profile()

static void scan_profile ( FILE *  f)
static

Definition at line 198 of file args.c.

199{
200 int comment, i;
201 char *p;
202 char buf[BUFSIZ];
203
204 while (1) {
205 p = buf;
206 comment = 0;
207 while ((i = getc(f)) != EOF) {
208 if (i == '*' && !comment && p > buf && p[-1] == '/') {
209 comment = p - buf;
210 *p++ = i;
211 } else if (i == '/' && comment && p > buf && p[-1] == '*') {
212 p = buf + comment - 1;
213 comment = 0;
214 } else if (isspace((unsigned char)i)) {
215 if (p > buf && !comment)
216 break;
217 } else {
218 *p++ = i;
219 }
220 }
221 if (p != buf) {
222 *p++ = 0;
223 if (verbose)
224 printf("profile: %s\n", buf);
226 }
227 else if (i == EOF)
228 return;
229 }
230}
void set_option(char *arg)
Definition: args.c:261
#define comment
Definition: indent_codes.h:49
int verbose
int i
Definition: isn.c:72
static char * buf
Definition: pg_test_fsync.c:72
#define printf(...)
Definition: port.h:244

References buf, comment, i, printf, set_option(), and verbose.

Referenced by set_profile().

◆ set_defaults()

void set_defaults ( void  )

Definition at line 246 of file args.c.

247{
248 struct pro *p;
249
250 /*
251 * Because ps.case_indent is a float, we can't initialize it from the
252 * table:
253 */
254 ps.case_indent = 0.0; /* -cli0.0 */
255 for (p = pro; p->p_name; p++)
256 if (p->p_type != PRO_SPECIAL)
257 *p->p_obj = p->p_default;
258}
#define PRO_SPECIAL
Definition: args.c:57
struct parser_state ps
float case_indent
Definition: indent_globs.h:322
Definition: args.c:86
int p_type
Definition: args.c:88
const char * p_name
Definition: args.c:87
int * p_obj
Definition: args.c:91
int p_default
Definition: args.c:89

References parser_state::case_indent, pro::p_default, pro::p_name, pro::p_obj, pro::p_type, PRO_SPECIAL, and ps.

Referenced by main().

◆ set_option()

void set_option ( char *  arg)

Definition at line 261 of file args.c.

262{
263 struct pro *p;
264 const char *param_start;
265
266 arg++; /* ignore leading "-" */
267 for (p = pro; p->p_name; p++)
268 if (*p->p_name == *arg && (param_start = eqin(p->p_name, arg)) != NULL)
269 goto found;
270 errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
271found:
272 switch (p->p_type) {
273
274 case PRO_SPECIAL:
275 switch (p->p_special) {
276
277 case IGN:
278 break;
279
280 case CLI:
281 if (*param_start == 0)
282 goto need_param;
283 ps.case_indent = atof(param_start);
284 break;
285
286 case STDIN:
287 if (input == NULL)
288 input = stdin;
289 if (output == NULL)
290 output = stdout;
291 break;
292
293 case KEY:
294 if (*param_start == 0)
295 goto need_param;
296 add_typename(param_start);
297 break;
298
299 case KEY_FILE:
300 if (*param_start == 0)
301 goto need_param;
302 add_typedefs_from_file(param_start);
303 break;
304
305 case VERSION:
306 printf("pg_bsd_indent %s (based on FreeBSD indent)\n", INDENT_VERSION);
307 exit(0);
308
309 default:
310 errx(1, "set_option: internal error: p_special %d", p->p_special);
311 }
312 break;
313
314 case PRO_BOOL:
315 if (p->p_special == OFF)
316 *p->p_obj = false;
317 else
318 *p->p_obj = true;
319 break;
320
321 case PRO_INT:
322 if (!isdigit((unsigned char)*param_start)) {
323 need_param:
324 errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name);
325 }
326 *p->p_obj = atoi(param_start);
327 break;
328
329 default:
330 errx(1, "set_option: internal error: p_type %d", p->p_type);
331 }
332}
#define VERSION
Definition: args.c:74
#define KEY_FILE
Definition: args.c:73
static const char * eqin(const char *s1, const char *s2)
Definition: args.c:233
#define OFF
Definition: args.c:63
void add_typedefs_from_file(const char *str)
Definition: args.c:335
#define CLI
Definition: args.c:67
#define KEY
Definition: args.c:69
#define PRO_INT
Definition: args.c:59
#define STDIN
Definition: args.c:68
const char * option_source
Definition: args.c:76
#define IGN
Definition: args.c:66
#define PRO_BOOL
Definition: args.c:58
#define INDENT_VERSION
Definition: args.c:54
void errx(int eval, const char *fmt,...)
Definition: err.c:58
FILE * input
FILE * output
void * arg
int p_special
Definition: args.c:90

References add_typedefs_from_file(), add_typename(), arg, parser_state::case_indent, CLI, eqin(), errx(), exit(), IGN, INDENT_VERSION, input, KEY, KEY_FILE, OFF, option_source, output, pro::p_name, pro::p_obj, pro::p_special, pro::p_type, printf, PRO_BOOL, PRO_INT, PRO_SPECIAL, ps, STDIN, generate_unaccent_rules::stdout, and VERSION.

Referenced by scan_profile().

◆ set_profile()

void set_profile ( const char *  profile_name)

Definition at line 176 of file args.c.

177{
178 FILE *f;
179 char fname[MAXPGPATH];
180 static char prof[] = ".indent.pro";
181
182 if (profile_name == NULL)
183 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
184 else
185 snprintf(fname, sizeof(fname), "%s", profile_name + 2);
186 if ((f = fopen(option_source = fname, "r")) != NULL) {
187 scan_profile(f);
188 (void) fclose(f);
189 }
190 if ((f = fopen(option_source = prof, "r")) != NULL) {
191 scan_profile(f);
192 (void) fclose(f);
193 }
194 option_source = "Command line";
195}
static void scan_profile(FILE *f)
Definition: args.c:198
#define MAXPGPATH
#define snprintf
Definition: port.h:238

References MAXPGPATH, option_source, scan_profile(), and snprintf.

Referenced by main().

Variable Documentation

◆ option_source

const char* option_source = "?"

Definition at line 76 of file args.c.

Referenced by set_option(), and set_profile().

◆ pro

struct pro pro[]