File: parse_options.c

package info (click to toggle)
tcl-syslog 2.0.2-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 504 kB
  • sloc: ansic: 742; sh: 400; tcl: 388; makefile: 27
file content (228 lines) | stat: -rw-r--r-- 7,957 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 *    syslog - Syslog loggin service for Tcl
 *    A Tcl interface to the POSIX syslog service.
 *
 *    Copyright (C) 2026 Massimo Manghi <mxmanghi@apache.org>
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>
#include <syslog.h>
#include "syslog.h"
#include "params.h"

extern SyslogGlobalStatus *g_status;
extern char* g_default_format;
extern int opt_class[];
extern int opt_code[];
extern const char* options[];

static void missing_option_value (Tcl_Interp* interp,char* cmd,Tcl_Obj* option)
{
    Tcl_Obj* error_code_list = Tcl_NewObj();
    Tcl_IncrRefCount(error_code_list);

    Tcl_ListObjAppendElement(interp, error_code_list, Tcl_NewStringObj("missing_argument_value", -1));
    Tcl_ListObjAppendElement(interp, error_code_list, option);
    Tcl_SetObjErrorCode(interp, error_code_list);

    Tcl_Obj* error_message = Tcl_NewStringObj("Missing option value for option '",-1);
    Tcl_IncrRefCount(error_message);

    Tcl_AppendObjToObj(error_message,option);
    Tcl_AppendStringsToObj(error_message,"' in ",cmd,NULL);

    Tcl_SetObjResult(interp,error_message);
    Tcl_DecrRefCount(error_message);

    Tcl_Obj* info_message = Tcl_NewStringObj("\n    (missing option value condition detected while parsing command ",-1);
    Tcl_IncrRefCount(info_message);
    Tcl_AppendStringsToObj(info_message,cmd,")",NULL);
    Tcl_AppendObjToErrorInfo(interp, info_message);

    Tcl_DecrRefCount(info_message);
    Tcl_DecrRefCount(error_code_list);
}

/*
 * parse_options
 *
 */

int parse_options (Tcl_Interp *interp, int objc, Tcl_Obj *CONST86 objv[],ParseArgsOptions* pao)
{
    int     index    = 1;
    int     fchanged = 0;
    int     option_idx;
    char*   tcl_command = Tcl_GetString(objv[0]);

    pao->status->facility = -1;
    if (pao->status->format != g_default_format) {
        Tcl_Free(pao->status->format);
        pao->status->format = (char *) g_default_format;
    }

    while (index < objc) {

        /* Read the docs! Tcl_GetIndexFromObj stores an 
         * error in the interpreter if the search
         * string is not found in the string array.
         * Setting it to NULL avoids this
         */

        if (Tcl_GetIndexFromObj(NULL,objv[index],options,"option",TCL_EXACT,&option_idx) == TCL_ERROR) {
            const char* argument = Tcl_GetString(objv[index]);

            if (strcmp(argument,"--") == 0) {
                /* end of options seqeunce, we proceed with the follwing opt */
                if (index == objc-1) {
                    missing_option_value(interp,tcl_command,objv[index]);
                    return ERROR;
                }
                return fchanged;
            } else if (argument[0] == '-') {
                pao->unhandled_opt_index = index;
                return INVALID_OPTION;
            } else {

                /* we hit a log message or log facility argument */

                return fchanged;
            }
            
        }

        if ((opt_class[option_idx] != UNDEFINED_OPTION_CLASS) && (opt_class[option_idx] & pao->option_class) == 0) {
            pao->unhandled_opt_index = index;
            return INVALID_OPTION_CLASS;
        }

        pao->modified_opt_class |= opt_class[option_idx];

        switch (option_idx) {
            case ident_idx:
            {
                if (index == objc-1) {
                    missing_option_value(interp,tcl_command,objv[index]);
                    return ERROR;
                }
                const char* ident = Tcl_GetString(objv[++index]);
                size_t len = strlen(ident);
                char *copy = (char *) Tcl_Alloc(len + 1);
                memcpy(copy,ident,len + 1);

                if (g_status->ident != NULL) {
                    Tcl_Free(g_status->ident);
                }
                g_status->ident = copy;
                fchanged++;
                pao->last_option_index = index;
                break;
            }
            case log_ndelay_idx:
            {
                g_status->options = g_status->options | LOG_NDELAY;
                fchanged++;
                pao->last_option_index = index;
                break;
            }
            case log_console_idx:
            {
                g_status->options = g_status->options | LOG_CONS;
                fchanged++;
                pao->last_option_index = index;
                break;
            }
            case log_pid_idx:
            {
                g_status->options = g_status->options | LOG_PID;
                fchanged++;
                pao->last_option_index = index;
                break;
            }
            case log_perror_idx:
            {
                g_status->options = g_status->options | LOG_PERROR;
                fchanged++;
                pao->last_option_index = index;
                break;
            }
            case priority_idx:
            case level_idx:
            {
                if (index == objc-1) {
                    missing_option_value(interp,tcl_command,objv[index]);
                    return ERROR;
                }

                const char *level_s = Tcl_GetString(objv[++index]);
                int p = level_cli_to_code(interp,level_s);
                if (p == ERROR) {
                    Tcl_SetObjResult(interp,Tcl_NewStringObj("Unknown level specified.",-1));
                    return ERROR;
                }
                pao->status->level = p;
                fchanged++;
                pao->last_option_index = index;
                break;
            }
            case facility_idx:
            {
                if (index == objc-1) {
                    missing_option_value(interp,tcl_command,objv[index]);
                    return ERROR;
                }

                const char *facility_s = Tcl_GetString(objv[++index]);
                int f = facility_cli_to_code(interp,facility_s);
                if (f == ERROR) {
                    Tcl_SetObjResult(interp,Tcl_NewStringObj("Unknown facility specified.",-1));
                    return ERROR;
                }
                if (pao->facility_is_private) {
                    pao->modified_opt_class |= PER_THREAD_OPTION_CLASS;
                    pao->status->facility = f;
                } else {
                    pao->modified_opt_class |= GLOBAL_OPTION_CLASS;
                    g_status->facility = f;
                }
                fchanged++;
                pao->last_option_index = index;
                break;
            }   
            case format_idx:
            {
                if (index == objc-1) {
                    missing_option_value(interp,tcl_command,objv[index]);
                    return ERROR;
                }

                const char *format_s = Tcl_GetString(objv[++index]);
                size_t len = strlen(format_s);
                char *copy = (char *) Tcl_Alloc(len + 1);
                memcpy(copy,format_s,len + 1);
                if ((pao->status->format != NULL) && (pao->status->format != g_default_format)) {
                    Tcl_Free(pao->status->format);
                }
                pao->status->format = copy;
                fchanged++;
                pao->last_option_index = index;
                break;
            }
        }
        index++;
    }
    return fchanged;
}