File: param.c

package info (click to toggle)
universal-ctags 6.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,612 kB
  • sloc: ansic: 158,498; sh: 8,621; lisp: 7,742; vhdl: 6,518; cpp: 2,583; perl: 2,578; python: 2,324; javascript: 2,054; cs: 1,193; lex: 1,015; sql: 897; makefile: 787; ruby: 764; php: 755; cobol: 741; f90: 566; ada: 559; asm: 509; yacc: 465; fortran: 412; xml: 405; objc: 289; tcl: 280; java: 173; erlang: 65; pascal: 58; ml: 49; awk: 44; haskell: 42
file content (136 lines) | stat: -rw-r--r-- 3,489 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 *
 *  Copyright (c) 2016, Red Hat, Inc.
 *  Copyright (c) 2016, Masatake YAMATO
 *
 *  Author: Masatake YAMATO <yamato@redhat.com>
 *
 *   This source code is released for free distribution under the terms of the
 *   GNU General Public License version 2 or (at your option) any later version.
 *
 */

#include "general.h"
#include "options.h"
#include "param.h"
#include "param_p.h"
#include "parse.h"

#include <string.h>

typedef struct sParamObject {
	paramDefinition *def;
	freeParamDefFunc free;
} paramObject;

struct paramControlBlock {
	paramObject *param;
	unsigned int count;
	langType owner;
};

extern struct paramControlBlock* allocParamControlBlock (parserDefinition *parser)
{
	struct paramControlBlock *pcb;

	pcb = xMalloc (1, struct paramControlBlock);
	pcb->param = xMalloc (parser->paramCount, paramObject);
	pcb->count = parser->paramCount;
	pcb->owner = parser->id;

	for (unsigned int i = 0; i < parser->paramCount; ++i)
	{
		paramObject *param = pcb->param + i;
		param->def = parser->paramTable + i;
		param->free = NULL;
	}

	return pcb;
}

extern void freeParamControlBlock (struct paramControlBlock* pcb)
{
	for (unsigned int i = 0; i< pcb->count; ++i)
	{
		if (pcb->param [i].free)
			pcb->param [i].free (pcb->param [i].def);
	}
	if (pcb->param)
		eFree (pcb->param);
	eFree (pcb);
}

extern int  defineParam (struct paramControlBlock* pcb, paramDefinition *def,
						 freeParamDefFunc freeParamDef)
{
	unsigned int id = pcb->count++;
	pcb->param = xRealloc (pcb->param, pcb->count, paramObject);
	pcb->param [id].def = def;
	pcb->param [id].free = freeParamDef;

	verbose ("Add param[%d] \"%s,%s\" to %s\n", id,
			 def->name, def->desc,
			 getLanguageName (pcb->owner));

	return id;
}

extern bool applyParam (struct paramControlBlock* pcb, const char *name, const char *args)
{
	for (unsigned int i = 0; i < pcb->count; i++)
	{
		paramDefinition *pdef = pcb->param[i].def;
		if (strcmp(pdef->name, name) == 0)
		{
			if (pdef->handleParam == NULL)
				return true;
			return pdef->handleParam (pcb->owner, name, args);
		}
	}
	const char *lang = getLanguageName (pcb->owner);
	error (FATAL, "no such parameter in %s: %s", lang, name);
	return false;
}

extern struct colprintTable * paramColprintTableNew (void)
{
	return colprintTableNew ("L:LANGUAGE", "L:NAME","L:DESCRIPTION", NULL);
}

extern void paramColprintAddParams (struct colprintTable *table,
									struct paramControlBlock* pcb)
{
	const char *lang = getLanguageName (pcb->owner);
	for (unsigned int i = 0; i < pcb->count; i++)
	{
		paramDefinition *pdef = pcb->param [i].def;
		struct colprintLine *line = colprintTableGetNewLine(table);

		colprintLineAppendColumnCString (line, lang);
		colprintLineAppendColumnCString (line, pdef->name);
		colprintLineAppendColumnCString (line, pdef->desc);
	}
}

static int paramColprintCompareLines (struct colprintLine *a , struct colprintLine *b)
{
	const char *a_parser = colprintLineGetColumn (a, 0);
	const char *b_parser = colprintLineGetColumn (b, 0);

	int r;
	r = strcmp (a_parser, b_parser);
	if (r != 0)
		return r;

	const char *a_name = colprintLineGetColumn (a, 1);
	const char *b_name = colprintLineGetColumn (b, 1);

	return strcmp(a_name, b_name);
}

extern void paramColprintTablePrint (struct colprintTable *table, bool noparser,
									bool withListHeader, bool machinable, FILE *fp)
{
	colprintTableSort (table, paramColprintCompareLines);
	colprintTablePrint (table, noparser? 1: 0, withListHeader, machinable, fp);
}