home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / list / nd110.lbr / ND.CZ / ND.C
Text File  |  1988-02-05  |  6KB  |  229 lines

  1. /****************************************************************/
  2. /*                nd.c                */
  3. /* A program to output a non-document textfile, page by page    */
  4. /*with a header containing FILENAME, PAGE# and each line also    */
  5. /*numbered.                            */
  6. /*                                */
  7. /* Usage:  A>nd d:filename.ext [$]                */
  8. /*                 ^          ^                    */
  9. /*            /           |                    */
  10. /*       Source file        OPTIONS                */
  11. /*                                */
  12. /*     OPTIONS:    L - No line numbers            */
  13. /*            P - Pause between pages            */
  14. /*            D - Draft mode (no header, pica type)    */
  15. /*            H - No header                */
  16. /****************************************************************/
  17. /*    main()        master control for nd.c            */
  18. /*    getParams()    finds line parameters and acts on them    */ 
  19. /*    abort()        inform user of ERROR and return        */    
  20. /*    openFile()    opens Source file            */
  21. /*    sendHeader()    sends header string to Epson printer    */
  22. /*    getLine()    reads line from SOURCE file        */
  23. /*    printLine()    prints line to line printer        */
  24. /****************************************************************/
  25. /*    88Feb05 BKB    Version 1.1.0 release            */    
  26. /*    88Jan30    BKB    Code initiated                */
  27. /****************************************************************/
  28.  
  29. #define TRUE 1
  30. #define FALSE 0
  31. #define ERROR -1
  32. #define SAME 0
  33. #define FLAG char
  34.  
  35. #define MAXLINES 60
  36. #define FORMFEED 12
  37. #define EOF 26
  38. #define ESC 27
  39. #define PICA 18     /* Change these values if not using Epson    */
  40. #define CONDENSED 15    /* compatible..                    */
  41.  
  42. #define VERSION "1.1.0"
  43. #define DATE "88Feb05"
  44.  
  45.     char line[120], fBuf[1024], fBuf2[1024], filename[15];
  46.  
  47.     FLAG done, formal, pagePause, lineNos, headerOut;
  48.  
  49.     int fileLine, pageLine, filePage;
  50.  
  51. main(argc, argv)
  52.     int argc;
  53.     char *argv[];
  54. {
  55.     done = FALSE;
  56.  
  57.     if (getParams(argc, argv) == ERROR) return;
  58.  
  59.     if (openFile(filename) == ERROR) {
  60.         fClose(fBuf);
  61.     return;
  62.     }
  63.  
  64.     fileLine = pageLine = filePage = 1;
  65.  
  66.     if (formal) sendHeader();
  67.  
  68.     while (!done) {
  69.     if (getLine() == ERROR) done = TRUE;
  70.         else {
  71.         printf("Printing page#%d line#%d\r", filePage, pageLine);
  72.         printLine();
  73.         fileLine++;
  74.         pageLine++;
  75.         if (pageLine > MAXLINES) {
  76.         pageLine = 1;
  77.         filePage++;
  78.             lprintf("%c", FORMFEED);
  79.         if (pagePause) {
  80.             printf("ready next page, press RETURN:  \r");
  81.             if (getChar() == ESC) return;
  82.         }
  83.         printf("                                    \r");
  84.         if (formal) sendHeader(); 
  85.         } /* new page */
  86.     } /* got line */
  87.     } /* while not done */
  88.  
  89.     lprintf("%c", FORMFEED); /* make it pretty */
  90.     if (formal) lprintf("%c", PICA); /* return to normality */
  91.     fClose(fBuf);
  92.     
  93.     printf("\n Exiting...");
  94. }
  95.  
  96. /****************************************************************/
  97. /*            getParams()                */
  98. /****************************************************************/
  99.  
  100. getParams(c, v)
  101.     int c;
  102.     char *v[];
  103. {
  104.     char on, temp[6];
  105.  
  106.     int i;
  107.  
  108.     printf("\nND version %s (%s)\n\n", VERSION, DATE);
  109.  
  110.     lineNos = formal = headerOut = TRUE;    /* initialize defaults    */
  111.     pagePause = FALSE;
  112.  
  113.     if (c < 2) {
  114.         abort();
  115.     return(ERROR);
  116.     }
  117.  
  118.     strCpy(filename, v[1]);
  119.  
  120.     if (c == 2) return(TRUE);
  121.  
  122.     on = 3;
  123.     while (on <= c) {
  124.     if (index(v[on-1], "$") != ERROR) {    /* found options list    */
  125.         strCpy(temp, v[on-1]);    /* easier to work with    */
  126.         for (i=1; i <= (strLen(temp)-1); i++) {
  127.         switch(toUpper(temp[i])) {
  128.             case 'L':
  129.             if (!lineNos) break;
  130.             printf("Line numbers OFF\n");
  131.             lineNos = FALSE;
  132.             break;
  133.             
  134.             case 'P':
  135.             if (pagePause) break;
  136.             printf("Page pause ON\n");
  137.             pagePause = TRUE;
  138.             break;
  139.           
  140.             case 'D':
  141.             if (!formal) break;
  142.             printf("Draft mode ON\n");
  143.             formal = FALSE;
  144.             break;
  145.         
  146.             case 'H':
  147.             if (!headerOut) break;
  148.             printf("Header OFF\n");
  149.             headerOut = FALSE;
  150.             break;
  151.  
  152.             default:
  153.             printf("?option '$%c' unknown -- Ignored\n",
  154.                 temp[i]);
  155.         } /* switch */
  156.         } /* for */
  157.     } /* if */
  158.     on++;
  159.     } /* while */
  160.     return(TRUE);
  161. }
  162.  
  163. /****************************************************************/
  164. /*            abort()                    */
  165. /****************************************************************/
  166.  
  167. abort() {
  168.     printf(" Illegal parameters passed!\n\n");
  169.  
  170.     printf("Proper usage:  A>nd d:fname.ext [$LPDH]\n");
  171.     printf("                         ^       ^\n");
  172.     printf("                        /        |\n");
  173.     printf("                 Source file  options\n\n");
  174. }
  175.  
  176. /****************************************************************/
  177. /*            openFile()                */
  178. /****************************************************************/
  179.  
  180. openFile(fn1)
  181.     char *fn1;
  182. {
  183.     if (index(fn1, "*") != ERROR ||  index(fn1, "?") != ERROR) {
  184.         printf("\n\007 Wildcards not allowed!\n");
  185.         return(ERROR);
  186.     }
  187.     if ((fOpen(fn1, fBuf)) == ERROR) {
  188.     printf("\007\n Can not open %s!\n", fn1);
  189.     return(ERROR);
  190.     }
  191.  
  192.     return(TRUE);
  193. }
  194.  
  195. /****************************************************************/
  196. /*            sendHeader()                */
  197. /****************************************************************/
  198.  
  199. sendHeader() {
  200.     lprintf("%c", PICA); /* go to PICA for the header */
  201.     if (headerOut) {
  202.         lprintf("    ND file: %s    Page: %d\n\n",
  203.         filename, filePage);
  204.     }
  205.     lprintf("%c", CONDENSED);  /* go back to CONDENSED    */
  206. }
  207.  
  208. /****************************************************************/
  209. /*            getLine()                */
  210. /****************************************************************/
  211.  
  212. getLine()
  213. {
  214.     if (fscanf(fBuf, "%s", line) == ERROR) return(ERROR);
  215.     if (line[0] == '\0') strCpy(line, "\n");
  216.     return(TRUE);
  217. }
  218.  
  219. /****************************************************************/
  220. /*            printLine()                */
  221. /****************************************************************/
  222.  
  223. printLine() 
  224. {
  225.     if (formal && lineNos) lprintf("     %4d: %s", fileLine, line);
  226.     else lprintf("%s", line);
  227.     return(TRUE);
  228. }
  229.