File: add_cr.awk

package info (click to toggle)
mawk 1.3.3-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,204 kB
  • ctags: 1,530
  • sloc: ansic: 13,023; yacc: 994; awk: 629; sh: 330; makefile: 164
file content (93 lines) | stat: -rw-r--r-- 1,621 bytes parent folder | download | duplicates (11)
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

# add_cr.awk
# converts from Unix (LF only) text files to
# DOS (CRLF) text files
#
# works on both unix and dos 
# if used on unix change COPY and DEL in BEGIN section
#
# mawk -f add_cr.awk  [files]

# with no files reads stdin writes stdout
# otherwise the original is overwritten
#
# If a file of the form `@file', then arguments are read from
# `file', one per line

#
# To add cr's to the whole distribution
#
# mawk -f doslist.awk packing.lis | mawk "{print $2}" > list
# mawk -f add_cr.awk  @list
#


# read arguments for @file into ARGV[]
function reset_argv(T, i, j, flag, file) #all args local
{
  for( i = 1 ; i < ARGC ; i++ ) 
  {
    T[i] = ARGV[i]
    if ( T[i] ~ /^@/ ) flag = 1
  }

  if ( ! flag )  return

  # need to read from a @file into ARGV
  j = 1
  for( i = 1 ; i < ARGC ; i++ )
  {
    if ( T[i] !~ /^@/ ) ARGV[j++] = T[i]
    else
    {
      T[i] = substr(T[i],2)
      # read arguments from T[i]
      while ( (getline file < T[i]) > 0 ) ARGV[j++] = file
    }
  }
  ARGC = j
}
   
  
BEGIN {
  COPY = "copy"    # unix: "cp"
  DEL = "del"      # unix: "rm"

  tmpfile = ENVIRON["MAWKTMPDIR"] "MAWK.TMP"

  reset_argv()
}


FILENAME == "-" {
   # just write to stdout
   printf "%s\r\n" , $0
   next
}

FILENAME != filename {
   
   if ( filename )
   {
     close(tmpfile)
     syscmd = sprintf( "%s %s %s", COPY, tmpfile, filename )
     system(syscmd)
   }

   filename = FILENAME
}

{ printf "%s\r\n" , $0 > tmpfile }


END {
  if ( filename )  
  {
    close(tmpfile)
    syscmd = sprintf( "%s %s %s", COPY, tmpfile, filename )
    system(syscmd)
    system(DEL " " tmpfile)
  }
}