File: ctl_prt.c

package info (click to toggle)
bibview 2.2-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,252 kB
  • ctags: 1,588
  • sloc: ansic: 15,149; yacc: 1,145; makefile: 297; lex: 221; sh: 17
file content (701 lines) | stat: -rw-r--r-- 21,965 bytes parent folder | download | duplicates (3)
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
/*********************************************************************/
/*  bibView: Administration of BibTeX-Databases                      */
/*           (Verwaltung von BibTeX-Literaturdatenbanken)            */
/*                                                                   */
/*  Module:  ctl_prt.c                                               */
/*                                                                   */
/*             Print Control                                         */
/*             - Print BibTeX File                                   */
/*             - Print list                                          */
/*                                                                   */
/*  Author:  Holger Martin,  martinh@informatik.tu-muenchen.de       */
/*           Peter M. Urban, urban@informatik.tu-muenchen.de         */
/*                                                                   */
/*  History:                                                         */
/*    18.02.92  HM   created                                         */
/*    05.26.92       Version 1.0 released                            */
/*                                                                   */
/*  Copyright 1992 TU MUENCHEN                                       */
/*    See ./Copyright for complete rights and liability information. */
/*                                                                   */
/*********************************************************************/

#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Dialog.h>
#include <X11/Xaw/Box.h>
#include "FileNom.h"
#include "bibview.h"


/* macros and definitions */
/* ---------------------- */
#define MAXTEX  20000

#define BIB      0
#define LIST     1

/* imported global variables */
/* ------------------------- */
extern XtAppContext app_context;
extern Widget topLevel, mainMenu, desktop;
extern Pixmap questPixmap;
extern char *latexHeader, *latexFooter;
extern int max_linelen, max_fields;

/* exported global variables */
/* ------------------------- */

char *style_file;


/* local function prototypes */
/* ------------------------- */
static void getFilename (BibPtr bp, int type);
static void cancelGetFname (Widget w, XtPointer clientData, XtPointer callData);
static void getFnameListOk (Widget w, XtPointer clientData, XtPointer callData);
static void getFnameBibOk (Widget w, XtPointer clientData, XtPointer callData);

static void getListFilename (BibPtr bp);
static void cancelListGetFname (Widget w, XtPointer clientData, XtPointer callData);
static void getListFnameOk (Widget w, XtPointer clientData, XtPointer callData);


static Errcode printBegDoc (FILE *fp, String fname);
static Errcode printEndDoc (FILE *fp);
static Errcode printList      (FILE *fp, BibPtr bp, int type);
static Errcode printBibList   (FILE *fp, BibPtr bp, int type);
static Errcode printCard   (FILE *fp, CardDataPtr cd);
static void WriteLineToArray(char *inh);


/* local global variables */
/* ---------------------- */
static char texline[MAXTEX];


/*********************************************************************/
/* cprPrintBibCmd:                                                   */
/*    Callback function for command print in main menu               */
/*********************************************************************/
void 
cprPrintBibCmd (Widget widget, XtPointer clientData, XtPointer callData)
{
Errcode status;

   if ((status = guwSelectBib("printHead", cprPrintBib)) != OK)
      guwWarning(status);
}


/*********************************************************************/
/* cprPrintCmd:                                                      */
/*    Callback function for command print in main menu               */
/*********************************************************************/
void 
cprPrintCmd (Widget widget, XtPointer clientData, XtPointer callData)
{
BibPtr bp = (BibPtr)clientData;
Errcode status;

   if ((status = cprPrintBib(bp)) != OK)
      guwError(status);
}


/*********************************************************************/
/* cprPrintBib:                                                      */
/*    Callback function for command print in main menu               */
/*********************************************************************/
Errcode
cprPrintBib (BibPtr bp)
{
   getFilename(bp, BIB);
   return(OK);
}


/*********************************************************************/
/* cprPrintListCmd:                                                  */
/*    Callback function for command print in list window             */
/*********************************************************************/
void 
cprPrintListCmd(Widget widget, XtPointer clientData, XtPointer callData)
{
BibPtr bp = (BibPtr) clientData;
   
   getFilename(bp, LIST);
}


/*********************************************************************/
/* cprPrintList:                                                     */
/*    Print a list of cards to file in LaTeX format                  */
/*********************************************************************/
Errcode
cprPrintList (BibPtr bp, String fname, int type)
{
FILE *fp;
Errcode status;

   /* make .bak file of original */
   if (fname == NULL)
      return(ERR_NO_OPEN_FILE);
   if (cotBackupBeforeSave())
      glbMakeBackup(fname);

   if ((fp = fopen(fname, "w")) == NULL) 
      return(ERR_NO_OPEN_FILE);
   if (latexHeader != NULL) {
      fprintf(fp,"%s\n", latexHeader);
   }
   else {
     if ((status = printBegDoc(fp, bp->filename)) != OK) {
	fclose(fp);
	return(status);
     }
   }

   if (cotPrintAsBib()){
      if ((status = printBibList(fp, bp, type)) != OK) {
         fclose(fp);
         return(status);
         }
      }
   else
      if ((status = printList(fp, bp, type)) != OK) {
         fclose(fp);
         return(status);
         }

   if (latexFooter != NULL) {
      fprintf(fp,"%s\n", latexFooter);
   }
   else {
     if ((status = printEndDoc(fp)) != OK) {
	fclose(fp);
	return(status);
     }
   }

   fclose(fp);
   return(OK);
}


/*********************************************************************/
/* cprSaveListCmd:                                                   */
/*    Callback function for command save in list window              */
/*********************************************************************/
void 
cprSaveListCmd(Widget widget, XtPointer clientData, XtPointer callData)
{
BibPtr bp = (BibPtr)clientData;

   getListFilename(bp);
}

/*********************************************************************/
/* LOCAL FUNCTIONS                                                   */
/*********************************************************************/

/*********************************************************************/
/* getFilename:                                                      */
/*    Opens dialogbox for user to enter name of file                 */
/*********************************************************************/
static void
getFilename (BibPtr bp, int type)
{
static Widget fsbShell, fsbBox, fsbDialog;
Position dx, dy, x, y;

   XtVaGetValues(desktop,
                 XtNx, &dx,
                 XtNy, &dy, NULL);
   XtTranslateCoords(desktop,
                     (Position)dx + SUBWIN_MARGIN,
                     (Position)dy + SUBWIN_MARGIN,
                     &x, &y);

   fsbShell  = XtVaCreatePopupShell("fileSelectBoxShell",
                 topLevelShellWidgetClass, desktop,
                 XtNx, x, XtNy, y, NULL);
   fsbBox    = XtVaCreateManagedWidget("fileSelectBox",
		 boxWidgetClass, fsbShell, NULL);
               XtVaCreateManagedWidget("filePrintHead",
		 labelWidgetClass, fsbBox, 
		 XtNborderWidth, 0, NULL);
   fsbDialog = XtVaCreateManagedWidget("fileSelectBox",
                 fileNominatorWidgetClass, fsbBox, 
		 XtNborderWidth, 0, NULL);
   
   XtAddCallback(fsbDialog, XtNcancelCallback, cancelGetFname, fsbShell);
   if (type == LIST)
      XtAddCallback(fsbDialog, XtNselectCallback, getFnameListOk, bp);
   else    
      XtAddCallback(fsbDialog, XtNselectCallback, getFnameBibOk, bp);

   XtSetSensitive(mainMenu, FALSE);
   gubSetSensitive(NULL, FALSE);
   XtPopup(fsbShell, XtGrabNonexclusive);
}


/*********************************************************************/
/* getListFilename:                                                  */
/*    Opens dialogbox for user to enter name of file                 */
/*********************************************************************/
static void
getListFilename (BibPtr bp)
{
static Widget fsbShell, fsbBox, fsbDialog;
Position dx, dy, x, y;

   XtVaGetValues(desktop,
                 XtNx, &dx,
                 XtNy, &dy, NULL);
   XtTranslateCoords(desktop,
                     (Position)dx + SUBWIN_MARGIN,
                     (Position)dy + SUBWIN_MARGIN,
                     &x, &y);

   fsbShell  = XtVaCreatePopupShell("fileSelectBoxShell",
                 topLevelShellWidgetClass, desktop,
                 XtNx, x, XtNy, y, NULL);
   fsbBox    = XtVaCreateManagedWidget("fileSelectBox",
		 boxWidgetClass, fsbShell, NULL);
               XtVaCreateManagedWidget("fileListSaveHead",
		 labelWidgetClass, fsbBox, 
		 XtNborderWidth, 0, NULL);
   fsbDialog = XtVaCreateManagedWidget("fileSelectBox",
                 fileNominatorWidgetClass, fsbBox, 
		 XtNborderWidth, 0, NULL);


   XtAddCallback(fsbDialog, XtNcancelCallback, cancelListGetFname, fsbShell);
   XtAddCallback(fsbDialog, XtNselectCallback, getListFnameOk, (XtPointer)bp);

   XtSetSensitive(mainMenu, FALSE);
   gubSetSensitive(NULL, FALSE);
   XtPopup(fsbShell, XtGrabNonexclusive);
}


/*********************************************************************/
/* cancelGetFname:                                                   */
/*    Callback function for CANCEL button in filename box            */
/*********************************************************************/
static void
cancelGetFname (Widget w, XtPointer clientData, XtPointer callData)
{
Widget shell = (Widget)clientData;

   XtSetSensitive(mainMenu, TRUE);
   gubSetSensitive(NULL, TRUE);
   XtPopdown(shell);
}


/*********************************************************************/
/* cancelListGetFname:                                               */
/*    Callback function for CANCEL button in filename box            */
/*********************************************************************/
static void
cancelListGetFname (Widget w, XtPointer clientData, XtPointer callData)
{
Widget shell = (Widget)clientData;

   XtSetSensitive(mainMenu, TRUE);
   gubSetSensitive(NULL, TRUE);
   XtPopdown(shell);
}


/*********************************************************************/
/* getFnameListOk:                                                   */
/*    Callback function for OK button in filename box                */
/*********************************************************************/
static void
getFnameListOk (Widget w, XtPointer clientData, XtPointer callData)
{
BibPtr bp = (BibPtr) clientData;
String prtName;
int status;

   /* get filename and rename windows */
   prtName = (String)FileNominatorGetFullFileName(w);
   if (prtName == NULL){
      XtPopdown(XtParent(XtParent(w)));
      XtSetSensitive(mainMenu, TRUE);
      gubSetSensitive(NULL, TRUE);
      guwError(NO_VALID_FILENAME);
      return;
      }

   /* remove file select box */
   XtPopdown(XtParent(XtParent(w)));
   XtSetSensitive(mainMenu, TRUE);
   gubSetSensitive(NULL, TRUE);

   /* write out printfile to disk */
   if ((status = cprPrintList(bp, prtName, LIST)) != OK)
      guwError(status);

}


/*********************************************************************/
/* getFnameBibOk:                                                    */
/*    Callback function for OK button in filename box                */
/*********************************************************************/
static void
getFnameBibOk (Widget w, XtPointer clientData, XtPointer callData)
{
BibPtr bp = (BibPtr) clientData;
String prtName;
int status;

   /* get filename and rename windows */
   prtName = (String)FileNominatorGetFullFileName(w);
   if (prtName == NULL){
      XtPopdown(XtParent(XtParent(w)));
      XtSetSensitive(mainMenu, TRUE);
      gubSetSensitive(NULL, TRUE);
      guwError(NO_VALID_FILENAME);
      return;
      }


   /* remove file select box */
   XtPopdown(XtParent(XtParent(w)));
   XtSetSensitive(mainMenu, TRUE);
   gubSetSensitive(NULL, TRUE);

   /* write out printfile to disk */
   if ((status = cprPrintList(bp, prtName, BIB)) != OK)
      guwError(status);

}



/*********************************************************************/
/* getListFnameOk:                                                   */
/*    Callback function for OK button in filename box                */
/*********************************************************************/
static void
getListFnameOk (Widget w, XtPointer clientData, XtPointer callData)
{
BibPtr bp = (BibPtr)clientData;
char tmpfile[MAX_FILEPATHLEN];
String prtName, prtPath;
int status;

   /* get filename and rename windows */
   prtName = (String)FileNominatorGetFileName(w);
   if (prtName == NULL){
      XtPopdown(XtParent(XtParent(w)));
      XtSetSensitive(mainMenu, TRUE);
      gubSetSensitive(NULL, TRUE);
      guwError(NO_VALID_FILENAME);
      return;
      }

   prtPath = (String)FileNominatorGetFullFileName(w);
   if (prtPath == NULL){
      XtPopdown(XtParent(XtParent(w)));
      XtSetSensitive(mainMenu, TRUE);
      gubSetSensitive(NULL, TRUE);
      guwError(NO_VALID_FILENAME);
      return;
      }

   /* remove file select box */
   XtPopdown(XtParent(XtParent(w)));
   XtSetSensitive(mainMenu, TRUE);
   gubSetSensitive(NULL, TRUE);

   /* make .bak file of original */
   strcpy(tmpfile, prtPath);
   if (glbCheckPath(prtPath, prtName) != OK){
      XtPopdown(XtParent(XtParent(w)));
      XtSetSensitive(mainMenu, TRUE);
      gubSetSensitive(NULL, TRUE);
      guwError(BIF_EWRITE);
      return;
      }

   if (cotBackupBeforeSave())
      glbMakeBackup(prtPath);

   /* write out printfile to disk */
   if ((status = bifFileListWrite(bp, bp->lw->cardLst, prtPath)) != OK)
      guwError(status);
}

/*********************************************************************/
/* printBegDoc:                                                      */
/*    Print begin of LaTeX file                                      */
/*********************************************************************/
static Errcode
printBegDoc (FILE *fp, String fname)
{
#ifdef GERMAN
   fprintf(fp, "\\documentstyle[german]{article}\n");
   fprintf(fp, "\\begin{document}\n");
   fprintf(fp, "{\\large \\bf Inhalt der Datei %s -- \\today}\n", fname);
#else
   fprintf(fp, "\\documentstyle{article}\n");
   fprintf(fp, "\\begin{document}\n");
   fprintf(fp, "{\\large \\bf Content of file %s -- \\today}\n", fname);
#endif
   return(OK);
}


/*********************************************************************/
/* printEndDoc:                                                      */
/*    Print end   of LaTeX file                                      */
/*********************************************************************/
static Errcode
printEndDoc (FILE *fp)
{
   fprintf(fp, "\\end{document}\n");
   return(OK);
}


/*********************************************************************/
/* printBibList:                                                     */
/*    Print list of cards to file                                    */
/*********************************************************************/
static Errcode
printBibList (FILE *fp, BibPtr bp, int type)
{
CardListNode *list;
char tmpfile[MAX_FILEPATHLEN];
int j, zaehler;
   
   strcpy(tmpfile, bp->filepath);
   for (j=0; tmpfile[j] != '\0'; j++){
     if (tmpfile[j] == '.'){
      if (!strcmp(&tmpfile[j], ".bib")){
	 tmpfile[j]='\0';
	 break;
         }
      }
     }
   
   fprintf(fp, "\\nocite{");
   if (type==LIST){
     zaehler = 8;
     list = bp->lw->cardLst;
     if (list != NULL){
      zaehler += strlen(list->data->mainkey);
      fprintf(fp, "%s", list->data->mainkey),
      list = list->next; 
      while (list != NULL) {                 
	 if (zaehler + strlen(list->data->mainkey) + 2 > max_linelen){
            fprintf(fp, "}\n\\nocite{%s", list->data->mainkey);
            zaehler=8+strlen(list->data->mainkey);
            list = list->next; 
            }		
	 else {
	    zaehler += strlen(list->data->mainkey) + 1;
            fprintf(fp, ",%s", list->data->mainkey);
            list = list->next; 
	    }
         }
      }
      fprintf(fp, "}\n\n");
     }
   else
      fprintf(fp, "*}\n\n");
   fprintf(fp, "\\bibliographystyle{%s}\n\n", style_file),
   fprintf(fp, "\\bibliography{%s}\n\n", tmpfile);
   return(OK);
}


/*********************************************************************/
/* printList:                                                        */
/*    Print list of cards to file                                    */
/*********************************************************************/
static Errcode
printList (FILE *fp, BibPtr bp, int type)
{
CardListNode *list, *hlist;
CardListNode *cl = NULL;
Errcode status;

   if (type == BIB){
      /* make list of all cards in src bib */
      if ((status = dbtBuildList(bp->treeIdx,  &cl, bp->sortedby)) != DBT_OK) {
         return(status);
      }
      list = cl;
   }
   else
     list = bp->lw->cardLst;
  
   hlist = list;
   fprintf(fp, "\\begin{enumerate}\n"); 
   while (hlist != NULL) {                 
      if ((status = printCard(fp, hlist->data)) != OK)
	 return(status); 

      hlist = hlist->next; 
   }  
   fprintf(fp, "\\end{enumerate}\n"); 
   if (type == BIB){
      if ((status = dbtCardListDelete(&list)) != OK)
	 return(status);  
      list = NULL;
      }
   return(OK);
}


/*********************************************************************/
/* printCard:                                                        */
/*    Print card to file                                             */
/*********************************************************************/
static Errcode
printCard (FILE *fp, CardDataPtr cd)
{
UserFld *hufield;
FieldName i;


   fprintf(fp, "\\item %s  %s\n", cd->cardtypestr, cd->mainkey); 
   fprintf(fp, "\\begin{tabbing}\n");
   fprintf(fp, "AAAAAAAAAAAAAAAAAAAA\\= \\kill\n");
   
   for (i=0; i<max_fields; i++){
      if (!glbIsStringEmpty(cd->field[i])) {
         WriteLineToArray(cd->field[i]);
         fprintf(fp, "\\bf %s \\>  \\parbox[t]{25em}{%s} \\\\ \n",
		 glbFldToName(i), texline);
      }
   }

/*   print userdefined fields  */
   hufield = cd->ufield;
   while (hufield != NULL) {
      WriteLineToArray(hufield->fldData);
      fprintf(fp, "\\bf %s \\>  \\parbox[t]{25em}{%s}  \\\\ \n",
		   hufield->fldName, texline);
      hufield = hufield->next;
   }  
   fprintf(fp, "\\end{tabbing}\n");
   return(OK);
}


/*********************************************************************/
/* writeLineToArray:                                                 */
/*                                                                   */
/*********************************************************************/
static void 
WriteLineToArray (char *inh)
{
int idx;
int makro = 2;    /* weder Makro, noch String */
char *tmp;

  idx = 0;
  while (*inh == ' ' || *inh == '\t' || *inh == '\n') {
    texline[idx] = *inh; 
    inh++;
    idx++;
  }
  if (*inh == '@') {
    texline[idx] = ' '; 
    makro = 1;
  }
  else {
    texline[idx] = '{'; 
    idx++;
    texline[idx] = *inh; 
    makro = 0;
  }
  idx++;
  inh++;
  while (*inh != '\0') {
    if (*inh == '@') {
      tmp = inh;
      tmp++;
      if (*tmp == '#') {                  /* KONKATENATION */
        if (makro == 1) {
                                          /* letzter Teil war ein MAKRO */ 
          texline[idx] = ' '; idx++; 
          texline[idx] = '\\'; idx++; 
          texline[idx] = '#'; idx++; 
          texline[idx] = ' '; idx++; 
          tmp++;
          while (*tmp == ' ' || *tmp == '\t' || *tmp == '\n') {
            texline[idx] = *tmp; 
            idx++;
            tmp++;
          }
          if (*tmp == '@') {              /* naechster Teil ist ein MAKRO */
            texline[idx] = ' '; idx++; 
            makro = 1;
          }
          else {
            texline[idx] = '{'; idx++; 
            texline[idx] = *tmp; 
            idx++;
                                          /* naechster Teil ist ein STRING */
            makro = 0;
          }
        }
        else if (makro == 0) {
                                          /* letzter Teil war ein STRING */ 
          texline[idx] = '}'; idx++; 
          texline[idx] = ' '; idx++; 
          texline[idx] = '\\'; idx++; 
          texline[idx] = '#'; idx++; 
          texline[idx] = ' '; idx++; 
          tmp++;
          while (*tmp == ' ' || *tmp == '\t' || *tmp == '\n') {
            texline[idx] = *tmp; 
            idx++;
            tmp++;
          }
          if (*tmp == '@') {              /* naechster Teil ist ein MAKRO */
            texline[idx] = ' '; idx++; 
            makro = 1;
          }
          else {
            texline[idx] = '{'; idx++; 
            texline[idx] = *tmp; 
            idx++;
            makro = 0;
          }
        }  /* else if makro == 0 */
        inh = tmp;
      }
      else {                              /* AFFE nicht schreiben */
        texline[idx] = ' '; idx++; 
        makro = 1;
      }
    }      /* if @ */
    else {
      texline[idx] = *inh; 
      idx++;
    }
    inh++;
  }  /* while */
  if (makro == 0) {
    texline[idx] = '}'; idx++; 
  }
  texline[idx] = ','; idx++; 
  texline[idx] = ' '; idx++; 
  texline[idx] = '\0'; idx++; 
}