home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume14 / pc2unix / part01 next >
Encoding:
Text File  |  1990-07-15  |  8.5 KB  |  316 lines

  1. Newsgroups: comp.sources.misc
  2. subject: v14i007: pc2unix, file translation programs
  3. From: magnus@thep.lu.se (Magnus Olsson)
  4. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5.  
  6. Posting-number: Volume 14, Issue 7
  7. Submitted-by: magnus@thep.lu.se (Magnus Olsson)
  8. Archive-name: pc2unix/part01
  9.  
  10. To the moderator of Comp.sources.misc:
  11.  
  12. I wrote the following two small filters since I do a lot of file transfer
  13. between a PC and a Unix workstation. The filters convert files between the
  14. formats used for text files by Unix and MSDOS. They also take care of 
  15. messy things like control characters and extended ASCII characters.
  16.  
  17. Both programs are public domain. I hope they will prove themselves useful.
  18.  
  19.  
  20. +=====================================================+
  21. | Magnus Olsson                 | \e+        /_          |
  22. | Dept. of Theoretical Physics     |  \  Z      / q          |
  23. | University of Lund             |   >----<          |
  24. | Solvegatan 14 a        |  /      \===== g    |
  25. | S-223 62 LUND, Sweden     | /e-       \q          | 
  26. +===============================+=====================+
  27.  
  28.  
  29. ---- Cut Here and unpack ----
  30. #!/bin/sh
  31. # shar:    Shell Archiver  (v1.22)
  32. #    Packed Wed Jul 11 16:13:42 MET DST 1990 by lena!magnus
  33. #    from directory /usr/users/magnus/hack
  34. #
  35. #    Run the following text with /bin/sh to create:
  36. #      pc2unix.c
  37. #      unix2pc.c
  38. #      pc2unix.doc
  39. #
  40. sed 's/^X//' << 'SHAR_EOF' > pc2unix.c &&
  41. X/******************************************
  42. X ***            pc2unix.c               ***
  43. X ***                    ***
  44. X ***   A filter to convert text files   ***
  45. X ***     from MSDOS to Unix format.     ***
  46. X ***      (to be run under Unix)        ***
  47. X ***                    ***
  48. X ***      Public domain software    ***
  49. X ***                    ***
  50. X ***            Written by         ***
  51. X *** Magnus Olsson (magnus@thep.lu.se)    ***
  52. X ***          July 11, 1990        ***
  53. X ******************************************/
  54. X
  55. X/* 
  56. X   Switches: 
  57. X   -c: Pass control characters through unchanged
  58. X        (default: Replace with ^+letter)
  59. X   -h: Put a header in front of each input file (not stdin)
  60. X   -d: Replace extended characters (> 126) with a dot
  61. X   -e: Pass extended characters through unchanged
  62. X     (default: Replace with the char code in parentheses)
  63. X*/
  64. X
  65. X#include <stdio.h>
  66. X
  67. X#define CTRL_Z 26
  68. X#define TAB 9
  69. X#define CR  13
  70. X#define LF  10
  71. X
  72. X#define TRUE 1
  73. X#define FALSE 0
  74. X
  75. Xint pass_ctrl = FALSE,
  76. X    pass_ext  = FALSE,
  77. X    ext_dot   = FALSE,
  78. X    header    = FALSE;
  79. X
  80. X
  81. Xmain (argc,argv)
  82. X    int argc;
  83. X    char **argv;
  84. X    
  85. X{
  86. X    int i,arg;
  87. X    
  88. X    FILE *f;
  89. X
  90. X    arg = 0;
  91. X    /* Look for switches */
  92. X    while (++arg < argc) {
  93. X         if (argv [arg][0] == '-') /* Was it a switch? */
  94. X        switch (argv [arg][1]) {
  95. X            case 'c': pass_ctrl = TRUE; break;
  96. X        case 'e': pass_ext  = TRUE; break;
  97. X        case 'd': ext_dot = TRUE; break;
  98. X        case 'h': header = TRUE; break;
  99. X        default:  fprintf (stderr,"Error: Bad switch %s\n",argv [arg]);
  100. X              exit (1);
  101. X        }
  102. X    else
  103. X        break;
  104. X    }        
  105. X        
  106. X    if (arg >= argc) /* No filenames as parameters */
  107. X        process (stdin);
  108. X    else    
  109. X        for (i = arg; i < argc; ++i) {
  110. X        if (header)
  111. X                printf ("\n\n<<< %s >>>\n\n",argv [i]);
  112. X        if (! (f = fopen (argv [i],"r"))) {
  113. X            fprintf (stderr,"Error: Couldn't open file %s\n",argv [i]);
  114. X        exit (1);
  115. X        }
  116. X            process (f);
  117. X            fclose (f);
  118. X        }
  119. X}
  120. X
  121. X    
  122. X
  123. Xprocess (f)
  124. X    FILE *f;
  125. X
  126. X{
  127. X    unsigned char ch; /* Must be unsigned to handle extended ASCII codes */
  128. X       
  129. X    ch = 0; 
  130. X    while (ch != CTRL_Z && ! feof (f)) {
  131. X        ch = getc (f);
  132. X        if (ch != CR && ch != CTRL_Z) {
  133. X        if (! pass_ctrl && ch < ' ' && ch != LF && ch != TAB) 
  134. X         printf ("^%c",ch + 'A' - 1);
  135. X        else if (! pass_ext && ch > 126) 
  136. X            if (ext_dot)
  137. X            putchar ('.');
  138. X        else
  139. X                printf ("(%d)",ch);
  140. X            else
  141. X        putchar (ch);
  142. X        }
  143. X    }
  144. X}
  145. SHAR_EOF
  146. chmod 0644 pc2unix.c || echo "restore of pc2unix.c fails"
  147. sed 's/^X//' << 'SHAR_EOF' > unix2pc.c &&
  148. X/******************************************
  149. X ***            unix2pc.c               ***
  150. X ***                    ***
  151. X ***   A filter to convert text files   ***
  152. X ***     from Unix to MSDOS format.     ***
  153. X ***      (to be run under Unix)        ***
  154. X ***                    ***
  155. X ***      Public domain software    ***
  156. X ***                    ***
  157. X ***            Written by         ***
  158. X *** Magnus Olsson (magnus@thep.lu.se)    ***
  159. X ***          July 11, 1990        ***
  160. X ******************************************/
  161. X
  162. X/* 
  163. X   Switches: 
  164. X   -c: Pass ^Z through unchanged
  165. X   -h: Put a header in front of each input file (not stdin)
  166. X*/
  167. X
  168. X#include <stdio.h>
  169. X
  170. X#define CTRL_Z 26
  171. X#define CR  13
  172. X#define LF  10
  173. X
  174. X#define TRUE 1
  175. X#define FALSE 0
  176. X
  177. Xint pass_ctrl = FALSE,
  178. X    header    = FALSE;
  179. X
  180. X
  181. Xmain (argc,argv)
  182. X    int argc;
  183. X    char **argv;
  184. X    
  185. X{
  186. X    int i,arg;
  187. X    
  188. X    FILE *f;
  189. X
  190. X    arg = 0;
  191. X    /* Look fo switches */
  192. X    while (++arg < argc) {
  193. X    if (argv [arg][0] == '-') /* Was it a switch? */
  194. X        switch (argv [arg][1]) {
  195. X        case 'c': pass_ctrl = TRUE; break;
  196. X        case 'h': header = TRUE; break;
  197. X        default:  fprintf (stderr,"Error: Bad switch %s\n",argv [arg]);
  198. X              exit (1);
  199. X        }
  200. X    else
  201. X        break;
  202. X    }        
  203. X        
  204. X    if (arg >= argc) /* No filenames as parameters */
  205. X        process (stdin);
  206. X    else    
  207. X        for (i = arg; i < argc; ++i) {
  208. X        if (header)
  209. X                printf ("\n\n<<< %s >>>\n\n",argv [i]);
  210. X        if (! (f = fopen (argv [i],"r"))) {
  211. X            fprintf (stderr,"Error: Couldn't open file %s\n",argv [i]);
  212. X        exit (1);
  213. X        }
  214. X            process (f);
  215. X            fclose (f);
  216. X        }
  217. X}
  218. X
  219. X    
  220. X
  221. Xprocess (f)
  222. X    FILE *f;
  223. X
  224. X{
  225. X    int ch; 
  226. X       
  227. X    ch = 0; 
  228. X    while ((ch = getc (f)) > -1) {
  229. X        switch (ch) {
  230. X        case CTRL_Z: if (pass_ctrl) 
  231. X                     putchar (ch);
  232. X             else
  233. X                 printf ("^Z");
  234. X        case LF:     putchar (CR);
  235. X        default:     putchar (ch);
  236. X    }
  237. X    }
  238. X    putchar (CTRL_Z); /* Add EOF marker */
  239. X}
  240. SHAR_EOF
  241. chmod 0644 unix2pc.c || echo "restore of unix2pc.c fails"
  242. sed 's/^X//' << 'SHAR_EOF' > pc2unix.doc &&
  243. Xpc2unix and unix2pc --- filters for text file conversion Unix <--> MSDOS
  244. X
  245. XBy Magnus Olsson (magnus@thep.lu.se)
  246. X
  247. X===============================================================================
  248. X
  249. XThese two small filters are useful if you, like I do, transfer text files
  250. Xbetween PC's and Unix machines (for example, the documentation for
  251. XComp.sources.ibm.pc programs). 
  252. X
  253. XAs is well known, MSDOS and Unix use differnet formats for text files. Under
  254. XUnix, each line ends with a newline character (ASCII 10) while under MSDOS,
  255. Xit ends in a carriage return (ASCII 13) + a line feed. MSDOS uses a special
  256. Xcharacter (ASCII 26) as an end of file marker, while Unix doesn't. Also, MSDOS
  257. Xuses an extended character set (ASCII codes > 127) which most Unixes can't
  258. Xhandle). 
  259. X
  260. XNormally, your transfer program (ftp, kermit etc) will automatically convert
  261. Xtext files to the correct format, but sometimes they don't, for example if the
  262. Xtext file is part of a .ARC file which is then unpacked under the 'wrong'
  263. Xoperating system. In that case, you may need these filters.
  264. X
  265. XHow to install them:
  266. X===================
  267. X
  268. XJust do 
  269. Xcc -o unix2pc unix2pc.c
  270. Xand
  271. Xcc -o pc2unix pc2unix.c
  272. X
  273. Xand copy the executables to somewhere in your path.
  274. X
  275. X
  276. XHow to use them:
  277. X===============
  278. X
  279. XIf you don't give these commands any parameters, they will act as filters,
  280. Xreading their input from stdin and writing to stdout. If you specify one or
  281. Xmore filenames on the command line, input will be read from the file(s) and
  282. Xthe converted text will be written on stdout (the files will be concatenated). 
  283. X
  284. XYou may also specify one or more of the following switches (*before* any
  285. Xfilenames):
  286. X
  287. XFor pc2unix:
  288. X
  289. X-c: Changes the way control characters are treated. If this switch is
  290. X    specified, all control characters will be passed through the filter. The
  291. X    default is to replace all control characters expcept tabs and line feeds
  292. X    with a symbolic representation (like ^C for ASCII 3).
  293. X    
  294. X-d: Replace all extended characters (ASCII > 126) with a dot, instead of
  295. X    replacing them with their character code in parentheses (the default).
  296. X
  297. X-e: Pass all extended characters unchanged through the filter.
  298. X
  299. X-h: Put a header with the file name before each file in the output if files
  300. X    are specified on the command line.
  301. X
  302. X
  303. XFor unix2pc:
  304. X
  305. X-c: Pass all ASCII 26 characters unchanged through the filter. The default
  306. X    is to replace them with the string "^Z". (This is the EOF character in
  307. X    MSDOS).
  308. X
  309. X-h: This switch has the same meaning as for pc2unix.
  310. X
  311. SHAR_EOF
  312. chmod 0644 pc2unix.doc || echo "restore of pc2unix.doc fails"
  313. exit 0
  314.  
  315.  
  316.