File: message.c

package info (click to toggle)
evms 1.0.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,168 kB
  • ctags: 5,853
  • sloc: ansic: 87,317; makefile: 691; sh: 238
file content (173 lines) | stat: -rw-r--r-- 5,144 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
/*
 *
 *   Copyright (c) International Business Machines  Corp., 2001
 *
 *   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 2 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, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Module: message.c
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include <fullengine.h>
#include "message.h"
#include "engine.h"
#include "handlemgr.h"

/*
 * Global data
 */
engine_message_functions_t * ui_callbacks = NULL;

char message_buffer[1024];

int engine_user_message(int    * answer,
                        char * * choice_text,
                        char   * message_fmt,
                        ...) {
    int rc = 0;

    LOG_PROC_ENTRY();

    if (ui_callbacks != NULL) {
        if (ui_callbacks->user_message != NULL) {
            va_list args;

            strcpy(message_buffer, "Engine: ");

            va_start(args, message_fmt);
            vsprintf(message_buffer + strlen(message_buffer), message_fmt, args);
            va_end(args);

            /*
             * Put the message (and later, any answer) into the log.  Use the
             * CRITICAL level so that the message will always go to the log,
             * even though the message itself may not be CRITICAL.
             */
            LOG_CRITICAL("Message is: %s\n", message_buffer);
            rc = ui_callbacks->user_message(message_buffer, answer, choice_text);

            if (rc == 0) {
                if ((answer != NULL) && (choice_text != NULL)) {
                    LOG_CRITICAL("Answer is: \"%s\"\n", choice_text[*answer]);
                }
            }
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


int plugin_user_message(plugin_record_t * plugin,
                        int             * answer,
                        char          * * choice_text,
                        char            * message_fmt,
                        ...) {
    int rc = 0;

    LOG_PROC_ENTRY();

    if (ui_callbacks != NULL) {
        if (ui_callbacks->user_message != NULL) {
            va_list args;

            strcpy(message_buffer, plugin->short_name);
            strcat(message_buffer, ": ");

            va_start(args, message_fmt);
            vsprintf(message_buffer + strlen(message_buffer), message_fmt, args);
            va_end(args);

            /*
             * Put the message (and later, any answer) into the log.  Use the
             * CRITICAL level so that the message will always go to the log,
             * even though the message itself may not be CRITICAL.
             */
            LOG_CRITICAL("Message is: %s\n", message_buffer);
            rc = ui_callbacks->user_message(message_buffer, answer, choice_text);

            if (rc == 0) {
                if ((answer != NULL) && (choice_text != NULL)) {
                    LOG_CRITICAL("Answer is: \"%s\"\n", choice_text[*answer]);
                }
            }
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}


int plugin_user_communication(void                * object_instance,
                              char                * message_text,
                              option_desc_array_t * options){
    int rc = 0;

    LOG_PROC_ENTRY();

    if (ui_callbacks != NULL) {
        if (ui_callbacks->user_communication != NULL) {

            task_context_t * task = calloc(1, sizeof(task_context_t));

            if (task != NULL) {
                task_handle_t task_handle;

                /*
                 * Fill in the appropriate task fields.  The rest are
                 * left 0 (NULL).
                 */

                task->plugin = NULL;
                task->object = object_instance;
                task->action = EVMS_Task_Message;

                task->option_descriptors = options;

                /* Make a handle for the task. */
                rc = create_handle(task,
                                   TASK_TAG,
                                   &task_handle);

                if (rc == 0) {

                    /* Call the UI callback. */
                    rc = ui_callbacks->user_communication(message_text, task_handle);

                } else {
                    LOG_WARNING("create_handle() returned error %d.\n", rc);
                }

                free(task);

            } else {
                LOG_CRITICAL("Memory allocation of task_context_t failed.\n");
                rc = ENOMEM;
            }
        }
    }

    LOG_PROC_EXIT_INT(rc);
    return rc;
}