-
Notifications
You must be signed in to change notification settings - Fork 0
/
srvtool.cc
1953 lines (1637 loc) · 36.1 KB
/
srvtool.cc
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// srvtool.cc
// include the survey class hierarchy
#include <iostream>
#include <curses.h>
#include "survey.h"
#include "queue.h"
#include "result.h"
// includes needed mostly for curses stuff
#define _BSD_SIGNALS 1
#include <fcntl.h>
#include <signal.h>
// #include <sgtty.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#define NO_RESIZE 0
#define AFTER_RESIZE 1
#define ESCAPE_KEY 27
#define PREV_KEY 16
#define NEXT_KEY 14
/*
** prototypes to keep the compiler quiet
*/
extern "C" {
int sleep(unsigned);
int usleep(unsigned);
void exit(int);
int close(int);
int getuid();
// int read(int, char*, int);
#ifndef __sgi
#ifndef __linux
// int printw(char *, ...);
// int ioctl(int, int, caddr_t);
int stty(int, struct sgttyb *);
#endif
#endif
// int getuid();
};
void adjust_window(int dummy);
void drawIntroScreen();
void drawQueryScreen();
int getAnswer();
void setToPrevious();
void setToNext();
void updateExcludes();
int excluded_query();
int excluded_section();
void moveAhead1();
extern void smart_box(WINDOW *, int, int, int, int);
extern char dialog(WINDOW *, int, int);
extern void helpBox(WINDOW *, int, int);
extern int countQueries(P_SURVEY, P_QUERY[10][50], int *, int *);
/*
** end of prototypes
*/
enum QSTATE {
NORMAL, IN_INTRO, IN_HELP,
IN_QUERY, IN_CHOOSER, IN_COMMENT,
IN_TEXT, IN_RANKER, IN_SHORT_TEXT
};
const char * COMMENTMSG = "Please enter your comment (press control-d to exit):";
/*
** An evil mess of global variables
*/
WINDOW *winMain;
WINDOW *winQuery;
int curSectionNum, curQueryNum;
int questionNumber = 1, maxQuestions;
char result_file_name[256];
char *result_file_dir = "/tmp";
P_SURVEY theSurvey;
P_RESULT theResults;
P_QUERY allQueries[10][50];
P_QUERY curQuery;
P_ANSWER thisAnswer;
P_SECTION thisSection;
P_RESPONSE thisResponse;
P_RESPONSE allResponses[128];
QSTATE curState = NORMAL;
int caughtResize = 0;
char *thoseExcluded[128];
int numExcluded = 0;
int qpSection[20];
int numSections;
int PrevOrNext = 0;
/*
** end of globals
*/
struct sigvec sv_resize;
void
saveResults()
{
FILE *fp;
int i;
int raw_loc = questionNumber;
int tmpSnum, tmpQnum;
theResults->setExcluded(numExcluded, thoseExcluded);
/*
** add all of the responses so far to the RESULTS list
** and then write the data out to disk
*/
tmpSnum = curSectionNum;
tmpQnum = curQueryNum;
curSectionNum = 1;
curQueryNum = 1;
theSurvey->gotoSection(curSectionNum);
thisSection = theSurvey->get_current_section();
thisSection->gotoQuery(curQueryNum);
for (i =0; i < maxQuestions; i++)
{
if (!allResponses[i])
{
allResponses[i] = new RESPONSE(DUMMY);
}
else if (excluded_section() || excluded_query())
{
allResponses[i]->setType(DUMMY);
}
theResults->addResponse(allResponses[i], i);
if (i < (maxQuestions-1))
moveAhead1();
}
theResults->setBookmark(tmpSnum, tmpQnum, raw_loc);
fp = fopen(result_file_name, "w");
if (fp)
{
theResults->writeToDisk(fp);
fclose(fp);
}
}
int
drawPage(char *txt, int n)
{
/*
** draw the all the text that will fit in the current screen
** return the last character printed so the calling function
** can use it as an index for the following page.
*/
int i;
int y, x;
for (i = n; i < strlen(txt); i++)
{
printw("%c", txt[i]);
getyx(winMain, y, x);
if (y > (LINES - 4))
{
return i+1;
}
}
return 0;
}
void
drawIntroScreen()
{
char ch;
char *introtext = theSurvey->getIntro();
int i;
int last_char;
clear();
last_char = 1;
while (last_char)
{
clear();
move(2, 0);
if (last_char == 1)
last_char = 0;
last_char = drawPage(introtext, last_char);
smart_box(winMain, 0, 0, LINES - 1, COLS);
move(LINES-2, 27);
printw("Press the ");
standout();
printw("space bar");
standend();
printw(" to continue.");
move(LINES-2, 42);
refresh();
ch = wgetch(winMain);
while (ch != 32)
{
if (ch == 8 || ch == 127 ||
ch == 'b' || ch == 'p' ||
ch == 'B' || ch == 'P' )
{
last_char = 1;
ch = 32;
}
else
{
beep();
ch = wgetch(winMain);
}
}
}
if (caughtResize)
{
caughtResize = 0;
drawIntroScreen();
}
}
void
drawInfoScreen()
{
FILE *info_fp = fopen("./srv.info", "r");
int result;
int cnt, last_char;
char ch, tmp;
char infoText[8192];
result = fscanf(info_fp, "%c", &tmp);
cnt = 0;
while (result != EOF)
{
infoText[cnt++] = tmp;
result = fscanf(info_fp, "%c", &tmp);
}
fclose(info_fp);
infoText[cnt] = NULL;
last_char = 1;
while (last_char)
{
clear();
move(1, 0);
if (last_char == 1)
last_char = 0;
last_char = drawPage(infoText, last_char);
smart_box(winMain, 0, 0, LINES-1, COLS);
move(LINES-2, 27);
printw("Press the ");
standout();
printw("space bar");
standend();
printw(" to continue.");
move(LINES-2, 42);
refresh();
ch = wgetch(winMain);
while (ch != 32)
{
if (ch == 8 || ch == 127)
{
last_char = 1;
ch = 32;
}
else
{
beep();
ch = wgetch(winMain);
}
}
}
if (caughtResize)
{
caughtResize = 0;
drawInfoScreen();
}
}
void
drawThankyouScreen()
{
FILE *thanks_fp = fopen("./srv.thanks", "r");
int result;
char ch, tmp;
result = fscanf(thanks_fp, "%c", &tmp);
printf("\n");
while (result != EOF)
{
printf("%c", tmp);
result = fscanf(thanks_fp, "%c", &tmp);
}
printf("\n");
fclose(thanks_fp);
}
void
drawQueryScreen()
{
char *qname, *qtext;
int len;
wclear(winQuery);
clear();
smart_box(winMain, 0, 0, LINES - 1, COLS);
qname = thisSection->get_current_query_name();
standout();
move(0, 3);
printw("%s", qname);
move(0, 70);
printw("%2d/%2d", questionNumber, maxQuestions);
standend();
wmove(winQuery, 1, 0);
qtext = thisSection->get_current_question()->gettext();
wprintw(winQuery, "%s", qtext);
move(LINES - 2, 3);
standout();
standend();
switch (curState)
{
case IN_CHOOSER:
printw("Press ");
standout();
printw("ESC");
standend();
printw("ape for ");
printw("HELP");
break;
case IN_TEXT:
printw("Press ");
standout();
printw("ESC");
standend();
printw("ape for ");
printw("HELP");
move(LINES - 2, 54);
printw("Press ");
standout(); printw("control-d"); standend();
printw(" to exit");
break;
case IN_SHORT_TEXT:
printw("Please type in your response and press RETURN.");
move(LINES - 1, 3);
printw("Use Backspace/Delete to correct your mistakes.");
break;
case IN_RANKER:
printw("Rank the items by pressing digits, highest to lowest");
move(LINES - 1, 3);
printw("Press the ");
standout();
printw("RETURN");
standend();
printw(" key when finished.");
break;
default:
exit(1);
}
refresh();
wrefresh(winQuery);
delete (qtext);
delete (qname);
}
void
drawCommentScreen()
{
char *qname;
int len;
wclear(winQuery);
clear();
smart_box(winMain, 0, 0, LINES - 1, COLS);
qname = thisSection->get_current_query_name();
len = strlen(qname);
standout();
move(0, 3);
printw("%s", qname);
standend();
wmove(winQuery, 1, 0);
wprintw(winQuery, "Please enter your comment (press control-d to exit):");
move(LINES - 2, 3);
printw("Press ");
standout();
printw("ESC");
standend();
printw("ape for HELP");
refresh();
wrefresh(winQuery);
delete (qname);
}
void die(int code)
{
signal(SIGINT, SIG_IGN);
mvcur(0, COLS-1, LINES-1, 0);
endwin();
exit(0);
}
void
displayChoices(int y, int numChoices, P_tp_choice choices, int selected[])
{
int i, l, j;
int hON;
char txt[1024];
wmove(winQuery, y, 0);
for (i = 0; i < numChoices; i++)
{
strcpy(txt, choices[i].text);
l = strlen(txt);
wprintw(winQuery, " ");
if (selected[i])
{
wstandout(winQuery);
hON = 1;
wprintw(winQuery, "%1d. ", i+1);
for (j = 0; j < l; j++)
{
if (txt[j] == 10)
hON = 0;
else
if (txt[j] != ' ')
hON = 1;
if (hON)
wstandout(winQuery);
else
wstandend(winQuery);
wprintw(winQuery, "%c", txt[j]);
}
wstandend(winQuery);
hON = 0;
wprintw(winQuery, "\n");
}
else
{
wprintw(winQuery, "%1d. %s\n", i+1, txt);
}
if (selected[i])
wstandend(winQuery);
}
}
void
displayRankedItems(int y, int n, P_tp_choice choices,
int selected[], QUEUE<int> *rQueue)
{
int i, j;
int tx, ty;
QUEUE<int> tQueue(MAX_CHOICES);
int marked[MAX_CHOICES];
for (i = 0; i < MAX_CHOICES; i++)
marked[i] = 0;
wmove(winQuery, y, 0);
while ((j = rQueue->next()))
{
wprintw(winQuery, " ");
wstandout(winQuery);
tQueue.add(j);
wprintw(winQuery, "%1d. %s\n", j, choices[j-1].text);
marked[j-1] = 1; // mark this one as already being printed
wstandend(winQuery);
}
for (i = 0; i < n; i++)
{
if (!marked[i])
wprintw(winQuery, " %1d. %s\n", i+1, choices[i].text);
}
// put the original queue back together again
while ((j = tQueue.next()))
rQueue->add(j);
}
void
redrawTextScreen(const char *header_text, char *iText)
{
int i;
wmove(winQuery, 0, 0);
wprintw(winQuery, "\n%s\n\n", header_text);
for (i = 0; i < strlen(iText); i++)
wprintw(winQuery, "%c", iText[i]);
wrefresh(winQuery);
}
char *
getText(const char *header_text, char *prev_text)
{
// a real basic text entry widget implementation
int numChars = 0;
int done = 0;
int xPos, yPos;
int xMax, yMax;
int xOrig, yOrig;
int i;
char ch;
char iText[4096];
// vars for doing the word wrap stuff
char *end_of_last_word;
char *tail_wrap;
wmove(winQuery, 1, 0);
wprintw(winQuery, "%s\n\n", header_text);
wrefresh(winQuery);
iText[0] = '\0';
getyx(winQuery, yOrig, xOrig);
if (prev_text)
{
numChars = strlen(prev_text);
strcpy(iText, prev_text);
redrawTextScreen(header_text, iText);
getyx(winQuery, yPos, xPos);
}
while (!done)
{
ch = wgetch(winQuery);
if (caughtResize)
{
caughtResize = 0;
redrawTextScreen(header_text, iText);
}
getyx(winQuery, yPos, xPos);
getmaxyx(winQuery, yMax, xMax);
if ((ch > 31) && (ch < 127)) // printable character?
{
if (numChars < 4093)
{
/*
** word wrap code
*/
if (xPos == (xMax - 1))
{
/*
** insert a newline, and bring down the last word with us if
** necessary.
*/
if (ch != 32)
{
end_of_last_word = strrchr(iText, ' ');
if (end_of_last_word)
{
tail_wrap = strdup(end_of_last_word+1);
*end_of_last_word++ = 10;
*end_of_last_word++ = 13;
while (*tail_wrap)
*end_of_last_word++ = *tail_wrap++;
*end_of_last_word++ = ch;
*end_of_last_word = '\0';
numChars += 2;
wmove(winQuery, 1, 0);
wclrtobot(winQuery);
redrawTextScreen(header_text, iText);
}
else
{
iText[numChars++] = 10;
iText[numChars++] = 13;
iText[numChars++] = ch;
iText[numChars] = '\0';
wprintw(winQuery, "\n%c", ch);
wrefresh(winQuery);
}
}
else
{
/*
** we ran into a space being typed in the last column. Simply
** add the lf/cr to string, and move the cursor to the next
** line.
*/
iText[numChars++] = 10;
iText[numChars++] = 13;
iText[numChars] = '\0';
wprintw(winQuery, "\n");
wrefresh(winQuery);
}
}
else
{
iText[numChars++] = ch;
iText[numChars] = '\0';
wprintw(winQuery, "%c", ch);
wrefresh(winQuery);
}
}
else
{
/*
** No more space for characters, beep and disregard the character
*/
beep();
wrefresh(winQuery);
}
}
else if (ch == 13)
{
if (numChars < 4093)
{
iText[numChars++] = 10;
iText[numChars++] = ch;
iText[numChars] = '\0';
wprintw(winQuery, "\n");
wrefresh(winQuery);
}
else
{
/*
** No more space for characters, beep and disregard the character
*/
beep();
wrefresh(winQuery);
}
}
else if (ch == ESCAPE_KEY)
{
helpBox(winQuery, LINES, COLS);
redrawTextScreen(header_text, iText);
wrefresh(winQuery);
}
else if (ch == 4) // control d
{
return strdup(iText);
}
else if (ch == PREV_KEY)
{
PrevOrNext = -1;
return strdup(iText);
}
else if (ch == NEXT_KEY)
{
PrevOrNext = 1;
return strdup(iText);
}
else if ((ch == 8) || (ch == 127)) // backspace/delete
{
if (numChars)
{
if (iText[--numChars] == 13) // a cr???
{
iText[numChars] = '\0';
iText[--numChars] = '\0';
wmove(winQuery, 1, 0);
wclrtobot(winQuery);
redrawTextScreen(header_text, iText);
}
else
{
iText[numChars] = '\0';
if ((xPos == 0) && (yPos > yOrig))
{
wmove(winQuery, yPos-1, (xMax - 1));
wdelch(winQuery);
wmove(winQuery, yPos-1, (xMax - 1));
}
else
{
wmove(winQuery, yPos, xPos-1);
wclrtoeol(winQuery);
}
}
wrefresh(winQuery);
}
}
else if (ch == killchar()) // kill line???
{
while (yPos - yOrig)
{
wmove(winQuery, yPos--, 0);
wclrtoeol(winQuery);
}
iText[0] = '\0';
numChars = 0;
wmove(winQuery, 1, 0);
wclrtobot(winQuery);
redrawTextScreen(header_text, iText);
}
}
return (char *) 0;
}
char *
getShortText()
{
// get some lines of text from the user.
int numChars = 0;
int numLines = 0;
int done = 0;
int xPos, yPos;
int xMax, yMax;
int xOrig, yOrig;
char ch;
char iText[4096];
iText[0] = '\0';
wprintw(winQuery, "\n\nYour Answer? ");
wrefresh(winQuery);
getyx(winQuery, yOrig, xOrig);
while (!done)
{
ch = wgetch(winQuery);
if (caughtResize)
{
caughtResize = 0;
wprintw(winQuery, "\n\nYour Answer? %s", iText);
wrefresh(winQuery);
}
getyx(winQuery, yPos, xPos);
getmaxyx(winQuery, yMax, xMax);
if ((ch > 31) && (ch < 127)) // printable character?
{
iText[numChars++] = ch;
iText[numChars] = '\0';
wprintw(winQuery, "%c", ch);
wrefresh(winQuery);
}
else if (ch == 13) // carriage return
{
return strdup(iText);
}
else if ((ch == 8) || (ch == 127)) // backspace/delete
{
if (numChars)
{
iText[--numChars] = '\0';
if ((xPos == 0) && (yPos > yOrig))
{
wmove(winQuery, yPos-1, (xMax -1));
}
else
{
wmove(winQuery, yPos, xPos-1);
wclrtoeol(winQuery);
}
wrefresh(winQuery);
}
}
else if (ch == 21)
{
while (yPos - yOrig)
{
wmove(winQuery, yPos--, 0);
wclrtoeol(winQuery);
}
iText[0] = '\0';
numChars = 0;
wmove(winQuery, yOrig, xOrig);
wclrtoeol(winQuery);
wrefresh(winQuery);
}
}
return (char *) 0;
}
void
ranker()
{
// user interface allowing the user to rank a list of items
// with dynamic feedback
int i, done, j;
char ch;
P_tp_choice choices = thisAnswer->getchoices();
int numToRank = thisAnswer->getnumch();
int numRanked = 0;
int ox, oy;
int xp, yp;
int selected[MAX_CHOICES];
int *copy_of_selected = new int[MAX_CHOICES];
QUEUE<int> rQueue(MAX_CHOICES);
QUEUE<int> tQueue(MAX_CHOICES);
for (i = 0; i < MAX_CHOICES; i++)
selected[i] = copy_of_selected[i] = 0;
getyx(winQuery, oy, ox);
displayRankedItems(oy+2, numToRank, choices, selected, &rQueue);
wprintw(winQuery, "\nRank Highest to Lowest : ");
wrefresh(winQuery);
done = 0;
while (!done)
{
ch = wgetch(winQuery);
i = ch - '1';
if ((ch == 8) || (ch == 127))
{
i = rQueue.pop();
if (i)
{
selected[i-1] = 0;
numRanked--;
}
if (numRanked == (numToRank-1))
{
i = rQueue.pop();
if (i)
{
selected[i-1] = 0;
numRanked--;
}
}
}
else if (ch == 21)
{
while ((i = rQueue.next()))
selected[i-1] = 0;
numRanked = 0;
}
else if (ch == 13)
{
done = (numRanked == numToRank);
}
else if ((i >= 0) && (i < numToRank))
{
if (selected[i])
{
numRanked--;
selected[i] = 0;
while ((j = rQueue.next()))
{
if (j != (i+1))
tQueue.add(j);
}
while ((i = tQueue.next()))
rQueue.add(i);
}
else if (numRanked < numToRank)
{
numRanked++;
selected[i] = 1;
rQueue.add(i+1);
// if the user has ranked n-1 of the n items, we can automagically
// select the last one for him/her.
if (numRanked == (numToRank-1))
for (i = 0; i < numToRank; i++)
if (!selected[i])
{
numRanked++;
selected[i] = 1;
rQueue.add(i+1);
}
}
}
if (caughtResize)
{
caughtResize = 0;
getyx(winQuery, oy, ox);
drawQueryScreen();
}
displayRankedItems(oy+2, numToRank, choices, selected, &rQueue);
wprintw(winQuery, "\nRank Highest to Lowest : ");
j = 0;
while ((i = rQueue.next()))
{
wprintw(winQuery, "%d", i);
j++;
if (j < numToRank)
wprintw(winQuery, ", ");
tQueue.add(i);
}
while ((i = tQueue.next()))
rQueue.add(i);
wclrtoeol(winQuery);
wrefresh(winQuery);
} /* while !done */
j = 0;
while ((i = rQueue.next())) {
copy_of_selected[j++] = i;
tQueue.add(i);
}
thisResponse->setData(j, copy_of_selected);
}
int
chooser(int *prev_selected)
{
int i, j; // temp iterator
int done; // user is done
char ch; // temp input char
char *tmpText;
int tmpLength;
P_tp_choice choices = thisAnswer->getchoices();
int numChoices = thisAnswer->getnumch();
int minSelect = thisAnswer->getminch();
int maxSelect = thisAnswer->getmaxch();