summaryrefslogtreecommitdiffstats
path: root/kxsldbg/kxsldbgpart/libqtnotfier/xsldbgthread.cpp
blob: d717a1f96c482f7cc3e6b569b96221754c6315d4 (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/***************************************************************************
                          xsldbgthread.cpp  -  description
                             -------------------
    begin                : Thu Dec 20 2001
    copyright            : (C) 2001 by keith
    email                : keith@linux
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <pthread.h>            /* need to create/work with process thread */
#include <errno.h>              /* need for EAGAIN */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>

#include <libxml/xmlerror.h>

#include "../libxsldbg/breakpoint.h"
#include "../libxsldbg/xsldbgmsg.h"
#include "../libxsldbg/xsldbgthread.h"
#include "../libxsldbg/qtnotifier2.h"

#ifdef HAVE_READLINE
#include <readline/readline.h>
#ifdef HAVE_HISTORY
#include <readline/history.h>
#endif
#endif

#define DEBUG_BUFFER_SIZE 500

static char inputBuffer[DEBUG_BUFFER_SIZE];
static char outputBuffer[DEBUG_BUFFER_SIZE];

/*the major structure to hold information about the process thread */
pthread_t mythread;

/* The reader for stdout */
pthread_t stdoutReaderThread;

FILE *stdoutIO = NULL;


/* -----------------------------------------------
   private functions
 ---------------------------------------------------*/

extern "C" {

/**
 * xsldbgGenericErrorFunc:
 * @ctx:  Is Valid
 * @msg:  Is valid
 * @...:  other parameters to use
 *
 * Handles print output from xsldbg and passes it to the application
 */
void
xsldbgGenericErrorFunc(void *ctx, const char *msg, ...);
xmlChar * qtXslDbgShellReadline(xmlChar * prompt);

}

/* -----------------------------------------------
   end functions
 ---------------------------------------------------*/

/* setup all application wide items */
int
xsldbgThreadInit(void)
{
    int result = 0;
    fprintf(stderr, "mainInit()\n");
    xsltSetGenericErrorFunc(0, xsldbgGenericErrorFunc);
    setThreadStatus(XSLDBG_MSG_THREAD_INIT);
    xsldbgSetAppFunc(qtNotifyXsldbgApp);
    xsldbgSetAppStateFunc(qtNotifyStateXsldbgApp); 
    xsldbgSetTextFunc(qtNotifyTextXsldbgApp);
    xsldbgSetReadlineFunc(qtXslDbgShellReadline);
    

    /* create the thread */
    if (pthread_create(&mythread, NULL, xsldbgThreadMain, NULL) != EAGAIN) {
      int counter;
      for (counter = 0; counter < 11; counter++){
	if (getThreadStatus() != XSLDBG_MSG_THREAD_INIT)
	  break;
	usleep(250000); /*guess that it will take at most 2.5 seconds to startup */
      }
      /* xsldbg should have started by now if it can */
      if (getThreadStatus() == XSLDBG_MSG_THREAD_RUN){
        fprintf(stderr, "Created thread\n");
	result++;
      }else
         fprintf(stderr, "Thread did not start\n");
    } else {
      fprintf(stderr, "Failed to create thread\n");
    }

    return result;
}


/* tell the thread to stop and free that memory !*/
void
xsldbgThreadFree(void)
{
  fprintf(stderr, "xsldbgThreadFree()\n");
    if (getThreadStatus() != XSLDBG_MSG_THREAD_DEAD)
    {
      int counter;
      fprintf(stderr, "Killing xsldbg thread\n");
      setThreadStatus(XSLDBG_MSG_THREAD_STOP);
      for (counter = 0; counter < 11; counter++){
	if (getThreadStatus() == XSLDBG_MSG_THREAD_DEAD)
	  break;
	usleep(250000); /*guess that it will take at most 2.5 seconds to stop */
      }
    }

}

const char *getFakeInput()
{
	return inputBuffer;
}


/* put text into standard input just like we had typed it */
int
fakeInput(const char *text)
{
    int result = 0;

    if (!text || (getInputReady() == 1) || (getThreadStatus() != XSLDBG_MSG_THREAD_RUN))
        return result;

    //    fprintf(stderr, "\nFaking input of \"%s\"\n", text);
    strncpy(inputBuffer, text, sizeof(inputBuffer));
    setInputReady(1);
    result++;
    return result;
}


/* use this function instead of the one that was in debugXSL.c */
/**
 * qtXslDbgShellReadline:
 * @prompt:  the prompt value
 *
 * Read a string
 *
 * Returns a copy of the text inputed or NULL if EOF in stdin found.
 *    The caller is expected to free the returned string.
 */
xmlChar *
qtXslDbgShellReadline(xmlChar * prompt)
{

  const char *inputReadBuff;

  static char last_read[DEBUG_BUFFER_SIZE] = { '\0' };

  if (getThreadStatus() != XSLDBG_MSG_THREAD_RUN)
    {
#ifdef HAVE_READLINE
      xmlChar *line_read;

      /* Get a line from the user. */
      line_read = (xmlChar *) readline((char *) prompt);

      /* If the line has any text in it, save it on the history. */
      if (line_read && *line_read) {
        add_history((char *) line_read);
        strncpy((char*)last_read, (char*)line_read, DEBUG_BUFFER_SIZE - 1);
      } else {
        /* if only <Enter>is pressed then try last saved command line */
        line_read = (xmlChar *) xmlMemStrdup(last_read);
      }
      return (line_read);
#else
      char line_read[DEBUG_BUFFER_SIZE];

      if (prompt != NULL)
        xsltGenericError(xsltGenericErrorContext, "%s", prompt);
      if (!fgets(line_read, DEBUG_BUFFER_SIZE - 1, stdin))
        return (NULL);
      line_read[DEBUG_BUFFER_SIZE - 1] = 0;
      /* if only <Enter>is pressed then try last saved command line */
      if ((strlen(line_read) == 0) || (line_read[0] == '\n')) {
        strcpy(line_read, last_read);
      } else {
        strcpy(last_read, line_read);
      }
      return (xmlChar *) xmlMemStrdup(line_read);
#endif

    }
  else{

    setInputStatus(XSLDBG_MSG_AWAITING_INPUT);
    notifyXsldbgApp(XSLDBG_MSG_AWAITING_INPUT, NULL);

    while (getInputReady() == 0){
      usleep(10000);
      /* have we been told to die */
      if (getThreadStatus() ==  XSLDBG_MSG_THREAD_STOP){
	fprintf(stderr, "About to stop thread\n");
	xslDebugStatus = DEBUG_QUIT;
	return NULL;
      }
    }

    setInputStatus(XSLDBG_MSG_READ_INPUT);
    inputReadBuff =  getFakeInput();
    if(inputReadBuff){
      notifyXsldbgApp(XSLDBG_MSG_READ_INPUT, inputReadBuff);
      return (xmlChar*)xmlMemStrdup(inputReadBuff);
    }else{
      return NULL;
    }
  }
}


xsldbgErrorMsg msg;
xsldbgErrorMsgPtr  msgPtr = &msg;
xmlChar *msgText = NULL;

int qtNotifyStateXsldbgApp(XsldbgMessageEnum type, int commandId,
			  XsldbgCommandStateEnum commandState, const char *text)
{
  int result = 0;
  msg.type = type;
  msg.commandId = commandId;
  msg.commandState = commandState;
  if (text != NULL)
    {
    msg.text = (xmlChar*)xmlMemStrdup(text);
    if (msg.text == NULL)
      return result; /* out of memory */
    }
   else
     msg.text = NULL;

  notifyXsldbgApp(XSLDBG_MSG_PROCESSING_RESULT, msgPtr);
  if (msg.text != NULL)
      {
	xmlFree(msg.text);
	msg.text = NULL;
      }

  result = 1; 
  return result;
}


int qtNotifyTextXsldbgApp(XsldbgMessageEnum type, const char *text)
{
  return qtNotifyStateXsldbgApp(type, -1, XSLDBG_COMMAND_NOTUSED, text);
}

char mainBuffer[DEBUG_BUFFER_SIZE];
static void xsldbgThreadCleanupTQt(void);


/* this is where the thread get to do all its work */
void *
xsldbgThreadMain(void *)
{
 // int defaultArgc = 2;
 // char *defaultArgv[2];
 // int i;

  if (getThreadStatus() != XSLDBG_MSG_THREAD_INIT){
    fprintf(stderr, "xsldbg thread is not ready to be started. Or one is already running.\n");
    return NULL; /* we can't start more than one thread of xsldbg */
  }

//  defaultArgv[0] = xmlMemStrdup("xsldbg");
//  defaultArgv[1] = xmlMemStrdup("--shell");
  /*
  defaultArgv[2] = xmlMemStrdup("xsldoc.xsl");
  defaultArgv[3] = xmlMemStrdup("xsldoc.xml");
  */
/*  for (i = 0; i < defaultArgc; i++){
    if (defaultArgv[i] == NULL){
      fprintf(stderr, "Start thread failed. Unable to create xsldbg arguments\n");
      return NULL;
    }
  }
*/
    xsldbgSetThreadCleanupFunc(xsldbgThreadCleanupTQt);
    setThreadStatus(XSLDBG_MSG_THREAD_RUN);
    setInputStatus(XSLDBG_MSG_AWAITING_INPUT);
    fprintf(stderr, "Starting thread\n");

    /* call the "main of xsldbg" found in debugXSL.c */
//    xsldbgMain(defaultArgc, defaultArgv);
    xsldbgMain(0,0);
    fprintf(stderr, "Stopping thread\n");
/*
  for (i = 0; i < defaultArgc; i++){
    xmlFree(defaultArgv[i]);
  }
*/

    setThreadStatus(XSLDBG_MSG_THREAD_DEAD);
    setInputStatus(XSLDBG_MSG_PROCESSING_INPUT);
    notifyXsldbgApp(XSLDBG_MSG_THREAD_DEAD, NULL);
    return NULL;
}



/* thread has died so cleanup after it not called directly but via
 notifyXsldbgApp*/
void
xsldbgThreadCleanupTQt(void)
{
    fprintf(stderr, "Thread has finished\n");
    if (getThreadStatus() == XSLDBG_MSG_THREAD_RUN)
      {
	xsldbgThreadFree();
      }
    /* its safe to modify threadStatus as the thread is now dead */
    setThreadStatus(XSLDBG_MSG_THREAD_DEAD);
}



void *
xsldbgThreadStdoutReader(void *data)
{
  if (!stdoutIO)
    return data;

  while (getThreadStatus() == XSLDBG_MSG_THREAD_RUN){
    if (fgets(outputBuffer, sizeof(outputBuffer -1), stdoutIO)){
      usleep(10000);
      strcat(outputBuffer, "\n");
      notifyTextXsldbgApp(XSLDBG_MSG_TEXTOUT, outputBuffer);
    }else{
      fprintf(stderr, "Unable to read from stdout from xsldbg\n");
      break;
    }
  }
  return data;
}